2023-04-02 18:39:31 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
//"fmt"
|
2023-04-12 09:48:12 +00:00
|
|
|
|
2023-04-02 18:39:31 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
git "github.com/go-git/go-git/v5"
|
|
|
|
)
|
|
|
|
|
|
|
|
func cloneRepository(repoURL, localPath string) error {
|
|
|
|
_, err := git.PlainClone(localPath, false, &git.CloneOptions{
|
|
|
|
URL: repoURL,
|
|
|
|
Progress: os.Stdout,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func pullRepository(localPath string) error {
|
|
|
|
repo, err := git.PlainOpen(localPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
worktree, err := repo.Worktree()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = worktree.Pull(&git.PullOptions{RemoteName: "origin"})
|
|
|
|
if err != nil && err != git.NoErrAlreadyUpToDate {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 09:48:12 +00:00
|
|
|
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()
|
2023-04-02 18:39:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-12 09:48:12 +00:00
|
|
|
|
|
|
|
// 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-02 18:39:31 +00:00
|
|
|
}
|