6

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?

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Glory to Russia
  • 17,289
  • 56
  • 182
  • 325
  • you have to issue a `fetch` probably –  Apr 24 '15 at 11:25
  • I'm no professional in this area but I have never heard of anyone attempting in-memory git access - I have a suggestion, though.. Try loading the repository into a temp directory (use the File.deleteOnExit function when creating the Directory via File.mkdirs) and then have all those files loaded into memory, saving any modifications that are made then resyncing the git with the temp dir.. – Wayne Apr 24 '15 at 11:36
  • 1
    `no HEAD` probably means you need to checkout some branch first (`checkout master` or whatever other branch) – LeGEC Apr 24 '15 at 12:29
  • @LeGEC Can you sketch the sequence of git commands? Right now, I'm doing this: 1) `git init` 2) `git remote add origin ...` 3) `git pull origin`. When I use git from the command line, it usually works this way. – Glory to Russia Apr 24 '15 at 12:49
  • @DmitriPisarenko : try to understand what is the active branch after your `new Git(repo)` command, and after your `git.init().call()` command. Go check JGit's documentation for more details on how it works. – LeGEC Apr 24 '15 at 13:04
  • I added working code for your case here: https://stackoverflow.com/questions/31271278/clone-a-git-repository-into-an-inmemoryrepository-with-jgit/54486558#54486558 – msangel Feb 01 '19 at 20:14

1 Answers1

3

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.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79