refs.c: add an err argument to repack_without_refs

Update repack_without_refs to take an err argument and update it if there is a failure. Pass the err variable from ref_transaction_commit to this function so that callers can print a meaningful error message if _commit fails due to this function. Signed-off-by: Ronnie Sahlberg <sahlberg@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> Acked-by: Michael Haggerty <mhagger@alum.mit.edu>

Ronnie Sahlberg committed Jun 20, 2014 at 07:42 UTC 60bca085c893f1866829b55bfee63888943cfe14
3 files changed +19 -8
builtin/remote.c
+3 -2
@@ -755,7 +755,7 @@ static int remove_branches(struct string_list *branches)
755 branch_names = xmalloc(branches->nr * sizeof(*branch_names));
756 for (i = 0; i < branches->nr; i++)
757 branch_names[i] = branches->items[i].string;
758 - result |= repack_without_refs(branch_names, branches->nr);
758 + result |= repack_without_refs(branch_names, branches->nr, NULL);
759 free(branch_names);
760
761 for (i = 0; i < branches->nr; i++) {
@@ -1333,7 +1333,8 @@ static int prune_remote(const char *remote, int dry_run)
1333 for (i = 0; i < states.stale.nr; i++)
1334 delete_refs[i] = states.stale.items[i].util;
1335 if (!dry_run)
1336 - result |= repack_without_refs(delete_refs, states.stale.nr);
1336 + result |= repack_without_refs(delete_refs,
1337 + states.stale.nr, NULL);
1338 free(delete_refs);
1339 }
1340
refs.c
+14 -5
@@ -2456,12 +2456,12 @@ static int curate_packed_ref_fn(struct ref_entry *entry, void *cb_data)
2456 return 0;
2457 }
2458
2459 -int repack_without_refs(const char **refnames, int n)
2459 +int repack_without_refs(const char **refnames, int n, struct strbuf *err)
2460 {
2461 struct ref_dir *packed;
2462 struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
2463 struct string_list_item *ref_to_delete;
2464 - int i, removed = 0;
2464 + int i, ret, removed = 0;
2465
2466 /* Look for a packed ref */
2467 for (i = 0; i < n; i++)
@@ -2473,6 +2473,11 @@ int repack_without_refs(const char **refnames, int n)
2473 return 0; /* no refname exists in packed refs */
2474
2475 if (lock_packed_refs(0)) {
2476 + if (err) {
2477 + unable_to_lock_message(git_path("packed-refs"), errno,
2478 + err);
2479 + return -1;
2480 + }
2481 unable_to_lock_error(git_path("packed-refs"), errno);
2482 return error("cannot delete '%s' from packed refs", refnames[i]);
2483 }
@@ -2499,12 +2504,16 @@ int repack_without_refs(const char **refnames, int n)
2504 }
2505
2506 /* Write what remains */
2502 - return commit_packed_refs();
2507 + ret = commit_packed_refs();
2508 + if (ret && err)
2509 + strbuf_addf(err, "unable to overwrite old ref-pack file: %s",
2510 + strerror(errno));
2511 + return ret;
2512 }
2513
2514 static int repack_without_ref(const char *refname)
2515 {
2507 - return repack_without_refs(&refname, 1);
2516 + return repack_without_refs(&refname, 1, NULL);
2517 }
2518
2519 static int delete_ref_loose(struct ref_lock *lock, int flag)
@@ -3508,7 +3517,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,
3517 }
3518 }
3519
3511 - ret |= repack_without_refs(delnames, delnum);
3520 + ret |= repack_without_refs(delnames, delnum, err);
3521 for (i = 0; i < delnum; i++)
3522 unlink_or_warn(git_path("logs/%s", delnames[i]));
3523 clear_loose_ref_cache(&ref_cache);
refs.h
+2 -1
@@ -122,7 +122,8 @@ extern void rollback_packed_refs(void);
122 */
123 int pack_refs(unsigned int flags);
124
125 -extern int repack_without_refs(const char **refnames, int n);
125 +extern int repack_without_refs(const char **refnames, int n,
126 + struct strbuf *err);
127
128 extern int ref_exists(const char *);
129