Search code examples
javagitfetchjgit

Fetching with JGit doesn't show the last commit


I'm trying to write a simple program to use the basic functions of JGit. I don't want to use the default git merge tool but I'd like to fetch and then make the merge on my own.

I'm using the code below and I'm having a weird problem, when I call the update method just after pushing a commit, the FETCH_HEAD does not contain the commit I just pushed (see output). But after using "git fetch" in git bash the fetching result contains the last commit. So it seems that the fetch of the library is different from the actual git fetch.

I think it comes from the way I'm using git.fetch() but I've try with and without many options for the FetchCommand and it doesn't change anything. Maybe I've forgotten a kind of update somewhere... Could it come from getFetchCommit() ?

OUTPUT:

> update
Fetch : [email protected]:XXX/XXX.git
Repository C:\XXX\XXX\.git is up to date.
    last commit : commit2 - fcddf71c214784e511ccef73b2c083c0fcacd653

// Making some changes

> update
Fetch : [email protected]:XXX/XXX.git
You have uncommited changes, commit them (y/n)? : y
Added : 
    README.md
message : commit3
Commit : commit3 d50209b080bd0d474b79801bfdc808dd494d83fe
local commit : commit3 - d50209b080bd0d474b79801bfdc808dd494d83fe
fetch commit : commit2 - fcddf71c214784e511ccef73b2c083c0fcacd653
Not up to date, do you wish to : fpush - fpull - merge? : fpush
Push : [email protected]:XXX/XXX.git   // The push appears as excepted on the repo
> update
Fetch : [email protected]:XXX/XXX.git
local commit : commit3 - d50209b080bd0d474b79801bfdc808dd494d83fe
fetch commit : commit2 - fcddf71c214784e511ccef73b2c083c0fcacd653
Not up to date, do you wish to : fpush - fpull - merge? :    // Should to be up to date
>

// git fetch on git bash

> update
Fetch : [email protected]:XXX/XXX.git
Repository C:\XXX\XXX\.git is up to date.
    last commit : commit3 - d50209b080bd0d474b79801bfdc808dd494d83fe

CODE:

public void gitFetch() throws Exception
{
    FetchResult fetch = git.fetch().setRemote("origin").call();
    System.out.println("Fetch : " + fetch.getURI().toString());
}

public void gitPush(boolean force) throws Exception
{
    Iterable<PushResult> push = git.push().setForce(force).call();
    System.out.println("Push : " + push.iterator().next().getURI().toString());
}

public RevCommit getLocalCommit() throws Exception
{
    Iterable<RevCommit> logL = git.log().call();
    return logL.iterator().next();
}

public RevCommit getFetchCommit() throws Exception
{
    gitFetch();
    Iterable<RevCommit> log = git.log().add(git.getRepository().resolve("FETCH_HEAD")).call();
    return log.iterator().next();
}

public void gitUpdate() throws Exception
{
    RevCommit localCommit = getLocalCommit();
    RevCommit fetchCommit = getFetchCommit();

    if (fetchCommit.getId().equals(localCommit.getId()))
    {
        System.out.println("Repository " + localRepo.getDirectory().getAbsolutePath() + " is up to date.");
        printCommit(localCommit, "\tlast");
    }
    else
    {
        printCommit(localCommit, "local");
        printCommit(fetchCommit, "fetch");
        String input = readInput("Not up to date, do you wish to : fpush - fpull - merge?");

        if (input.equals("fpush"))
            gitPush(true);
        else if (input.equals("fpull"))
            gitPull();
        else if (input.equals("merge"))
            // Do my own merge
    }
}

Solution

  • Wow it worked with resolve("origin/master") ! Thank you robinst. Actually a friend also found another answer just when you answered by replacing getFetchCommit() by a new gitFetch()

    public RevCommit gitFetch() throws Exception
    {
        FetchResult result = git.fetch().setRemote("origin").call();
        System.out.println("Fetch : " + result.getURI().toString());
        return revWalk.parseCommit(result.getAdvertisedRef("refs/heads/" + localRepo.getBranch()).getTarget().getObjectId());
    }
    

    But your solution robinst seems clearer so I'll take it!