summaryrefslogtreecommitdiff
path: root/branch.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2017-11-28 04:41:49 (GMT)
committerJunio C Hamano <gitster@pobox.com>2017-11-28 04:41:49 (GMT)
commit16169285f1e63f6a3d9ec10e48f55d6825d3156b (patch)
treeddcde546556a9bd2a5d4cce6daca273edc30963c /branch.c
parent5f9953d2c365bffed6f9ee0c6966556bd4d7e2f4 (diff)
parent662a4c8a097248a3c08a671866ecf37743f3ca4d (diff)
downloadgit-16169285f1e63f6a3d9ec10e48f55d6825d3156b.zip
git-16169285f1e63f6a3d9ec10e48f55d6825d3156b.tar.gz
git-16169285f1e63f6a3d9ec10e48f55d6825d3156b.tar.bz2
Merge branch 'jc/branch-name-sanity'
"git branch" and "git checkout -b" are now forbidden from creating a branch whose name is "HEAD". * jc/branch-name-sanity: builtin/branch: remove redundant check for HEAD branch: correctly reject refs/heads/{-dash,HEAD} branch: split validate_new_branchname() into two branch: streamline "attr_only" handling in validate_new_branchname()
Diffstat (limited to 'branch.c')
-rw-r--r--branch.c44
1 files changed, 30 insertions, 14 deletions
diff --git a/branch.c b/branch.c
index 62f7b0d..fe1e1c3 100644
--- a/branch.c
+++ b/branch.c
@@ -178,24 +178,40 @@ 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)
{
if (strbuf_check_branch_ref(ref, name))
die(_("'%s' is not a valid branch name."), name);
- if (!ref_exists(ref->buf))
+ return ref_exists(ref->buf);
+}
+
+/*
+ * 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;
- else if (!force && !attr_only)
- die(_("A branch named '%s' already exists."), ref->buf + strlen("refs/heads/"));
- if (!attr_only) {
- const char *head;
+ 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;
}
@@ -242,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