Skip to content

Commit 2e92085

Browse files
committed
github#96: rebase --preserve-dates for stable SHAs
Add a RebaseOpts struct threaded through git.Rebase/RebaseOnto/RebaseContinue (and the internal rerere auto-continue helpers). The `rebase` command gains --committer-date-is-author-date (alias --preserve-dates): when set, identical content rebased onto an identical parent keeps stable commit SHAs, cutting force-push churn. The flag is persisted in rebase state so --continue honors it. sync passes RebaseOpts{} (default). All rebase mocks updated to the new signatures.
1 parent 655d85a commit 2e92085

7 files changed

Lines changed: 196 additions & 109 deletions

File tree

‎cmd/rebase.go‎

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,24 @@ import (
1616
)
1717

1818
type rebaseOptions struct {
19-
branch string
20-
downstack bool
21-
upstack bool
22-
cont bool
23-
abort bool
24-
remote string
19+
branch string
20+
downstack bool
21+
upstack bool
22+
cont bool
23+
abort bool
24+
remote string
25+
committerDateIsAuthorDate bool
2526
}
2627

2728
type rebaseState struct {
28-
CurrentBranchIndex int `json:"currentBranchIndex"`
29-
ConflictBranch string `json:"conflictBranch"`
30-
RemainingBranches []string `json:"remainingBranches"`
31-
OriginalBranch string `json:"originalBranch"`
32-
OriginalRefs map[string]string `json:"originalRefs"`
33-
UseOnto bool `json:"useOnto,omitempty"`
34-
OntoOldBase string `json:"ontoOldBase,omitempty"`
29+
CurrentBranchIndex int `json:"currentBranchIndex"`
30+
ConflictBranch string `json:"conflictBranch"`
31+
RemainingBranches []string `json:"remainingBranches"`
32+
OriginalBranch string `json:"originalBranch"`
33+
OriginalRefs map[string]string `json:"originalRefs"`
34+
UseOnto bool `json:"useOnto,omitempty"`
35+
OntoOldBase string `json:"ontoOldBase,omitempty"`
36+
CommitterDateIsAuthorDate bool `json:"committerDateIsAuthorDate,omitempty"`
3537
}
3638

