feat: checkout the repository worktree with go-git instead of invoking the git command

This commit is contained in:
Himadri Bhattacharjee 2023-12-16 19:02:45 +05:30 committed by maia arson crimew
parent a02feedb42
commit e59a558dcd
1 changed files with 18 additions and 4 deletions

View File

@ -8,7 +8,6 @@ import (
"io/ioutil" "io/ioutil"
"net/url" "net/url"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
@ -17,6 +16,7 @@ import (
"github.com/deletescape/goop/internal/workers" "github.com/deletescape/goop/internal/workers"
"github.com/deletescape/jobtracker" "github.com/deletescape/jobtracker"
"github.com/go-git/go-billy/v5/osfs" "github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/cache" "github.com/go-git/go-git/v5/plumbing/cache"
"github.com/go-git/go-git/v5/plumbing/format/commitgraph" "github.com/go-git/go-git/v5/plumbing/format/commitgraph"
@ -425,9 +425,23 @@ func FetchGit(baseUrl, baseDir string) error {
func checkout(baseDir string) error { func checkout(baseDir string) error {
log.Info().Str("dir", baseDir).Msg("running git checkout .") log.Info().Str("dir", baseDir).Msg("running git checkout .")
cmd := exec.Command("git", "checkout", ".")
cmd.Dir = baseDir log.Info().Str("dir", baseDir).Msg("opening the current repository")
return cmd.Run()
repo, err := git.PlainOpen(baseDir)
if err != nil {
return err
}
log.Info().Str("dir", baseDir).Msg("switching to repo worktree")
worktree, err := repo.Worktree()
if err != nil {
return err
}
log.Info().Str("dir", baseDir).Msg("checking out current worktree")
return worktree.Checkout(&git.CheckoutOptions{})
} }
func fetchLfs(baseDir, baseUrl string) { func fetchLfs(baseDir, baseUrl string) {