Search code examples
c#gitlibgit2sharpgit-fetch

How to get a list of remote changes from fetch using LibGit2Sharp


I am able to successfully fetch, pull, push, etc. using LibGit2Sharp, but I would like to be able to list files that have changed, added, etc. after doing a fetch. I'm using https://github.com/libgit2/libgit2sharp/wiki/git-fetch and no errors or exceptions occur and logMessage is an empty string.

I would like to be able to show a list of changes like Visual Studio does when you perform a fetch.

How can I use LibGit2Sharp to accomplish this?

Edit: I have read through the LibGit2Sharp Wiki and the LibGit2Sharp Hitchhiker's Guide to Git. While I have tried some of the available commands to review what results they offer, I am not sure what the equivalent git command would be for this either. It would be helpful to know and understand which command would provide this information and would be appreciated if you are familiar with Git, but not LibGit2Sharp.


Solution

  • Once the fetch is done, you can list the fetched commit of a given branch with

    git log ..@{u}
    

    with @{u} designating the branch you are merging from (the upstream remote tracking branch, generally origin/yourbranch)

    In LibGitSharp, that is what LibGit2Sharp/BranchUpdater.cs#UpstreamBranch reference (the upstream branch)

    With that, you should be able to list the commmits between your current branch HEAD and "UpstreamBranch", a bit like in issue 1161, but that issue was listing what is being pushed: let's invert the log parameters here.

    var trackingBranch = repo.Head.TrackedBranch;
    var log = repo.Commits.QueryBy(new CommitFilter 
                { IncludeReachableFrom = trackingBranch.Tip.Id, ExcludeReachableFrom = repo.Head.Tip.Id });
    
    var count = log.Count();//Counts the number of log entries
    
    //iterate the commits that represent the difference between your last 
    //push to the remote branch and latest commits
    foreach (var commit in log)
    {
        Console.WriteLine(commit.Message);
    }