checkout,clone: check return value of create_symref
authorJeff King <peff@peff.net>
Tue, 12 Jan 2016 09:57:34 +0000 (04:57 -0500)
committerJunio C Hamano <gitster@pobox.com>
Tue, 12 Jan 2016 19:11:52 +0000 (11:11 -0800)
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>
builtin/checkout.c
builtin/clone.c
t/t2011-checkout-invalid-head.sh
index e8110a9243f648129ffbea0de2af039ad7791aa1..5af84a3118d20da437d3e08c1667f76dc2f57845 100644 (file)
@@ -661,7 +661,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
                        describe_detached_head(_("HEAD is now at"), new->commit);
                }
        } else if (new->path) { /* Switch branches. */
-               create_symref("HEAD", new->path, msg.buf);
+               if (create_symref("HEAD", new->path, msg.buf) < 0)
+                       die("unable to update HEAD");
                if (!opts->quiet) {
                        if (old->path && !strcmp(new->path, old->path)) {
                                if (opts->new_branch_force)
index a0b3cd9e5617b5a3a45dd01c8a2c7af0b2fd9668..a7c8def8cb7199e82acad9e94bb01d93ba4ef0fb 100644 (file)
@@ -636,9 +636,11 @@ static void update_remote_refs(const struct ref *refs,
                struct strbuf head_ref = STRBUF_INIT;
                strbuf_addstr(&head_ref, branch_top);
                strbuf_addstr(&head_ref, "HEAD");
-               create_symref(head_ref.buf,
-                             remote_head_points_at->peer_ref->name,
-                             msg);
+               if (create_symref(head_ref.buf,
+                                 remote_head_points_at->peer_ref->name,
+                                 msg) < 0)
+                       die("unable to update %s", head_ref.buf);
+               strbuf_release(&head_ref);
        }
 }
 
@@ -648,7 +650,8 @@ static void update_head(const struct ref *our, const struct ref *remote,
        const char *head;
        if (our && skip_prefix(our->name, "refs/heads/", &head)) {
                /* Local default branch link */
-               create_symref("HEAD", our->name, NULL);
+               if (create_symref("HEAD", our->name, NULL) < 0)
+                       die("unable to update HEAD");
                if (!option_bare) {
                        update_ref(msg, "HEAD", our->old_oid.hash, NULL, 0,
                                   UPDATE_REFS_DIE_ON_ERR);
index 300f8bf25c34cf4ea4e011d1daa525285ca94c5a..d444d5ee417220e8428543a6c76d5839ce91b979 100755 (executable)
@@ -19,4 +19,10 @@ test_expect_success 'checkout master from invalid HEAD' '
        git checkout master --
 '
 
+test_expect_success 'checkout notices failure to lock HEAD' '
+       test_when_finished "rm -f .git/HEAD.lock" &&
+       >.git/HEAD.lock &&
+       test_must_fail git checkout -b other
+'
+
 test_done