12

I'm a new user of git and am using JGit to interact with a remote git repository. In JGit, I used CloneCommand to initially to clone a repo, and it worked without a issue. However, when I try to use PullCommand, which is the equivalent of SVN update AFAIK, the local repo contents are not updated.

This is the code that I used:

private String localPath;
private Repository localRepo;
private Git git;

localPath = "/home/test/git_repo_test";
remotePath = "https://github.com/test/repo_1.git";

try {
    localRepo = new FileRepository(localPath + "/.git");
} catch (IOException e) {
    e.printStackTrace();  
}
git = new Git(localRepo);

PullCommand pullCmd = git.pull();
try {
    pullCmd.call();
} catch (GitAPIException e) {
    e.printStackTrace();  
}

This doesn't update the local repository for new files which I have pushed to the remote repository using the command line. However, if I delete the local repository and take a clone again, all the changes are reflected.

Please let me know what is the correct approach of using PullCommand in JGit.

EDIT:

The structure of the remote repository:

root ____ file_1
  |______ directory_1
              |__________ file_2 
              |__________ file_3

directory_1 and the two files are pushed from the commandline after the initial cloning and I tried this code so that it will get reflected in the local repository, which is not happening.

The code used to clone the repository:

File file = new File(localPath);
CloneCommand cloneCmd = git.cloneRepository();
try {
    cloneCmd.setURI(remotePath)
            .setDirectory(file)
            .call();
} catch (GitAPIException e) {
    e.printStackTrace();  
}

Here, git, localPath and remotePath are the same variable as above.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Izza
  • 2,389
  • 8
  • 38
  • 60

4 Answers4

9

I suspect the problem is that the current branch has no upstream configuration (and so pull won't merge the fetched branch).

To see what happened during the pull, inspect the result of pullCmd.call():

PullResult result = pullCmd.call();
FetchResult fetchResult = result.getFetchResult();
MergeResult mergeResult = result.getMergeResult();
mergeResult.getMergeStatus();  // this should be interesting
robinst
  • 30,027
  • 10
  • 102
  • 108
  • Thank you for the suggesstion. Result of getMergeStatus.isSuccessful() is true. However, still the changes are not reflected locally. Any idea? – Izza Nov 15 '12 at 15:30
  • What is the status? It should be `FAST_FORWARD`. Also, have you looked at the cloned repository using the command-line git client, does it look alright (e.g. `git status`)? – robinst Nov 15 '12 at 19:38
  • I checked it, it says ALREADY_UP_TO_DATE. Guess this is the issue then. But somehow the files from the remote repository are not updated. And git status says on branch master, and shows some files that have deleted earlier. – Izza Nov 16 '12 at 11:19
4

Documentations says about constructor of Git class following:

Constructs a new Git object which can interact with the specified git repository. All command classes returned by methods of this class will always interact with this git repository.

So, as I can suggest, you have to pass path of remote repo to constructor, now you're trying to pull from your local repo.

bsiamionau
  • 8,099
  • 4
  • 46
  • 73
2

I ran into the same issue. For me the solution was to set the git config file after cloning:

CloneCommand cloneCommand = Git.cloneRepository();
cloneCommand.setURI("<repo-uri>");
cloneCommand.setDirectory("<repo-dir>");
cloneCommand.call();

Git git = Git.open("<repo-dir>");
StoredConfig config = git.getRepository().getConfig();
config.setString("branch", "master", "merge", "refs/heads/master");
config.setString("branch", "master", "remote", "origin");
config.setString("remote", "origin", "fetch", "+refs/heads/*:refs/remotes/origin/*");
config.setString("remote", "origin", "url", "<repo-uri>");
config.save();

When pulling I set the remote branch name to "master" and the remote to "origin":

PullCommand pull = git.pull();
pull.setRemote("origin");
pull.setRemoteBranchName("master");

With these changes after pulling I see the changes reflected locally.

Amos Joshua
  • 1,601
  • 18
  • 25
1

Try this code for git pull from private Repository

try {
        Repository localRepo = new FileRepository(localPath + "/.git");
        Git git = new Git(localRepo);
        CredentialsProvider cp = new UsernamePasswordCredentialsProvider(userName, pass);
        git.pull().setCredentialsProvider(cp).call();
        git.close();
    } catch (Exception e) {
    }
Arun Patel
  • 29
  • 3