From 8280c4c1ea59bc6d101c5616490627b63934318e Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 13 Oct 2017 12:57:02 +0900 Subject: [PATCH 1/4] branch: streamline "attr_only" handling in validate_new_branchname() The function takes a parameter "attr_only" (which is a name that is hard to reason about, which will be corrected soon) and skips some checks when it is set. Reorganize the conditionals to make it more obvious that some checks are never made when this parameter is set. Signed-off-by: Junio C Hamano --- branch.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/branch.c b/branch.c index 4377ce2fb1..7404597678 100644 --- a/branch.c +++ b/branch.c @@ -181,21 +181,25 @@ int read_branch_desc(struct strbuf *buf, const char *branch_name) int validate_new_branchname(const char *name, struct strbuf *ref, int force, int attr_only) { + const char *head; + 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 && !attr_only) - die(_("A branch named '%s' already exists."), ref->buf + strlen("refs/heads/")); - if (!attr_only) { - const char *head; + if (attr_only) + return 1; + + if (!force) + die(_("A branch named '%s' already exists."), + ref->buf + strlen("refs/heads/")); + + head = resolve_ref_unsafe("HEAD", 0, NULL, NULL); + if (!is_bare_repository() && head && !strcmp(head, ref->buf)) + die(_("Cannot force update the current branch.")); - head = resolve_ref_unsafe("HEAD", 0, NULL, NULL); - if (!is_bare_repository() && head && !strcmp(head, ref->buf)) - die(_("Cannot force update the current branch.")); - } return 1; } From bc1c9c0e674bdd293c29ae84365915848ed01d7a Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 13 Oct 2017 13:45:40 +0900 Subject: [PATCH 2/4] branch: split validate_new_branchname() into two Checking if a proposed name is appropriate for a branch is strictly a subset of checking if we want to allow creating or updating a branch with such a name. The mysterious sounding 'attr_only' parameter to validate_new_branchname() is used to switch the function between these two roles. Instead, split the function into two, and adjust the callers. A new helper validate_branchname() only checks the name and reports if the branch already exists. This loses one NEEDSWORK from the branch API. Signed-off-by: Junio C Hamano --- branch.c | 34 +++++++++++++++++++++++----------- branch.h | 27 ++++++++++++--------------- builtin/branch.c | 8 ++++---- builtin/checkout.c | 10 +++++----- 4 files changed, 44 insertions(+), 35 deletions(-) diff --git a/branch.c b/branch.c index 7404597678..2c3a364a0b 100644 --- a/branch.c +++ b/branch.c @@ -178,19 +178,31 @@ int read_branch_desc(struct strbuf *buf, const char *branch_name) return 0; } -int validate_new_branchname(const char *name, struct strbuf *ref, - int force, int attr_only) +/* + * Check if 'name' can be a valid name for a branch; die otherwise. + * Return 1 if the named branch already exists; return 0 otherwise. + * Fill ref with the full refname for the branch. + */ +int validate_branchname(const char *name, struct strbuf *ref) { - const char *head; - if (strbuf_check_branch_ref(ref, name)) die(_("'%s' is not a valid branch name."), name); - if (!ref_exists(ref->buf)) - return 0; + return ref_exists(ref->buf); +} - if (attr_only) - return 1; +/* + * Check if a branch 'name' can be created as a new branch; die otherwise. + * 'force' can be used when it is OK for the named branch already exists. + * Return 1 if the named branch already exists; return 0 otherwise. + * Fill ref with the full refname for the branch. + */ +int validate_new_branchname(const char *name, struct strbuf *ref, int force) +{ + const char *head; + + if (!validate_branchname(name, ref)) + return 0; if (!force) die(_("A branch named '%s' already exists."), @@ -246,9 +258,9 @@ void create_branch(const char *name, const char *start_name, if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE) explicit_tracking = 1; - if (validate_new_branchname(name, &ref, force, - track == BRANCH_TRACK_OVERRIDE || - clobber_head)) { + if ((track == BRANCH_TRACK_OVERRIDE || clobber_head) + ? validate_branchname(name, &ref) + : validate_new_branchname(name, &ref, force)) { if (!force) dont_change_ref = 1; else diff --git a/branch.h b/branch.h index b07788558c..be5e5d1308 100644 --- a/branch.h +++ b/branch.h @@ -23,22 +23,19 @@ void create_branch(const char *name, const char *start_name, int clobber_head, int quiet, enum branch_track track); /* - * Validates that the requested branch may be created, returning the - * interpreted ref in ref, force indicates whether (non-head) branches - * may be overwritten. A non-zero return value indicates that the force - * parameter was non-zero and the branch already exists. - * - * Contrary to all of the above, when attr_only is 1, the caller is - * not interested in verifying if it is Ok to update the named - * branch to point at a potentially different commit. It is merely - * asking if it is OK to change some attribute for the named branch - * (e.g. tracking upstream). - * - * NEEDSWORK: This needs to be split into two separate functions in the - * longer run for sanity. - * + * Check if 'name' can be a valid name for a branch; die otherwise. + * Return 1 if the named branch already exists; return 0 otherwise. + * Fill ref with the full refname for the branch. */ -int validate_new_branchname(const char *name, struct strbuf *ref, int force, int attr_only); +extern int validate_branchname(const char *name, struct strbuf *ref); + +/* + * Check if a branch 'name' can be created as a new branch; die otherwise. + * 'force' can be used when it is OK for the named branch already exists. + * Return 1 if the named branch already exists; return 0 otherwise. + * Fill ref with the full refname for the branch. + */ +extern int validate_new_branchname(const char *name, struct strbuf *ref, int force); /* * Remove information about the state of working on the current diff --git a/builtin/branch.c b/builtin/branch.c index b67593288c..e5bbfb4a17 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -463,7 +463,6 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT; struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT; int recovery = 0; - int clobber_head_ok; if (!oldname) { if (copy) @@ -487,9 +486,10 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int * A command like "git branch -M currentbranch currentbranch" cannot * cause the worktree to become inconsistent with HEAD, so allow it. */ - clobber_head_ok = !strcmp(oldname, newname); - - validate_new_branchname(newname, &newref, force, clobber_head_ok); + if (!strcmp(oldname, newname)) + validate_branchname(newname, &newref); + else + validate_new_branchname(newname, &newref, force); reject_rebase_or_bisect_branch(oldref.buf); diff --git a/builtin/checkout.c b/builtin/checkout.c index fc4f8fd2ea..697ac7dcaf 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -1289,11 +1289,11 @@ int cmd_checkout(int argc, const char **argv, const char *prefix) if (opts.new_branch) { struct strbuf buf = STRBUF_INIT; - opts.branch_exists = - validate_new_branchname(opts.new_branch, &buf, - !!opts.new_branch_force, - !!opts.new_branch_force); - + if (opts.new_branch_force) + opts.branch_exists = validate_branchname(opts.new_branch, &buf); + else + opts.branch_exists = + validate_new_branchname(opts.new_branch, &buf, 0); strbuf_release(&buf); } From a625b092cc59940521789fe8a3ff69c8d6b14eb2 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 14 Nov 2017 17:12:58 +0530 Subject: [PATCH 3/4] branch: correctly reject refs/heads/{-dash,HEAD} strbuf_check_branch_ref() is the central place where many codepaths see if a proposed name is suitable for the name of a branch. It was designed to allow us to get stricter than the check_refname_format() check used for refnames in general, and we already use it to reject a branch whose name begins with a '-'. The function gets a strbuf and a string "name", and returns non-zero if the name is not appropriate as the name for a branch. When the name is good, it places the full refname for the branch with the proposed name in the strbuf before it returns. However, it turns out that one caller looks at what is in the strbuf even when the function returns an error. Make the function populate the strbuf even when it returns an error. That way, when "-dash" is given as name, "refs/heads/-dash" is placed in the strbuf when returning an error to copy_or_rename_branch(), which notices that the user is trying to recover with "git branch -m -- -dash dash" to rename "-dash" to "dash". While at it, use the same mechanism to also reject "HEAD" as a branch name. Helped-by: Jeff King Helped-by: Kaartic Sivaraam Signed-off-by: Junio C Hamano --- sha1_name.c | 14 ++++++++++++-- t/t1430-bad-ref-name.sh | 43 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/sha1_name.c b/sha1_name.c index c7c5ab376c..67961d6e47 100644 --- a/sha1_name.c +++ b/sha1_name.c @@ -1332,9 +1332,19 @@ void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed) int strbuf_check_branch_ref(struct strbuf *sb, const char *name) { strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL); - if (name[0] == '-') - return -1; + + /* + * This splice must be done even if we end up rejecting the + * name; builtin/branch.c::copy_or_rename_branch() still wants + * to see what the name expanded to so that "branch -m" can be + * used as a tool to correct earlier mistakes. + */ strbuf_splice(sb, 0, 0, "refs/heads/", 11); + + if (*name == '-' || + !strcmp(sb->buf, "refs/heads/HEAD")) + return -1; + return check_refname_format(sb->buf, 0); } diff --git a/t/t1430-bad-ref-name.sh b/t/t1430-bad-ref-name.sh index e88349c8a0..c7878a60ed 100755 --- a/t/t1430-bad-ref-name.sh +++ b/t/t1430-bad-ref-name.sh @@ -331,4 +331,47 @@ test_expect_success 'update-ref --stdin -z fails delete with bad ref name' ' grep "fatal: invalid ref format: ~a" err ' +test_expect_success 'branch rejects HEAD as a branch name' ' + test_must_fail git branch HEAD HEAD^ && + test_must_fail git show-ref refs/heads/HEAD +' + +test_expect_success 'checkout -b rejects HEAD as a branch name' ' + test_must_fail git checkout -B HEAD HEAD^ && + test_must_fail git show-ref refs/heads/HEAD +' + +test_expect_success 'update-ref can operate on refs/heads/HEAD' ' + git update-ref refs/heads/HEAD HEAD^ && + git show-ref refs/heads/HEAD && + git update-ref -d refs/heads/HEAD && + test_must_fail git show-ref refs/heads/HEAD +' + +test_expect_success 'branch -d can remove refs/heads/HEAD' ' + git update-ref refs/heads/HEAD HEAD^ && + git branch -d HEAD && + test_must_fail git show-ref refs/heads/HEAD +' + +test_expect_success 'branch -m can rename refs/heads/HEAD' ' + git update-ref refs/heads/HEAD HEAD^ && + git branch -m HEAD tail && + test_must_fail git show-ref refs/heads/HEAD && + git show-ref refs/heads/tail +' + +test_expect_success 'branch -d can remove refs/heads/-dash' ' + git update-ref refs/heads/-dash HEAD^ && + git branch -d -- -dash && + test_must_fail git show-ref refs/heads/-dash +' + +test_expect_success 'branch -m can rename refs/heads/-dash' ' + git update-ref refs/heads/-dash HEAD^ && + git branch -m -- -dash dash && + test_must_fail git show-ref refs/heads/-dash && + git show-ref refs/heads/dash +' + test_done From 662a4c8a097248a3c08a671866ecf37743f3ca4d Mon Sep 17 00:00:00 2001 From: Kaartic Sivaraam Date: Tue, 14 Nov 2017 17:12:59 +0530 Subject: [PATCH 4/4] builtin/branch: remove redundant check for HEAD The lower level code has been made to handle this case for the sake of consistency. This has made this check redundant. So, remove the redundant check. Signed-off-by: Kaartic Sivaraam Signed-off-by: Junio C Hamano --- builtin/branch.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/builtin/branch.c b/builtin/branch.c index e5bbfb4a17..945790b60b 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -793,9 +793,6 @@ int cmd_branch(int argc, const char **argv, const char *prefix) } else if (argc > 0 && argc <= 2) { struct branch *branch = branch_get(argv[0]); - if (!strcmp(argv[0], "HEAD")) - die(_("it does not make sense to create 'HEAD' manually")); - if (!branch) die(_("no such branch '%s'"), argv[0]);