gpt4 book ai didi

linux - 验证文件仅在两次提交之间的末尾被修改

转载 作者:行者123 更新时间:2023-12-03 10:00:17 25 4
gpt4 key购买 nike

我的存储库中有一个文件,我只想在末尾插入。
我可以使用哪个linux命令来验证两次git commit之间的文件仅在最后修改?

最佳答案

没有Git命令可以执行此操作,但是您可以在Bash(或您选择的shell)中编写脚本:
您可以创建执行此操作的脚本:

#!/bin/bash

# Create a file which is a version of your file of interest before last commit

git --no-pager show HEAD~:<file> > original

# Get the number of lines for that file and call it n:

n=$(cat original | wc -l)

# Test whether the last commit modified the file other than
# by adding content at the end

[[ "$(diff <(cat original) <(head -$n <file>))" == "" ]]

# Finally, clean up by deleting the temp file:

rm original
如果仅在文件末尾(或根本没有修改),则返回0;如果要阻止的修改(即末尾),则返回1。
当然,您需要将 <file>替换为目标文件的路径。
如果您感兴趣的2个提交不是最后2个提交,则可以将脚本修改为更通用。例如,在这里,我将对2个提交使用 <hash1><hash2>,但是您可以使用任何方便识别任何2个提交的引用:
#!/bin/bash

# Create a file which is a version of your file of interest at the first commit
# you care about (using its hash):

git --no-pager show <hash1>:<file> > original

# Create a file which is a version of your file of interest at the second commit
# you care about (using its hash):

git --no-pager show <hash2>:<file> > next

# Get the number of lines for the first of these files and call it n:

n=$(cat original | wc -l)

# Test whether the commit with hash2 modified the file other
# than by adding content at the end

[[ "$(diff <(cat original) <(head -$n next))" == "" ]]

# Finally, clean up by deleting the 2 temp files:

rm original next
另外,为了方便起见,由于您可能需要经常在管道中进行测试,因此可以创建一个函数并将其添加到 .bashrc或等效项中:
function check {
local n
git --no-pager show "$2":"$1" > original
git --no-pager show "$3":"$1" > next
n=$(cat original | wc -l)
[[ "$(diff <(cat original) <(head -$n next))" == "" ]]
rm original next
}
然后可以将其用于:
check <file-path> <commit-hash1> <commit-hash2>

关于linux - 验证文件仅在两次提交之间的末尾被修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64286676/

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