Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a git pull with an in-memory database in JGit? [duplicate]

Tags:

java

git

jgit

I want to create a Java program, which

  1. connects to a certain Git repository,
  2. appends text to a file,
  3. commits and
  4. pushes the changes to that repository.

Ideally, all of this should happen in the memory.

I'm using JGit for interaction with Git:

InMemoryRepository repo = new InMemoryRepository(new DfsRepositoryDescription());
Git git = new Git(repo);
git.init().call();
PullCommand pull = git.pull();

StoredConfig config = git.getRepository().getConfig();
config.setString("remote", "origin", "url", "https://XXX");
config.save();

PullResult result = pull.call();

The pull.call() results in the following exception:

org.eclipse.jgit.api.errors.NoHeadException: Pull on repository without HEAD currently not supported
    at org.eclipse.jgit.api.PullCommand.call(PullCommand.java:191)

How can I retrieve the contents of a repository into an in-memory JGit repository?

like image 538
Dmitrii Pisarenko Avatar asked Apr 24 '15 11:04

Dmitrii Pisarenko


1 Answers

To append a file you would need a non-bare repository (one that has a work directory. This mailing list post states, that

The porcelain commands assume a File basis in some cases, particularly when it comes to the working tree. You can perform inserts into an in-memory repository if you do the work yourself, but the porcelain commands do not work in that way.

While you could do without the procelain commands, I also recommend (like @Wayne commented above) to clone into a temporary repository, append to the file, push and then delete the repository.

like image 131
Rüdiger Herrmann Avatar answered Sep 20 '22 16:09

Rüdiger Herrmann