Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a "Copy to clipboard" menu to the Commit Files panel #4271

Merged
merged 2 commits into from
Feb 19, 2025
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
1 change: 1 addition & 0 deletions docs/keybindings/Keybindings_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ _Legend: `<c-b>` means ctrl+b, `<a-b>` means alt+b, `B` means shift+b_
| Key | Action | Info |
|-----|--------|-------------|
| `` <c-o> `` | Copy path to clipboard | |
| `` y `` | Copy to clipboard | |
| `` c `` | Checkout | Checkout file. This replaces the file in your working tree with the version from the selected commit. |
| `` d `` | Remove | Discard this commit's changes to this file. This runs an interactive rebase in the background, so you may get a merge conflict if a later commit also changes this file. |
| `` o `` | Open file | Open file in default application. |
Expand Down
1 change: 1 addition & 0 deletions docs/keybindings/Keybindings_ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ If you would instead like to start an interactive rebase from the selected commi
| Key | Action | Info |
|-----|--------|-------------|
| `` <c-o> `` | ファイル名をクリップボードにコピー | |
| `` y `` | Copy to clipboard | |
| `` c `` | チェックアウト | Checkout file. This replaces the file in your working tree with the version from the selected commit. |
| `` d `` | Remove | Discard this commit's changes to this file. This runs an interactive rebase in the background, so you may get a merge conflict if a later commit also changes this file. |
| `` o `` | ファイルを開く | Open file in default application. |
Expand Down
1 change: 1 addition & 0 deletions docs/keybindings/Keybindings_ko.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ If you would instead like to start an interactive rebase from the selected commi
| Key | Action | Info |
|-----|--------|-------------|
| `` <c-o> `` | 파일명을 클립보드에 복사 | |
| `` y `` | 클립보드에 복사 | |
| `` c `` | 체크아웃 | Checkout file |
| `` d `` | Remove | Discard this commit's changes to this file |
| `` o `` | 파일 닫기 | Open file in default application. |
Expand Down
1 change: 1 addition & 0 deletions docs/keybindings/Keybindings_nl.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ _Legend: `<c-b>` means ctrl+b, `<a-b>` means alt+b, `B` means shift+b_
| Key | Action | Info |
|-----|--------|-------------|
| `` <c-o> `` | Kopieer de bestandsnaam naar het klembord | |
| `` y `` | Copy to clipboard | |
| `` c `` | Uitchecken | Bestand uitchecken |
| `` d `` | Remove | Uitsluit deze commit zijn veranderingen aan dit bestand |
| `` o `` | Open bestand | Open file in default application. |
Expand Down
1 change: 1 addition & 0 deletions docs/keybindings/Keybindings_pl.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ Jeśli chcesz zamiast tego rozpocząć interaktywny rebase od wybranego commita,
| Key | Action | Info |
|-----|--------|-------------|
| `` <c-o> `` | Kopiuj ścieżkę do schowka | |
| `` y `` | Kopiuj do schowka | |
| `` c `` | Przełącz | Przełącz plik. Zastępuje plik w twoim drzewie roboczym wersją z wybranego commita. |
| `` d `` | Usuń | Odrzuć zmiany w tym pliku z tego commita. Uruchamia interaktywny rebase w tle, więc możesz otrzymać konflikt scalania, jeśli późniejszy commit również zmienia ten plik. |
| `` o `` | Otwórz plik | Otwórz plik w domyślnej aplikacji. |
Expand Down
1 change: 1 addition & 0 deletions docs/keybindings/Keybindings_pt.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ Veja a documentação:
| Key | Action | Info |
|-----|--------|-------------|
| `` <c-o> `` | Copy path to clipboard | |
| `` y `` | Copy to clipboard | |
| `` c `` | Verificar | Checkout file. This replaces the file in your working tree with the version from the selected commit. |
| `` d `` | Remove | Discard this commit's changes to this file. This runs an interactive rebase in the background, so you may get a merge conflict if a later commit also changes this file. |
| `` o `` | Abrir arquivo | Abrir arquivo no aplicativo padrão. |
Expand Down
1 change: 1 addition & 0 deletions docs/keybindings/Keybindings_ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ If you would instead like to start an interactive rebase from the selected commi
| Key | Action | Info |
|-----|--------|-------------|
| `` <c-o> `` | Скопировать название файла в буфер обмена | |
| `` y `` | Copy to clipboard | |
| `` c `` | Переключить | Переключить файл |
| `` d `` | Remove | Отменить изменения коммита в этом файле |
| `` o `` | Открыть файл | Open file in default application. |
Expand Down
1 change: 1 addition & 0 deletions docs/keybindings/Keybindings_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ _图例:`<c-b>` 意味着ctrl+b, `<a-b>意味着Alt+b, `B` 意味着shift+b_
| Key | Action | Info |
|-----|--------|-------------|
| `` <c-o> `` | 将文件名复制到剪贴板 | |
| `` y `` | 复制到剪贴板 | |
| `` c `` | 检出 | 检出文件 |
| `` d `` | 删除 | 放弃对此文件的提交变更 |
| `` o `` | 打开文件 | 使用默认程序打开该文件 |
Expand Down
1 change: 1 addition & 0 deletions docs/keybindings/Keybindings_zh-TW.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ If you would instead like to start an interactive rebase from the selected commi
| Key | Action | Info |
|-----|--------|-------------|
| `` <c-o> `` | 複製檔案名稱到剪貼簿 | |
| `` y `` | 複製到剪貼簿 | |
| `` c `` | 檢出 | 檢出檔案 |
| `` d `` | Remove | Discard this commit's changes to this file. This runs an interactive rebase in the background, so you may get a merge conflict if a later commit also changes this file. |
| `` o `` | 開啟檔案 | 使用預設軟體開啟 |
Expand Down
77 changes: 77 additions & 0 deletions pkg/gui/controllers/commits_files_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ func NewCommitFilesController(

func (self *CommitFilesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{
{
Key: opts.GetKey(opts.Config.Files.CopyFileInfoToClipboard),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm using a key from the Files section here, which could be considered wrong. Actually I think it's ok though, I prefer this both to adding a new one to the CommitFiles section, and to moving it into Universal. It's also not the first time we do this, we are already using a few others from the Files section here.

Handler: self.openCopyMenu,
Description: self.c.Tr.CopyToClipboardMenu,
OpensMenu: true,
},
{
Key: opts.GetKey(opts.Config.CommitFiles.CheckoutCommitFile),
Handler: self.withItem(self.checkout),
Expand Down Expand Up @@ -181,6 +187,77 @@ func (self *CommitFilesController) onClickMain(opts gocui.ViewMouseBindingOpts)
return self.enterCommitFile(node, types.OnFocusOpts{ClickedWindowName: "main", ClickedViewLineIdx: opts.Y})
}

func (self *CommitFilesController) copyDiffToClipboard(path string, toastMessage string) error {
from, to := self.context().GetFromAndToForDiff()
from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(from)

cmdObj := self.c.Git().WorkingTree.ShowFileDiffCmdObj(from, to, reverse, path, true)
diff, err := cmdObj.RunWithOutput()
if err != nil {
return err
}
if err := self.c.OS().CopyToClipboard(diff); err != nil {
return err
}
self.c.Toast(toastMessage)
return nil
}

func (self *CommitFilesController) openCopyMenu() error {
node := self.context().GetSelected()

copyNameItem := &types.MenuItem{
Label: self.c.Tr.CopyFileName,
OnPress: func() error {
if err := self.c.OS().CopyToClipboard(node.Name()); err != nil {
return err
}
self.c.Toast(self.c.Tr.FileNameCopiedToast)
return nil
},
DisabledReason: self.require(self.singleItemSelected())(),
Key: 'n',
}
copyPathItem := &types.MenuItem{
Label: self.c.Tr.CopyFilePath,
OnPress: func() error {
if err := self.c.OS().CopyToClipboard(node.Path); err != nil {
return err
}
self.c.Toast(self.c.Tr.FilePathCopiedToast)
return nil
},
DisabledReason: self.require(self.singleItemSelected())(),
Key: 'p',
}
copyFileDiffItem := &types.MenuItem{
Label: self.c.Tr.CopySelectedDiff,
OnPress: func() error {
return self.copyDiffToClipboard(node.GetPath(), self.c.Tr.FileDiffCopiedToast)
},
DisabledReason: self.require(self.singleItemSelected())(),
Key: 's',
}
copyAllDiff := &types.MenuItem{
Label: self.c.Tr.CopyAllFilesDiff,
OnPress: func() error {
return self.copyDiffToClipboard(".", self.c.Tr.AllFilesDiffCopiedToast)
},
DisabledReason: self.require(self.itemsSelected())(),
Key: 'a',
}

return self.c.Menu(types.CreateMenuOptions{
Title: self.c.Tr.CopyToClipboardMenu,
Items: []*types.MenuItem{
copyNameItem,
copyPathItem,
copyFileDiffItem,
copyAllDiff,
},
})
}

func (self *CommitFilesController) checkout(node *filetree.CommitFileNode) error {
self.c.LogAction(self.c.Tr.Actions.CheckoutFile)
if err := self.c.Git().WorkingTree.CheckoutFile(self.context().GetRef().RefName(), node.GetPath()); err != nil {
Expand Down
12 changes: 2 additions & 10 deletions pkg/integration/tests/commit/copy_author_to_clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ var CopyAuthorToClipboard = NewIntegrationTest(NewIntegrationTestArgs{
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {
// Include delimiters around the text so that we can assert on the entire content
config.GetUserConfig().OS.CopyToClipboardCmd = "echo /{{text}}/ > clipboard"
config.GetUserConfig().OS.CopyToClipboardCmd = "printf '%s' {{text}} > clipboard"
},

SetupRepo: func(shell *Shell) {
Expand All @@ -36,13 +35,6 @@ var CopyAuthorToClipboard = NewIntegrationTest(NewIntegrationTestArgs{

t.ExpectToast(Equals("Commit author copied to clipboard"))

t.Views().Files().
Focus().
Press(keys.Files.RefreshFiles).
Lines(
Contains("clipboard").IsSelected(),
)

t.Views().Main().Content(Contains("/John Doe <john@doe.com>/"))
t.FileSystem().FileContent("clipboard", Equals("John Doe <john@doe.com>"))
},
})
13 changes: 2 additions & 11 deletions pkg/integration/tests/commit/copy_tag_to_clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ var CopyTagToClipboard = NewIntegrationTest(NewIntegrationTestArgs{
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {
// Include delimiters around the text so that we can assert on the entire content
config.GetUserConfig().OS.CopyToClipboardCmd = "echo _{{text}}_ > clipboard"
config.GetUserConfig().OS.CopyToClipboardCmd = "printf '%s' {{text}} > clipboard"
},

SetupRepo: func(shell *Shell) {
Expand All @@ -38,14 +37,6 @@ var CopyTagToClipboard = NewIntegrationTest(NewIntegrationTestArgs{

t.ExpectToast(Equals("Commit tags copied to clipboard"))

t.Views().Files().
Focus().
Press(keys.Files.RefreshFiles).
Lines(
Contains("clipboard").IsSelected(),
)

t.Views().Main().Content(Contains("+_tag2"))
t.Views().Main().Content(Contains("+tag1_"))
t.FileSystem().FileContent("clipboard", Equals("tag2\ntag1"))
},
})
2 changes: 1 addition & 1 deletion pkg/integration/tests/commit/paste_commit_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var PasteCommitMessage = NewIntegrationTest(NewIntegrationTestArgs{
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {
config.GetUserConfig().OS.CopyToClipboardCmd = "echo {{text}} > ../clipboard"
config.GetUserConfig().OS.CopyToClipboardCmd = "printf '%s' {{text}} > ../clipboard"
config.GetUserConfig().OS.ReadFromClipboardCmd = "cat ../clipboard"
},
SetupRepo: func(shell *Shell) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var PasteCommitMessageOverExisting = NewIntegrationTest(NewIntegrationTestArgs{
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {
config.GetUserConfig().OS.CopyToClipboardCmd = "echo {{text}} > ../clipboard"
config.GetUserConfig().OS.CopyToClipboardCmd = "printf '%s' {{text}} > ../clipboard"
config.GetUserConfig().OS.ReadFromClipboardCmd = "cat ../clipboard"
},
SetupRepo: func(shell *Shell) {
Expand Down
123 changes: 123 additions & 0 deletions pkg/integration/tests/diff/copy_to_clipboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package diff

import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)

// note: this is required to simulate the clipboard during CI
func expectClipboard(t *TestDriver, matcher *TextMatcher) {
defer t.Shell().DeleteFile("clipboard")

t.FileSystem().FileContent("clipboard", matcher)
}

var CopyToClipboard = NewIntegrationTest(NewIntegrationTestArgs{
Description: "The copy menu allows to copy name and diff of selected/all files",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {
config.GetUserConfig().OS.CopyToClipboardCmd = "printf '%s' {{text}} > clipboard"
},
SetupRepo: func(shell *Shell) {
shell.CreateDir("dir")
shell.CreateFileAndAdd("dir/file1", "1st line\n")
shell.Commit("1")
shell.CreateFileAndAdd("dir/file1", "1st line\n2nd line\n")
shell.CreateFileAndAdd("dir/file2", "file2\n")
shell.Commit("2")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Commits().
Focus().
Lines(
Contains("2").IsSelected(),
Contains("1"),
).
PressEnter()

t.Views().CommitFiles().
IsFocused().
Lines(
Contains("dir").IsSelected(),
Contains("file1"),
Contains("file2"),
).
NavigateToLine(Contains("file1")).
Press(keys.Files.CopyFileInfoToClipboard).
Tap(func() {
t.ExpectPopup().Menu().
Title(Equals("Copy to clipboard")).
Select(Contains("File name")).
Confirm().
Tap(func() {
t.ExpectToast(Equals("File name copied to clipboard"))
expectClipboard(t, Equals("file1"))
})
}).
Press(keys.Files.CopyFileInfoToClipboard).
Tap(func() {
t.ExpectPopup().Menu().
Title(Equals("Copy to clipboard")).
Select(Contains("Path")).
Confirm().
Tap(func() {
t.ExpectToast(Equals("File path copied to clipboard"))
expectClipboard(t, Equals("dir/file1"))
})
}).
Press(keys.Files.CopyFileInfoToClipboard).
Tap(func() {
t.ExpectPopup().Menu().
Title(Equals("Copy to clipboard")).
Select(Contains("Diff of selected file")).
Confirm().
Tap(func() {
t.ExpectToast(Equals("File diff copied to clipboard"))
expectClipboard(t,
Contains("diff --git a/dir/file1 b/dir/file1").Contains("+2nd line").DoesNotContain("+1st line").
DoesNotContain("diff --git a/dir/file2 b/dir/file2").DoesNotContain("+file2"))
})
}).
Press(keys.Files.CopyFileInfoToClipboard).
Tap(func() {
t.ExpectPopup().Menu().
Title(Equals("Copy to clipboard")).
Select(Contains("Diff of all files")).
Confirm().
Tap(func() {
t.ExpectToast(Equals("All files diff copied to clipboard"))
expectClipboard(t,
Contains("diff --git a/dir/file1 b/dir/file1").Contains("+2nd line").DoesNotContain("+1st line").
Contains("diff --git a/dir/file2 b/dir/file2").Contains("+file2"))
})
})

t.Views().Commits().
Focus().
// Select both commits
Press(keys.Universal.RangeSelectDown).
PressEnter()

t.Views().CommitFiles().
IsFocused().
Lines(
Contains("dir").IsSelected(),
Contains("file1"),
Contains("file2"),
).
NavigateToLine(Contains("file1")).
Press(keys.Files.CopyFileInfoToClipboard).
Tap(func() {
t.ExpectPopup().Menu().
Title(Equals("Copy to clipboard")).
Select(Contains("Diff of selected file")).
Confirm().
Tap(func() {
t.ExpectToast(Equals("File diff copied to clipboard"))
expectClipboard(t,
Contains("diff --git a/dir/file1 b/dir/file1").Contains("+1st line").Contains("+2nd line"))
})
})
},
})
6 changes: 3 additions & 3 deletions pkg/integration/tests/file/copy_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var CopyMenu = NewIntegrationTest(NewIntegrationTestArgs{
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {
config.GetUserConfig().OS.CopyToClipboardCmd = "echo {{text}} > clipboard"
config.GetUserConfig().OS.CopyToClipboardCmd = "printf '%s' {{text}} > clipboard"
},
SetupRepo: func(shell *Shell) {},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
Expand Down Expand Up @@ -100,7 +100,7 @@ var CopyMenu = NewIntegrationTest(NewIntegrationTestArgs{

t.ExpectToast(Equals("File name copied to clipboard"))

expectClipboard(t, Contains("unstaged_file"))
expectClipboard(t, Equals("1-unstaged_file"))
})

// Copy file path
Expand All @@ -114,7 +114,7 @@ var CopyMenu = NewIntegrationTest(NewIntegrationTestArgs{

t.ExpectToast(Equals("File path copied to clipboard"))

expectClipboard(t, Contains("dir/1-unstaged_file"))
expectClipboard(t, Equals("dir/1-unstaged_file"))
})

// Selected path diff on a single (unstaged) file
Expand Down
Loading