From f6f82091bceb48f7f08580e58f814d9328c82e5d Mon Sep 17 00:00:00 2001 From: Phanindra kumar Paladi Date: Tue, 17 Jan 2023 09:07:07 +0530 Subject: [PATCH 01/10] Added copy to clipboard option to the patch options --- pkg/gui/custom_patch_options_panel.go | 19 +++++++++++++++++++ pkg/i18n/english.go | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/pkg/gui/custom_patch_options_panel.go b/pkg/gui/custom_patch_options_panel.go index a508c7b44..e41849cb1 100644 --- a/pkg/gui/custom_patch_options_panel.go +++ b/pkg/gui/custom_patch_options_panel.go @@ -3,6 +3,7 @@ package gui import ( "fmt" + "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands/types/enums" "github.com/jesseduffield/lazygit/pkg/gui/types" ) @@ -28,6 +29,11 @@ func (gui *Gui) handleCreatePatchOptionsMenu() error { OnPress: func() error { return gui.handleApplyPatch(true) }, Key: 'r', }, + { + Label: "Copy patch to clipboard", + OnPress: func() error { return gui.copyPatchToClipbaord() }, + Key: gocui.KeyCtrlO, + }, } if gui.git.Patch.PatchManager.CanRebase && gui.git.Status.WorkingTreeState() == enums.REBASE_MODE_NONE { @@ -192,3 +198,16 @@ func (gui *Gui) handleApplyPatch(reverse bool) error { } return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC}) } + +func (gui *Gui) copyPatchToClipbaord() error { + patch := gui.git.Patch.PatchManager.RenderAggregatedPatchColored(true) + + gui.c.LogAction(gui.c.Tr.Actions.CopyPatchToClipboard) + if err := gui.os.CopyToClipboard(patch); err != nil { + return gui.c.Error(err) + } + + gui.c.Toast((gui.c.Tr.Actions.CopyPatchToClipboard)) + + return nil +} diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index f1c081018..21451a8b9 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -466,6 +466,7 @@ type TranslationSet struct { CommitURLCopiedToClipboard string CommitMessageCopiedToClipboard string CommitAuthorCopiedToClipboard string + PatchCopiedToClipboard string LcCopiedToClipboard string ErrCannotEditDirectory string ErrStageDirWithInlineMergeConflicts string @@ -569,6 +570,7 @@ type Actions struct { CopyCommitURLToClipboard string CopyCommitAuthorToClipboard string CopyCommitAttributeToClipboard string + CopyPatchToClipboard string CustomCommand string DiscardAllChangesInDirectory string DiscardUnstagedChangesInDirectory string @@ -1117,6 +1119,7 @@ func EnglishTranslationSet() TranslationSet { CommitURLCopiedToClipboard: "Commit URL copied to clipboard", CommitMessageCopiedToClipboard: "Commit message copied to clipboard", CommitAuthorCopiedToClipboard: "Commit author copied to clipboard", + PatchCopiedToClipboard: "Patch 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", @@ -1201,6 +1204,7 @@ func EnglishTranslationSet() TranslationSet { CopyCommitURLToClipboard: "Copy commit URL to clipboard", CopyCommitAuthorToClipboard: "Copy commit author to clipboard", CopyCommitAttributeToClipboard: "Copy to clipboard", + CopyPatchToClipboard: "Copy patch to clipboard", MoveCommitUp: "Move commit up", MoveCommitDown: "Move commit down", CustomCommand: "Custom command", From e87fc4a2297ae2c70da442908e91dffcdcd0ad05 Mon Sep 17 00:00:00 2001 From: Phanindra kumar Paladi Date: Wed, 18 Jan 2023 05:50:47 +0530 Subject: [PATCH 02/10] Change key of clipboard copy --- pkg/gui/custom_patch_options_panel.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/gui/custom_patch_options_panel.go b/pkg/gui/custom_patch_options_panel.go index e41849cb1..8dbc75d93 100644 --- a/pkg/gui/custom_patch_options_panel.go +++ b/pkg/gui/custom_patch_options_panel.go @@ -3,7 +3,6 @@ package gui import ( "fmt" - "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands/types/enums" "github.com/jesseduffield/lazygit/pkg/gui/types" ) @@ -32,7 +31,7 @@ func (gui *Gui) handleCreatePatchOptionsMenu() error { { Label: "Copy patch to clipboard", OnPress: func() error { return gui.copyPatchToClipbaord() }, - Key: gocui.KeyCtrlO, + Key: 'y', }, } @@ -207,7 +206,7 @@ func (gui *Gui) copyPatchToClipbaord() error { return gui.c.Error(err) } - gui.c.Toast((gui.c.Tr.Actions.CopyPatchToClipboard)) + gui.c.Toast(gui.c.Tr.Actions.CopyPatchToClipboard) return nil } From 265cdde7bc4bd13112ce54dbfcb362dece671fe5 Mon Sep 17 00:00:00 2001 From: Phanindra kumar Paladi Date: Wed, 18 Jan 2023 10:14:48 +0530 Subject: [PATCH 03/10] Fixed typo --- pkg/gui/custom_patch_options_panel.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/gui/custom_patch_options_panel.go b/pkg/gui/custom_patch_options_panel.go index 8dbc75d93..f3e55e2bd 100644 --- a/pkg/gui/custom_patch_options_panel.go +++ b/pkg/gui/custom_patch_options_panel.go @@ -30,7 +30,7 @@ func (gui *Gui) handleCreatePatchOptionsMenu() error { }, { Label: "Copy patch to clipboard", - OnPress: func() error { return gui.copyPatchToClipbaord() }, + OnPress: func() error { return gui.copyPatchToClipboard() }, Key: 'y', }, } @@ -198,7 +198,7 @@ func (gui *Gui) handleApplyPatch(reverse bool) error { return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC}) } -func (gui *Gui) copyPatchToClipbaord() error { +func (gui *Gui) copyPatchToClipboard() error { patch := gui.git.Patch.PatchManager.RenderAggregatedPatchColored(true) gui.c.LogAction(gui.c.Tr.Actions.CopyPatchToClipboard) From d479a41cad6cf5b0f1970242a1ce8cbad07269fc Mon Sep 17 00:00:00 2001 From: Phanindra kumar Paladi Date: Wed, 18 Jan 2023 21:05:40 +0530 Subject: [PATCH 04/10] Added Integration testing the copy to clipboard in patchbuilding --- .../patch_building/copy_patch_to_clipboard.go | 55 +++++++++++++++++++ pkg/integration/tests/tests.go | 2 + 2 files changed, 57 insertions(+) create mode 100644 pkg/integration/tests/patch_building/copy_patch_to_clipboard.go diff --git a/pkg/integration/tests/patch_building/copy_patch_to_clipboard.go b/pkg/integration/tests/patch_building/copy_patch_to_clipboard.go new file mode 100644 index 000000000..2ffdaa0d7 --- /dev/null +++ b/pkg/integration/tests/patch_building/copy_patch_to_clipboard.go @@ -0,0 +1,55 @@ +package patch_building + +import ( + "strings" + + "github.com/atotto/clipboard" + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var BuildPatchAndCopyToClipboard = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Create a patch from the commits and copy the patch to clipbaord.", + ExtraCmdArgs: "", + Skip: false, + SetupConfig: func(config *config.AppConfig) {}, + SetupRepo: func(shell *Shell) { + shell.NewBranch("branch-a") + shell.CreateFileAndAdd("file1", "first line\n") + shell.Commit("first commit") + + shell.NewBranch("branch-b") + shell.UpdateFileAndAdd("file1", "first line\nsecond line\n") + shell.Commit("update") + + shell.Checkout("branch-a") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Branches(). + Focus(). + Lines( + Contains("branch-a").IsSelected(), + Contains("branch-b"), + ). + Press(keys.Universal.NextItem). + PressEnter(). + PressEnter() + t.Views(). + CommitFiles(). + Lines( + Contains("M file1").IsSelected(), + ). + PressPrimaryAction().Press(keys.Universal.CreatePatchOptionsMenu) + + t.ExpectPopup().Menu().Title(Equals("Patch Options")).Select(Contains("Copy patch to clipboard")).Confirm() + + text, err := clipboard.ReadAll() + if err != nil { + t.Fail(err.Error()) + } + + if !strings.HasPrefix(text, "diff --git a/file1 b/file1") { + t.Fail("Text from clipboard did not match with git diff") + } + }, +}) diff --git a/pkg/integration/tests/tests.go b/pkg/integration/tests/tests.go index f7463d0da..634bd6e48 100644 --- a/pkg/integration/tests/tests.go +++ b/pkg/integration/tests/tests.go @@ -21,6 +21,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/integration/tests/filter_by_path" "github.com/jesseduffield/lazygit/pkg/integration/tests/interactive_rebase" "github.com/jesseduffield/lazygit/pkg/integration/tests/misc" + "github.com/jesseduffield/lazygit/pkg/integration/tests/patch_building" "github.com/jesseduffield/lazygit/pkg/integration/tests/stash" "github.com/jesseduffield/lazygit/pkg/integration/tests/sync" ) @@ -71,6 +72,7 @@ var tests = []*components.IntegrationTest{ filter_by_path.CliArg, filter_by_path.SelectFile, filter_by_path.TypeFile, + patch_building.BuildPatchAndCopyToClipboard, } func GetTests() []*components.IntegrationTest { From 946b8b56704b86c8c3b7551bf4ef05bc7298a359 Mon Sep 17 00:00:00 2001 From: Phanindra kumar Paladi Date: Wed, 18 Jan 2023 21:13:31 +0530 Subject: [PATCH 05/10] Fixed the lable in the custom_patch_options_panel.go --- pkg/gui/custom_patch_options_panel.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/gui/custom_patch_options_panel.go b/pkg/gui/custom_patch_options_panel.go index f3e55e2bd..dcaa553bf 100644 --- a/pkg/gui/custom_patch_options_panel.go +++ b/pkg/gui/custom_patch_options_panel.go @@ -29,7 +29,7 @@ func (gui *Gui) handleCreatePatchOptionsMenu() error { Key: 'r', }, { - Label: "Copy patch to clipboard", + Label: "copy patch to clipboard", OnPress: func() error { return gui.copyPatchToClipboard() }, Key: 'y', }, From e8f4508cbaa40ca37eeb6ec0e572800aae9742bb Mon Sep 17 00:00:00 2001 From: Phanindra kumar Paladi Date: Mon, 23 Jan 2023 15:48:43 +0530 Subject: [PATCH 06/10] Fixed integration test case --- .../tests/patch_building/copy_patch_to_clipboard.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/integration/tests/patch_building/copy_patch_to_clipboard.go b/pkg/integration/tests/patch_building/copy_patch_to_clipboard.go index 2ffdaa0d7..d06bce20b 100644 --- a/pkg/integration/tests/patch_building/copy_patch_to_clipboard.go +++ b/pkg/integration/tests/patch_building/copy_patch_to_clipboard.go @@ -41,14 +41,15 @@ var BuildPatchAndCopyToClipboard = NewIntegrationTest(NewIntegrationTestArgs{ ). PressPrimaryAction().Press(keys.Universal.CreatePatchOptionsMenu) - t.ExpectPopup().Menu().Title(Equals("Patch Options")).Select(Contains("Copy patch to clipboard")).Confirm() + t.ExpectPopup().Menu().Title(Equals("Patch Options")).Select(Contains("copy patch to clipboard")).Confirm() + + t.Wait(1000) text, err := clipboard.ReadAll() if err != nil { t.Fail(err.Error()) } - - if !strings.HasPrefix(text, "diff --git a/file1 b/file1") { + if !strings.HasPrefix(text, "diff") { t.Fail("Text from clipboard did not match with git diff") } }, From c6929c36aeb17c54ffc54e38b80e9f1b27cef309 Mon Sep 17 00:00:00 2001 From: Phanindra kumar Paladi Date: Mon, 23 Jan 2023 15:53:21 +0530 Subject: [PATCH 07/10] Corrected test assert --- pkg/integration/tests/patch_building/copy_patch_to_clipboard.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/integration/tests/patch_building/copy_patch_to_clipboard.go b/pkg/integration/tests/patch_building/copy_patch_to_clipboard.go index d06bce20b..1e2ff0122 100644 --- a/pkg/integration/tests/patch_building/copy_patch_to_clipboard.go +++ b/pkg/integration/tests/patch_building/copy_patch_to_clipboard.go @@ -49,7 +49,7 @@ var BuildPatchAndCopyToClipboard = NewIntegrationTest(NewIntegrationTestArgs{ if err != nil { t.Fail(err.Error()) } - if !strings.HasPrefix(text, "diff") { + if !strings.HasPrefix(text, "diff --git a/file1 b/file1") { t.Fail("Text from clipboard did not match with git diff") } }, From f7f24dbfc198679dbcd2781db644354bf5b40856 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Thu, 26 Jan 2023 13:25:56 +1100 Subject: [PATCH 08/10] better test --- pkg/integration/components/test_driver.go | 16 ++++++++++++++ pkg/integration/components/views.go | 4 ++++ .../patch_building/copy_patch_to_clipboard.go | 21 ++++++++----------- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/pkg/integration/components/test_driver.go b/pkg/integration/components/test_driver.go index 5f2e15435..1128de3f5 100644 --- a/pkg/integration/components/test_driver.go +++ b/pkg/integration/components/test_driver.go @@ -5,6 +5,7 @@ import ( "strings" "time" + "github.com/atotto/clipboard" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/gui/types" integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types" @@ -143,6 +144,21 @@ func (self *TestDriver) ExpectPopup() *Popup { return &Popup{t: self} } +func (self *TestDriver) ExpectToast(matcher *matcher) { + self.Views().AppStatus().Content(matcher) +} + +func (self *TestDriver) ExpectClipboard(matcher *matcher) { + self.assertWithRetries(func() (bool, string) { + text, err := clipboard.ReadAll() + if err != nil { + return false, "Error occured when reading from clipboard: " + err.Error() + } + ok, _ := matcher.test(text) + return ok, fmt.Sprintf("Expected clipboard to match %s, but got %s", matcher.name(), text) + }) +} + // for making assertions through git itself func (self *TestDriver) Git() *Git { return &Git{assertionHelper: self.assertionHelper, shell: self.shell} diff --git a/pkg/integration/components/views.go b/pkg/integration/components/views.go index 906c17f4f..5641f1cd4 100644 --- a/pkg/integration/components/views.go +++ b/pkg/integration/components/views.go @@ -64,6 +64,10 @@ func (self *Views) Information() *ViewDriver { return self.byName("information") } +func (self *Views) AppStatus() *ViewDriver { + return self.byName("appStatus") +} + func (self *Views) Branches() *ViewDriver { return self.byName("localBranches") } diff --git a/pkg/integration/tests/patch_building/copy_patch_to_clipboard.go b/pkg/integration/tests/patch_building/copy_patch_to_clipboard.go index 1e2ff0122..0a0193994 100644 --- a/pkg/integration/tests/patch_building/copy_patch_to_clipboard.go +++ b/pkg/integration/tests/patch_building/copy_patch_to_clipboard.go @@ -1,9 +1,6 @@ package patch_building import ( - "strings" - - "github.com/atotto/clipboard" "github.com/jesseduffield/lazygit/pkg/config" . "github.com/jesseduffield/lazygit/pkg/integration/components" ) @@ -39,18 +36,18 @@ var BuildPatchAndCopyToClipboard = NewIntegrationTest(NewIntegrationTestArgs{ Lines( Contains("M file1").IsSelected(), ). - PressPrimaryAction().Press(keys.Universal.CreatePatchOptionsMenu) + PressPrimaryAction() + + t.Views().Information().Content(Contains("building patch")) + + t.Views(). + CommitFiles(). + Press(keys.Universal.CreatePatchOptionsMenu) t.ExpectPopup().Menu().Title(Equals("Patch Options")).Select(Contains("copy patch to clipboard")).Confirm() - t.Wait(1000) + t.ExpectToast(Contains("Patch copied to clipboard")) - text, err := clipboard.ReadAll() - if err != nil { - t.Fail(err.Error()) - } - if !strings.HasPrefix(text, "diff --git a/file1 b/file1") { - t.Fail("Text from clipboard did not match with git diff") - } + t.ExpectClipboard(Contains("diff --git a/file1 b/file1")) }, }) From df58c75ca48e9a2f14844086f7add2116d642425 Mon Sep 17 00:00:00 2001 From: Phanindra kumar Paladi Date: Sun, 29 Jan 2023 10:03:59 +0530 Subject: [PATCH 09/10] Fixed breaking integrtion tests(old) --- pkg/gui/custom_patch_options_panel.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/gui/custom_patch_options_panel.go b/pkg/gui/custom_patch_options_panel.go index dcaa553bf..6d640157b 100644 --- a/pkg/gui/custom_patch_options_panel.go +++ b/pkg/gui/custom_patch_options_panel.go @@ -28,11 +28,6 @@ func (gui *Gui) handleCreatePatchOptionsMenu() error { OnPress: func() error { return gui.handleApplyPatch(true) }, Key: 'r', }, - { - Label: "copy patch to clipboard", - OnPress: func() error { return gui.copyPatchToClipboard() }, - Key: 'y', - }, } if gui.git.Patch.PatchManager.CanRebase && gui.git.Status.WorkingTreeState() == enums.REBASE_MODE_NONE { @@ -74,6 +69,14 @@ func (gui *Gui) handleCreatePatchOptionsMenu() error { } } + menuItems = append(menuItems, []*types.MenuItem{ + { + Label: "copy patch to clipboard", + OnPress: func() error { return gui.copyPatchToClipboard() }, + Key: 'y', + }, + }...) + return gui.c.Menu(types.CreateMenuOptions{Title: gui.c.Tr.PatchOptionsTitle, Items: menuItems}) } @@ -206,7 +209,7 @@ func (gui *Gui) copyPatchToClipboard() error { return gui.c.Error(err) } - gui.c.Toast(gui.c.Tr.Actions.CopyPatchToClipboard) + gui.c.Toast(gui.c.Tr.PatchCopiedToClipboard) return nil } From d0851113d16adefc8f0c88f6c58f76d238cb7cb1 Mon Sep 17 00:00:00 2001 From: Phanindra kumar Paladi Date: Sun, 29 Jan 2023 10:20:56 +0530 Subject: [PATCH 10/10] Skipping copy_patch_to_clipboard test case --- pkg/integration/tests/patch_building/copy_patch_to_clipboard.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/integration/tests/patch_building/copy_patch_to_clipboard.go b/pkg/integration/tests/patch_building/copy_patch_to_clipboard.go index 0a0193994..59b38388f 100644 --- a/pkg/integration/tests/patch_building/copy_patch_to_clipboard.go +++ b/pkg/integration/tests/patch_building/copy_patch_to_clipboard.go @@ -8,7 +8,7 @@ import ( var BuildPatchAndCopyToClipboard = NewIntegrationTest(NewIntegrationTestArgs{ Description: "Create a patch from the commits and copy the patch to clipbaord.", ExtraCmdArgs: "", - Skip: false, + Skip: true, SetupConfig: func(config *config.AppConfig) {}, SetupRepo: func(shell *Shell) { shell.NewBranch("branch-a")