summaryrefslogtreecommitdiffstats
path: root/pkg/commands
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2023-02-22 19:36:31 +1100
committerJesse Duffield <jessedduffield@gmail.com>2023-02-22 19:36:31 +1100
commit78f3a7a4786bf5f70af3489e6c4f2e44cddbd978 (patch)
tree4e1920587bb05585816107e05ab3c42a8fa9bfc6 /pkg/commands
parent526c9dea9b298d69a3fafec3303549b0d6292af4 (diff)
migrate interactive rebase integration tests
Diffstat (limited to 'pkg/commands')
-rw-r--r--pkg/commands/git_commands/rebase.go35
1 files changed, 22 insertions, 13 deletions
diff --git a/pkg/commands/git_commands/rebase.go b/pkg/commands/git_commands/rebase.go
index 50ab4de7a..cff9b961a 100644
--- a/pkg/commands/git_commands/rebase.go
+++ b/pkg/commands/git_commands/rebase.go
@@ -95,17 +95,15 @@ func (self *RebaseCommands) GenericAmend(commits []*models.Commit, index int, f
}
func (self *RebaseCommands) MoveCommitDown(commits []*models.Commit, index int) error {
- // we must ensure that we have at least two commits after the selected one
- if len(commits) <= index+2 {
- // assuming they aren't picking the bottom commit
- return errors.New(self.Tr.NoRoom)
- }
-
- orderedCommits := append(commits[0:index], commits[index+1], commits[index])
+ // not appending to original slice so that we don't mutate it
+ orderedCommits := append([]*models.Commit{}, commits[0:index]...)
+ orderedCommits = append(orderedCommits, commits[index+1], commits[index])
todoLines := self.BuildTodoLinesSingleAction(orderedCommits, "pick")
- return self.PrepareInteractiveRebaseCommand(commits[index+2].Sha, todoLines, true).Run()
+ baseShaOrRoot := getBaseShaOrRoot(commits, index+2)
+
+ return self.PrepareInteractiveRebaseCommand(baseShaOrRoot, todoLines, true).Run()
}
func (self *RebaseCommands) InteractiveRebase(commits []*models.Commit, index int, action string) error {
@@ -189,12 +187,9 @@ func (self *RebaseCommands) BuildSingleActionTodo(commits []*models.Commit, acti
}
})
- baseSha := "--root"
- if baseIndex < len(commits) {
- baseSha = commits[baseIndex].Sha
- }
+ baseShaOrRoot := getBaseShaOrRoot(commits, baseIndex)
- return todoLines, baseSha, nil
+ return todoLines, baseShaOrRoot, nil
}
// AmendTo amends the given commit with whatever files are staged
@@ -418,3 +413,17 @@ func (self *TodoLine) ToString() string {
return self.Action + " " + self.Commit.Sha + " " + self.Commit.Name + "\n"
}
}
+
+// we can't start an interactive rebase from the first commit without passing the
+// '--root' arg
+func getBaseShaOrRoot(commits []*models.Commit, index int) string {
+ // We assume that the commits slice contains the initial commit of the repo.
+ // Technically this assumption could prove false, but it's unlikely you'll
+ // be starting a rebase from 300 commits ago (which is the original commit limit
+ // at time of writing)
+ if index < len(commits) {
+ return commits[index].Sha
+ } else {
+ return "--root"
+ }
+}