簡體   English   中英

如何檢索文件的歷史記錄?

[英]How to retrieve the history of a file?

我得到了另一個libgit2問題,非常感謝你的幫助。

我正在嘗試檢索文件歷史記錄,即更改此文件的提交列表。 而且它似乎非常傳統......據我所見,沒有任何功能。

我能想到的唯一方法是使用版本控制API迭代修訂版本,檢查附加到提交的樹對象並在那里搜索給定文件,如果找到,則將提交添加到我的列表中,否則繼續下一次提交。

但它對我來說似乎不是最理想的......

可能是有任何其他方法,例如,直接查看.git文件夾並獲取所需信息嗎?

提前謝謝了!

但它對我來說似乎不是最理想的......

你的方法是正確的。 要小心你必須要反對:

  • 普通重命名(相同的對象哈希,不同的樹條目名稱)
  • 在同一個提交中發生重命名和內容更新(不同的哈希,不同的樹條目名稱。需要文件內容分析和比較功能,這在libgit2中不可用)
  • 多個父級歷史記錄(已合並的兩個分支,並且以不同方式修改了同一文件)

可能是有任何其他方法,例如,直接查看.git文件夾並獲取所需信息嗎?

雖然理解.git文件夾布局總是花費很多時間,但我擔心這不會幫助您解決這個特定的文件歷史問題。

注意:這個問題與libgit2sharp問題非常接近: 如何獲取影響給定文件的最后一次提交?

更新

拉請求#963添加了這個功能。

LibGit2Sharp.0.22.0-pre20150415174523預發布NuGet包以來可用。

這主要是在libgit2的問題/ 495中。
即使它是在libgit2sharpPR 963里程碑22 )中實現的,它仍然在libgit2本身“搶奪”。

問題/ 3041中記錄了該問題:提供包裝revwalk日志功能
問題中提到的方法在這個libgit2sharp示例中使用 ,可以使用libgit2適應C語言。 在3041號決議之前,它仍然是當前的解決方法。

如果使用C#,則此功能已添加到LibGit2Sharp 0.22.0 NuGet包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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM