Merge branch 'ci/forbid-unwanted-current-branch-update'
authorJunio C Hamano <gitster@pobox.com>
Mon, 29 Aug 2011 04:19:31 +0000 (21:19 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 29 Aug 2011 04:19:31 +0000 (21:19 -0700)
* ci/forbid-unwanted-current-branch-update:
Show interpreted branch name in error messages
Prevent force-updating of the current branch

1  2 
branch.c
builtin/branch.c
builtin/checkout.c
diff --combined branch.c
index d62cc0132cbff6de04b69864279d6c92f9e95e0a,1fe3078a6cade3c34c5dcd65977b286840bec6ee..478d82567d7773d62c686d73780659b86594eede
+++ b/branch.c
@@@ -135,6 -135,26 +135,26 @@@ static int setup_tracking(const char *n
        return 0;
  }
  
+ int validate_new_branchname(const char *name, struct strbuf *ref, int force)
+ {
+       const char *head;
+       unsigned char sha1[20];
+       if (strbuf_check_branch_ref(ref, name))
+               die("'%s' is not a valid branch name.", name);
+       if (!ref_exists(ref->buf))
+               return 0;
+       else if (!force)
+               die("A branch named '%s' already exists.", ref->buf + strlen("refs/heads/"));
+       head = resolve_ref("HEAD", sha1, 0, NULL);
+       if (!is_bare_repository() && head && !strcmp(head, ref->buf))
+               die("Cannot force update the current branch.");
+       return 1;
+ }
  void create_branch(const char *head,
                   const char *name, const char *start_name,
                   int force, int reflog, enum branch_track track)
        if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
                explicit_tracking = 1;
  
-       if (strbuf_check_branch_ref(&ref, name))
-               die("'%s' is not a valid branch name.", name);
-       if (resolve_ref(ref.buf, sha1, 1, NULL)) {
-               if (!force && track == BRANCH_TRACK_OVERRIDE)
+       if (validate_new_branchname(name, &ref, force || track == BRANCH_TRACK_OVERRIDE)) {
+               if (!force)
                        dont_change_ref = 1;
-               else if (!force)
-                       die("A branch named '%s' already exists.", name);
-               else if (!is_bare_repository() && head && !strcmp(head, name))
-                       die("Cannot force update the current branch.");
-               forcing = 1;
+               else
+                       forcing = 1;
        }
  
        real_ref = NULL;
                         start_name);
  
        if (real_ref && track)
 -              setup_tracking(name, real_ref, track);
 +              setup_tracking(ref.buf+11, real_ref, track);
  
        if (!dont_change_ref)
                if (write_ref_sha1(lock, sha1, msg) < 0)
diff --combined builtin/branch.c
index f7da69c932a6e75f4fc37f063719a08f730f9974,40f885c9fe9598f8e7c25f14c6e5aa01b45d1c82..aa705a0fb0b7d4cdd0fb29fc4853b8c8c05c625d
@@@ -71,7 -71,7 +71,7 @@@ static int parse_branch_color_slot(cons
  static int git_branch_config(const char *var, const char *value, void *cb)
  {
        if (!strcmp(var, "color.branch")) {
 -              branch_use_color = git_config_colorbool(var, value, -1);
 +              branch_use_color = git_config_colorbool(var, value);
                return 0;
        }
        if (!prefixcmp(var, "color.branch.")) {
@@@ -88,7 -88,7 +88,7 @@@
  
  static const char *branch_get_color(enum color_branch ix)
  {
 -      if (branch_use_color > 0)
 +      if (want_color(branch_use_color))
                return branch_colors[ix];
        return "";
  }
@@@ -566,11 -566,7 +566,7 @@@ static void rename_branch(const char *o
                        die(_("Invalid branch name: '%s'"), oldname);
        }
  
-       if (strbuf_check_branch_ref(&newref, newname))
-               die(_("Invalid branch name: '%s'"), newname);
-       if (resolve_ref(newref.buf, sha1, 1, NULL) && !force)
-               die(_("A branch named '%s' already exists."), newref.buf + 11);
+       validate_new_branchname(newname, &newref, force);
  
        strbuf_addf(&logmsg, "Branch: renamed %s to %s",
                 oldref.buf, newref.buf);
@@@ -613,7 -609,7 +609,7 @@@ static int opt_parse_merge_filter(cons
  int cmd_branch(int argc, const char **argv, const char *prefix)
  {
        int delete = 0, rename = 0, force_create = 0;
 -      int verbose = 0, abbrev = DEFAULT_ABBREV, detached = 0;
 +      int verbose = 0, abbrev = -1, detached = 0;
        int reflog = 0;
        enum branch_track track;
        int kinds = REF_LOCAL_BRANCH;
  
        git_config(git_branch_config, NULL);
  
 -      if (branch_use_color == -1)
 -              branch_use_color = git_use_color_default;
 -
        track = git_branch_track;
  
        head = resolve_ref("HEAD", head_sha1, 0, NULL);
        if (!!delete + !!rename + !!force_create > 1)
                usage_with_options(builtin_branch_usage, options);
  
 +      if (abbrev == -1)
 +              abbrev = DEFAULT_ABBREV;
 +
        if (delete)
                return delete_branches(argc, argv, delete > 1, kinds);
        else if (argc == 0)
diff --combined builtin/checkout.c
index 4eaedff0c47cbc8ab38e4c1ec813b1f7a248cdff,ddefec053e563dff0e16cd5adb2f8f6d26c77d87..3bb652591bb0ee53f2c4d7aed2c32dc480f6e5ae
@@@ -201,7 -201,7 +201,7 @@@ static int checkout_merged(int pos, str
  }
  
  static int checkout_paths(struct tree *source_tree, const char **pathspec,
 -                        struct checkout_opts *opts)
 +                        const char *prefix, struct checkout_opts *opts)
  {
        int pos;
        struct checkout state;
                match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, ps_matched);
        }
  
 -      if (report_path_error(ps_matched, pathspec, 0))
 +      if (report_path_error(ps_matched, pathspec, prefix))
                return 1;
  
        /* "checkout -m path" to recreate conflicted state */
@@@ -657,25 -657,24 +657,25 @@@ static void suggest_reattach(struct com
                "Warning: you are leaving %d commit behind, "
                "not connected to\n"
                "any of your branches:\n\n"
 -              "%s\n"
 -              "If you want to keep it by creating a new branch, "
 -              "this may be a good time\nto do so with:\n\n"
 -              " git branch new_branch_name %s\n\n",
 +              "%s\n",
                /* The plural version */
                "Warning: you are leaving %d commits behind, "
                "not connected to\n"
                "any of your branches:\n\n"
 -              "%s\n"
 -              "If you want to keep them by creating a new branch, "
 -              "this may be a good time\nto do so with:\n\n"
 -              " git branch new_branch_name %s\n\n",
 +              "%s\n",
                /* Give ngettext() the count */
                lost),
                lost,
 -              sb.buf,
 -              sha1_to_hex(commit->object.sha1));
 +              sb.buf);
        strbuf_release(&sb);
 +
 +      if (advice_detached_head)
 +              fprintf(stderr,
 +                      _(
 +                      "If you want to keep them by creating a new branch, "
 +                      "this may be a good time\nto do so with:\n\n"
 +                      " git branch new_branch_name %s\n\n"),
 +                      sha1_to_hex(commit->object.sha1));
  }
  
  /*
@@@ -1064,7 -1063,7 +1064,7 @@@ int cmd_checkout(int argc, const char *
                if (1 < !!opts.writeout_stage + !!opts.force + !!opts.merge)
                        die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\nchecking out of the index."));
  
 -              return checkout_paths(source_tree, pathspec, &opts);
 +              return checkout_paths(source_tree, pathspec, prefix, &opts);
        }
  
        if (patch_mode)
  
        if (opts.new_branch) {
                struct strbuf buf = STRBUF_INIT;
-               if (strbuf_check_branch_ref(&buf, opts.new_branch))
-                       die(_("git checkout: we do not like '%s' as a branch name."),
-                           opts.new_branch);
-               if (ref_exists(buf.buf)) {
-                       opts.branch_exists = 1;
-                       if (!opts.new_branch_force)
-                               die(_("git checkout: branch %s already exists"),
-                                   opts.new_branch);
-               }
+               opts.branch_exists = validate_new_branchname(opts.new_branch, &buf, !!opts.new_branch_force);
                strbuf_release(&buf);
        }