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>

Ronnie Sahlberg committed Jun 20, 2014 at 07:43 UTC 8e34800e5bce1dc6f1ab5520a4c866a2a73639de
3 files changed +28 -16
builtin/update-ref.c
+7 -5
@@ -16,6 +16,7 @@ static struct ref_transaction *transaction;
16
17 static char line_termination = '\n';
18 static int update_flags;
19 +static struct strbuf err = STRBUF_INIT;
20
21 /*
22 * 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)
198 if (*next != line_termination)
199 die("update %s: extra input: %s", refname, next);
200
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);
204
205 update_flags = 0;
206 free(refname);
@@ -286,8 +288,9 @@ static const char *parse_cmd_verify(struct strbuf *input, const char *next)
288 if (*next != line_termination)
289 die("verify %s: extra input: %s", refname, next);
290
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);
294
295 update_flags = 0;
296 free(refname);
@@ -342,7 +345,6 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
345 const char *refname, *oldval, *msg = NULL;
346 unsigned char sha1[20], oldsha1[20];
347 int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0, flags = 0;
345 - struct strbuf err = STRBUF_INIT;
348 struct option options[] = {
349 OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")),
350 OPT_BOOL('d', NULL, &delete, N_("delete the reference")),
refs.c
+12 -6
@@ -3428,19 +3428,25 @@ static struct ref_update *add_update(struct ref_transaction *transaction,
3428 return update;
3429 }
3430
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)
3437 {
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");
3442
3443 + update = add_update(transaction, refname);
3444 hashcpy(update->new_sha1, new_sha1);
3445 update->flags = flags;
3446 update->have_old = have_old;
3447 if (have_old)
3448 hashcpy(update->old_sha1, old_sha1);
3449 + return 0;
3450 }
3451
3452 void ref_transaction_create(struct ref_transaction *transaction,
refs.h
+9 -5
@@ -246,12 +246,16 @@ struct ref_transaction *ref_transaction_begin(void);
246 * be deleted. If have_old is true, then old_sha1 holds the value
247 * that the reference should have had before the update, or zeros if
248 * 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.
252 */
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);
259
260 /*
261 * Add a reference creation to transaction. new_sha1 is the value