git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/1] Allow the 'revert' option in Git Gui to operate on untracked files, deleting them
@ 2019-10-28 18:58 Jonathan Gilbert via GitGitGadget
  2019-10-28 18:58 ` [PATCH 1/1] git-gui: Revert untracked files by " Jonathan Gilbert via GitGitGadget
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Jonathan Gilbert via GitGitGadget @ 2019-10-28 18:58 UTC (permalink / raw)
  To: git; +Cc: Jonathan Gilbert, Junio C Hamano

My development environment sometimes makes automatic changes that I don't
want to keep. In some cases, this involves new files being added that I
don't want to commit or keep. I have typically had to explicitly delete
those files externally to Git Gui, and I want to be able to just select
those newly-created untracked files and "revert" them into oblivion.

This change updates the revert_helper function to check for untracked files
as well as changes, and then any changes to be reverted and untracked files
are handled by independent blocks of code. The user is prompted
independently for untracked files, since the underlying action is
fundamentally different (rm -f). If after deleting untracked files, the
directory containing them becomes empty, then the directory is removed as
well.

This introduces new strings in index.tcl. I have been told that there is a
separate process whereby the translations get updated.

Jonathan Gilbert (1):
  git-gui: Revert untracked files by deleting them

 git-gui/lib/index.tcl | 139 +++++++++++++++++++++++++++++++-----------
 1 file changed, 104 insertions(+), 35 deletions(-)


base-commit: 566a1439f6f56c2171b8853ddbca0ad3f5098770
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-423%2Flogiclrd%2Fgit-gui-revert-untracked-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-423/logiclrd/git-gui-revert-untracked-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/423
-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 1/1] git-gui: Revert untracked files by deleting them
  2019-10-28 18:58 [PATCH 0/1] Allow the 'revert' option in Git Gui to operate on untracked files, deleting them Jonathan Gilbert via GitGitGadget
@ 2019-10-28 18:58 ` Jonathan Gilbert via GitGitGadget
  2019-10-29 21:27   ` Pratyush Yadav
  2019-10-29  0:12 ` [PATCH 0/1] Allow the 'revert' option in Git Gui to operate on untracked files, " brian m. carlson
  2019-10-29 14:29 ` Bert Wesarg
  2 siblings, 1 reply; 10+ messages in thread
From: Jonathan Gilbert via GitGitGadget @ 2019-10-28 18:58 UTC (permalink / raw)
  To: git; +Cc: Jonathan Gilbert, Junio C Hamano, Jonathan Gilbert

From: Jonathan Gilbert <JonathanG@iQmetrix.com>

My development environment sometimes makes automatic changes
that I don't want to keep. In some cases, this involves new
files being added that I don't want to commit or keep. I have
typically had to explicitly delete those files externally to
Git Gui, and I want to be able to just select those newly-
created untracked files and "revert" them into oblivion.

This change updates the revert_helper function to check for
untracked files as well as changes, and then any changes to be
reverted and untracked files are handled by independent
blocks of code. The user is prompted independently for
untracked files, since the underlying action is fundamentally
different (rm -f). If after deleting untracked files, the
directory containing them becomes empty, then the directory is
removed as well.

This introduces new strings in index.tcl. I have been told that
there is a separate process whereby the translations get updated.

Signed-off-by: Jonathan Gilbert <JonathanG@iQmetrix.com>
---
 git-gui/lib/index.tcl | 139 +++++++++++++++++++++++++++++++-----------
 1 file changed, 104 insertions(+), 35 deletions(-)

diff --git a/git-gui/lib/index.tcl b/git-gui/lib/index.tcl
index e07b7a3762..cc1b651d56 100644
--- a/git-gui/lib/index.tcl
+++ b/git-gui/lib/index.tcl
@@ -393,11 +393,24 @@ proc revert_helper {txt paths} {
 
 	if {![lock_index begin-update]} return
 
+	# If an action is taken that implicitly unlocks the index, this gets cleared. Either way, it is executed at the end of the procedure.
+	set epilogue [list]
+	lappend epilogue {unlock_index}
+
+	proc already_unlocked {} { upvar epilogue epilogue; set epilogue [lsearch -inline -all -not -exact $epilogue {unlock_index}] }
+
 	set pathList [list]
+	set untrackedList [list]
 	set after {}
 	foreach path $paths {
 		switch -glob -- [lindex $file_states($path) 0] {
 		U? {continue}
+		?O {
+			lappend untrackedList $path
+			if {$path eq $current_diff_path} {
+				set after {reshow_diff;}
+			}
+		}
 		?M -
 		?T -
 		?D {
@@ -410,45 +423,101 @@ proc revert_helper {txt paths} {
 	}
 
 
-	# Split question between singular and plural cases, because
-	# such distinction is needed in some languages. Previously, the
-	# code used "Revert changes in" for both, but that can't work
-	# in languages where 'in' must be combined with word from
-	# rest of string (in different way for both cases of course).
-	#
-	# FIXME: Unfortunately, even that isn't enough in some languages
-	# as they have quite complex plural-form rules. Unfortunately,
-	# msgcat doesn't seem to support that kind of string translation.
-	#
-	set n [llength $pathList]
-	if {$n == 0} {
-		unlock_index
-		return
-	} elseif {$n == 1} {
-		set query [mc "Revert changes in file %s?" [short_path [lindex $pathList]]]
-	} else {
-		set query [mc "Revert changes in these %i files?" $n]
-	}
+	set numPaths [llength $pathList]
+	set numUntracked [llength $untrackedList]
 
-	set reply [tk_dialog \
-		.confirm_revert \
-		"[appname] ([reponame])" \
-		"$query
+	if {$numPaths > 0} {
+		# Split question between singular and plural cases, because
+		# such distinction is needed in some languages. Previously, the
+		# code used "Revert changes in" for both, but that can't work
+		# in languages where 'in' must be combined with word from
+		# rest of string (in different way for both cases of course).
+		#
+		# FIXME: Unfortunately, even that isn't enough in some languages
+		# as they have quite complex plural-form rules. Unfortunately,
+		# msgcat doesn't seem to support that kind of string translation.
+		if {$numPaths == 1} {
+			set query [mc "Revert changes in file %s?" [short_path [lindex $pathList]]]
+		} else {
+			set query [mc "Revert changes in these %i files?" $numPaths]
+		}
+
+		set reply [tk_dialog \
+			.confirm_revert \
+			"[appname] ([reponame])" \
+			"$query
 
 [mc "Any unstaged changes will be permanently lost by the revert."]" \
-		question \
-		1 \
-		[mc "Do Nothing"] \
-		[mc "Revert Changes"] \
-		]
-	if {$reply == 1} {
-		checkout_index \
-			$txt \
-			$pathList \
-			[concat $after [list ui_ready]]
-	} else {
-		unlock_index
+			question \
+			1 \
+			[mc "Do Nothing"] \
+			[mc "Revert Changes"] \
+			]
+
+		if {$reply == 1} {
+			checkout_index \
+				$txt \
+				$pathList \
+				[concat $after [list ui_ready]]
+
+			already_unlocked
+		}
+	}
+
+	if {$numUntracked > 0} {
+		# Split question between singular and plural cases, because
+		# such distinction is needed in some languages.
+		#
+		# FIXME: Unfortunately, even that isn't enough in some languages
+		# as they have quite complex plural-form rules. Unfortunately,
+		# msgcat doesn't seem to support that kind of string translation.
+		if {$numUntracked == 1} {
+			set query [mc "Delete untracked file %s?" [short_path [lindex $untrackedList]]]
+		} else {
+			set query [mc "Delete these %i untracked files?" $numUntracked]
+		}
+
+		set reply [tk_dialog \
+			.confirm_revert \
+			"[appname] ([reponame])" \
+			"$query
+
+[mc "Files will be permanently deleted."]" \
+			question \
+			1 \
+			[mc "Do Nothing"] \
+			[mc "Delete Files"] \
+			]
+
+		if {$reply == 1} {
+			file delete -- {*}$untrackedList
+
+			foreach path $untrackedList {
+				set directoryPath [file dirname $path]
+
+				while {$directoryPath != $path} {
+					set contents [glob -nocomplain -dir $path *]
+
+					if {[llength $contents] > 0} { break }
+
+					try {
+						file delete -- $path
+					}
+					catch {
+						# This is just a best effort, don't annoy the user with failure to remove empty directories.
+						break
+					}
+
+					set path $directoryPath
+					set directoryPath [file dirname $path]
+				}
+			}
+
+			lappend epilogue {ui_do_rescan}
+		}
 	}
+
+	foreach epilogueCommand $epilogue { {*}$epilogueCommand }
 }
 
 proc do_revert_selection {} {
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH 0/1] Allow the 'revert' option in Git Gui to operate on untracked files, deleting them
  2019-10-28 18:58 [PATCH 0/1] Allow the 'revert' option in Git Gui to operate on untracked files, deleting them Jonathan Gilbert via GitGitGadget
  2019-10-28 18:58 ` [PATCH 1/1] git-gui: Revert untracked files by " Jonathan Gilbert via GitGitGadget
