Skip to content

Commit f8bd36a

Browse files
jrngitster
authored andcommitted
checkout: rearrange update_refs_for_switch for clarity
Take care of simple, exceptional cases before the meat of the "check out by branch name" code begins. After this change, the function vaguely follows the following pseudocode: if (-B or -b) create branch; if (plain "git checkout" or "git checkout HEAD") ; else if (--detach or checking out by non-branch commit name) detach HEAD; else if (checking out by branch name) attach HEAD; One nice side benefit is to make it possible to remove handling of the --detach option from outside switch_branches. Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3266967 commit f8bd36a

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

builtin/checkout.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,17 @@ static void update_refs_for_switch(struct checkout_opts *opts,
542542
strbuf_addf(&msg, "checkout: moving from %s to %s",
543543
old_desc ? old_desc : "(invalid)", new->name);
544544

545-
if (new->path) {
545+
if (!strcmp(new->name, "HEAD") && !new->path && !opts->force_detach) {
546+
/* Nothing to do. */
547+
} else if (opts->force_detach || !new->path) { /* No longer on any branch. */
548+
update_ref(msg.buf, "HEAD", new->commit->object.sha1, NULL,
549+
REF_NODEREF, DIE_ON_ERR);
550+
if (!opts->quiet) {
551+
if (old->path && advice_detached_head)
552+
detach_advice(old->path, new->name);
553+
describe_detached_head("HEAD is now at", new->commit);
554+
}
555+
} else if (new->path) { /* Switch branches. */
546556
create_symref("HEAD", new->path, msg.buf);
547557
if (!opts->quiet) {
548558
if (old->path && !strcmp(new->path, old->path))
@@ -564,14 +574,6 @@ static void update_refs_for_switch(struct checkout_opts *opts,
564574
if (!file_exists(ref_file) && file_exists(log_file))
565575
remove_path(log_file);
566576
}
567-
} else if (opts->force_detach || strcmp(new->name, "HEAD")) {
568-
update_ref(msg.buf, "HEAD", new->commit->object.sha1, NULL,
569-
REF_NODEREF, DIE_ON_ERR);
570-
if (!opts->quiet) {
571-
if (old->path && advice_detached_head)
572-
detach_advice(old->path, new->name);
573-
describe_detached_head("HEAD is now at", new->commit);
574-
}
575577
}
576578
remove_branch_state();
577579
strbuf_release(&msg);
@@ -679,7 +681,6 @@ static const char *unique_tracking_name(const char *name)
679681

680682
static int parse_branchname_arg(int argc, const char **argv,
681683
int dwim_new_local_branch_ok,
682-
int force_detach,
683684
struct branch_info *new,
684685
struct tree **source_tree,
685686
unsigned char rev[20],
@@ -756,8 +757,7 @@ static int parse_branchname_arg(int argc, const char **argv,
756757
new->name = arg;
757758
setup_branch_path(new);
758759

759-
if (!force_detach &&
760-
check_ref_format(new->path) == CHECK_REF_FORMAT_OK &&
760+
if (check_ref_format(new->path) == CHECK_REF_FORMAT_OK &&
761761
resolve_ref(new->path, branch_rev, 1, NULL))
762762
hashcpy(rev, branch_rev);
763763
else
@@ -906,8 +906,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
906906
dwim_new_local_branch &&
907907
opts.track == BRANCH_TRACK_UNSPECIFIED &&
908908
!opts.new_branch;
909-
int n = parse_branchname_arg(argc, argv,
910-
dwim_ok, opts.force_detach,
909+
int n = parse_branchname_arg(argc, argv, dwim_ok,
911910
&new, &source_tree, rev, &opts.new_branch);
912911
argv += n;
913912
argc -= n;

0 commit comments

Comments
 (0)