From: Junio C Hamano Date: Mon, 29 Aug 2011 04:19:31 +0000 (-0700) Subject: Merge branch 'ci/forbid-unwanted-current-branch-update' X-Git-Tag: v1.7.7-rc1~36 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/1da6d98a9acd2db3902bf432a097771c76f4fd44?ds=inline;hp=-c Merge branch 'ci/forbid-unwanted-current-branch-update' * ci/forbid-unwanted-current-branch-update: Show interpreted branch name in error messages Prevent force-updating of the current branch --- 1da6d98a9acd2db3902bf432a097771c76f4fd44 diff --combined branch.c index d62cc0132c,1fe3078a6c..478d82567d --- a/branch.c +++ 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) @@@ -151,17 -171,11 +171,11 @@@ 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; @@@ -210,7 -224,7 +224,7 @@@ 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 f7da69c932,40f885c9fe..aa705a0fb0 --- a/builtin/branch.c +++ b/builtin/branch.c @@@ -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; @@@ -673,6 -669,9 +669,6 @@@ 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); @@@ -693,9 -692,6 +689,9 @@@ 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 4eaedff0c4,ddefec053e..3bb652591b --- a/builtin/checkout.c +++ b/builtin/checkout.c @@@ -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; @@@ -231,7 -231,7 +231,7 @@@ 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) @@@ -1072,15 -1071,9 +1072,9 @@@ 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); }