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 <gitster@pobox.com>

Junio C Hamano committed Oct 13, 2017 at 12:57 UTC 8280c4c1ea59bc6d101c5616490627b63934318e
1 file changed +12 -8
branch.c
+12 -8
@@ -181,21 +181,25 @@ int read_branch_desc(struct strbuf *buf, const char *branch_name)
181 int validate_new_branchname(const char *name, struct strbuf *ref,
182 int force, int attr_only)
183 {
184 + const char *head;
185 +
186 if (strbuf_check_branch_ref(ref, name))
187 die(_("'%s' is not a valid branch name."), name);
188
189 if (!ref_exists(ref->buf))
190 return 0;
189 - else if (!force && !attr_only)
190 - die(_("A branch named '%s' already exists."), ref->buf + strlen("refs/heads/"));
191
192 - if (!attr_only) {
193 - const char *head;
192 + if (attr_only)
193 + return 1;
194 +
195 + if (!force)
196 + die(_("A branch named '%s' already exists."),
197 + ref->buf + strlen("refs/heads/"));
198 +
199 + head = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
200 + if (!is_bare_repository() && head && !strcmp(head, ref->buf))
201 + die(_("Cannot force update the current branch."));
202
195 - head = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
196 - if (!is_bare_repository() && head && !strcmp(head, ref->buf))
197 - die(_("Cannot force update the current branch."));
198 - }
203 return 1;
204 }
205