【问题标题】:How to compute the git hash-object of a set of files?如何计算一组文件的 git 哈希对象?
【发布时间】:2021-07-31 01:00:44
【问题描述】:

我的 Git 存储库包含多个出版物的文件。每个出版物都有一个顶级文件,该文件引用了各种较低级别的文件(章节、主题、图像文件)。例如,

book1.ditamap       (.ditamap == top-level map file)
book1/topicA.dita   (.dita == lower-level topic file)
book1/topicB.dita
book1/figureC.png    (image for figure)
book2.ditamap
book2/topicD.dita
book2/topicE.dita
book2/figureF.png
shared/shared_topicG.dita
shared/shared_topicH.dita

我想为给定出版物中的所有文件(顶级文件和所有依赖文件)计算 Git SHA。不幸的是,它不像为目录计算 SHA 那样简单,因为出版物会重复使用其他出版物的文件,文件分散在共享目录中,等等。

我该怎么做?

【问题讨论】:

    标签: git hash


    【解决方案1】:

    此解决方案是 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);
    

    【讨论】:

      猜你喜欢
      • 2011-11-05
      • 2011-08-26
      • 2019-05-31
      • 1970-01-01
      • 2010-09-11
      • 2014-07-29
      • 1970-01-01
      相关资源
      最近更新 更多