@ 2019-10-29  0:12 ` brian m. carlson
  2019-10-29  1:45   ` Jonathan Gilbert
  2019-10-29 14:29 ` Bert Wesarg
  2 siblings, 1 reply; 10+ messages in thread
From: brian m. carlson @ 2019-10-29  0:12 UTC (permalink / raw)
  To: Jonathan Gilbert via GitGitGadget; +Cc: git, Jonathan Gilbert, Junio C Hamano

[-- Attachment #1: Type: text/plain, Size: 773 bytes --]

On 2019-10-28 at 18:58:06, Jonathan Gilbert via GitGitGadget wrote:
> My development environment sometimes makes automatic changes that I don't
> want to keep. In some cases, this involves new files being added that I
> don't want to commit or keep. I have typically had to explicitly delete
> those files externally to Git Gui, and I want to be able to just select
> those newly-created untracked files and "revert" them into oblivion.

Is there a reason these new files can't be ignored, with one of the
.gitignore file, .git/info/exclude, or core.excludesFile?

If so, it would be helpful to explain that in the commit message so we
can more fully understand the rationale here.
-- 
brian m. carlson: Houston, Texas, US
OpenPGP: https://keybase.io/bk2204

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 868 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 0/1] Allow the 'revert' option in Git Gui to operate on untracked files, deleting them
  2019-10-29  0:12 ` [PATCH 0/1] Allow the 'revert' option in Git Gui to operate on untracked files, " brian m. carlson
