allow rewording old commits

This commit is contained in:
Jesse Duffield 2022-01-09 13:36:07 +11:00
parent ee15202207
commit 0dfd02c42d
9 changed files with 61 additions and 71 deletions

View file

@ -24,9 +24,9 @@ func NewCommitCommands(
}
}
// RewordLastCommit renames the topmost commit with the given name
func (self *CommitCommands) RewordLastCommit(name string) error {
return self.cmd.New("git commit --allow-empty --amend --only -m " + self.cmd.Quote(name)).Run()
// RewordLastCommit rewords the topmost commit with the given message
func (self *CommitCommands) RewordLastCommit(message string) error {
return self.cmd.New("git commit --allow-empty --amend --only -m " + self.cmd.Quote(message)).Run()
}
// ResetToCommit reset to commit

View file

@ -51,7 +51,7 @@ func (self *PatchCommands) DeletePatchesFromCommit(commits []*models.Commit, com
// apply each patch in reverse
if err := self.PatchManager.ApplyPatches(true); err != nil {
if err := self.rebase.GenericMergeOrRebaseAction("rebase", "abort"); err != nil {
if err := self.rebase.AbortRebase(); err != nil {
return err
}
return err
@ -68,7 +68,7 @@ func (self *PatchCommands) DeletePatchesFromCommit(commits []*models.Commit, com
}
// continue
return self.rebase.GenericMergeOrRebaseAction("rebase", "continue")
return self.rebase.ContinueRebase()
}
func (self *PatchCommands) MovePatchToSelectedCommit(commits []*models.Commit, sourceCommitIdx int, destinationCommitIdx int) error {
@ -79,7 +79,7 @@ func (self *PatchCommands) MovePatchToSelectedCommit(commits []*models.Commit, s
// apply each patch forward
if err := self.PatchManager.ApplyPatches(false); err != nil {
if err := self.rebase.GenericMergeOrRebaseAction("rebase", "abort"); err != nil {
if err := self.rebase.AbortRebase(); err != nil {
return err
}
return err
@ -96,7 +96,7 @@ func (self *PatchCommands) MovePatchToSelectedCommit(commits []*models.Commit, s
}
// continue
return self.rebase.GenericMergeOrRebaseAction("rebase", "continue")
return self.rebase.ContinueRebase()
}
if len(commits)-1 < sourceCommitIdx {
@ -120,18 +120,14 @@ func (self *PatchCommands) MovePatchToSelectedCommit(commits []*models.Commit, s
todo = a + " " + commit.Sha + " " + commit.Name + "\n" + todo
}
cmdObj, err := self.rebase.PrepareInteractiveRebaseCommand(commits[baseIndex].Sha, todo, true)
err := self.rebase.PrepareInteractiveRebaseCommand(commits[baseIndex].Sha, todo, true).Run()
if err != nil {
return err
}
if err := cmdObj.Run(); err != nil {
return err
}
// apply each patch in reverse
if err := self.PatchManager.ApplyPatches(true); err != nil {
if err := self.rebase.GenericMergeOrRebaseAction("rebase", "abort"); err != nil {
if err := self.rebase.AbortRebase(); err != nil {
return err
}
return err
@ -150,7 +146,7 @@ func (self *PatchCommands) MovePatchToSelectedCommit(commits []*models.Commit, s
// now we should be up to the destination, so let's apply forward these patches to that.
// ideally we would ensure we're on the right commit but I'm not sure if that check is necessary
if err := self.PatchManager.ApplyPatches(false); err != nil {
if err := self.rebase.GenericMergeOrRebaseAction("rebase", "abort"); err != nil {
if err := self.rebase.AbortRebase(); err != nil {
return err
}
return err
@ -166,10 +162,10 @@ func (self *PatchCommands) MovePatchToSelectedCommit(commits []*models.Commit, s
return nil
}
return self.rebase.GenericMergeOrRebaseAction("rebase", "continue")
return self.rebase.ContinueRebase()
}
return self.rebase.GenericMergeOrRebaseAction("rebase", "continue")
return self.rebase.ContinueRebase()
}
func (self *PatchCommands) MovePatchIntoIndex(commits []*models.Commit, commitIdx int, stash bool) error {
@ -185,7 +181,7 @@ func (self *PatchCommands) MovePatchIntoIndex(commits []*models.Commit, commitId
if err := self.PatchManager.ApplyPatches(true); err != nil {
if self.status.WorkingTreeState() == enums.REBASE_MODE_REBASING {
if err := self.rebase.GenericMergeOrRebaseAction("rebase", "abort"); err != nil {
if err := self.rebase.AbortRebase(); err != nil {
return err
}
}
@ -205,7 +201,7 @@ func (self *PatchCommands) MovePatchIntoIndex(commits []*models.Commit, commitId
// add patches to index
if err := self.PatchManager.ApplyPatches(false); err != nil {
if self.status.WorkingTreeState() == enums.REBASE_MODE_REBASING {
if err := self.rebase.GenericMergeOrRebaseAction("rebase", "abort"); err != nil {
if err := self.rebase.AbortRebase(); err != nil {
return err
}
}
@ -222,7 +218,7 @@ func (self *PatchCommands) MovePatchIntoIndex(commits []*models.Commit, commitId
return nil
}
return self.rebase.GenericMergeOrRebaseAction("rebase", "continue")
return self.rebase.ContinueRebase()
}
func (self *PatchCommands) PullPatchIntoNewCommit(commits []*models.Commit, commitIdx int) error {
@ -231,7 +227,7 @@ func (self *PatchCommands) PullPatchIntoNewCommit(commits []*models.Commit, comm
}
if err := self.PatchManager.ApplyPatches(true); err != nil {
if err := self.rebase.GenericMergeOrRebaseAction("rebase", "abort"); err != nil {
if err := self.rebase.AbortRebase(); err != nil {
return err
}
return err
@ -244,7 +240,7 @@ func (self *PatchCommands) PullPatchIntoNewCommit(commits []*models.Commit, comm
// add patches to index
if err := self.PatchManager.ApplyPatches(false); err != nil {
if err := self.rebase.GenericMergeOrRebaseAction("rebase", "abort"); err != nil {
if err := self.rebase.AbortRebase(); err != nil {
return err
}
return err
@ -262,5 +258,5 @@ func (self *PatchCommands) PullPatchIntoNewCommit(commits []*models.Commit, comm
}
self.PatchManager.Reset()
return self.rebase.GenericMergeOrRebaseAction("rebase", "continue")
return self.rebase.ContinueRebase()
}

View file

@ -45,13 +45,33 @@ func NewRebaseCommands(
}
}
func (self *RebaseCommands) RewordCommit(commits []*models.Commit, index int) (oscommands.ICmdObj, error) {
func (self *RebaseCommands) RewordCommit(commits []*models.Commit, index int, message string) error {
if index == 0 {
// we've selected the top commit so no rebase is required
return self.commit.RewordLastCommit(message)
}
err := self.BeginInteractiveRebaseForCommit(commits, index)
if err != nil {
return err
}
// now the selected commit should be our head so we'll amend it with the new message
err = self.commit.RewordLastCommit(message)
if err != nil {
return err
}
return self.ContinueRebase()
}
func (self *RebaseCommands) RewordCommitInEditor(commits []*models.Commit, index int) (oscommands.ICmdObj, error) {
todo, sha, err := self.GenerateGenericRebaseTodo(commits, index, "reword")
if err != nil {
return nil, err
}
return self.PrepareInteractiveRebaseCommand(sha, todo, false)
return self.PrepareInteractiveRebaseCommand(sha, todo, false), nil
}
func (self *RebaseCommands) MoveCommitDown(commits []*models.Commit, index int) error {
@ -67,12 +87,7 @@ func (self *RebaseCommands) MoveCommitDown(commits []*models.Commit, index int)
todo = "pick " + commit.Sha + " " + commit.Name + "\n" + todo
}
cmdObj, err := self.PrepareInteractiveRebaseCommand(commits[index+2].Sha, todo, true)
if err != nil {
return err
}
return cmdObj.Run()
return self.PrepareInteractiveRebaseCommand(commits[index+2].Sha, todo, true).Run()
}
func (self *RebaseCommands) InteractiveRebase(commits []*models.Commit, index int, action string) error {
@ -81,18 +96,13 @@ func (self *RebaseCommands) InteractiveRebase(commits []*models.Commit, index in
return err
}
cmdObj, err := self.PrepareInteractiveRebaseCommand(sha, todo, true)
if err != nil {
return err
}
return cmdObj.Run()
return self.PrepareInteractiveRebaseCommand(sha, todo, true).Run()
}
// 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, todo string, overrideEditor bool) (oscommands.ICmdObj, error) {
func (self *RebaseCommands) PrepareInteractiveRebaseCommand(baseSha string, todo string, overrideEditor bool) oscommands.ICmdObj {
ex := oscommands.GetLazygitPath()
debug := "FALSE"
@ -125,7 +135,7 @@ func (self *RebaseCommands) PrepareInteractiveRebaseCommand(baseSha string, todo
cmdObj.AddEnvVars("GIT_EDITOR=" + ex)
}
return cmdObj, nil
return cmdObj
}
func (self *RebaseCommands) GenerateGenericRebaseTodo(commits []*models.Commit, actionIndex int, action string) (string, string, error) {
@ -235,7 +245,7 @@ func (self *RebaseCommands) SquashAllAboveFixupCommits(sha string) error {
}
// BeginInteractiveRebaseForCommit starts an interactive rebase to edit the current
// commit and pick all others. After this you'll want to call `self.GenericMergeOrRebaseAction("rebase", "continue")`
// commit and pick all others. After this you'll want to call `self.ContinueRebase()
func (self *RebaseCommands) BeginInteractiveRebaseForCommit(commits []*models.Commit, commitIndex int) error {
if len(commits)-1 < commitIndex {
return errors.New("index outside of range of commits")
@ -253,28 +263,26 @@ func (self *RebaseCommands) BeginInteractiveRebaseForCommit(commits []*models.Co
return err
}
cmdObj, err := self.PrepareInteractiveRebaseCommand(sha, todo, true)
if err != nil {
return err
}
return cmdObj.Run()
return self.PrepareInteractiveRebaseCommand(sha, todo, true).Run()
}
// RebaseBranch interactive rebases onto a branch
func (self *RebaseCommands) RebaseBranch(branchName string) error {
cmdObj, err := self.PrepareInteractiveRebaseCommand(branchName, "", false)
if err != nil {
return err
}
return cmdObj.Run()
return self.PrepareInteractiveRebaseCommand(branchName, "", false).Run()
}
func (self *RebaseCommands) GenericMergeOrRebaseActionCmdObj(commandType string, command string) oscommands.ICmdObj {
return self.cmd.New("git " + commandType + " --" + command)
}
func (self *RebaseCommands) ContinueRebase() error {
return self.GenericMergeOrRebaseAction("rebase", "continue")
}
func (self *RebaseCommands) AbortRebase() error {
return self.GenericMergeOrRebaseAction("rebase", "abort")
}
// GenericMerge takes a commandType of "merge" or "rebase" and a command of "abort", "skip" or "continue"
// By default we skip the editor in the case where a commit will be made
func (self *RebaseCommands) GenericMergeOrRebaseAction(commandType string, command string) error {
@ -337,7 +345,7 @@ func (self *RebaseCommands) DiscardOldFileChanges(commits []*models.Commit, comm
}
// continue
return self.GenericMergeOrRebaseAction("rebase", "continue")
return self.ContinueRebase()
}
// CherryPickCommits begins an interactive rebase with the given shas being cherry picked onto HEAD
@ -347,10 +355,5 @@ func (self *RebaseCommands) CherryPickCommits(commits []*models.Commit) error {
todo = "pick " + commit.Sha + " " + commit.Name + "\n" + todo
}
cmdObj, err := self.PrepareInteractiveRebaseCommand("HEAD", todo, false)
if err != nil {
return err
}
return cmdObj.Run()
return self.PrepareInteractiveRebaseCommand("HEAD", todo, false).Run()
}

View file

@ -221,10 +221,6 @@ func (gui *Gui) handleRewordCommit() error {
return nil
}
if gui.State.Panels.Commits.SelectedLineIdx != 0 {
return gui.createErrorPanel(gui.Tr.OnlyRewordTopCommit)
}
commit := gui.getSelectedLocalCommit()
if commit == nil {
return nil
@ -240,7 +236,7 @@ func (gui *Gui) handleRewordCommit() error {
initialContent: message,
handleConfirm: func(response string) error {
gui.logAction(gui.Tr.Actions.RewordCommit)
if err := gui.Git.Commit.RewordLastCommit(response); err != nil {
if err := gui.Git.Rebase.RewordCommit(gui.State.Commits, gui.State.Panels.Commits.SelectedLineIdx, response); err != nil {
return gui.surfaceError(err)
}
@ -249,7 +245,7 @@ func (gui *Gui) handleRewordCommit() error {
})
}
func (gui *Gui) handleRenameCommitEditor() error {
func (gui *Gui) handleRewordCommitEditor() error {
if ok, err := gui.validateNotInFilterMode(); err != nil || !ok {
return err
}
@ -263,7 +259,7 @@ func (gui *Gui) handleRenameCommitEditor() error {
}
gui.logAction(gui.Tr.Actions.RewordCommit)
subProcess, err := gui.Git.Rebase.RewordCommit(gui.State.Commits, gui.State.Panels.Commits.SelectedLineIdx)
subProcess, err := gui.Git.Rebase.RewordCommitInEditor(gui.State.Commits, gui.State.Panels.Commits.SelectedLineIdx)
if err != nil {
return gui.surfaceError(err)
}

View file

@ -750,7 +750,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
ViewName: "commits",
Contexts: []string{string(BRANCH_COMMITS_CONTEXT_KEY)},
Key: gui.getKey(config.Commits.RenameCommitWithEditor),
Handler: gui.handleRenameCommitEditor,
Handler: gui.handleRewordCommitEditor,
Description: gui.Tr.LcRenameCommitEditor,
},
{

View file

@ -109,7 +109,6 @@ func chineseTranslationSet() TranslationSet {
Squash: "压缩",
LcPickCommit: "选择提交(变基过程中)",
LcRevertCommit: "还原提交",
OnlyRewordTopCommit: "只能从 lazygit 内部重写最高的提交。请使用 shift-R",
LcRewordCommit: "改写提交",
LcDeleteCommit: "删除提交",
LcMoveDownCommit: "下移提交",

View file

@ -79,7 +79,6 @@ func dutchTranslationSet() TranslationSet {
Squash: "Squash",
LcPickCommit: "kies commit (wanneer midden in rebase)",
LcRevertCommit: "commit ongedaan maken",
OnlyRewordTopCommit: "Je kan alleen de bovenste commit hernoemen",
LcRewordCommit: "hernoem commit",
LcDeleteCommit: "verwijder commit",
LcMoveDownCommit: "verplaats commit 1 naar beneden",

View file

@ -93,7 +93,6 @@ type TranslationSet struct {
Squash string
LcPickCommit string
LcRevertCommit string
OnlyRewordTopCommit string
LcRewordCommit string
LcDeleteCommit string
LcMoveDownCommit string
@ -645,7 +644,6 @@ func EnglishTranslationSet() TranslationSet {
Squash: "Squash",
LcPickCommit: "pick commit (when mid-rebase)",
LcRevertCommit: "revert commit",
OnlyRewordTopCommit: "Can only reword topmost commit from within lazygit. Use shift+R instead",
LcRewordCommit: "reword commit",
LcDeleteCommit: "delete commit",
LcMoveDownCommit: "move commit down one",

View file

@ -69,7 +69,6 @@ func polishTranslationSet() TranslationSet {
YouNoCommitsToSquash: "Nie masz commitów do spłaszczenia",
Fixup: "Napraw",
SureFixupThisCommit: "Jesteś pewny, ze chcesz naprawić ten commit? Commit poniżej zostanie spłaszczony w górę wraz z tym",
OnlyRewordTopCommit: "Można zmienić nazwę tylko ostatniemu commitowi",
LcRewordCommit: "zmień nazwę commita",
LcRenameCommitEditor: "zmień nazwę commita w edytorze",
Error: "Błąd",