]> git.ipfire.org Git - thirdparty/git.git/commit - config.c
git config --unset: remove empty sections (in the common case)
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Mon, 9 Apr 2018 08:32:24 +0000 (10:32 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 9 Apr 2018 12:32:59 +0000 (21:32 +0900)
commit22aedfccd0cfe7c8f09a990cf4777efc7f27bd2e
tree9065d747692b570ca27b144796a539d3465e6383
parent6ae996f2acf3ad780b8d338c81e24143f0b0d304
git config --unset: remove empty sections (in the common case)

The original reasoning for not removing section headers upon removal of
the last entry went like this: the user could have added comments about
the section, or about the entries therein, and if there were other
comments there, we would not know whether we should remove them.

In particular, a concocted example was presented that looked like this
(and was added to t1300):

# some generic comment on the configuration file itself
# a comment specific to this "section" section.
[section]
# some intervening lines
# that should also be dropped

key = value
# please be careful when you update the above variable

The ideal thing for `git config --unset section.key` in this case would
be to leave only the first line behind, because all the other comments
are now obsolete.

However, this is unfeasible, short of adding a complete Natural Language
Processing module to Git, which seems not only a lot of work, but a
totally unreasonable feature (for little benefit to most users).

Now, the real kicker about this problem is: most users do not edit their
config files at all! In their use case, the config looks like this
instead:

[section]
key = value

... and it is totally obvious what should happen if the entry is
removed: the entire section should vanish.

Let's generalize this observation to this conservative strategy: if we
are removing the last entry from a section, and there are no comments
inside that section nor surrounding it, then remove the entire section.
Otherwise behave as before: leave the now-empty section (including those
comments, even ones about the now-deleted entry).

We have to be extra careful to handle the case where more than one entry
is removed: any subset of them might be the last entries of their
respective sections (and if there are no comments in or around that
section, the section should be removed, too).

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
config.c
t/t1300-config.sh