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

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