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

Re: [PATCH v6 2/6] Add git_env_ulong() to parse environment variable

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 26, 2014, 20:20 UTC
Message-ID
<xmqq38cjhuje.fsf@gitster.dls.corp.google.com>
In-Reply-To
<20140826182125.GC17546@peff.net>
Jeff King <peff@peff.net> writes:
Show 26 quoted lines
> On Tue, Aug 26, 2014 at 05:23:21PM +0200, Steffen Prohaska wrote:
>
>> +/*
>> + * Use default if environment variable is unset or empty string.
>> + */
>> +unsigned long git_env_ulong(const char *k, unsigned long val)
>> +{
>> +	const char *v = getenv(k);
>> +	if (v && *v && !git_parse_ulong(v, &val))
>> +		die("failed to parse %s", k);
>> +	return val;
>> +}
>
> I think the "empty string" behavior here is sensible. I notice that
> git_env_bool is not so careful. I think we should probably do this
> (independent of your series):
>
> -- >8 --
> Subject: git_env_bool: treat empty string as "not set"
>
> If an environment variable we treat as a boolean is not set,
> we use some default value provided by the caller. If it is
> set but is the empty string, however, we treat it as
> "false". This can be rather confusing, as it is easy to set
> the variable to the empty string in the shell (e.g., by
> calling GIT_SMART_HTTP= instead of "unset").

I think different people have different confusion criteria. To me, these two are very different operations:

    $ VAR=
    $ unset VAR

I think it boils down to that I see that the distance between "unset vs set to empty" is far larger than the distance between "empty vs false". You probably see these two distances the other way, i.e. "set to empty is almost like unset" and "empty is not a valid way to say false".

Due to this difference, the new test confused me and had me read it three times.

Show 6 quoted lines
> +test_expect_success 'empty GIT_SMART_HTTP leaves smart http enabled' '
> +	(GIT_SMART_HTTP= &&
> +	 export GIT_SMART_HTTP &&
> +	 cd clone &&
> +	 git fetch)
> +'

The test before this one explicitly sets GIT_SMART_HTTP to "0" and expects the fetch to fail. It is sensible to you because "0" is a lot more explicit "false" than an empty string to you, and you equate an empty and unset, hence the new one should succeed.

But it looks nonsensical to me that the new one expects to succeed, because "0" and an empty string are both valid way to say "false" to me, and it should behave the same way as the "0" one.

I view the *v check before git_parse_ulong() being unnecessarily defensive to avoid triggering "die()". An empty string is obviously not a number (somebody could argue that it is the same as zero, though), but nevertheless the user _is_ telling us to use that value by setting and exporting the variable. If we cannot parse it, barfing is what the user would appreciate.

So, I am not sure the patch in the message I am responding to, and I am not sure about that *v check in Steffen's patch, either.

Previous: Jeff KingNext: Jeff King
Message 5 of 19 in “Stream fd to clean filter; GIT_MMAP_LIMIT, GIT_ALLOC_LIMIT with git_env_ulong()”
  1. 0/6 Stream fd to clean filter; GIT_MMAP_LIMIT, GIT_ALLOC_LIMIT with git_env_ulong()Steffen Prohaska, Aug 26, 2014
  2. 1/6 convert: drop arguments other than 'path' from would_convert_to_git()Steffen Prohaska, Aug 26, 2014
  3. 2/6 Add git_env_ulong() to parse environment variableSteffen Prohaska, Aug 26, 2014
  4. Jeff KingAug 26, 2014
  5. Junio C HamanoAug 26, 2014
  6. Jeff KingAug 26, 2014
  7. Junio C HamanoAug 26, 2014
  8. Jeff KingAug 27, 2014
  9. Junio C HamanoAug 27, 2014
  10. Steffen ProhaskaAug 28, 2014
  11. Junio C HamanoAug 28, 2014
  12. 3/6 Change GIT_ALLOC_LIMIT check to use git_env_ulong()Steffen Prohaska, Aug 26, 2014
  13. 4/6 Introduce GIT_MMAP_LIMIT to allow testing expected mmap sizeSteffen Prohaska, Aug 26, 2014
  14. 5/6 Change copy_fd() to not close input fdSteffen Prohaska, Aug 26, 2014
  15. Junio C HamanoAug 26, 2014
  16. Jeff KingAug 26, 2014
  17. Steffen ProhaskaAug 28, 2014
  18. Junio C HamanoAug 28, 2014
  19. 6/6 convert: stream from fd to required clean filter to reduce used address spaceSteffen Prohaska, Aug 26, 2014

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.