尽管文件内容没有改变,但git克隆之后zip文件的Sha256哈希是不同的? [英] Sha256 hash of a zip file is different after git clone despite file contents not changing?
问题描述
我有一个问题,我的自动编译需要压缩目录的内容,并且压缩zip文件。很容易。
然而,下一轮自动编译需要压缩相同的内容,并且需要压缩zip文件以查看是否有任何源代码发生了变化。 / p>
本地我运行了以下命令,并且每次都使用相同的散列生成一个zip文件(因为我没有更改任何代码):
zip -q -r -X my-directory.zip my_directory / * --exclude.gitignorerequirements.txt* __pycache __ / ** / \infrastructure / *
sha256sum my-directory.zip | awk'{print $ 1}'> my-directory.zip.hash
cat my-directory.zip.hash
我的构建运行相同的命令,但是在每次运行开始时,它会从GitHub中检查代码(因为构建在Docker容器中运行),尽管没有代码更改,但仍会产生不同的哈希。
我通过删除回购和重新克隆在本地重新创建了问题。
任何想法?我认为它是元数据或某种排序,但尝试了不同的排除命令,但没有运气。 解决方案
您当前的方法看起来很容易出错。
它依赖仔细构造的 - 排除
参数,
并假定没有意外的文件。
这非常脆弱。
更好的方法是使用 git archive
命令创建zip:
git archive HEAD -o my-directory.zip my_directory
这将忽略忽略的文件和其他不属于版本库的文件和其他文件。
这应该保证一致的结果。
请注意, git archive
会添加注释commit id。
如果由于某种原因想要删除它,可以通过运行以下命令来完成:
zip - z my-directory.zip< / dev / null
I have an issue whereby my automated build needs to zip the contents of a directory and Sha256sum the zip file. Easy enough.
However, the next run of the automated build needs to zip the same contents and Sha256sum the zip file in order to see whether any source code has changed.
Locally I've ran the following commands and generated a zip with the same hash each time (expected, as I haven't changed any of the code):
zip -q -r -X my-directory.zip my_directory/* --exclude ".gitignore" "requirements.txt" "*__pycache__/*" "*/\infrastructure/*"
sha256sum my-directory.zip | awk '{ print $1 }' > my-directory.zip.hash
cat my-directory.zip.hash
My build runs the same commands, but at the start of each run it is doing a checkout of the code from GitHub (as the build runs inside a docker container), which results in a different hash despite no code changes.
I've re-created the problem locally by deleting the repo and re-cloning.
Any ideas? I am thinking it's metadata or some sort but have tried different exclusion commands without luck.
Your current method looks error prone.
It relies on carefully constructed --exclude
parameters,
and assumes no unexpected files.
That's very fragile.
A better way would be to use the git archive
command to create the zip:
git archive HEAD -o my-directory.zip my_directory
This will disregard ignored files and other files not part of the repository. This should guarantee consistent results.
Note however that git archive
adds as comment the commit id.
If for some reason you want to remove that, you can do so by running this additional command:
zip -z my-directory.zip < /dev/null
这篇关于尽管文件内容没有改变,但git克隆之后zip文件的Sha256哈希是不同的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!