@ 2019-10-29  1:45   ` Jonathan Gilbert
  0 siblings, 0 replies; 10+ messages in thread
From: Jonathan Gilbert @ 2019-10-29  1:45 UTC (permalink / raw)
  To: brian m. carlson sandals-at-crustytoothpaste.net |GitHub
	Public/Example Allow|
  Cc: Jonathan Gilbert via GitGitGadget, git, Jonathan Gilbert, Junio C Hamano

> Is there a reason these new files can't be ignored, with one of the.gitignore file, .git/info/exclude, or core.excludesFile?

I guess it's implied in the way I worded the message, but I have
fallen into the habit of using Git-Gui to manage the state of the
working copy (at least in detail). I am primarily doing .NET
development, for which projects can have a file App.config that stores
various settings that apply to the project at runtime. The NuGet
package manager sometimes edits App.config on your behalf as part of
installing a package, and if a project doesn't already have an
App.config file, it adds one. It has also updated a packages.config
file and the main project file. If I decide that I actually don't want
the change after all, Git-Gui permits me to revert the packages.config
change and revert the project file change. If an existing App.config
file was edited, I can revert that too, but if it was newly-generated,
then I want to delete that file, but I don't want to ignore App.config
files going forward, because there's a good chance a future change may
introduce a different need for an App.config file.

With the current Git-Gui version, I need to exit the Git-Gui UI/flow,
navigate to the project in a console window (which probably isn't
already in the correct folder) and manually delete the unwanted file.
This deletion requires me to identify the file explicitly as well.
With the proposed change, the untracked file, which Git-Gui already
lists, can be selected, and then activating the "revert" function
performs a UI flow for deleting the file. Without these changes,
Git-Gui simply does nothing at all when you tell it to revert an
untracked file.

Another example is when I go to review changes and discover a VIM
crash dump or a spurious temporary file from a code analyzer, files
that aren't _expected_ to normally come into existence at all. While
we could try to anticipate every type of spurious file and .gitignore
them all, I prefer to simply delete the files, and for similar reasons
as before, navigating a separate tool to the correct folder to perform
the deletion is a manual, time-consuming and context-switching
process. This is what led me to want this feature directly from the
Git-Gui tool, which is what identifies the rogue file to me in the
first place.

If you can think of a concise way to say that, I'll be happy to add it
to the commit message. My intuition is that that's too wordy
as-written, but if that intuition is wrong, I can copy/paste this text
too. :-)

Thanks,

Jonathan Gilbert


On Mon, Oct 28, 2019 at 7:12 PM brian m. carlson
sandals-at-crustytoothpaste.net |GitHub Public/Example Allow|
<92ue75mvem3o2ht@sneakemail.com> wrote:
>
> On 2019-10-28 at 18:58:06, Jonathan Gilbert via GitGitGadget wrote:
> > My development environment sometimes makes automatic changes that I don't
> > want to keep. In some cases, this involves new files being added that I
> > don't want to commit or keep. I have typically had to explicitly delete
> > those files externally to Git Gui, and I want to be able to just select
> > those newly-created untracked files and "revert" them into oblivion.
>
> Is there a reason these new files can't be ignored, with one of the
> .gitignore file, .git/info/exclude, or core.excludesFile?
>
> If so, it would be helpful to explain that in the commit message so we
> can more fully understand the rationale here.
> --
> brian m. carlson: Houston, Texas, US
> OpenPGP: https://keybase.io/bk2204

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 0/1] Allow the 'revert' option in Git Gui to operate on untracked files, deleting them
  2019-10-28 18:58 [PATCH 0/1] Allow the 'revert' option in Git Gui to operate on untracked files, deleting them Jonathan Gilbert via GitGitGadget
  2019-10-28 18:58 ` [PATCH 1/1] git-gui: Revert untracked files by " Jonathan Gilbert via GitGitGadget
  2019-10-29  0:12 ` [PATCH 0/1] Allow the 'revert' option in Git Gui to operate on untracked files, " brian m. carlson
@ 2019-10-29 14:29 ` Bert Wesarg
  2019-10-29 20:25   ` Jonathan Gilbert
  2 siblings, 1 reply; 10+ messages in thread
From: Bert Wesarg @ 2019-10-29 14:29 UTC (permalink / raw)
  To: Jonathan Gilbert via GitGitGadget
  Cc: Git Mailing List, Jonathan Gilbert, Junio C Hamano

On Mon, Oct 28, 2019 at 7:58 PM Jonathan Gilbert via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> My development environment sometimes makes automatic changes that I don't
> want to keep. In some cases, this involves new files being added that I
> don't want to commit or keep. I have typically had to explicitly delete
> those files externally to Git Gui, and I want to be able to just select
> those newly-created untracked files and "revert" them into oblivion.
>

I have an entry in the 'Tools" menu for this called 'Delete':

[guitool "Delete"]
    cmd = rm -f \"$FILENAME\"
    noconsole = yes
    needsfile = yes
    confirm = yes

Best,
Bert

> This change updates the revert_helper function to check for untracked files
> as well as changes, and then any changes to be reverted and untracked files
> are handled by independent blocks of code. The user is prompted
> independently for untracked files, since the underlying action is
> fundamentally different (rm -f). If after deleting untracked files, the
> directory containing them becomes empty, then the directory is removed as
> well.
>
> This introduces new strings in index.tcl. I have been told that there is a
> separate process whereby the translations get updated.
>
> Jonathan Gilbert (1):
>   git-gui: Revert untracked files by deleting them
>
>  git-gui/lib/index.tcl | 139 +++++++++++++++++++++++++++++++-----------
>  1 file changed, 104 insertions(+), 35 deletions(-)
>
>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 0/1] Allow the 'revert' option in Git Gui to operate on untracked files, deleting them
  2019-10-29 14:29 ` Bert Wesarg
