Skip to content

Commit

Permalink
Add copy commit attributes option
Browse files Browse the repository at this point in the history
  • Loading branch information
mark2185 authored and jesseduffield committed Mar 26, 2022
1 parent bbaa651 commit 540edb0
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 53 deletions.
2 changes: 1 addition & 1 deletion docs/keybindings/Keybindings_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
<kbd>g</kbd>: reset to this commit
<kbd>space</kbd>: checkout commit
<kbd>T</kbd>: tag commit
<kbd>ctrl+y</kbd>: copy commit message to clipboard
<kbd>y</kbd>: copy commit attribute
<kbd>o</kbd>: open commit in browser
<kbd>enter</kbd>: view selected item's files
</pre>
Expand Down
2 changes: 1 addition & 1 deletion docs/keybindings/Keybindings_nl.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
<kbd>g</kbd>: reset naar deze commit
<kbd>space</kbd>: checkout commit
<kbd>T</kbd>: tag commit
<kbd>ctrl+y</kbd>: kopieer commit bericht naar klembord
<kbd>y</kbd>: copy commit attribute
<kbd>o</kbd>: open commit in browser
<kbd>enter</kbd>: bekijk gecommite bestanden
</pre>
Expand Down
2 changes: 1 addition & 1 deletion docs/keybindings/Keybindings_pl.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
<kbd>g</kbd>: zresetuj do tego commita
<kbd>space</kbd>: checkout commit
<kbd>T</kbd>: tag commit
<kbd>ctrl+y</kbd>: copy commit message to clipboard
<kbd>y</kbd>: copy commit attribute
<kbd>o</kbd>: open commit in browser
<kbd>enter</kbd>: przeglądaj pliki commita
</pre>
Expand Down
4 changes: 2 additions & 2 deletions docs/keybindings/Keybindings_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ _This file is auto-generated. To update, make the changes in the pkg/i18n direct
<kbd>g</kbd>: 重置为此提交
<kbd>space</kbd>: 检出提交
<kbd>T</kbd>: 标签提交
<kbd>ctrl+y</kbd>: 将提交消息复制到剪贴板
<kbd>o</kbd>: 在浏览器中打开提交
<kbd>y</kbd>: copy commit attribute
<kbd>o</kbd>: open commit in browser
<kbd>enter</kbd>: 查看提交的文件
</pre>

Expand Down
6 changes: 6 additions & 0 deletions pkg/commands/git_commands/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ func (self *CommitCommands) GetCommitMessage(commitSha string) (string, error) {
return strings.TrimSpace(message), err
}

func (self *CommitCommands) GetCommitDiff(commitSha string) (string, error) {
cmdStr := "git show --no-color " + commitSha
diff, err := self.cmd.New(cmdStr).DontLog().RunWithOutput()
return diff, err
}

func (self *CommitCommands) GetCommitMessageFirstLine(sha string) (string, error) {
return self.GetCommitMessagesFirstLine([]string{sha})
}
Expand Down
88 changes: 44 additions & 44 deletions pkg/config/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,28 +229,28 @@ type KeybindingBranchesConfig struct {
}

type KeybindingCommitsConfig struct {
SquashDown string `yaml:"squashDown"`
RenameCommit string `yaml:"renameCommit"`
RenameCommitWithEditor string `yaml:"renameCommitWithEditor"`
ViewResetOptions string `yaml:"viewResetOptions"`
MarkCommitAsFixup string `yaml:"markCommitAsFixup"`
CreateFixupCommit string `yaml:"createFixupCommit"`
SquashAboveCommits string `yaml:"squashAboveCommits"`
MoveDownCommit string `yaml:"moveDownCommit"`
MoveUpCommit string `yaml:"moveUpCommit"`
AmendToCommit string `yaml:"amendToCommit"`
PickCommit string `yaml:"pickCommit"`
RevertCommit string `yaml:"revertCommit"`
CherryPickCopy string `yaml:"cherryPickCopy"`
CherryPickCopyRange string `yaml:"cherryPickCopyRange"`
PasteCommits string `yaml:"pasteCommits"`
TagCommit string `yaml:"tagCommit"`
CheckoutCommit string `yaml:"checkoutCommit"`
ResetCherryPick string `yaml:"resetCherryPick"`
CopyCommitMessageToClipboard string `yaml:"copyCommitMessageToClipboard"`
OpenLogMenu string `yaml:"openLogMenu"`
OpenInBrowser string `yaml:"openInBrowser"`
ViewBisectOptions string `yaml:"viewBisectOptions"`
SquashDown string `yaml:"squashDown"`
RenameCommit string `yaml:"renameCommit"`
RenameCommitWithEditor string `yaml:"renameCommitWithEditor"`
ViewResetOptions string `yaml:"viewResetOptions"`
MarkCommitAsFixup string `yaml:"markCommitAsFixup"`
CreateFixupCommit string `yaml:"createFixupCommit"`
SquashAboveCommits string `yaml:"squashAboveCommits"`
MoveDownCommit string `yaml:"moveDownCommit"`
MoveUpCommit string `yaml:"moveUpCommit"`
AmendToCommit string `yaml:"amendToCommit"`
PickCommit string `yaml:"pickCommit"`
RevertCommit string `yaml:"revertCommit"`
CherryPickCopy string `yaml:"cherryPickCopy"`
CherryPickCopyRange string `yaml:"cherryPickCopyRange"`
PasteCommits string `yaml:"pasteCommits"`
TagCommit string `yaml:"tagCommit"`
CheckoutCommit string `yaml:"checkoutCommit"`
ResetCherryPick string `yaml:"resetCherryPick"`
CopyCommitAttributeToClipboard string `yaml:"copyCommitAttributeToClipboard"`
OpenLogMenu string `yaml:"openLogMenu"`
OpenInBrowser string `yaml:"openInBrowser"`
ViewBisectOptions string `yaml:"viewBisectOptions"`
}

