2023-04-12 12:26:32 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
//"fmt"
|
|
|
|
|
|
|
|
"os"
|
2023-04-13 01:35:10 +00:00
|
|
|
"time"
|
2023-04-12 12:26:32 +00:00
|
|
|
|
|
|
|
git "github.com/go-git/go-git/v5"
|
2023-04-12 22:22:36 +00:00
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
2023-04-13 01:35:10 +00:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
2023-04-12 12:26:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func cloneRepository(repoURL, localPath string) error {
|
|
|
|
_, err := git.PlainClone(localPath, false, &git.CloneOptions{
|
|
|
|
URL: repoURL,
|
|
|
|
Progress: os.Stdout,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-12 22:22:36 +00:00
|
|
|
func pullRepository(localPath, branch string) error {
|
2023-04-12 12:26:32 +00:00
|
|
|
repo, err := git.PlainOpen(localPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
worktree, err := repo.Worktree()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-12 22:22:36 +00:00
|
|
|
err = worktree.Pull(&git.PullOptions{
|
|
|
|
RemoteName: "origin",
|
|
|
|
ReferenceName: plumbing.NewBranchReferenceName(branch),
|
|
|
|
})
|
2023-04-12 12:26:32 +00:00
|
|
|
if err != nil && err != git.NoErrAlreadyUpToDate {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readFileFromRepo(localPath string, filePath string) ([]byte, error) {
|
|
|
|
// Open the local repository
|
|
|
|
repo, err := git.PlainOpen(localPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the head reference
|
|
|
|
ref, err := repo.Head()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the commit object
|
|
|
|
commit, err := repo.CommitObject(ref.Hash())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the file contents from the commit tree
|
|
|
|
tree, err := commit.Tree()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := tree.File(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := file.Contents()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return []byte(content), nil
|
|
|
|
}
|
2023-04-13 01:35:10 +00:00
|
|
|
|
|
|
|
func getAuthorAndLastModification(localPath, filePath string) (string, time.Time, string, time.Time, error) {
|
|
|
|
repo, err := git.PlainOpen(localPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", time.Time{}, "", time.Time{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
iter, err := repo.Log(&git.LogOptions{FileName: &filePath})
|
|
|
|
if err != nil {
|
|
|
|
return "", time.Time{}, "", time.Time{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var firstCommit *object.Commit
|
|
|
|
var lastCommit *object.Commit
|
|
|
|
|
|
|
|
err = iter.ForEach(func(c *object.Commit) error {
|
|
|
|
if firstCommit == nil {
|
|
|
|
firstCommit = c
|
|
|
|
}
|
|
|
|
lastCommit = c
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", time.Time{}, "", time.Time{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return firstCommit.Author.Name, firstCommit.Author.When, lastCommit.Author.Name, lastCommit.Author.When, nil
|
|
|
|
}
|