Skip to content

Commit 5a603b0

Browse files
jrngitster
authored andcommitted
refs.c: do not permit err == NULL
Some functions that take a strbuf argument to append an error treat !err as an indication that the message should be suppressed (e.g., ref_update_reject_duplicates). Others write the message to stderr on !err (e.g., repack_without_refs). Others crash (e.g., ref_transaction_update). Some of these behaviors are for historical reasons and others were accidents. Luckily no callers pass err == NULL any more. Simplify by consistently requiring the strbuf argument. Signed-off-by: Jonathan Nieder <[email protected]> Reviewed-by: Ronnie Sahlberg <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2ebb49c commit 5a603b0

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

refs.c

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2646,6 +2646,8 @@ int repack_without_refs(const char **refnames, int n, struct strbuf *err)
26462646
struct string_list_item *ref_to_delete;
26472647
int i, ret, removed = 0;
26482648

2649+
assert(err);
2650+
26492651
/* Look for a packed ref */
26502652
for (i = 0; i < n; i++)
26512653
if (get_packed_ref(refnames[i]))
@@ -2656,13 +2658,8 @@ int repack_without_refs(const char **refnames, int n, struct strbuf *err)
26562658
return 0; /* no refname exists in packed refs */
26572659

26582660
if (lock_packed_refs(0)) {
2659-
if (err) {
2660-
unable_to_lock_message(git_path("packed-refs"), errno,
2661-
err);
2662-
return -1;
2663-
}
2664-
unable_to_lock_error(git_path("packed-refs"), errno);
2665-
return error("cannot delete '%s' from packed refs", refnames[i]);
2661+
unable_to_lock_message(git_path("packed-refs"), errno, err);
2662+
return -1;
26662663
}
26672664
packed = get_packed_refs(&ref_cache);
26682665

@@ -2688,14 +2685,16 @@ int repack_without_refs(const char **refnames, int n, struct strbuf *err)
26882685

26892686
/* Write what remains */
26902687
ret = commit_packed_refs();
2691-
if (ret && err)
2688+
if (ret)
26922689
strbuf_addf(err, "unable to overwrite old ref-pack file: %s",
26932690
strerror(errno));
26942691
return ret;
26952692
}
26962693

26972694
static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)
26982695
{
2696+
assert(err);
2697+
26992698
if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {
27002699
/*
27012700
* loose. The loose file name is the same as the
@@ -3551,6 +3550,8 @@ struct ref_transaction {
35513550

35523551
struct ref_transaction *ref_transaction_begin(struct strbuf *err)
35533552
{
3553+
assert(err);
3554+
35543555
return xcalloc(1, sizeof(struct ref_transaction));
35553556
}
35563557

@@ -3590,6 +3591,8 @@ int ref_transaction_update(struct ref_transaction *transaction,
35903591
{
35913592
struct ref_update *update;
35923593

3594+
assert(err);
3595+
35933596
if (transaction->state != REF_TRANSACTION_OPEN)
35943597
die("BUG: update called for transaction that is not open");
35953598

@@ -3622,6 +3625,8 @@ int ref_transaction_create(struct ref_transaction *transaction,
36223625
{
36233626
struct ref_update *update;
36243627

3628+
assert(err);
3629+
36253630
if (transaction->state != REF_TRANSACTION_OPEN)
36263631
die("BUG: create called for transaction that is not open");
36273632

@@ -3653,6 +3658,8 @@ int ref_transaction_delete(struct ref_transaction *transaction,
36533658
{
36543659
struct ref_update *update;
36553660

3661+
assert(err);
3662+
36563663
if (transaction->state != REF_TRANSACTION_OPEN)
36573664
die("BUG: delete called for transaction that is not open");
36583665

@@ -3715,13 +3722,14 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,
37153722
struct strbuf *err)
37163723
{
37173724
int i;
3725+
3726+
assert(err);
3727+
37183728
for (i = 1; i < n; i++)
37193729
if (!strcmp(updates[i - 1]->refname, updates[i]->refname)) {
3720-
const char *str =
3721-
"Multiple updates for ref '%s' not allowed.";
3722-
if (err)
3723-
strbuf_addf(err, str, updates[i]->refname);
3724-
3730+
strbuf_addf(err,
3731+
"Multiple updates for ref '%s' not allowed.",
3732+
updates[i]->refname);
37253733
return 1;
37263734
}
37273735
return 0;
@@ -3735,6 +3743,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,
37353743
int n = transaction->nr;
37363744
struct ref_update **updates = transaction->updates;
37373745

3746+
assert(err);
3747+
37383748
if (transaction->state != REF_TRANSACTION_OPEN)
37393749
die("BUG: commit called for transaction that is not open");
37403750

@@ -3771,9 +3781,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,
37713781
ret = (errno == ENOTDIR)
37723782
? TRANSACTION_NAME_CONFLICT
37733783
: TRANSACTION_GENERIC_ERROR;
3774-
if (err)
3775-
strbuf_addf(err, "Cannot lock the ref '%s'.",
3776-
update->refname);
3784+
strbuf_addf(err, "Cannot lock the ref '%s'.",
3785+
update->refname);
37773786
goto cleanup;
37783787
}
37793788
}
@@ -3786,9 +3795,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,
37863795
if (write_ref_sha1(update->lock, update->new_sha1,
37873796
update->msg)) {
37883797
update->lock = NULL; /* freed by write_ref_sha1 */
3789-
if (err)
3790-
strbuf_addf(err, "Cannot update the ref '%s'.",
3791-
update->refname);
3798+
strbuf_addf(err, "Cannot update the ref '%s'.",
3799+
update->refname);
37923800
ret = TRANSACTION_GENERIC_ERROR;
37933801
goto cleanup;
37943802
}

0 commit comments

Comments
 (0)