Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从.git/config中删除设置时如何避免空节?_Git_Configuration Files - Fatal编程技术网

从.git/config中删除设置时如何避免空节?

从.git/config中删除设置时如何避免空节?,git,configuration-files,Git,Configuration Files,如果我使用git config foo.bar baz,这会在.git/config中添加一个foo部分: ... [foo] bar = baz ... 我可以使用git config--unset foo.bar再次删除该设置,但该部分仍保留在文件中,没有任何内容: ... [foo] ... 如果我用git config foo.bar baz添加另一个foo设置,git config不会将其添加到空的foo部分;它开始了一个新的: ... [foo] [foo]

如果我使用
git config foo.bar baz
,这会在
.git/config
中添加一个
foo
部分:

...
[foo]
        bar = baz
...
我可以使用git config--unset foo.bar再次删除该设置,但该部分仍保留在文件中,没有任何内容:

...
[foo]
...
如果我用
git config foo.bar baz
添加另一个
foo
设置,
git config
不会将其添加到空的
foo
部分;它开始了一个新的:

...
[foo]
[foo]
        bar = baz
...
我的问题是:

  • 这是预期的行为吗
  • 如果不是,它是一个bug吗
  • 在取消设置配置时,是否有办法避免配置文件中可能出现的大量空节

  • git config--remove section
    ,所以您可以删除整个部分。但是,对我来说,如果存在一个空分区,它会创建一个新分区,这看起来像是一个bug。

    托马斯·拉斯特引起了我的注意

    根据我对Peff描述的理解,如果配置解析器的编程不是特别的,那么问题很容易解决。解析文件后,结果是一个包含配置设置的结构,但不包含文件的原始结构。由于调用者无法获得有关节结构的信息,因此插入代码的设置无法知道是否存在标题正确的空节。另外,删除空节也有点棘手,因为它们可能包含重要的注释,而不应该仅仅因为删除了节的最后一个功能部分就自动删除这些注释

    结论是:

  • 不,这不是预期的行为
  • 是的,它是一只虫子

  • 这类编程虽然繁琐但简单,所以我将看看是否可以制作一个补丁。

    要回答第3点,您可以使用git config--get regexp的结果来决定何时清理:

    git config --unset "foo.bar";
    # Cleanup empty "foo" section.
    # Regular expression has a trailing period to avoid testing against
    # other sections that share the same prefix (e.g. "foo" vs "food").
    if ! git config --get-regexp '^foo\.'; then
        git config --remove-section "foo" 2> /dev/null;
    fi;
    
    从.git/config中删除设置时如何避免空节

    这应该通过Gti 2.18(2018年第二季度)进行修正:
    git config--unset a.b
    ”,当“
    a.b
    ”是另一个空节“
    a
    ”中的最后一个变量时,留下了一个空节“a”,更糟糕的是,随后的“
    git config a.c值
    ”没有重用该变量 空shell并创建了一个新的shell。
    这些问题已(部分)得到纠正

    参见,,,(2018年4月9日)和,,,(2018年4月3日)的作者。
    (于2018年5月8日合并)

    git config--unset:删除空节(在常见情况下) 在删除节标题时不删除节标题的原始推理 最后一个条目是这样的:用户可以添加关于 该部分,或关于其中的条目,以及如果有其他 如果有评论,我们不知道是否应该删除它们

    特别是,一个炮制的例子是这样呈现的 (并添加到t1300中):

    在本例中,
    git config--unset section.key的理想情况是
    只留下第一行,因为所有其他注释
    现在已经过时了

    然而,这是不可行的,除非添加一个完整的自然语言 处理模块到Git,这似乎不仅要做很多工作,而且要花很多时间 完全不合理的特性(对大多数用户几乎没有好处)

    现在,这个问题的真正关键是:大多数用户不编辑他们的 配置文件!在他们的用例中,配置如下所示 相反:

    。。。很明显,如果删除条目会发生什么:整个部分都会消失


    我没有做补丁。这应该用Git 2.18(2018年第二季度)来修复。看见
    # 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
    
    [section]
        key = value