refs: add function to repack without multiple refs

Generalize repack_without_ref as repack_without_refs to support a list of refs and implement the former in terms of the latter. Signed-off-by: Brad King <brad.king@kitware.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brad King committed Sep 4, 2013 at 11:22 UTC 61cee0dbac84160ad4a1bcc2ecdb15761bd284fc
1 file changed +24 -9
refs.c
+24 -9
@@ -2414,42 +2414,57 @@ static int curate_packed_ref_fn(struct ref_entry *entry, void *cb_data)
2414 return 0;
2415 }
2416
2417 -static int repack_without_ref(const char *refname)
2417 +static int repack_without_refs(const char **refnames, int n)
2418 {
2419 struct ref_dir *packed;
2420 struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
2421 struct string_list_item *ref_to_delete;
2422 + int i, removed = 0;
2423 +
2424 + /* Look for a packed ref */
2425 + for (i = 0; i < n; i++)
2426 + if (get_packed_ref(refnames[i]))
2427 + break;
2428
2423 - if (!get_packed_ref(refname))
2424 - return 0; /* refname does not exist in packed refs */
2429 + /* Avoid locking if we have nothing to do */
2430 + if (i == n)
2431 + return 0; /* no refname exists in packed refs */
2432
2433 if (lock_packed_refs(0)) {
2434 unable_to_lock_error(git_path("packed-refs"), errno);
2428 - return error("cannot delete '%s' from packed refs", refname);
2435 + return error("cannot delete '%s' from packed refs", refnames[i]);
2436 }
2437 packed = get_packed_refs(&ref_cache);
2438
2432 - /* Remove refname from the cache: */
2433 - if (remove_entry(packed, refname) == -1) {
2439 + /* Remove refnames from the cache */
2440 + for (i = 0; i < n; i++)
2441 + if (remove_entry(packed, refnames[i]) != -1)
2442 + removed = 1;
2443 + if (!removed) {
2444 /*
2435 - * The packed entry disappeared while we were
2445 + * All packed entries disappeared while we were
2446 * acquiring the lock.
2447 */
2448 rollback_packed_refs();
2449 return 0;
2450 }
2451
2442 - /* Remove any other accumulated cruft: */
2452 + /* Remove any other accumulated cruft */
2453 do_for_each_entry_in_dir(packed, 0, curate_packed_ref_fn, &refs_to_delete);
2454 for_each_string_list_item(ref_to_delete, &refs_to_delete) {
2455 if (remove_entry(packed, ref_to_delete->string) == -1)
2456 die("internal error");
2457 }
2458
2449 - /* Write what remains: */
2459 + /* Write what remains */
2460 return commit_packed_refs();
2461 }
2462
2463 +static int repack_without_ref(const char *refname)
2464 +{
2465 + return repack_without_refs(&refname, 1);
2466 +}
2467 +
2468 static int delete_ref_loose(struct ref_lock *lock, int flag)
2469 {
2470 if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {