#8 checkout on first head.

This commit is contained in:
Gorbunov Kirill Andreevich
2024-04-09 00:04:57 +03:00
parent 2309bba07e
commit c0887adf99
3 changed files with 36 additions and 10 deletions

View File

@@ -34,6 +34,7 @@ type Github struct {
Repository string
Owner string
pulls []*githubPull
baseRef *plumbing.Reference
}
func NewGithub(cfg configcli.Config) *Github {
@@ -111,6 +112,14 @@ func (g *Github) RequestOpen(ctx context.Context, branch string, path string, mo
} //обновляем репозиторий
var headRef *plumbing.Reference // вроде ссылка на гит
if g.baseRef == nil {
g.baseRef, err = repo.Head()
if err != nil {
logger.Fatal(ctx, fmt.Sprintf("Error head: %s", err))
}
}
refIter, err := repo.Branches() //получение веток
if err != nil {
logger.Fatal(ctx, fmt.Sprintf("failed to get branches: %v", err))
@@ -139,6 +148,7 @@ func (g *Github) RequestOpen(ctx context.Context, branch string, path string, mo
if err != nil {
logger.Fatal(ctx, fmt.Sprintf("failed to get worktree: %v", err))
}
defer checkout(wtree, *g.baseRef)
g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password)
if err != nil {
@@ -365,3 +375,12 @@ func GetPulls(ctx context.Context, url, owner, repo, password string) ([]*github
return pullsAll, nil
}
func checkout(w *git.Worktree, ref plumbing.Reference) {
ctx := context.Background()
logger.Debug(ctx, fmt.Sprintf("Checkout: %s", ref.Name().Short()))
if err := w.Checkout(&git.CheckoutOptions{Hash: ref.Hash()}); err != nil {
logger.Fatal(ctx, fmt.Sprintf("failed to reset: %v", err))
}
}