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

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