git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Jonathan Gilbert via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Jonathan Gilbert <rcq8n2xf3v@liamekaens.com>,
	Junio C Hamano <gitster@pobox.com>,
	Jonathan Gilbert <JonathanG@iQmetrix.com>
Subject: [PATCH 1/1] git-gui: Revert untracked files by deleting them
Date: Mon, 28 Oct 2019 18:58:07 +0000	[thread overview]
Message-ID: <56faba848676ffc5441f373bc2105392fc2a3e11.1572289087.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.423.git.1572289087.gitgitgadget@gmail.com>

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

  reply	other threads:[~2019-10-28 18:58 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-28 18:58 [PATCH 0/1] Allow the 'revert' option in Git Gui to operate on untracked files, " Jonathan Gilbert via GitGitGadget
2019-10-28 18:58 ` Jonathan Gilbert via GitGitGadget [this message]
2019-10-29 21:27   ` [PATCH 1/1] git-gui: Revert untracked files by " 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=56faba848676ffc5441f373bc2105392fc2a3e11.1572289087.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=JonathanG@iQmetrix.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=rcq8n2xf3v@liamekaens.com \
    --subject='Re: [PATCH 1/1] git-gui: Revert untracked files by deleting them' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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).