type KeybindingStashConfig struct {
Expand Down Expand Up @@ -496,28 +496,28 @@ func GetDefaultConfig() *UserConfig {
FetchRemote: "f",
},
Commits: KeybindingCommitsConfig{
SquashDown: "s",
RenameCommit: "r",
RenameCommitWithEditor: "R",
ViewResetOptions: "g",
MarkCommitAsFixup: "f",
CreateFixupCommit: "F",
SquashAboveCommits: "S",
MoveDownCommit: "<c-j>",
MoveUpCommit: "<c-k>",
AmendToCommit: "A",
PickCommit: "p",
RevertCommit: "t",
CherryPickCopy: "c",
CherryPickCopyRange: "C",
PasteCommits: "v",
TagCommit: "T",
CheckoutCommit: "<space>",
ResetCherryPick: "<c-R>",
CopyCommitMessageToClipboard: "<c-y>",
OpenLogMenu: "<c-l>",
OpenInBrowser: "o",
ViewBisectOptions: "b",
SquashDown: "s",
RenameCommit: "r",
RenameCommitWithEditor: "R",
ViewResetOptions: "g",
MarkCommitAsFixup: "f",
CreateFixupCommit: "F",
SquashAboveCommits: "S",
MoveDownCommit: "<c-j>",
MoveUpCommit: "<c-k>",
AmendToCommit: "A",
PickCommit: "p",
RevertCommit: "t",
CherryPickCopy: "c",
CherryPickCopyRange: "C",
PasteCommits: "v",
TagCommit: "T",
CheckoutCommit: "<space>",
ResetCherryPick: "<c-R>",
CopyCommitAttributeToClipboard: "y",
OpenLogMenu: "<c-l>",
OpenInBrowser: "o",
ViewBisectOptions: "b",
},
Stash: KeybindingStashConfig{
PopStash: "g",
Expand Down
80 changes: 76 additions & 4 deletions pkg/gui/controllers/local_commits_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,10 @@ func (self *LocalCommitsController) GetKeybindings(opts types.KeybindingsOpts) [
Description: self.c.Tr.LcTagCommit,
},
{
Key: opts.GetKey(opts.Config.Commits.CopyCommitMessageToClipboard),
Handler: self.checkSelected(self.copyCommitMessageToClipboard),
Description: self.c.Tr.LcCopyCommitMessageToClipboard,
Key: opts.GetKey(opts.Config.Commits.CopyCommitAttributeToClipboard),
Handler: self.checkSelected(self.copyCommitAttribute),
Description: self.c.Tr.LcCopyCommitAttributeToClipboard,
OpensMenu: true,
},
{
Key: opts.GetKey(opts.Config.Commits.OpenInBrowser),
Expand Down Expand Up @@ -599,6 +600,78 @@ func (self *LocalCommitsController) gotoBottom() error {
return nil
}

func (self *LocalCommitsController) copyCommitAttribute(commit *models.Commit) error {
return self.c.Menu(types.CreateMenuOptions{
Title: self.c.Tr.Actions.CopyCommitAttributeToClipboard,
Items: []*types.MenuItem{
{
DisplayString: self.c.Tr.LcCommitSha,
OnPress: func() error {
return self.copyCommitSHAToClipboard(commit)
},
},
{
DisplayString: self.c.Tr.LcCommitURL,
OnPress: func() error {
return self.copyCommitURLToClipboard(commit)
},
},
{
DisplayString: self.c.Tr.LcCommitDiff,
OnPress: func() error {
return self.copyCommitDiffToClipboard(commit)
},
},
{
DisplayString: self.c.Tr.LcCommitMessage,
OnPress: func() error {
return self.copyCommitMessageToClipboard(commit)
},
},
},
})
}

func (self *LocalCommitsController) copyCommitSHAToClipboard(commit *models.Commit) error {
self.c.LogAction(self.c.Tr.Actions.CopyCommitSHAToClipboard)
if err := self.os.CopyToClipboard(commit.Sha); err != nil {
return self.c.Error(err)
}

self.c.Toast(self.c.Tr.CommitSHACopiedToClipboard)
return nil
}

func (self *LocalCommitsController) copyCommitURLToClipboard(commit *models.Commit) error {
url, err := self.helpers.Host.GetCommitURL(commit.Sha)
if err != nil {
return err
}

self.c.LogAction(self.c.Tr.Actions.CopyCommitURLToClipboard)
if err := self.os.CopyToClipboard(url); err != nil {
return self.c.Error(err)
}

self.c.Toast(self.c.Tr.CommitURLCopiedToClipboard)
return nil
}

func (self *LocalCommitsController) copyCommitDiffToClipboard(commit *models.Commit) error {
diff, err := self.git.Commit.GetCommitDiff(commit.Sha)
if err != nil {
return self.c.Error(err)
}

self.c.LogAction(self.c.Tr.Actions.CopyCommitDiffToClipboard)
if err := self.os.CopyToClipboard(diff); err != nil {
return self.c.Error(err)
}

self.c.Toast(self.c.Tr.CommitDiffCopiedToClipboard)
return nil
}

func (self *LocalCommitsController) copyCommitMessageToClipboard(commit *models.Commit) error {
message, err := self.git.Commit.GetCommitMessage(commit.Sha)
if err != nil {
Expand All @@ -611,7 +684,6 @@ func (self *LocalCommitsController) copyCommitMessageToClipboard(commit *models.
}

self.c.Toast(self.c.Tr.CommitMessageCopiedToClipboard)

return nil
}

Expand Down
24 changes: 24 additions & 0 deletions pkg/i18n/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,13 @@ type TranslationSet struct {
LcOpenDiffingMenu string
LcOpenExtrasMenu string
LcShowingGitDiff string
LcCommitDiff string
LcCopyCommitShaToClipboard string
LcCommitSha string
LcCommitURL string
LcCopyCommitMessageToClipboard string
LcCommitMessage string
LcCopyCommitAttributeToClipboard string
LcCopyBranchNameToClipboard string
LcCopyFileNameToClipboard string
LcCopyCommitFileNameToClipboard string
Expand Down Expand Up @@ -415,6 +420,9 @@ type TranslationSet struct {
ExtrasTitle string
PushingTagStatus string
PullRequestURLCopiedToClipboard string
CommitDiffCopiedToClipboard string
CommitSHACopiedToClipboard string
CommitURLCopiedToClipboard string
CommitMessageCopiedToClipboard string
LcCopiedToClipboard string
ErrCannotEditDirectory string
Expand Down Expand Up @@ -505,6 +513,10 @@ type Actions struct {
MoveCommitUp string
MoveCommitDown string
CopyCommitMessageToClipboard string
CopyCommitDiffToClipboard string
CopyCommitSHAToClipboard string
CopyCommitURLToClipboard string
CopyCommitAttributeToClipboard string
CustomCommand string
DiscardAllChangesInDirectory string
DiscardUnstagedChangesInDirectory string
Expand Down Expand Up @@ -941,8 +953,13 @@ func EnglishTranslationSet() TranslationSet {
// the actual view is the extras view which I intend to give more tabs in future but for now we'll only mention the command log part
LcOpenExtrasMenu: "open command log menu",
LcShowingGitDiff: "showing output for:",
LcCommitDiff: "commit diff",
LcCopyCommitShaToClipboard: "copy commit SHA to clipboard",
LcCommitSha: "commit SHA",
LcCommitURL: "commit URL",
LcCopyCommitMessageToClipboard: "copy commit message to clipboard",
LcCommitMessage: "commit message",
LcCopyCommitAttributeToClipboard: "copy commit attribute",
LcCopyBranchNameToClipboard: "copy branch name to clipboard",
LcCopyFileNameToClipboard: "copy the file name to the clipboard",
LcCopyCommitFileNameToClipboard: "copy the committed file name to the clipboard",
Expand Down Expand Up @@ -993,6 +1010,9 @@ func EnglishTranslationSet() TranslationSet {
ExtrasTitle: "Extras",
PushingTagStatus: "pushing tag",
PullRequestURLCopiedToClipboard: "Pull request URL copied to clipboard",
CommitDiffCopiedToClipboard: "Commit diff copied to clipboard",
CommitSHACopiedToClipboard: "Commit SHA copied to clipboard",
CommitURLCopiedToClipboard: "Commit URL copied to clipboard",
CommitMessageCopiedToClipboard: "Commit message copied to clipboard",
LcCopiedToClipboard: "copied to clipboard",
ErrCannotEditDirectory: "Cannot edit directory: you can only edit individual files",
Expand Down Expand Up @@ -1064,6 +1084,10 @@ func EnglishTranslationSet() TranslationSet {
CreateLightweightTag: "Create lightweight tag",
CreateAnnotatedTag: "Create annotated tag",
CopyCommitMessageToClipboard: "Copy commit message to clipboard",
CopyCommitDiffToClipboard: "Copy commit diff to clipboard",
CopyCommitSHAToClipboard: "Copy commit SHA to clipboard",
CopyCommitURLToClipboard: "Copy commit URL to clipboard",
CopyCommitAttributeToClipboard: "Copy to clipboard",
MoveCommitUp: "Move commit up",
MoveCommitDown: "Move commit down",
CustomCommand: "Custom command",
Expand Down

0 comments on commit 540edb0

Please sign in to comment.