Skip to content

Commit af6c6e0

Browse files
committed
Merge branch 'gp/pack-refs-remove-empty-dirs' into maint
* gp/pack-refs-remove-empty-dirs: pack-refs: remove newly empty directories
2 parents c7f649a + be7c6d4 commit af6c6e0

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

pack-refs.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,37 @@ static int handle_one_ref(const char *path, const unsigned char *sha1,
6060
return 0;
6161
}
6262

63+
/*
64+
* Remove empty parents, but spare refs/ and immediate subdirs.
65+
* Note: munges *name.
66+
*/
67+
static void try_remove_empty_parents(char *name)
68+
{
69+
char *p, *q;
70+
int i;
71+
p = name;
72+
for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */
73+
while (*p && *p != '/')
74+
p++;
75+
/* tolerate duplicate slashes; see check_ref_format() */
76+
while (*p == '/')
77+
p++;
78+
}
79+
for (q = p; *q; q++)
80+
;
81+
while (1) {
82+
while (q > p && *q != '/')
83+
q--;
84+
while (q > p && *(q-1) == '/')
85+
q--;
86+
if (q == p)
87+
break;
88+
*q = '\0';
89+
if (rmdir(git_path("%s", name)))
90+
break;
91+
}
92+
}
93+
6394
/* make sure nobody touched the ref, and unlink */
6495
static void prune_ref(struct ref_to_prune *r)
6596
{
@@ -68,6 +99,7 @@ static void prune_ref(struct ref_to_prune *r)
6899
if (lock) {
69100
unlink_or_warn(git_path("%s", r->name));
70101
unlock_ref(lock);
102+
try_remove_empty_parents(r->name);
71103
}
72104
}
73105

t/t3210-pack-refs.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ test_expect_success 'see if git pack-refs --prune remove ref files' '
6060
! test -f .git/refs/heads/f
6161
'
6262

63+
test_expect_success 'see if git pack-refs --prune removes empty dirs' '
64+
git branch r/s/t &&
65+
git pack-refs --all --prune &&
66+
! test -e .git/refs/heads/r
67+
'
68+
6369
test_expect_success \
6470
'git branch g should work when git branch g/h has been deleted' \
6571
'git branch g/h &&

0 commit comments

Comments
 (0)