From db409fa69fe3a04ba99ab83cd2e2cac085827b30 Mon Sep 17 00:00:00 2001 From: Orlando Maussa Date: Sat, 9 Sep 2023 07:18:47 -0500 Subject: [PATCH] Add coauthor (#2) Add co-author to commits Add addCoAuthor command for commits - Implement the `addCoAuthor` command to add co-authors to commits. - Utilize suggestions helpers to populate author names from the suggestions list. - Added command to gui at `LocalCommitsController`. This commit introduces the `addCoAuthor` command, which allows users to easily add co-authors to their commits. The co-author names are populated from the suggestions list, minimizing the chances of user input errors. The co-authors are added using the Co-authored-by metadata format recognized by GitHub and GitLab. --- pkg/commands/git_commands/commit.go | 16 ++++++++ pkg/commands/git_commands/rebase.go | 6 +++ .../controllers/local_commits_controller.go | 22 ++++++++++ pkg/i18n/english.go | 7 ++++ pkg/integration/tests/commit/add_co_author.go | 40 +++++++++++++++++++ pkg/integration/tests/test_list.go | 1 + 6 files changed, 92 insertions(+) create mode 100644 pkg/integration/tests/commit/add_co_author.go diff --git a/pkg/commands/git_commands/commit.go b/pkg/commands/git_commands/commit.go index 2f8446b1a..7a218e4e1 100644 --- a/pkg/commands/git_commands/commit.go +++ b/pkg/commands/git_commands/commit.go @@ -38,6 +38,22 @@ func (self *CommitCommands) SetAuthor(value string) error { return self.cmd.New(cmdArgs).Run() } +// Add a commit's coauthor using Github/Gitlab Co-authored-by metadata. Value is expected to be of the form 'Name ' +func (self *CommitCommands) AddCoAuthor(sha string, value string) error { + message, err := self.GetCommitMessage(sha) + if err != nil { + return err + } + + message = message + fmt.Sprintf("\nCo-authored-by: %s", value) + + cmdArgs := NewGitCmd("commit"). + Arg("--allow-empty", "--amend", "--only", "-m", message). + ToArgv() + + return self.cmd.New(cmdArgs).Run() +} + // ResetToCommit reset to commit func (self *CommitCommands) ResetToCommit(sha string, strength string, envVars []string) error { cmdArgs := NewGitCmd("reset").Arg("--"+strength, sha).ToArgv() diff --git a/pkg/commands/git_commands/rebase.go b/pkg/commands/git_commands/rebase.go index 5732bc0d3..66adee2af 100644 --- a/pkg/commands/git_commands/rebase.go +++ b/pkg/commands/git_commands/rebase.go @@ -79,6 +79,12 @@ func (self *RebaseCommands) SetCommitAuthor(commits []*models.Commit, index int, }) } +func (self *RebaseCommands) AddCommitCoAuthor(commits []*models.Commit, index int, value string) error { + return self.GenericAmend(commits, index, func() error { + return self.commit.AddCoAuthor(commits[index].Sha, value) + }) +} + func (self *RebaseCommands) GenericAmend(commits []*models.Commit, index int, f func() error) error { if models.IsHeadCommit(commits, index) { // we've selected the top commit so no rebase is required diff --git a/pkg/gui/controllers/local_commits_controller.go b/pkg/gui/controllers/local_commits_controller.go index a110310f7..1bd079f53 100644 --- a/pkg/gui/controllers/local_commits_controller.go +++ b/pkg/gui/controllers/local_commits_controller.go @@ -621,6 +621,12 @@ func (self *LocalCommitsController) amendAttribute(commit *models.Commit) error Key: 'A', Tooltip: "Set the author based on a prompt", }, + { + Label: self.c.Tr.AddCoAuthor, + OnPress: self.addCoAuthor, + Key: 'c', + Tooltip: self.c.Tr.AddCoAuthorTooltip, + }, }, }) } @@ -653,6 +659,22 @@ func (self *LocalCommitsController) setAuthor() error { }) } +func (self *LocalCommitsController) addCoAuthor() error { + return self.c.Prompt(types.PromptOpts{ + Title: self.c.Tr.AddCoAuthorPromptTitle, + FindSuggestionsFunc: self.c.Helpers().Suggestions.GetAuthorsSuggestionsFunc(), + HandleConfirm: func(value string) error { + return self.c.WithWaitingStatus(self.c.Tr.AmendingStatus, func(gocui.Task) error { + self.c.LogAction(self.c.Tr.Actions.AddCommitCoAuthor) + if err := self.c.Git().Rebase.AddCommitCoAuthor(self.c.Model().Commits, self.context().GetSelectedLineIdx(), value); err != nil { + return self.c.Error(err) + } + return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC}) + }) + }, + }) +} + func (self *LocalCommitsController) revert(commit *models.Commit) error { if commit.IsMerge() { return self.createRevertMergeCommitMenu(commit) diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 87ca8c8ca..09577c210 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -103,8 +103,11 @@ type TranslationSet struct { AmendToCommit string ResetAuthor string SetAuthor string + AddCoAuthor string SetResetCommitAuthor string SetAuthorPromptTitle string + AddCoAuthorPromptTitle string + AddCoAuthorTooltip string SureResetCommitAuthor string RenameCommitEditor string NoCommitsThisBranch string @@ -674,6 +677,7 @@ type Actions struct { AmendCommit string ResetCommitAuthor string SetCommitAuthor string + AddCommitCoAuthor string RevertCommit string CreateFixupCommit string SquashAllAboveFixupCommits string @@ -890,8 +894,11 @@ func EnglishTranslationSet() TranslationSet { AmendToCommit: "Amend commit with staged changes", ResetAuthor: "Reset author", SetAuthor: "Set author", + AddCoAuthor: "Add co-author", SetResetCommitAuthor: "Set/Reset commit author", SetAuthorPromptTitle: "Set author (must look like 'Name ')", + AddCoAuthorPromptTitle: "Add co-author (must look like 'Name ')", + AddCoAuthorTooltip: "Add co-author using the Github/Gitlab metadata Co-authored-by", SureResetCommitAuthor: "The author field of this commit will be updated to match the configured user. This also renews the author timestamp. Continue?", RenameCommitEditor: "Reword commit with editor", Error: "Error", diff --git a/pkg/integration/tests/commit/add_co_author.go b/pkg/integration/tests/commit/add_co_author.go new file mode 100644 index 000000000..3a2021dac --- /dev/null +++ b/pkg/integration/tests/commit/add_co_author.go @@ -0,0 +1,40 @@ +package commit + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var AddCoAuthor = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Add co-author on a commit", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) {}, + SetupRepo: func(shell *Shell) { + shell.EmptyCommit("initial commit") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Commits(). + Focus(). + Lines( + Contains("initial commit").IsSelected(), + ). + Press(keys.Commits.ResetCommitAuthor). + Tap(func() { + t.ExpectPopup().Menu(). + Title(Equals("Amend commit attribute")). + Select(Contains("Add co-author")). + Confirm() + + t.ExpectPopup().Prompt(). + Title(Contains("Add co-author")). + Type("John Smith "). + Confirm() + }) + + t.Views().Main().ContainsLines( + Contains("initial commit"), + Contains("Co-authored-by: John Smith "), + ) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index 6a694c753..b820555f2 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -55,6 +55,7 @@ var tests = []*components.IntegrationTest{ branch.Suggestions, cherry_pick.CherryPick, cherry_pick.CherryPickConflicts, + commit.AddCoAuthor, commit.Amend, commit.Commit, commit.CommitMultiline,