From c0c4c6f09ac1d601a5d9fe9855e36e4b96b10335 Mon Sep 17 00:00:00 2001
From: David Ostrovsky
Date: Sun, 15 Feb 2015 20:31:29 +0100
Subject: [PATCH] ArchiveCommand: Allow to pass options to underlying stream
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 level = ImmutableMap. 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
---
.../org/eclipse/jgit/archive/BaseFormat.java | 81 +++++++++++++++++++
.../org/eclipse/jgit/archive/TarFormat.java | 17 +++-
.../org/eclipse/jgit/archive/Tbz2Format.java | 12 ++-
.../org/eclipse/jgit/archive/TgzFormat.java | 12 ++-
.../org/eclipse/jgit/archive/TxzFormat.java | 12 ++-
.../org/eclipse/jgit/archive/ZipFormat.java | 15 +++-
.../eclipse/jgit/api/ArchiveCommandTest.java | 41 +++++++++-
.../org/eclipse/jgit/api/ArchiveCommand.java | 34 +++++++-
.../org/eclipse/jgit/util/StringUtils.java | 25 ++++++
9 files changed, 232 insertions(+), 17 deletions(-)
create mode 100644 org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/BaseFormat.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
index 000000000..6803251f9
--- /dev/null
+++ b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/BaseFormat.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2015, David Ostrovsky
+ * 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 o) throws IOException {
+ for (Map.Entry 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;
+ }
+}
diff --git a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TarFormat.java b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TarFormat.java
index 9a0e3040f..c510c64c9 100644
--- a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TarFormat.java
+++ b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TarFormat.java
@@ -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 {
+public final class TarFormat extends BaseFormat implements
+ ArchiveCommand.Format {
private static final List 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. emptyMap());
+ }
+
+ public ArchiveOutputStream createArchiveOutputStream(OutputStream s,
+ Map 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,
diff --git a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/Tbz2Format.java b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/Tbz2Format.java
index 65e1e7964..ba5348a15 100644
--- a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/Tbz2Format.java
+++ b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/Tbz2Format.java
@@ -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 {
+public final class Tbz2Format extends BaseFormat implements
+ ArchiveCommand.Format {
private static final List 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 emptyMap());
+ }
+
+ public ArchiveOutputStream createArchiveOutputStream(OutputStream s,
+ Map o) throws IOException {
BZip2CompressorOutputStream out = new BZip2CompressorOutputStream(s);
- return tarFormat.createArchiveOutputStream(out);
+ return tarFormat.createArchiveOutputStream(out, o);
}
public void putEntry(ArchiveOutputStream out,
diff --git a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TgzFormat.java b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TgzFormat.java
index e13c88a04..71adca0e5 100644
--- a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TgzFormat.java
+++ b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TgzFormat.java
@@ -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 {
+public final class TgzFormat extends BaseFormat implements
+ ArchiveCommand.Format {
private static final List 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 emptyMap());
+ }
+
+ public ArchiveOutputStream createArchiveOutputStream(OutputStream s,
+ Map o) throws IOException {
GzipCompressorOutputStream out = new GzipCompressorOutputStream(s);
- return tarFormat.createArchiveOutputStream(out);
+ return tarFormat.createArchiveOutputStream(out, o);
}
public void putEntry(ArchiveOutputStream out,
diff --git a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TxzFormat.java b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TxzFormat.java
index d74ca9ba0..197235913 100644
--- a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TxzFormat.java
+++ b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TxzFormat.java
@@ -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 {
+public final class TxzFormat extends BaseFormat implements
+ ArchiveCommand.Format {
private static final List 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 emptyMap());
+ }
+
+ public ArchiveOutputStream createArchiveOutputStream(OutputStream s,
+ Map o) throws IOException {
XZCompressorOutputStream out = new XZCompressorOutputStream(s);
- return tarFormat.createArchiveOutputStream(out);
+ return tarFormat.createArchiveOutputStream(out, o);
}
public void putEntry(ArchiveOutputStream out,
diff --git a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/ZipFormat.java b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/ZipFormat.java
index 988ef90f9..0b5108ad0 100644
--- a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/ZipFormat.java
+++ b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/ZipFormat.java
@@ -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 {
+public final class ZipFormat extends BaseFormat implements
+ ArchiveCommand.Format {
private static final List 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. emptyMap());
+ }
+
+ public ArchiveOutputStream createArchiveOutputStream(OutputStream s,
+ Map o) throws IOException {
+ return applyFormatOptions(new ZipArchiveOutputStream(s), o);
}
public void putEntry(ArchiveOutputStream out,
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ArchiveCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ArchiveCommandTest.java
index 8cdf6a6c0..44e70885b 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ArchiveCommandTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ArchiveCommandTest.java
@@ -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 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. emptyMap());
+ }
+
+ public MockOutputStream createArchiveOutputStream(OutputStream s,
+ Map o) throws IOException {
+ for (Map.Entry 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 {
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 616d5b436..f52a93477 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java
@@ -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 {
*/
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 o)
+ throws IOException;
+
/**
* Write an entry to an archive.
*
@@ -328,6 +349,7 @@ public class ArchiveCommand extends GitCommand {
private ObjectId tree;
private String prefix;
private String format;
+ private Map formatOptions = new HashMap<>();
private List paths = new ArrayList();
/** Filename suffix, for automatically choosing a format. */
@@ -345,7 +367,7 @@ public class ArchiveCommand extends GitCommand {
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 {
return this;
}
+ /**
+ * @param options
+ * archive format options (e.g., level=9 for zip compression).
+ * @return this
+ */
+ public ArchiveCommand setFormatOptions(Map 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
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java
index 40b89fba9..1fd508930 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java
@@ -96,6 +96,31 @@ public final class StringUtils {
return r.toString();
}
+
+ /**
+ * Borrowed from commons-lang StringUtils.capitalize()
method.
+ *
+ *
+ * Capitalizes a String changing the first letter to title case as per
+ * {@link Character#toTitleCase(char)}. No other letters are changed.
+ *
+ *
+ * A null
input String returns null
.
+ *
+ * @param str
+ * the String to capitalize, may be null
+ * @return the capitalized String, null
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.
*