@ 2019-10-29 20:25   ` Jonathan Gilbert
  2019-10-29 20:33     ` Jonathan Gilbert
  2019-10-29 21:43     ` Pratyush Yadav
  0 siblings, 2 replies; 10+ messages in thread
From: Jonathan Gilbert @ 2019-10-29 20:25 UTC (permalink / raw)
  To: Bert Wesarg bert.wesarg-at-googlemail.com |GitHub Public/Example Allow|
  Cc: Jonathan Gilbert via GitGitGadget, Git Mailing List,
	Jonathan Gilbert, Junio C Hamano

That's kind of neat, I wasn't aware of that facet of Git Gui :-) But,
it isn't quite the same feature:

* It has to be manually set up on each installation.
* It invokes an external process, I don't know if it's safe to assume
that "rm" will work on all platforms (though I just tested it on my
Windows installation and it worked).
* It doesn't remove directories that it makes empty.
* I don't see a way to bind it to a keyboard shortcut. That could just
be me not knowing enough about custom tools, though. :-)
* It only processes the first file selected.
* If I select a tracked file, it will still delete it, and the feature
I'm looking for is more of a "return repository to clean state" type
function, like "revert" already is but extended to handle files that
you can't actually "git revert".

Thanks,

Jonathan Gilbert

On Tue, Oct 29, 2019 at 9:32 AM Bert Wesarg
bert.wesarg-at-googlemail.com |GitHub Public/Example Allow|
<xlwsizdz58ciy7t@sneakemail.com> wrote:
>
> On Mon, Oct 28, 2019 at 7:58 PM Jonathan Gilbert via GitGitGadget
> <gitgitgadget@gmail.com> wrote:
> >
> > My development environment sometimes makes automatic changes that I don't
> > want to keep. In some cases, this involves new files being added that I
> > don't want to commit or keep. I have typically had to explicitly delete
> > those files externally to Git Gui, and I want to be able to just select
> > those newly-created untracked files and "revert" them into oblivion.
> >
>
> I have an entry in the 'Tools" menu for this called 'Delete':
>
> [guitool "Delete"]
>     cmd = rm -f \"$FILENAME\"
>     noconsole = yes
>     needsfile = yes
>     confirm = yes
>
> Best,
> Bert
>
> > This change updates the revert_helper function to check for untracked files
> > as well as changes, and then any changes to be reverted and untracked files
> > are handled by independent blocks of code. The user is prompted
> > independently for untracked files, since the underlying action is
> > fundamentally different (rm -f). If after deleting untracked files, the
> > directory containing them becomes empty, then the directory is removed as
> > well.
> >
> > This introduces new strings in index.tcl. I have been told that there is a
> > separate process whereby the translations get updated.
> >
> > Jonathan Gilbert (1):
> >   git-gui: Revert untracked files by deleting them
> >
> >  git-gui/lib/index.tcl | 139 +++++++++++++++++++++++++++++++-----------
> >  1 file changed, 104 insertions(+), 35 deletions(-)
> >
> >

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 0/1] Allow the 'revert' option in Git Gui to operate on untracked files, deleting them
  2019-10-29 20:25   ` Jonathan Gilbert
@ 2019-10-29 20:33     ` Jonathan Gilbert
  2019-10-29 21:43     ` Pratyush Yadav
  1 sibling, 0 replies; 10+ messages in thread
From: Jonathan Gilbert @ 2019-10-29 20:33 UTC (permalink / raw)
  To: Bert Wesarg bert.wesarg-at-googlemail.com |GitHub Public/Example Allow|
  Cc: Jonathan Gilbert via GitGitGadget, Git Mailing List,
	Jonathan Gilbert, Junio C Hamano

(should have had:)
> > I have an entry in the "Tools" menu for this called 'Delete':

> That's kind of neat, I wasn't aware of that facet of Git Gui :-) But,
> it isn't quite the same feature:

Oops, double gaffe. I accidentally forgot to "Reply All", so this was
a re-send of the message. And when I re-sent it, I didn't notice that
the e-mail client hid the quoted line from me and accidentally sent it
without quoting Bert's line. I wasn't sure whether to write this
follow-up but the longer I stared at it, the more sure I was that
somebody would call me out on it so I decided to be pre-emptive. My
apologies if it would have been better to just let it slide.

Jonathan Gilbert

