c# - LibGit2Sharp 查找 pull 后更新/添加/删除的文件

标签 c# git libgit2sharp

运行 repo.Network.Pull() 命令后,我希望能够查看哪些文件已添加到存储库、在存储库中更改以及从存储库中删除。我只需要文件的文件路径以及它是添加/更新还是删除。

有没有简单的方法来做到这一点?我试过研究 Diff.Compare() 但我不确定这是否是正确的方法。

最佳答案

LibGit2Sharp 0.21.0.176

这是一个 libGit2 示例,它遍历当前的提交树并获取更改的文件和更改的类型。

Git 版本:

git log --name-status --pretty=oneline

1d9d4bb881f97f5d3b67741a893f238e7221e2b1 Updated readme with fork info
M       README.md
58cc5c41963d5ff68556476158c9c0c2499e061c Update Makefile for PE32+ (platform:x64) assemblies
M       Makefile
M       README.md
a7823c1c0a737c5218d33691f98828c78d52130b Fix Makefile for 64-bit native lib and add README.md
M       Makefile
A       README.md
ea7e6722f67569cb9d7a433ff2c036fc630d8561 Update solution files.
M       mono-curses.csproj
M       mono-curses.sln
05dbe6e18895d1037ce333b0a1f652eeae3f8b33 Fix resize handling.
M       attrib.c
M       gui.cs

libGit2 版本:

    var repo = new LibGit2Sharp.Repository ("/your/repo/path");
    foreach (Commit commit in repo.Commits) {
        foreach (var parent in commit.Parents) {
            Console.WriteLine ("{0} | {1}", commit.Sha, commit.MessageShort);
            foreach (TreeEntryChanges change in repo.Diff.Compare<TreeChanges>(parent.Tree,
            commit.Tree)) {
                Console.WriteLine ("{0} : {1}", change.Status, change.Path);
            }
        }
    }

输出:

1d9d4bb881f97f5d3b67741a893f238e7221e2b1 | Updated readme with fork info
Modified : README.md
58cc5c41963d5ff68556476158c9c0c2499e061c | Update Makefile for PE32+ (platform:x64) assemblies
Modified : Makefile
Modified : README.md
a7823c1c0a737c5218d33691f98828c78d52130b | Fix Makefile for 64-bit native lib and add README.md
Modified : Makefile
Added : README.md
ea7e6722f67569cb9d7a433ff2c036fc630d8561 | Update solution files.
Modified : mono-curses.csproj
Modified : mono-curses.sln
05dbe6e18895d1037ce333b0a1f652eeae3f8b33 | Fix resize handling.
Modified : attrib.c
Modified : gui.cs

在直接回答您的问题时,只需从 Commits 枚举器中获取第一个提交并将其树与其父项(由于 merge 可能有多个父项)与我循环当前所有提交的示例进行比较分支机构。

关于c# - LibGit2Sharp 查找 pull 后更新/添加/删除的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30945725/

相关文章:

windows - 为什么powershell和cmd.exe使用的git配置文件在C :\c\Users\xxx\. gitconfig?

git - 可移植 Git 和 GitHub : SSH Keys

windows - 是否有人使用 libgit2、libgit2sharp、Ngit 来替代 msysgit 的 Windows?

libgit2sharp - 如何计算前支或后支

c# - 十进制 SQL Server EF C# 的参数超出范围异常

c# - 使用 CaSTLe.Windsor 和 Polly 响应 429 异常( throttle )

c# - 在 C# 中,当我调用 BeginXXX 时有两个线程

java - Protobuf如何多次接收字符串和对象

macos - Mac OS X 10.10 merge 工具 git bug

libgit2sharp - 标记为 'Deleted' 的文件在 LibGit2Sharp 中报告为 'Missing'