From a5d39c23f8f30629f696ff43e19b89b0932c83fe Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Sun, 21 Apr 2024 15:46:14 +0300 Subject: [PATCH] #8 fix checkout. --- internal/source/gitea/gitea.go | 13 +++++++++---- internal/source/github/github.go | 13 +++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/internal/source/gitea/gitea.go b/internal/source/gitea/gitea.go index 99c96e3..ca32dbe 100644 --- a/internal/source/gitea/gitea.go +++ b/internal/source/gitea/gitea.go @@ -149,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, *g.baseRef) + defer checkout(*wtree, *g.baseRef) g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password) if err != nil { @@ -473,11 +473,16 @@ func GetPulls(ctx context.Context, url, owner, repo, password string) ([]*giteaP return pullsAll, nil } -func checkout(w *git.Worktree, ref plumbing.Reference) { +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)) + if err := w.Checkout(&git.CheckoutOptions{ + Branch: ref.Name(), + Create: false, + Force: true, + Keep: false, + }); err != nil { + logger.Error(ctx, fmt.Sprintf("failed to reset: %v", err)) } } diff --git a/internal/source/github/github.go b/internal/source/github/github.go index 9c15271..972457e 100644 --- a/internal/source/github/github.go +++ b/internal/source/github/github.go @@ -148,7 +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) + defer checkout(*wtree, *g.baseRef) g.pulls, err = GetPulls(ctx, g.URL, g.Owner, g.Repository, g.Password) if err != nil { @@ -376,11 +376,16 @@ func GetPulls(ctx context.Context, url, owner, repo, password string) ([]*github return pullsAll, nil } -func checkout(w *git.Worktree, ref plumbing.Reference) { +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)) + if err := w.Checkout(&git.CheckoutOptions{ + Branch: ref.Name(), + Create: false, + Force: true, + Keep: false, + }); err != nil { + logger.Error(ctx, fmt.Sprintf("failed to reset: %v", err)) } }