On Tue, Oct 29, 2019 at 3:25 PM Jonathan Gilbert <logic@deltaq.org> wrote:
>
> That's kind of neat, I wasn't aware of that facet of Git Gui :-) But,
> it isn't quite the same feature:
>
> * It has to be manually set up on each installation.
> * It invokes an external process, I don't know if it's safe to assume
> that "rm" will work on all platforms (though I just tested it on my
> Windows installation and it worked).
> * It doesn't remove directories that it makes empty.
> * I don't see a way to bind it to a keyboard shortcut. That could just
> be me not knowing enough about custom tools, though. :-)
> * It only processes the first file selected.
> * If I select a tracked file, it will still delete it, and the feature
> I'm looking for is more of a "return repository to clean state" type
> function, like "revert" already is but extended to handle files that
> you can't actually "git revert".
>
> Thanks,
>
> Jonathan Gilbert
>
> On Tue, Oct 29, 2019 at 9:32 AM Bert Wesarg
> bert.wesarg-at-googlemail.com |GitHub Public/Example Allow|
> <xlwsizdz58ciy7t@sneakemail.com> wrote:
> >
> > On Mon, Oct 28, 2019 at 7:58 PM Jonathan Gilbert via GitGitGadget
> > <gitgitgadget@gmail.com> wrote:
> > >
> > > My development environment sometimes makes automatic changes that I don't
> > > want to keep. In some cases, this involves new files being added that I
> > > don't want to commit or keep. I have typically had to explicitly delete
> > > those files externally to Git Gui, and I want to be able to just select
> > > those newly-created untracked files and "revert" them into oblivion.
> > >
> >
> > I have an entry in the 'Tools" menu for this called 'Delete':
> >
> > [guitool "Delete"]
> >     cmd = rm -f \"$FILENAME\"
> >     noconsole = yes
> >     needsfile = yes
> >     confirm = yes
> >
> > Best,
> > Bert
> >
> > > This change updates the revert_helper function to check for untracked files
> > > as well as changes, and then any changes to be reverted and untracked files
> > > are handled by independent blocks of code. The user is prompted
> > > independently for untracked files, since the underlying action is
> > > fundamentally different (rm -f). If after deleting untracked files, the
> > > directory containing them becomes empty, then the directory is removed as
> > > well.
> > >
> > > This introduces new strings in index.tcl. I have been told that there is a
> > > separate process whereby the translations get updated.
> > >
> > > Jonathan Gilbert (1):
> > >   git-gui: Revert untracked files by deleting them
> > >
> > >  git-gui/lib/index.tcl | 139 +++++++++++++++++++++++++++++++-----------
> > >  1 file changed, 104 insertions(+), 35 deletions(-)
> > >
> > >

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 1/1] git-gui: Revert untracked files by deleting them
  2019-10-28 18:58 ` [PATCH 1/1] git-gui: Revert untracked files by " Jonathan Gilbert via GitGitGadget
@ 2019-10-29 21:27   ` Pratyush Yadav
  2019-10-29 23:52     ` Jonathan Gilbert
  0 siblings, 1 reply; 10+ messages in thread
From: Pratyush Yadav @ 2019-10-29 21:27 UTC (permalink / raw)
  To: Jonathan Gilbert via GitGitGadget
  Cc: git, Jonathan Gilbert, Junio C Hamano, Jonathan Gilbert

Hi Jonathan,

Thanks for the patch.

While git-gui is distributed in the main Git tree, the development 
happens on a separate repo, and the Git maintainer periodically pulls in 
changes from that repo. It can be found at [0]. For now, I munged your 
patch to apply on my tree, but please base it on the git-gui repo for 
your re-rolls or future patches. You can use GitGitGadget to do that 
[1].

Now, on to the patch.

Nitpick: Do not use a capital letter after 'git-gui:' in your subject. 
So the subject should look something like:

  git-gui: revert untracked files by deleting them

On 28/10/19 06:58PM, Jonathan Gilbert via GitGitGadget wrote:
> From: Jonathan Gilbert <JonathanG@iQmetrix.com>
> 
> My development environment sometimes makes automatic changes
> that I don't want to keep. In some cases, this involves new
> files being added that I don't want to commit or keep. I have
> typically had to explicitly delete those files externally to
> Git Gui, and I want to be able to just select those newly-
> created untracked files and "revert" them into oblivion.

I think the description of your workflow belongs in the cover letter 
more than here. The commit message should take a more neutral tone. So, 
describe the problem in an objective way that not only you, but other 
git-gui users might face.
 
> This change updates the revert_helper function to check for
> untracked files as well as changes, and then any changes to be
> reverted and untracked files are handled by independent
> blocks of code. The user is prompted independently for
> untracked files, since the underlying action is fundamentally
> different (rm -f). If after deleting untracked files, the
> directory containing them becomes empty, then the directory is
> removed as well.
> 
> This introduces new strings in index.tcl. I have been told that
> there is a separate process whereby the translations get updated.

I don't think this should be in the commit message. The commit message 
describes the change. The process of getting that change integrated 
should be discussed elsewhere (elsewhere == this list). But yes, there 
is a separate process to update translations (but unfortunately no one 
is actively doing that yet).
 
> Signed-off-by: Jonathan Gilbert <JonathanG@iQmetrix.com>
> ---
>  git-gui/lib/index.tcl | 139 +++++++++++++++++++++++++++++++-----------
>  1 file changed, 104 insertions(+), 35 deletions(-)
> 
> diff --git a/git-gui/lib/index.tcl b/git-gui/lib/index.tcl
> index e07b7a3762..cc1b651d56 100644
> --- a/git-gui/lib/index.tcl
> +++ b/git-gui/lib/index.tcl
> @@ -393,11 +393,24 @@ proc revert_helper {txt paths} {
>  
>  	if {![lock_index begin-update]} return
>  
> +	# If an action is taken that implicitly unlocks the index, this gets cleared. Either way, it is executed at the end of the procedure.

The convention is to wrap lines at 80 columns wherever possible. Please 
follow that. You can look at the rest of the code for examples.

You have other lines too that are too long. The same comment applies to 
all those.

> +	set epilogue [list]
> +	lappend epilogue {unlock_index}
> +
> +	proc already_unlocked {} { upvar epilogue epilogue; set epilogue [lsearch -inline -all -not -exact $epilogue {unlock_index}] }

A procedure defined inside a procedure? Please don't do that. Define it 
outside.

Also, what is this procedure supposed to do? It is not very clear at 
first read.

> +
>  	set pathList [list]
> +	set untrackedList [list]

Nitpick: Ugh! camelCase in a sea of snake_cases. What's even more 
unfortunate is that `pathList` itself is in camelCase, so that's 
probably the reason you went with camelCase in the first place. Maybe 
re-name `pathList` to `path_list` while we're at it, and then use 
snake_case everywhere?

>  	set after {}
>  	foreach path $paths {
>  		switch -glob -- [lindex $file_states($path) 0] {
>  		U? {continue}
> +		?O {
> +			lappend untrackedList $path
> +			if {$path eq $current_diff_path} {
> +				set after {reshow_diff;}
> +			}
> +		}
>  		?M -
>  		?T -
>  		?D {
> @@ -410,45 +423,101 @@ proc revert_helper {txt paths} {
>  	}
>  
>  
> -	# Split question between singular and plural cases, because
> -	# such distinction is needed in some languages. Previously, the
> -	# code used "Revert changes in" for both, but that can't work
> -	# in languages where 'in' must be combined with word from
> -	# rest of string (in different way for both cases of course).
> -	#
> -	# FIXME: Unfortunately, even that isn't enough in some languages
> -	# as they have quite complex plural-form rules. Unfortunately,
> -	# msgcat doesn't seem to support that kind of string translation.
> -	#
> -	set n [llength $pathList]
> -	if {$n == 0} {
> -		unlock_index
> -		return
> -	} elseif {$n == 1} {
> -		set query [mc "Revert changes in file %s?" [short_path [lindex $pathList]]]
> -	} else {
> -		set query [mc "Revert changes in these %i files?" $n]
> -	}
> +	set numPaths [llength $pathList]
> +	set numUntracked [llength $untrackedList]
>  
> -	set reply [tk_dialog \
> -		.confirm_revert \
> -		"[appname] ([reponame])" \
> -		"$query
> +	if {$numPaths > 0} {
> +		# Split question between singular and plural cases, because
> +		# such distinction is needed in some languages. Previously, the
> +		# code used "Revert changes in" for both, but that can't work
> +		# in languages where 'in' must be combined with word from
> +		# rest of string (in different way for both cases of course).
> +		#
> +		# FIXME: Unfortunately, even that isn't enough in some languages
> +		# as they have quite complex plural-form rules. Unfortunately,
> +		# msgcat doesn't seem to support that kind of string translation.
> +		if {$numPaths == 1} {
> +			set query [mc "Revert changes in file %s?" [short_path [lindex $pathList]]]
> +		} else {
> +			set query [mc "Revert changes in these %i files?" $numPaths]
> +		}
> +
> +		set reply [tk_dialog \
> +			.confirm_revert \
> +			"[appname] ([reponame])" \
> +			"$query
>  
>  [mc "Any unstaged changes will be permanently lost by the revert."]" \
> -		question \
> -		1 \
> -		[mc "Do Nothing"] \
> -		[mc "Revert Changes"] \
> -		]
> -	if {$reply == 1} {
> -		checkout_index \
> -			$txt \
> -			$pathList \
> -			[concat $after [list ui_ready]]
> -	} else {
> -		unlock_index
> +			question \
> +			1 \
> +			[mc "Do Nothing"] \
> +			[mc "Revert Changes"] \
> +			]
> +
> +		if {$reply == 1} {
> +			checkout_index \
> +				$txt \
> +				$pathList \
> +				[concat $after [list ui_ready]]
> +
> +			already_unlocked
> +		}
> +	}
> +
> +	if {$numUntracked > 0} {
> +		# Split question between singular and plural cases, because
> +		# such distinction is needed in some languages.
> +		#
> +		# FIXME: Unfortunately, even that isn't enough in some languages
> +		# as they have quite complex plural-form rules. Unfortunately,
> +		# msgcat doesn't seem to support that kind of string translation.
> +		if {$numUntracked == 1} {
> +			set query [mc "Delete untracked file %s?" [short_path [lindex $untrackedList]]]
> +		} else {
> +			set query [mc "Delete these %i untracked files?" $numUntracked]
> +		}
> +
> +		set reply [tk_dialog \
> +			.confirm_revert \
> +			"[appname] ([reponame])" \
> +			"$query
> +
> +[mc "Files will be permanently deleted."]" \
> +			question \
> +			1 \
> +			[mc "Do Nothing"] \
> +			[mc "Delete Files"] \
> +			]
> +
> +		if {$reply == 1} {
> +			file delete -- {*}$untrackedList
> +
> +			foreach path $untrackedList {
> +				set directoryPath [file dirname $path]
> +
> +				while {$directoryPath != $path} {
> +					set contents [glob -nocomplain -dir $path *]
> +
> +					if {[llength $contents] > 0} { break }
> +
> +					try {
> +						file delete -- $path
> +					}
> +					catch {
> +						# This is just a best effort, don't annoy the user with failure to remove empty directories.
> +						break
> +					}

The convention in this project is to just use `catch`, and not try. So 
something like:

  catch {file delete -- $path}

> +
> +					set path $directoryPath
> +					set directoryPath [file dirname $path]

I read this loop as "if all the paths in a directory are removed, remove 
the empty directory as well". Do I read correctly?

Will there be problems in deleting the directory? What if the user wants 
to keep the directory, and just delete the files? Is that even a valid 
use-case?

> +				}
> +			}
> +
> +			lappend epilogue {ui_do_rescan}

A rescan is an expensive operation, so we should use it judiciously. Are 
you sure it is really needed? The "Revert" code does not do a rescan but 
still manages to update the list of "unstaged files". How does it manage 
that? Can the new code do something similar?

> +		}
>  	}
> +
> +	foreach epilogueCommand $epilogue { {*}$epilogueCommand }

Why not use `eval` [2]? Are there any downsides to that compared to your 
way? If not, use `eval`. At least it means better readability if nothing 
else.

As far as I see, you use $epilogue for two things: unlocking the index 
and rescanning. Can you move the control flow around that both can be 
done in the "normal" way. That is, they are not a part of a list of 
things to do at the end, but instead are done when needed. For example, 
just move the call to `unlock_index` at the end instead of putting it in 
epilogue. Can the same be done for `ui_do_rescan` (if you do go with a 
rescan instead of doing it like the existing revert does)?

>  }
>  
>  proc do_revert_selection {} {

While I appreciate the idea of such a feature, I'm surprised by how 
complex the implementation is. I expected something much simpler. The 
complexity can probably be managed a bit better by moving the control 
flow around.

I couldn't dive in the code as deep as I wanted to because I don't have 
too much time on my hands. But maybe I'll look further by the time your 
re-roll arrives. Thanks.

[0] https://github.com/prati0100/git-gui
[1] https://github.com/prati0100/git-gui#using-gitgitgadget
[2] https://www.tcl.tk/man/tcl8.6/TclCmd/eval.htm

-- 
Regards,
Pratyush Yadav

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 0/1] Allow the 'revert' option in Git Gui to operate on untracked files, deleting them
  2019-10-29 20:25   ` Jonathan Gilbert
  2019-10-29 20:33     ` Jonathan Gilbert
@ 2019-10-29 21:43     ` Pratyush Yadav
  1 sibling, 0 replies; 10+ messages in thread
