gpt4 book ai didi

git - pathspec 应该如何工作?

转载 作者:太空狗 更新时间:2023-10-29 14:39:57 27 4
gpt4 key购买 nike

我在使用带有路径规范的 git-status 时观察到一个奇怪的行为。我想听听您的意见,看看这是预期的行为,还是 git 中的未定义行为。

初始设置

$ mkdir main && cd main
$ git init .
Initialized empty Git repository in d:/temp/main/.git/
$ touch test[1].txt
$ touch test[2].txt

下面几个是可以理解的

$ # 1. escaped square brackets
$ git status test\[1\].txt --short
?? test[1].txt

$ # 2. escaped square brackets with range
$ git status test\[[1-9]\].txt --short
?? test[1].txt
?? test[2].txt

$ # 3. unmatched range, with what looks like a fallback to literal comparison
$ git status test[1].txt --short
?? test[1].txt

针对意外行为的额外设置

$ touch test1.txt

意外行为

$ # 4. matched range, so no fallback to literal comparison (this one looks OK)
$ git status test[1].txt --short
?? test1.txt

$ # 5. escaped square brackets => I would expect only test[1].txt to be returned
$ git status test\[1\].txt --short
?? test1.txt
?? test[1].txt

$ # 6. escaped square brackets with range => I would expect only test[1].txt
$ # and test[2].txt to be returned
$ git status test\[[1-9]\].txt --short
?? test1.txt
?? test[1].txt
?? test[2].txt

$ # 7. only escaping the last square bracket
$ # result looks similar to the 5th case
$ git status test[1\].txt --short
?? test1.txt
?? test[1].txt

额外设置带来更多乐趣

$ git add test1.txt
$ rm test1.txt
$ touch test2.txt

更多意外行为

$ # 8. ???
$ git status test[1].txt --short
AD test1.txt
?? test[1].txt

$ # 9. We lost test1.txt ???
$ git status test[1-2].txt --short
?? test2.txt

$ # Woo... Should this really work?
$ git status test[*.txt --short
AD test1.txt
?? test2.txt
?? test[1].txt
?? test[2].txt

我有点困惑。我已阅读与 pathspec 相关的 Git 文档而且没有那么详细。

谁能帮我理解背后的逻辑?

最佳答案

这里有很多事情要讨论,但我会尽量关注:1.这背后的逻辑,2.它如何修改行为。

逻辑

大部分路径扩展 都是由 shell 完成的(因此我的评论)。有些是由 git 完成的,当它有它需要的时候。

一些测试

设置

我用这个程序来调查这个问题:

include <stdio.h>

int
main(int argc, char **argv)
{
int i;

for (i = 1; i < argc; i++) {
puts(argv[i]);
}
}

我知道,这是非常高技能的编程。

测试

我们现在可以看看发生了什么,看看 shell 如何修改 git 接收的内容:

第 1、2、3、4 点:一切正常,运行小程序会给您相同的结果。

$ ./a.out test\[1\].txt test\[[1-9]\].txt test[1].txt
test[1].txt
test[1].txt
test[2].txt
test[1].txt

第 5、6、7 点:这次由 Git 处理,行为并不奇怪(同时进行 glob 和文字比较)

$ ./a.out test\[1\].txt test\[[1-9]\].txt test[1\].txt
test[1].txt
test[1].txt
test[2].txt
test[1].txt

第8、9、10点:嗯,根据我们之前看到的,已经不奇怪了。对于 9.,没有 bash 比较与 test1.txt 匹配(已删除,因此,...已删除)

$ ./a.out test[1].txt                         
test[1].txt

$ ./a.out test[1-2].txt
test2.txt

$ ./a.out test[*.txt
test[1].txt
test[2].txt

结论

如果你想测试 Git 处理 pathspec 的方式,你应该用双引号将你的路径括起来:

$ ./a.out "test[*.txt" "test[1\].txt"
test[*.txt
test[1\].txt

希望对你有帮助,

关于git - pathspec 应该如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11314419/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com