]> source.dussan.org Git - jgit.git/commitdiff
Pick default archive format based on filename suffix 09/13609/3
authorJonathan Nieder <jrn@google.com>
Fri, 7 Jun 2013 01:39:04 +0000 (18:39 -0700)
committerJonathan Nieder <jrn@google.com>
Fri, 7 Jun 2013 01:39:04 +0000 (18:39 -0700)
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

org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TarFormat.java
org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/Tbz2Format.java
org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TgzFormat.java
org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TxzFormat.java
org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/ZipFormat.java
org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java

index 228de7c3792077d9a3d8ca1f6322827915de2575..23f4beda1448961a81fbc6333c086ac38f9bb7c5 100644 (file)
@@ -44,6 +44,9 @@ package org.eclipse.jgit.archive;
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
 
 import org.apache.commons.compress.archivers.ArchiveOutputStream;
 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
@@ -57,6 +60,9 @@ import org.eclipse.jgit.lib.ObjectLoader;
  * Unix TAR format (ustar + some PAX extensions).
  */
 public class TarFormat implements ArchiveCommand.Format<ArchiveOutputStream> {
+       private static final List<String> SUFFIXES =
+                       Collections.unmodifiableList(Arrays.asList(".tar"));
+
        public ArchiveOutputStream createArchiveOutputStream(OutputStream s) {
                TarArchiveOutputStream out = new TarArchiveOutputStream(s, "UTF-8"); //$NON-NLS-1$
                out.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
@@ -90,4 +96,8 @@ public class TarFormat implements ArchiveCommand.Format<ArchiveOutputStream> {
                loader.copyTo(out);
                out.closeArchiveEntry();
        }
+
+       public Iterable<String> suffixes() {
+               return SUFFIXES;
+       }
 }
index 9883118ea2050e1b102451e9504632363d51bc35..d3482d181381332028c3cd5c99205a41b867c3b8 100644 (file)
@@ -44,6 +44,9 @@ package org.eclipse.jgit.archive;
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
 
 import org.apache.commons.compress.archivers.ArchiveOutputStream;
 import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
@@ -55,6 +58,10 @@ import org.eclipse.jgit.lib.ObjectLoader;
  * bzip2-compressed tarball (tar.bz2) format.
  */
 public class Tbz2Format implements ArchiveCommand.Format<ArchiveOutputStream> {
+       private static final List<String> SUFFIXES =
+                       Collections.unmodifiableList(Arrays.asList(
+                               ".tar.bz2", ".tbz", ".tbz2"));
+
        private final ArchiveCommand.Format<ArchiveOutputStream> tarFormat = new TarFormat();
 
        public ArchiveOutputStream createArchiveOutputStream(OutputStream s)
@@ -68,4 +75,8 @@ public class Tbz2Format implements ArchiveCommand.Format<ArchiveOutputStream> {
                        throws IOException {
                tarFormat.putEntry(out, path, mode, loader);
        }
+
+       public Iterable<String> suffixes() {
+               return SUFFIXES;
+       }
 }
index 1c72bf8dccac1c953f9b5cd0da263ead8388fe7c..fe1350cbb0149a9549bf60ed5e9ce9d4a4e2fc18 100644 (file)
@@ -44,6 +44,9 @@ package org.eclipse.jgit.archive;
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
 
 import org.apache.commons.compress.archivers.ArchiveOutputStream;
 import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
@@ -55,6 +58,10 @@ import org.eclipse.jgit.lib.ObjectLoader;
  * gzip-compressed tarball (tar.gz) format.
  */
 public class TgzFormat implements ArchiveCommand.Format<ArchiveOutputStream> {
+       private static final List<String> SUFFIXES =
+                       Collections.unmodifiableList(Arrays.asList(
+                               ".tar.gz", ".tgz"));
+
        private final ArchiveCommand.Format<ArchiveOutputStream> tarFormat = new TarFormat();
 
        public ArchiveOutputStream createArchiveOutputStream(OutputStream s)
@@ -68,4 +75,8 @@ public class TgzFormat implements ArchiveCommand.Format<ArchiveOutputStream> {
                        throws IOException {
                tarFormat.putEntry(out, path, mode, loader);
        }
+
+       public Iterable<String> suffixes() {
+               return SUFFIXES;
+       }
 }
index d1547c683c8b4371fd5b36861b72cbb4ed9f53ab..c09b0621a7b7139a8ef392779d27eb1005e6a03e 100644 (file)
@@ -44,6 +44,9 @@ package org.eclipse.jgit.archive;
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
 
 import org.apache.commons.compress.archivers.ArchiveOutputStream;
 import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream;
@@ -55,6 +58,10 @@ import org.eclipse.jgit.lib.ObjectLoader;
  * Xz-compressed tar (tar.xz) format.
  */
 public class TxzFormat implements ArchiveCommand.Format<ArchiveOutputStream> {
+       private static final List<String> SUFFIXES =
+                       Collections.unmodifiableList(Arrays.asList(
+                               ".tar.xz", ".txz"));
+
        private final ArchiveCommand.Format<ArchiveOutputStream> tarFormat = new TarFormat();
 
        public ArchiveOutputStream createArchiveOutputStream(OutputStream s)
@@ -68,4 +75,8 @@ public class TxzFormat implements ArchiveCommand.Format<ArchiveOutputStream> {
                        throws IOException {
                tarFormat.putEntry(out, path, mode, loader);
        }
+
+       public Iterable<String> suffixes() {
+               return SUFFIXES;
+       }
 }
index a0906d4dd52e281d357c1924fc5481a038f0229f..00c962bc9827f71beef9498cb4459420d8d8fcd7 100644 (file)
@@ -44,6 +44,9 @@ package org.eclipse.jgit.archive;
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
 
 import org.apache.commons.compress.archivers.ArchiveOutputStream;
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
@@ -56,6 +59,9 @@ import org.eclipse.jgit.lib.ObjectLoader;
  * PKWARE's ZIP format.
  */
 public class ZipFormat implements ArchiveCommand.Format<ArchiveOutputStream> {
+       private static final List<String> SUFFIXES =
+                       Collections.unmodifiableList(Arrays.asList(".zip"));
+
        public ArchiveOutputStream createArchiveOutputStream(OutputStream s) {
                return new ZipArchiveOutputStream(s);
        }
@@ -79,4 +85,8 @@ public class ZipFormat implements ArchiveCommand.Format<ArchiveOutputStream> {
                loader.copyTo(out);
                out.closeArchiveEntry();
        }
+
+       public Iterable<String> suffixes() {
+               return SUFFIXES;
+       }
 }
index 7726e15eeb3ffe29dd56a22e68054221433895fa..ab94d3a80d0645767418559959a5289cc58cd2b3 100644 (file)
@@ -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);
        }
 
@@ -300,6 +333,26 @@ public class ArchiveCommand extends GitCommand<OutputStream> {
                return this;
        }
 
+       /**
+        * 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
@@ -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) {