gpt4 book ai didi

git diff-files 输出在 git status 之后发生变化

转载 作者:太空狗 更新时间:2023-10-29 12:52:05 25 4
gpt4 key购买 nike

我有一个脚本,update.py ,它会下载我的 git 存储库中跟踪的文件的新版本:

$ python update.py
Doing work...
Done
$ git status
On branch my-branch
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified: foo.txt
modified: bar.txt
modified: baz.txt

no changes added to commit (use "git add" and/or "git commit -a")

有时,下载的文件与 HEAD 中已有的文件相同, 所以下载后工作目录是干净的:

$ python update.py
Doing work...
Done
$ git status
On branch my-branch
nothing to commit, working directory clean

但是,我发现 git diff-files替换文件时似乎会感到困惑,即使它们的内容相同:

$ python update.py
Doing work...
Done
$ git diff-files
:100644 100644 ffa91f655007c56f209cf15fee13c55991a76e18 0000000000000000000000000000000000000000 M foo.txt
:100644 100644 dc05558729c3c94a088aa63da3bbd8f1213b8cf3 0000000000000000000000000000000000000000 M bar.txt
:100644 100644 002cc3f53dc64b89b1b91adbb6fe61035ba9e832 0000000000000000000000000000000000000000 M baz.txt
$ git status
On branch my-branch
nothing to commit, working directory clean
$ git diff-files
$

在上面的代码片段中:

  1. 我运行 update.py,它替换了 foo.txt , bar.txt , 和 baz.txt从其他地方下载的具有相同副本的文件。
  2. git diff-files根据 git diff man page 中描述的原始输出格式,错误地报告这三个文件已在工作树中就地编辑。 .
  3. git status正确报告没有任何变化。
  4. git diff-files , 在 git status 之后运行,现在报告说什么都没有改变。

运行后update.py , git diff-files将继续错误地报告更改,直到我运行 git status , 之后它再次运行。

这是怎么回事?为什么是git diff-files在没有变化时报告变化?


如果你好奇为什么这会给我带来麻烦,这里有一些更多的背景信息:

我有另一个脚本,update_and_commit_if_needed.py执行以下操作:

  1. 运行 update.py .
  2. 如果git diff-files返回零,工作树是干净的,update.py没有改变任何东西。退出。
  3. 否则,工作树是脏的。提交更改。

我在 update_and_commit_if_needed.py 中看到了一个奇怪的失败:我会进入第三步,然后 git commit会提示有 nothing to commit, working directory clean .在追踪该错误时,我发现了 git diff-files 的这种奇怪行为.

我在 OS X 10.11.4 (15E65) 上使用 git 版本 2.5.0。


编辑 1:我找到了重现此行为的简单方法:

$ git diff-files
$ git status
On branch my-branch
nothing to commit, working directory clean
$ cp foo.txt ~
$ mv ~/foo.txt .
$ git diff-files
:100755 100755 20084b5d6da359748f62c259c24f2b9cc2359780 0000000000000000000000000000000000000000 M foo.txt
$ git status
On branch my-branch
nothing to commit, working directory clean
$ git diff-files
$

编辑 2: 正如评论中所建议的,我尝试反转 core.trustctimecore.ignoreStat从他们的默认值。在这种情况下,这似乎并没有改变 git 的行为。

最佳答案

git diff-index 实际上并不检查工作树中文件的内容。相反,它使用文件的统计信息并将其与索引进行比较。事实上,diff-index man page notes :

As with other commands of this type, git diff-index does not actually look at the contents of the file at all. So maybe kernel/sched.c hasn’t actually changed, and it’s just that you touched it. In either case, it’s a note that you need to git update-index it to make the index be in sync.

如注释所示,可以通过在 diff-files 之前运行 git update-index --refresh 来更新索引的统计条目。 man page for update-index elaborates :

--refresh does not calculate a new sha1 file or bring the index up-to-date for mode/content changes. But what it does do is to "re-match" the stat information of a file with the index, so that you can refresh the index for a file that hasn’t been changed but where the stat entry is out of date.

For example, you’d want to do this after doing a git read-tree, to link up the stat index details with the proper files.

diff-files 之前运行 update-index --refresh 消除我描述的症状,解决问题。

关于git diff-files 输出在 git status 之后发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36367190/

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