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

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