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.

ArchiveCommandTest.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Copyright (C) 2014, Shaul Zorea <shaulzorea@gmail.com>
  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 static java.nio.charset.StandardCharsets.UTF_8;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertNull;
  47. import java.beans.Statement;
  48. import java.io.BufferedInputStream;
  49. import java.io.File;
  50. import java.io.FileNotFoundException;
  51. import java.io.FileOutputStream;
  52. import java.io.IOException;
  53. import java.io.InputStream;
  54. import java.io.OutputStream;
  55. import java.nio.file.Files;
  56. import java.util.Arrays;
  57. import java.util.Collections;
  58. import java.util.HashMap;
  59. import java.util.List;
  60. import java.util.Map;
  61. import org.apache.commons.compress.archivers.ArchiveEntry;
  62. import org.apache.commons.compress.archivers.ArchiveInputStream;
  63. import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
  64. import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
  65. import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
  66. import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
  67. import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
  68. import org.eclipse.jgit.api.errors.AbortedByHookException;
  69. import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
  70. import org.eclipse.jgit.api.errors.GitAPIException;
  71. import org.eclipse.jgit.api.errors.NoFilepatternException;
  72. import org.eclipse.jgit.api.errors.NoHeadException;
  73. import org.eclipse.jgit.api.errors.NoMessageException;
  74. import org.eclipse.jgit.api.errors.UnmergedPathsException;
  75. import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
  76. import org.eclipse.jgit.archive.ArchiveFormats;
  77. import org.eclipse.jgit.errors.AmbiguousObjectException;
  78. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  79. import org.eclipse.jgit.junit.RepositoryTestCase;
  80. import org.eclipse.jgit.lib.FileMode;
  81. import org.eclipse.jgit.lib.ObjectId;
  82. import org.eclipse.jgit.lib.ObjectLoader;
  83. import org.eclipse.jgit.revwalk.RevCommit;
  84. import org.eclipse.jgit.util.IO;
  85. import org.eclipse.jgit.util.StringUtils;
  86. import org.junit.After;
  87. import org.junit.Before;
  88. import org.junit.Test;
  89. public class ArchiveCommandTest extends RepositoryTestCase {
  90. // archives store timestamp with 1 second resolution
  91. private static final int WAIT = 2000;
  92. private static final String UNEXPECTED_ARCHIVE_SIZE = "Unexpected archive size";
  93. private static final String UNEXPECTED_FILE_CONTENTS = "Unexpected file contents";
  94. private static final String UNEXPECTED_TREE_CONTENTS = "Unexpected tree contents";
  95. private static final String UNEXPECTED_LAST_MODIFIED =
  96. "Unexpected lastModified mocked by MockSystemReader, truncated to 1 second";
  97. private static final String UNEXPECTED_DIFFERENT_HASH = "Unexpected different hash";
  98. private MockFormat format = null;
  99. @Before
  100. public void setup() {
  101. format = new MockFormat();
  102. ArchiveCommand.registerFormat(format.SUFFIXES.get(0), format);
  103. ArchiveFormats.registerAll();
  104. }
  105. @Override
  106. @After
  107. public void tearDown() {
  108. ArchiveCommand.unregisterFormat(format.SUFFIXES.get(0));
  109. ArchiveFormats.unregisterAll();
  110. }
  111. @Test
  112. public void archiveHeadAllFiles() throws IOException, GitAPIException {
  113. try (Git git = new Git(db)) {
  114. createTestContent(git);
  115. git.archive().setOutputStream(new MockOutputStream())
  116. .setFormat(format.SUFFIXES.get(0))
  117. .setTree(git.getRepository().resolve("HEAD")).call();
  118. assertEquals(UNEXPECTED_ARCHIVE_SIZE, 2, format.size());
  119. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_1_2", format.getByPath("file_1.txt"));
  120. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_2_2", format.getByPath("file_2.txt"));
  121. }
  122. }
  123. @Test
  124. public void archiveHeadSpecificPath() throws IOException, GitAPIException {
  125. try (Git git = new Git(db)) {
  126. writeTrashFile("file_1.txt", "content_1_1");
  127. git.add().addFilepattern("file_1.txt").call();
  128. git.commit().setMessage("create file").call();
  129. writeTrashFile("file_1.txt", "content_1_2");
  130. String expectedFilePath = "some_directory/file_2.txt";
  131. writeTrashFile(expectedFilePath, "content_2_2");
  132. git.add().addFilepattern(".").call();
  133. git.commit().setMessage("updated file").call();
  134. git.archive().setOutputStream(new MockOutputStream())
  135. .setFormat(format.SUFFIXES.get(0))
  136. .setTree(git.getRepository().resolve("HEAD"))
  137. .setPaths(expectedFilePath).call();
  138. assertEquals(UNEXPECTED_ARCHIVE_SIZE, 2, format.size());
  139. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_2_2", format.getByPath(expectedFilePath));
  140. assertNull(UNEXPECTED_TREE_CONTENTS, format.getByPath("some_directory"));
  141. }
  142. }
  143. @Test
  144. public void archiveByIdSpecificFile() throws IOException, GitAPIException {
  145. try (Git git = new Git(db)) {
  146. writeTrashFile("file_1.txt", "content_1_1");
  147. git.add().addFilepattern("file_1.txt").call();
  148. RevCommit first = git.commit().setMessage("create file").call();
  149. writeTrashFile("file_1.txt", "content_1_2");
  150. String expectedFilePath = "some_directory/file_2.txt";
  151. writeTrashFile(expectedFilePath, "content_2_2");
  152. git.add().addFilepattern(".").call();
  153. git.commit().setMessage("updated file").call();
  154. Map<String, Object> options = new HashMap<>();
  155. Integer opt = Integer.valueOf(42);
  156. options.put("foo", opt);
  157. MockOutputStream out = new MockOutputStream();
  158. git.archive().setOutputStream(out)
  159. .setFormat(format.SUFFIXES.get(0))
  160. .setFormatOptions(options)
  161. .setTree(first)
  162. .setPaths("file_1.txt").call();
  163. assertEquals(opt.intValue(), out.getFoo());
  164. assertEquals(UNEXPECTED_ARCHIVE_SIZE, 1, format.size());
  165. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_1_1", format.getByPath("file_1.txt"));
  166. }
  167. }
  168. @Test
  169. public void archiveByDirectoryPath() throws GitAPIException, IOException {
  170. try (Git git = new Git(db)) {
  171. writeTrashFile("file_0.txt", "content_0_1");
  172. git.add().addFilepattern("file_0.txt").call();
  173. git.commit().setMessage("commit_1").call();
  174. writeTrashFile("file_0.txt", "content_0_2");
  175. String expectedFilePath1 = "some_directory/file_1.txt";
  176. writeTrashFile(expectedFilePath1, "content_1_2");
  177. String expectedFilePath2 = "some_directory/file_2.txt";
  178. writeTrashFile(expectedFilePath2, "content_2_2");
  179. String expectedFilePath3 = "some_directory/nested_directory/file_3.txt";
  180. writeTrashFile(expectedFilePath3, "content_3_2");
  181. git.add().addFilepattern(".").call();
  182. git.commit().setMessage("commit_2").call();
  183. git.archive().setOutputStream(new MockOutputStream())
  184. .setFormat(format.SUFFIXES.get(0))
  185. .setTree(git.getRepository().resolve("HEAD"))
  186. .setPaths("some_directory/").call();
  187. assertEquals(UNEXPECTED_ARCHIVE_SIZE, 5, format.size());
  188. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_1_2", format.getByPath(expectedFilePath1));
  189. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_2_2", format.getByPath(expectedFilePath2));
  190. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_3_2", format.getByPath(expectedFilePath3));
  191. assertNull(UNEXPECTED_TREE_CONTENTS, format.getByPath("some_directory"));
  192. assertNull(UNEXPECTED_TREE_CONTENTS, format.getByPath("some_directory/nested_directory"));
  193. }
  194. }
  195. @Test
  196. public void archiveHeadAllFilesTarTimestamps() throws Exception {
  197. try (Git git = new Git(db)) {
  198. createTestContent(git);
  199. String fmt = "tar";
  200. File archive = new File(getTemporaryDirectory(),
  201. "archive." + format);
  202. archive(git, archive, fmt);
  203. ObjectId hash1 = ObjectId.fromRaw(IO.readFully(archive));
  204. try (InputStream fi = Files.newInputStream(archive.toPath());
  205. InputStream bi = new BufferedInputStream(fi);
  206. ArchiveInputStream o = new TarArchiveInputStream(bi)) {
  207. assertEntries(o);
  208. }
  209. Thread.sleep(WAIT);
  210. archive(git, archive, fmt);
  211. assertEquals(UNEXPECTED_DIFFERENT_HASH, hash1,
  212. ObjectId.fromRaw(IO.readFully(archive)));
  213. }
  214. }
  215. @Test
  216. public void archiveHeadAllFilesTgzTimestamps() throws Exception {
  217. try (Git git = new Git(db)) {
  218. createTestContent(git);
  219. String fmt = "tgz";
  220. File archive = new File(getTemporaryDirectory(),
  221. "archive." + fmt);
  222. archive(git, archive, fmt);
  223. ObjectId hash1 = ObjectId.fromRaw(IO.readFully(archive));
  224. try (InputStream fi = Files.newInputStream(archive.toPath());
  225. InputStream bi = new BufferedInputStream(fi);
  226. InputStream gzi = new GzipCompressorInputStream(bi);
  227. ArchiveInputStream o = new TarArchiveInputStream(gzi)) {
  228. assertEntries(o);
  229. }
  230. Thread.sleep(WAIT);
  231. archive(git, archive, fmt);
  232. assertEquals(UNEXPECTED_DIFFERENT_HASH, hash1,
  233. ObjectId.fromRaw(IO.readFully(archive)));
  234. }
  235. }
  236. @Test
  237. public void archiveHeadAllFilesTbz2Timestamps() throws Exception {
  238. try (Git git = new Git(db)) {
  239. createTestContent(git);
  240. String fmt = "tbz2";
  241. File archive = new File(getTemporaryDirectory(),
  242. "archive." + fmt);
  243. archive(git, archive, fmt);
  244. ObjectId hash1 = ObjectId.fromRaw(IO.readFully(archive));
  245. try (InputStream fi = Files.newInputStream(archive.toPath());
  246. InputStream bi = new BufferedInputStream(fi);
  247. InputStream gzi = new BZip2CompressorInputStream(bi);
  248. ArchiveInputStream o = new TarArchiveInputStream(gzi)) {
  249. assertEntries(o);
  250. }
  251. Thread.sleep(WAIT);
  252. archive(git, archive, fmt);
  253. assertEquals(UNEXPECTED_DIFFERENT_HASH, hash1,
  254. ObjectId.fromRaw(IO.readFully(archive)));
  255. }
  256. }
  257. @Test
  258. public void archiveHeadAllFilesTxzTimestamps() throws Exception {
  259. try (Git git = new Git(db)) {
  260. createTestContent(git);
  261. String fmt = "txz";
  262. File archive = new File(getTemporaryDirectory(), "archive." + fmt);
  263. archive(git, archive, fmt);
  264. ObjectId hash1 = ObjectId.fromRaw(IO.readFully(archive));
  265. try (InputStream fi = Files.newInputStream(archive.toPath());
  266. InputStream bi = new BufferedInputStream(fi);
  267. InputStream gzi = new XZCompressorInputStream(bi);
  268. ArchiveInputStream o = new TarArchiveInputStream(gzi)) {
  269. assertEntries(o);
  270. }
  271. Thread.sleep(WAIT);
  272. archive(git, archive, fmt);
  273. assertEquals(UNEXPECTED_DIFFERENT_HASH, hash1,
  274. ObjectId.fromRaw(IO.readFully(archive)));
  275. }
  276. }
  277. @Test
  278. public void archiveHeadAllFilesZipTimestamps() throws Exception {
  279. try (Git git = new Git(db)) {
  280. createTestContent(git);
  281. String fmt = "zip";
  282. File archive = new File(getTemporaryDirectory(), "archive." + fmt);
  283. archive(git, archive, fmt);
  284. ObjectId hash1 = ObjectId.fromRaw(IO.readFully(archive));
  285. try (InputStream fi = Files.newInputStream(archive.toPath());
  286. InputStream bi = new BufferedInputStream(fi);
  287. ArchiveInputStream o = new ZipArchiveInputStream(bi)) {
  288. assertEntries(o);
  289. }
  290. Thread.sleep(WAIT);
  291. archive(git, archive, fmt);
  292. assertEquals(UNEXPECTED_DIFFERENT_HASH, hash1,
  293. ObjectId.fromRaw(IO.readFully(archive)));
  294. }
  295. }
  296. private void createTestContent(Git git) throws IOException, GitAPIException,
  297. NoFilepatternException, NoHeadException, NoMessageException,
  298. UnmergedPathsException, ConcurrentRefUpdateException,
  299. WrongRepositoryStateException, AbortedByHookException {
  300. writeTrashFile("file_1.txt", "content_1_1");
  301. git.add().addFilepattern("file_1.txt").call();
  302. git.commit().setMessage("create file").call();
  303. writeTrashFile("file_1.txt", "content_1_2");
  304. writeTrashFile("file_2.txt", "content_2_2");
  305. git.add().addFilepattern(".").call();
  306. git.commit().setMessage("updated file").call();
  307. }
  308. private static void archive(Git git, File archive, String fmt)
  309. throws GitAPIException,
  310. FileNotFoundException, AmbiguousObjectException,
  311. IncorrectObjectTypeException, IOException {
  312. git.archive().setOutputStream(new FileOutputStream(archive))
  313. .setFormat(fmt)
  314. .setTree(git.getRepository().resolve("HEAD")).call();
  315. }
  316. private static void assertEntries(ArchiveInputStream o) throws IOException {
  317. ArchiveEntry e;
  318. int n = 0;
  319. while ((e = o.getNextEntry()) != null) {
  320. n++;
  321. assertEquals(UNEXPECTED_LAST_MODIFIED,
  322. (1250379778668L / 1000L) * 1000L,
  323. e.getLastModifiedDate().getTime());
  324. }
  325. assertEquals(UNEXPECTED_ARCHIVE_SIZE, 2, n);
  326. }
  327. private static class MockFormat
  328. implements ArchiveCommand.Format<MockOutputStream> {
  329. private Map<String, String> entries = new HashMap<>();
  330. private int size() {
  331. return entries.size();
  332. }
  333. private String getByPath(String path) {
  334. return entries.get(path);
  335. }
  336. private final List<String> SUFFIXES = Collections
  337. .unmodifiableList(Arrays.asList(".mck"));
  338. @Override
  339. public MockOutputStream createArchiveOutputStream(OutputStream s)
  340. throws IOException {
  341. return createArchiveOutputStream(s,
  342. Collections.<String, Object> emptyMap());
  343. }
  344. @Override
  345. public MockOutputStream createArchiveOutputStream(OutputStream s,
  346. Map<String, Object> o) throws IOException {
  347. for (Map.Entry<String, Object> p : o.entrySet()) {
  348. try {
  349. String methodName = "set"
  350. + StringUtils.capitalize(p.getKey());
  351. new Statement(s, methodName, new Object[] { p.getValue() })
  352. .execute();
  353. } catch (Exception e) {
  354. throw new IOException("cannot set option: " + p.getKey(), e);
  355. }
  356. }
  357. return new MockOutputStream();
  358. }
  359. @Override
  360. public void putEntry(MockOutputStream out, ObjectId tree, String path, FileMode mode, ObjectLoader loader) {
  361. String content = mode != FileMode.TREE
  362. ? new String(loader.getBytes(), UTF_8)
  363. : null;
  364. entries.put(path, content);
  365. }
  366. @Override
  367. public Iterable<String> suffixes() {
  368. return SUFFIXES;
  369. }
  370. }
  371. public static class MockOutputStream extends OutputStream {
  372. private int foo;
  373. public void setFoo(int foo) {
  374. this.foo = foo;
  375. }
  376. public int getFoo() {
  377. return foo;
  378. }
  379. @Override
  380. public void write(int b) throws IOException {
  381. // Do nothing. for testing purposes.
  382. }
  383. }
  384. }