Git基于内容而没有文件,因此我目前了解以下行为,但我想知道是否有特殊选项或黑客来检测这样的事情:
git init
mkdir -p foo/bar
echo "test" foo/a.txt
echo "test2" foo/bar/b.txt
git add -A
git commit -m "test"
rm -fr foo
git add -A
git commit -m "delete whole dir"
git log --name-status
Run Code Online (Sandbox Code Playgroud)
当我检查记录,Git将无法说清楚我foo被删除,但所有的文件foo/a.txt和foo/bar/b.txt被删除
commit d1513a9b36cd546371a194e798566c49e779e3a9
Date: Tue Sep 8 16:58:21 2015 +0200
delete whole dir
D foo/a.txt
D foo/bar/b.txt
commit 135f7ae52dfddcee5eeb7bdfa9f0d5c924fed3af
Date: Tue Sep 8 16:58:10 2015 +0200
test
A foo/a.txt
A foo/bar/b.txt
Run Code Online (Sandbox Code Playgroud)
因此,如果我创建以下提交:
mkdir -p foo/bar
echo "test" > foo/a.txt
echo "test2" > foo/bar/b.txt
echo "test3" > foo/bar/c.txt
git add -A
git commit -m "test2"
rm -f foo/a.txt foo/bar/b.txt
git add -A
git commit -m "delete just 2 files"
git log --name-status
Run Code Online (Sandbox Code Playgroud)
name-status提交delete whole dir和delete just 2 files类似之间
commit 92564fb59464fd6bba2766a6d488c2ff8ca967ea
Date: Tue Sep 8 17:03:09 2015 +0200
delete just 2 files
D foo/a.txt
D foo/bar/b.txt
commit 3b13f27e960c4ec66464e8a1e0d23f038a872564
Date: Tue Sep 8 17:02:50 2015 +0200
test2
A foo/a.txt
A foo/bar/b.txt
A foo/bar/c.txt
Run Code Online (Sandbox Code Playgroud)
删除整个目录时有没有办法检测差异?
假设存储库中的示例树结构为:
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: a/b/c/d/e/q.txt
new file: a/b/c/m.txt
new file: a/b/c/n.txt
new file: a/b/f/o.txt
new file: a/b/f/p.txt
new file: a/b/k.txt
new file: a/b/z.txt
new file: a/x.txt
new file: a/y.txt
Run Code Online (Sandbox Code Playgroud)
让我们进行以下提交:
commit 0bb4d4d50072c1eac1c3cb2b14b670deba8ee31b
Author: Site User <user@site.com>
Date: Tue Sep 8 19:27:58 2015 +0100
removed a/b/c/d/e/q.txt
D a/b/c/d/e/q.txt
Run Code Online (Sandbox Code Playgroud)
-
commit 3b53f16eb2fd7d3d605180ccabcfa71eb9e9225a
Author: Site User <user@site.com>
Date: Tue Sep 8 19:28:55 2015 +0100
removed a/b/c/m.txt and a/b/c/n.txt (full a/b/c)
D a/b/c/m.txt
D a/b/c/n.txt
Run Code Online (Sandbox Code Playgroud)
-
commit 3af8ace473944996fb8b21135106360305e8b89a
Author: Site User <user@site.com>
Date: Tue Sep 8 19:30:30 2015 +0100
added a/b/g/w.txt
A a/b/g/w.txt
Run Code Online (Sandbox Code Playgroud)
-
Commit 3b53f16eb2fd7d3d605180ccabcfa71eb9e9225a 是看到文件夹“a/b/c”消失的文件夹,因为其中的最后一个文件被删除。
要在删除文件夹 a/b/c 时查找提交的 SHA#,您可以使用以下组合来查找涉及“a/b/c”文件夹的最后一次提交:
#> git log --name-status -- a/b/c
Run Code Online (Sandbox Code Playgroud)
和
#> git ls-tree -r [commit] -- a/b/c
Run Code Online (Sandbox Code Playgroud)
就像是:
for cmt in $(git log --pretty=%H -- a/b/c); \
do X=$(git ls-tree -r "${cmt}" -- a/b/c); \
[[ -z "${X}" ]] && echo "The folder a/b/c has been deleted in commit: ${cmt}"; \
done
Run Code Online (Sandbox Code Playgroud)
输出:
The folder a/b/c has been deleted in commit: 3b53f16eb2fd7d3d605180ccabcfa71eb9e9225a
Run Code Online (Sandbox Code Playgroud)
以简单的BASH脚本的形式(我将其命名为deleted.sh,应该具有可执行权限):
#!/bin/bash
P="$1"
GIT=$(which git)
for cmt in $($GIT log --pretty=%H -- "${P}"); do
X=$($GIT ls-tree -r "${cmt}" -- "${P}");
[[ -z "${X}" ]] && echo "The folder a/b/c has been deleted in commit: ${cmt}";
done
Run Code Online (Sandbox Code Playgroud)
用法:
./deleted.sh a/b/c
Run Code Online (Sandbox Code Playgroud)
输出:
The folder a/b/c has been deleted in commit: 3b53f16eb2fd7d3d605180ccabcfa71eb9e9225a
Run Code Online (Sandbox Code Playgroud)
它是如何工作的
历史上第一次提交,树中没有与文件夹路径匹配的文件,应该可以。
这就是 shell 脚本正在做的事情。
它在历史记录中向后迭代,检索与文件夹中任何文件相关的所有 SHA#,然后在这些提交中找到第一个,在 Git 树中没有任何与提供的路径匹配的文件。
事实上,该提交出现在要检查的列表中,确保其中存在涉及该文件夹的更改。
事实上,没有与树中的文件夹匹配的文件(过滤后的“ls-tree”返回空字符串),确保这是该文件夹中最后一个文件已被删除的提交。