From 1153a59e3820ebcba8b6871ae09304015c1d6f89 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Thu, 6 Jun 2013 18:32:10 -0700 Subject: Add support for "jgit archive --output=" If the --format option is not given and the output filename is, then infer the format from that filename. Otherwise match "git archive" by defaulting to tar (this is a change from the existing "jgit archive" default behavior, which was to default to zip). Change-Id: I5806bc48a403d05e4cfc3c180b82b33ad7cfae7f --- .../src/org/eclipse/jgit/pgm/Archive.java | 35 ++++++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) (limited to 'org.eclipse.jgit.pgm') diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Archive.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Archive.java index 0fc7afa6bb..7b88a94345 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Archive.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Archive.java @@ -43,6 +43,11 @@ package org.eclipse.jgit.pgm; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.OutputStream; + +import org.eclipse.jgit.api.ArchiveCommand; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.archive.ArchiveFormats; @@ -62,21 +67,39 @@ class Archive extends TextBuiltin { private ObjectId tree; @Option(name = "--format", metaVar = "metaVar_archiveFormat", usage = "usage_archiveFormat") - private String format = "zip"; + private String format; + + @Option(name = "--output", aliases = { "-o" }, metaVar = "metaVar_file", usage = "usage_archiveOutput") + private String output; @Override protected void run() throws Exception { if (tree == null) throw die(CLIText.get().treeIsRequired); + OutputStream stream = null; try { - new Git(db).archive() - .setTree(tree) - .setFormat(format) - .setOutputStream(outs) - .call(); + if (output != null) + stream = new FileOutputStream(output); + else + stream = outs; + + try { + ArchiveCommand cmd = new Git(db).archive() + .setTree(tree) + .setFormat(format) + .setOutputStream(stream); + if (output != null) + cmd.setFilename(output); + cmd.call(); } catch (GitAPIException e) { throw die(e.getMessage()); } + } catch (FileNotFoundException e) { + throw die(e.getMessage()); + } finally { + if (output != null && stream != null) + stream.close(); + } } } -- cgit v1.2.3