From: Pratyush Yadav @ 2019-10-29 21:43 UTC (permalink / raw)
  To: Jonathan Gilbert
  Cc: Bert Wesarg bert.wesarg-at-googlemail.com |GitHub Public/Example
	Allow|,
	Jonathan Gilbert via GitGitGadget, Git Mailing List,
	Jonathan Gilbert, Junio C Hamano

On 29/10/19 03:25PM, Jonathan Gilbert wrote:
> That's kind of neat, I wasn't aware of that facet of Git Gui :-) But,
> it isn't quite the same feature:
> 
> * It has to be manually set up on each installation.
> * It invokes an external process, I don't know if it's safe to assume
> that "rm" will work on all platforms (though I just tested it on my
> Windows installation and it worked).
> * It doesn't remove directories that it makes empty.
> * I don't see a way to bind it to a keyboard shortcut. That could just
> be me not knowing enough about custom tools, though. :-)

You can't as of now. Harish was in the process of implementing this [0], 
but I left some comments and he hasn't re-rolled the patch since. So 
unless I find some time to tie it up, this will remain un-implemented. 

Of course, if you'd like to see that feature in git-gui, feel free to 
pick it up and brush up the changes :). The latest version can be found 
at [1]. But it does not have a proper commit message (apart from the 
other changes I suggested) since Harish did not format the patch 
correctly when sending.

