Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of 'pull' command in Jgit

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.

like image 357
Izza Avatar asked Nov 15 '12 14:11

Izza


People also ask

What is the use of pull command in Git?

The git pull command is used to fetch and download content from a remote repository and immediately update the local repository to match that content.


1 Answers

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
like image 168
robinst Avatar answered Sep 19 '22 12:09

robinst