Skip to content

Commit d43f990

Browse files
rsahlberggitster
authored andcommitted
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>
1 parent de7e86f commit d43f990

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

branch.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ void create_branch(const char *head,
226226
int force, int reflog, int clobber_head,
227227
int quiet, enum branch_track track)
228228
{
229-
struct ref_lock *lock = NULL;
230229
struct commit *commit;
231230
unsigned char sha1[20];
232231
char *real_ref, msg[PATH_MAX + 20];
@@ -285,29 +284,33 @@ void create_branch(const char *head,
285284
die(_("Not a valid branch point: '%s'."), start_name);
286285
hashcpy(sha1, commit->object.sha1);
287286

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-
297287
if (forcing)
298288
snprintf(msg, sizeof msg, "branch: Reset to %s",
299289
start_name);
300290
else if (!dont_change_ref)
301291
snprintf(msg, sizeof msg, "branch: Created from %s",
302292
start_name);
303293

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+
304311
if (real_ref && track)
305312
setup_tracking(ref.buf + 11, real_ref, track, quiet);
306313

307-
if (!dont_change_ref)
308-
if (write_ref_sha1(lock, sha1, msg) < 0)
309-
die_errno(_("Failed to write ref"));
310-
311314
strbuf_release(&ref);
312315
free(real_ref);
313316
}

0 commit comments

Comments
 (0)