You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArchiveCommand.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (C) 2012 Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.api;
  44. import java.io.Closeable;
  45. import java.io.IOException;
  46. import java.io.OutputStream;
  47. import java.text.MessageFormat;
  48. import java.util.concurrent.ConcurrentHashMap;
  49. import java.util.concurrent.ConcurrentMap;
  50. import org.eclipse.jgit.api.Git;
  51. import org.eclipse.jgit.api.GitCommand;
  52. import org.eclipse.jgit.api.errors.GitAPIException;
  53. import org.eclipse.jgit.api.errors.JGitInternalException;
  54. import org.eclipse.jgit.internal.JGitText;
  55. import org.eclipse.jgit.lib.FileMode;
  56. import org.eclipse.jgit.lib.MutableObjectId;
  57. import org.eclipse.jgit.lib.ObjectId;
  58. import org.eclipse.jgit.lib.ObjectLoader;
  59. import org.eclipse.jgit.lib.ObjectReader;
  60. import org.eclipse.jgit.lib.Repository;
  61. import org.eclipse.jgit.revwalk.RevWalk;
  62. import org.eclipse.jgit.treewalk.TreeWalk;
  63. /**
  64. * Create an archive of files from a named tree.
  65. * <p>
  66. * Examples (<code>git</code> is a {@link Git} instance):
  67. * <p>
  68. * Create a tarball from HEAD:
  69. *
  70. * <pre>
  71. * ArchiveCommand.registerFormat("tar", new TarFormat());
  72. * try {
  73. * git.archive()
  74. * .setTree(db.resolve(&quot;HEAD&quot;))
  75. * .setOutputStream(out)
  76. * .call();
  77. * } finally {
  78. * ArchiveCommand.unregisterFormat("tar");
  79. * }
  80. * </pre>
  81. * <p>
  82. * Create a ZIP file from master:
  83. *
  84. * <pre>
  85. * ArchiveCommand.registerFormat("zip", new ZipFormat());
  86. * try {
  87. * git.archive().
  88. * .setTree(db.resolve(&quot;master&quot;))
  89. * .setFormat("zip")
  90. * .setOutputStream(out)
  91. * .call();
  92. * } finally {
  93. * ArchiveCommand.unregisterFormat("zip");
  94. * }
  95. * </pre>
  96. *
  97. * @see <a href="http://git-htmldocs.googlecode.com/git/git-archive.html"
  98. * >Git documentation about archive</a>
  99. *
  100. * @since 3.0
  101. */
  102. public class ArchiveCommand extends GitCommand<OutputStream> {
  103. /**
  104. * Archival format.
  105. *
  106. * Usage:
  107. * Repository repo = git.getRepository();
  108. * T out = format.createArchiveOutputStream(System.out);
  109. * try {
  110. * for (...) {
  111. * format.putEntry(out, path, mode, repo.open(objectId));
  112. * }
  113. * } finally {
  114. * out.close();
  115. * }
  116. */
  117. public static interface Format<T extends Closeable> {
  118. T createArchiveOutputStream(OutputStream s);
  119. void putEntry(T out, String path, FileMode mode,
  120. ObjectLoader loader) throws IOException;
  121. }
  122. /**
  123. * Signals an attempt to use an archival format that ArchiveCommand
  124. * doesn't know about (for example due to a typo).
  125. */
  126. public static class UnsupportedFormatException extends GitAPIException {
  127. private static final long serialVersionUID = 1L;
  128. private final String format;
  129. /**
  130. * @param format the problematic format name
  131. */
  132. public UnsupportedFormatException(String format) {
  133. super(MessageFormat.format(JGitText.get().unsupportedArchiveFormat, format));
  134. this.format = format;
  135. }
  136. /**
  137. * @return the problematic format name
  138. */
  139. public String getFormat() {
  140. return format;
  141. }
  142. }
  143. /**
  144. * Available archival formats (corresponding to values for
  145. * the --format= option)
  146. */
  147. private static final ConcurrentMap<String, Format<?>> formats =
  148. new ConcurrentHashMap<String, Format<?>>();
  149. /**
  150. * Adds support for an additional archival format. To avoid
  151. * unnecessary dependencies, ArchiveCommand does not have support
  152. * for any formats built in; use this function to add them.
  153. *
  154. * OSGi plugins providing formats should call this function at
  155. * bundle activation time.
  156. *
  157. * @param name name of a format (e.g., "tar" or "zip").
  158. * @param fmt archiver for that format
  159. * @throws JGitInternalException
  160. * An archival format with that name was already registered.
  161. */
  162. public static void registerFormat(String name, Format<?> fmt) {
  163. if (formats.putIfAbsent(name, fmt) != null)
  164. throw new JGitInternalException(MessageFormat.format(
  165. JGitText.get().archiveFormatAlreadyRegistered,
  166. name));
  167. }
  168. /**
  169. * Removes support for an archival format so its Format can be
  170. * garbage collected.
  171. *
  172. * @param name name of format (e.g., "tar" or "zip").
  173. * @throws JGitInternalException
  174. * No such archival format was registered.
  175. */
  176. public static void unregisterFormat(String name) {
  177. if (formats.remove(name) == null)
  178. throw new JGitInternalException(MessageFormat.format(
  179. JGitText.get().archiveFormatAlreadyAbsent,
  180. name));
  181. }
  182. private static Format<?> lookupFormat(String formatName) throws UnsupportedFormatException {
  183. Format<?> fmt = formats.get(formatName);
  184. if (fmt == null)
  185. throw new UnsupportedFormatException(formatName);
  186. return fmt;
  187. }
  188. private OutputStream out;
  189. private ObjectId tree;
  190. private String format = "tar";
  191. /**
  192. * @param repo
  193. */
  194. public ArchiveCommand(Repository repo) {
  195. super(repo);
  196. setCallable(false);
  197. }
  198. private <T extends Closeable>
  199. OutputStream writeArchive(Format<T> fmt) throws GitAPIException {
  200. final TreeWalk walk = new TreeWalk(repo);
  201. try {
  202. final T outa = fmt.createArchiveOutputStream(out);
  203. try {
  204. final MutableObjectId idBuf = new MutableObjectId();
  205. final ObjectReader reader = walk.getObjectReader();
  206. final RevWalk rw = new RevWalk(walk.getObjectReader());
  207. walk.reset(rw.parseTree(tree));
  208. walk.setRecursive(true);
  209. while (walk.next()) {
  210. final String name = walk.getPathString();
  211. final FileMode mode = walk.getFileMode(0);
  212. if (mode == FileMode.TREE)
  213. // ZIP entries for directories are optional.
  214. // Leave them out, mimicking "git archive".
  215. continue;
  216. walk.getObjectId(idBuf, 0);
  217. fmt.putEntry(outa, name, mode, reader.open(idBuf));
  218. }
  219. } finally {
  220. outa.close();
  221. }
  222. return out;
  223. } catch (IOException e) {
  224. // TODO(jrn): Throw finer-grained errors.
  225. throw new JGitInternalException(
  226. JGitText.get().exceptionCaughtDuringExecutionOfArchiveCommand, e);
  227. } finally {
  228. walk.release();
  229. }
  230. }
  231. /**
  232. * @return the stream to which the archive has been written
  233. */
  234. @Override
  235. public OutputStream call() throws GitAPIException {
  236. checkCallable();
  237. final Format<?> fmt = lookupFormat(format);
  238. return writeArchive(fmt);
  239. }
  240. /**
  241. * @param tree
  242. * the tag, commit, or tree object to produce an archive for
  243. * @return this
  244. */
  245. public ArchiveCommand setTree(ObjectId tree) {
  246. if (tree == null)
  247. throw new IllegalArgumentException();
  248. this.tree = tree;
  249. setCallable(true);
  250. return this;
  251. }
  252. /**
  253. * @param out
  254. * the stream to which to write the archive
  255. * @return this
  256. */
  257. public ArchiveCommand setOutputStream(OutputStream out) {
  258. this.out = out;
  259. return this;
  260. }
  261. /**
  262. * @param fmt
  263. * archive format (e.g., "tar" or "zip")
  264. * @return this
  265. */
  266. public ArchiveCommand setFormat(String fmt) {
  267. this.format = fmt;
  268. return this;
  269. }
  270. }