diff options
author | Jonathan Nieder <jrn@google.com> | 2013-06-06 18:39:04 -0700 |
---|---|---|
committer | Jonathan Nieder <jrn@google.com> | 2013-06-06 18:39:04 -0700 |
commit | 56cb2d925c93eadf03a551a2f40bbc9cb2b3241a (patch) | |
tree | b42ae518cd96c0342b85f3313b744a50ddd47156 /org.eclipse.jgit | |
parent | ebfe85d0374aee1992d01029c12338da3d67e26b (diff) | |
download | jgit-56cb2d925c93eadf03a551a2f40bbc9cb2b3241a.tar.gz jgit-56cb2d925c93eadf03a551a2f40bbc9cb2b3241a.zip |
Pick default archive format based on filename suffix
Introduce a setFilename() method for ArchiveCommand so callers can
specify the intended filename of the produced archive. If the
filename ends with .tar, the format will default to tar; if .zip, zip;
if .tar.gz, gzip-compressed tar; and so on.
This doesn't affect "jgit archive" because it doesn't support the
--output=<file> option yet. A later patch might do that.
Change-Id: Ic0236a70f7aa7f2271c3ef11083b21ee986b4df5
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java | 61 |
1 files changed, 58 insertions, 3 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java index 7726e15eeb..ab94d3a80d 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java @@ -46,6 +46,8 @@ import java.io.Closeable; import java.io.IOException; import java.io.OutputStream; import java.text.MessageFormat; +import java.util.Collection; +import java.util.Iterator; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -151,6 +153,18 @@ public class ArchiveCommand extends GitCommand<OutputStream> { */ void putEntry(T out, String path, FileMode mode, ObjectLoader loader) throws IOException; + + /** + * Filename suffixes representing this format (e.g., + * { ".tar.gz", ".tgz" }). + * + * The behavior is undefined when suffixes overlap (if + * one format claims suffix ".7z", no other format should + * take ".tar.7z"). + * + * @return this format's suffixes + */ + Iterable<String> suffixes(); } /** @@ -199,6 +213,8 @@ public class ArchiveCommand extends GitCommand<OutputStream> { * An archival format with that name was already registered. */ public static void registerFormat(String name, Format<?> fmt) { + // TODO(jrn): Check that suffixes don't overlap. + if (formats.putIfAbsent(name, fmt) != null) throw new JGitInternalException(MessageFormat.format( JGitText.get().archiveFormatAlreadyRegistered, @@ -220,6 +236,16 @@ public class ArchiveCommand extends GitCommand<OutputStream> { name)); } + private static Format<?> formatBySuffix(String filenameSuffix) + throws UnsupportedFormatException { + if (filenameSuffix != null) + for (Format<?> fmt : formats.values()) + for (String sfx : fmt.suffixes()) + if (filenameSuffix.endsWith(sfx)) + return fmt; + return lookupFormat("tar"); + } + private static Format<?> lookupFormat(String formatName) throws UnsupportedFormatException { Format<?> fmt = formats.get(formatName); if (fmt == null) @@ -229,7 +255,10 @@ public class ArchiveCommand extends GitCommand<OutputStream> { private OutputStream out; private ObjectId tree; - private String format = "tar"; + private String format; + + /** Filename suffix, for automatically choosing a format. */ + private String suffix; /** * @param repo @@ -282,7 +311,11 @@ public class ArchiveCommand extends GitCommand<OutputStream> { public OutputStream call() throws GitAPIException { checkCallable(); - final Format<?> fmt = lookupFormat(format); + final Format<?> fmt; + if (format == null) + fmt = formatBySuffix(suffix); + else + fmt = lookupFormat(format); return writeArchive(fmt); } @@ -301,6 +334,26 @@ public class ArchiveCommand extends GitCommand<OutputStream> { } /** + * Set the intended filename for the produced archive. + * Currently the only effect is to determine the default + * archive format when none is specified with + * {@link #setFormat(String)}. + * + * @param filename + * intended filename for the archive + */ + public ArchiveCommand setFilename(String filename) { + int slash = filename.lastIndexOf('/'); + int dot = filename.indexOf('.', slash + 1); + + if (dot == -1) + this.suffix = ""; + else + this.suffix = filename.substring(dot); + return this; + } + + /** * @param out * the stream to which to write the archive * @return this @@ -312,7 +365,9 @@ public class ArchiveCommand extends GitCommand<OutputStream> { /** * @param fmt - * archive format (e.g., "tar" or "zip") + * archive format (e.g., "tar" or "zip"). + * null means to choose automatically based on + * the archive filename. * @return this */ public ArchiveCommand setFormat(String fmt) { |