【问题标题】:How to retrieve the history of a file?如何检索文件的历史记录?
【发布时间】:2012-01-04 14:11:06
【问题描述】:

我遇到了另一个 libgit2 问题,非常感谢您的帮助。

我正在尝试检索文件历史记录,即更改此文件的提交列表。而且它似乎很不合常规......据我所知,没有任何功能。

我能想出的唯一方法是使用修订遍历 API 来迭代修订,检查附加到提交的树对象并在那里搜索给定文件,如果找到,将提交添加到我的列表中,否则继续下一次提交.

但它看起来对我来说不是最理想的......

可能有没有其他方法,例如,直接查看 .git 文件夹并在那里获取所需的信息?

提前非常感谢!

【问题讨论】:

    标签: git history libgit2 libgit2sharp


    【解决方案1】:

    但它看起来对我来说不是最理想的......

    您的方法是正确的。请注意,您将不得不与之抗争:

    • 普通重命名(相同的对象哈希,不同的树条目名称)
    • 在同一提交中发生重命名和内容更新(不同的哈希,不同的树条目名称。需要文件内容分析和比较功能,这在 libgit2 中不可用)
    • 多个父级历史记录(已合并的两个分支,其中同一文件以不同方式修改)

    可能有没有其他方法,例如,直接查看 .git 文件夹并在那里获取所需的信息?

    尽管了解 .git 文件夹布局总是很值得花时间,但恐怕这对您解决这个特定的文件历史问题没有帮助。

    注意:这个问题与这个 libgit2sharp 问题非常接近:How to get the last commit that affected a given file?

    更新

    Pull 请求 #963 添加了这个功能。

    它从LibGit2Sharp.0.22.0-pre20150415174523 预发布 NuGet 包开始可用。

    【讨论】:

    • 其实是同一个问题:)
    【解决方案2】:

    这主要是在libgit2的issues/495后面。
    即使它是 implemented in libgit2sharpPR 963,对于 milestone 22),它仍然在 libgit2 本身中“待命”。

    issues/3041: Provide log functionality wrapping the revwalk 中记录了该问题。
    问题中提到的方法在this libgit2sharp example 中使用,可以使用libgit2 适应C。它仍然是当前的解决方法,等待 3041 的决议。

    【讨论】:

    • 谢谢,但这并不比空令牌所说的多。他还提供了一个链接。 + 我也用谷歌.. 我希望 shyitok 谈到的要点。 - 我实现了它,但它没有跟随重命名 - 它基于 git log 示例代码
    • 我同意,这是 4 年后的更新。如前所述,实施仍有待商榷。
    • @Daij-Djan nulltoken 提到了我刚才提到的同一个 PR:LibGit2Sharp.0.22.0-pre20150415174523 有这个功能。
    • 我也想检索特定文件的文件历史记录。我已经尝试了下一个代码,但是历史 IEnumerable 中没有任何提交。您能否提供一些信息如何解决这个问题。存储库 repo = new Repository(repoPath); IEnumerable history = repo.Commits.QueryBy(filePath, new FollowFilter { SortBy = CommitSortStrategies.Topological });
    • @Odrai 我明白了。 github.com/libgit2/rugged/pull/531 似乎仍在“进行中”。
    【解决方案3】:

    如果使用 C#,此功能已添加到 LibGit2Sharp 0.22.0 NuGet Package (Pull Request 963)。您可以执行以下操作:

    var fileHistory = repository.Commits.QueryBy(filePathRelativeToRepository);
    foreach (var version in fileHistory)
    {
        // Get further details by inspecting version.Commit
    }
    

    在我的Diff All Files VS Extension(它是开源的,因此您可以查看代码)中,我需要获取文件的先前提交,以便查看在给定提交中对文件所做的更改。这就是我检索文件先前提交的方式:

    /// <summary>
    /// Gets the previous commit of the file.
    /// </summary>
    /// <param name="repository">The repository.</param>
    /// <param name="filePathRelativeToRepository">The file path relative to repository.</param>
    /// <param name="commitSha">The commit sha to start the search for the previous version from. If null, the latest commit of the file will be returned.</param>
    /// <returns></returns>
    private static Commit GetPreviousCommitOfFile(Repository repository, string filePathRelativeToRepository, string commitSha = null)
    {
        bool versionMatchesGivenVersion = false;
        var fileHistory = repository.Commits.QueryBy(filePathRelativeToRepository);
        foreach (var version in fileHistory)
        {
            // If they want the latest commit or we have found the "previous" commit that they were after, return it.
            if (string.IsNullOrWhiteSpace(commitSha) || versionMatchesGivenVersion)
                return version.Commit;
    
            // If this commit version matches the version specified, we want to return the next commit in the list, as it will be the previous commit.
            if (version.Commit.Sha.Equals(commitSha))
                versionMatchesGivenVersion = true;
        }
    
        return null;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-03-09
      • 2021-03-18
      • 2014-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-28
      相关资源
      最近更新 更多