]> source.dussan.org Git - jgit.git/commitdiff
ArchiveCommand: Allow to pass options to underlying stream 87/41887/3
authorDavid Ostrovsky <david@ostrovsky.org>
Sun, 15 Feb 2015 19:31:29 +0000 (20:31 +0100)
committerDavid Ostrovsky <david@ostrovsky.org>
Fri, 20 Feb 2015 00:40:06 +0000 (01:40 +0100)
Current ArchiveCommand design doesn't allow to pass in options to
underlying stream implementations. To overcome this, client has to
implement custom format implementation (it cannot be derived from
the existing one, because the classes are marked as final), and set
the options using ThreadLocal, before the method

  ArchiveOutputStream createArchiveOutputStream(OutputStream s)

is get called.

This change extends the ArchiveCommand.Format by allowing to pass
option map during creation of ArchiveOutputStream.

ArchiveCommand is extended correspondingly. That way client can
easily pass options to the underlying streams:

  Map<String, Object> level = ImmutableMap.<String, Object> of(
      "level", new Integer(9));
  new ArchiveCommand(repo)
      .setFormat("zip")
      .setFormatOptions(level)
      .setTree(tree)
      .setPaths(paths)
      .setPrefix(prefix)
      .setOutputStream(sidebandOut)
      .call();

Change-Id: I1d92a1e5249117487da39d19c7593e4b812ad97a
Signed-off-by: David Ostrovsky <david@ostrovsky.org>
org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/BaseFormat.java [new file with mode: 0644]
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.test/tst/org/eclipse/jgit/api/ArchiveCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java

diff --git a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/BaseFormat.java b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/BaseFormat.java
new file mode 100644 (file)
index 0000000..6803251
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2015, David Ostrovsky <david@ostrovsky.org>
+ * and other copyright owners as documented in the project's IP log.
+ *
+ * This program and the accompanying materials are made available
+ * under the terms of the Eclipse Distribution License v1.0 which
+ * accompanies this distribution, is reproduced below, and is
+ * available at http://www.eclipse.org/org/documents/edl-v10.php
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ *   copyright notice, this list of conditions and the following
+ *   disclaimer in the documentation and/or other materials provided
+ *   with the distribution.
+ *
+ * - Neither the name of the Eclipse Foundation, Inc. nor the
+ *   names of its contributors may be used to endorse or promote
+ *   products derived from this software without specific prior
+ *   written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.eclipse.jgit.archive;
+
+import java.beans.Statement;
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.commons.compress.archivers.ArchiveOutputStream;
+import org.eclipse.jgit.util.StringUtils;
+
+/**
+ * Base format class
+ */
+public class BaseFormat {
+
+       /**
+        * Apply options to archive output stream
+        *
+        * @param s
+        *            stream to apply options to
+        * @param o
+        *            options map
+        * @return stream with option applied
+        * @throws IOException
+        */
+       protected ArchiveOutputStream applyFormatOptions(ArchiveOutputStream s,
+                       Map<String, Object> o) throws IOException {
+               for (Map.Entry<String, Object> p : o.entrySet()) {
+                       try {
+                               new Statement(s,
+                                               "set" + StringUtils.capitalize(p.getKey()),
+                                               new Object[]{p.getValue()}).execute();
+                       } catch (Exception e) {
+                               throw new IOException("cannot set option: " + p.getKey(), e);
+                       }
+               }
+               return s;
+       }
+}
index 9a0e3040fa6207129b137b8a964f89713caaff3b..c510c64c9dd1a845a944e4d00c89bdfa7ac2b692 100644 (file)
@@ -44,10 +44,11 @@ package org.eclipse.jgit.archive;
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.text.MessageFormat;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
-import java.text.MessageFormat;
+import java.util.Map;
 
 import org.apache.commons.compress.archivers.ArchiveOutputStream;
 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
@@ -61,15 +62,23 @@ import org.eclipse.jgit.lib.ObjectLoader;
 /**
  * Unix TAR format (ustar + some PAX extensions).
  */