3739
const rebaseStateFile = "gh-stack-rebase-state"
@@ -60,6 +62,8 @@ layer in its commit history, rebasing if necessary.`,
6062
cmd.Flags().BoolVar(&opts.cont, "continue", false, "Continue rebase after resolving conflicts")
6163
cmd.Flags().BoolVar(&opts.abort, "abort", false, "Abort rebase and restore all branches")
6264
cmd.Flags().StringVar(&opts.remote, "remote", "", "Remote to fetch from (defaults to auto-detected remote)")
65+
cmd.Flags().BoolVar(&opts.committerDateIsAuthorDate, "committer-date-is-author-date", false, "Preserve author dates so unchanged commits keep stable SHAs (reduces force-push churn)")
66+
cmd.Flags().BoolVar(&opts.committerDateIsAuthorDate, "preserve-dates", false, "Alias for --committer-date-is-author-date")
6367

6468
return cmd
6569
}
@@ -222,6 +226,8 @@ func runRebase(cfg *config.Config, opts *rebaseOptions) error {
222226
}
223227
}
224228

229+
rebaseOpts := git.RebaseOpts{CommitterDateIsAuthorDate: opts.committerDateIsAuthorDate}
230+
225231
// Track --onto rebase state for merged branches.
226232
needsOnto := false
227233
var ontoOldBase string
@@ -287,7 +293,7 @@ func runRebase(cfg *config.Config, opts *rebaseOptions) error {
287293
}
288294
}
289295

290-
if err := git.RebaseOnto(newBase, actualOldBase, br.Branch); err != nil {
296+
if err := git.RebaseOnto(newBase, actualOldBase, br.Branch, rebaseOpts); err != nil {
291297
cfg.Warningf("Rebasing %s onto %s — conflict", br.Branch, newBase)
292298

293299
remaining := make([]string, 0)
@@ -296,13 +302,14 @@ func runRebase(cfg *config.Config, opts *rebaseOptions) error {
296302
}
297303

298304
state := &rebaseState{
299-
CurrentBranchIndex: absIdx,
300-
ConflictBranch: br.Branch,
301-
RemainingBranches: remaining,
302-
OriginalBranch: currentBranch,
303-
OriginalRefs: originalRefs,
304-
UseOnto: true,
305-
OntoOldBase: originalRefs[br.Branch],
305+
CurrentBranchIndex: absIdx,
306+
ConflictBranch: br.Branch,
307+
RemainingBranches: remaining,
308+
OriginalBranch: currentBranch,
309+
OriginalRefs: originalRefs,
310+
UseOnto: true,
311+
OntoOldBase: originalRefs[br.Branch],
312+
CommitterDateIsAuthorDate: opts.committerDateIsAuthorDate,
306313
}
307314
if err := saveRebaseState(gitDir, state); err != nil {
308315
cfg.Warningf("failed to save rebase state: %s", err)
@@ -338,13 +345,13 @@ func runRebase(cfg *config.Config, opts *rebaseOptions) error {
338345
if mbErr != nil {
339346
return fmt.Errorf("resolving fork point for %s: %w", br.Branch, mbErr)
340347
}
341-
rebaseErr = git.RebaseOnto(base, forkPoint, br.Branch)
348+
rebaseErr = git.RebaseOnto(base, forkPoint, br.Branch, rebaseOpts)
342349
} else {
343350
if err := git.CheckoutBranch(br.Branch); err != nil {
344351
return fmt.Errorf("checking out %s: %w", br.Branch, err)
345352
}
346353
// Use regular rebase for the first branch.
347-
rebaseErr = git.Rebase(base)
354+
rebaseErr = git.Rebase(base, rebaseOpts)
348355
}
349356

350357
if rebaseErr != nil {
@@ -356,11 +363,12 @@ func runRebase(cfg *config.Config, opts *rebaseOptions) error {
356363
}
357364

358365
state := &rebaseState{
359-
CurrentBranchIndex: absIdx,
360-
ConflictBranch: br.Branch,
361-
RemainingBranches: remaining,
362-
OriginalBranch: currentBranch,
363-
OriginalRefs: originalRefs,
366+
CurrentBranchIndex: absIdx,
367+
ConflictBranch: br.Branch,
368+
RemainingBranches: remaining,
369+
OriginalBranch: currentBranch,
370+
OriginalRefs: originalRefs,
371+
CommitterDateIsAuthorDate: opts.committerDateIsAuthorDate,
364372
}
365373
if err := saveRebaseState(gitDir, state); err != nil {
366374
cfg.Warningf("failed to save rebase state: %s", err)
@@ -444,8 +452,10 @@ func continueRebase(cfg *config.Config, gitDir string) error {
444452
cfg.Printf("Continuing rebase of stack, resuming from %s to %s",
445453
conflictBranch, s.Branches[len(s.Branches)-1].Branch)
446454

455+
rebaseOpts := git.RebaseOpts{CommitterDateIsAuthorDate: state.CommitterDateIsAuthorDate}
456+
447457
if git.IsRebaseInProgress() {
448-
if err := git.RebaseContinue(); err != nil {
458+
if err := git.RebaseContinue(rebaseOpts); err != nil {
449459
return fmt.Errorf("rebase continue failed — resolve remaining conflicts and try again: %w", err)
450460
}
451461
}
@@ -508,7 +518,7 @@ func continueRebase(cfg *config.Config, gitDir string) error {
508518
}
509519
}
510520

511-
if err := git.RebaseOnto(newBase, state.OntoOldBase, branchName); err != nil {
521+
if err := git.RebaseOnto(newBase, state.OntoOldBase, branchName, rebaseOpts); err != nil {
512522
remainIdx := -1
513523
for ri, rb := range state.RemainingBranches {
514524
if rb == branchName {
@@ -548,13 +558,13 @@ func continueRebase(cfg *config.Config, gitDir string) error {
548558
cfg.Errorf("resolving fork point for %s: %s", branchName, mbErr)
549559
return ErrSilent
550560
}
551-
rebaseErr = git.RebaseOnto(base, forkPoint, branchName)
561+
rebaseErr = git.RebaseOnto(base, forkPoint, branchName, rebaseOpts)
552562
} else {
553563
if err := git.CheckoutBranch(branchName); err != nil {
554564
cfg.Errorf("checking out %s: %s", branchName, err)
555565
return ErrSilent
556566
}
557-
rebaseErr = git.Rebase(base)
567+
rebaseErr = git.Rebase(base, rebaseOpts)
558568
}
559569

560570
if rebaseErr != nil {

‎cmd/rebase_test.go‎

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ func TestRebase_CascadeRebase(t *testing.T) {
7575
currentCheckedOut = name
7676
return nil
7777
}
78-
mock.RebaseFn = func(base string) error {
78+
mock.RebaseFn = func(base string, _ git.RebaseOpts) error {
7979
allRebaseCalls = append(allRebaseCalls, rebaseCall{newBase: base, oldBase: "", branch: currentCheckedOut})
8080
return nil
8181
}
82-
mock.RebaseOntoFn = func(newBase, oldBase, branch string) error {
82+
mock.RebaseOntoFn = func(newBase, oldBase, branch string, _ git.RebaseOpts) error {
8383
allRebaseCalls = append(allRebaseCalls, rebaseCall{newBase, oldBase, branch})
8484
return nil
8585
}
@@ -142,7 +142,7 @@ func TestRebase_SquashMergedBranch_UsesOnto(t *testing.T) {
142142
}
143143
return "default-sha", nil
144144
}
145-
mock.RebaseOntoFn = func(newBase, oldBase, branch string) error {
145+
mock.RebaseOntoFn = func(newBase, oldBase, branch string, _ git.RebaseOpts) error {
146146
rebaseCalls = append(rebaseCalls, rebaseCall{newBase, oldBase, branch})
147147
return nil
148148
}
@@ -207,7 +207,7 @@ func TestRebase_OntoPropagatesToSubsequentBranches(t *testing.T) {
207207
}
208208
return "default-sha", nil
209209
}
210-
mock.RebaseOntoFn = func(newBase, oldBase, branch string) error {
210+
mock.RebaseOntoFn = func(newBase, oldBase, branch string, _ git.RebaseOpts) error {
211211
rebaseCalls = append(rebaseCalls, rebaseCall{newBase, oldBase, branch})
212212
return nil
213213
}
@@ -259,8 +259,8 @@ func TestRebase_ConflictSavesState(t *testing.T) {
259259

260260
mock := newRebaseMock(tmpDir, "b1")
261261
mock.CheckoutBranchFn = func(string) error { return nil }
262-
mock.RebaseFn = func(string) error { return nil } // b1 succeeds
263-
mock.RebaseOntoFn = func(newBase, oldBase, branch string) error {
262+
mock.RebaseFn = func(string, git.RebaseOpts) error { return nil } // b1 succeeds
263+
mock.RebaseOntoFn = func(newBase, oldBase, branch string, _ git.RebaseOpts) error {
264264
if branch == "b2" {
265265
return assert.AnError // conflict on b2
266266
}
@@ -416,11 +416,11 @@ func TestRebase_DownstackOnly(t *testing.T) {
416416
currentCheckedOut = name
417417
return nil
418418
}
419-
mock.RebaseFn = func(base string) error {
419+
mock.RebaseFn = func(base string, _ git.RebaseOpts) error {
420420
allRebaseCalls = append(allRebaseCalls, rebaseCall{newBase: base, oldBase: "", branch: currentCheckedOut})
421421
return nil
422422
}
423-
mock.RebaseOntoFn = func(newBase, oldBase, branch string) error {
423+
mock.RebaseOntoFn = func(newBase, oldBase, branch string, _ git.RebaseOpts) error {
424424
allRebaseCalls = append(allRebaseCalls, rebaseCall{newBase, oldBase, branch})
425425
return nil
426426
}
@@ -468,11 +468,11 @@ func TestRebase_UpstackOnly(t *testing.T) {
468468
currentCheckedOut = name
469469
return nil
470470
}
471-
mock.RebaseFn = func(base string) error {
471+
mock.RebaseFn = func(base string, _ git.RebaseOpts) error {
472472
allRebaseCalls = append(allRebaseCalls, rebaseCall{newBase: base, oldBase: "", branch: currentCheckedOut})
473473
return nil
474474
}
475-
mock.RebaseOntoFn = func(newBase, oldBase, branch string) error {
475+
mock.RebaseOntoFn = func(newBase, oldBase, branch string, _ git.RebaseOpts) error {
476476
allRebaseCalls = append(allRebaseCalls, rebaseCall{newBase, oldBase, branch})
477477
return nil
478478
}
@@ -514,7 +514,7 @@ func TestRebase_SkipsMergedBranches(t *testing.T) {
514514
var rebaseCalls []rebaseCall
515515

516516
mock := newRebaseMock(tmpDir, "b2")
517-
mock.RebaseOntoFn = func(newBase, oldBase, branch string) error {
517+
mock.RebaseOntoFn = func(newBase, oldBase, branch string, _ git.RebaseOpts) error {
518518
rebaseCalls = append(rebaseCalls, rebaseCall{newBase, oldBase, branch})
519519
return nil
520520
}
@@ -614,11 +614,11 @@ func TestRebase_Continue_RebasesRemainingBranches(t *testing.T) {
614614

615615
mock := newRebaseMock(tmpDir, "b2")
616616
mock.IsRebaseInProgressFn = func() bool { return true }
617-
mock.RebaseContinueFn = func() error {
617+
mock.RebaseContinueFn = func(git.RebaseOpts) error {
618618
rebaseContinueCalled = true
619619
return nil
620620
}
621-
mock.RebaseOntoFn = func(newBase, oldBase, branch string) error {
621+
mock.RebaseOntoFn = func(newBase, oldBase, branch string, _ git.RebaseOpts) error {
622622
rebaseCalls = append(rebaseCalls, rebaseCall{newBase, oldBase, branch})
623623
return nil
624624
}
@@ -696,7 +696,7 @@ func TestRebase_Continue_OntoMode(t *testing.T) {
696696

697697
mock := newRebaseMock(tmpDir, "b3")
698698
mock.IsRebaseInProgressFn = func() bool { return true }
699-
mock.RebaseContinueFn = func() error {
699+
mock.RebaseContinueFn = func(git.RebaseOpts) error {
700700
rebaseContinueCalled = true
701701
return nil
702702
}
@@ -755,8 +755,8 @@ func TestRebase_Continue_ConflictOnRemaining(t *testing.T) {
755755

756756
mock := newRebaseMock(tmpDir, "b2")
757757
mock.IsRebaseInProgressFn = func() bool { return true }
758-
mock.RebaseContinueFn = func() error { return nil }
759-
mock.RebaseOntoFn = func(newBase, oldBase, branch string) error {
758+
mock.RebaseContinueFn = func(git.RebaseOpts) error { return nil }
759+
mock.RebaseOntoFn = func(newBase, oldBase, branch string, _ git.RebaseOpts) error {
760760
if branch == "b3" {
761761
return assert.AnError // conflict on b3
762762
}
@@ -899,7 +899,7 @@ func TestRebase_SkipsMergedBranchesNotExistingLocally(t *testing.T) {
899899
}
900900
return shas, nil
901901
}
902-
mock.RebaseOntoFn = func(newBase, oldBase, branch string) error {
902+
mock.RebaseOntoFn = func(newBase, oldBase, branch string, _ git.RebaseOpts) error {
903903
rebaseCalls = append(rebaseCalls, rebaseCall{newBase, oldBase, branch})
904904
return nil
905905
}
@@ -946,7 +946,7 @@ func TestRebase_SkipsQueuedBranch(t *testing.T) {
946946

947947
mock := newRebaseMock(tmpDir, "b2")
948948
mock.BranchExistsFn = func(name string) bool { return true }
949-
mock.RebaseOntoFn = func(newBase, oldBase, branch string) error {
949+
mock.RebaseOntoFn = func(newBase, oldBase, branch string, _ git.RebaseOpts) error {
950950
rebaseOntoCalls = append(rebaseOntoCalls, rebaseCall{newBase, oldBase, branch})
951951
return nil
952952
}
@@ -1033,7 +1033,7 @@ func TestRebase_StaleOntoOldBase_FallsBackToMergeBase(t *testing.T) {
10331033
}
10341034
return "default-mergebase", nil
10351035
}
1036-
mock.RebaseOntoFn = func(newBase, oldBase, branch string) error {
1036+
mock.RebaseOntoFn = func(newBase, oldBase, branch string, _ git.RebaseOpts) error {
10371037
rebaseCalls = append(rebaseCalls, rebaseCall{newBase, oldBase, branch})
10381038
return nil
10391039
}
@@ -1086,11 +1086,11 @@ func TestRebase_UpstackWithMergedBranchBelow(t *testing.T) {
10861086
return nil
10871087
}
10881088
mock.BranchExistsFn = func(name string) bool { return true }
1089-
mock.RebaseFn = func(base string) error {
1089+
mock.RebaseFn = func(base string, _ git.RebaseOpts) error {
10901090
allRebaseCalls = append(allRebaseCalls, rebaseCall{newBase: base, oldBase: "", branch: currentCheckedOut})
10911091
return nil
10921092
}
1093-
mock.RebaseOntoFn = func(newBase, oldBase, branch string) error {
1093+
mock.RebaseOntoFn = func(newBase, oldBase, branch string, _ git.RebaseOpts) error {
10941094
allRebaseCalls = append(allRebaseCalls, rebaseCall{newBase, oldBase, branch})
10951095
return nil
10961096
}
@@ -1122,3 +1122,57 @@ func TestRebase_UpstackWithMergedBranchBelow(t *testing.T) {
11221122
assert.Equal(t, "b2", allRebaseCalls[1].newBase, "b3 should be rebased onto b2")
11231123
assert.NotEmpty(t, allRebaseCalls[1].oldBase, "b3 should also use --onto")
11241124
}
1125+
1126+
// TestRebase_PreserveDatesFlag verifies that --preserve-dates (and its alias
1127+
// --committer-date-is-author-date) propagate to every rebase operation, and
1128+
// that the option is off by default.
1129+
func TestRebase_PreserveDatesFlag(t *testing.T) {
1130+
cases := []struct {
1131+
name string
1132+
args []string
1133+
want bool
1134+
}{
1135+
{"default off", nil, false},
1136+
{"preserve-dates", []string{"--preserve-dates"}, true},
1137+
{"committer-date-is-author-date", []string{"--committer-date-is-author-date"}, true},
1138+
}
1139+
1140+
for _, tc := range cases {
1141+
t.Run(tc.name, func(t *testing.T) {
1142+
s := stack.Stack{
1143+
Trunk: stack.BranchRef{Branch: "main"},
1144+
Branches: []stack.BranchRef{{Branch: "b1"}, {Branch: "b2"}},
1145+
}
1146+
tmpDir := t.TempDir()
1147+
writeStackFile(t, tmpDir, s)
1148+
1149+
var seen []git.RebaseOpts
1150+
mock := newRebaseMock(tmpDir, "b1")
1151+
mock.BranchExistsFn = func(string) bool { return true }
1152+
mock.CheckoutBranchFn = func(string) error { return nil }
1153+
mock.RebaseFn = func(_ string, opts git.RebaseOpts) error {
1154+
seen = append(seen, opts)
1155+
return nil
1156+
}
1157+
mock.RebaseOntoFn = func(_, _, _ string, opts git.RebaseOpts) error {
1158+
seen = append(seen, opts)
1159+
return nil
1160+
}
1161+
1162+
restore := git.SetOps(mock)
1163+
defer restore()
1164+
1165+
cfg, _, _ := config.NewTestConfig()
1166+
cmd := RebaseCmd(cfg)
1167+
cmd.SetArgs(tc.args)
1168+
cmd.SetOut(io.Discard)
1169+
cmd.SetErr(io.Discard)
1170+
require.NoError(t, cmd.Execute())
1171+
1172+
require.NotEmpty(t, seen, "expected at least one rebase op")
1173+
for _, o := range seen {
1174+
assert.Equal(t, tc.want, o.CommitterDateIsAuthorDate)
1175+
}
1176+
})
1177+
}
1178+
}

‎cmd/sync.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func runSync(cfg *config.Config, opts *syncOptions) error {
241241
}
242242
}
243243

244-
if err := git.RebaseOnto(newBase, actualOldBase, br.Branch); err != nil {
244+
if err := git.RebaseOnto(newBase, actualOldBase, br.Branch, git.RebaseOpts{}); err != nil {
245245
// Conflict detected — abort and restore everything
246246
if git.IsRebaseInProgress() {
247247
_ = git.RebaseAbort()
@@ -263,14 +263,14 @@ func runSync(cfg *config.Config, opts *syncOptions) error {
263263
var rebaseErr error
264264
if i > 0 {
265265
// Use --onto to replay only this branch's unique commits.
266-
rebaseErr = git.RebaseOnto(base, originalRefs[base], br.Branch)
266+
rebaseErr = git.RebaseOnto(base, originalRefs[base], br.Branch, git.RebaseOpts{})
267267
} else {
268268
if err := git.CheckoutBranch(br.Branch); err != nil {
269269
cfg.Errorf("Failed to checkout %s: %v", br.Branch, err)
270270
conflicted = true
271271
break
272272
}
273-
rebaseErr = git.Rebase(base)
273+
rebaseErr = git.Rebase(base, git.RebaseOpts{})
274274
}
275275

276276
if rebaseErr != nil {

0 commit comments

Comments
 (0)