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 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.pgm.archive;
  44. import java.io.IOException;
  45. import java.io.OutputStream;
  46. import java.text.MessageFormat;
  47. import java.util.concurrent.ConcurrentHashMap;
  48. import java.util.concurrent.ConcurrentMap;
  49. import org.apache.commons.compress.archivers.ArchiveOutputStream;
  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.lib.FileMode;
  55. import org.eclipse.jgit.lib.MutableObjectId;
  56. import org.eclipse.jgit.lib.ObjectId;
  57. import org.eclipse.jgit.lib.ObjectLoader;
  58. import org.eclipse.jgit.lib.ObjectReader;
  59. import org.eclipse.jgit.lib.Repository;
  60. import org.eclipse.jgit.pgm.internal.CLIText;
  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. * cmd = new ArchiveCommand(git.getRepository());
  72. * try {
  73. * cmd.setTree(db.resolve(&quot;HEAD&quot;))
  74. * .setOutputStream(out).call();
  75. * } finally {
  76. * cmd.release();
  77. * }
  78. * </pre>
  79. * <p>
  80. * Create a ZIP file from master:
  81. *
  82. * <pre>
  83. * try {
  84. * cmd.setTree(db.resolve(&quot;master&quot;))
  85. * .setFormat("zip")
  86. * .setOutputStream(out).call();
  87. * } finally {
  88. * cmd.release();
  89. * }
  90. * </pre>
  91. *
  92. * @see <a href="http://git-htmldocs.googlecode.com/git/git-archive.html"
  93. * >Git documentation about archive</a>
  94. *
  95. * @since 3.0
  96. */
  97. public class ArchiveCommand extends GitCommand<OutputStream> {
  98. /**
  99. * Archival format.
  100. *
  101. * Usage:
  102. * Repository repo = git.getRepository();
  103. * ArchiveOutputStream out = format.createArchiveOutputStream(System.out);
  104. * try {
  105. * for (...) {
  106. * format.putEntry(path, mode, repo.open(objectId), out);
  107. * }
  108. * } finally {
  109. * out.close();
  110. * }
  111. */
  112. public static interface Format {
  113. ArchiveOutputStream createArchiveOutputStream(OutputStream s);
  114. void putEntry(String path, FileMode mode, //
  115. ObjectLoader loader, ArchiveOutputStream out) //
  116. throws IOException;
  117. }
  118. /**
  119. * Signals an attempt to use an archival format that ArchiveCommand
  120. * doesn't know about (for example due to a typo).
  121. */
  122. public static class UnsupportedFormatException extends GitAPIException {
  123. private static final long serialVersionUID = 1L;
  124. private final String format;
  125. /**
  126. * @param format the problematic format name
  127. */
  128. public UnsupportedFormatException(String format) {
  129. super(MessageFormat.format(CLIText.get().unsupportedArchiveFormat, format));
  130. this.format = format;
  131. }
  132. /**
  133. * @return the problematic format name
  134. */
  135. public String getFormat() {
  136. return format;
  137. }
  138. }
  139. private static final ConcurrentMap<String, Format> formats =
  140. new ConcurrentHashMap<String, Format>();
  141. static {
  142. formats.put("zip", new ZipFormat());
  143. formats.put("tar", new TarFormat());
  144. }
  145. private static Format lookupFormat(String formatName) throws UnsupportedFormatException {
  146. Format fmt = formats.get(formatName);
  147. if (fmt == null)
  148. throw new UnsupportedFormatException(formatName);
  149. return fmt;
  150. }
  151. private OutputStream out;
  152. private TreeWalk walk;
  153. private String format = "tar";
  154. /**
  155. * @param repo
  156. */
  157. public ArchiveCommand(Repository repo) {
  158. super(repo);
  159. walk = new TreeWalk(repo);
  160. }
  161. /**
  162. * Release any resources used by the internal ObjectReader.
  163. * <p>
  164. * This does not close the output stream set with setOutputStream, which
  165. * belongs to the caller.
  166. */
  167. public void release() {
  168. walk.release();
  169. }
  170. /**
  171. * @return the stream to which the archive has been written
  172. */
  173. @Override
  174. public OutputStream call() throws GitAPIException {
  175. final MutableObjectId idBuf = new MutableObjectId();
  176. final Format fmt = lookupFormat(format);
  177. final ArchiveOutputStream outa = fmt.createArchiveOutputStream(out);
  178. final ObjectReader reader = walk.getObjectReader();
  179. try {
  180. try {
  181. walk.setRecursive(true);
  182. while (walk.next()) {
  183. final String name = walk.getPathString();
  184. final FileMode mode = walk.getFileMode(0);
  185. if (mode == FileMode.TREE)
  186. // ZIP entries for directories are optional.
  187. // Leave them out, mimicking "git archive".
  188. continue;
  189. walk.getObjectId(idBuf, 0);
  190. fmt.putEntry(name, mode, reader.open(idBuf), outa);
  191. }
  192. } finally {
  193. outa.close();
  194. }
  195. } catch (IOException e) {
  196. // TODO(jrn): Throw finer-grained errors.
  197. throw new JGitInternalException(
  198. CLIText.get().exceptionCaughtDuringExecutionOfArchiveCommand, e);
  199. }
  200. return out;
  201. }
  202. /**
  203. * @param tree
  204. * the tag, commit, or tree object to produce an archive for
  205. * @return this
  206. * @throws IOException
  207. */
  208. public ArchiveCommand setTree(ObjectId tree) throws IOException {
  209. final RevWalk rw = new RevWalk(walk.getObjectReader());
  210. walk.reset(rw.parseTree(tree));
  211. return this;
  212. }
  213. /**
  214. * @param out
  215. * the stream to which to write the archive
  216. * @return this
  217. */
  218. public ArchiveCommand setOutputStream(OutputStream out) {
  219. this.out = out;
  220. return this;
  221. }
  222. /**
  223. * @param fmt
  224. * archive format (e.g., "tar" or "zip")
  225. * @return this
  226. */
  227. public ArchiveCommand setFormat(String fmt) {
  228. this.format = fmt;
  229. return this;
  230. }
  231. }