> * It only processes the first file selected.
> * If I select a tracked file, it will still delete it, and the feature
> I'm looking for is more of a "return repository to clean state" type
> function, like "revert" already is but extended to handle files that
> you can't actually "git revert".

[0] https://public-inbox.org/git/CACV9s2MQCP04QASgt0xhi3cSNPSKjwXTufxmZQXAUNvnWD9DSw@mail.gmail.com/ 
[1] https://github.com/prati0100/git-gui/tree/hk/custom-keyboard-shortcuts

-- 
Regards,
Pratyush Yadav

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 1/1] git-gui: Revert untracked files by deleting them
  2019-10-29 21:27   ` Pratyush Yadav
@ 2019-10-29 23:52     ` Jonathan Gilbert
  0 siblings, 0 replies; 10+ messages in thread
From: Jonathan Gilbert @ 2019-10-29 23:52 UTC (permalink / raw)
  To: Pratyush Yadav me-at-yadavpratyush.com |GitHub Public/Example Allow|
  Cc: Jonathan Gilbert via GitGitGadget, git, Jonathan Gilbert,
	Junio C Hamano, Jonathan Gilbert

Thanks for the reply :-)

> While git-gui is distributed in the main Git tree, the development
> happens on a separate repo, and the Git maintainer periodically pulls in
> changes from that repo. It can be found at [0]. For now, I munged your
> patch to apply on my tree, but please base it on the git-gui repo for
> your re-rolls or future patches. You can use GitGitGadget to do that
> [1].

Alright :-)

> Now, on to the patch.
>
> On 28/10/19 06:58PM, Jonathan Gilbert via GitGitGadget wrote:
> > From: Jonathan Gilbert <JonathanG@iQmetrix.com>
> >
> > My development environment sometimes makes automatic changes
> > that I don't want to keep. In some cases, this involves new
> > files being added that I don't want to commit or keep. I have
> > typically had to explicitly delete those files externally to
> > Git Gui, and I want to be able to just select those newly-
> > created untracked files and "revert" them into oblivion.
>
> I think the description of your workflow belongs in the cover letter
> more than here. The commit message should take a more neutral tone. So,
> describe the problem in an objective way that not only you, but other
> git-gui users might face.

That's totally fair, I was sort of shooting in the dark since this is
the first such patch I have made. I will reword the commit message.

> > +     # If an action is taken that implicitly unlocks the index, this gets cleared. Either way, it is executed at the end of the procedure.
>
> The convention is to wrap lines at 80 columns wherever possible. Please
> follow that. You can look at the rest of the code for examples.
>
> You have other lines too that are too long. The same comment applies to
> all those.

Roger.

> > +     set epilogue [list]
> > +     lappend epilogue {unlock_index}
> > +
> > +     proc already_unlocked {} { upvar epilogue epilogue; set epilogue [lsearch -inline -all -not -exact $epilogue {unlock_index}] }
>
> A procedure defined inside a procedure? Please don't do that. Define it
> outside.
>
> Also, what is this procedure supposed to do? It is not very clear at
> first read.

