I recently started working with JGit and want to programmatically create an archive of a single folder of a certain branch of an arbitrary existing remote without cloning the whole repository. Using the normal command line git, this can be translated to the following command:
git archive --remote=<url/to/repo.git> --format=zip -o archive.zip <branch> <folder>
However, I failed building this command with JGit since the ArchiveCommand doesn't have an option to set a remote and requires a repository.
I also tried initializing a Git Repository with Git.init(), adding a remote and then calling JGit's archive, which also doesn't work. Sample code for this is below (although I'd rather not have to call Git.init).
So my question is: Is this actually possible? If not, are there reasons why this wasn't implemented?
public void archiveFromRemote(final String repositoryUrl, final String requestDirectory, final String archiveResultPath)
{
final Git git = Git.init().call();
git.remoteAdd().setName("origin").setUri(new URIish(repositoryUrl));
final ObjectId masterObjectId = git.getRepository().resolve("origin/master");
final OutputStream archive = new FileOutputStream(archiveResultPath);
final ZipArchiveFormat format = new ZipArchiveFormat();
ArchiveCommand.registerFormat("zip", format);
// No set remote available
final ArchiveCommand archiveCmd = git.archive().setFormat("zip").setOutputStream(archive).setPaths(requestDirectory)
.setTree(masterObjectId);
try
{
archiveCmd.call();
}
catch (final GitAPIException e)
{
e.printStackTrace();
}
finally
{
git.close();
}
}