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

Re: Inconsistent/buggy behaviour of "git config --add"

From
TLTim Landscheidt <tim@tim-landscheidt.de>
Date
Mar 24, 2024, 18:57 UTC
Message-ID
<87o7b3xxta.fsf@vagabond.tim-landscheidt.de>
In-Reply-To
<xmqq8r28ygwi.fsf@gitster.g>
Junio C Hamano <gitster@pobox.com> wrote:
> […]
Show 6 quoted lines
>> So on one hand, "--add" must be given before the key to add
>> a line, but if on the other hand one passes the option after
>> the key and before the value, it is literally taken as the
>> value and the value does not seem to be interpreted as a
>> value-pattern, either.  However, if the value is missing,
>> Git correctly recognizes that this does not make sense.
> Not really.  I agree that the "git config" syntax is messy, but I
> followed your example with "git config --help" (especially its
> SYNOPSIS section) in hand, and reached the above explanation, which
> your conjecutre does not quite match.
I respectfully disagree (somewhat).  git-config(1) reads:
| […]
|        Multiple lines can be added to an option by using
|        the --add option. If you want to update or unset an
|        option which can occur on multiple lines, a
|        value-pattern (which is an extended regular
|        expression, unless the --fixed-value option is
|        given) needs to be given. Only the existing values
|        that match the pattern are updated or unset. If you
|        want to handle the lines that do not match the
|        pattern, just prepend a single exclamation mark in
|        front (see also the section called “EXAMPLES”), but
|        note that this only works when the --fixed-value
|        option is not in use.
| […]
|        --replace-all
|            Default behavior is to replace at most one
|            line. This replaces all lines matching the key
|            (and optionally the value-pattern).

This says quite clearly if one wants to /add/ a value, "--add" must be given; the default is to /replace/ an existing value.

The only indication that this might be false is the description of "--add" itself which gives another and different explanation for the behaviour of "--replace-all":

|        --add
|            Adds a new line to the option without altering
|            any existing values. This is the same as
|            providing ^$ as the value-pattern in
|            --replace-all.
| […]

So the first section states that "only the existing values that match the pattern are updated or unset", and here it says that if there are no matches, a new line is added.

>> My expectation of least surprise is that "git config
>> section.key --add value" should be equivalent to "git config
>> --add section.key value".
> You cannot have "--add" as a value by doing so.
> […]

The standard way to achieve this would be to use "--", i. e. "git config section.key --add -- --add" (apparently even part of POSIX as "Utility Syntax Guidelines", but also commonly used elsewhere in Git).

Tim
Previous: Junio C HamanoNext: brian m. carlson
Message 3 of 6 in “Inconsistent/buggy behaviour of "git config --add"”
  1. Tim LandscheidtMar 23, 2024
  2. Junio C HamanoMar 23, 2024
  3. Tim LandscheidtMar 24, 2024
  4. brian m. carlsonMar 25, 2024
  5. Patrick SteinhardtMar 25, 2024
  6. Junio C HamanoMar 25, 2024

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.