The name could probably be improved, but this procedure can't live
outside of the outer proc because it is lexically tied to it. It takes
an action on state that is in a local variable. If it's flat-out
disallowed to use a proc to abstract this, then every place that wants
to indicate that the repository is already unlocked and doesn't need
to be explicitly unlocked in the epilogue will have to repeat the code
inside the proc.

This becomes a non-issue if I rework the function so that it doesn't
end with a dynamic epilogue (see below).

> > +
> >       set pathList [list]
> > +     set untrackedList [list]
>
> Nitpick: Ugh! camelCase in a sea of snake_cases. What's even more
> unfortunate is that `pathList` itself is in camelCase, so that's
> probably the reason you went with camelCase in the first place. Maybe
> re-name `pathList` to `path_list` while we're at it, and then use
> snake_case everywhere?

Absolutely, yeah I was just copying what I already saw there, but I'm
all in favour of consistency. :-)

> > +     set numPaths [llength $pathList]
> > +     set numUntracked [llength $untrackedList]

Will fix this too.

> > +                                     try {
> > +                                             file delete -- $path
> > +                                     }
> > +                                     catch {
> > +                                             # This is just a best effort, don't annoy the user with failure to remove empty directories.
> > +                                             break
> > +                                     }
>
> The convention in this project is to just use `catch`, and not try. So
> something like:
>
>   catch {file delete -- $path}

I'm not super familiar with TCL, where does the `break` statement fit into this?

I did a Google search and saw that catch returns a value that you can
inspect, would I write this?:
```
if { [catch {file delete -- $path}] } {
  break
}
```

> > +                                     set path $directoryPath
> > +                                     set directoryPath [file dirname $path]
>
> I read this loop as "if all the paths in a directory are removed, remove
> the empty directory as well". Do I read correctly?
>
> Will there be problems in deleting the directory? What if the user wants
> to keep the directory, and just delete the files? Is that even a valid
> use-case?

Well, Git itself doesn't keep empty directories. As such, I wrote the
code with an (undocumented) assumption that if there is a directory
that contains only a single untracked file, then the directory was
probably created to put the file in it.

> > +                             }
> > +                     }
> > +
> > +                     lappend epilogue {ui_do_rescan}
>
> A rescan is an expensive operation, so we should use it judiciously. Are
> you sure it is really needed? The "Revert" code does not do a rescan but
> still manages to update the list of "unstaged files". How does it manage
> that? Can the new code do something similar?

I'll look into it, but I'm assuming it's happening as part of `checkout_index`.

> > +             }
> >       }
> > +
> > +     foreach epilogueCommand $epilogue { {*}$epilogueCommand }
>
> Why not use `eval` [2]? Are there any downsides to that compared to your
> way? If not, use `eval`. At least it means better readability if nothing
> else.

I wrote some TCL over a decade ago for Eggdrop bot scripts, and
haven't touched it until now, so my ambient knowledge of TCL is quite
limited. I'll look into how to use `eval` for this. :-)

> As far as I see, you use $epilogue for two things: unlocking the index
> and rescanning. Can you move the control flow around that both can be
> done in the "normal" way. That is, they are not a part of a list of
> things to do at the end, but instead are done when needed. For example,
> just move the call to `unlock_index` at the end instead of putting it in
> epilogue. Can the same be done for `ui_do_rescan` (if you do go with a
> rescan instead of doing it like the existing revert does)?

Well, `unlock_index` will presumably throw an error if
`checkout_index` gets called, but `checkout_index` only gets called if
the scan finds tracked files with changes _and_ the user opts to
revert them.

Similarly, `ui_do_rescan` wants to be done exactly once at the end,
but only if the scan finds untracked files _and_ the user opts to
delete them.

An alternative to the epilogue could be two booleans
`need_unlock_index` that starts out true and `need_rescan` that starts
out false, and then the function's epilogue, instead of being dynamic,
just checks the booleans and does the things.

> >  }
> >
> >  proc do_revert_selection {} {
>
> While I appreciate the idea of such a feature, I'm surprised by how
> complex the implementation is. I expected something much simpler. The
> complexity can probably be managed a bit better by moving the control
> flow around.
>
> I couldn't dive in the code as deep as I wanted to because I don't have
> too much time on my hands. But maybe I'll look further by the time your
> re-roll arrives. Thanks.
>
> [0] https://github.com/prati0100/git-gui
> [1] https://github.com/prati0100/git-gui#using-gitgitgadget
> [2] https://www.tcl.tk/man/tcl8.6/TclCmd/eval.htm
>
> --
> Regards,
> Pratyush Yadav

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2019-10-29 23:52 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-28 18:58 [PATCH 0/1] Allow the 'revert' option in Git Gui to operate on untracked files, deleting them Jonathan Gilbert via GitGitGadget
2019-10-28 18:58 ` [PATCH 1/1] git-gui: Revert untracked files by " Jonathan Gilbert via GitGitGadget
2019-10-29 21:27   ` Pratyush Yadav
2019-10-29 23:52     ` Jonathan Gilbert
2019-10-29  0:12 ` [PATCH 0/1] Allow the 'revert' option in Git Gui to operate on untracked files, " brian m. carlson
2019-10-29  1:45   ` Jonathan Gilbert
2019-10-29 14:29 ` Bert Wesarg
2019-10-29 20:25   ` Jonathan Gilbert
2019-10-29 20:33     ` Jonathan Gilbert
2019-10-29 21:43     ` Pratyush Yadav

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).