此解决方案是 Andy Pryke 在 "How to compute the git hash-object of a directory?" 中出色回答的扩展。它适用于存在依赖关系的任何类型的文件(源代码、出版物、游戏资产)。
首先,用您选择的语言编写一个脚本,返回与指定顶级文件相关的所有文件,每行一个文件名。例如,
% get_referenced_files.pl book1.ditamap
book1.ditamap
book1/topicA.dita
book1/topicB.dita
book1/figureC.png
shared/shared_topicH.dita
%
接下来,将此文件列表通过管道传递给git hash-object --stdin-paths 命令,该命令会为从 STDIN 读取的每个文件名返回相应的 SHA:
% get_referenced_files.pl book1.ditamap | git hash-object --stdin-paths
6b7d90792b9b9f40d553b19808978a78ba4994e5
03e54ce5a8880abed1daacb519122148f6b08373
aef9d70aac4fc5bde8527ca48e6cb44e4cc7083c
e93f704c031883f829a0c5ce4e01d16983ce709a
8d844111208f31be33855310663d625bdf4f37f6
%
最后,将此 SHA 列表传递给 git hash-object --stdin 命令,该命令计算从 STDIN 读取的文字字符串内容的 SHA(在这种情况下,每个文件的单个 SHA 值):
% get_referenced_files.pl book1.ditamap | git hash-object --stdin-paths | git hash-object --stdin
f30c715c5bc89846104d336b1ce7a5b95efa7fd8
%
这个最终的 SHA 值代表指定顶级文件及其所有依赖文件的唯一“指纹”。如果这个指纹发生变化,那么文件集中的某处发生了变化。
在我的 perl 脚本中,我使用以下单行代码来获取特定出版物的 SHA 指纹:
my $sha = (`get_referenced_files.pl "$top_file" | git hash-object --stdin-paths | git hash-object --stdin` =~ s!\n$!!r);