diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2023-04-15 19:17:06 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-15 19:17:06 +1000 |
commit | 1efb565b22c17733acc8d39591cf3cdec54aa556 (patch) | |
tree | 7ba58add3688c35eddec5099c5bd8122b76a07b1 | |
parent | e18b4a4cc39e1f031936911910202b1925218117 (diff) | |
parent | 08a445eb9dbf447de4121220712279458e9020a5 (diff) |
Merge pull request #2370 from AzraelSec/rebase-with-todo-edit
-rw-r--r-- | pkg/app/daemon/daemon.go | 30 | ||||
-rw-r--r-- | pkg/commands/git_commands/patch.go | 6 | ||||
-rw-r--r-- | pkg/commands/git_commands/rebase.go | 65 | ||||
-rw-r--r-- | pkg/gui/controllers/helpers/merge_and_rebase_helper.go | 42 | ||||
-rw-r--r-- | pkg/i18n/chinese.go | 2 | ||||
-rw-r--r-- | pkg/i18n/dutch.go | 2 | ||||
-rw-r--r-- | pkg/i18n/english.go | 10 | ||||
-rw-r--r-- | pkg/i18n/korean.go | 2 | ||||
-rw-r--r-- | pkg/i18n/polish.go | 2 | ||||
-rw-r--r-- | pkg/integration/tests/branch/rebase.go | 6 | ||||
-rw-r--r-- | pkg/integration/tests/branch/rebase_and_drop.go | 6 | ||||
-rw-r--r-- | pkg/integration/tests/branch/rebase_does_not_autosquash.go | 6 | ||||
-rw-r--r-- | pkg/integration/tests/interactive_rebase/advanced_interactive_rebase.go | 65 | ||||
-rw-r--r-- | pkg/integration/tests/test_list.go | 1 |
14 files changed, 197 insertions, 48 deletions
diff --git a/pkg/app/daemon/daemon.go b/pkg/app/daemon/daemon.go index 182807b02..e040e79c2 100644 --- a/pkg/app/daemon/daemon.go +++ b/pkg/app/daemon/daemon.go @@ -29,6 +29,11 @@ const ( 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" ) type Daemon interface { @@ -74,12 +79,11 @@ type rebaseDaemon struct { 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] - if strings.HasSuffix(os.Args[1], "git-rebase-todo") { - if err := os.WriteFile(os.Args[1], []byte(os.Getenv(RebaseTODOEnvKey)), 0o644); err != nil { - return err - } - } else if strings.HasSuffix(os.Args[1], filepath.Join(gitDir(), "COMMIT_EDITMSG")) { // TODO: test + 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 { @@ -89,6 +93,22 @@ func (self *rebaseDaemon) Run() error { return nil } +func (self *rebaseDaemon) writeTodoFile(path string) error { + todoContent := []byte(os.Getenv(RebaseTODOEnvKey)) + + prependLines := os.Getenv(PrependLinesEnvKey) != "" + if prependLines { + existingContent, err := os.ReadFile(path) + if err != nil { + return err + } + + todoContent = append(todoContent, existingContent...) + } + + return os.WriteFile(path, todoContent, 0o644) +} + func gitDir() string { dir := env.GetGitDirEnv() if dir == "" { diff --git a/pkg/commands/git_commands/patch.go b/pkg/commands/git_commands/patch.go index 7511d1a5b..8c956529c 100644 --- a/pkg/commands/git_commands/patch.go +++ b/pkg/commands/git_commands/patch.go @@ -111,7 +111,11 @@ func (self *PatchCommands) MovePatchToSelectedCommit(commits []*models.Commit, s } }) - err := self.rebase.PrepareInteractiveRebaseCommand(commits[baseIndex].Sha, todoLines, true).Run() + err := self.rebase.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{ + baseShaOrRoot: commits[baseIndex].Sha, + todoLines: todoLines, + overrideEditor: true, + }).Run() if err != nil { return err } diff --git a/pkg/commands/git_commands/rebase.go b/pkg/commands/git_commands/rebase.go index e4c20426f..cffc5d68a 100644 --- a/pkg/commands/git_commands/rebase.go +++ b/pkg/commands/git_commands/rebase.go @@ -60,7 +60,10 @@ func (self *RebaseCommands) RewordCommitInEditor(commits []*models.Commit, index return nil, err } - return self.PrepareInteractiveRebaseCommand(sha, todo, false), nil + return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{ + baseShaOrRoot: sha, + todoLines: todo, + }), nil } func (self *RebaseCommands) ResetCommitAuthor(commits []*models.Commit, index int) error { @@ -104,7 +107,11 @@ func (self *RebaseCommands) MoveCommitDown(commits []*models.Commit, index int) baseShaOrRoot := getBaseShaOrRoot(commits, index+2) - return self.PrepareInteractiveRebaseCommand(baseShaOrRoot, todoLines, true).Run() + return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{ + baseShaOrRoot: baseShaOrRoot, + todoLines: todoLines, + overrideEditor: true, + }).Run() } func (self *RebaseCommands) InteractiveRebase(commits []*models.Commit, index int, action string) error { @@ -113,7 +120,11 @@ func (self *RebaseCommands) InteractiveRebase(commits []*models.Commit, index in return err } - return self.PrepareInteractiveRebaseCommand(sha, todo, true).Run() + return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{ + baseShaOrRoot: sha, + todoLines: todo, + overrideEditor: true, + }).Run() } func (self *RebaseCommands) InteractiveRebaseBreakAfter(commits []*models.Commit, index int) error { @@ -123,22 +134,46 @@ func (self *RebaseCommands) InteractiveRebaseBreakAfter(commits []*models.Commit } todo = append(todo, TodoLine{Action: "break", Commit: nil}) - return self.PrepareInteractiveRebaseCommand(sha, todo, true).Run() + return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{ + baseShaOrRoot: sha, + todoLines: todo, + overrideEditor: true, + }).Run() +} + +func (self *RebaseCommands) EditRebase(branchRef string) error { + commands := []TodoLine{{Action: "break"}} + return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{ + baseShaOrRoot: branchRef, + todoLines: commands, + prepend: true, + }).Run() +} + +type PrepareInteractiveRebaseCommandOpts struct { + baseShaOrRoot string + todoLines []TodoLine + 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(baseShaOrRoot string, todoLines []TodoLine, overrideEditor bool) oscommands.ICmdObj { - todo := self.buildTodo(todoLines) +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", baseShaOrRoot) + cmdStr := fmt.Sprintf("git rebase --interactive --autostash --keep-empty --empty=keep --no-autosquash %s", opts.baseShaOrRoot) self.Log.WithField("command", cmdStr).Debug("RunCommand") cmdObj := self.cmd.New(cmdStr) @@ -153,13 +188,14 @@ func (self *RebaseCommands) PrepareInteractiveRebaseCommand(baseShaOrRoot string 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 "GIT_SEQUENCE_EDITOR="+gitSequenceEditor, ) - if overrideEditor { + if opts.overrideEditor { cmdObj.AddEnvVars("GIT_EDITOR=" + ex) } @@ -273,12 +309,16 @@ func (self *RebaseCommands) BeginInteractiveRebaseForCommit(commits []*models.Co return err } - return self.PrepareInteractiveRebaseCommand(sha, todo, true).Run() + return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{ + baseShaOrRoot: sha, + todoLines: todo, + overrideEditor: true, + }).Run() } // RebaseBranch interactive rebases onto a branch func (self *RebaseCommands) RebaseBranch(branchName string) error { - return self.PrepareInteractiveRebaseCommand(branchName, nil, false).Run() + return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{baseShaOrRoot: branchName}).Run() } func (self *RebaseCommands) GenericMergeOrRebaseActionCmdObj(commandType string, command string) oscommands.ICmdObj { @@ -363,7 +403,10 @@ func (self *RebaseCommands) DiscardOldFileChanges(commits []*models.Commit, comm func (self *RebaseCommands) CherryPickCommits(commits []*models.Commit) error { todoLines := self.BuildTodoLinesSingleAction(commits, "pick") - return self.PrepareInteractiveRebaseCommand("HEAD", todoLines, false).Run() + return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{ + baseShaOrRoot: "HEAD", + todoLines: todoLines, + }).Run() } func (self *RebaseCommands) buildTodo(todoLines []TodoLine) string { diff --git a/pkg/gui/controllers/helpers/merge_and_rebase_helper.go b/pkg/gui/controllers/helpers/merge_and_rebase_helper.go index b70f7b2ed..981b85a0d 100644 --- a/pkg/gui/controllers/helpers/merge_and_rebase_helper.go +++ b/pkg/gui/controllers/helpers/merge_and_rebase_helper.go @@ -201,22 +201,42 @@ func (self *MergeAndRebaseHelper) RebaseOntoRef(ref string) error { if ref == checkedOutBranch { return self.c.ErrorMsg(self.c.Tr.CantRebaseOntoSelf) } - prompt := utils.ResolvePlaceholderString( - self.c.Tr.ConfirmRebase, + menuItems := []*types.MenuItem{ + { + Label: self.c.Tr.SimpleRebase, + Key: 's', + OnPress: func() error { + self.c.LogAction(self.c.Tr.Actions.RebaseBranch) + err := self.git.Rebase.RebaseBranch(ref) + return self.CheckMergeOrRebase(err) + }, + }, + { + Label: self.c.Tr.InteractiveRebase, + Key: 'i', + Tooltip: self.c.Tr.InteractiveRebaseTooltip, + OnPress: func() error { + self.c.LogAction(self.c.Tr.Actions.RebaseBranch) + err := self.git.Rebase.EditRebase(ref) + if err = self.CheckMergeOrRebase(err); err != nil { + return err + } + return self.c.PushContext(self.contexts.LocalCommits) + }, + }, + } + + title := utils.ResolvePlaceholderString( + self.c.Tr.RebasingTitle, map[string]string{ "checkedOutBranch": checkedOutBranch, - "selectedBranch": ref, + "ref": ref, }, ) - return self.c.Confirm(types.ConfirmOpts{ - Title: self.c.Tr.RebasingTitle, - Prompt: prompt, - HandleConfirm: func() error { - self.c.LogAction(self.c.Tr.Actions.RebaseBranch) - err := self.git.Rebase.RebaseBranch(ref) - return self.CheckMergeOrRebase(err) - }, + return self.c.Menu(types.CreateMenuOptions{ + Title: title, + Items: menuItems, }) } diff --git a/pkg/i18n/chinese.go b/pkg/i18n/chinese.go index dbc8122e9..2d300caf7 100644 --- a/pkg/i18n/chinese.go +++ b/pkg/i18n/chinese.go @@ -201,8 +201,6 @@ func chineseTranslationSet() TranslationSet { ReflogCommitsTitle: "Reflog 页面", GlobalTitle: "全局键绑定", ConflictsResolved: "已解决所有冲突。是否继续?", - RebasingTitle: "变基", - ConfirmRebase: "您确定要将分支 {{.checkedOutBranch}} 变基到 {{.selectedBranch}} 吗?", ConfirmMerge: "您确定要将分支 {{.selectedBranch}} 合并到 {{.checkedOutBranch}} 吗?", FwdNoUpstream: "此分支没有上游,无法快进", FwdNoLocalUpstream: "此分支的远程未在本地注册,无法快进", diff --git a/pkg/i18n/dutch.go b/pkg/i18n/dutch.go index d8fcb5338..f52908cea 100644 --- a/pkg/i18n/dutch.go +++ b/pkg/i18n/dutch.go @@ -166,9 +166,7 @@ func dutchTranslationSet() TranslationSet { ReflogCommitsTitle: "Reflog", GlobalTitle: "Globale Sneltoetsen", ConflictsResolved: "alle merge conflicten zijn opgelost. Wilt je verder gaan?", - RebasingTitle: "Rebasen", MergingTitle: "Mergen", - ConfirmRebase: "Weet je zeker dat je '{{.checkedOutBranch}}' op '{{.selectedBranch}}' wil rebasen?", ConfirmMerge: "Weet je zeker dat je '{{.selectedBranch}}' in '{{.checkedOutBranch}}' wil mergen?", FwdNoUpstream: "Kan niet de branch vooruitspoelen zonder upstream", FwdCommitsToPush: "Je kan niet vooruitspoelen als de branch geen nieuwe commits heeft", diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 80179f365..4d384da71 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -209,7 +209,9 @@ type TranslationSet struct { ReflogCommitsTitle string ConflictsResolved string RebasingTitle string - ConfirmRebase string + SimpleRebase string + InteractiveRebase string + InteractiveRebaseTooltip string ConfirmMerge string FwdNoUpstream string FwdNoLocalUpstream string @@ -877,8 +879,10 @@ func EnglishTranslationSet() TranslationSet { ReflogCommitsTitle: "Reflog", GlobalTitle: "Global Keybindings", ConflictsResolved: "all merge conflicts resolved. Continue?", - RebasingTitle: "Rebasing", - ConfirmRebase: "Are you sure you want to rebase '{{.checkedOutBranch}}' on top of '{{.selectedBranch}}'?", + RebasingTitle: "Rebase '{{.checkedOutBranch}}' onto '{{.ref}}'", + SimpleRebase: "simple rebase", + InteractiveRebase: "interactive rebase", + InteractiveRebaseTooltip: "Begin an interactive rebase with a break at the start, so you can update the TODO commits before continuing", ConfirmMerge: "Are you sure you want to merge '{{.selectedBranch}}' into '{{.checkedOutBranch}}'?", FwdNoUpstream: "Cannot fast-forward a branch with no upstream", FwdNoLocalUpstream: "Cannot fast-forward a branch whose remote is not registered locally", diff --git a/pkg/i18n/korean.go b/pkg/i18n/korean.go index fa649bcc0..88e7db8f6 100644 --- a/pkg/i18n/korean.go +++ b/pkg/i18n/korean.go @@ -202,8 +202,6 @@ func koreanTranslationSet() TranslationSet { ReflogCommitsTitle: "Reflog", GlobalTitle: "글로벌 키 바인딩", ConflictsResolved: "모든 병합 충돌이 해결되었습니다. 계속 할까요?", - RebasingTitle: "리베이스 중", - ConfirmRebase: "정말로 '{{.checkedOutBranch}}' 을(를) '{{.selectedBranch}}'에 리베이스 하시겠습니까?", ConfirmMerge: "정말로 '{{.selectedBranch}}' 을(를) '{{.checkedOutBranch}}'에 병합하시겠습니까?", FwdNoUpstream: "Cannot fast-forward a branch with no upstream", FwdNoLocalUpstream: "Cannot fast-forward a branch whose remote is not registered locally", diff --git a/pkg/i18n/polish.go b/pkg/i18n/polish.go index 3db26a22c..00ed9f38f 100644 --- a/pkg/i18n/polish.go +++ b/pkg/i18n/polish.go @@ -112,9 +112,7 @@ func polishTranslationSet() TranslationSet { FileStagingRequirements: "Można tylko zatwierdzić pojedyncze linie dla śledzonych plików z niezatwierdzonymi zmianami", StagingTitle: "Poczekalnia", ReturnToFilesPanel: "wróć do panelu plików", - RebasingTitle: "Zmiana bazy", MergingTitle: "Scalanie", - ConfirmRebase: "Czy napewno chcesz zmienić bazę '{{.checkedOutBranch}}' na '{{.selectedBranch}}'?", ConfirmMerge: "Czy na pewno chcesz scalić '{{.selectedBranch}}' do '{{.checkedOutBranch}}'?", FwdNoUpstream: "Nie można przewinąć gałęzi bez gałęzi nadrzędnej", FwdCommitsToPush: "Nie można przewinąć gałęzi z commitami do wysłania", diff --git a/pkg/integration/tests/branch/rebase.go b/pkg/integration/tests/branch/rebase.go index 07b9037a3..c34e73c2d 100644 --- a/pkg/integration/tests/branch/rebase.go +++ b/pkg/integration/tests/branch/rebase.go @@ -30,9 +30,9 @@ var Rebase = NewIntegrationTest(NewIntegrationTestArgs{ SelectNextItem(). Press(keys.Branches.RebaseBranch) - t.ExpectPopup().Confirmation(). - Title(Equals("Rebasing")). - Content(Contains("Are you sure you want to rebase 'first-change-branch' on top of 'second-change-branch'?")). + t.ExpectPopup().Menu(). + Title(Equals("Rebase 'first-change-branch' onto 'second-change-branch'")). + Select(Contains("simple rebase")). Confirm() t.Common().AcknowledgeConflicts() diff --git a/pkg/integration/tests/branch/rebase_and_drop.go b/pkg/integration/tests/branch/rebase_and_drop.go index 298636c59..8a1ade2fe 100644 --- a/pkg/integration/tests/branch/rebase_and_drop.go +++ b/pkg/integration/tests/branch/rebase_and_drop.go @@ -36,9 +36,9 @@ var RebaseAndDrop = NewIntegrationTest(NewIntegrationTestArgs{ SelectNextItem(). Press(keys.Branches.RebaseBranch) - t.ExpectPopup().Confirmation(). - Title(Equals("Rebasing")). - Content(Contains("Are you sure you want to rebase 'first-change-branch' on top of 'second-change-branch'?")). + t.ExpectPopup().Menu(). + Title(Equals("Rebase 'first-change-branch' onto 'second-change-branch'")). + Select(Contains("simple rebase")). Confirm() t.Views().Information().Content(Contains("rebasing")) diff --git a/pkg/integration/tests/branch/rebase_does_not_autosquash.go b/pkg/integration/tests/branch/rebase_does_not_autosquash.go index 78c957a13..2638bec7b 100644 --- a/pkg/integration/tests/branch/rebase_does_not_autosquash.go +++ b/pkg/integration/tests/branch/rebase_does_not_autosquash.go @@ -39,9 +39,9 @@ var RebaseDoesNotAutosquash = NewIntegrationTest(NewIntegrationTestArgs{ SelectNextItem(). Press(keys.Branches.RebaseBranch) - t.ExpectPopup().Confirmation(). - Title(Equals("Rebasing")). - Content(Contains("Are you sure you want to rebase 'my-branch' on top of 'master'?")). + t.ExpectPopup().Menu(). + Title(Equals("Rebase 'my-branch' onto 'master'")). + Select(Contains("simple rebase")). Confirm() t.Views().Commits().Lines( diff --git a/pkg/integration/tests/interactive_rebase/advanced_interactive_rebase.go b/pkg/integration/tests/interactive_rebase/advanced_interactive_rebase.go new file mode 100644 index 000000000..5ebc86d52 --- /dev/null +++ b/pkg/integration/tests/interactive_rebase/advanced_interactive_rebase.go @@ -0,0 +1,65 @@ +package interactive_rebase + +import ( + "fmt" + + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +const ( + BASE_BRANCH = "base-branch" + TOP_BRANCH = "top-branch" + BASE_COMMIT = "base-commit" + TOP_COMMIT = "top-commit" +) + +var AdvancedInteractiveRebase = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "It begins an interactive rebase and verifies to have the possibility of editing the commits of the branch before proceeding with the actual rebase", + ExtraCmdArgs: "", + SetupConfig: func(config *config.AppConfig) {}, + SetupRepo: func(shell *Shell) { + shell. + NewBranch(BASE_BRANCH). + EmptyCommit(BASE_COMMIT). + NewBranch(TOP_BRANCH). + EmptyCommit(TOP_COMMIT) + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Commits(). + Focus(). + Lines( + Contains(TOP_COMMIT), + Contains(BASE_COMMIT), + ) + + t.Views().Branches(). + Focus(). + NavigateToLine(Contains(BASE_BRANCH)). + Press(keys.Branches.RebaseBranch) + + t.ExpectPopup().Menu(). + Title(Equals(fmt.Sprintf("Rebase '%s' onto '%s'", TOP_BRANCH, BASE_BRANCH))). + Select(Contains("interactive rebase")). + Confirm() + t.Views().Commits(). + IsFocused(). + Lines( + Contains(TOP_COMMIT), + Contains(BASE_COMMIT).Contains("YOU ARE HERE"), + ). + NavigateToLine(Contains(TOP_COMMIT)). + Press(keys.Universal.Edit). + Lines( + Contains(TOP_COMMIT).Contains("edit"), + Contains(BASE_COMMIT).Contains("YOU ARE HERE"), + ). + Tap(func() { + t.Common().ContinueRebase() + }). + Lines( + Contains(TOP_COMMIT).Contains("YOU ARE HERE"), + Contains(BASE_COMMIT), + ) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index a8959ee24..d4da79732 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -83,6 +83,7 @@ var tests = []*components.IntegrationTest{ filter_by_path.CliArg, filter_by_path.SelectFile, filter_by_path.TypeFile, + interactive_rebase.AdvancedInteractiveRebase, interactive_rebase.AmendFirstCommit, interactive_rebase.AmendHeadCommitDuringRebase, interactive_rebase.AmendMerge, |