--- /dev/null
+/*
+ * 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;
+ }
+}
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;
/**
* 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,
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;
/**
* 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$
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,
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;
/**
* 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$
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,
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;
/**
* 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$
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,
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;
/**
* 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,
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;
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;
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"));
}
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();
}
}
}
- 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 {
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;
*/
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.
*
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. */
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();
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
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>