{"thread":{"id":"3363","subject":"[PATCH] Make git-reset delete empty directories","startedAt":"2006-02-17T07:26:16Z","lastAt":"2006-02-17T16:01:12Z","messageCount":4,"participants":["Shawn Pearce","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"16304","messageId":"20060217072616.GA15358@spearce.org","threadId":"3363","inReplyTo":null,"subject":"[PATCH] Make git-reset delete empty directories","fromName":"Shawn Pearce","fromEmail":"spearce@spearce.org","sentAt":"2006-02-17T07:26:16Z","receivedAt":"2006-02-17T07:26:16Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"When git-reset --hard is used and a subdirectory becomes\nempty (as it contains no tracked files in the target tree)\nthe empty subdirectory should be removed.  This matches\nthe behavior of git-checkout-index and git-read-tree -m\nwhich would not have created the subdirectory or would\nhave deleted it when updating the working directory.\n\nSubdirectories which are not empty will be left behind.\nThis may happen if the subdirectory still contains object\nfiles from the user's build process (for example).\n\n---\n\n git-reset.sh     |   17 ++++++++++++---\n t/t7101-reset.sh |   63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 77 insertions(+), 3 deletions(-)\n create mode 100755 t/t7101-reset.sh\n\nbase 3397f9df53092871de2c33c143f1f4413654c40d\nlast 9e4158a26134a05e6011613976e26e7bfa813aca\ndiff --git a/git-reset.sh b/git-reset.sh\nindex fe53fc8065dc82c0054ba9891b28a00b0752bf87..8eac4640bc8dfb655f8429036b68644df027dea4 100755\n--- a/git-reset.sh\n+++ b/git-reset.sh\n@@ -74,22 +74,33 @@ case \"$reset_type\" in\n \tgit-ls-files --cached -z |\n \tperl -e '\n \t\tuse strict;\n-\t\tmy (%keep, $fh);\n+\t\tmy (%keep_file, %keep_dir, %rm_dir, $fh);\n \t\t$/ = \"\\0\";\n \t\twhile (<STDIN>) {\n \t\t\tchomp;\n-\t\t\t$keep{$_} = 1;\n+\t\t\t$keep_file{$_} = 1;\n+\t\t\twhile (s,/?[^/]+$,, && $_) {\n+\t\t\t\tlast if $keep_dir{$_}++;\n+\t\t\t}\n \t\t}\n \t\topen $fh, \"<\", $ARGV[0]\n \t\t\tor die \"cannot open $ARGV[0]\";\n \t\twhile (<$fh>) {\n \t\t\tchomp;\n-\t\t\tif (! exists $keep{$_}) {\n+\t\t\tif (! exists $keep_file{$_}) {\n \t\t\t\t# it is ok if this fails -- it may already\n \t\t\t\t# have been culled by checkout-index.\n \t\t\t\tunlink $_;\n+\t\t\t\twhile (s,/?[^/]+$,, && $_ && !$keep_dir{$_}) {\n+\t\t\t\t\tlast if $rm_dir{$_}++;\n+\t\t\t\t}\n \t\t\t}\n \t\t}\n+\t\tforeach (sort {length($b) <=> length($a)} keys %rm_dir) {\n+\t\t\t# it is ok if this fails -- it may have user files\n+\t\t\t# we do not track and thus should not delete.\n+\t\t\trmdir $_;\n+\t\t}\n \t' $tmp-exists\n \t;;\n --soft )\ndiff --git a/t/t7101-reset.sh b/t/t7101-reset.sh\nnew file mode 100755\nindex 0000000000000000000000000000000000000000..a9191407f21c748f4c00bf909f670fc2b5124ec3\n--- /dev/null\n+++ b/t/t7101-reset.sh\n@@ -0,0 +1,63 @@\n+#!/bin/sh\n+#\n+# Copyright (c) 2006 Shawn Pearce\n+#\n+\n+test_description='git-reset should cull empty subdirs'\n+. ./test-lib.sh\n+\n+test_expect_success \\\n+    'creating initial files' \\\n+    'mkdir path0 &&\n+     cp ../../COPYING path0/COPYING &&\n+     git-add path0/COPYING &&\n+     git-commit -m add -a'\n+\n+test_expect_success \\\n+    'creating second files' \\\n+    'mkdir path1 &&\n+     mkdir path1/path2 &&\n+     cp ../../COPYING path1/path2/COPYING &&\n+     cp ../../COPYING path1/COPYING &&\n+     cp ../../COPYING COPYING &&\n+     cp ../../COPYING path0/COPYING-TOO &&\n+     git-add path1/path2/COPYING &&\n+     git-add path1/COPYING &&\n+     git-add COPYING &&\n+     git-add path0/COPYING-TOO &&\n+     git-commit -m change -a'\n+\n+test_expect_success \\\n+    'resetting tree HEAD^' \\\n+    'git-reset --hard HEAD^'\n+\n+test_expect_success \\\n+    'checking initial files exist after rewind' \\\n+    'test -d path0 &&\n+     test -f path0/COPYING'\n+\n+test_expect_failure \\\n+    'checking lack of path1/path2/COPYING' \\\n+    'test -f path1/path2/COPYING'\n+\n+test_expect_failure \\\n+    'checking lack of path1/COPYING' \\\n+    'test -f path1/COPYING'\n+\n+test_expect_failure \\\n+    'checking lack of COPYING' \\\n+    'test -f COPYING'\n+\n+test_expect_failure \\\n+    'checking checking lack of path1/COPYING-TOO' \\\n+    'test -f path0/COPYING-TOO'\n+\n+test_expect_failure \\\n+    'checking lack of path1/path2' \\\n+    'test -d path1/path2'\n+\n+test_expect_failure \\\n+    'checking lack of path1' \\\n+    'test -d path1'\n+\n+test_done\n"},{"id":"16305","messageId":"7v7j7u8koz.fsf@assigned-by-dhcp.cox.net","threadId":"3363","inReplyTo":"20060217072616.GA15358@spearce.org","subject":"Re: [PATCH] Make git-reset delete empty directories","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-02-17T08:15:56Z","receivedAt":"2006-02-17T08:15:56Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Shawn Pearce <spearce@spearce.org> writes:\n\n> When git-reset --hard is used and a subdirectory becomes\n> empty (as it contains no tracked files in the target tree)\n> the empty subdirectory should be removed.\n\nI thought I said it would be a few-liner, but it appears I did\nnot send that message.\n\nThis untested one is far simpler, if less efficient, isn't it?\n\n---\n\ndiff --git a/git-reset.sh b/git-reset.sh\nindex fe53fc8..195d043 100755\n--- a/git-reset.sh\n+++ b/git-reset.sh\n@@ -88,6 +88,9 @@ case \"$reset_type\" in\n \t\t\t\t# it is ok if this fails -- it may already\n \t\t\t\t# have been culled by checkout-index.\n \t\t\t\tunlink $_;\n+\t\t\t\twhile (s|/[^/]*$|| && $_ ne \"\") {\n+\t\t\t\t\trmdir($_) or last;\n+\t\t\t\t}\n \t\t\t}\n \t\t}\n \t' $tmp-exists\n"},{"id":"16325","messageId":"20060217151654.GA26362@spearce.org","threadId":"3363","inReplyTo":"7v7j7u8koz.fsf@assigned-by-dhcp.cox.net","subject":"Re: [PATCH] Make git-reset delete empty directories","fromName":"Shawn Pearce","fromEmail":"spearce@spearce.org","sentAt":"2006-02-17T15:16:54Z","receivedAt":"2006-02-17T15:16:54Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Junio C Hamano <junkio@cox.net> wrote:\n> I thought I said it would be a few-liner, but it appears I did\n> not send that message.\n\nYou did.  Somehow I forgot that request.  I wrote a simple approach\nsimilar to what you show below then tried to make it slightly\nmore efficient by only attempting to delete each directory once.\nObviously that wasn't wise on my part.  :-)\n \n> This untested one is far simpler, if less efficient, isn't it?\n> \n> ---\n> \n> diff --git a/git-reset.sh b/git-reset.sh\n> index fe53fc8..195d043 100755\n> --- a/git-reset.sh\n> +++ b/git-reset.sh\n> @@ -88,6 +88,9 @@ case \"$reset_type\" in\n>  \t\t\t\t# it is ok if this fails -- it may already\n>  \t\t\t\t# have been culled by checkout-index.\n>  \t\t\t\tunlink $_;\n> +\t\t\t\twhile (s|/[^/]*$|| && $_ ne \"\") {\n> +\t\t\t\t\trmdir($_) or last;\n> +\t\t\t\t}\n>  \t\t\t}\n>  \t\t}\n>  \t' $tmp-exists\n\nI just ran it through the test script (t7101-reset.sh) that I\nincluded in my patch and it passed, so clearly the version above\nwould be the better version to include.\n\nSince it was untested before you are welcome to include the test\nscript with your version.  :-)\n\n-- \nShawn.\n"},{"id":"16329","messageId":"20060217160112.GB26362@spearce.org","threadId":"3363","inReplyTo":"20060217151654.GA26362@spearce.org","subject":"Re: [PATCH] Make git-reset delete empty directories","fromName":"Shawn Pearce","fromEmail":"spearce@spearce.org","sentAt":"2006-02-17T16:01:12Z","receivedAt":"2006-02-17T16:01:12Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Shawn Pearce <spearce@spearce.org> wrote:\n> Junio C Hamano <junkio@cox.net> wrote:\n> > I thought I said it would be a few-liner, but it appears I did\n> > not send that message.\n\nActually its only a one line change if you accept Perl as Perl,\nbut not everyone likes having their loops do nothing in the loop\nbody whilest the condition does all of the work.\n\n---\ndiff --git a/git-reset.sh b/git-reset.sh\nindex fe53fc8..e6471c0 100755\n--- a/git-reset.sh\n+++ b/git-reset.sh\n@@ -88,6 +88,7 @@ case \"$reset_type\" in\n                                # it is ok if this fails -- it may already\n                                # have been culled by checkout-index.\n                                unlink $_;\n+                               1 while (s,/[^/]*$,, && rmdir $_);\n                        }\n                }\n        ' $tmp-exists\n"}]}