Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Git 如何检索文件的历史记录?_Git_History_Libgit2_Libgit2sharp - Fatal编程技术网

Git 如何检索文件的历史记录?

Git 如何检索文件的历史记录?,git,history,libgit2,libgit2sharp,Git,History,Libgit2,Libgit2sharp,我还有一期libgit2,非常感谢您的帮助 我正在尝试检索文件历史记录,即更改此文件的提交列表。这似乎很不传统。。。据我所知,这是没有功能的 我能想到的唯一方法是使用修订行走API来迭代修订,检查附加到提交的树对象并在那里搜索给定的文件,如果找到,将提交添加到我的列表中,否则继续下一次提交 但这对我来说似乎不是最理想的 是否有其他方法,例如,直接查看.git文件夹并从中获取所需信息 非常感谢 但这对我来说似乎不是最理想的 你的方法是正确的。请注意,您将不得不与以下因素作斗争: 普通重命名(相同

我还有一期libgit2,非常感谢您的帮助

我正在尝试检索文件历史记录,即更改此文件的提交列表。这似乎很不传统。。。据我所知,这是没有功能的

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

但这对我来说似乎不是最理想的

是否有其他方法,例如,直接查看.git文件夹并从中获取所需信息

非常感谢

但这对我来说似乎不是最理想的

你的方法是正确的。请注意,您将不得不与以下因素作斗争:

  • 普通重命名(相同的对象哈希,不同的树条目名称)
  • 重命名和内容更新发生在同一个提交中(不同的散列、不同的树条目名称。需要文件内容分析和比较功能,这在libgit2中不可用)
  • 多个父历史记录(两个已合并且同一文件已以不同方式修改到其中的分支)
是否有其他方法,例如,直接查看.git文件夹并从中获取所需信息

尽管理解.git文件夹布局始终是一个很好的时间,但恐怕这对解决这个特定的文件历史记录问题没有帮助

注意:这个问题与libgit2sharp问题非常接近:

更新 拉取请求添加了这一功能


它是从LibGit2Sharp.0.22.0-pre2015041574523开始提供的。预发布NuGet包。

这主要是在libgit2的后面。
尽管它是(,for),但在libgit2中它仍然是“待价而沽”的

该问题记录在中。
问题中提到的方法在中使用过,并且可以使用libgit2将其改编为C。它仍然是当前的解决方法,等待3041的解决。

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

var fileHistory = repository.Commits.QueryBy(filePathRelativeToRepository);
foreach (var version in fileHistory)
{
    // Get further details by inspecting version.Commit
}
在my中(这是开源的,您可以查看代码),我需要获取文件的上一次提交,以便查看在给定提交中对文件所做的更改。以下是我检索文件先前提交的方式:

/// <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;
}
//
///获取文件的上一次提交。
/// 
///存储库。
///相对于存储库的文件路径。
///提交sha以从中开始搜索以前的版本。如果为null,将返回文件的最新提交。
/// 
私有静态提交GetPreviousCommitToFile(存储库存储库,字符串filePathRelativeToRepository,字符串commitSha=null)
{
bool versionMatchesGiveInversion=false;
var fileHistory=repository.Commits.QueryBy(filePathRelativeToRepository);
foreach(fileHistory中的var版本)
{
//如果他们想要最新的提交,或者我们找到了他们想要的“上一次”提交,请返回它。
if(string.IsNullOrWhiteSpace(commitSha)| | versionMatchesGiveVersion)
返回version.Commit;
//如果此提交版本与指定的版本匹配,我们希望返回列表中的下一个提交,因为它将是上一个提交。
if(version.Commit.Sha.Equals(commitSha))
VersionMatchesGiveInversion=true;
}
返回null;
}

事实上,这是完全相同的问题:)谢谢,但这并不比null token说的多多少。他还提供了一个链接我也用过谷歌。。我希望shyitok所说的要点我实现了它,但它没有遵循重命名-它基于git日志示例代码。我同意,这是一个更新,4年后。如前所述,该实现仍有待抓取。@Daij Djan nulltoken提到了我刚才提到的同一个PR:LibGit2Sharp.0.22.0-pre20150415174523具有此功能。我还想检索特定文件的文件历史记录。我已经尝试了下一个代码,但在历史记录IEnumerable中没有任何提交。你能提供一些如何解决这个问题的信息吗。Repository repo=新存储库(repoPath);IEnumerable history=repo.Commits.QueryBy(文件路径,新的FollowFilter{SortBy=commitportstrategies.Topological})@我明白了。它似乎仍在“进行中”。