1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-01 13:17:53 +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 ( import (
"fmt" "fmt"
"io"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
@ -46,13 +47,24 @@ const (
// //
// If this is used, the value of RebaseTODOEnvKey must be empty. // If this is used, the value of RebaseTODOEnvKey must be empty.
ChangeTodoActionEnvKey string = "LAZYGIT_CHANGE_TODO_ACTION" 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 { type Daemon interface {
Run() error Run() error
} }
var logFile io.StringWriter
func Handle(common *common.Common) { func Handle(common *common.Common) {
logFile, _ = os.Create("/tmp/daemon-log.txt")
_, _ = logFile.WriteString("Hello Daemon\n")
d := getDaemon(common) d := getDaemon(common)
if d == nil { if d == nil {
return return
@ -108,6 +120,12 @@ func (self *rebaseDaemon) Run() error {
func (self *rebaseDaemon) writeTodoFile(path string) error { func (self *rebaseDaemon) writeTodoFile(path string) error {
if changeTodoActionEnvValue := os.Getenv(ChangeTodoActionEnvKey); changeTodoActionEnvValue != "" { if changeTodoActionEnvValue := os.Getenv(ChangeTodoActionEnvKey); changeTodoActionEnvValue != "" {
return self.changeTodoAction(path, 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 { } else {
todoContent := []byte(os.Getenv(RebaseTODOEnvKey)) 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 { 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) baseShaOrRoot := getBaseShaOrRoot(commits, index+2)
return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{ return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{
baseShaOrRoot: baseShaOrRoot, baseShaOrRoot: baseShaOrRoot,
todoLines: todoLines,
overrideEditor: true, 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() }).Run()
} }
@ -152,6 +156,8 @@ type PrepareInteractiveRebaseCommandOpts struct {
overrideEditor bool overrideEditor bool
prepend bool prepend bool
changeTodoActions []ChangeTodoAction changeTodoActions []ChangeTodoAction
moveDown string
moveUp string
} }
// PrepareInteractiveRebaseCommand returns the cmd for an interactive rebase // 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) return fmt.Sprintf("%s:%s", c.sha, c.newAction)
}), "\n") }), "\n")
moveDownValue := ""
if opts.moveDown != "" {
moveDownValue = opts.moveDown
}
moveUpValue := ""
if opts.moveUp != "" {
moveUpValue = opts.moveUp
}
if todo != "" && changeTodoValue != "" { if todo != "" && changeTodoValue != "" {
panic("It's not allowed to pass both todoLines and changeActionOpts") 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) self.os.LogCommand(fmt.Sprintf("Creating TODO file for interactive rebase: \n\n%s", todo), false)
} else if changeTodoValue != "" { } else if changeTodoValue != "" {
self.os.LogCommand(fmt.Sprintf("Changing TODO action: %s", changeTodoValue), false) 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 { } else {
gitSequenceEditor = "true" gitSequenceEditor = "true"
} }
@ -202,6 +221,8 @@ func (self *RebaseCommands) PrepareInteractiveRebaseCommand(opts PrepareInteract
daemon.RebaseTODOEnvKey+"="+todo, daemon.RebaseTODOEnvKey+"="+todo,
daemon.PrependLinesEnvKey+"="+prependLines, daemon.PrependLinesEnvKey+"="+prependLines,
daemon.ChangeTodoActionEnvKey+"="+changeTodoValue, daemon.ChangeTodoActionEnvKey+"="+changeTodoValue,
daemon.MoveTodoDownEnvKey+"="+moveDownValue,
daemon.MoveTodoUpEnvKey+"="+moveUpValue,
"DEBUG="+debug, "DEBUG="+debug,
"LANG=en_US.UTF-8", // Force using EN as language "LANG=en_US.UTF-8", // Force using EN as language
"LC_ALL=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 { return self.c.WithWaitingStatus(self.c.Tr.MovingStatus, func() error {
self.c.LogAction(self.c.Tr.Actions.MoveCommitUp) 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 { if err == nil {
self.context().MoveSelectedLine(-1) self.context().MoveSelectedLine(-1)
} }