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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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.assertEquals;
  46. import static org.junit.Assert.fail;
  47. import static org.junit.Assume.assumeNoException;
  48. import java.io.BufferedInputStream;
  49. import java.io.BufferedReader;
  50. import java.io.ByteArrayInputStream;
  51. import java.io.File;
  52. import java.io.FileInputStream;
  53. import java.io.FileOutputStream;
  54. import java.io.InputStreamReader;
  55. import java.io.IOException;
  56. import java.io.OutputStream;
  57. import java.lang.Object;
  58. import java.lang.String;
  59. import java.util.ArrayList;
  60. import java.util.Arrays;
  61. import java.util.List;
  62. import java.util.concurrent.Callable;
  63. import java.util.concurrent.Executors;
  64. import java.util.concurrent.ExecutorService;
  65. import java.util.concurrent.Future;
  66. import java.util.zip.ZipEntry;
  67. import java.util.zip.ZipInputStream;
  68. import org.eclipse.jgit.api.Git;
  69. import org.eclipse.jgit.dircache.DirCache;
  70. import org.eclipse.jgit.lib.CLIRepositoryTestCase;
  71. import org.eclipse.jgit.lib.FileMode;
  72. import org.eclipse.jgit.pgm.CLIGitCommand;
  73. import org.junit.Before;
  74. import org.junit.Ignore;
  75. import org.junit.Test;
  76. public class ArchiveTest extends CLIRepositoryTestCase {
  77. private Git git;
  78. private String emptyTree;
  79. @Override
  80. @Before
  81. public void setUp() throws Exception {
  82. super.setUp();
  83. git = new Git(db);
  84. git.commit().setMessage("initial commit").call();
  85. emptyTree = db.resolve("HEAD^{tree}").abbreviate(12).name();
  86. }
  87. @Ignore("Some versions of java.util.zip refuse to write an empty ZIP")
  88. @Test
  89. public void testEmptyArchive() throws Exception {
  90. final byte[] result = CLIGitCommand.rawExecute( //
  91. "git archive --format=zip " + emptyTree, db);
  92. assertArrayEquals(new String[0], listZipEntries(result));
  93. }
  94. @Test
  95. public void testEmptyTar() throws Exception {
  96. final byte[] result = CLIGitCommand.rawExecute( //
  97. "git archive --format=tar " + emptyTree, db);
  98. assertArrayEquals(new String[0], listTarEntries(result));
  99. }
  100. @Test
  101. public void testUnrecognizedFormat() throws Exception {
  102. final String[] expect = new String[] { "fatal: Unknown archive format 'nonsense'" };
  103. final String[] actual = execute("git archive --format=nonsense " + emptyTree);
  104. assertArrayEquals(expect, actual);
  105. }
  106. @Test
  107. public void testArchiveWithFiles() throws Exception {
  108. writeTrashFile("a", "a file with content!");
  109. writeTrashFile("c", ""); // empty file
  110. writeTrashFile("unrelated", "another file, just for kicks");
  111. git.add().addFilepattern("a").call();
  112. git.add().addFilepattern("c").call();
  113. git.commit().setMessage("populate toplevel").call();
  114. final byte[] result = CLIGitCommand.rawExecute( //
  115. "git archive --format=zip HEAD", db);
  116. assertArrayEquals(new String[] { "a", "c" }, //
  117. listZipEntries(result));
  118. }
  119. private void commitGreeting() throws Exception {
  120. writeTrashFile("greeting", "hello, world!");
  121. git.add().addFilepattern("greeting").call();
  122. git.commit().setMessage("a commit with a file").call();
  123. }
  124. @Test
  125. public void testDefaultFormatIsTar() throws Exception {
  126. commitGreeting();
  127. final byte[] result = CLIGitCommand.rawExecute( //
  128. "git archive HEAD", db);
  129. assertArrayEquals(new String[] { "greeting" }, //
  130. listTarEntries(result));
  131. }
  132. private static String shellQuote(String s) {
  133. return "'" + s.replace("'", "'\\''") + "'";
  134. }
  135. @Test
  136. public void testFormatOverridesFilename() throws Exception {
  137. final File archive = new File(db.getWorkTree(), "format-overrides-name.tar");
  138. final String path = archive.getAbsolutePath();
  139. commitGreeting();
  140. assertArrayEquals(new String[] { "" },
  141. execute("git archive " +
  142. "--format=zip " +
  143. shellQuote("--output=" + path) + " " +
  144. "HEAD"));
  145. assertContainsEntryWithMode(path, "", "greeting");
  146. assertIsZip(archive);
  147. }
  148. @Test
  149. public void testUnrecognizedExtensionMeansTar() throws Exception {
  150. final File archive = new File(db.getWorkTree(), "example.txt");
  151. final String path = archive.getAbsolutePath();
  152. commitGreeting();
  153. assertArrayEquals(new String[] { "" },
  154. execute("git archive " +
  155. shellQuote("--output=" + path) + " " +
  156. "HEAD"));
  157. assertTarContainsEntry(path, "", "greeting");
  158. assertIsTar(archive);
  159. }
  160. @Test
  161. public void testNoExtensionMeansTar() throws Exception {
  162. final File archive = new File(db.getWorkTree(), "example");
  163. final String path = archive.getAbsolutePath();
  164. commitGreeting();
  165. assertArrayEquals(new String[] { "" },
  166. execute("git archive " +
  167. shellQuote("--output=" + path) + " " +
  168. "HEAD"));
  169. assertIsTar(archive);
  170. }
  171. @Test
  172. public void testExtensionMatchIsAnchored() throws Exception {
  173. final File archive = new File(db.getWorkTree(), "two-extensions.zip.bak");
  174. final String path = archive.getAbsolutePath();
  175. commitGreeting();
  176. assertArrayEquals(new String[] { "" },
  177. execute("git archive " +
  178. shellQuote("--output=" + path) + " " +
  179. "HEAD"));
  180. assertIsTar(archive);
  181. }
  182. @Test
  183. public void testZipExtension() throws Exception {
  184. final File archiveWithDot = new File(db.getWorkTree(), "greeting.zip");
  185. final File archiveNoDot = new File(db.getWorkTree(), "greetingzip");
  186. commitGreeting();
  187. execute("git archive " +
  188. shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " +
  189. "HEAD");
  190. execute("git archive " +
  191. shellQuote("--output=" + archiveNoDot.getAbsolutePath()) + " " +
  192. "HEAD");
  193. assertIsZip(archiveWithDot);
  194. assertIsTar(archiveNoDot);
  195. }
  196. @Test
  197. public void testTarExtension() throws Exception {
  198. final File archive = new File(db.getWorkTree(), "tarball.tar");
  199. final String path = archive.getAbsolutePath();
  200. commitGreeting();
  201. assertArrayEquals(new String[] { "" },
  202. execute("git archive " +
  203. shellQuote("--output=" + path) + " " +
  204. "HEAD"));
  205. assertIsTar(archive);
  206. }
  207. @Test
  208. public void testTgzExtensions() throws Exception {
  209. commitGreeting();
  210. for (String ext : Arrays.asList("tar.gz", "tgz")) {
  211. final File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext);
  212. final File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext);
  213. execute("git archive " +
  214. shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " +
  215. "HEAD");
  216. execute("git archive " +
  217. shellQuote("--output=" + archiveNoDot.getAbsolutePath()) + " " +
  218. "HEAD");
  219. assertIsGzip(archiveWithDot);
  220. assertIsTar(archiveNoDot);
  221. }
  222. }
  223. @Test
  224. public void testTbz2Extension() throws Exception {
  225. commitGreeting();
  226. for (String ext : Arrays.asList("tar.bz2", "tbz", "tbz2")) {
  227. final File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext);
  228. final File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext);
  229. execute("git archive " +
  230. shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " +
  231. "HEAD");
  232. execute("git archive " +
  233. shellQuote("--output=" + archiveNoDot.getAbsolutePath()) + " " +
  234. "HEAD");
  235. assertIsBzip2(archiveWithDot);
  236. assertIsTar(archiveNoDot);
  237. }
  238. }
  239. @Test
  240. public void testTxzExtension() throws Exception {
  241. commitGreeting();
  242. for (String ext : Arrays.asList("tar.xz", "txz")) {
  243. final File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext);
  244. final File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext);
  245. execute("git archive " +
  246. shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " +
  247. "HEAD");
  248. execute("git archive " +
  249. shellQuote("--output=" + archiveNoDot.getAbsolutePath()) + " " +
  250. "HEAD");
  251. assertIsXz(archiveWithDot);
  252. assertIsTar(archiveNoDot);
  253. }
  254. }
  255. @Test
  256. public void testArchiveWithSubdir() throws Exception {
  257. writeTrashFile("a", "a file with content!");
  258. writeTrashFile("b.c", "before subdir in git sort order");
  259. writeTrashFile("b0c", "after subdir in git sort order");
  260. writeTrashFile("c", "");
  261. git.add().addFilepattern("a").call();
  262. git.add().addFilepattern("b.c").call();
  263. git.add().addFilepattern("b0c").call();
  264. git.add().addFilepattern("c").call();
  265. git.commit().setMessage("populate toplevel").call();
  266. writeTrashFile("b/b", "file in subdirectory");
  267. writeTrashFile("b/a", "another file in subdirectory");
  268. git.add().addFilepattern("b").call();
  269. git.commit().setMessage("add subdir").call();
  270. final byte[] result = CLIGitCommand.rawExecute( //
  271. "git archive --format=zip master", db);
  272. String[] expect = { "a", "b.c", "b0c", "b/", "b/a", "b/b", "c" };
  273. String[] actual = listZipEntries(result);
  274. Arrays.sort(expect);
  275. Arrays.sort(actual);
  276. assertArrayEquals(expect, actual);
  277. }
  278. @Test
  279. public void testTarWithSubdir() throws Exception {
  280. writeTrashFile("a", "a file with content!");
  281. writeTrashFile("b.c", "before subdir in git sort order");
  282. writeTrashFile("b0c", "after subdir in git sort order");
  283. writeTrashFile("c", "");
  284. git.add().addFilepattern("a").call();
  285. git.add().addFilepattern("b.c").call();
  286. git.add().addFilepattern("b0c").call();
  287. git.add().addFilepattern("c").call();
  288. git.commit().setMessage("populate toplevel").call();
  289. writeTrashFile("b/b", "file in subdirectory");
  290. writeTrashFile("b/a", "another file in subdirectory");
  291. git.add().addFilepattern("b").call();
  292. git.commit().setMessage("add subdir").call();
  293. final byte[] result = CLIGitCommand.rawExecute( //
  294. "git archive --format=tar master", db);
  295. String[] expect = { "a", "b.c", "b0c", "b/", "b/a", "b/b", "c" };
  296. String[] actual = listTarEntries(result);
  297. Arrays.sort(expect);
  298. Arrays.sort(actual);
  299. assertArrayEquals(expect, actual);
  300. }
  301. private void commitBazAndFooSlashBar() throws Exception {
  302. writeTrashFile("baz", "a file");
  303. writeTrashFile("foo/bar", "another file");
  304. git.add().addFilepattern("baz").call();
  305. git.add().addFilepattern("foo").call();
  306. git.commit().setMessage("sample commit").call();
  307. }
  308. @Test
  309. public void testArchivePrefixOption() throws Exception {
  310. commitBazAndFooSlashBar();
  311. byte[] result = CLIGitCommand.rawExecute(
  312. "git archive --prefix=x/ --format=zip master", db);
  313. String[] expect = { "x/baz", "x/foo/", "x/foo/bar" };
  314. String[] actual = listZipEntries(result);
  315. Arrays.sort(expect);
  316. Arrays.sort(actual);
  317. assertArrayEquals(expect, actual);
  318. }
  319. @Test
  320. public void testTarPrefixOption() throws Exception {
  321. commitBazAndFooSlashBar();
  322. byte[] result = CLIGitCommand.rawExecute(
  323. "git archive --prefix=x/ --format=tar master", db);
  324. String[] expect = { "x/baz", "x/foo/", "x/foo/bar" };
  325. String[] actual = listTarEntries(result);
  326. Arrays.sort(expect);
  327. Arrays.sort(actual);
  328. assertArrayEquals(expect, actual);
  329. }
  330. private void commitFoo() throws Exception {
  331. writeTrashFile("foo", "a file");
  332. git.add().addFilepattern("foo").call();
  333. git.commit().setMessage("boring commit").call();
  334. }
  335. @Test
  336. public void testPrefixDoesNotNormalizeDoubleSlash() throws Exception {
  337. commitFoo();
  338. byte[] result = CLIGitCommand.rawExecute(
  339. "git archive --prefix=x// --format=zip master", db);
  340. String[] expect = { "x//foo" };
  341. assertArrayEquals(expect, listZipEntries(result));
  342. }
  343. @Test
  344. public void testPrefixDoesNotNormalizeDoubleSlashInTar() throws Exception {
  345. commitFoo();
  346. final byte[] result = CLIGitCommand.rawExecute( //
  347. "git archive --prefix=x// --format=tar master", db);
  348. String[] expect = { "x//foo" };
  349. assertArrayEquals(expect, listTarEntries(result));
  350. }
  351. /**
  352. * The prefix passed to "git archive" need not end with '/'.
  353. * In practice it is not very common to have a nonempty prefix
  354. * that does not name a directory (and hence end with /), but
  355. * since git has historically supported other prefixes, we do,
  356. * too.
  357. *
  358. * @throws Exception
  359. */
  360. @Test
  361. public void testPrefixWithoutTrailingSlash() throws Exception {
  362. commitBazAndFooSlashBar();
  363. byte[] result = CLIGitCommand.rawExecute(
  364. "git archive --prefix=my- --format=zip master", db);
  365. String[] expect = { "my-baz", "my-foo/", "my-foo/bar" };
  366. String[] actual = listZipEntries(result);
  367. Arrays.sort(expect);
  368. Arrays.sort(actual);
  369. assertArrayEquals(expect, actual);
  370. }
  371. @Test
  372. public void testTarPrefixWithoutTrailingSlash() throws Exception {
  373. commitBazAndFooSlashBar();
  374. final byte[] result = CLIGitCommand.rawExecute( //
  375. "git archive --prefix=my- --format=tar master", db);
  376. String[] expect = { "my-baz", "my-foo/", "my-foo/bar" };
  377. String[] actual = listTarEntries(result);
  378. Arrays.sort(expect);
  379. Arrays.sort(actual);
  380. assertArrayEquals(expect, actual);
  381. }
  382. @Test
  383. public void testArchivePreservesMode() throws Exception {
  384. writeTrashFile("plain", "a file with content");
  385. writeTrashFile("executable", "an executable file");
  386. writeTrashFile("symlink", "plain");
  387. writeTrashFile("dir/content", "clutter in a subdir");
  388. git.add().addFilepattern("plain").call();
  389. git.add().addFilepattern("executable").call();
  390. git.add().addFilepattern("symlink").call();
  391. git.add().addFilepattern("dir").call();
  392. DirCache cache = db.lockDirCache();
  393. cache.getEntry("executable").setFileMode(FileMode.EXECUTABLE_FILE);
  394. cache.getEntry("symlink").setFileMode(FileMode.SYMLINK);
  395. cache.write();
  396. cache.commit();
  397. cache.unlock();
  398. git.commit().setMessage("three files with different modes").call();
  399. final byte[] zipData = CLIGitCommand.rawExecute( //
  400. "git archive --format=zip master", db);
  401. writeRaw("zip-with-modes.zip", zipData);
  402. assertContainsEntryWithMode("zip-with-modes.zip", "-rw-", "plain");
  403. assertContainsEntryWithMode("zip-with-modes.zip", "-rwx", "executable");
  404. assertContainsEntryWithMode("zip-with-modes.zip", "l", "symlink");
  405. assertContainsEntryWithMode("zip-with-modes.zip", "-rw-", "dir/");
  406. }
  407. @Test
  408. public void testTarPreservesMode() throws Exception {
  409. writeTrashFile("plain", "a file with content");
  410. writeTrashFile("executable", "an executable file");
  411. writeTrashFile("symlink", "plain");
  412. writeTrashFile("dir/content", "clutter in a subdir");
  413. git.add().addFilepattern("plain").call();
  414. git.add().addFilepattern("executable").call();
  415. git.add().addFilepattern("symlink").call();
  416. git.add().addFilepattern("dir").call();
  417. DirCache cache = db.lockDirCache();
  418. cache.getEntry("executable").setFileMode(FileMode.EXECUTABLE_FILE);
  419. cache.getEntry("symlink").setFileMode(FileMode.SYMLINK);
  420. cache.write();
  421. cache.commit();
  422. cache.unlock();
  423. git.commit().setMessage("three files with different modes").call();
  424. final byte[] archive = CLIGitCommand.rawExecute( //
  425. "git archive --format=tar master", db);
  426. writeRaw("with-modes.tar", archive);
  427. assertTarContainsEntry("with-modes.tar", "-rw-r--r--", "plain");
  428. assertTarContainsEntry("with-modes.tar", "-rwxr-xr-x", "executable");
  429. assertTarContainsEntry("with-modes.tar", "l", "symlink -> plain");
  430. assertTarContainsEntry("with-modes.tar", "drwxr-xr-x", "dir/");
  431. }
  432. @Test
  433. public void testArchiveWithLongFilename() throws Exception {
  434. String filename = "";
  435. final List<String> l = new ArrayList<String>();
  436. for (int i = 0; i < 20; i++) {
  437. filename = filename + "1234567890/";
  438. l.add(filename);
  439. }
  440. filename = filename + "1234567890";
  441. l.add(filename);
  442. writeTrashFile(filename, "file with long path");
  443. git.add().addFilepattern("1234567890").call();
  444. git.commit().setMessage("file with long name").call();
  445. final byte[] result = CLIGitCommand.rawExecute( //
  446. "git archive --format=zip HEAD", db);
  447. assertArrayEquals(l.toArray(new String[l.size()]),
  448. listZipEntries(result));
  449. }
  450. @Test
  451. public void testTarWithLongFilename() throws Exception {
  452. String filename = "";
  453. final List<String> l = new ArrayList<String>();
  454. for (int i = 0; i < 20; i++) {
  455. filename = filename + "1234567890/";
  456. l.add(filename);
  457. }
  458. filename = filename + "1234567890";
  459. l.add(filename);
  460. writeTrashFile(filename, "file with long path");
  461. git.add().addFilepattern("1234567890").call();
  462. git.commit().setMessage("file with long name").call();
  463. final byte[] result = CLIGitCommand.rawExecute( //
  464. "git archive --format=tar HEAD", db);
  465. assertArrayEquals(l.toArray(new String[l.size()]),
  466. listTarEntries(result));
  467. }
  468. @Test
  469. public void testArchivePreservesContent() throws Exception {
  470. final String payload = "“The quick brown fox jumps over the lazy dog!”";
  471. writeTrashFile("xyzzy", payload);
  472. git.add().addFilepattern("xyzzy").call();
  473. git.commit().setMessage("add file with content").call();
  474. final byte[] result = CLIGitCommand.rawExecute( //
  475. "git archive --format=zip HEAD", db);
  476. assertArrayEquals(new String[] { payload }, //
  477. zipEntryContent(result, "xyzzy"));
  478. }
  479. @Test
  480. public void testTarPreservesContent() throws Exception {
  481. final String payload = "“The quick brown fox jumps over the lazy dog!”";
  482. writeTrashFile("xyzzy", payload);
  483. git.add().addFilepattern("xyzzy").call();
  484. git.commit().setMessage("add file with content").call();
  485. final byte[] result = CLIGitCommand.rawExecute( //
  486. "git archive --format=tar HEAD", db);
  487. assertArrayEquals(new String[] { payload }, //
  488. tarEntryContent(result, "xyzzy"));
  489. }
  490. private Process spawnAssumingCommandPresent(String... cmdline) {
  491. final File cwd = db.getWorkTree();
  492. final ProcessBuilder procBuilder = new ProcessBuilder(cmdline) //
  493. .directory(cwd) //
  494. .redirectErrorStream(true);
  495. Process proc = null;
  496. try {
  497. proc = procBuilder.start();
  498. } catch (IOException e) {
  499. // On machines without `cmdline[0]`, let the test pass.
  500. assumeNoException(e);
  501. }
  502. return proc;
  503. }
  504. private BufferedReader readFromProcess(Process proc) throws Exception {
  505. return new BufferedReader( //
  506. new InputStreamReader(proc.getInputStream(), "UTF-8"));
  507. }
  508. private void grepForEntry(String name, String mode, String... cmdline) //
  509. throws Exception {
  510. final Process proc = spawnAssumingCommandPresent(cmdline);
  511. proc.getOutputStream().close();
  512. final BufferedReader reader = readFromProcess(proc);
  513. try {
  514. String line;
  515. while ((line = reader.readLine()) != null)
  516. if (line.startsWith(mode) && line.endsWith(name))
  517. // found it!
  518. return;
  519. fail("expected entry " + name + " with mode " + mode + " but found none");
  520. } finally {
  521. proc.getOutputStream().close();
  522. proc.destroy();
  523. }
  524. }
  525. private void assertMagic(long offset, byte[] magicBytes, File file) throws Exception {
  526. BufferedInputStream in = new BufferedInputStream(
  527. new FileInputStream(file));
  528. try {
  529. in.skip(offset);
  530. byte[] actual = new byte[magicBytes.length];
  531. in.read(actual);
  532. assertArrayEquals(magicBytes, actual);
  533. } finally {
  534. in.close();
  535. }
  536. }
  537. private void assertMagic(byte[] magicBytes, File file) throws Exception {
  538. assertMagic(0, magicBytes, file);
  539. }
  540. private void assertIsTar(File file) throws Exception {
  541. assertMagic(257, new byte[] { 'u', 's', 't', 'a', 'r', 0 }, file);
  542. }
  543. private void assertIsZip(File file) throws Exception {
  544. assertMagic(new byte[] { 'P', 'K', 3, 4 }, file);
  545. }
  546. private void assertIsGzip(File file) throws Exception {
  547. assertMagic(new byte[] { 037, (byte) 0213 }, file);
  548. }
  549. private void assertIsBzip2(File file) throws Exception {
  550. assertMagic(new byte[] { 'B', 'Z', 'h' }, file);
  551. }
  552. private void assertIsXz(File file) throws Exception {
  553. assertMagic(new byte[] { (byte) 0xfd, '7', 'z', 'X', 'Z', 0 }, file);
  554. }
  555. private void assertContainsEntryWithMode(String zipFilename, String mode, String name) //
  556. throws Exception {
  557. grepForEntry(name, mode, "zipinfo", zipFilename);
  558. }
  559. private void assertTarContainsEntry(String tarfile, String mode, String name) //
  560. throws Exception {
  561. grepForEntry(name, mode, "tar", "tvf", tarfile);
  562. }
  563. private void writeRaw(String filename, byte[] data) //
  564. throws IOException {
  565. final File path = new File(db.getWorkTree(), filename);
  566. final OutputStream out = new FileOutputStream(path);
  567. try {
  568. out.write(data);
  569. } finally {
  570. out.close();
  571. }
  572. }
  573. private static String[] listZipEntries(byte[] zipData) throws IOException {
  574. final List<String> l = new ArrayList<String>();
  575. final ZipInputStream in = new ZipInputStream( //
  576. new ByteArrayInputStream(zipData));
  577. ZipEntry e;
  578. while ((e = in.getNextEntry()) != null)
  579. l.add(e.getName());
  580. in.close();
  581. return l.toArray(new String[l.size()]);
  582. }
  583. private static Future<Object> writeAsync(final OutputStream stream, final byte[] data) {
  584. final ExecutorService executor = Executors.newSingleThreadExecutor();
  585. return executor.submit(new Callable<Object>() { //
  586. public Object call() throws IOException {
  587. try {
  588. stream.write(data);
  589. return null;
  590. } finally {
  591. stream.close();
  592. }
  593. }
  594. });
  595. }
  596. private String[] listTarEntries(byte[] tarData) throws Exception {
  597. final List<String> l = new ArrayList<String>();
  598. final Process proc = spawnAssumingCommandPresent("tar", "tf", "-");
  599. final BufferedReader reader = readFromProcess(proc);
  600. final OutputStream out = proc.getOutputStream();
  601. // Dump tarball to tar stdin in background
  602. final Future<?> writing = writeAsync(out, tarData);
  603. try {
  604. String line;
  605. while ((line = reader.readLine()) != null)
  606. l.add(line);
  607. return l.toArray(new String[l.size()]);
  608. } finally {
  609. writing.get();
  610. reader.close();
  611. proc.destroy();
  612. }
  613. }
  614. private static String[] zipEntryContent(byte[] zipData, String path) //
  615. throws IOException {
  616. final ZipInputStream in = new ZipInputStream( //
  617. new ByteArrayInputStream(zipData));
  618. ZipEntry e;
  619. while ((e = in.getNextEntry()) != null) {
  620. if (!e.getName().equals(path))
  621. continue;
  622. // found!
  623. final List<String> l = new ArrayList<String>();
  624. final BufferedReader reader = new BufferedReader( //
  625. new InputStreamReader(in, "UTF-8"));
  626. String line;
  627. while ((line = reader.readLine()) != null)
  628. l.add(line);
  629. return l.toArray(new String[l.size()]);
  630. }
  631. // not found
  632. return null;
  633. }
  634. private String[] tarEntryContent(byte[] tarData, String path) //
  635. throws Exception {
  636. final List<String> l = new ArrayList<String>();
  637. final Process proc = spawnAssumingCommandPresent("tar", "Oxf", "-", path);
  638. final BufferedReader reader = readFromProcess(proc);
  639. final OutputStream out = proc.getOutputStream();
  640. final Future<?> writing = writeAsync(out, tarData);
  641. try {
  642. String line;
  643. while ((line = reader.readLine()) != null)
  644. l.add(line);
  645. return l.toArray(new String[l.size()]);
  646. } finally {
  647. writing.get();
  648. reader.close();
  649. proc.destroy();
  650. }
  651. }
  652. }