@@ -178,24 +178,40 @@ int read_branch_desc(struct strbuf *buf, const char *branch_name)
178178 return 0 ;
179179}
180180
181- int validate_new_branchname (const char * name , struct strbuf * ref ,
182- int force , int attr_only )
181+ /*
182+ * Check if 'name' can be a valid name for a branch; die otherwise.
183+ * Return 1 if the named branch already exists; return 0 otherwise.
184+ * Fill ref with the full refname for the branch.
185+ */
186+ int validate_branchname (const char * name , struct strbuf * ref )
183187{
184188 if (strbuf_check_branch_ref (ref , name ))
185189 die (_ ("'%s' is not a valid branch name." ), name );
186190
187- if (!ref_exists (ref -> buf ))
191+ return ref_exists (ref -> buf );
192+ }
193+
194+ /*
195+ * Check if a branch 'name' can be created as a new branch; die otherwise.
196+ * 'force' can be used when it is OK for the named branch already exists.
197+ * Return 1 if the named branch already exists; return 0 otherwise.
198+ * Fill ref with the full refname for the branch.
199+ */
200+ int validate_new_branchname (const char * name , struct strbuf * ref , int force )
201+ {
202+ const char * head ;
203+
204+ if (!validate_branchname (name , ref ))
188205 return 0 ;
189- else if (!force && !attr_only )
190- die (_ ("A branch named '%s' already exists." ), ref -> buf + strlen ("refs/heads/" ));
191206
192- if (!attr_only ) {
193- const char * head ;
207+ if (!force )
208+ die (_ ("A branch named '%s' already exists." ),
209+ ref -> buf + strlen ("refs/heads/" ));
210+
211+ head = resolve_ref_unsafe ("HEAD" , 0 , NULL , NULL );
212+ if (!is_bare_repository () && head && !strcmp (head , ref -> buf ))
213+ die (_ ("Cannot force update the current branch." ));
194214
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- }
199215 return 1 ;
200216}
201217
@@ -242,9 +258,9 @@ void create_branch(const char *name, const char *start_name,
242258 if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE )
243259 explicit_tracking = 1 ;
244260
245- if (validate_new_branchname ( name , & ref , force ,
246- track == BRANCH_TRACK_OVERRIDE ||
247- clobber_head )) {
261+ if (( track == BRANCH_TRACK_OVERRIDE || clobber_head )
262+ ? validate_branchname ( name , & ref )
263+ : validate_new_branchname ( name , & ref , force )) {
248264 if (!force )
249265 dont_change_ref = 1 ;
250266 else
0 commit comments