From 4913ad12c039e43af94c1065b42af2738f7073f2 Mon Sep 17 00:00:00 2001 From: Cooper Oh Date: Mon, 16 Dec 2024 22:28:22 +0900 Subject: [PATCH] fix --- cmd/cherry-pick/main.go | 4 +++- git/cherry-pick.go | 33 +++++++++++++++------------------ internal/tui/spinner.go | 3 ++- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/cmd/cherry-pick/main.go b/cmd/cherry-pick/main.go index d8de275..9a049c4 100644 --- a/cmd/cherry-pick/main.go +++ b/cmd/cherry-pick/main.go @@ -6,6 +6,7 @@ import ( "os" "github.com/134130/gh-cherry-pick/git" + "github.com/134130/gh-cherry-pick/internal/tui" ) var ( @@ -29,6 +30,7 @@ func main() { ctx := context.Background() if err := cherryPick.RunWithContext(ctx); err != nil { - panic(err) + tui.PrintError(err.Error()) + os.Exit(1) } } diff --git a/git/cherry-pick.go b/git/cherry-pick.go index 51b0e40..f3b8f07 100644 --- a/git/cherry-pick.go +++ b/git/cherry-pick.go @@ -3,7 +3,6 @@ package git import ( "context" "fmt" - "os" "strconv" "strings" "time" @@ -21,23 +20,25 @@ type CherryPick struct { func (cherryPick *CherryPick) RunWithContext(ctx context.Context) error { if dirty, err := IsDirty(ctx); err != nil { - if _, err = fmt.Fprintf(os.Stderr, "error checking if the repository is dirty: %v", err); err != nil { - return err - } + return fmt.Errorf("error checking if the repository is dirty: %w", err) } else if dirty { - if _, err = fmt.Fprintf(os.Stderr, "the repository is dirty. Please commit your changes before continuing"); err != nil { - return err - } + return fmt.Errorf("the repository is dirty. please commit your changes before continuing") } if rebaseOrAm, err := IsInRebaseOrAm(ctx); err != nil { - if _, err = fmt.Fprintf(os.Stderr, "error checking if the repository is in a rebase or am: %v", err); err != nil { - return err - } + return fmt.Errorf("error checking if the repository is in a rebase or am: %w", err) } else if rebaseOrAm { - if _, err = fmt.Fprintf(os.Stderr, "the repository is in a rebase or am. Please resolve the rebase or am before continuing"); err != nil { - return err - } + return fmt.Errorf("the repository is in a rebase or am. please resolve the rebase or am before continuing") + } + + stdout, stderr, err := ExecContext(ctx, "gh", "pr", "view", strconv.Itoa(cherryPick.PRNumber), "--json", "state", "--jq", ".state") + if err != nil { + return fmt.Errorf("error getting PR #%d: %w: %s", cherryPick.PRNumber, err, stderr.String()) + } + + state := strings.TrimSpace(stdout.String()) + if state != "MERGED" { + return fmt.Errorf("PR #%d is not merged (current state: %s). please ensure the PR is merged before continuing", cherryPick.PRNumber, state) } var cherryPickBranchName = fmt.Sprintf("cherry-pick-pr-%d-onto-%s-%d", cherryPick.PRNumber, cherryPick.OnTo, time.Now().Unix()) @@ -73,16 +74,12 @@ func (cherryPick *CherryPick) RunWithContext(ctx context.Context) error { }) } else { tui.WithSpinner(ctx, fmt.Sprintf("Cherry-picking branch %s onto %s", cherryPickBranchName, cherryPick.OnTo), func(ctx context.Context) (string, error) { - stdout, stderr, err := ExecContext(ctx, "gh", "pr", "view", strconv.Itoa(cherryPick.PRNumber), "--json", "--mergeCommit", "--jq", ".mergeCommit.oid") + stdout, stderr, err = ExecContext(ctx, "gh", "pr", "view", strconv.Itoa(cherryPick.PRNumber), "--json", "mergeCommit", "--jq", ".mergeCommit.oid") if err != nil { return "", fmt.Errorf("error getting PR merge commit: %w: %s", err, stderr.String()) } mergeCommit := strings.TrimSpace(stdout.String()) - if len(mergeCommit) == 0 { - return "", fmt.Errorf("error getting PR merge commit: please ensure the PR has been merged") - } - if _, stderr, err = ExecContext(ctx, "git", "cherry-pick", "--keep-redundant-commits", mergeCommit); err != nil { return "", fmt.Errorf("error cherry-picking PR merge commit: %w: %s", err, stderr.String()) } diff --git a/internal/tui/spinner.go b/internal/tui/spinner.go index b6754a6..d34e78f 100644 --- a/internal/tui/spinner.go +++ b/internal/tui/spinner.go @@ -10,6 +10,7 @@ import ( ) var ( + cyan = color.New(color.FgCyan).SprintFunc() green = color.New(color.FgGreen).SprintFunc() red = color.New(color.FgRed).SprintFunc() ) @@ -23,7 +24,7 @@ func PrintError(message string) { } func WithSpinner(ctx context.Context, message string, f func(ctx context.Context) (string, error)) { - sp := spinner.New(spinner.CharSets[14], 40*time.Millisecond) + sp := spinner.New(spinner.CharSets[14], 40*time.Millisecond, spinner.WithColor("cyan")) sp.Suffix = " " + message sp.Start()