1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-20 05:19:24 +02:00

Implement moving commits up/down in terms of daemon

This commit is contained in:
Stefan Haller 2023-04-06 09:53:10 +02:00
parent 3791f0b2fa
commit dad7a70bf8
3 changed files with 47 additions and 8 deletions

View File

@ -2,6 +2,7 @@ package daemon
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
@ -46,13 +47,24 @@ const (
//
// If this is used, the value of RebaseTODOEnvKey must be empty.
ChangeTodoActionEnvKey string = "LAZYGIT_CHANGE_TODO_ACTION"
// Can be set to the sha of a "pick" todo that will be moved down by one.
MoveTodoDownEnvKey string = "LAZYGIT_MOVE_COMMIT_DOWN"
// Can be set to the sha of a "pick" todo that will be moved up by one.
MoveTodoUpEnvKey string = "LAZYGIT_MOVE_COMMIT_UP"
)
type Daemon interface {
Run() error
}
var logFile io.StringWriter
func Handle(common *common.Common) {
logFile, _ = os.Create("/tmp/daemon-log.txt")
_, _ = logFile.WriteString("Hello Daemon\n")
d := getDaemon(common)
if d == nil {
return
@ -108,6 +120,12 @@ func (self *rebaseDaemon) Run() error {
func (self *rebaseDaemon) writeTodoFile(path string) error {
if changeTodoActionEnvValue := os.Getenv(ChangeTodoActionEnvKey); changeTodoActionEnvValue != "" {
return self.changeTodoAction(path, changeTodoActionEnvValue)
} else if shaToMoveDown := os.Getenv(MoveTodoDownEnvKey); shaToMoveDown != "" {
_, _ = logFile.WriteString(fmt.Sprintf("Moving commit down: %s\n", shaToMoveDown))
return utils.MoveTodoDown(path, shaToMoveDown, todo.Pick)
} else if shaToMoveUp := os.Getenv(MoveTodoUpEnvKey); shaToMoveUp != "" {
_, _ = logFile.WriteString(fmt.Sprintf("Moving commit up: %s\n", shaToMoveUp))
return utils.MoveTodoUp(path, shaToMoveUp, todo.Pick)
} else {
todoContent := []byte(os.Getenv(RebaseTODOEnvKey))

View File

@ -99,18 +99,22 @@ 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])
todoLines := self.BuildTodoLinesSingleAction(orderedCommits, "pick")
baseShaOrRoot := getBaseShaOrRoot(commits, index+2)
return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{
baseShaOrRoot: baseShaOrRoot,
todoLines: todoLines,
overrideEditor: true,
moveDown: commits[index].Sha,
}).Run()
}
func (self *RebaseCommands) MoveCommitUp(commits []*models.Commit, index int) error {
baseShaOrRoot := getBaseShaOrRoot(commits, index+1)
return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{
baseShaOrRoot: baseShaOrRoot,
overrideEditor: true,
moveUp: commits[index].Sha,
}).Run()
}
@ -152,6 +156,8 @@ type PrepareInteractiveRebaseCommandOpts struct {
overrideEditor bool
prepend bool
changeTodoActions []ChangeTodoAction
moveDown string
moveUp string
}
// PrepareInteractiveRebaseCommand returns the cmd for an interactive rebase
@ -174,6 +180,15 @@ func (self *RebaseCommands) PrepareInteractiveRebaseCommand(opts PrepareInteract
return fmt.Sprintf("%s:%s", c.sha, c.newAction)
}), "\n")
moveDownValue := ""
if opts.moveDown != "" {
moveDownValue = opts.moveDown
}
moveUpValue := ""
if opts.moveUp != "" {
moveUpValue = opts.moveUp
}
if todo != "" && changeTodoValue != "" {
panic("It's not allowed to pass both todoLines and changeActionOpts")
}
@ -193,6 +208,10 @@ func (self *RebaseCommands) PrepareInteractiveRebaseCommand(opts PrepareInteract
self.os.LogCommand(fmt.Sprintf("Creating TODO file for interactive rebase: \n\n%s", todo), false)
} else if changeTodoValue != "" {
self.os.LogCommand(fmt.Sprintf("Changing TODO action: %s", changeTodoValue), false)
} else if moveDownValue != "" {
self.os.LogCommand(fmt.Sprintf("Moving TODO down: %s", moveDownValue), false)
} else if moveUpValue != "" {
self.os.LogCommand(fmt.Sprintf("Moving TODO up: %s", moveUpValue), false)
} else {
gitSequenceEditor = "true"
}
@ -202,6 +221,8 @@ func (self *RebaseCommands) PrepareInteractiveRebaseCommand(opts PrepareInteract
daemon.RebaseTODOEnvKey+"="+todo,
daemon.PrependLinesEnvKey+"="+prependLines,
daemon.ChangeTodoActionEnvKey+"="+changeTodoValue,
daemon.MoveTodoDownEnvKey+"="+moveDownValue,
daemon.MoveTodoUpEnvKey+"="+moveUpValue,
"DEBUG="+debug,
"LANG=en_US.UTF-8", // Force using EN as language
"LC_ALL=en_US.UTF-8", // Force using EN as language

View File

@ -440,7 +440,7 @@ func (self *LocalCommitsController) moveUp(commit *models.Commit) error {
return self.c.WithWaitingStatus(self.c.Tr.MovingStatus, func() error {
self.c.LogAction(self.c.Tr.Actions.MoveCommitUp)
err := self.git.Rebase.MoveCommitDown(self.model.Commits, index-1)
err := self.git.Rebase.MoveCommitUp(self.model.Commits, index)
if err == nil {
self.context().MoveSelectedLine(-1)
}