Skip to content
  • Johannes Schindelin's avatar
    git config --unset: remove empty sections (in the common case) · 22aedfcc
    Johannes Schindelin authored and Junio C Hamano's avatar Junio C Hamano committed
    
    
    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: default avatarJohannes Schindelin <johannes.schindelin@gmx.de>
    Signed-off-by: default avatarJunio C Hamano <gitster@pobox.com>
    22aedfcc