-2

I am using the jgit . I am new to it i am able to clone the code from github but when i tried to push code in java it is giving me error.

Here's code : public class PullFromRemoteRepository {

private static final String REMOTE_URL = "https://github.com/raghav1/local.git";

public static void main(String[] args) throws IOException, InvalidRemoteException, TransportException, GitAPIException {
    // prepare a new folder for the cloned repository
    File localPath = File.createTempFile("raghav345", "");
    localPath.delete();

    // then clone
    System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
    Git.cloneRepository()
            .setURI(REMOTE_URL)
            .setDirectory(localPath)
            .call();

    // now open the created repository
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repository = builder.setGitDir(localPath)
            .readEnvironment() // scan environment GIT_* variables
            .findGitDir() // scan up the file system tree
            .build();

    Git git = new Git(repository);
    git.pull()
            .call();

    System.out.println("Pulled from remote repository to local repository at " + repository.getDirectory());

    repository.close();
}

}

Errors are coming like this:

Exception in thread "main" org.eclipse.jgit.api.errors.NoHeadException: Pull on repository without HEAD currently not supported at org.eclipse.jgit.api.PullCommand.call(PullCommand.java:170) at org.dstadler.jgit.unfinished.PullFromRemoteRepository.main(PullFromRemoteRepository.java:61)

Can anybody help me where to set the path of head.

Thanks

user3352615
  • 119
  • 2
  • 5
  • 13

1 Answers1

0

The code after the comment 'now open the created repository' does not actually open the cloned repository. Thus the pull command acts on an empty repository.

Either re-use the Git instance that is returned from the CloneCommand or change your code to something like this:

Git git = Git.cloneRepository().setURI( REMOTE_URL ).setDirectory( new File( "local/path/to/repo" ) ).call();
File gitDir = git.getRepository().getDirectory();
git.close();
// ...
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir( gitDir ).setMustExist( true ).build();
Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • When i am using your suggestion . It is giving me this error Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method setDirectory(File) in the type CloneCommand is not applicable for the arguments (String) – user3352615 Aug 15 '14 at 17:26
  • As the exception message says, you are running the snippet despite a compile error. Replace "local/path/to/repo" with a File that points to the directory where the cloned repository should go - same as you did in the snippet from the question. – Rüdiger Herrmann Aug 19 '14 at 11:07