diff options
author | Junio C Hamano <gitster@pobox.com> | 2017-10-13 13:45:40 +0900 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2017-10-13 17:11:41 +0900 |
commit | bc1c9c0e674bdd293c29ae84365915848ed01d7a (patch) | |
tree | ddde1bceda59ec50837294f152d49834aa9d6bdd /branch.c | |
parent | 8280c4c1ea59bc6d101c5616490627b63934318e (diff) | |
download | git-bc1c9c0e674bdd293c29ae84365915848ed01d7a.tar.gz |
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 <gitster@pobox.com>
Diffstat (limited to 'branch.c')
-rw-r--r-- | branch.c | 34 |
1 files changed, 23 insertions, 11 deletions
@@ -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 |