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

Re: [PATCH v2 1/2] remote: drop "explicit" parameter from remote_ref_for_branch()

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 3, 2020, 17:51 UTC
Message-ID
<xmqqzhcx8gz8.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<20200303161223.1870298-2-damien.olivier.robert+git@gmail.com>
Damien Robert <damien.olivier.robert@gmail.com> writes:
Show 10 quoted lines
> From: Jeff King <peff@peff.net>
>
> Commit 9700fae5ee (for-each-ref: let upstream/push report the remote ref
> name, 2017-11-07) added a remote_ref_for_branch() helper, which is
> modeled after remote_for_branch(). This includes providing an "explicit"
> out-parameter that tells the caller whether the remote was configured by
> the user, or whether we picked a default name like "origin".
>
> But unlike remote names, there's no default case for the remote branch
> name.

Up to this point, it is well written and easy to read. I think "there is no case where a default name for the remoate branch is used" would be even more easy to read.

In any case, if there is no case that default name, I understand that explicit is always set to 1?

> In any case where we don't set "explicit", we'd just an empty
> string anyway.

Sorry, but I cannot parse this. But earlier, you established that there is no case that a default is used, so is there any case where we don't set "explicit"? I don't get it.

> Let's instead return NULL in this case, letting us
> simplify the function interface.
Show 9 quoted lines
>  	} else if (atom->u.remote_ref.option == RR_REMOTE_REF) {
> -		int explicit;
>  		const char *merge;
>  
> -		merge = remote_ref_for_branch(branch, atom->u.remote_ref.push,
> -					      &explicit);
> -		*s = xstrdup(explicit ? merge : "");
> +		merge = remote_ref_for_branch(branch, atom->u.remote_ref.push);
> +		*s = xstrdup(merge ? merge : "");

Mental note: as long as explicit is set to true when the function returns a non-null value, this change will be a no-op as far as the result is concerned, which is a good thing.

Show 18 quoted lines
> diff --git a/remote.c b/remote.c
> index 593ce297ed..c43196ec06 100644
> --- a/remote.c
> +++ b/remote.c
> @@ -516,14 +516,11 @@ const char *pushremote_for_branch(struct branch *branch, int *explicit)
>  	return remote_for_branch(branch, explicit);
>  }
>  
> -const char *remote_ref_for_branch(struct branch *branch, int for_push,
> -				  int *explicit)
> +const char *remote_ref_for_branch(struct branch *branch, int for_push)
>  {
>  	if (branch) {
>  		if (!for_push) {
>  			if (branch->merge_nr) {
> -				if (explicit)
> -					*explicit = 1;
>  				return branch->merge_name[0];

This case returns a non-NULL pointer, and used to set explicit to true.

Show 9 quoted lines
>  			}
>  		} else {
> @@ -534,15 +531,11 @@ const char *remote_ref_for_branch(struct branch *branch, int for_push,
>  			if (remote && remote->push.nr &&
>  			    (dst = apply_refspecs(&remote->push,
>  						  branch->refname))) {
> -				if (explicit)
> -					*explicit = 1;
>  				return dst;
So did this.
Show 7 quoted lines
>  			}
>  		}
>  	}
> -	if (explicit)
> -		*explicit = 0;
> -	return "";
> +	return NULL;

And this one used to return 0 in explicit and returned an empty string. The only caller ignored that empty string so returning NULL in the new code does not make a difference, which also is a good thing.

So the code looks correctly done. I just don't get the feeling that the proposed log message explains the change clearly to readers.

After reading the code through, I think "there's no default case" was what caused my confusion.

    But unlike remote names, there is no default name when the user
    didn't configure one.  The only way the "explicit" parameter is
    used by the caller is to use the value returned from the helper
    when it is set, and use an empty string otherwise, ignoring the
    returned value from the helper.
    Let's drop the "explicit" out-parameter, and return NULL when
    the returned value from the helper should be ignored, to
    simplify the function interface.
perhaps?
Previous: Damien RobertNext: Jeff King
Message 7 of 32 in “remote.c: fix handling of push:remote_ref”
  1. 1/1 remote.c: fix handling of push:remote_refDamien Robert, Feb 28, 2020
  2. Jeff KingFeb 28, 2020
  3. Damien RobertMar 1, 2020
  4. Jeff KingMar 2, 2020
  5. 0/2 Damien Robert, Mar 3, 2020
  6. 1/2 remote: drop "explicit" parameter from remote_ref_for_branch()Damien Robert, Mar 3, 2020
  7. Junio C HamanoMar 3, 2020
  8. Jeff KingMar 3, 2020
  9. Junio C HamanoMar 3, 2020
  10. 2/2 remote.c: fix handling of %(push:remoteref)Damien Robert, Mar 3, 2020
  11. Damien RobertMar 3, 2020
  12. Junio C HamanoMar 3, 2020
  13. Junio C HamanoMar 3, 2020
  14. Damien RobertMar 3, 2020
  15. Junio C HamanoMar 3, 2020
  16. 1/1 remote.c: fix handling of %(push:remoteref)Damien Robert, Mar 12, 2020
  17. Damien RobertMar 25, 2020
  18. Junio C HamanoMar 27, 2020
  19. Damien RobertMar 28, 2020
  20. Jeff KingMar 28, 2020
  21. Jeff KingMar 28, 2020
  22. Damien RobertApr 16, 2020
  23. Damien RobertApr 6, 2020
  24. Jeff KingApr 6, 2020
  25. 1/1 remote.c: fix handling of %(push:remoteref)Damien Robert, Apr 16, 2020
  26. Damien RobertApr 16, 2020
  27. Junio C HamanoSep 3, 2020
  28. Damien RobertSep 11, 2020
  29. Junio C HamanoSep 14, 2020
  30. Damien RobertMar 3, 2020
  31. Jeff KingMar 2, 2020
  32. Damien RobertMar 3, 2020

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.