Skip to content

Commit a386eef

Browse files
committed
cmd/gg: verify -dst argument in rebase
Fixes #127
1 parent 7267372 commit a386eef

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ The format is based on [Keep a Changelog][], and this project adheres to
3434
([#121](https://github.com/gg-scm/gg/issues/121))
3535
- `commit --amend` no longer exits with an error if the commit contains a
3636
rename. ([#129](https://github.com/gg-scm/gg/issues/129))
37+
- `rebase` displays a simpler error message if the `-dst` argument doesn't
38+
exist. ([#127](https://github.com/gg-scm/gg/issues/127))
3739

3840
## [1.0.3][] - 2020-09-07
3941

cmd/gg/rebase.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@ import (
2828
const rebaseSynopsis = "move revision (and descendants) to a different branch"
2929

3030
func rebase(ctx context.Context, cc *cmdContext, args []string) error {
31+
const upstreamRev = "@{upstream}"
3132
f := flag.NewFlagSet(true, "gg rebase [--src REV | --base REV] [--dst REV] [options]", rebaseSynopsis+`
3233
3334
Rebasing will replay a set of changes on top of the destination
3435
revision and set the current branch to the final revision.
3536
3637
If neither `+"`--src`"+` or `+"`--base`"+` is specified, it acts as if
37-
`+"`--base=@{upstream}`"+` was specified.`)
38+
`+"`--base="+upstreamRev+"`"+` was specified.`)
3839
base := f.String("base", "", "rebase everything from branching point of specified `rev`ision")
39-
dst := f.String("dst", "@{upstream}", "rebase onto the specified `rev`ision")
40+
dst := f.String("dst", upstreamRev, "rebase onto the specified `rev`ision")
4041
src := f.String("src", "", "rebase the specified `rev`ision and descendants")
4142
abort := f.Bool("abort", false, "abort an interrupted rebase")
4243
continue_ := f.Bool("continue", false, "continue an interrupted rebase")
@@ -52,7 +53,7 @@ func rebase(ctx context.Context, cc *cmdContext, args []string) error {
5253
if *abort && *continue_ {
5354
return usagef("can't specify both --abort and --continue")
5455
}
55-
if (*abort || *continue_) && (*base != "" || *dst != "@{upstream}" || *src != "") {
56+
if (*abort || *continue_) && (*base != "" || *dst != upstreamRev || *src != "") {
5657
return usagef("can't specify other options with --abort or --continue")
5758
}
5859
if *abort {
@@ -61,6 +62,11 @@ func rebase(ctx context.Context, cc *cmdContext, args []string) error {
6162
if *continue_ {
6263
return continueRebase(ctx, cc)
6364
}
65+
// Verify that -dst exists to give the user a better error message.
66+
// See https://github.com/gg-scm/gg/issues/127
67+
if _, err := cc.git.ParseRev(ctx, *dst); err != nil {
68+
return fmt.Errorf("destination: %w", err)
69+
}
6470
switch {
6571
case *base != "" && *src != "":
6672
return usagef("can't specify both -s and -b")

cmd/gg/rebase_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,30 @@ func TestRebase_ResetUpstream(t *testing.T) {
569569
})
570570
}
571571

572+
func TestRebase_NoUpstream(t *testing.T) {
573+
// Regression test for https://github.com/gg-scm/gg/issues/127
574+
575+
t.Parallel()
576+
ctx := context.Background()
577+
env, err := newTestEnv(ctx, t)
578+
if err != nil {
579+
t.Fatal(err)
580+
}
581+
582+
if err := env.initRepoWithHistory(ctx, "."); err != nil {
583+
t.Fatal(err)
584+
}
585+
586+
// Call gg rebase.
587+
stdout, err := env.gg(ctx, env.root.String(), "rebase")
588+
if err == nil {
589+
t.Error("gg succeeded even though it should have returned an error")
590+
} else {
591+
t.Log(err)
592+
t.Logf("%s", stdout)
593+
}
594+
}
595+
572596
func TestHistedit(t *testing.T) {
573597
t.Parallel()
574598
runRebaseArgVariants(t, func(t *testing.T, argFunc rebaseArgFunc) {
@@ -1001,6 +1025,30 @@ func TestHistedit_ContinueNoModifications(t *testing.T) {
10011025
})
10021026
}
10031027

1028+
func TestHistedit_NoUpstream(t *testing.T) {
1029+
// Regression test for https://github.com/gg-scm/gg/issues/127
1030+
1031+
t.Parallel()
1032+
ctx := context.Background()
1033+
env, err := newTestEnv(ctx, t)
1034+
if err != nil {
1035+
t.Fatal(err)
1036+
}
1037+
1038+
if err := env.initRepoWithHistory(ctx, "."); err != nil {
1039+
t.Fatal(err)
1040+
}
1041+
1042+
// Call gg histedit.
1043+
stdout, err := env.gg(ctx, env.root.String(), "histedit")
1044+
if err == nil {
1045+
t.Error("gg succeeded even though it should have returned an error")
1046+
} else {
1047+
t.Log(err)
1048+
t.Logf("%s", stdout)
1049+
}
1050+
}
1051+
10041052
type rebaseArgFunc = func(mainCommit git.Hash) string
10051053

10061054
func runRebaseArgVariants(t *testing.T, f func(*testing.T, rebaseArgFunc)) {

0 commit comments

Comments
 (0)