Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cooper-oh committed Dec 16, 2024
1 parent a7e227e commit 4913ad1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
4 changes: 3 additions & 1 deletion cmd/cherry-pick/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

"github.com/134130/gh-cherry-pick/git"
"github.com/134130/gh-cherry-pick/internal/tui"
)

var (
Expand All @@ -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)
}
}
33 changes: 15 additions & 18 deletions git/cherry-pick.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package git
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"time"
Expand All @@ -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())
Expand Down Expand Up @@ -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())
}
Expand Down
3 changes: 2 additions & 1 deletion internal/tui/spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

var (
cyan = color.New(color.FgCyan).SprintFunc()
green = color.New(color.FgGreen).SprintFunc()
red = color.New(color.FgRed).SprintFunc()
)
Expand All @@ -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()

Expand Down

0 comments on commit 4913ad1

Please sign in to comment.