Skip to content

Commit 4be49d7

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 396da8f commit 4be49d7

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

builtin/checkout.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
661661
describe_detached_head(_("HEAD is now at"), new->commit);
662662
}
663663
} 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");
665666
if (!opts->quiet) {
666667
if (old->path && !strcmp(new->path, old->path)) {
667668
if (opts->new_branch_force)

builtin/clone.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -636,9 +636,11 @@ static void update_remote_refs(const struct ref *refs,
636636
struct strbuf head_ref = STRBUF_INIT;
637637
strbuf_addstr(&head_ref, branch_top);
638638
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);
642644
}
643645
}
644646

@@ -648,7 +650,8 @@ static void update_head(const struct ref *our, const struct ref *remote,
648650
const char *head;
649651
if (our && skip_prefix(our->name, "refs/heads/", &head)) {
650652
/* 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");
652655
if (!option_bare) {
653656
update_ref(msg, "HEAD", our->old_oid.hash, NULL, 0,
654657
UPDATE_REFS_DIE_ON_ERR);

t/t2011-checkout-invalid-head.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@ test_expect_success 'checkout master from invalid HEAD' '
1919
git checkout master --
2020
'
2121

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+
2228
test_done

0 commit comments

Comments
 (0)