git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 2/3] branch: split validate_new_branchname() into two

From
Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
Date
Oct 21, 2017, 04:58 UTC
Message-ID
<1508561910.2516.22.camel@gmail.com>
In-Reply-To
<20171013051132.3973-3-gitster@pobox.com>
On Fri, 2017-10-13 at 14:11 +0900, Junio C Hamano wrote:
Show 36 quoted lines
> 
> 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.
> + */

I guess it's better to avoid repeating the comments in both the .h and .c file as they might quite easily become stale. I would prefer keeping it in the header file alone.

Show 22 quoted lines
> +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;
> 

The change was simple by splitting the function into two and calling the right function as required at every call site! As far as I could see this doesn't seem to be reducing the confusion that the 'attr_only' parameter caused. That's because the new validate_branchname function seems to be called in some cases when the 'force' parameter is true and in other cases the 'force' parameter is sent to the 'validate_new_branchname' function. So, I think consistency is lacking in this change. That's just my opinion, of course.

-- 
Kaartic
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 of 26 in “a small branch API clean-up”
  1. 0/3 a small branch API clean-upJunio C Hamano, Oct 13, 2017
  2. 2/3 branch: split validate_new_branchname() into twoJunio C Hamano, Oct 13, 2017
  3. Kaartic SivaraamOct 21, 2017
  4. Junio C HamanoOct 21, 2017
  5. 1/3 branch: streamline "attr_only" handling in validate_new_branchname()Junio C Hamano, Oct 13, 2017
  6. Eric SunshineOct 13, 2017
  7. 3/3 branch: forbid refs/heads/HEADJunio C Hamano, Oct 13, 2017
  8. Jeff KingOct 13, 2017
  9. Junio C HamanoOct 14, 2017
  10. Junio C HamanoOct 14, 2017
  11. Jeff KingOct 16, 2017
  12. Kaartic SivaraamOct 21, 2017
  13. Junio C HamanoOct 21, 2017
  14. Kaartic SivaraamOct 22, 2017
  15. Kaartic SivaraamOct 21, 2017
  16. Junio C HamanoOct 21, 2017
  17. Kaartic SivaraamOct 22, 2017
  18. 1/2 branch: forbid refs/heads/HEADKaartic Sivaraam, Nov 14, 2017
  19. 2/2 builtin/branch: remove redundant check for HEADKaartic Sivaraam, Nov 14, 2017
  20. Kaartic SivaraamNov 14, 2017
  21. Junio C HamanoNov 14, 2017
  22. Kaartic SivaraamNov 15, 2017
  23. 3/4 branch: correctly reject refs/heads/{-dash,HEAD}Junio C Hamano, Nov 15, 2017
  24. Kaartic SivaraamNov 16, 2017
  25. Junio C HamanoNov 16, 2017
  26. Kaartic SivaraamNov 16, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.