]> source.dussan.org Git - jgit.git/commitdiff
Drop dependency by ArchiveCommand.Format interface on commons-compress 62/12562/4
authorJonathan Nieder <jrn@google.com>
Thu, 23 May 2013 23:53:57 +0000 (16:53 -0700)
committerJonathan Nieder <jrn@google.com>
Fri, 24 May 2013 01:09:13 +0000 (18:09 -0700)
Otherwise, anyone trying to implement a new format would have to
depend on commons-compress, even if using a different underlying
library to write the archive.

Change-Id: I301a1997e3b48aa7e32d693fd8f4b2d436c9b3a7

org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/archive/ArchiveCommand.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/archive/TarFormat.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/archive/ZipFormat.java

index ad1daebd42f7760d8bf95524123a573db65ae326..437c613a34c1f9d5a4949390aed63f8f6e0e55a3 100644 (file)
  */
 package org.eclipse.jgit.pgm.archive;
 
+import java.io.Closeable;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.text.MessageFormat;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
-import org.apache.commons.compress.archivers.ArchiveOutputStream;
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.api.GitCommand;
 import org.eclipse.jgit.api.errors.GitAPIException;
@@ -103,7 +103,7 @@ public class ArchiveCommand extends GitCommand<OutputStream> {
         *
         * Usage:
         *      Repository repo = git.getRepository();
-        *      ArchiveOutputStream out = format.createArchiveOutputStream(System.out);
+        *      T out = format.createArchiveOutputStream(System.out);
         *      try {
         *              for (...) {
         *                      format.putEntry(out, path, mode, repo.open(objectId));
@@ -112,9 +112,9 @@ public class ArchiveCommand extends GitCommand<OutputStream> {
         *              out.close();
         *      }
         */
-       public static interface Format {
-               ArchiveOutputStream createArchiveOutputStream(OutputStream s);
-               void putEntry(ArchiveOutputStream out, String path, FileMode mode,
+       public static interface Format<T extends Closeable> {
+               T createArchiveOutputStream(OutputStream s);
+               void putEntry(T out, String path, FileMode mode,
                                ObjectLoader loader) throws IOException;
        }
 
@@ -143,16 +143,20 @@ public class ArchiveCommand extends GitCommand<OutputStream> {
                }
        }
 
-       private static final ConcurrentMap<String, Format> formats =
-                       new ConcurrentHashMap<String, Format>();
+       /**
+        * Available archival formats (corresponding to values for
+        * the --format= option)
+        */
+       private static final ConcurrentMap<String, Format<?>> formats =
+                       new ConcurrentHashMap<String, Format<?>>();
 
        static {
                formats.put("zip", new ZipFormat());
                formats.put("tar", new TarFormat());
        }
 
-       private static Format lookupFormat(String formatName) throws UnsupportedFormatException {
-               Format fmt = formats.get(formatName);
+       private static Format<?> lookupFormat(String formatName) throws UnsupportedFormatException {
+               Format<?> fmt = formats.get(formatName);
                if (fmt == null)
                        throw new UnsupportedFormatException(formatName);
                return fmt;
@@ -180,14 +184,10 @@ public class ArchiveCommand extends GitCommand<OutputStream> {
                walk.release();
        }
 
-       /**
-        * @return the stream to which the archive has been written
-        */
-       @Override
-       public OutputStream call() throws GitAPIException {
+       private <T extends Closeable>
+       OutputStream writeArchive(Format<T> fmt) throws GitAPIException {
                final MutableObjectId idBuf = new MutableObjectId();
-               final Format fmt = lookupFormat(format);
-               final ArchiveOutputStream outa = fmt.createArchiveOutputStream(out);
+               final T outa = fmt.createArchiveOutputStream(out);
                final ObjectReader reader = walk.getObjectReader();
 
                try {
@@ -217,6 +217,15 @@ public class ArchiveCommand extends GitCommand<OutputStream> {
                return out;
        }
 
+       /**
+        * @return the stream to which the archive has been written
+        */
+       @Override
+       public OutputStream call() throws GitAPIException {
+               final Format<?> fmt = lookupFormat(format);
+               return writeArchive(fmt);
+       }
+
        /**
         * @param tree
         *            the tag, commit, or tree object to produce an archive for
index a0bbd52403281415d6a5e4c52be0f9930ef64056..52252593d5a310ce6dc4092476866adbfe9a9039 100644 (file)
@@ -52,7 +52,7 @@ import org.apache.commons.compress.archivers.tar.TarConstants;
 import org.eclipse.jgit.lib.FileMode;
 import org.eclipse.jgit.lib.ObjectLoader;
 
-class TarFormat implements ArchiveCommand.Format {
+class TarFormat implements ArchiveCommand.Format<ArchiveOutputStream> {
        public ArchiveOutputStream createArchiveOutputStream(OutputStream s) {
                return new TarArchiveOutputStream(s);
        }
index a85ae3bfa823a5f7b51e84fa3a763356d4ba1adb..d264161c5b217a5b05f3d5332c82f7ba69148c93 100644 (file)
@@ -51,7 +51,7 @@ import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
 import org.eclipse.jgit.lib.FileMode;
 import org.eclipse.jgit.lib.ObjectLoader;
 
-class ZipFormat implements ArchiveCommand.Format {
+class ZipFormat implements ArchiveCommand.Format<ArchiveOutputStream> {
        public ArchiveOutputStream createArchiveOutputStream(OutputStream s) {
                return new ZipArchiveOutputStream(s);
        }