代码之家  ›  专栏  ›  技术社区  ›  nanounanue

如何使用python获取特定文件的最后一次提交?

  •  1
  • nanounanue  · 技术社区  · 6 年前

    我试过了 GitPython 但是我只是得到 实际的 提交Git哈希。

    import git
    repo = git.Repo(search_parent_directories=True)
    repo.head.commit.hexsha
    

    但是,为了可移植性,我希望存储特定文件的git commit散列,即等效于此命令(使用 git )

    git log -n 1 --pretty=format:%h -- experiments/test.yaml
    

    有可能实现 吉特帕森 ?

    1 回复  |  直到 6 年前
        1
  •  0
  •   VonC    6 年前

    像这样的问题 how do I get sha key for any repository' file. 指向 the Tree object ,作为对Git树递归遍历的访问,提供对所有元数据(包括sha1哈希)的访问。

    self.assertEqual(tree['smmap'], tree / 'smmap')          # access by index and by sub-path
    for entry in tree:                                         # intuitive iteration of tree members
        print(entry)
    blob = tree.trees[0].blobs[0]                              # let's get a blob in a sub-tree
    assert blob.name
    

    blob.hexsha 会是一团的沙。