Browse Source

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<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>
stable-4.0
David Ostrovsky 10 years ago
parent
commit
c0c4c6f09a
  1. 81
      org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/BaseFormat.java
  2. 17
      org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TarFormat.java
  3. 12
      org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/Tbz2Format.java
  4. 12
      org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TgzFormat.java
  5. 12
      org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TxzFormat.java
  6. 15
      org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/ZipFormat.java
  7. 41
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ArchiveCommandTest.java
  8. 34
      org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java
  9. 25
      org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java

81
org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/BaseFormat.java

@ -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;
}
}

17
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<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,

12
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<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,

12
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<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,

12
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<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,

15
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<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,

41
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<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 {

34
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<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

25
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 <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>

Loading…
Cancel
Save