summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2023-05-01 21:07:19 +1000
committerGitHub <noreply@github.com>2023-05-01 21:07:19 +1000
commitee9ae8f07f493cc22059f10e04f2459c2d1d32c3 (patch)
tree962810fc43cba06ad62f12583ce209f191e4be37
parent3ec416047fe087dead6e47098b06ed1a30092f40 (diff)
parente63858215e8778f58d66bf064359d70a48eb6756 (diff)
Merge pull request #2552 from stefanhaller/support-stacked-branches
-rw-r--r--go.mod2
-rw-r--r--go.sum4
-rw-r--r--pkg/app/daemon/daemon.go300
-rw-r--r--pkg/app/daemon/rebase.go64
-rw-r--r--pkg/commands/git_commands/deps_test.go6
-rw-r--r--pkg/commands/git_commands/patch.go16
-rw-r--r--pkg/commands/git_commands/rebase.go215
-rw-r--r--pkg/commands/git_commands/rebase_test.go3
-rw-r--r--pkg/gui/controllers/local_commits_controller.go14
-rw-r--r--pkg/integration/tests/interactive_rebase/amend_fixup_commit.go50
-rw-r--r--pkg/integration/tests/interactive_rebase/drop_todo_commit_with_update_ref.go13
-rw-r--r--pkg/integration/tests/test_list.go1
-rw-r--r--pkg/utils/rebase_todo.go76
-rw-r--r--pkg/utils/rebase_todo_test.go108
-rw-r--r--vendor/github.com/fsmiamoto/git-todo-parser/todo/parse.go10
-rw-r--r--vendor/modules.txt2
16 files changed, 659 insertions, 225 deletions
diff --git a/go.mod b/go.mod
index f86062a69..098d19c67 100644
--- a/go.mod
+++ b/go.mod
@@ -9,7 +9,7 @@ require (
github.com/cli/safeexec v1.0.0
github.com/cloudfoundry/jibber_jabber v0.0.0-20151120183258-bcc4c8345a21
github.com/creack/pty v1.1.11
- github.com/fsmiamoto/git-todo-parser v0.0.4-0.20230403011024-617a5a7ce980
+ github.com/fsmiamoto/git-todo-parser v0.0.4
github.com/fsnotify/fsnotify v1.4.7
github.com/gdamore/tcell/v2 v2.6.0
github.com/go-errors/errors v1.4.2
diff --git a/go.sum b/go.sum
index e6a83d627..dd4a8b81e 100644
--- a/go.sum
+++ b/go.sum
@@ -30,8 +30,8 @@ github.com/fatih/color v1.7.1-0.20180516100307-2d684516a886/go.mod h1:Zm6kSWBoL9
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
-github.com/fsmiamoto/git-todo-parser v0.0.4-0.20230403011024-617a5a7ce980 h1:ay9aM+Ay9I4LJttUVF4EFVmeNUkS9/snYVFK6lwieVQ=
-github.com/fsmiamoto/git-todo-parser v0.0.4-0.20230403011024-617a5a7ce980/go.mod h1:B+AgTbNE2BARvJqzXygThzqxLIaEWvwr2sxKYYb0Fas=
+github.com/fsmiamoto/git-todo-parser v0.0.4 h1:fzcGaoAFDHWzJRKw//CSZFrXucsLKplIvOSab3FtWWM=
+github.com/fsmiamoto/git-todo-parser v0.0.4/go.mod h1:B+AgTbNE2BARvJqzXygThzqxLIaEWvwr2sxKYYb0Fas=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
diff --git a/pkg/app/daemon/daemon.go b/pkg/app/daemon/daemon.go
index e040e79c2..adc287309 100644
--- a/pkg/app/daemon/daemon.go
+++ b/pkg/app/daemon/daemon.go
@@ -1,13 +1,17 @@
package daemon
import (
+ "encoding/json"
+ "fmt"
"log"
"os"
- "path/filepath"
- "strings"
+ "strconv"
+ "github.com/fsmiamoto/git-todo-parser/todo"
+ "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/common"
- "github.com/jesseduffield/lazygit/pkg/env"
+ "github.com/jesseduffield/lazygit/pkg/utils"
+ "github.com/samber/lo"
)
// Sometimes lazygit will be invoked in daemon mode from a parent lazygit process.
@@ -15,38 +19,58 @@ import (
// For example, if we want to ensure that a git command doesn't hang due to
// waiting for an editor to save a commit message, we can tell git to invoke lazygit
// as the editor via 'GIT_EDITOR=lazygit', and use the env var
-// 'LAZYGIT_DAEMON_KIND=EXIT_IMMEDIATELY' to specify that we want to run lazygit
-// as a daemon which simply exits immediately. Any additional arguments we want
-// to pass to a daemon can be done via other env vars.
+// 'LAZYGIT_DAEMON_KIND=1' (exit immediately) to specify that we want to run lazygit
+// as a daemon which simply exits immediately.
+//
+// 'Daemon' is not the best name for this, because it's not a persistent background
+// process, but it's close enough.
-type DaemonKind string
+type DaemonKind int
const (
- InteractiveRebase DaemonKind = "INTERACTIVE_REBASE"
- ExitImmediately DaemonKind = "EXIT_IMMEDIATELY"
+ // for when we fail to parse the daemon kind
+ DaemonKindUnknown DaemonKind = iota
+
+ DaemonKindExitImmediately
+ DaemonKindCherryPick
+ DaemonKindMoveTodoUp
+ DaemonKindMoveTodoDown
+ DaemonKindInsertBreak
+ DaemonKindChangeTodoActions
+ DaemonKindMoveFixupCommitDown
)
const (
DaemonKindEnvKey string = "LAZYGIT_DAEMON_KIND"
- RebaseTODOEnvKey string = "LAZYGIT_REBASE_TODO"
- // The `PrependLinesEnvKey` env variable is set to `true` to tell our daemon
- // to prepend the content of `RebaseTODOEnvKey` to the default `git-rebase-todo`
- // file instead of using it as a replacement.
- PrependLinesEnvKey string = "LAZYGIT_PREPEND_LINES"
+ // Contains json-encoded arguments to the daemon
+ DaemonInstructionEnvKey string = "LAZYGIT_DAEMON_INSTRUCTION"
)
-type Daemon interface {
- Run() error
+func getInstruction() Instruction {
+ jsonData := os.Getenv(DaemonInstructionEnvKey)
+
+ mapping := map[DaemonKind]func(string) Instruction{
+ DaemonKindExitImmediately: deserializeInstruction[*ExitImmediatelyInstruction],
+ DaemonKindCherryPick: deserializeInstruction[*CherryPickCommitsInstruction],
+ DaemonKindChangeTodoActions: deserializeInstruction[*ChangeTodoActionsInstruction],
+ DaemonKindMoveFixupCommitDown: deserializeInstruction[*MoveFixupCommitDownInstruction],
+ DaemonKindMoveTodoUp: deserializeInstruction[*MoveTodoUpInstruction],
+ DaemonKindMoveTodoDown: deserializeInstruction[*MoveTodoDownInstruction],
+ DaemonKindInsertBreak: deserializeInstruction[*InsertBreakInstruction],
+ }
+
+ return mapping[getDaemonKind()](jsonData)
}
func Handle(common *common.Common) {
- d := getDaemon(common)
- if d == nil {
+ if !InDaemonMode() {
return
}
- if err := d.Run(); err != nil {
+ instruction := getInstruction()
+
+ if err := instruction.run(common); err != nil {
log.Fatal(err)
}
@@ -54,73 +78,229 @@ func Handle(common *common.Common) {
}
func InDaemonMode() bool {
- return getDaemonKind() != ""
+ return getDaemonKind() != DaemonKindUnknown
}
-func getDaemon(common *common.Common) Daemon {
- switch getDaemonKind() {
- case InteractiveRebase:
- return &rebaseDaemon{c: common}
- case ExitImmediately:
- return &exitImmediatelyDaemon{c: common}
+func getDaemonKind() DaemonKind {
+ intValue, err := strconv.Atoi(os.Getenv(DaemonKindEnvKey))
+ if err != nil {
+ return DaemonKindUnknown
}
- return nil
+ return DaemonKind(intValue)
}
-func getDaemonKind() DaemonKind {
- return DaemonKind(os.Getenv(DaemonKindEnvKey))
+// An Instruction is a command to be run by lazygit in daemon mode.
+// It is serialized to json and passed to lazygit via environment variables
+type Instruction interface {
+ Kind() DaemonKind
+ SerializedInstructions() string
+
+ // runs the instruction
+ run(common *common.Common) error
}
-type rebaseDaemon struct {
- c *common.Common
+func serializeInstruction[T any](instruction T) string {
+ jsonData, err := json.Marshal(instruction)
+ if err != nil {
+ // this should never happen
+ panic(err)
+ }
+
+ return string(jsonData)
}
-func (self *rebaseDaemon) Run() error {
- self.c.Log.Info("Lazygit invoked as interactive rebase demon")
- self.c.Log.Info("args: ", os.Args)
- path := os.Args[1]
+func deserializeInstruction[T Instruction](jsonData string) Instruction {
+ var instruction T
+ err := json.Unmarshal([]byte(jsonData), &instruction)
+ if err != nil {
+ panic(err)
+ }
- if strings.HasSuffix(path, "git-rebase-todo") {
- return self.writeTodoFile(path)
- } else if strings.HasSuffix(path, filepath.Join(gitDir(), "COMMIT_EDITMSG")) { // TODO: test
- // if we are rebasing and squashing, we'll see a COMMIT_EDITMSG
- // but in this case we don't need to edit it, so we'll just return
- } else {
- self.c.Log.Info("Lazygit demon did not match on any use cases")
+ return instruction
+}
+
+func ToEnvVars(instruction Instruction) []string {
+ return []string{
+ fmt.Sprintf("%s=%d", DaemonKindEnvKey, instruction.Kind()),
+ fmt.Sprintf("%s=%s", DaemonInstructionEnvKey, instruction.SerializedInstructions()),
}
+}
+
+type ExitImmediatelyInstruction struct{}
+
+func (self *ExitImmediatelyInstruction) Kind() DaemonKind {
+ return DaemonKindExitImmediately
+}
+
+func (self *ExitImmediatelyInstruction) SerializedInstructions() string {
+ return serializeInstruction(self)
+}
+func (self *ExitImmediatelyInstruction) run(common *common.Common) error {
return nil
}
-func (self *rebaseDaemon) writeTodoFile(path string) error {
- todoContent := []byte(os.Getenv(RebaseTODOEnvKey))
+func NewExitImmediatelyInstruction() Instruction {
+ return &ExitImmediatelyInstruction{}
+}
+
+type CherryPickCommitsInstruction struct {
+ Todo string
+}
- prependLines := os.Getenv(PrependLinesEnvKey) != ""
- if prependLines {
- existingContent, err := os.ReadFile(path)
- if err != nil {
- return err
+func NewCherryPickCommitsInstruction(commits []*models.Commit) Instruction {
+ todoLines := lo.Map(commits, func(commit *models.Commit, _ int) TodoLine {
+ return TodoLine{
+ Action: "pick",
+ Commit: commit,
}
+ })
- todoContent = append(todoContent, existingContent...)
+ todo := TodoLinesToString(todoLines)
+
+ return &CherryPickCommitsInstruction{
+ Todo: todo,
}
+}
- return os.WriteFile(path, todoContent, 0o644)
+func (self *CherryPickCommitsInstruction) Kind() DaemonKind {
+ return DaemonKindCherryPick
}
-func gitDir() string {
- dir := env.GetGitDirEnv()
- if dir == "" {
- return ".git"
+func (self *CherryPickCommitsInstruction) SerializedInstructions() string {
+ return serializeInstruction(self)
+}
+
+func (self *CherryPickCommitsInstruction) run(common *common.Common) error {
+ return handleInteractiveRebase(common, func(path string) error {
+ return utils.PrependStrToTodoFile(path, []byte(self.Todo))
+ })
+}
+
+type ChangeTodoActionsInstruction struct {
+ Changes []ChangeTodoAction
+}
+
+func NewChangeTodoActionsInstruction(changes []ChangeTodoAction) Instruction {
+ return &ChangeTodoActionsInstruction{
+ Changes: changes,
}
- return dir
}
-type exitImmediatelyDaemon struct {
- c *common.Common
+func (self *ChangeTodoActionsInstruction) Kind() DaemonKind {
+ return DaemonKindChangeTodoActions
}
-func (self *exitImmediatelyDaemon) Run() error {
- return nil
+func (self *ChangeTodoActionsInstruction) SerializedInstructions() string {
+ return serializeInstruction(self)
+}
+
+func (self *ChangeTodoActionsInstruction) run(common *common.Common) error {
+ return handleInteractiveRebase(common, func(path string) error {
+ for _, c := range self.Changes {
+ if err := utils.EditRebaseTodo(path, c.Sha, todo.Pick, c.NewAction); err != nil {
+ return err
+ }
+ }
+
+ return nil
+ })
+}
+
+// Takes the sha of some commit, and the sha of a fixup commit that was created
+// at the end of the branch, then moves the fixup commit down to right after the
+// original commit, changing its type to "fixup"
+type MoveFixupCommitDownInstruction struct {
+ OriginalSha string
+ FixupSha string
+}
+
+func NewMoveFixupCommitDownInstruction(originalSha string, fixupSha string) Instruction {
+ return &MoveFixupCommitDownInstruction{
+ OriginalSha: originalSha,
+ FixupSha: fixupSha,
+ }
+}
+
+func (self *MoveFixupCommitDownInstruction) Kind() DaemonKind {
+ return DaemonKindMoveFixupCommitDown
+}
+
+func (self *MoveFixupCommitDownInstruction) SerializedInstructions() string {
+ return serializeInstruction(self)
+}
+
+func (self *MoveFixupCommitDownInstruction) run(common *common.Common) error {
+ return handleInteractiveRebase(common, func(path string) error {
+ return utils.MoveFixupCommitDown(path, self.OriginalSha, self.FixupSha)
+ })
+}
+
+type MoveTodoUpInstruction struct {
+ Sha string
+}
+
+func NewMoveTodoUpInstruction(sha string) Instruction {
+ return &MoveTodoUpInstruction{
+ Sha: sha,
+ }
+}
+
+func (self *MoveTodoUpInstruction) Kind() DaemonKind {
+ return DaemonKindMoveTodoUp
+}
+
+func (self *MoveTodoUpInstruction) SerializedInstructions() string {
+ return serializeInstruction(self)
+}
+
+func (self *MoveTodoUpInstruction) run(common *common.Common) error {
+ return handleInteractiveRebase(common, func(path string) error {
+ return utils.MoveTodoUp(path, self.Sha, todo.Pick)
+ })
+}
+
+type MoveTodoDownInstruction struct {
+ Sha string
+}
+
+func NewMoveTodoDownInstruction(sha string) Instruction {
+ return &MoveTodoDownInstruction{
+ Sha: sha,
+ }
+}
+
+func (self *MoveTodoDownInstruction) Kind() DaemonKind {
+ return DaemonKindMoveTodoDown
+}
+
+func (self *MoveTodoDownInstruction) SerializedInstructions() string {
+ return serializeInstruction(self)
+}
+
+func (self *MoveTodoDownInstruction) run(common *common.Common) error {
+ return handleInteractiveRebase(common, func(path string) error {
+ return utils.MoveTodoDown(path, self.Sha, todo.Pick)
+ })
+}
+
+type InsertBreakInstruction struct{}
+
+func NewInsertBreakInstruction() Instruction {
+ return &InsertBreakInstruction{}
+}
+
+func (self *InsertBreakInstruction) Kind() DaemonKind {
+ return DaemonKindInsertBreak
+}
+
+func (self *InsertBreakInstruction) SerializedInstructions() string {
+ return serializeInstruction(self)
+}
+
+func (self *InsertBreakInstruction) run(common *common.Common) error {
+ return handleInteractiveRebase(common, func(path string) error {
+ return utils.PrependStrToTodoFile(path, []byte("break\n"))
+ })
}
diff --git a/pkg/app/daemon/rebase.go b/pkg/app/daemon/rebase.go
new file mode 100644
index 000000000..8702f0f69
--- /dev/null
+++ b/pkg/app/daemon/rebase.go
@@ -0,0 +1,64 @@
+package daemon
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+
+ "github.com/fsmiamoto/git-todo-parser/todo"
+ "github.com/jesseduffield/generics/slices"
+ "github.com/jesseduffield/lazygit/pkg/commands/models"
+ "github.com/jesseduffield/lazygit/pkg/common"
+ "github.com/jesseduffield/lazygit/pkg/env"
+)
+
+type TodoLine struct {
+ Action string
+ Commit *models.Commit
+}
+
+func (self *TodoLine) ToString() string {
+ if self.Action == "break" {
+ return self.Action + "\n"
+ } else {
+ return self.Action + " " + self.Commit.Sha + " " + self.Commit.Name + "\n"
+ }
+}
+
+func TodoLinesToString(todoLines []TodoLine) string {
+ lines := slices.Map(todoLines, func(todoLine TodoLine) string {
+ return todoLine.ToString()
+ })
+
+ return strings.Join(slices.Reverse(lines), "")
+}
+
+type ChangeTodoAction struct {
+ Sha string
+ NewAction todo.TodoCommand
+}
+
+func handleInteractiveRebase(common *common.Common, f func(path string) error) error {
+ common.Log.Info("Lazygit invoked as interactive rebase demon")
+ common.Log.Info("args: ", os.Args)
+ path := os.Args[1]
+
+ if strings.HasSuffix(path, "git-rebase-todo") {
+ return f(path)
+ } else if strings.HasSuffix(path, filepath.Join(gitDir(), "COMMIT_EDITMSG")) { // TODO: test
+ // if we are rebasing and squashing, we'll see a COMMIT_EDITMSG
+ // but in this case we don't need to edit it, so we'll just return
+ } else {
+ common.Log.Info("Lazygit demon did not match on any use cases")
+ }
+
+ return nil
+}
+
+func gitDir() string {
+ dir := env.GetGitDirEnv()
+ if dir == "" {
+ return ".git"
+ }
+ return dir
+}
diff --git a/pkg/commands/git_commands/deps_test.go b/pkg/commands/git_commands/deps_test.go
index df2c96253..bcf36b168 100644
--- a/pkg/commands/git_commands/deps_test.go
+++ b/pkg/commands/git_commands/deps_test.go
@@ -15,6 +15,7 @@ import (
type commonDeps struct {
runner *oscommands.FakeCmdObjRunner
userConfig *config.UserConfig
+ gitVersion *GitVersion
gitConfig *git_config.FakeGitConfig
getenv func(string) string
removeFile func(string) error
@@ -48,6 +49,11 @@ func buildGitCommon(deps commonDeps) *GitCommon {
gitCommon.Common.UserConfig = config.GetDefaultConfig()
}
+ gitCommon.version = deps.gitVersion
+ if gitCommon.version == nil {
+ gitCommon.version = &GitVersion{2, 0, 0, ""}
+ }
+
gitConfig := deps.gitConfig
if gitConfig == nil {
gitConfig = git_config.NewFakeGitConfig(nil)
diff --git a/pkg/commands/git_commands/patch.go b/pkg/commands/git_commands/patch.go
index 8c956529c..06e5e0f67 100644
--- a/pkg/commands/git_commands/patch.go
+++ b/pkg/commands/git_commands/patch.go
@@ -3,7 +3,9 @@ package git_commands
import (
"fmt"
+ "github.com/fsmiamoto/git-todo-parser/todo"
"github.com/go-errors/errors"
+ "github.com/jesseduffield/lazygit/pkg/app/daemon"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/patch"
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
@@ -103,18 +105,16 @@ func (self *PatchCommands) MovePatchToSelectedCommit(commits []*models.Commit, s
baseIndex := sourceCommitIdx + 1
- todoLines := self.rebase.BuildTodoLines(commits[0:baseIndex], func(commit *models.Commit, i int) string {
- if i == sourceCommitIdx || i == destinationCommitIdx {
- return "edit"
- } else {
- return "pick"
- }
- })
+ changes := []daemon.ChangeTodoAction{
+ {Sha: commits[sourceCommitIdx].Sha, NewAction: todo.Edit},
+ {Sha: commits[destinationCommitIdx].Sha, NewAction: todo.Edit},
+ }
+ self.os.LogCommand(logTodoChanges(changes), false)
err := self.rebase.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{
baseShaOrRoot: commits[baseIndex].Sha,
- todoLines: todoLines,
overrideEditor: true,
+ instruction: daemon.NewChangeTodoActionsInstruction(changes),
}).Run()
if err != nil {
return err
diff --git a/pkg/commands/git_commands/rebase.go b/pkg/commands/git_commands/rebase.go
index 5828ffbaa..50c599a66 100644
--- a/pkg/commands/git_commands/rebase.go
+++ b/pkg/commands/git_commands/rebase.go
@@ -12,6 +12,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/utils"
+ "github.com/samber/lo"
)
type RebaseCommands struct {
@@ -55,14 +56,15 @@ func (self *RebaseCommands) RewordCommit(commits []*models.Commit, index int, me
}
func (self *RebaseCommands) RewordCommitInEditor(commits []*models.Commit, index int) (oscommands.ICmdObj, error) {
- todo, sha, err := self.BuildSingleActionTodo(commits, index, "reword")
- if err != nil {
- return nil, err
- }
+ changes := []daemon.ChangeTodoAction{{
+ Sha: commits[index].Sha,
+ NewAction: todo.Reword,
+ }}
+ self.os.LogCommand(logTodoChanges(changes), false)
return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{
- baseShaOrRoot: sha,
- todoLines: todo,
+ baseShaOrRoot: getBaseShaOrRoot(commits, index+1),
+ instruction: daemon.NewChangeTodoActionsInstruction(changes),
}), nil
}
@@ -99,96 +101,105 @@ func (self *RebaseCommands) GenericAmend(commits []*models.Commit, index int, f
}
func (self *RebaseCommands) MoveCommitDown(commits []*models.Commit, index int) error {
- // 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])
+ baseShaOrRoot := getBaseShaOrRoot(commits, index+2)
- todoLines := self.BuildTodoLinesSingleAction(orderedCommits, "pick")
+ sha := commits[index].Sha
- baseShaOrRoot := getBaseShaOrRoot(commits, index+2)
+ self.os.LogCommand(fmt.Sprintf("Moving TODO down: %s", utils.ShortSha(sha)), false)
return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{
baseShaOrRoot: baseShaOrRoot,
- todoLines: todoLines,
+ instruction: daemon.NewMoveTodoDownInstruction(sha),
overrideEditor: true,
}).Run()
}
-func (self *RebaseCommands) InteractiveRebase(commits []*models.Commit, index int, action string) error {
- todo, sha, err := self.BuildSingleActionTodo(commits, index, action)
- if err != nil {
- return err
- }
+func (self *RebaseCommands) MoveCommitUp(commits []*models.Commit, index int) error {
+ baseShaOrRoot := getBaseShaOrRoot(commits, index+1)
+
+ sha := commits[index].Sha
+
+ self.os.LogCommand(fmt.Sprintf("Moving TODO up: %s", utils.ShortSha(sha)), false)
return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{
- baseShaOrRoot: sha,
- todoLines: todo,
+ baseShaOrRoot: baseShaOrRoot,
+ instruction: daemon.NewMoveTodoUpInstruction(sha),
overrideEditor: true,
}).Run()
}
-func (self *RebaseCommands) InteractiveRebaseBreakAfter(commits []*models.Commit, index int) error {
- todo, sha, err := self.BuildSingleActionTodo(commits, index-1, "pick")
- if err != nil {
- return err
+func (self *RebaseCommands) InteractiveRebase(commits []*models.Commit, index int, action todo.TodoCommand) error {
+ baseIndex := index + 1
+ if action == todo.Squash || action == todo.Fixup {
+ baseIndex++
}
- todo = append(todo, TodoLine{Action: "break", Commit: nil})
+ baseShaOrRoot := getBaseShaOrRoot(commits, baseIndex)
+
+ changes := []daemon.ChangeTodoAction{{
+ Sha: commits[index].Sha,
+ NewAction: action,
+ }}
+ self.os.LogCommand(logTodoChanges(changes), false)
+
return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{
- baseShaOrRoot: sha,
- todoLines: todo,
+ baseShaOrRoot: baseShaOrRoot,
overrideEditor: true,
+ instruction: daemon.NewChangeTodoActionsInstruction(changes),
}).Run()
}
func (self *RebaseCommands) EditRebase(branchRef string) error {
- commands := []TodoLine{{Action: "break"}}
+ self.os.LogCommand(fmt.Sprintf("Beginning interactive rebase at '%s'", branchRef), false)
return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{
baseShaOrRoot: branchRef,
- todoLines: commands,
- prepend: true,
+ instruction: daemon.NewInsertBreakInstruction(),
}).Run()
}
+func logTodoChanges(changes []daemon.ChangeTodoAction) string {
+ changeTodoStr := strings.Join(slices.Map(changes, func(c daemon.ChangeTodoAction) string {
+ return fmt.Sprintf("%s:%s", c.Sha, c.NewAction)
+ }), "\n")
+ return fmt.Sprintf("Changing TODO actions: %s", changeTodoStr)
+}
+
type PrepareInteractiveRebaseCommandOpts struct {
baseShaOrRoot string
- todoLines []TodoLine
+ instruction daemon.Instruction
overrideEditor bool
- prepend bool
}
// 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(opts PrepareInteractiveRebaseCommandOpts) oscommands.ICmdObj {
- todo := self.buildTodo(opts.todoLines)
ex := oscommands.GetLazygitPath()
- prependLines := ""
- if opts.prepend {
- prependLines = "TRUE"
- }
debug := "FALSE"
if self.Debug {
debug = "TRUE"
}
- cmdStr := fmt.Sprintf("git rebase --interactive --autostash --keep-empty --empty=keep --no-autosquash %s", opts.baseShaOrRoot)
+ rebaseMergesArg := " --rebase-merges"
+ if self.version.IsOlderThan(2, 22, 0) {
+ rebaseMergesArg = ""
+ }
+ cmdStr := fmt.Sprintf("git rebase --interactive --autostash --keep-empty --empty=keep --no-autosquash%s %s",
+ rebaseMergesArg, opts.baseShaOrRoot)
self.Log.WithField("command", cmdStr).Debug("RunCommand")
cmdObj := self.cmd.New(cmdStr)
gitSequenceEditor := ex
- if todo == "" {
- gitSequenceEditor = "true"
+
+ if opts.instruction != nil {
+ cmdObj.AddEnvVars(daemon.ToEnvVars(opts.instruction)...)
} else {
- self.os.LogCommand(fmt.Sprintf("Creating TODO file for interactive rebase: \n\n%s", todo), false)
+ gitSequenceEditor = "true"
}
cmdObj.AddEnvVars(
- daemon.DaemonKindEnvKey+"="+string(daemon.InteractiveRebase),
- daemon.RebaseTODOEnvKey+"="+todo,
- daemon.PrependLinesEnvKey+"="+prependLines,
"DEBUG="+debug,
"LANG=en_US.UTF-8", // Force using EN as language
"LC_ALL=en_US.UTF-8", // Force using EN as language
@@ -202,63 +213,31 @@ func (self *RebaseCommands) PrepareInteractiveRebaseCommand(opts PrepareInteract
return cmdObj
}
-// produces TodoLines where every commit is picked (or dropped for merge commits) except for the commit at the given index, which
-// will have the given action applied to it.
-func (self *RebaseCommands) BuildSingleActionTodo(commits []*models.Commit, actionIndex int, action string) ([]TodoLine, string, error) {
- baseIndex := actionIndex + 1
-
- if action == "squash" || action == "fixup" {
- baseIndex++
- }
-
- todoLines := self.BuildTodoLines(commits[0:baseIndex], func(commit *models.Commit, i int) string {
- if i == actionIndex {
- return action
- } else if commit.IsMerge() {
- // your typical interactive rebase will actually drop merge commits by default. Damn git CLI, you scary!
- // doing this means we don't need to worry about rebasing over merges which always causes problems.
- // you typically shouldn't be doing rebases that pass over merge commits anyway.
- return "drop"
- } else {
- return "pick"
- }
- })
-
- baseShaOrRoot := getBaseShaOrRoot(commits, baseIndex)
-
- return todoLines, baseShaOrRoot, nil
-}
-
// AmendTo amends the given commit with whatever files are staged
-func (self *RebaseCommands) AmendTo(commit *models.Commit) error {
+func (self *RebaseCommands) AmendTo(commits []*models.Commit, commitIndex int) error {
+ commit := commits[commitIndex]
+
if err := self.commit.CreateFixupCommit(commit.Sha); err != nil {
return err
}
- return self.SquashAllAboveFixupCommits(commit)
-}
-
-// EditRebaseTodo sets the action for a given rebase commit in the git-rebase-todo file
-func (self *RebaseCommands) EditRebaseTodo(commit *models.Commit, action todo.TodoCommand) error {
- fileName := filepath.Join(self.dotGitDir, "rebase-merge/git-rebase-todo")
- todos, err := utils.ReadRebaseTodoFile(fileName)
+ // Get the sha of the commit we just created
+ fixupSha, err := self.cmd.New("git rev-parse --verify HEAD").RunWithOutput()
if err != nil {
return err
}
- for i := range todos {
- t := &todos[i]
- // Comparing just the sha is not enough; we need to compare both the
- // action and the sha, as the sha could appear multiple times (e.g. in a
- // pick and later in a merge)
- if t.Command == commit.Action && t.Commit == commit.Sha {
- t.Command = action
- return utils.WriteRebaseTodoFile(fileName, todos)
- }
- }
+ return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{
+ baseShaOrRoot: getBaseShaOrRoot(commits, commitIndex+1),
+ overrideEditor: true,
+ instruction: daemon.NewMoveFixupCommitDownInstruction(commit.Sha, fixupSha),
+ }).Run()
+}
- // Should never get here
- return fmt.Errorf("Todo %s not found in git-rebase-todo", commit.Sha)
+// EditRebaseTodo sets the action for a given rebase commit in the git-rebase-todo file
+func (self *RebaseCommands) EditRebaseTodo(commit *models.Commit, action todo.TodoCommand) error {
+ return utils.EditRebaseTodo(
+ filepath.Join(self.dotGitDir, "rebase-merge/git-rebase-todo"), commit.Sha, commit.Action, action)
}
// MoveTodoDown moves a rebase todo item down by one position
@@ -304,15 +283,16 @@ func (self *RebaseCommands) BeginInteractiveRebaseForCommit(commits []*models.Co
return errors.New(self.Tr.DisabledForGPG)
}
- todo, sha, err := self.BuildSingleActionTodo(commits, commitIndex, "edit")
- if err != nil {
- return err
- }
+ changes := []daemon.ChangeTodoAction{{
+ Sha: commits[commitIndex].Sha,
+ NewAction: todo.Edit,
+ }}
+ self.os.LogCommand(logTodoChanges(changes), false)
return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{
- baseShaOrRoot: sha,
- todoLines: todo,
+ baseShaOrRoot: getBaseShaOrRoot(commits, commitIndex+1),
overrideEditor: true,
+ instruction: daemon.NewChangeTodoActionsInstruction(changes),
}).Run()
}
@@ -359,15 +339,16 @@ func (self *RebaseCommands) GenericMergeOrRebaseAction(commandType string, comma
}
func (self *RebaseCommands) runSkipEditorCommand(cmdObj oscommands.ICmdObj) error {
+ instruction := daemon.NewExitImmediatelyInstruction()
lazyGitPath := oscommands.GetLazygitPath()
return cmdObj.
AddEnvVars(
- daemon.DaemonKindEnvKey+"="+string(daemon.ExitImmediately),
"GIT_EDITOR="+lazyGitPath,
"GIT_SEQUENCE_EDITOR="+lazyGitPath,
"EDITOR="+lazyGitPath,
"VISUAL="+lazyGitPath,
).
+ AddEnvVars(daemon.ToEnvVars(instruction)...).
Run()
}
@@ -401,47 +382,17 @@ func (self *RebaseCommands) DiscardOldFileChanges(commits []*models.Commit, comm
// CherryPickCommits begins an interactive rebase with the given shas being cherry picked onto HEAD
func (self *RebaseCommands) CherryPickCommits(commits []*models.Commit) error {
- todoLines := self.BuildTodoLinesSingleAction(commits, "pick")
+ commitLines := lo.Map(commits, func(commit *models.Commit, _ int) string {
+ return fmt.Sprintf("%s %s", utils.ShortSha(commit.Sha), commit.Name)
+ })
+ self.os.LogCommand(fmt.Sprintf("Cherry-picking commits:\n%s", strings.Join(commitLines, "\n")), false)
return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{
baseShaOrRoot: "HEAD",
- todoLines: todoLines,