checkout,clone: check return value of create_symref

It's unlikely that we would fail to create or update a symbolic ref (especially HEAD), but if we do, we should notice and complain. Note that there's no need to give more details in our error message; create_symref will already have done so. While we're here, let's also fix a minor memory leak in clone. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jan 12, 2016 at 04:57 UTC 4be49d756894daca0e8a4477d36c6ed1096ccddc
3 files changed +15 -5
builtin/checkout.c
+2 -1
@@ -661,7 +661,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
661 describe_detached_head(_("HEAD is now at"), new->commit);
662 }
663 } else if (new->path) { /* Switch branches. */
664 - create_symref("HEAD", new->path, msg.buf);
664 + if (create_symref("HEAD", new->path, msg.buf) < 0)
665 + die("unable to update HEAD");
666 if (!opts->quiet) {
667 if (old->path && !strcmp(new->path, old->path)) {
668 if (opts->new_branch_force)
builtin/clone.c
+7 -4
@@ -636,9 +636,11 @@ static void update_remote_refs(const struct ref *refs,
636 struct strbuf head_ref = STRBUF_INIT;
637 strbuf_addstr(&head_ref, branch_top);
638 strbuf_addstr(&head_ref, "HEAD");
639 - create_symref(head_ref.buf,
640 - remote_head_points_at->peer_ref->name,
641 - msg);
639 + if (create_symref(head_ref.buf,
640 + remote_head_points_at->peer_ref->name,
641 + msg) < 0)
642 + die("unable to update %s", head_ref.buf);
643 + strbuf_release(&head_ref);
644 }
645 }
646
@@ -648,7 +650,8 @@ static void update_head(const struct ref *our, const struct ref *remote,
650 const char *head;
651 if (our && skip_prefix(our->name, "refs/heads/", &head)) {
652 /* Local default branch link */
651 - create_symref("HEAD", our->name, NULL);
653 + if (create_symref("HEAD", our->name, NULL) < 0)
654 + die("unable to update HEAD");
655 if (!option_bare) {
656 update_ref(msg, "HEAD", our->old_oid.hash, NULL, 0,
657 UPDATE_REFS_DIE_ON_ERR);
t/t2011-checkout-invalid-head.sh
+6
@@ -19,4 +19,10 @@ test_expect_success 'checkout master from invalid HEAD' '
19 git checkout master --
20 '
21
22 +test_expect_success 'checkout notices failure to lock HEAD' '
23 + test_when_finished "rm -f .git/HEAD.lock" &&
24 + >.git/HEAD.lock &&
25 + test_must_fail git checkout -b other
26 +'
27 +
28 test_done