選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ArchiveTest.java 24KB

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