diff --git a/pkg/commands/git_commands/commit.go b/pkg/commands/git_commands/commit.go index 1e0a442f2..8d673ee2c 100644 --- a/pkg/commands/git_commands/commit.go +++ b/pkg/commands/git_commands/commit.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/go-errors/errors" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" ) @@ -80,6 +81,27 @@ func (self *CommitCommands) GetCommitDiff(commitSha string) (string, error) { return diff, err } +type Author struct { + Name string + Email string +} + +func (self *CommitCommands) GetCommitAuthor(commitSha string) (Author, error) { + cmdStr := "git show --no-patch --pretty=format:'%an|%ae' " + commitSha + output, err := self.cmd.New(cmdStr).DontLog().RunWithOutput() + if err != nil { + return Author{}, err + } + + split := strings.Split(strings.TrimSpace(output), "|") + if len(split) < 2 { + return Author{}, errors.New("unexpected git output") + } + + author := Author{Name: split[0], Email: split[1]} + return author, err +} + func (self *CommitCommands) GetCommitMessageFirstLine(sha string) (string, error) { return self.GetCommitMessagesFirstLine([]string{sha}) } diff --git a/pkg/commands/models/commit.go b/pkg/commands/models/commit.go index da992f65a..641cc4242 100644 --- a/pkg/commands/models/commit.go +++ b/pkg/commands/models/commit.go @@ -17,7 +17,7 @@ type Commit struct { Action string // one of "", "pick", "edit", "squash", "reword", "drop", "fixup" Tags []string ExtraInfo string // something like 'HEAD -> master, tag: v0.15.2' - Author string + Author string // something like 'Jesse Duffield ' UnixTimestamp int64 // SHAs of parent commits (will be multiple if it's a merge commit) diff --git a/pkg/gui/controllers/basic_commits_controller.go b/pkg/gui/controllers/basic_commits_controller.go index a951349cc..66455eecf 100644 --- a/pkg/gui/controllers/basic_commits_controller.go +++ b/pkg/gui/controllers/basic_commits_controller.go @@ -1,6 +1,8 @@ package controllers import ( + "fmt" + "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/gui/types" ) @@ -126,6 +128,13 @@ func (self *BasicCommitsController) copyCommitAttribute(commit *models.Commit) e }, Key: 'm', }, + { + DisplayString: self.c.Tr.LcCommitAuthor, + OnPress: func() error { + return self.copyAuthorToClipboard(commit) + }, + Key: 'a', + }, }, }) } @@ -170,6 +179,23 @@ func (self *BasicCommitsController) copyCommitDiffToClipboard(commit *models.Com return nil } +func (self *BasicCommitsController) copyAuthorToClipboard(commit *models.Commit) error { + author, err := self.git.Commit.GetCommitAuthor(commit.Sha) + if err != nil { + return self.c.Error(err) + } + + formattedAuthor := fmt.Sprintf("%s <%s>", author.Name, author.Email) + + self.c.LogAction(self.c.Tr.Actions.CopyCommitAuthorToClipboard) + if err := self.os.CopyToClipboard(formattedAuthor); err != nil { + return self.c.Error(err) + } + + self.c.Toast(self.c.Tr.CommitAuthorCopiedToClipboard) + return nil +} + func (self *BasicCommitsController) copyCommitMessageToClipboard(commit *models.Commit) error { message, err := self.git.Commit.GetCommitMessage(commit.Sha) if err != nil { diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index a341447dc..31d88197f 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -379,6 +379,7 @@ type TranslationSet struct { LcCommitURL string LcCopyCommitMessageToClipboard string LcCommitMessage string + LcCommitAuthor string LcCopyCommitAttributeToClipboard string LcCopyBranchNameToClipboard string LcCopyFileNameToClipboard string @@ -435,6 +436,7 @@ type TranslationSet struct { CommitSHACopiedToClipboard string CommitURLCopiedToClipboard string CommitMessageCopiedToClipboard string + CommitAuthorCopiedToClipboard string LcCopiedToClipboard string ErrCannotEditDirectory string ErrStageDirWithInlineMergeConflicts string @@ -526,6 +528,7 @@ type Actions struct { CopyCommitDiffToClipboard string CopyCommitSHAToClipboard string CopyCommitURLToClipboard string + CopyCommitAuthorToClipboard string CopyCommitAttributeToClipboard string CustomCommand string DiscardAllChangesInDirectory string @@ -980,6 +983,7 @@ func EnglishTranslationSet() TranslationSet { LcCommitURL: "commit URL", LcCopyCommitMessageToClipboard: "copy commit message to clipboard", LcCommitMessage: "commit message", + LcCommitAuthor: "commit author", LcCopyCommitAttributeToClipboard: "copy commit attribute", LcCopyBranchNameToClipboard: "copy branch name to clipboard", LcCopyFileNameToClipboard: "copy the file name to the clipboard", @@ -1035,6 +1039,7 @@ func EnglishTranslationSet() TranslationSet { CommitSHACopiedToClipboard: "Commit SHA copied to clipboard", CommitURLCopiedToClipboard: "Commit URL copied to clipboard", CommitMessageCopiedToClipboard: "Commit message copied to clipboard", + CommitAuthorCopiedToClipboard: "Commit author copied to clipboard", LcCopiedToClipboard: "copied to clipboard", ErrCannotEditDirectory: "Cannot edit directory: you can only edit individual files", ErrStageDirWithInlineMergeConflicts: "Cannot stage/unstage directory containing files with inline merge conflicts. Please fix up the merge conflicts first", @@ -1107,6 +1112,7 @@ func EnglishTranslationSet() TranslationSet { CopyCommitDiffToClipboard: "Copy commit diff to clipboard", CopyCommitSHAToClipboard: "Copy commit SHA to clipboard", CopyCommitURLToClipboard: "Copy commit URL to clipboard", + CopyCommitAuthorToClipboard: "Copy commit author to clipboard", CopyCommitAttributeToClipboard: "Copy to clipboard", MoveCommitUp: "Move commit up", MoveCommitDown: "Move commit down",