git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/1] git-gui: Revert untracked files by deleting them

From
Jonathan Gilbert via GitGitGadget <gitgitgadget@gmail.com>
Date
Oct 28, 2019, 18:58 UTC
Message-ID
<56faba848676ffc5441f373bc2105392fc2a3e11.1572289087.git.gitgitgadget@gmail.com>
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
Previous: Jonathan Gilbert via GitGitGadgetNext: Pratyush Yadav
Message 2 of 10 in “Allow the 'revert' option in Git Gui to operate on untracked files, deleting them”
  1. 0/1 Allow the 'revert' option in Git Gui to operate on untracked files, deleting themJonathan Gilbert via GitGitGadget, Oct 28, 2019
  2. 1/1 git-gui: Revert untracked files by deleting themJonathan Gilbert via GitGitGadget, Oct 28, 2019
  3. Pratyush YadavOct 29, 2019
  4. Jonathan GilbertOct 29, 2019
  5. brian m. carlsonOct 29, 2019
  6. Jonathan GilbertOct 29, 2019
  7. Bert WesargOct 29, 2019
  8. Jonathan GilbertOct 29, 2019
  9. Jonathan GilbertOct 29, 2019
  10. Pratyush YadavOct 29, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.