1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-05-21 22:43:27 +02:00

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.
This commit is contained in:
Orlando Maussa 2023-09-09 07:18:47 -05:00 committed by GitHub
parent d7e2ca3f10
commit db409fa69f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 92 additions and 0 deletions

View File

@ -38,6 +38,22 @@ func (self *CommitCommands) SetAuthor(value string) error {
return self.cmd.New(cmdArgs).Run() 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 <Email>'
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 // ResetToCommit reset to commit
func (self *CommitCommands) ResetToCommit(sha string, strength string, envVars []string) error { func (self *CommitCommands) ResetToCommit(sha string, strength string, envVars []string) error {
cmdArgs := NewGitCmd("reset").Arg("--"+strength, sha).ToArgv() cmdArgs := NewGitCmd("reset").Arg("--"+strength, sha).ToArgv()

View File

@ -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 { func (self *RebaseCommands) GenericAmend(commits []*models.Commit, index int, f func() error) error {
if models.IsHeadCommit(commits, index) { if models.IsHeadCommit(commits, index) {
// we've selected the top commit so no rebase is required // we've selected the top commit so no rebase is required

View File

@ -621,6 +621,12 @@ func (self *LocalCommitsController) amendAttribute(commit *models.Commit) error
Key: 'A', Key: 'A',
Tooltip: "Set the author based on a prompt", 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 { func (self *LocalCommitsController) revert(commit *models.Commit) error {
if commit.IsMerge() { if commit.IsMerge() {
return self.createRevertMergeCommitMenu(commit) return self.createRevertMergeCommitMenu(commit)

View File

@ -103,8 +103,11 @@ type TranslationSet struct {
AmendToCommit string AmendToCommit string
ResetAuthor string ResetAuthor string
SetAuthor string SetAuthor string
AddCoAuthor string
SetResetCommitAuthor string SetResetCommitAuthor string
SetAuthorPromptTitle string SetAuthorPromptTitle string
AddCoAuthorPromptTitle string
AddCoAuthorTooltip string
SureResetCommitAuthor string SureResetCommitAuthor string
RenameCommitEditor string RenameCommitEditor string
NoCommitsThisBranch string NoCommitsThisBranch string
@ -674,6 +677,7 @@ type Actions struct {
AmendCommit string AmendCommit string
ResetCommitAuthor string ResetCommitAuthor string
SetCommitAuthor string SetCommitAuthor string
AddCommitCoAuthor string
RevertCommit string RevertCommit string
CreateFixupCommit string CreateFixupCommit string
SquashAllAboveFixupCommits string SquashAllAboveFixupCommits string
@ -890,8 +894,11 @@ func EnglishTranslationSet() TranslationSet {
AmendToCommit: "Amend commit with staged changes", AmendToCommit: "Amend commit with staged changes",
ResetAuthor: "Reset author", ResetAuthor: "Reset author",
SetAuthor: "Set author", SetAuthor: "Set author",
AddCoAuthor: "Add co-author",
SetResetCommitAuthor: "Set/Reset commit author", SetResetCommitAuthor: "Set/Reset commit author",
SetAuthorPromptTitle: "Set author (must look like 'Name <Email>')", SetAuthorPromptTitle: "Set author (must look like 'Name <Email>')",
AddCoAuthorPromptTitle: "Add co-author (must look like 'Name <Email>')",
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?", 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", RenameCommitEditor: "Reword commit with editor",
Error: "Error", Error: "Error",

View File

@ -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 <jsmith@gmail.com>").
Confirm()
})
t.Views().Main().ContainsLines(
Contains("initial commit"),
Contains("Co-authored-by: John Smith <jsmith@gmail.com>"),
)
},
})

View File

@ -55,6 +55,7 @@ var tests = []*components.IntegrationTest{
branch.Suggestions, branch.Suggestions,
cherry_pick.CherryPick, cherry_pick.CherryPick,
cherry_pick.CherryPickConflicts, cherry_pick.CherryPickConflicts,
commit.AddCoAuthor,
commit.Amend, commit.Amend,
commit.Commit, commit.Commit,
commit.CommitMultiline, commit.CommitMultiline,