branch.c: use ref transaction for all ref updates

Change create_branch to use a ref transaction when creating the new branch. This also fixes a race condition in the old code where two concurrent create_branch could race since the lock_any_ref_for_update/write_ref_sha1 did not protect against the ref already existing. I.e. one thread could end up overwriting a branch even if the forcing flag is false. Signed-off-by: Ronnie Sahlberg <sahlberg@google.com> Reviewed-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ronnie Sahlberg committed Apr 16, 2014 at 16:21 UTC d43f990fac72264c1553beb7dbc6e8e795083d42
1 file changed +17 -14
branch.c
+17 -14
@@ -226,7 +226,6 @@ void create_branch(const char *head,
226 int force, int reflog, int clobber_head,
227 int quiet, enum branch_track track)
228 {
229 - struct ref_lock *lock = NULL;
229 struct commit *commit;
230 unsigned char sha1[20];
231 char *real_ref, msg[PATH_MAX + 20];
@@ -285,15 +284,6 @@ void create_branch(const char *head,
284 die(_("Not a valid branch point: '%s'."), start_name);
285 hashcpy(sha1, commit->object.sha1);
286
288 - if (!dont_change_ref) {
289 - lock = lock_any_ref_for_update(ref.buf, NULL, 0, NULL);
290 - if (!lock)
291 - die_errno(_("Failed to lock ref for update"));
292 - }
293 -
294 - if (reflog)
295 - log_all_ref_updates = 1;
296 -
287 if (forcing)
288 snprintf(msg, sizeof msg, "branch: Reset to %s",
289 start_name);
@@ -301,13 +291,26 @@ void create_branch(const char *head,
291 snprintf(msg, sizeof msg, "branch: Created from %s",
292 start_name);
293
294 + if (reflog)
295 + log_all_ref_updates = 1;
296 +
297 + if (!dont_change_ref) {
298 + struct ref_transaction *transaction;
299 + struct strbuf err = STRBUF_INIT;
300 +
301 + transaction = ref_transaction_begin(&err);
302 + if (!transaction ||
303 + ref_transaction_update(transaction, ref.buf, sha1,
304 + null_sha1, 0, !forcing, &err) ||
305 + ref_transaction_commit(transaction, msg, &err))
306 + die("%s", err.buf);
307 + ref_transaction_free(transaction);
308 + strbuf_release(&err);
309 + }
310 +
311 if (real_ref && track)
312 setup_tracking(ref.buf + 11, real_ref, track, quiet);
313
307 - if (!dont_change_ref)
308 - if (write_ref_sha1(lock, sha1, msg) < 0)
309 - die_errno(_("Failed to write ref"));
310 -
314 strbuf_release(&ref);
315 free(real_ref);
316 }