Skip to content

Commit 8e34800

Browse files
rsahlberggitster
authored andcommitted
refs.c: change ref_transaction_update() to do error checking and return status
Update ref_transaction_update() do some basic error checking and return non-zero on error. Update all callers to check ref_transaction_update() for error. There are currently no conditions in _update that will return error but there will be in the future. Add an err argument that will be updated on failure. In future patches we will start doing both locking and checking for name conflicts in _update instead of _commit at which time this function will start returning errors for these conditions. Also check for BUGs during update and die(BUG:...) if we are calling _update with have_old but the old_sha1 pointer is NULL. Reviewed-by: Jonathan Nieder <jrnieder@gmail.com> 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>
1 parent 0131983 commit 8e34800

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

builtin/update-ref.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ static struct ref_transaction *transaction;
1616

1717
static char line_termination = '\n';
1818
static int update_flags;
19+
static struct strbuf err = STRBUF_INIT;
1920

2021
/*
2122
* Parse one whitespace- or NUL-terminated, possibly C-quoted argument
@@ -197,8 +198,9 @@ static const char *parse_cmd_update(struct strbuf *input, const char *next)
197198
if (*next != line_termination)
198199
die("update %s: extra input: %s", refname, next);
199200

200-
ref_transaction_update(transaction, refname, new_sha1, old_sha1,
201-
update_flags, have_old);
201+
if (ref_transaction_update(transaction, refname, new_sha1, old_sha1,
202+
update_flags, have_old, &err))
203+
die("%s", err.buf);
202204

203205
update_flags = 0;
204206
free(refname);
@@ -286,8 +288,9 @@ static const char *parse_cmd_verify(struct strbuf *input, const char *next)
286288
if (*next != line_termination)
287289
die("verify %s: extra input: %s", refname, next);
288290

289-
ref_transaction_update(transaction, refname, new_sha1, old_sha1,
290-
update_flags, have_old);
291+
if (ref_transaction_update(transaction, refname, new_sha1, old_sha1,
292+
update_flags, have_old, &err))
293+
die("%s", err.buf);
291294

292295
update_flags = 0;
293296
free(refname);
@@ -342,7 +345,6 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
342345
const char *refname, *oldval, *msg = NULL;
343346
unsigned char sha1[20], oldsha1[20];
344347
int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0, flags = 0;
345-
struct strbuf err = STRBUF_INIT;
346348
struct option options[] = {
347349
OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")),
348350
OPT_BOOL('d', NULL, &delete, N_("delete the reference")),

refs.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3428,19 +3428,25 @@ static struct ref_update *add_update(struct ref_transaction *transaction,
34283428
return update;
34293429
}
34303430

3431-
void ref_transaction_update(struct ref_transaction *transaction,
3432-
const char *refname,
3433-
const unsigned char *new_sha1,
3434-
const unsigned char *old_sha1,
3435-
int flags, int have_old)
3431+
int ref_transaction_update(struct ref_transaction *transaction,
3432+
const char *refname,
3433+
const unsigned char *new_sha1,
3434+
const unsigned char *old_sha1,
3435+
int flags, int have_old,
3436+
struct strbuf *err)
34363437
{
3437-
struct ref_update *update = add_update(transaction, refname);
3438+
struct ref_update *update;
3439+
3440+
if (have_old && !old_sha1)
3441+
die("BUG: have_old is true but old_sha1 is NULL");
34383442

3443+
update = add_update(transaction, refname);
34393444
hashcpy(update->new_sha1, new_sha1);
34403445
update->flags = flags;
34413446
update->have_old = have_old;
34423447
if (have_old)
34433448
hashcpy(update->old_sha1, old_sha1);
3449+
return 0;
34443450
}
34453451

34463452
void ref_transaction_create(struct ref_transaction *transaction,

refs.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,16 @@ struct ref_transaction *ref_transaction_begin(void);
246246
* be deleted. If have_old is true, then old_sha1 holds the value
247247
* that the reference should have had before the update, or zeros if
248248
* it must not have existed beforehand.
249+
* Function returns 0 on success and non-zero on failure. A failure to update
250+
* means that the transaction as a whole has failed and will need to be
251+
* rolled back. On failure the err buffer will be updated.
249252
*/
250-
void ref_transaction_update(struct ref_transaction *transaction,
251-
const char *refname,
252-
const unsigned char *new_sha1,
253-
const unsigned char *old_sha1,
254-
int flags, int have_old);
253+
int ref_transaction_update(struct ref_transaction *transaction,
254+
const char *refname,
255+
const unsigned char *new_sha1,
256+
const unsigned char *old_sha1,
257+
int flags, int have_old,
258+
struct strbuf *err);
255259

256260
/*
257261
* Add a reference creation to transaction. new_sha1 is the value

0 commit comments

Comments
 (0)