summaryrefslogtreecommitdiffstats
path: root/pkg/commands
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2023-02-20 19:21:37 +1100
committerGitHub <noreply@github.com>2023-02-20 19:21:37 +1100
commite1c376ef5490f3e964ee8c5bd554957c05c60884 (patch)
treee56451c32f53f3949b3a5d730b36ecd38d2c7bfd /pkg/commands
parentc13f550d634a4bda12a244d67f4363a59dc05fb4 (diff)
parentc5cd217a6504259c4040402f6044b1d50ca3a410 (diff)
Merge pull request #2453 from stefanhaller/allow-rebasing-to-first-commit
Diffstat (limited to 'pkg/commands')
-rw-r--r--pkg/commands/git_commands/rebase.go36
1 files changed, 19 insertions, 17 deletions
diff --git a/pkg/commands/git_commands/rebase.go b/pkg/commands/git_commands/rebase.go
index 66dbcf45f..50ab4de7a 100644
--- a/pkg/commands/git_commands/rebase.go
+++ b/pkg/commands/git_commands/rebase.go
@@ -130,7 +130,7 @@ func (self *RebaseCommands) InteractiveRebaseBreakAfter(commits []*models.Commit
// PrepareInteractiveRebaseCommand returns the cmd for an interactive rebase
// we tell git to run lazygit to edit the todo list, and we pass the client
// lazygit a todo string to write to the todo file
-func (self *RebaseCommands) PrepareInteractiveRebaseCommand(baseSha string, todoLines []TodoLine, overrideEditor bool) oscommands.ICmdObj {
+func (self *RebaseCommands) PrepareInteractiveRebaseCommand(baseShaOrRoot string, todoLines []TodoLine, overrideEditor bool) oscommands.ICmdObj {
todo := self.buildTodo(todoLines)
ex := oscommands.GetLazygitPath()
@@ -139,7 +139,7 @@ func (self *RebaseCommands) PrepareInteractiveRebaseCommand(baseSha string, todo
debug = "TRUE"
}
- cmdStr := fmt.Sprintf("git rebase --interactive --autostash --keep-empty --no-autosquash %s", baseSha)
+ cmdStr := fmt.Sprintf("git rebase --interactive --autostash --keep-empty --no-autosquash %s", baseShaOrRoot)
self.Log.WithField("command", cmdStr).Debug("RunCommand")
cmdObj := self.cmd.New(cmdStr)
@@ -172,16 +172,8 @@ func (self *RebaseCommands) PrepareInteractiveRebaseCommand(baseSha string, todo
func (self *RebaseCommands) BuildSingleActionTodo(commits []*models.Commit, actionIndex int, action string) ([]TodoLine, string, error) {
baseIndex := actionIndex + 1
- if len(commits) <= baseIndex {
- return nil, "", errors.New(self.Tr.CannotRebaseOntoFirstCommit)
- }
-
if action == "squash" || action == "fixup" {
baseIndex++
-
- if len(commits) <= baseIndex {
- return nil, "", errors.New(self.Tr.CannotSquashOntoSecondCommit)
- }
}
todoLines := self.BuildTodoLines(commits[0:baseIndex], func(commit *models.Commit, i int) string {
@@ -197,16 +189,21 @@ func (self *RebaseCommands) BuildSingleActionTodo(commits []*models.Commit, acti
}
})
- return todoLines, commits[baseIndex].Sha, nil
+ baseSha := "--root"
+ if baseIndex < len(commits) {
+ baseSha = commits[baseIndex].Sha
+ }
+
+ return todoLines, baseSha, nil
}
// AmendTo amends the given commit with whatever files are staged
-func (self *RebaseCommands) AmendTo(sha string) error {
- if err := self.commit.CreateFixupCommit(sha); err != nil {
+func (self *RebaseCommands) AmendTo(commit *models.Commit) error {
+ if err := self.commit.CreateFixupCommit(commit.Sha); err != nil {
return err
}
- return self.SquashAllAboveFixupCommits(sha)
+ return self.SquashAllAboveFixupCommits(commit)
}
// EditRebaseTodo sets the action at a given index in the git-rebase-todo file
@@ -261,12 +258,17 @@ func (self *RebaseCommands) MoveTodoDown(index int) error {
}
// SquashAllAboveFixupCommits squashes all fixup! commits above the given one
-func (self *RebaseCommands) SquashAllAboveFixupCommits(sha string) error {
+func (self *RebaseCommands) SquashAllAboveFixupCommits(commit *models.Commit) error {
+ shaOrRoot := commit.Sha + "^"
+ if commit.IsFirstCommit() {
+ shaOrRoot = "--root"
+ }
+
return self.runSkipEditorCommand(
self.cmd.New(
fmt.Sprintf(
- "git rebase --interactive --rebase-merges --autostash --autosquash %s^",
- sha,
+ "git rebase --interactive --rebase-merges --autostash --autosquash %s",
+ shaOrRoot,
),
),
)