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

557 views Asked by At

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.

2

There are 2 answers

1
mockinterface On BEST ANSWER

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");
}
0
solti On

Here I use test_value to store the number of remote file that has changed. Initially it is zero and if something change in the remote it test_value becomes 1 or 2 depending on the number of file changed, this help to escape the loop and we know something changed.

var repository = Git.Open(activeRepopath);
int test_value = 0; 
ICollection<TrackingRefUpdate> refUpdate = null;

while (test_value == 0 )
{

    FetchResult result = repository.Fetch().Call();
    refUpdate = result.GetTrackingRefUpdates();
    test_value = refUpdate.Count();
    Console.Write(test_value);

}

Console.WriteLine("Something changed");