Skip to contentSkip to conversation

Make git-reset delete empty directories

PATCH In review Shawn Pearce 2 participants 4 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Make git-reset delete empty directories

When git-reset --hard is used and a subdirectory becomes empty (as it contains no tracked files in the target tree) the empty subdirectory should be removed. This matches the behavior of git-checkout-index and git-read-tree -m which would not have created the subdirectory or would have deleted it when updating the working directory.

Subdirectories which are not empty will be left behind. This may happen if the subdirectory still contains object files from the user's build process (for example).

---

 git-reset.sh     |   17 ++++++++++++---
 t/t7101-reset.sh |   63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+), 3 deletions(-)
 create mode 100755 t/t7101-reset.sh

base 3397f9df53092871de2c33c143f1f4413654c40d
last 9e4158a26134a05e6011613976e26e7bfa813aca

git-reset.sh +14 -3
index fe53fc8065dc82c0054ba9891b28a00b0752bf87..8eac4640bc8dfb655f8429036b68644df027dea4 100755
@@ -74,22 +74,33 @@ case "$reset_type" in
 	git-ls-files --cached -z |
 	perl -e '
 		use strict;
-		my (%keep, $fh);
+		my (%keep_file, %keep_dir, %rm_dir, $fh);
 		$/ = "\0";
 		while (<STDIN>) {
 			chomp;
-			$keep{$_} = 1;
+			$keep_file{$_} = 1;
+			while (s,/?[^/]+$,, && $_) {
+				last if $keep_dir{$_}++;
+			}
 		}
 		open $fh, "<", $ARGV[0]
 			or die "cannot open $ARGV[0]";
 		while (<$fh>) {
 			chomp;
-			if (! exists $keep{$_}) {
+			if (! exists $keep_file{$_}) {
 				# it is ok if this fails -- it may already
 				# have been culled by checkout-index.
 				unlink $_;
+				while (s,/?[^/]+$,, && $_ && !$keep_dir{$_}) {
+					last if $rm_dir{$_}++;
+				}
 			}
 		}
+		foreach (sort {length($b) <=> length($a)} keys %rm_dir) {
+			# it is ok if this fails -- it may have user files
+			# we do not track and thus should not delete.
+			rmdir $_;
+		}
 	' $tmp-exists
 	;;
 --soft )
t/t7101-reset.sh new file +63 -0

63 lines not on this page · raw message

#1 lore raw
Quoted 3 lines · Shawn Pearce

When git-reset --hard is used and a subdirectory becomes empty (as it contains no tracked files in the target tree) the empty subdirectory should be removed.

I thought I said it would be a few-liner, but it appears I did not send that message.

This untested one is far simpler, if less efficient, isn't it?

---

git-reset.sh +3 -0
index fe53fc8..195d043 100755
@@ -88,6 +88,9 @@ case "$reset_type" in
 				# it is ok if this fails -- it may already
 				# have been culled by checkout-index.
 				unlink $_;
+				while (s|/[^/]*$|| && $_ ne "") {
+					rmdir($_) or last;
+				}
 			}
 		}
 	' $tmp-exists
#2 lore raw
Quoted 2 lines · Junio C Hamano

I thought I said it would be a few-liner, but it appears I did not send that message.

You did. Somehow I forgot that request. I wrote a simple approach similar to what you show below then tried to make it slightly more efficient by only attempting to delete each directory once. Obviously that wasn't wise on my part. :-)

Quoted 9 lines

This untested one is far simpler, if less efficient, isn't it?

---

git-reset.sh +0 -0
index fe53fc8..195d043 100755

git-reset.sh case "$reset_type" in · line 96

Quoted 9 lines
git-reset.sh +3 -0
@@ -88,6 +88,9 @@ case "$reset_type" in
 				# it is ok if this fails -- it may already
 				# have been culled by checkout-index.
 				unlink $_;
+				while (s|/[^/]*$|| && $_ ne "") {
+					rmdir($_) or last;
+				}
 			}
 		}
 	' $tmp-exists

I just ran it through the test script (t7101-reset.sh) that I included in my patch and it passed, so clearly the version above would be the better version to include.

Since it was untested before you are welcome to include the test script with your version. :-)

Signature · 1 line
Shawn.
#3 lore raw
Quoted 3 lines · Shawn Pearce
Quoted 2 lines · Junio C Hamano

I thought I said it would be a few-liner, but it appears I did not send that message.

Actually its only a one line change if you accept Perl as Perl, but not everyone likes having their loops do nothing in the loop body whilest the condition does all of the work.

---

git-reset.sh +1 -0
index fe53fc8..e6471c0 100755
@@ -88,6 +88,7 @@ case "$reset_type" in
                                # it is ok if this fails -- it may already
                                # have been culled by checkout-index.
                                unlink $_;
+                               1 while (s,/[^/]*$,, && rmdir $_);
                        }
                }
        ' $tmp-exists