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 <mhagger@alum.mit.edu> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Jan 6, 2017 at 17:22 UTC a8f0db2d99552d786fd1428a06d5fb8e0425dca8
1 file changed +18 -6
refs/files-backend.c
+18 -6
@@ -2280,10 +2280,18 @@ static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)
2280 return 0;
2281 }
2282
2283 +enum {
2284 + REMOVE_EMPTY_PARENTS_REF = 0x01,
2285 + REMOVE_EMPTY_PARENTS_REFLOG = 0x02
2286 +};
2287 +
2288 /*
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.
2293 */
2286 -static void try_remove_empty_parents(const char *refname)
2294 +static void try_remove_empty_parents(const char *refname, unsigned int flags)
2295 {
2296 struct strbuf buf = STRBUF_INIT;
2297 char *p, *q;
@@ -2299,7 +2307,7 @@ static void try_remove_empty_parents(const char *refname)
2307 p++;
2308 }
2309 q = buf.buf + buf.len;
2302 - while (1) {
2310 + while (flags & (REMOVE_EMPTY_PARENTS_REF | REMOVE_EMPTY_PARENTS_REFLOG)) {
2311 while (q > p && *q != '/')
2312 q--;
2313 while (q > p && *(q-1) == '/')
@@ -2307,8 +2315,12 @@ static void try_remove_empty_parents(const char *refname)
2315 if (q == p)
2316 break;
2317 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;
2324 }
2325 strbuf_release(&buf);
2326 }
@@ -2334,7 +2346,7 @@ static void prune_ref(struct ref_to_prune *r)
2346 }
2347 ref_transaction_free(transaction);
2348 strbuf_release(&err);
2337 - try_remove_empty_parents(r->name);
2349 + try_remove_empty_parents(r->name, REMOVE_EMPTY_PARENTS_REF);
2350 }
2351
2352 static void prune_refs(struct ref_to_prune *r)