Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ArchiveCommand.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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.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.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. * ArchiveCommand.registerFormat("tar", new TarFormat());
  72. * cmd = new ArchiveCommand(git.getRepository());
  73. * try {
  74. * cmd.setTree(db.resolve(&quot;HEAD&quot;))
  75. * .setOutputStream(out).call();
  76. * } finally {
  77. * cmd.release();
  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. * cmd.setTree(db.resolve(&quot;master&quot;))
  88. * .setFormat("zip")
  89. * .setOutputStream(out).call();
  90. * } finally {
  91. * cmd.release();
  92. * ArchiveCommand.unregisterFormat("zip");
  93. * }
  94. * </pre>
  95. *
  96. * @see <a href="http://git-htmldocs.googlecode.com/git/git-archive.html"
  97. * >Git documentation about archive</a>
  98. *
  99. * @since 3.0
  100. */
  101. public class ArchiveCommand extends GitCommand<OutputStream> {
  102. /**
  103. * Archival format.
  104. *
  105. * Usage:
  106. * Repository repo = git.getRepository();
  107. * T out = format.createArchiveOutputStream(System.out);
  108. * try {
  109. * for (...) {
  110. * format.putEntry(out, path, mode, repo.open(objectId));
  111. * }
  112. * } finally {
  113. * out.close();
  114. * }
  115. */
  116. public static interface Format<T extends Closeable> {
  117. T createArchiveOutputStream(OutputStream s);
  118. void putEntry(T out, String path, FileMode mode,
  119. ObjectLoader loader) throws IOException;
  120. }
  121. /**
  122. * Signals an attempt to use an archival format that ArchiveCommand
  123. * doesn't know about (for example due to a typo).
  124. */
  125. public static class UnsupportedFormatException extends GitAPIException {
  126. private static final long serialVersionUID = 1L;
  127. private final String format;
  128. /**
  129. * @param format the problematic format name
  130. */
  131. public UnsupportedFormatException(String format) {
  132. super(MessageFormat.format(CLIText.get().unsupportedArchiveFormat, format));
  133. this.format = format;
  134. }
  135. /**
  136. * @return the problematic format name
  137. */
  138. public String getFormat() {
  139. return format;
  140. }
  141. }
  142. /**
  143. * Available archival formats (corresponding to values for
  144. * the --format= option)
  145. */
  146. private static final ConcurrentMap<String, Format<?>> formats =
  147. new ConcurrentHashMap<String, Format<?>>();
  148. /**
  149. * Adds support for an additional archival format. To avoid
  150. * unnecessary dependencies, ArchiveCommand does not have support
  151. * for any formats built in; use this function to add them.
  152. *
  153. * OSGi plugins providing formats should call this function at
  154. * bundle activation time.
  155. *
  156. * @param name name of a format (e.g., "tar" or "zip").
  157. * @param fmt archiver for that format
  158. * @throws JGitInternalException
  159. * An archival format with that name was already registered.
  160. */
  161. public static void registerFormat(String name, Format<?> fmt) {
  162. if (formats.putIfAbsent(name, fmt) != null)
  163. throw new JGitInternalException(MessageFormat.format(
  164. CLIText.get().archiveFormatAlreadyRegistered,
  165. name));
  166. }
  167. /**
  168. * Removes support for an archival format so its Format can be
  169. * garbage collected.
  170. *
  171. * @param name name of format (e.g., "tar" or "zip").
  172. * @throws JGitInternalException
  173. * No such archival format was registered.
  174. */
  175. public static void unregisterFormat(String name) {
  176. if (formats.remove(name) == null)
  177. throw new JGitInternalException(MessageFormat.format(
  178. CLIText.get().archiveFormatAlreadyAbsent,
  179. name));
  180. }
  181. private static Format<?> lookupFormat(String formatName) throws UnsupportedFormatException {
  182. Format<?> fmt = formats.get(formatName);
  183. if (fmt == null)
  184. throw new UnsupportedFormatException(formatName);
  185. return fmt;
  186. }
  187. private OutputStream out;
  188. private TreeWalk walk;
  189. private String format = "tar";
  190. /**
  191. * @param repo
  192. */
  193. public ArchiveCommand(Repository repo) {
  194. super(repo);
  195. walk = new TreeWalk(repo);
  196. }
  197. /**
  198. * Release any resources used by the internal ObjectReader.
  199. * <p>
  200. * This does not close the output stream set with setOutputStream, which
  201. * belongs to the caller.
  202. */
  203. public void release() {
  204. walk.release();
  205. }
  206. private <T extends Closeable>
  207. OutputStream writeArchive(Format<T> fmt) throws GitAPIException {
  208. final MutableObjectId idBuf = new MutableObjectId();
  209. final T outa = fmt.createArchiveOutputStream(out);
  210. final ObjectReader reader = walk.getObjectReader();
  211. try {
  212. try {
  213. walk.setRecursive(true);
  214. while (walk.next()) {
  215. final String name = walk.getPathString();
  216. final FileMode mode = walk.getFileMode(0);
  217. if (mode == FileMode.TREE)
  218. // ZIP entries for directories are optional.
  219. // Leave them out, mimicking "git archive".
  220. continue;
  221. walk.getObjectId(idBuf, 0);
  222. fmt.putEntry(outa, name, mode, reader.open(idBuf));
  223. }
  224. } finally {
  225. outa.close();
  226. }
  227. } catch (IOException e) {
  228. // TODO(jrn): Throw finer-grained errors.
  229. throw new JGitInternalException(
  230. CLIText.get().exceptionCaughtDuringExecutionOfArchiveCommand, e);
  231. }
  232. return out;
  233. }
  234. /**
  235. * @return the stream to which the archive has been written
  236. */
  237. @Override
  238. public OutputStream call() throws GitAPIException {
  239. final Format<?> fmt = lookupFormat(format);
  240. return writeArchive(fmt);
  241. }
  242. /**
  243. * @param tree
  244. * the tag, commit, or tree object to produce an archive for
  245. * @return this
  246. * @throws IOException
  247. */
  248. public ArchiveCommand setTree(ObjectId tree) throws IOException {
  249. final RevWalk rw = new RevWalk(walk.getObjectReader());
  250. walk.reset(rw.parseTree(tree));
  251. return this;
  252. }
  253. /**
  254. * @param out
  255. * the stream to which to write the archive
  256. * @return this
  257. */
  258. public ArchiveCommand setOutputStream(OutputStream out) {
  259. this.out = out;
  260. return this;
  261. }
  262. /**
  263. * @param fmt
  264. * archive format (e.g., "tar" or "zip")
  265. * @return this
  266. */
  267. public ArchiveCommand setFormat(String fmt) {
  268. this.format = fmt;
  269. return this;
  270. }
  271. }