Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ArchiveTest.java 24KB

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