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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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.ArrayList;
  49. import java.util.Arrays;
  50. import java.util.HashMap;
  51. import java.util.List;
  52. import java.util.Map;
  53. import java.util.concurrent.ConcurrentHashMap;
  54. import java.util.concurrent.ConcurrentMap;
  55. import org.eclipse.jgit.api.errors.GitAPIException;
  56. import org.eclipse.jgit.api.errors.JGitInternalException;
  57. import org.eclipse.jgit.internal.JGitText;
  58. import org.eclipse.jgit.lib.FileMode;
  59. import org.eclipse.jgit.lib.MutableObjectId;
  60. import org.eclipse.jgit.lib.ObjectId;
  61. import org.eclipse.jgit.lib.ObjectLoader;
  62. import org.eclipse.jgit.lib.ObjectReader;
  63. import org.eclipse.jgit.lib.Repository;
  64. import org.eclipse.jgit.revwalk.RevWalk;
  65. import org.eclipse.jgit.treewalk.TreeWalk;
  66. import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
  67. /**
  68. * Create an archive of files from a named tree.
  69. * <p>
  70. * Examples (<code>git</code> is a {@link Git} instance):
  71. * <p>
  72. * Create a tarball from HEAD:
  73. *
  74. * <pre>
  75. * ArchiveCommand.registerFormat("tar", new TarFormat());
  76. * try {
  77. * git.archive()
  78. * .setTree(db.resolve(&quot;HEAD&quot;))
  79. * .setOutputStream(out)
  80. * .call();
  81. * } finally {
  82. * ArchiveCommand.unregisterFormat("tar");
  83. * }
  84. * </pre>
  85. * <p>
  86. * Create a ZIP file from master:
  87. *
  88. * <pre>
  89. * ArchiveCommand.registerFormat("zip", new ZipFormat());
  90. * try {
  91. * git.archive().
  92. * .setTree(db.resolve(&quot;master&quot;))
  93. * .setFormat("zip")
  94. * .setOutputStream(out)
  95. * .call();
  96. * } finally {
  97. * ArchiveCommand.unregisterFormat("zip");
  98. * }
  99. * </pre>
  100. *
  101. * @see <a href="http://git-htmldocs.googlecode.com/git/git-archive.html" >Git
  102. * documentation about archive</a>
  103. *
  104. * @since 3.1
  105. */
  106. public class ArchiveCommand extends GitCommand<OutputStream> {
  107. /**
  108. * Archival format.
  109. *
  110. * Usage:
  111. * Repository repo = git.getRepository();
  112. * T out = format.createArchiveOutputStream(System.out);
  113. * try {
  114. * for (...) {
  115. * format.putEntry(out, path, mode, repo.open(objectId));
  116. * }
  117. * out.close();
  118. * }
  119. *
  120. * @param <T>
  121. * type representing an archive being created.
  122. */
  123. public static interface Format<T extends Closeable> {
  124. /**
  125. * Start a new archive. Entries can be included in the archive using the
  126. * putEntry method, and then the archive should be closed using its
  127. * close method.
  128. *
  129. * @param s
  130. * underlying output stream to which to write the archive.
  131. * @return new archive object for use in putEntry
  132. * @throws IOException
  133. * thrown by the underlying output stream for I/O errors
  134. */
  135. T createArchiveOutputStream(OutputStream s) throws IOException;
  136. /**
  137. * Start a new archive. Entries can be included in the archive using the
  138. * putEntry method, and then the archive should be closed using its
  139. * close method. In addition options can be applied to the underlying
  140. * stream. E.g. compression level.
  141. *
  142. * @param s
  143. * underlying output stream to which to write the archive.
  144. * @param o
  145. * options to apply to the underlying output stream. Keys are
  146. * option names and values are option values.
  147. * @return new archive object for use in putEntry
  148. * @throws IOException
  149. * thrown by the underlying output stream for I/O errors
  150. * @since 4.0
  151. */
  152. T createArchiveOutputStream(OutputStream s, Map<String, Object> o)
  153. throws IOException;
  154. /**
  155. * Write an entry to an archive.
  156. *
  157. * @param out
  158. * archive object from createArchiveOutputStream
  159. * @param path
  160. * full filename relative to the root of the archive
  161. * (with trailing '/' for directories)
  162. * @param mode
  163. * mode (for example FileMode.REGULAR_FILE or
  164. * FileMode.SYMLINK)
  165. * @param loader
  166. * blob object with data for this entry (null for
  167. * directories)
  168. * @throws IOException
  169. * thrown by the underlying output stream for I/O errors
  170. */
  171. void putEntry(T out, String path, FileMode mode,
  172. ObjectLoader loader) throws IOException;
  173. /**
  174. * Filename suffixes representing this format (e.g.,
  175. * { ".tar.gz", ".tgz" }).
  176. *
  177. * The behavior is undefined when suffixes overlap (if
  178. * one format claims suffix ".7z", no other format should
  179. * take ".tar.7z").
  180. *
  181. * @return this format's suffixes
  182. */
  183. Iterable<String> suffixes();
  184. }
  185. /**
  186. * Signals an attempt to use an archival format that ArchiveCommand
  187. * doesn't know about (for example due to a typo).
  188. */
  189. public static class UnsupportedFormatException extends GitAPIException {
  190. private static final long serialVersionUID = 1L;
  191. private final String format;
  192. /**
  193. * @param format the problematic format name
  194. */
  195. public UnsupportedFormatException(String format) {
  196. super(MessageFormat.format(JGitText.get().unsupportedArchiveFormat, format));
  197. this.format = format;
  198. }
  199. /**
  200. * @return the problematic format name
  201. */
  202. public String getFormat() {
  203. return format;
  204. }
  205. }
  206. private static class FormatEntry {
  207. final Format<?> format;
  208. /** Number of times this format has been registered. */
  209. final int refcnt;
  210. public FormatEntry(Format<?> format, int refcnt) {
  211. if (format == null)
  212. throw new NullPointerException();
  213. this.format = format;
  214. this.refcnt = refcnt;
  215. }
  216. }
  217. /**
  218. * Available archival formats (corresponding to values for
  219. * the --format= option)
  220. */
  221. private static final ConcurrentMap<String, FormatEntry> formats =
  222. new ConcurrentHashMap<String, FormatEntry>();
  223. /**
  224. * Replaces the entry for a key only if currently mapped to a given
  225. * value.
  226. *
  227. * @param map a map
  228. * @param key key with which the specified value is associated
  229. * @param oldValue expected value for the key (null if should be absent).
  230. * @param newValue value to be associated with the key (null to remove).
  231. * @return true if the value was replaced
  232. */
  233. private static <K, V> boolean replace(ConcurrentMap<K, V> map,
  234. K key, V oldValue, V newValue) {
  235. if (oldValue == null && newValue == null) // Nothing to do.
  236. return true;
  237. if (oldValue == null)
  238. return map.putIfAbsent(key, newValue) == null;
  239. else if (newValue == null)
  240. return map.remove(key, oldValue);
  241. else
  242. return map.replace(key, oldValue, newValue);
  243. }
  244. /**
  245. * Adds support for an additional archival format. To avoid
  246. * unnecessary dependencies, ArchiveCommand does not have support
  247. * for any formats built in; use this function to add them.
  248. * <p>
  249. * OSGi plugins providing formats should call this function at
  250. * bundle activation time.
  251. * <p>
  252. * It is okay to register the same archive format with the same
  253. * name multiple times, but don't forget to unregister it that
  254. * same number of times, too.
  255. * <p>
  256. * Registering multiple formats with different names and the
  257. * same or overlapping suffixes results in undefined behavior.
  258. * TODO: check that suffixes don't overlap.
  259. *
  260. * @param name name of a format (e.g., "tar" or "zip").
  261. * @param fmt archiver for that format
  262. * @throws JGitInternalException
  263. * A different archival format with that name was
  264. * already registered.
  265. */
  266. public static void registerFormat(String name, Format<?> fmt) {
  267. if (fmt == null)
  268. throw new NullPointerException();
  269. FormatEntry old, entry;
  270. do {
  271. old = formats.get(name);
  272. if (old == null) {
  273. entry = new FormatEntry(fmt, 1);
  274. continue;
  275. }
  276. if (!old.format.equals(fmt))
  277. throw new JGitInternalException(MessageFormat.format(
  278. JGitText.get().archiveFormatAlreadyRegistered,
  279. name));
  280. entry = new FormatEntry(old.format, old.refcnt + 1);
  281. } while (!replace(formats, name, old, entry));
  282. }
  283. /**
  284. * Marks support for an archival format as no longer needed so its
  285. * Format can be garbage collected if no one else is using it either.
  286. * <p>
  287. * In other words, this decrements the reference count for an
  288. * archival format. If the reference count becomes zero, removes
  289. * support for that format.
  290. *
  291. * @param name name of format (e.g., "tar" or "zip").
  292. * @throws JGitInternalException
  293. * No such archival format was registered.
  294. */
  295. public static void unregisterFormat(String name) {
  296. FormatEntry old, entry;
  297. do {
  298. old = formats.get(name);
  299. if (old == null)
  300. throw new JGitInternalException(MessageFormat.format(
  301. JGitText.get().archiveFormatAlreadyAbsent,
  302. name));
  303. if (old.refcnt == 1) {
  304. entry = null;
  305. continue;
  306. }
  307. entry = new FormatEntry(old.format, old.refcnt - 1);
  308. } while (!replace(formats, name, old, entry));
  309. }
  310. private static Format<?> formatBySuffix(String filenameSuffix)
  311. throws UnsupportedFormatException {
  312. if (filenameSuffix != null)
  313. for (FormatEntry entry : formats.values()) {
  314. Format<?> fmt = entry.format;
  315. for (String sfx : fmt.suffixes())
  316. if (filenameSuffix.endsWith(sfx))
  317. return fmt;
  318. }
  319. return lookupFormat("tar"); //$NON-NLS-1$
  320. }
  321. private static Format<?> lookupFormat(String formatName) throws UnsupportedFormatException {
  322. FormatEntry entry = formats.get(formatName);
  323. if (entry == null)
  324. throw new UnsupportedFormatException(formatName);
  325. return entry.format;
  326. }
  327. private OutputStream out;
  328. private ObjectId tree;
  329. private String prefix;
  330. private String format;
  331. private Map<String, Object> formatOptions = new HashMap<>();
  332. private List<String> paths = new ArrayList<String>();
  333. /** Filename suffix, for automatically choosing a format. */
  334. private String suffix;
  335. /**
  336. * @param repo
  337. */
  338. public ArchiveCommand(Repository repo) {
  339. super(repo);
  340. setCallable(false);
  341. }
  342. private <T extends Closeable> OutputStream writeArchive(Format<T> fmt) {
  343. try {
  344. try (TreeWalk walk = new TreeWalk(repo);
  345. RevWalk rw = new RevWalk(walk.getObjectReader())) {
  346. String pfx = prefix == null ? "" : prefix; //$NON-NLS-1$
  347. T outa = fmt.createArchiveOutputStream(out, formatOptions);
  348. MutableObjectId idBuf = new MutableObjectId();
  349. ObjectReader reader = walk.getObjectReader();
  350. walk.reset(rw.parseTree(tree));
  351. if (!paths.isEmpty())
  352. walk.setFilter(PathFilterGroup.createFromStrings(paths));
  353. while (walk.next()) {
  354. String name = pfx + walk.getPathString();
  355. FileMode mode = walk.getFileMode(0);
  356. if (walk.isSubtree())
  357. walk.enterSubtree();
  358. if (mode == FileMode.GITLINK)
  359. // TODO(jrn): Take a callback to recurse
  360. // into submodules.
  361. mode = FileMode.TREE;
  362. if (mode == FileMode.TREE) {
  363. fmt.putEntry(outa, name + "/", mode, null); //$NON-NLS-1$
  364. continue;
  365. }
  366. walk.getObjectId(idBuf, 0);
  367. fmt.putEntry(outa, name, mode, reader.open(idBuf));
  368. }
  369. outa.close();
  370. return out;
  371. } finally {
  372. out.close();
  373. }
  374. } catch (IOException e) {
  375. // TODO(jrn): Throw finer-grained errors.
  376. throw new JGitInternalException(
  377. JGitText.get().exceptionCaughtDuringExecutionOfArchiveCommand, e);
  378. }
  379. }
  380. /**
  381. * @return the stream to which the archive has been written
  382. */
  383. @Override
  384. public OutputStream call() throws GitAPIException {
  385. checkCallable();
  386. Format<?> fmt;
  387. if (format == null)
  388. fmt = formatBySuffix(suffix);
  389. else
  390. fmt = lookupFormat(format);
  391. return writeArchive(fmt);
  392. }
  393. /**
  394. * @param tree
  395. * the tag, commit, or tree object to produce an archive for
  396. * @return this
  397. */
  398. public ArchiveCommand setTree(ObjectId tree) {
  399. if (tree == null)
  400. throw new IllegalArgumentException();
  401. this.tree = tree;
  402. setCallable(true);
  403. return this;
  404. }
  405. /**
  406. * @param prefix
  407. * string prefixed to filenames in archive (e.g., "master/").
  408. * null means to not use any leading prefix.
  409. * @return this
  410. * @since 3.3
  411. */
  412. public ArchiveCommand setPrefix(String prefix) {
  413. this.prefix = prefix;
  414. return this;
  415. }
  416. /**
  417. * Set the intended filename for the produced archive. Currently the only
  418. * effect is to determine the default archive format when none is specified
  419. * with {@link #setFormat(String)}.
  420. *
  421. * @param filename
  422. * intended filename for the archive
  423. * @return this
  424. */
  425. public ArchiveCommand setFilename(String filename) {
  426. int slash = filename.lastIndexOf('/');
  427. int dot = filename.indexOf('.', slash + 1);
  428. if (dot == -1)
  429. this.suffix = ""; //$NON-NLS-1$
  430. else
  431. this.suffix = filename.substring(dot);
  432. return this;
  433. }
  434. /**
  435. * @param out
  436. * the stream to which to write the archive
  437. * @return this
  438. */
  439. public ArchiveCommand setOutputStream(OutputStream out) {
  440. this.out = out;
  441. return this;
  442. }
  443. /**
  444. * @param fmt
  445. * archive format (e.g., "tar" or "zip").
  446. * null means to choose automatically based on
  447. * the archive filename.
  448. * @return this
  449. */
  450. public ArchiveCommand setFormat(String fmt) {
  451. this.format = fmt;
  452. return this;
  453. }
  454. /**
  455. * @param options
  456. * archive format options (e.g., level=9 for zip compression).
  457. * @return this
  458. * @since 4.0
  459. */
  460. public ArchiveCommand setFormatOptions(Map<String, Object> options) {
  461. this.formatOptions = options;
  462. return this;
  463. }
  464. /**
  465. * Set an optional parameter path. without an optional path parameter, all
  466. * files and subdirectories of the current working directory are included in
  467. * the archive. If one or more paths are specified, only these are included.
  468. *
  469. * @param paths
  470. * file names (e.g <code>file1.c</code>) or directory names (e.g.
  471. * <code>dir</code> to add <code>dir/file1</code> and
  472. * <code>dir/file2</code>) can also be given to add all files in
  473. * the directory, recursively. Fileglobs (e.g. *.c) are not yet
  474. * supported.
  475. * @return this
  476. * @since 3.4
  477. */
  478. public ArchiveCommand setPaths(String... paths) {
  479. this.paths = Arrays.asList(paths);
  480. return this;
  481. }
  482. }