@matheusbsbs / matheus-bagliano / commits / f8bd36a433
Fork 0

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 <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Nieder committed Feb 8, 2011 at 04:34 UTC f8bd36a433768f1b11642faff5b88e3510aa8af8
1 file changed +13 -14
builtin/checkout.c
+13 -14
@@ -542,7 +542,17 @@ static void update_refs_for_switch(struct checkout_opts *opts,
542 strbuf_addf(&msg, "checkout: moving from %s to %s",
543 old_desc ? old_desc : "(invalid)", new->name);
544
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. */
556 create_symref("HEAD", new->path, msg.buf);
557 if (!opts->quiet) {
558 if (old->path && !strcmp(new->path, old->path))
@@ -564,14 +574,6 @@ static void update_refs_for_switch(struct checkout_opts *opts,
574 if (!file_exists(ref_file) && file_exists(log_file))
575 remove_path(log_file);
576 }
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 - }
577 }
578 remove_branch_state();
579 strbuf_release(&msg);
@@ -679,7 +681,6 @@ static const char *unique_tracking_name(const char *name)
681
682 static int parse_branchname_arg(int argc, const char **argv,
683 int dwim_new_local_branch_ok,
682 - int force_detach,
684 struct branch_info *new,
685 struct tree **source_tree,
686 unsigned char rev[20],
@@ -756,8 +757,7 @@ static int parse_branchname_arg(int argc, const char **argv,
757 new->name = arg;
758 setup_branch_path(new);
759
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 &&
761 resolve_ref(new->path, branch_rev, 1, NULL))
762 hashcpy(rev, branch_rev);
763 else
@@ -906,8 +906,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
906 dwim_new_local_branch &&
907 opts.track == BRANCH_TRACK_UNSPECIFIED &&
908 !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,
910 &new, &source_tree, rev, &opts.new_branch);
911 argv += n;
912 argc -= n;