Skip to content

Commit 515599d

Browse files
committed
Fix other minor inconsistencies round 3
1 parent 7719e9b commit 515599d

22 files changed

Lines changed: 130 additions & 106 deletions

commands/command_code_owners.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func changedFilesOwners(changedFiles []string) map[string][]string {
8686
Returns changed files against main.
8787
*/
8888
func getChangedFiles() []string {
89-
firstOriginCommit := gitutil.FirstOriginMainCommit(util.GetCurrentBranchName())
89+
firstOriginCommit := gitutil.GetMergeBaseWithOriginMain(util.GetCurrentBranchName())
9090
filenamesRaw := util.ExecuteOrDie(util.ExecuteOptions{}, "git", "--no-pager",
9191
"log", "--pretty=format:\"\"", "--name-only", firstOriginCommit+"..HEAD")
9292
return strings.Split(strings.TrimSpace(filenamesRaw), "\n")

commands/command_migrate.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ func processBranch(branch string, baseCommit string) migrationResult {
134134

135135
// Re-check commits after rebase (hashes may have changed)
136136
commitsAhead = getCommitsAhead(baseCommit, "HEAD")
137+
if len(commitsAhead) == 0 {
138+
slog.Info(fmt.Sprint("Branch ", branch, " has no commits after rebase - skipping"))
139+
return migrationResult{
140+
branchName: branch,
141+
success: false,
142+
reason: "no commits after rebase",
143+
numCommits: 0,
144+
}
145+
}
137146
slog.Debug(fmt.Sprint("Found ", len(commitsAhead), " commits ahead of main for branch ", branch))
138147
for _, commit := range commitsAhead {
139148
slog.Debug(fmt.Sprint(" - ", commit))
@@ -204,7 +213,7 @@ func findUserBranches() []string {
204213
result[i] = b.name
205214
}
206215

207-
slog.Info(fmt.Sprint("Found ", len(result), " branches with your commits"))
216+
slog.Debug(fmt.Sprint("Found ", len(result), " branches with your commits"))
208217
return result
209218
}
210219

@@ -261,18 +270,18 @@ func selectBranchesToMigrate(branches []string) []string {
261270
}
262271

263272
if len(disabledBranches) == len(branches) {
264-
slog.Info("All branches already exist on main - nothing to migrate")
273+
slog.Debug("All branches already exist on main - nothing to migrate")
265274
util.Fprintln(appConfig.Io.Out, "All branches have already been migrated to main")
266275
return []string{}
267276
}
268277

269-
slog.Info("Starting interactive branch selection...")
278+
slog.Debug("Starting interactive branch selection...")
270279
selectedBranches, err := interactive.GetBranchSelectionWithFilter(
271280
branches,
272281
"Select branches to migrate (use space to select/deselect, enter to confirm):",
273282
rowEnabled,
274283
)
275-
slog.Info("Interactive selection completed")
284+
slog.Debug("Interactive selection completed")
276285
if err != nil {
277286
slog.Warn("Failed to get branch selection: " + err.Error())
278287
return []string{}
@@ -284,10 +293,10 @@ func selectBranchesToMigrate(branches []string) []string {
284293
// computeDisabledBranches returns a set of branch indices that already have their
285294
// commits on main and should be disabled in the interactive selector.
286295
func computeDisabledBranches(branches []string) map[int]bool {
287-
slog.Info("Fetching commits from main for branch filtering...")
296+
slog.Debug("Fetching commits from main for branch filtering...")
288297
mainBranch := gitutil.GetLocalMainBranchOrDie()
289298
mainCommits := templates.GetNewCommits(mainBranch, "")
290-
slog.Info(fmt.Sprint("Found ", len(mainCommits), " commits on main for branch filtering"))
299+
slog.Debug(fmt.Sprint("Found ", len(mainCommits), " commits on main for branch filtering"))
291300

292301
branchesOnMain := make(map[string]bool)
293302
for _, commit := range mainCommits {
@@ -296,12 +305,12 @@ func computeDisabledBranches(branches []string) map[int]bool {
296305
}
297306
}
298307

299-
slog.Info("Building branch filter...")
308+
slog.Debug("Building branch filter...")
300309
disabledBranches := make(map[int]bool)
301310
for i, branch := range branches {
302311
if branchesOnMain[branch] {
303312
disabledBranches[i] = true
304-
slog.Info(fmt.Sprint("Branch ", branch, " already exists on main - will be disabled"))
313+
slog.Debug(fmt.Sprint("Branch ", branch, " already exists on main - will be disabled"))
305314
}
306315
}
307316
return disabledBranches
@@ -319,7 +328,7 @@ func findMostRecentMainCommit(branches []string) string {
319328

320329
// For each branch, find its merge-base with origin/main
321330
for _, branch := range allBranches {
322-
mergeBase := gitutil.FirstOriginMainCommit(branch)
331+
mergeBase := gitutil.GetMergeBaseWithOriginMain(branch)
323332
slog.Debug(fmt.Sprint("Merge-base for ", branch, " with origin/main: ", mergeBase))
324333

325334
// Get the timestamp of this commit

commands/command_migrate_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func TestSdMigrate_RebaseSingleBranch(t *testing.T) {
140140
util.ExecuteOrDie(util.ExecuteOptions{}, "git", "checkout", "feature-1")
141141

142142
// Get the most recent main commit
143-
mostRecentMainCommit := gitutil.FirstOriginMainCommit(gitutil.GetLocalMainBranchOrDie())
143+
mostRecentMainCommit := gitutil.GetMergeBaseWithOriginMain(gitutil.GetLocalMainBranchOrDie())
144144

145145
// Perform rebase
146146
_, err := util.Execute(util.ExecuteOptions{}, "git", "rebase", mostRecentMainCommit)
@@ -177,7 +177,7 @@ func TestSdMigrate_RebaseMultipleBranches(t *testing.T) {
177177
util.ExecuteOrDie(util.ExecuteOptions{}, "git", "push", "origin", gitutil.GetLocalMainBranchOrDie())
178178

179179
// Get the most recent main commit
180-
mostRecentMainCommit := gitutil.FirstOriginMainCommit(gitutil.GetLocalMainBranchOrDie())
180+
mostRecentMainCommit := gitutil.GetMergeBaseWithOriginMain(gitutil.GetLocalMainBranchOrDie())
181181

182182
// Save current branch
183183
currentBranch := util.ExecuteOrDieTrimmed(util.ExecuteOptions{}, "git", "branch", "--show-current")
@@ -220,7 +220,7 @@ func TestSdMigrate_RebaseFailure(t *testing.T) {
220220
util.ExecuteOrDie(util.ExecuteOptions{}, "git", "push", "origin", gitutil.GetLocalMainBranchOrDie())
221221

222222
// Get the most recent main commit
223-
mostRecentMainCommit := gitutil.FirstOriginMainCommit(gitutil.GetLocalMainBranchOrDie())
223+
mostRecentMainCommit := gitutil.GetMergeBaseWithOriginMain(gitutil.GetLocalMainBranchOrDie())
224224

225225
// Try to rebase - should fail due to conflict
226226
util.ExecuteOrDie(util.ExecuteOptions{}, "git", "checkout", "feature-1")
@@ -260,7 +260,7 @@ func TestSdMigrate_EndsOnMainBranch(t *testing.T) {
260260
assert.Equal("feature-2", originalBranch)
261261

262262
// Get the most recent main commit
263-
mostRecentMainCommit := gitutil.FirstOriginMainCommit(gitutil.GetLocalMainBranchOrDie())
263+
mostRecentMainCommit := gitutil.GetMergeBaseWithOriginMain(gitutil.GetLocalMainBranchOrDie())
264264

265265
// Simulate the migrate process: checkout feature-1, rebase it
266266
util.ExecuteOrDie(util.ExecuteOptions{}, "git", "checkout", "feature-1")
@@ -483,7 +483,7 @@ func TestSdMigrate_SkipsBranchWithMergedPR(t *testing.T) {
483483
"--json", "number,title,state", "--jq", util.MatchAnyRemainingArgs)
484484

485485
// Get the most recent main commit
486-
mostRecentMainCommit := gitutil.FirstOriginMainCommit(gitutil.GetLocalMainBranchOrDie())
486+
mostRecentMainCommit := gitutil.GetMergeBaseWithOriginMain(gitutil.GetLocalMainBranchOrDie())
487487

488488
// Record current branch before processing
489489
currentBranch := util.ExecuteOrDieTrimmed(util.ExecuteOptions{}, "git", "branch", "--show-current")
@@ -529,7 +529,7 @@ func TestSdMigrate_SkipsDuplicateCommitsWhenMigratingBranchWithoutPR(t *testing.
529529
"--json", "number,title,state", "--jq", util.MatchAnyRemainingArgs)
530530

531531
// Get the base commit for the feature branch
532-
mostRecentMainCommit := gitutil.FirstOriginMainCommit(gitutil.GetLocalMainBranchOrDie())
532+
mostRecentMainCommit := gitutil.GetMergeBaseWithOriginMain(gitutil.GetLocalMainBranchOrDie())
533533

534534
appConfig := util.GetAppConfig()
535535
appConfig.Io.In = strings.NewReader("My Feature PR\n\n") // PR name, then empty string for prefix (press enter to skip)

commands/command_new.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func createNewCommand() *cobra.Command {
100100
*baseBranch = gitutil.GetLocalMainBranchOrDie()
101101
remoteBaseBranch = gitutil.GetRemoteMainBranchOrDie()
102102
} else {
103+
util.RequireGitRef(*baseBranch)
103104
remoteBaseBranch = *baseBranch
104105
}
105106
ticketUrlPattern := userConfig.TicketUrlPattern
@@ -139,7 +140,7 @@ func createNewPr(draft bool, noTemplate bool, featureFlag string, ticketUrlPatte
139140
func createBranchAndCherryPick(rollbackManager *gitutil.GitRollbackManager, baseBranch string, gitLog templates.GitLog) {
140141
var commitToBranchFrom string
141142
if baseBranch == gitutil.GetLocalMainBranchOrDie() {
142-
commitToBranchFrom = gitutil.FirstOriginMainCommit(gitutil.GetLocalMainBranchOrDie())
143+
commitToBranchFrom = gitutil.GetMergeBaseWithOriginMain(gitutil.GetLocalMainBranchOrDie())
143144
slog.Info(fmt.Sprint("Switching to branch ", gitLog.Branch, " based off commit ", commitToBranchFrom))
144145
} else {
145146
commitToBranchFrom = baseBranch

commands/command_rebase_main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,13 @@ func rebaseMain() {
106106
_, rebaseError = gitutil.RebaseAndSkipAllEmpty(options, "origin/"+gitutil.GetRemoteMainBranchOrDie())
107107
}
108108
if rebaseError != nil {
109+
if shouldPopStash {
110+
slog.Info("Your changes are still stashed. Run `git stash pop` after resolving the rebase.")
111+
}
109112
panic("Rebase failed, check output ^^ for details. Continue rebase manually.")
110-
} else {
111-
gitutil.PopStash(shouldPopStash)
112113
}
114+
// Only pop stash on success — popping onto merge conflicts would be a problem.
115+
gitutil.PopStash(shouldPopStash)
113116
}
114117

115118
// getMergedBranches returns branches from merged PRs that were merged AFTER our branch diverged.

commands/command_replace_commit.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,11 @@ func replaceCommitOfBranchInfo(rollbackManager *gitutil.GitRollbackManager, onCh
6262
rollbackCommit := util.ExecuteOrDieTrimmed(util.ExecuteOptions{}, "git", "log", "-n", "1", "--pretty=format:%H")
6363
commitsAfter := strings.Fields(util.ExecuteOrDieTrimmed(util.ExecuteOptions{}, "git", "--no-pager", "log", gitLog.Commit+"..HEAD", "--pretty=format:%h"))
6464
slices.Reverse(commitsAfter)
65-
commitToDiffFrom := gitutil.FirstOriginMainCommit(gitLog.Branch)
65+
commitToDiffFrom := gitutil.GetMergeBaseWithOriginMain(gitLog.Branch)
6666
slog.Info("Resetting to " + gitLog.Commit + "~1")
6767
util.ExecuteOrDie(util.ExecuteOptions{}, "git", "reset", "--hard", gitLog.Commit+"~1")
6868
slog.Info("Adding diff from commits " + gitLog.Branch)
69-
diff := util.ExecuteOrDie(util.ExecuteOptions{}, "git", "diff", "--binary", commitToDiffFrom, gitLog.Branch)
70-
util.ExecuteOrDie(
71-
util.ExecuteOptions{Io: util.StdIo{In: strings.NewReader(diff), Out: nil, Err: nil}},
72-
"git", "apply",
73-
)
74-
util.ExecuteOrDie(util.ExecuteOptions{}, "git", "add", ".")
69+
gitutil.ApplyDiffFromRef(commitToDiffFrom, gitLog.Branch)
7570
commitSummary := util.ExecuteOrDie(util.ExecuteOptions{}, "git", "--no-pager", "show", "--no-patch", "--format=%s", gitLog.Commit)
7671
util.ExecuteOrDie(util.ExecuteOptions{}, "git", "commit", "--no-verify", "-m", strings.TrimSpace(commitSummary))
7772
if len(commitsAfter) != 0 {

commands/command_replace_conflicts.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,12 @@ func createReplaceConflictsCommand() *cobra.Command {
3535
// For failed rebase: replace changes with its associated branch.
3636
func replaceConflicts(confirmed bool) {
3737
commitWithConflicts := getCommitWithConflicts()
38-
gitLog := templates.GetBranchInfo(commitWithConflicts, templates.IndicatorTypeCommit)
38+
gitLog := templates.ResolveCommitIndicator(commitWithConflicts, templates.IndicatorTypeCommit)
3939
checkConfirmed(confirmed)
4040
util.ExecuteOrDie(util.ExecuteOptions{}, "git", "reset", "--hard", "HEAD")
4141
slog.Info(fmt.Sprint("Replacing changes (merge conflicts) for failed rebase of commit ", commitWithConflicts, ", with changes from associated branch, ", gitLog.Branch))
42-
diff := util.ExecuteOrDie(util.ExecuteOptions{}, "git", "diff", "--binary", "origin/"+gitutil.GetRemoteMainBranchOrDie(), gitLog.Branch)
43-
util.ExecuteOrDie(util.ExecuteOptions{Io: util.StdIo{In: strings.NewReader(diff), Out: nil, Err: nil}},
44-
"git", "apply",
45-
)
42+
gitutil.ApplyDiffFromRef("origin/"+gitutil.GetRemoteMainBranchOrDie(), gitLog.Branch)
4643
slog.Info("Adding changes and continuing rebase")
47-
util.ExecuteOrDie(util.ExecuteOptions{}, "git", "add", ".")
4844
continueOptions := util.ExecuteOptions{EnvironmentVariables: []string{"GIT_EDITOR=true"}}
4945
// Note: --continue cannot be used with --no-verify.
5046
util.ExecuteOrDie(continueOptions, "git", "rebase", "--continue")
@@ -70,7 +66,11 @@ func getCommitWithConflicts() string {
7066
panic("Cannot determine which commit is being rebased with because \"git status\" does not have a \"Last commands done\" section. To use this command you must be in the middle of a rebase")
7167
}
7268
// Return the 2nd field, from a string such as "pick f52e867 next1"
73-
return strings.Fields(statusLines[lastCommandDoneLine])[1]
69+
fields := strings.Fields(statusLines[lastCommandDoneLine])
70+
if len(fields) < 2 {
71+
panic("Cannot parse commit hash from git status line: " + statusLines[lastCommandDoneLine])
72+
}
73+
return fields[1]
7474
}
7575

7676
func checkConfirmed(confirmed bool) {

commands/command_update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func updatePr(destCommit templates.GitLog, commitsToCherryPick []templates.GitLo
102102
if cherryPickError != nil {
103103
slog.Info("First attempt at cherry-pick failed")
104104
util.ExecuteOrDie(util.ExecuteOptions{}, "git", "cherry-pick", "--abort")
105-
rebaseCommit := gitutil.FirstOriginMainCommit(gitutil.GetLocalMainBranchOrDie())
105+
rebaseCommit := gitutil.GetMergeBaseWithOriginMain(gitutil.GetLocalMainBranchOrDie())
106106
slog.Info(fmt.Sprint("Rebasing with the base commit on "+gitutil.GetLocalMainBranchOrDie()+" branch, ", rebaseCommit,
107107
", in case the local "+gitutil.GetLocalMainBranchOrDie()+" was rebased with origin/"+gitutil.GetRemoteMainBranchOrDie()))
108108
gitutil.RebaseAndSkipAllEmptyOrDie(util.ExecuteOptions{Io: appConfig.Io}, rebaseCommit)

commands/command_worktree_move.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,9 @@ func worktreeMove(args []string, indicatorTypeString *string, worktreeFlag strin
9494
}
9595
selectedCommits := getTargetCommits(args, indicatorTypeString, selectCommitOptions)
9696
slog.Info(fmt.Sprint("Cherry-picking ", len(selectedCommits), " commit(s) onto ", mainBranch, " in main worktree"))
97-
commits := make([]string, len(selectedCommits))
98-
for i, commit := range selectedCommits {
99-
commits[i] = commit.Commit
100-
}
97+
commits := util.MapSlice(selectedCommits, func(commit templates.GitLog) string {
98+
return commit.Commit
99+
})
101100
cherryPickWithRecovery(mainPath, commits, cherryPickRecoveryOptions{
102101
OnRollback: func() {
103102
util.ExecuteOrDie(util.ExecuteOptions{}, "git", gitutil.PrependGitDir(mainPath, "cherry-pick", "--abort")...)

commands/get_target_commits.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func getTargetCommits(
3737
} else {
3838
indicatorType := checkIndicatorFlag(indicatorTypeString)
3939
return util.MapSlice(commitsFromCommandLine, func(commit string) templates.GitLog {
40-
return templates.GetBranchInfo(commit, indicatorType)
40+
return templates.ResolveCommitIndicator(commit, indicatorType)
4141
})
4242
}
4343
}

0 commit comments

Comments
 (0)