From 1f801b91e48e5af9a0707f3a2286d9e249785048 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 12 Mar 2023 16:52:02 +0100 Subject: [PATCH 1/2] Disallow discarding file changes while a directory is selected Discarding changes to an entire directory doesn't quite work correctly in all cases; for example, if the current commit added files to the directory (but the directory existed before) then those files won't be removed. It might be possible to fix the command so that these cases always work for directories, but I don't think it's worth the effort (you can always use a custom patch for that), so let's display an error for now. --- pkg/gui/controllers/commits_files_controller.go | 4 ++++ pkg/i18n/english.go | 2 ++ 2 files changed, 6 insertions(+) diff --git a/pkg/gui/controllers/commits_files_controller.go b/pkg/gui/controllers/commits_files_controller.go index 0cea2bd46..3e8849d88 100644 --- a/pkg/gui/controllers/commits_files_controller.go +++ b/pkg/gui/controllers/commits_files_controller.go @@ -153,6 +153,10 @@ func (self *CommitFilesController) checkout(node *filetree.CommitFileNode) error } func (self *CommitFilesController) discard(node *filetree.CommitFileNode) error { + if node.File == nil { + return self.c.ErrorMsg(self.c.Tr.DiscardNotSupportedForDirectory) + } + if ok, err := self.c.Helpers().PatchBuilding.ValidateNormalWorkingTreeState(); !ok { return err } diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 22a00b91c..4025c6e9d 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -268,6 +268,7 @@ type TranslationSet struct { DiscardOldFileChange string DiscardFileChangesTitle string DiscardFileChangesPrompt string + DiscardNotSupportedForDirectory string DisabledForGPG string CreateRepo string BareRepo string @@ -955,6 +956,7 @@ func EnglishTranslationSet() TranslationSet { DiscardOldFileChange: "Discard this commit's changes to this file", DiscardFileChangesTitle: "Discard file changes", DiscardFileChangesPrompt: "Are you sure you want to discard this commit's changes to this file? If this file was created in this commit, it will be deleted", + DiscardNotSupportedForDirectory: "Discarding changes is not supported for entire directories. Please use a custom patch for this.", DisabledForGPG: "Feature not available for users using GPG", CreateRepo: "Not in a git repository. Create a new git repository? (y/n): ", BareRepo: "You've attempted to open Lazygit in a bare repo but Lazygit does not yet support bare repos. Open most recent repo? (y/n) ", From 5c55ce6555bd427bdb7ae21d482494a13d08c547 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 12 Mar 2023 16:45:46 +0100 Subject: [PATCH 2/2] Better prompt for discarding old file changes Lazygit knows what kind of file change this is, so there doesn't have to be any "if" in the prompt text. --- pkg/commands/models/commit_file.go | 8 ++++++++ pkg/gui/controllers/commits_files_controller.go | 9 ++++++++- pkg/i18n/english.go | 6 +++++- pkg/integration/tests/commit/discard_old_file_change.go | 2 +- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/pkg/commands/models/commit_file.go b/pkg/commands/models/commit_file.go index 5f68e45b3..45b56d2dd 100644 --- a/pkg/commands/models/commit_file.go +++ b/pkg/commands/models/commit_file.go @@ -15,3 +15,11 @@ func (f *CommitFile) ID() string { func (f *CommitFile) Description() string { return f.Name } + +func (f *CommitFile) Added() bool { + return f.ChangeStatus == "A" +} + +func (f *CommitFile) Deleted() bool { + return f.ChangeStatus == "D" +} diff --git a/pkg/gui/controllers/commits_files_controller.go b/pkg/gui/controllers/commits_files_controller.go index 3e8849d88..f4acc0d31 100644 --- a/pkg/gui/controllers/commits_files_controller.go +++ b/pkg/gui/controllers/commits_files_controller.go @@ -161,9 +161,16 @@ func (self *CommitFilesController) discard(node *filetree.CommitFileNode) error return err } + prompt := self.c.Tr.DiscardFileChangesPrompt + if node.File.Added() { + prompt = self.c.Tr.DiscardAddedFileChangesPrompt + } else if node.File.Deleted() { + prompt = self.c.Tr.DiscardDeletedFileChangesPrompt + } + return self.c.Confirm(types.ConfirmOpts{ Title: self.c.Tr.DiscardFileChangesTitle, - Prompt: self.c.Tr.DiscardFileChangesPrompt, + Prompt: prompt, HandleConfirm: func() error { return self.c.WithWaitingStatus(self.c.Tr.RebasingStatus, func() error { self.c.LogAction(self.c.Tr.Actions.DiscardOldFileChange) diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 4025c6e9d..34d33f1ab 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -268,6 +268,8 @@ type TranslationSet struct { DiscardOldFileChange string DiscardFileChangesTitle string DiscardFileChangesPrompt string + DiscardAddedFileChangesPrompt string + DiscardDeletedFileChangesPrompt string DiscardNotSupportedForDirectory string DisabledForGPG string CreateRepo string @@ -955,7 +957,9 @@ func EnglishTranslationSet() TranslationSet { CheckoutCommitFile: "Checkout file", DiscardOldFileChange: "Discard this commit's changes to this file", DiscardFileChangesTitle: "Discard file changes", - DiscardFileChangesPrompt: "Are you sure you want to discard this commit's changes to this file? If this file was created in this commit, it will be deleted", + DiscardFileChangesPrompt: "Are you sure you want to discard this commit's changes to this file?", + DiscardAddedFileChangesPrompt: "Are you sure you want to discard this commit's changes to this file? The file was added in this commit, so it will be deleted again.", + DiscardDeletedFileChangesPrompt: "Are you sure you want to discard this commit's changes to this file? The file was deleted in this commit, so it will reappear.", DiscardNotSupportedForDirectory: "Discarding changes is not supported for entire directories. Please use a custom patch for this.", DisabledForGPG: "Feature not available for users using GPG", CreateRepo: "Not in a git repository. Create a new git repository? (y/n): ", diff --git a/pkg/integration/tests/commit/discard_old_file_change.go b/pkg/integration/tests/commit/discard_old_file_change.go index c42a0f743..5d5fcc0c8 100644 --- a/pkg/integration/tests/commit/discard_old_file_change.go +++ b/pkg/integration/tests/commit/discard_old_file_change.go @@ -43,7 +43,7 @@ var DiscardOldFileChange = NewIntegrationTest(NewIntegrationTestArgs{ t.ExpectPopup().Confirmation(). Title(Equals("Discard file changes")). - Content(Contains("Are you sure you want to discard this commit's changes to this file?")). + Content(Equals("Are you sure you want to discard this commit's changes to this file? The file was added in this commit, so it will be deleted again.")). Confirm() t.Views().CommitFiles().