Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LibGit2Sharp Find what files were updated/added/deleted after pull

After running a repo.Network.Pull() command I want to be able to see what files were added to the repository, altered in the repository and removed from the repository. All I need is the file path of the file and if it was an add/update or a delete.

Is there an easy way to do this? I've tried looking into Diff.Compare() but I'm not sure if that is the right way to do it.

like image 334
Nived Avatar asked Oct 19 '22 09:10

Nived


1 Answers

LibGit2Sharp 0.21.0.176

Here is an libGit2 example of walking your current commit tree and getting the files that changed and the type of change.

Git version:

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 version:

    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);
            }
        }
    }

Output:

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

In directly answering to your question, just grab the first commit from the Commits enumerator and compare its tree to its parents (could be more than one parent due to a merge) vs. my example of looping all the commits in the current branch.

like image 149
SushiHangover Avatar answered Oct 22 '22 02:10

SushiHangover