Skip to content

Commit a8f0db2

Browse files
mhaggergitster
authored andcommitted
try_remove_empty_parents(): teach to remove parents of reflogs, too
Add a new "flags" parameter that tells the function whether to remove empty parent directories of the loose reference file, of the reflog file, or both. The new functionality is not yet used. Signed-off-by: Michael Haggerty <[email protected]> Reviewed-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8bdaecb commit a8f0db2

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

refs/files-backend.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,10 +2280,18 @@ static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)
22802280
return 0;
22812281
}
22822282

2283+
enum {
2284+
REMOVE_EMPTY_PARENTS_REF = 0x01,
2285+
REMOVE_EMPTY_PARENTS_REFLOG = 0x02
2286+
};
2287+
22832288
/*
2284-
* Remove empty parents, but spare refs/ and immediate subdirs.
2289+
* Remove empty parent directories associated with the specified
2290+
* reference and/or its reflog, but spare [logs/]refs/ and immediate
2291+
* subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or
2292+
* REMOVE_EMPTY_PARENTS_REFLOG.
22852293
*/
2286-
static void try_remove_empty_parents(const char *refname)
2294+
static void try_remove_empty_parents(const char *refname, unsigned int flags)
22872295
{
22882296
struct strbuf buf = STRBUF_INIT;
22892297
char *p, *q;
@@ -2299,16 +2307,20 @@ static void try_remove_empty_parents(const char *refname)
22992307
p++;
23002308
}
23012309
q = buf.buf + buf.len;
2302-
while (1) {
2310+
while (flags & (REMOVE_EMPTY_PARENTS_REF | REMOVE_EMPTY_PARENTS_REFLOG)) {
23032311
while (q > p && *q != '/')
23042312
q--;
23052313
while (q > p && *(q-1) == '/')
23062314
q--;
23072315
if (q == p)
23082316
break;
23092317
strbuf_setlen(&buf, q - buf.buf);
2310-
if (rmdir(git_path("%s", buf.buf)))
2311-
break;
2318+
if ((flags & REMOVE_EMPTY_PARENTS_REF) &&
2319+
rmdir(git_path("%s", buf.buf)))
2320+
flags &= ~REMOVE_EMPTY_PARENTS_REF;
2321+
if ((flags & REMOVE_EMPTY_PARENTS_REFLOG) &&
2322+
rmdir(git_path("logs/%s", buf.buf)))
2323+
flags &= ~REMOVE_EMPTY_PARENTS_REFLOG;
23122324
}
23132325
strbuf_release(&buf);
23142326
}
@@ -2334,7 +2346,7 @@ static void prune_ref(struct ref_to_prune *r)
23342346
}
23352347
ref_transaction_free(transaction);
23362348
strbuf_release(&err);
2337-
try_remove_empty_parents(r->name);
2349+
try_remove_empty_parents(r->name, REMOVE_EMPTY_PARENTS_REF);
23382350
}
23392351

23402352
static void prune_refs(struct ref_to_prune *r)

0 commit comments

Comments
 (0)