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.

ArchiveTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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;
  44. import static org.junit.Assert.assertArrayEquals;
  45. import static org.junit.Assert.fail;
  46. import static org.junit.Assume.assumeNoException;
  47. import java.io.BufferedReader;
  48. import java.io.ByteArrayInputStream;
  49. import java.io.File;
  50. import java.io.FileOutputStream;
  51. import java.io.InputStreamReader;
  52. import java.io.IOException;
  53. import java.io.OutputStream;
  54. import java.lang.Object;
  55. import java.lang.String;
  56. import java.util.ArrayList;
  57. import java.util.Arrays;
  58. import java.util.List;
  59. import java.util.concurrent.Callable;
  60. import java.util.concurrent.Executors;
  61. import java.util.concurrent.ExecutorService;
  62. import java.util.concurrent.Future;
  63. import java.util.zip.ZipEntry;
  64. import java.util.zip.ZipInputStream;
  65. import org.eclipse.jgit.api.Git;
  66. import org.eclipse.jgit.dircache.DirCache;
  67. import org.eclipse.jgit.lib.CLIRepositoryTestCase;
  68. import org.eclipse.jgit.lib.FileMode;
  69. import org.eclipse.jgit.pgm.CLIGitCommand;
  70. import org.junit.Before;
  71. import org.junit.Ignore;
  72. import org.junit.Test;
  73. public class ArchiveTest extends CLIRepositoryTestCase {
  74. private Git git;
  75. private String emptyTree;
  76. @Override
  77. @Before
  78. public void setUp() throws Exception {
  79. super.setUp();
  80. git = new Git(db);
  81. git.commit().setMessage("initial commit").call();
  82. emptyTree = db.resolve("HEAD^{tree}").abbreviate(12).name();
  83. }
  84. @Ignore("Some versions of java.util.zip refuse to write an empty ZIP")
  85. @Test
  86. public void testEmptyArchive() throws Exception {
  87. final byte[] result = CLIGitCommand.rawExecute( //
  88. "git archive " + emptyTree, db);
  89. assertArrayEquals(new String[0], listZipEntries(result));
  90. }
  91. @Test
  92. public void testEmptyTar() throws Exception {
  93. final byte[] result = CLIGitCommand.rawExecute( //
  94. "git archive --format=tar " + emptyTree, db);
  95. assertArrayEquals(new String[0], listTarEntries(result));
  96. }
  97. @Test
  98. public void testArchiveWithFiles() throws Exception {
  99. writeTrashFile("a", "a file with content!");
  100. writeTrashFile("c", ""); // empty file
  101. writeTrashFile("unrelated", "another file, just for kicks");
  102. git.add().addFilepattern("a").call();
  103. git.add().addFilepattern("c").call();
  104. git.commit().setMessage("populate toplevel").call();
  105. final byte[] result = CLIGitCommand.rawExecute( //
  106. "git archive HEAD", db);
  107. assertArrayEquals(new String[] { "a", "c" }, //
  108. listZipEntries(result));
  109. }
  110. @Test
  111. public void testArchiveWithSubdir() throws Exception {
  112. writeTrashFile("a", "a file with content!");
  113. writeTrashFile("b.c", "before subdir in git sort order");
  114. writeTrashFile("b0c", "after subdir in git sort order");
  115. writeTrashFile("c", "");
  116. git.add().addFilepattern("a").call();
  117. git.add().addFilepattern("b.c").call();
  118. git.add().addFilepattern("b0c").call();
  119. git.add().addFilepattern("c").call();
  120. git.commit().setMessage("populate toplevel").call();
  121. writeTrashFile("b/b", "file in subdirectory");
  122. writeTrashFile("b/a", "another file in subdirectory");
  123. git.add().addFilepattern("b").call();
  124. git.commit().setMessage("add subdir").call();
  125. final byte[] result = CLIGitCommand.rawExecute( //
  126. "git archive master", db);
  127. String[] expect = { "a", "b.c", "b0c", "b/a", "b/b", "c" };
  128. String[] actual = listZipEntries(result);
  129. Arrays.sort(expect);
  130. Arrays.sort(actual);
  131. assertArrayEquals(expect, actual);
  132. }
  133. @Test
  134. public void testTarWithSubdir() throws Exception {
  135. writeTrashFile("a", "a file with content!");
  136. writeTrashFile("b.c", "before subdir in git sort order");
  137. writeTrashFile("b0c", "after subdir in git sort order");
  138. writeTrashFile("c", "");
  139. git.add().addFilepattern("a").call();
  140. git.add().addFilepattern("b.c").call();
  141. git.add().addFilepattern("b0c").call();
  142. git.add().addFilepattern("c").call();
  143. git.commit().setMessage("populate toplevel").call();
  144. writeTrashFile("b/b", "file in subdirectory");
  145. writeTrashFile("b/a", "another file in subdirectory");
  146. git.add().addFilepattern("b").call();
  147. git.commit().setMessage("add subdir").call();
  148. final byte[] result = CLIGitCommand.rawExecute( //
  149. "git archive --format=tar master", db);
  150. String[] expect = { "a", "b.c", "b0c", "b/a", "b/b", "c" };
  151. String[] actual = listTarEntries(result);
  152. Arrays.sort(expect);
  153. Arrays.sort(actual);
  154. assertArrayEquals(expect, actual);
  155. }
  156. @Test
  157. public void testArchivePreservesMode() throws Exception {
  158. writeTrashFile("plain", "a file with content");
  159. writeTrashFile("executable", "an executable file");
  160. writeTrashFile("symlink", "plain");
  161. git.add().addFilepattern("plain").call();
  162. git.add().addFilepattern("executable").call();
  163. git.add().addFilepattern("symlink").call();
  164. DirCache cache = db.lockDirCache();
  165. cache.getEntry("executable").setFileMode(FileMode.EXECUTABLE_FILE);
  166. cache.getEntry("symlink").setFileMode(FileMode.SYMLINK);
  167. cache.write();
  168. cache.commit();
  169. cache.unlock();
  170. git.commit().setMessage("three files with different modes").call();
  171. final byte[] zipData = CLIGitCommand.rawExecute( //
  172. "git archive master", db);
  173. writeRaw("zip-with-modes.zip", zipData);
  174. assertContainsEntryWithMode("zip-with-modes.zip", "-rw-", "plain");
  175. assertContainsEntryWithMode("zip-with-modes.zip", "-rwx", "executable");
  176. assertContainsEntryWithMode("zip-with-modes.zip", "l", "symlink");
  177. }
  178. @Test
  179. public void testTarPreservesMode() throws Exception {
  180. writeTrashFile("plain", "a file with content");
  181. writeTrashFile("executable", "an executable file");
  182. writeTrashFile("symlink", "plain");
  183. git.add().addFilepattern("plain").call();
  184. git.add().addFilepattern("executable").call();
  185. git.add().addFilepattern("symlink").call();
  186. DirCache cache = db.lockDirCache();
  187. cache.getEntry("executable").setFileMode(FileMode.EXECUTABLE_FILE);
  188. cache.getEntry("symlink").setFileMode(FileMode.SYMLINK);
  189. cache.write();
  190. cache.commit();
  191. cache.unlock();
  192. git.commit().setMessage("three files with different modes").call();
  193. final byte[] archive = CLIGitCommand.rawExecute( //
  194. "git archive --format=tar master", db);
  195. writeRaw("with-modes.tar", archive);
  196. assertTarContainsEntry("with-modes.tar", "-rw-r--r--", "plain");
  197. assertTarContainsEntry("with-modes.tar", "-rwxr-xr-x", "executable");
  198. assertTarContainsEntry("with-modes.tar", "l", "symlink -> plain");
  199. }
  200. @Test
  201. public void testArchivePreservesContent() throws Exception {
  202. final String payload = "“The quick brown fox jumps over the lazy dog!”";
  203. writeTrashFile("xyzzy", payload);
  204. git.add().addFilepattern("xyzzy").call();
  205. git.commit().setMessage("add file with content").call();
  206. final byte[] result = CLIGitCommand.rawExecute( //
  207. "git archive HEAD", db);
  208. assertArrayEquals(new String[] { payload }, //
  209. zipEntryContent(result, "xyzzy"));
  210. }
  211. @Test
  212. public void testTarPreservesContent() throws Exception {
  213. final String payload = "“The quick brown fox jumps over the lazy dog!”";
  214. writeTrashFile("xyzzy", payload);
  215. git.add().addFilepattern("xyzzy").call();
  216. git.commit().setMessage("add file with content").call();
  217. final byte[] result = CLIGitCommand.rawExecute( //
  218. "git archive --format=tar HEAD", db);
  219. assertArrayEquals(new String[] { payload }, //
  220. tarEntryContent(result, "xyzzy"));
  221. }
  222. private Process spawnAssumingCommandPresent(String... cmdline) {
  223. final File cwd = db.getWorkTree();
  224. final ProcessBuilder procBuilder = new ProcessBuilder(cmdline) //
  225. .directory(cwd) //
  226. .redirectErrorStream(true);
  227. Process proc = null;
  228. try {
  229. proc = procBuilder.start();
  230. } catch (IOException e) {
  231. // On machines without `cmdline[0]`, let the test pass.
  232. assumeNoException(e);
  233. }
  234. return proc;
  235. }
  236. private BufferedReader readFromProcess(Process proc) throws Exception {
  237. return new BufferedReader( //
  238. new InputStreamReader(proc.getInputStream(), "UTF-8"));
  239. }
  240. private void grepForEntry(String name, String mode, String... cmdline) //
  241. throws Exception {
  242. final Process proc = spawnAssumingCommandPresent(cmdline);
  243. proc.getOutputStream().close();
  244. final BufferedReader reader = readFromProcess(proc);
  245. try {
  246. String line;
  247. while ((line = reader.readLine()) != null)
  248. if (line.startsWith(mode) && line.endsWith(name))
  249. // found it!
  250. return;
  251. fail("expected entry " + name + " with mode " + mode + " but found none");
  252. } finally {
  253. proc.getOutputStream().close();
  254. proc.destroy();
  255. }
  256. }
  257. private void assertContainsEntryWithMode(String zipFilename, String mode, String name) //
  258. throws Exception {
  259. grepForEntry(name, mode, "zipinfo", zipFilename);
  260. }
  261. private void assertTarContainsEntry(String tarfile, String mode, String name) //
  262. throws Exception {
  263. grepForEntry(name, mode, "tar", "tvf", tarfile);
  264. }
  265. private void writeRaw(String filename, byte[] data) //
  266. throws IOException {
  267. final File path = new File(db.getWorkTree(), filename);
  268. final OutputStream out = new FileOutputStream(path);
  269. try {
  270. out.write(data);
  271. } finally {
  272. out.close();
  273. }
  274. }
  275. private static String[] listZipEntries(byte[] zipData) throws IOException {
  276. final List<String> l = new ArrayList<String>();
  277. final ZipInputStream in = new ZipInputStream( //
  278. new ByteArrayInputStream(zipData));
  279. ZipEntry e;
  280. while ((e = in.getNextEntry()) != null)
  281. l.add(e.getName());
  282. in.close();
  283. return l.toArray(new String[l.size()]);
  284. }
  285. private static Future<Object> writeAsync(final OutputStream stream, final byte[] data) {
  286. final ExecutorService executor = Executors.newSingleThreadExecutor();
  287. return executor.submit(new Callable<Object>() { //
  288. public Object call() throws IOException {
  289. try {
  290. stream.write(data);
  291. return null;
  292. } finally {
  293. stream.close();
  294. }
  295. }
  296. });
  297. }
  298. private String[] listTarEntries(byte[] tarData) throws Exception {
  299. final List<String> l = new ArrayList<String>();
  300. final Process proc = spawnAssumingCommandPresent("tar", "tf", "-");
  301. final BufferedReader reader = readFromProcess(proc);
  302. final OutputStream out = proc.getOutputStream();
  303. // Dump tarball to tar stdin in background
  304. final Future<?> writing = writeAsync(out, tarData);
  305. try {
  306. String line;
  307. while ((line = reader.readLine()) != null)
  308. l.add(line);
  309. return l.toArray(new String[l.size()]);
  310. } finally {
  311. writing.get();
  312. reader.close();
  313. proc.destroy();
  314. }
  315. }
  316. private static String[] zipEntryContent(byte[] zipData, String path) //
  317. throws IOException {
  318. final ZipInputStream in = new ZipInputStream( //
  319. new ByteArrayInputStream(zipData));
  320. ZipEntry e;
  321. while ((e = in.getNextEntry()) != null) {
  322. if (!e.getName().equals(path))
  323. continue;
  324. // found!
  325. final List<String> l = new ArrayList<String>();
  326. final BufferedReader reader = new BufferedReader( //
  327. new InputStreamReader(in, "UTF-8"));
  328. String line;
  329. while ((line = reader.readLine()) != null)
  330. l.add(line);
  331. return l.toArray(new String[l.size()]);
  332. }
  333. // not found
  334. return null;
  335. }
  336. private String[] tarEntryContent(byte[] tarData, String path) //
  337. throws Exception {
  338. final List<String> l = new ArrayList<String>();
  339. final Process proc = spawnAssumingCommandPresent("tar", "Oxf", "-", path);
  340. final BufferedReader reader = readFromProcess(proc);
  341. final OutputStream out = proc.getOutputStream();
  342. final Future<?> writing = writeAsync(out, tarData);
  343. try {
  344. String line;
  345. while ((line = reader.readLine()) != null)
  346. l.add(line);
  347. return l.toArray(new String[l.size()]);
  348. } finally {
  349. writing.get();
  350. reader.close();
  351. proc.destroy();
  352. }
  353. }
  354. }