Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pkg/commands/git_commands/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's okay to leave this in English (as opposed to internationalising it). Do we know if github looks for this specific wording in order to render the commit differently?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, so in the commit message it looks for the metadata Co-authored-by and that's how it knows how to render the commit


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()
Expand Down
6 changes: 6 additions & 0 deletions pkg/commands/git_commands/rebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions pkg/gui/controllers/local_commits_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
})
}
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions pkg/i18n/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -674,6 +677,7 @@ type Actions struct {
AmendCommit string
ResetCommitAuthor string
SetCommitAuthor string
AddCommitCoAuthor string
RevertCommit string
CreateFixupCommit string
SquashAllAboveFixupCommits string
Expand Down Expand Up @@ -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 <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?",
RenameCommitEditor: "Reword commit with editor",
Error: "Error",
Expand Down
40 changes: 40 additions & 0 deletions pkg/integration/tests/commit/add_co_author.go
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>").
Confirm()
})

t.Views().Main().ContainsLines(
Contains("initial commit"),
Contains("Co-authored-by: John Smith <[email protected]>"),
)
},
})
1 change: 1 addition & 0 deletions pkg/integration/tests/test_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ var tests = []*components.IntegrationTest{
branch.Suggestions,
cherry_pick.CherryPick,
cherry_pick.CherryPickConflicts,
commit.AddCoAuthor,
commit.Amend,
commit.Commit,
commit.CommitMultiline,
Expand Down