From 4a2508e96095c86d51e65df14199aec4d0eadcc6 Mon Sep 17 00:00:00 2001 From: Neko Box Coder Date: Sat, 10 Aug 2024 13:25:35 +0100 Subject: [PATCH] Allow rewording for last commit using GPG --- pkg/commands/git_commands/commit.go | 4 ++-- pkg/commands/git_commands/commit_test.go | 2 +- pkg/commands/git_commands/rebase.go | 7 +------ pkg/gui/controllers/local_commits_controller.go | 11 ++++++++++- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/pkg/commands/git_commands/commit.go b/pkg/commands/git_commands/commit.go index 4153dfeb9..1ff7c095f 100644 --- a/pkg/commands/git_commands/commit.go +++ b/pkg/commands/git_commands/commit.go @@ -117,7 +117,7 @@ func (self *CommitCommands) CommitInEditorWithMessageFileCmdObj(tmpMessageFile s } // RewordLastCommit rewords the topmost commit with the given message -func (self *CommitCommands) RewordLastCommit(summary string, description string) error { +func (self *CommitCommands) RewordLastCommit(summary string, description string) oscommands.ICmdObj { messageArgs := self.commitMessageArgs(summary, description) cmdArgs := NewGitCmd("commit"). @@ -125,7 +125,7 @@ func (self *CommitCommands) RewordLastCommit(summary string, description string) Arg(messageArgs...). ToArgv() - return self.cmd.New(cmdArgs).Run() + return self.cmd.New(cmdArgs) } func (self *CommitCommands) commitMessageArgs(summary string, description string) []string { diff --git a/pkg/commands/git_commands/commit_test.go b/pkg/commands/git_commands/commit_test.go index 239d7fa8f..3e2c9e664 100644 --- a/pkg/commands/git_commands/commit_test.go +++ b/pkg/commands/git_commands/commit_test.go @@ -33,7 +33,7 @@ func TestCommitRewordCommit(t *testing.T) { t.Run(s.testName, func(t *testing.T) { instance := buildCommitCommands(commonDeps{runner: s.runner}) - assert.NoError(t, instance.RewordLastCommit(s.summary, s.description)) + assert.NoError(t, instance.RewordLastCommit(s.summary, s.description).Run()) s.runner.CheckForMissingCalls() }) } diff --git a/pkg/commands/git_commands/rebase.go b/pkg/commands/git_commands/rebase.go index 019d58e3a..56537e92f 100644 --- a/pkg/commands/git_commands/rebase.go +++ b/pkg/commands/git_commands/rebase.go @@ -39,18 +39,13 @@ func (self *RebaseCommands) RewordCommit(commits []*models.Commit, index int, su return errors.New(self.Tr.DisabledForGPG) } - if models.IsHeadCommit(commits, index) { - // we've selected the top commit so no rebase is required - return self.commit.RewordLastCommit(summary, description) - } - err := self.BeginInteractiveRebaseForCommit(commits, index, false) 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(summary, description) + err = self.commit.RewordLastCommit(summary, description).Run() if err != nil { return err } diff --git a/pkg/gui/controllers/local_commits_controller.go b/pkg/gui/controllers/local_commits_controller.go index f95062fdb..913601b5c 100644 --- a/pkg/gui/controllers/local_commits_controller.go +++ b/pkg/gui/controllers/local_commits_controller.go @@ -400,7 +400,16 @@ func (self *LocalCommitsController) switchFromCommitMessagePanelToEditor(filepat } func (self *LocalCommitsController) handleReword(summary string, description string) error { - err := self.c.Git().Rebase.RewordCommit(self.c.Model().Commits, self.c.Contexts().LocalCommits.GetSelectedLineIdx(), summary, description) + var err error + + if models.IsHeadCommit(self.c.Model().Commits, self.c.Contexts().LocalCommits.GetSelectedLineIdx()) { + // we've selected the top commit so no rebase is required + err = self.c.Helpers().GPG.WithGpgHandling(self.c.Git().Commit.RewordLastCommit(summary, description), + self.c.Tr.CommittingStatus, nil) + } else { + err = self.c.Git().Rebase.RewordCommit(self.c.Model().Commits, self.c.Contexts().LocalCommits.GetSelectedLineIdx(), summary, description) + } + if err != nil { return err }