From 98e6c119f52fbcef9c3df5ff33247b7322937005 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Thu, 3 Aug 2023 19:22:51 +0200 Subject: [PATCH] Select same commit again after pressing "e" to edit a commit When editing a commit, the index of the current commit can change; for example, when merge commits are involved, or when working with stacked branches where "update-ref" commands may be added above the selected commit. Reselect the current commit after pressing "e"; this requires doing the refresh blocking on the main thread. (Another option that I considered was to use a SYNC refresh, and then select the new line with an OnUIThread inside the Then function. This also works, but results in a very visible lag.) --- .../controllers/helpers/merge_and_rebase_helper.go | 8 ++++++-- pkg/gui/controllers/local_commits_controller.go | 11 ++++++++++- .../drop_todo_commit_with_update_ref.go | 4 ++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/pkg/gui/controllers/helpers/merge_and_rebase_helper.go b/pkg/gui/controllers/helpers/merge_and_rebase_helper.go index 4c7b087aa..a88615271 100644 --- a/pkg/gui/controllers/helpers/merge_and_rebase_helper.go +++ b/pkg/gui/controllers/helpers/merge_and_rebase_helper.go @@ -125,8 +125,8 @@ func isMergeConflictErr(errStr string) bool { return false } -func (self *MergeAndRebaseHelper) CheckMergeOrRebase(result error) error { - if err := self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC}); err != nil { +func (self *MergeAndRebaseHelper) CheckMergeOrRebaseWithRefreshOptions(result error, refreshOptions types.RefreshOptions) error { + if err := self.c.Refresh(refreshOptions); err != nil { return err } if result == nil { @@ -143,6 +143,10 @@ func (self *MergeAndRebaseHelper) CheckMergeOrRebase(result error) error { } } +func (self *MergeAndRebaseHelper) CheckMergeOrRebase(result error) error { + return self.CheckMergeOrRebaseWithRefreshOptions(result, types.RefreshOptions{Mode: types.ASYNC}) +} + func (self *MergeAndRebaseHelper) CheckForConflicts(result error) error { if result == nil { return nil diff --git a/pkg/gui/controllers/local_commits_controller.go b/pkg/gui/controllers/local_commits_controller.go index 38ddaa515..b3ef598d2 100644 --- a/pkg/gui/controllers/local_commits_controller.go +++ b/pkg/gui/controllers/local_commits_controller.go @@ -392,7 +392,16 @@ func (self *LocalCommitsController) edit(commit *models.Commit) error { return self.c.WithWaitingStatus(self.c.Tr.RebasingStatus, func(gocui.Task) error { self.c.LogAction(self.c.Tr.Actions.EditCommit) err := self.c.Git().Rebase.EditRebase(commit.Sha) - return self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err) + return self.c.Helpers().MergeAndRebase.CheckMergeOrRebaseWithRefreshOptions( + err, + types.RefreshOptions{Mode: types.BLOCK_UI, Then: func() { + _, index, ok := lo.FindIndexOf(self.c.Model().Commits, func(c *models.Commit) bool { + return c.Sha == commit.Sha + }) + if ok { + self.context().SetSelectedLineIdx(index) + } + }}) }) } diff --git a/pkg/integration/tests/interactive_rebase/drop_todo_commit_with_update_ref.go b/pkg/integration/tests/interactive_rebase/drop_todo_commit_with_update_ref.go index b1eb20ba4..de085710a 100644 --- a/pkg/integration/tests/interactive_rebase/drop_todo_commit_with_update_ref.go +++ b/pkg/integration/tests/interactive_rebase/drop_todo_commit_with_update_ref.go @@ -44,8 +44,8 @@ var DropTodoCommitWithUpdateRef = NewIntegrationTest(NewIntegrationTestArgs{ Contains("pick").Contains("CI commit 05"), Contains("update-ref").Contains("branch1").DoesNotContain("*"), Contains("pick").Contains("CI * commit 04"), - Contains("pick").Contains("CI commit 03").IsSelected(), // wrong line selected - Contains("<-- YOU ARE HERE --- commit 02"), + Contains("pick").Contains("CI commit 03"), + Contains("<-- YOU ARE HERE --- commit 02").IsSelected(), Contains("CI commit 01"), ). NavigateToLine(Contains("commit 06")).