Search code examples
c#gitngit

What is the method in NGit to see if remote repo changed?


I am trying to use IsClean() from NGit to determine if any changes have been detected in the working copy, it works fine but when I try to see if anything changed in remote I don't think IsClean() is the proper method to try. So I wanted to know if there is any other method that would help me to see the change made in remote. I tried pulling the remote repo but it doest seem to work, does anyone know if there is any method in NGit for this.

       var repository = Git.Open(activeRepopath);
       var status = repository.Status().Call();
       Consoel.WriteLine(stauts.IsClean());

       while (status.IsClean())
       {
            repository.Pull().Call();

       }

I found the tutorial from here on IsClean().

I actually want something similar to buildbot's gitpoller. If someone could show me the way how to start I am happy to work in that direction.


Solution

  • In the solution you give yourself you are fetching the remote. That changes your local repository remote references, and may or may not be what you want. In this answer: How to know local repo is different from remote repo, without fetch? it is outlined how you can examine if the remote has changed with the ls-remote command, or LsRemote with NGit in this case.

    Checking without fetching can look like this:

    var repository = Git.Open(activeRepopath);
    ICollection<Ref> refs = repository.LsRemote().SetHeads(true).SetTags(true).Call();
    
    // Compare the results of GetObjectId() of refs with 'oldRefs', obtained
    // previously, to find out if tags or heads changed between the two calls.
    // Possibly:
    var newIds = refs.Select(r => r.GetObjectId().ToString()).OrderBy(r => r);
    var oldIds = oldRefs.Select(r => r.GetObjectId().ToString()).OrderBy(r => r);
    if (!newIds.SequenceEqual(oldIds)) {
         Console.WriteLine("Something changed");
    }