1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-03 13:21:56 +02:00

Merge pull request #1904 from jesseduffield/yank-author

This commit is contained in:
Jesse Duffield 2022-05-01 15:11:00 +10:00 committed by GitHub
commit f143d04d87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 1 deletions

View File

@ -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})
}

View File

@ -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 <jessedduffield@gmail.com>'
UnixTimestamp int64
// SHAs of parent commits (will be multiple if it's a merge commit)

View File

@ -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 {

View File

@ -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",