diff options
author | David Ostrovsky <david@ostrovsky.org> | 2015-02-15 20:31:29 +0100 |
---|---|---|
committer | David Ostrovsky <david@ostrovsky.org> | 2015-02-20 01:40:06 +0100 |
commit | c0c4c6f09ac1d601a5d9fe9855e36e4b96b10335 (patch) | |
tree | ca02a75898d38203c510efef4037553e597691b4 /org.eclipse.jgit | |
parent | 6c1f7393882baf8464859136a70199ea96fcae0f (diff) | |
download | jgit-c0c4c6f09ac1d601a5d9fe9855e36e4b96b10335.tar.gz jgit-c0c4c6f09ac1d601a5d9fe9855e36e4b96b10335.zip |
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>
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java | 34 | ||||
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java | 25 |
2 files changed, 58 insertions, 1 deletions
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 616d5b436b..f52a934774 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; @@ -136,6 +138,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. * * @param out @@ -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(); @@ -472,6 +494,16 @@ public class ArchiveCommand extends GitCommand<OutputStream> { } /** + * @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 * the archive. If one or more paths are specified, only these are included. 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 40b89fba9b..1fd5089300 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 <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> |