Skip to content

Commit 368d643

Browse files
Merge pull request #2373 from phanithinks/clipboard_patch_option_2357
2 parents d8c7d47 + d085111 commit 368d643

File tree

6 files changed

+100
-0
lines changed

6 files changed

+100
-0
lines changed

pkg/gui/custom_patch_options_panel.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ func (gui *Gui) handleCreatePatchOptionsMenu() error {
6969
}
7070
}
7171

72+
menuItems = append(menuItems, []*types.MenuItem{
73+
{
74+
Label: "copy patch to clipboard",
75+
OnPress: func() error { return gui.copyPatchToClipboard() },
76+
Key: 'y',
77+
},
78+
}...)
79+
7280
return gui.c.Menu(types.CreateMenuOptions{Title: gui.c.Tr.PatchOptionsTitle, Items: menuItems})
7381
}
7482

@@ -192,3 +200,16 @@ func (gui *Gui) handleApplyPatch(reverse bool) error {
192200
}
193201
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
194202
}
203+
204+
func (gui *Gui) copyPatchToClipboard() error {
205+
patch := gui.git.Patch.PatchManager.RenderAggregatedPatchColored(true)
206+
207+
gui.c.LogAction(gui.c.Tr.Actions.CopyPatchToClipboard)
208+
if err := gui.os.CopyToClipboard(patch); err != nil {
209+
return gui.c.Error(err)
210+
}
211+
212+
gui.c.Toast(gui.c.Tr.PatchCopiedToClipboard)
213+
214+
return nil
215+
}

pkg/i18n/english.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ type TranslationSet struct {
463463
CommitURLCopiedToClipboard string
464464
CommitMessageCopiedToClipboard string
465465
CommitAuthorCopiedToClipboard string
466+
PatchCopiedToClipboard string
466467
LcCopiedToClipboard string
467468
ErrCannotEditDirectory string
468469
ErrStageDirWithInlineMergeConflicts string
@@ -566,6 +567,7 @@ type Actions struct {
566567
CopyCommitURLToClipboard string
567568
CopyCommitAuthorToClipboard string
568569
CopyCommitAttributeToClipboard string
570+
CopyPatchToClipboard string
569571
CustomCommand string
570572
DiscardAllChangesInDirectory string
571573
DiscardUnstagedChangesInDirectory string
@@ -1111,6 +1113,7 @@ func EnglishTranslationSet() TranslationSet {
11111113
CommitURLCopiedToClipboard: "Commit URL copied to clipboard",
11121114
CommitMessageCopiedToClipboard: "Commit message copied to clipboard",
11131115
CommitAuthorCopiedToClipboard: "Commit author copied to clipboard",
1116+
PatchCopiedToClipboard: "Patch copied to clipboard",
11141117
LcCopiedToClipboard: "copied to clipboard",
11151118
ErrCannotEditDirectory: "Cannot edit directory: you can only edit individual files",
11161119
ErrStageDirWithInlineMergeConflicts: "Cannot stage/unstage directory containing files with inline merge conflicts. Please fix up the merge conflicts first",
@@ -1195,6 +1198,7 @@ func EnglishTranslationSet() TranslationSet {
11951198
CopyCommitURLToClipboard: "Copy commit URL to clipboard",
11961199
CopyCommitAuthorToClipboard: "Copy commit author to clipboard",
11971200
CopyCommitAttributeToClipboard: "Copy to clipboard",
1201+
CopyPatchToClipboard: "Copy patch to clipboard",
11981202
MoveCommitUp: "Move commit up",
11991203
MoveCommitDown: "Move commit down",
12001204
CustomCommand: "Custom command",

pkg/integration/components/test_driver.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66
"time"
77

8+
"github.com/atotto/clipboard"
89
"github.com/jesseduffield/lazygit/pkg/config"
910
"github.com/jesseduffield/lazygit/pkg/gui/types"
1011
integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types"
@@ -143,6 +144,21 @@ func (self *TestDriver) ExpectPopup() *Popup {
143144
return &Popup{t: self}
144145
}
145146

147+
func (self *TestDriver) ExpectToast(matcher *matcher) {
148+
self.Views().AppStatus().Content(matcher)
149+
}
150+
151+
func (self *TestDriver) ExpectClipboard(matcher *matcher) {
152+
self.assertWithRetries(func() (bool, string) {
153+
text, err := clipboard.ReadAll()
154+
if err != nil {
155+
return false, "Error occured when reading from clipboard: " + err.Error()
156+
}
157+
ok, _ := matcher.test(text)
158+
return ok, fmt.Sprintf("Expected clipboard to match %s, but got %s", matcher.name(), text)
159+
})
160+
}
161+
146162
// for making assertions through git itself
147163
func (self *TestDriver) Git() *Git {
148164
return &Git{assertionHelper: self.assertionHelper, shell: self.shell}

pkg/integration/components/views.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ func (self *Views) Information() *ViewDriver {
6464
return self.byName("information")
6565
}
6666

67+
func (self *Views) AppStatus() *ViewDriver {
68+
return self.byName("appStatus")
69+
}
70+
6771
func (self *Views) Branches() *ViewDriver {
6872
return self.byName("localBranches")
6973
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package patch_building
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var BuildPatchAndCopyToClipboard = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Create a patch from the commits and copy the patch to clipbaord.",
10+
ExtraCmdArgs: "",
11+
Skip: true,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
shell.NewBranch("branch-a")
15+
shell.CreateFileAndAdd("file1", "first line\n")
16+
shell.Commit("first commit")
17+
18+
shell.NewBranch("branch-b")
19+
shell.UpdateFileAndAdd("file1", "first line\nsecond line\n")
20+
shell.Commit("update")
21+
22+
shell.Checkout("branch-a")
23+
},
24+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
25+
t.Views().Branches().
26+
Focus().
27+
Lines(
28+
Contains("branch-a").IsSelected(),
29+
Contains("branch-b"),
30+
).
31+
Press(keys.Universal.NextItem).
32+
PressEnter().
33+
PressEnter()
34+
t.Views().
35+
CommitFiles().
36+
Lines(
37+
Contains("M file1").IsSelected(),
38+
).
39+
PressPrimaryAction()
40+
41+
t.Views().Information().Content(Contains("building patch"))
42+
43+
t.Views().
44+
CommitFiles().
45+
Press(keys.Universal.CreatePatchOptionsMenu)
46+
47+
t.ExpectPopup().Menu().Title(Equals("Patch Options")).Select(Contains("copy patch to clipboard")).Confirm()
48+
49+
t.ExpectToast(Contains("Patch copied to clipboard"))
50+
51+
t.ExpectClipboard(Contains("diff --git a/file1 b/file1"))
52+
},
53+
})

pkg/integration/tests/tests.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/jesseduffield/lazygit/pkg/integration/tests/filter_by_path"
2222
"github.com/jesseduffield/lazygit/pkg/integration/tests/interactive_rebase"
2323
"github.com/jesseduffield/lazygit/pkg/integration/tests/misc"
24+
"github.com/jesseduffield/lazygit/pkg/integration/tests/patch_building"
2425
"github.com/jesseduffield/lazygit/pkg/integration/tests/stash"
2526
"github.com/jesseduffield/lazygit/pkg/integration/tests/sync"
2627
)
@@ -72,6 +73,7 @@ var tests = []*components.IntegrationTest{
7273
filter_by_path.CliArg,
7374
filter_by_path.SelectFile,
7475
filter_by_path.TypeFile,
76+
patch_building.BuildPatchAndCopyToClipboard,
7577
}
7678

7779
func GetTests() []*components.IntegrationTest {

0 commit comments

Comments
 (0)