-public final class TarFormat implements ArchiveCommand.Format<ArchiveOutputStream> {
+public final class TarFormat extends BaseFormat implements
+               ArchiveCommand.Format<ArchiveOutputStream> {
        private static final List<String> SUFFIXES = Collections
                        .unmodifiableList(Arrays.asList(".tar")); //$NON-NLS-1$
 
-       public ArchiveOutputStream createArchiveOutputStream(OutputStream s) {
+       public ArchiveOutputStream createArchiveOutputStream(OutputStream s)
+                       throws IOException {
+               return createArchiveOutputStream(s,
+                               Collections.<String, Object> emptyMap());
+       }
+
+       public ArchiveOutputStream createArchiveOutputStream(OutputStream s,
+                       Map<String, Object> o) throws IOException {
                TarArchiveOutputStream out = new TarArchiveOutputStream(s, "UTF-8"); //$NON-NLS-1$
                out.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
                out.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
-               return out;
+               return applyFormatOptions(out, o);
        }
 
        public void putEntry(ArchiveOutputStream out,
index 65e1e796428fdc8a568b9b28b8936dafad9fc2d8..ba5348a1542cd62409b718488620fbbba337e39e 100644 (file)
@@ -47,6 +47,7 @@ import java.io.OutputStream;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.commons.compress.archivers.ArchiveOutputStream;
 import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
@@ -57,7 +58,8 @@ import org.eclipse.jgit.lib.ObjectLoader;
 /**
  * bzip2-compressed tarball (tar.bz2) format.
  */
-public final class Tbz2Format implements ArchiveCommand.Format<ArchiveOutputStream> {
+public final class Tbz2Format extends BaseFormat implements
+               ArchiveCommand.Format<ArchiveOutputStream> {
        private static final List<String> SUFFIXES = Collections
                        .unmodifiableList(Arrays.asList(".tar.bz2", ".tbz", ".tbz2")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
 
@@ -65,8 +67,14 @@ public final class Tbz2Format implements ArchiveCommand.Format<ArchiveOutputStre
 
        public ArchiveOutputStream createArchiveOutputStream(OutputStream s)
                        throws IOException {
+               return createArchiveOutputStream(s,
+                               Collections.<String, Object> emptyMap());
+       }
+
+       public ArchiveOutputStream createArchiveOutputStream(OutputStream s,
+                       Map<String, Object> o) throws IOException {
                BZip2CompressorOutputStream out = new BZip2CompressorOutputStream(s);
-               return tarFormat.createArchiveOutputStream(out);
+               return tarFormat.createArchiveOutputStream(out, o);
        }
 
        public void putEntry(ArchiveOutputStream out,
index e13c88a044e207961eef52313747edfe847df91d..71adca0e5d850e3a6b7a96678bd3d694504dd941 100644 (file)
@@ -47,6 +47,7 @@ import java.io.OutputStream;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.commons.compress.archivers.ArchiveOutputStream;
 import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
@@ -57,7 +58,8 @@ import org.eclipse.jgit.lib.ObjectLoader;
 /**
  * gzip-compressed tarball (tar.gz) format.
  */
-public final class TgzFormat implements ArchiveCommand.Format<ArchiveOutputStream> {
+public final class TgzFormat extends BaseFormat implements
+               ArchiveCommand.Format<ArchiveOutputStream> {
        private static final List<String> SUFFIXES = Collections
                        .unmodifiableList(Arrays.asList(".tar.gz", ".tgz")); //$NON-NLS-1$ //$NON-NLS-2$
 
@@ -65,8 +67,14 @@ public final class TgzFormat implements ArchiveCommand.Format<ArchiveOutputStrea
 
        public ArchiveOutputStream createArchiveOutputStream(OutputStream s)
                        throws IOException {
+               return createArchiveOutputStream(s,
+                               Collections.<String, Object> emptyMap());
+       }
+
+       public ArchiveOutputStream createArchiveOutputStream(OutputStream s,
+                       Map<String, Object> o) throws IOException {
                GzipCompressorOutputStream out = new GzipCompressorOutputStream(s);
-               return tarFormat.createArchiveOutputStream(out);
+               return tarFormat.createArchiveOutputStream(out, o);
        }
 
        public void putEntry(ArchiveOutputStream out,
index d74ca9ba0856506d1961632877a59604b986bef5..19723591350fd99b2b013e3df1b04d643654e663 100644 (file)
@@ -47,6 +47,7 @@ import java.io.OutputStream;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.commons.compress.archivers.ArchiveOutputStream;
 import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream;
@@ -57,7 +58,8 @@ import org.eclipse.jgit.lib.ObjectLoader;
 /**
  * Xz-compressed tar (tar.xz) format.
  */
-public final class TxzFormat implements ArchiveCommand.Format<ArchiveOutputStream> {
+public final class TxzFormat extends BaseFormat implements
+               ArchiveCommand.Format<ArchiveOutputStream> {
        private static final List<String> SUFFIXES = Collections
                        .unmodifiableList(Arrays.asList(".tar.xz", ".txz")); //$NON-NLS-1$ //$NON-NLS-2$
 
@@ -65,8 +67,14 @@ public final class TxzFormat implements ArchiveCommand.Format<ArchiveOutputStrea
 
        public ArchiveOutputStream createArchiveOutputStream(OutputStream s)
                        throws IOException {
+               return createArchiveOutputStream(s,
+                               Collections.<String, Object> emptyMap());
+       }
+
+       public ArchiveOutputStream createArchiveOutputStream(OutputStream s,
+                       Map<String, Object> o) throws IOException {
                XZCompressorOutputStream out = new XZCompressorOutputStream(s);
-               return tarFormat.createArchiveOutputStream(out);
+               return tarFormat.createArchiveOutputStream(out, o);
        }
 
        public void putEntry(ArchiveOutputStream out,
index 988ef90f9d17e90751ed8fe68fac8bc2db772ad8..0b5108ad0440074ddcbc389d4468d790c07aa5fe 100644 (file)
@@ -48,6 +48,7 @@ import java.text.MessageFormat;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.commons.compress.archivers.ArchiveOutputStream;
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
@@ -60,12 +61,20 @@ import org.eclipse.jgit.lib.ObjectLoader;
 /**
  * PKWARE's ZIP format.
  */
-public final class ZipFormat implements ArchiveCommand.Format<ArchiveOutputStream> {
+public final class ZipFormat extends BaseFormat implements
+               ArchiveCommand.Format<ArchiveOutputStream> {
        private static final List<String> SUFFIXES = Collections
                        .unmodifiableList(Arrays.asList(".zip")); //$NON-NLS-1$
 
-       public ArchiveOutputStream createArchiveOutputStream(OutputStream s) {
-               return new ZipArchiveOutputStream(s);
+       public ArchiveOutputStream createArchiveOutputStream(OutputStream s)
+                       throws IOException {
+               return createArchiveOutputStream(s,
+                               Collections.<String, Object> emptyMap());
+       }
+
+       public ArchiveOutputStream createArchiveOutputStream(OutputStream s,
+                       Map<String, Object> o) throws IOException {
+               return applyFormatOptions(new ZipArchiveOutputStream(s), o);
        }
 
        public void putEntry(ArchiveOutputStream out,
index 8cdf6a6c0d1439139925426f5249a3f386c69eb6..44e70885ba3c4a28566312ddba9205f1cd7bfe0e 100644 (file)
@@ -45,6 +45,7 @@ package org.eclipse.jgit.api;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
 
+import java.beans.Statement;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.util.Arrays;
@@ -58,6 +59,7 @@ import org.eclipse.jgit.junit.RepositoryTestCase;
 import org.eclipse.jgit.lib.FileMode;
 import org.eclipse.jgit.lib.ObjectLoader;
 import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.util.StringUtils;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -138,10 +140,17 @@ public class ArchiveCommandTest extends RepositoryTestCase {
                git.add().addFilepattern(".").call();
                git.commit().setMessage("updated file").call();
 
-               git.archive().setOutputStream(new MockOutputStream())
-                               .setFormat(format.SUFFIXES.get(0)).setTree(first)
+               Map<String, Object> options = new HashMap<>();
+               Integer opt = Integer.valueOf(42);
+               options.put("foo", opt);
+               MockOutputStream out = new MockOutputStream();
+               git.archive().setOutputStream(out)
+                               .setFormat(format.SUFFIXES.get(0))
+                               .setFormatOptions(options)
+                               .setTree(first)
                                .setPaths("file_1.txt").call();
 
+               assertEquals(opt.intValue(), out.getFoo());
                assertEquals(UNEXPECTED_ARCHIVE_SIZE, 1, format.size());
                assertEquals(UNEXPECTED_FILE_CONTENTS, "content_1_1", format.getByPath("file_1.txt"));
        }
@@ -192,6 +201,22 @@ public class ArchiveCommandTest extends RepositoryTestCase {
 
                public MockOutputStream createArchiveOutputStream(OutputStream s)
                                throws IOException {
+                       return createArchiveOutputStream(s,
+                                       Collections.<String, Object> emptyMap());
+               }
+
+               public MockOutputStream createArchiveOutputStream(OutputStream s,
+                               Map<String, Object> o) throws IOException {
+                       for (Map.Entry<String, Object> p : o.entrySet()) {
+                               try {
+                                       String methodName = "set"
+                                                       + StringUtils.capitalize(p.getKey());
+                                       new Statement(s, methodName, new Object[] { p.getValue() })
+                                                       .execute();
+                               } catch (Exception e) {
+                                       throw new IOException("cannot set option: " + p.getKey(), e);
+                               }
+                       }
                        return new MockOutputStream();
                }
 
@@ -205,7 +230,17 @@ public class ArchiveCommandTest extends RepositoryTestCase {
                }
        }
 
-       private class MockOutputStream extends OutputStream {
+       public class MockOutputStream extends OutputStream {
+
+               private int foo;
+
+               public void setFoo(int foo) {
+                       this.foo = foo;
+               }
+
+               public int getFoo() {
+                       return foo;
+               }
 
                @Override
                public void write(int b) throws IOException {
index 616d5b436bf933b1d8397dfde382b3909f8d4774..f52a9347748befe1c043e9f38b892f8bd1cbc76b 100644 (file)
@@ -48,7 +48,9 @@ import java.io.OutputStream;
 import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
@@ -135,6 +137,25 @@ public class ArchiveCommand extends GitCommand<OutputStream> {
                 */
                T createArchiveOutputStream(OutputStream s) throws IOException;
 
+               /**
+                * Start a new archive. Entries can be included in the archive using the
+                * putEntry method, and then the archive should be closed using its
+                * close method. In addition options can be applied to the underlying
+                * stream. E.g. compression level.
+                *
+                * @param s
+                *            underlying output stream to which to write the archive.
+                * @param o
+                *            options to apply to the underlying output stream. Keys are
+                *            option names and values are option values.
+                * @return new archive object for use in putEntry
+                * @throws IOException
+                *             thrown by the underlying output stream for I/O errors
+                * @since 4.0
+                */
+               T createArchiveOutputStream(OutputStream s, Map<String, Object> o)
+                               throws IOException;
+
                /**
                 * Write an entry to an archive.
                 *
@@ -328,6 +349,7 @@ public class ArchiveCommand extends GitCommand<OutputStream> {
        private ObjectId tree;
        private String prefix;
        private String format;
+       private Map<String, Object> formatOptions = new HashMap<>();
        private List<String> paths = new ArrayList<String>();
 
        /** Filename suffix, for automatically choosing a format. */
@@ -345,7 +367,7 @@ public class ArchiveCommand extends GitCommand<OutputStream> {
                final String pfx = prefix == null ? "" : prefix; //$NON-NLS-1$
                final TreeWalk walk = new TreeWalk(repo);
                try {
-                       final T outa = fmt.createArchiveOutputStream(out);
+                       final T outa = fmt.createArchiveOutputStream(out, formatOptions);
                        try {
                                final MutableObjectId idBuf = new MutableObjectId();
                                final ObjectReader reader = walk.getObjectReader();
@@ -471,6 +493,16 @@ public class ArchiveCommand extends GitCommand<OutputStream> {
                return this;
        }
 
+       /**
+        * @param options
+        *            archive format options (e.g., level=9 for zip compression).
+        * @return this
+        */
+       public ArchiveCommand setFormatOptions(Map<String, Object> options) {
+               this.formatOptions = options;
+               return this;
+       }
+
        /**
         * Set an optional parameter path. without an optional path parameter, all
         * files and subdirectories of the current working directory are included in
index 40b89fba9b413957839b1c02e06bb14f19edd718..1fd5089300e99a4a6ce8672a27ca5f96e96c51c5 100644 (file)
@@ -96,6 +96,31 @@ public final class StringUtils {
                return r.toString();
        }
 
+
+       /**
+        * Borrowed from commons-lang <code>StringUtils.capitalize()</code> method.
+        *
+        * <p>
+        * Capitalizes a String changing the first letter to title case as per
+        * {@link Character#toTitleCase(char)}. No other letters are changed.
+        * </p>
+        *
+        * A <code>null</code> input String returns <code>null</code>.</p>
+        *
+        * @param str
+        *            the String to capitalize, may be null
+        * @return the capitalized String, <code>null</code> if null String input
+        */
+       public static String capitalize(String str) {
+               int strLen;
+               if (str == null || (strLen = str.length()) == 0) {
+                       return str;
+               }
+               return new StringBuffer(strLen)
+                               .append(Character.toTitleCase(str.charAt(0)))
+                               .append(str.substring(1)).toString();
+       }
+
        /**
         * Test if two strings are equal, ignoring case.
         * <p>