]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Merge branch 'gp/pack-refs-remove-empty-dirs' into maint
authorJunio C Hamano <gitster@pobox.com>
Wed, 1 Sep 2010 21:05:05 +0000 (14:05 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 1 Sep 2010 21:05:05 +0000 (14:05 -0700)
* gp/pack-refs-remove-empty-dirs:
  pack-refs: remove newly empty directories

pack-refs.c
t/t3210-pack-refs.sh

index 7f43f8ac3398fb2dbe393f08811989b0a8d4e2dd..1290570260d184b9c94fdab62650d6e474b365b4 100644 (file)
@@ -60,6 +60,37 @@ static int handle_one_ref(const char *path, const unsigned char *sha1,
        return 0;
 }
 
+/*
+ * Remove empty parents, but spare refs/ and immediate subdirs.
+ * Note: munges *name.
+ */
+static void try_remove_empty_parents(char *name)
+{
+       char *p, *q;
+       int i;
+       p = name;
+       for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */
+               while (*p && *p != '/')
+                       p++;
+               /* tolerate duplicate slashes; see check_ref_format() */
+               while (*p == '/')
+                       p++;
+       }
+       for (q = p; *q; q++)
+               ;
+       while (1) {
+               while (q > p && *q != '/')
+                       q--;
+               while (q > p && *(q-1) == '/')
+                       q--;
+               if (q == p)
+                       break;
+               *q = '\0';
+               if (rmdir(git_path("%s", name)))
+                       break;
+       }
+}
+
 /* make sure nobody touched the ref, and unlink */
 static void prune_ref(struct ref_to_prune *r)
 {
@@ -68,6 +99,7 @@ static void prune_ref(struct ref_to_prune *r)
        if (lock) {
                unlink_or_warn(git_path("%s", r->name));
                unlock_ref(lock);
+               try_remove_empty_parents(r->name);
        }
 }
 
index 525174013c4c33eab5bdbde8831d43f1ddbaeaae..cd04361df811d329e269ac1d5eb0b0f03baa74d8 100755 (executable)
@@ -60,6 +60,12 @@ test_expect_success 'see if git pack-refs --prune remove ref files' '
      ! test -f .git/refs/heads/f
 '
 
+test_expect_success 'see if git pack-refs --prune removes empty dirs' '
+     git branch r/s/t &&
+     git pack-refs --all --prune &&
+     ! test -e .git/refs/heads/r
+'
+
 test_expect_success \
     'git branch g should work when git branch g/h has been deleted' \
     'git branch g/h &&