From c0887adf996c79b8fd4681ce184504708e413b05 Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Tue, 9 Apr 2024 00:04:57 +0300 Subject: [PATCH] #8 checkout on first head. --- internal/source/gitea/gitea.go | 14 +++++++++----- internal/source/github/github.go | 19 +++++++++++++++++++ internal/source/gitlab/gitlab.go | 13 ++++++++----- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/internal/source/gitea/gitea.go b/internal/source/gitea/gitea.go index 01c843a..99c96e3 100644 --- a/internal/source/gitea/gitea.go +++ b/internal/source/gitea/gitea.go @@ -35,6 +35,7 @@ type Gitea struct { Repository string Owner string pulls []*giteaPull + baseRef *plumbing.Reference } func NewGitea(cfg configcli.Config) *Gitea { @@ -111,12 +112,15 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod logger.Fatal(ctx, fmt.Sprintf("failed to fetch repo : %v", err)) } //обновляем репозиторий - var headRef, baseRef *plumbing.Reference // вроде ссылка на гит + var headRef *plumbing.Reference // вроде ссылка на гит - baseRef, err = repo.Head() - if err != nil { - logger.Fatal(ctx, fmt.Sprintf("Error head: %s", err)) + 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)) @@ -145,7 +149,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod if err != nil { logger.Fatal(ctx, fmt.Sprintf("failed to get worktree: %v", err)) } - defer checkout(wtree, *baseRef) + defer checkout(wtree, *g.baseRef) g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password) if err != nil { diff --git a/internal/source/github/github.go b/internal/source/github/github.go index ad796cc..9c15271 100644 --- a/internal/source/github/github.go +++ b/internal/source/github/github.go @@ -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)) + } +} diff --git a/internal/source/gitlab/gitlab.go b/internal/source/gitlab/gitlab.go index 07263a5..4b1898c 100644 --- a/internal/source/gitlab/gitlab.go +++ b/internal/source/gitlab/gitlab.go @@ -36,6 +36,7 @@ type Gitlab struct { RepositoryId string Owner string pulls []*gitlabPull + baseRef *plumbing.Reference } func NewGitlab(cfg configcli.Config) *Gitlab { @@ -108,11 +109,13 @@ func (g *Gitlab) RequestOpen(ctx context.Context, branch string, path string, mo logger.Fatal(ctx, fmt.Sprintf("failed to fetch repo : %v", err)) } //обновляем репозиторий - var headRef, baseRef *plumbing.Reference // вроде ссылка на гит + var headRef *plumbing.Reference // вроде ссылка на гит - baseRef, err = repo.Head() - if err != nil { - logger.Error(ctx, fmt.Sprintf("Error head: %s", err)) + 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() //получение веток @@ -142,7 +145,7 @@ func (g *Gitlab) 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, *baseRef) + defer checkout(wtree, *g.baseRef) g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password) if err != nil {