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.

ApplyCommandTest.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  2. * Copyright (C) 2011, 2021 IBM Corporation 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.api;
  11. import static org.junit.Assert.assertArrayEquals;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertFalse;
  14. import static org.junit.Assert.assertTrue;
  15. import static org.junit.Assert.fail;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.nio.file.Files;
  22. import org.eclipse.jgit.api.errors.PatchApplyException;
  23. import org.eclipse.jgit.api.errors.PatchFormatException;
  24. import org.eclipse.jgit.attributes.FilterCommand;
  25. import org.eclipse.jgit.attributes.FilterCommandFactory;
  26. import org.eclipse.jgit.attributes.FilterCommandRegistry;
  27. import org.eclipse.jgit.diff.RawText;
  28. import org.eclipse.jgit.junit.RepositoryTestCase;
  29. import org.eclipse.jgit.lib.Config;
  30. import org.eclipse.jgit.lib.ConfigConstants;
  31. import org.eclipse.jgit.util.IO;
  32. import org.junit.Test;
  33. public class ApplyCommandTest extends RepositoryTestCase {
  34. private RawText a;
  35. private RawText b;
  36. private ApplyResult init(String name) throws Exception {
  37. return init(name, true, true);
  38. }
  39. private ApplyResult init(final String name, final boolean preExists,
  40. final boolean postExists) throws Exception {
  41. try (Git git = new Git(db)) {
  42. if (preExists) {
  43. a = new RawText(readFile(name + "_PreImage"));
  44. write(new File(db.getDirectory().getParent(), name),
  45. a.getString(0, a.size(), false));
  46. git.add().addFilepattern(name).call();
  47. git.commit().setMessage("PreImage").call();
  48. }
  49. if (postExists) {
  50. b = new RawText(readFile(name + "_PostImage"));
  51. }
  52. return git
  53. .apply()
  54. .setPatch(getTestResource(name + ".patch")).call();
  55. }
  56. }
  57. @Test
  58. public void testCrLf() throws Exception {
  59. try {
  60. db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  61. ConfigConstants.CONFIG_KEY_AUTOCRLF, true);
  62. ApplyResult result = init("crlf", true, true);
  63. assertEquals(1, result.getUpdatedFiles().size());
  64. assertEquals(new File(db.getWorkTree(), "crlf"),
  65. result.getUpdatedFiles().get(0));
  66. checkFile(new File(db.getWorkTree(), "crlf"),
  67. b.getString(0, b.size(), false));
  68. } finally {
  69. db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null,
  70. ConfigConstants.CONFIG_KEY_AUTOCRLF);
  71. }
  72. }
  73. @Test
  74. public void testCrLfOff() throws Exception {
  75. try {
  76. db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  77. ConfigConstants.CONFIG_KEY_AUTOCRLF, false);
  78. ApplyResult result = init("crlf", true, true);
  79. assertEquals(1, result.getUpdatedFiles().size());
  80. assertEquals(new File(db.getWorkTree(), "crlf"),
  81. result.getUpdatedFiles().get(0));
  82. checkFile(new File(db.getWorkTree(), "crlf"),
  83. b.getString(0, b.size(), false));
  84. } finally {
  85. db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null,
  86. ConfigConstants.CONFIG_KEY_AUTOCRLF);
  87. }
  88. }
  89. @Test
  90. public void testCrLfEmptyCommitted() throws Exception {
  91. try {
  92. db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  93. ConfigConstants.CONFIG_KEY_AUTOCRLF, true);
  94. ApplyResult result = init("crlf3", true, true);
  95. assertEquals(1, result.getUpdatedFiles().size());
  96. assertEquals(new File(db.getWorkTree(), "crlf3"),
  97. result.getUpdatedFiles().get(0));
  98. checkFile(new File(db.getWorkTree(), "crlf3"),
  99. b.getString(0, b.size(), false));
  100. } finally {
  101. db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null,
  102. ConfigConstants.CONFIG_KEY_AUTOCRLF);
  103. }
  104. }
  105. @Test
  106. public void testCrLfNewFile() throws Exception {
  107. try {
  108. db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  109. ConfigConstants.CONFIG_KEY_AUTOCRLF, true);
  110. ApplyResult result = init("crlf4", false, true);
  111. assertEquals(1, result.getUpdatedFiles().size());
  112. assertEquals(new File(db.getWorkTree(), "crlf4"),
  113. result.getUpdatedFiles().get(0));
  114. checkFile(new File(db.getWorkTree(), "crlf4"),
  115. b.getString(0, b.size(), false));
  116. } finally {
  117. db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null,
  118. ConfigConstants.CONFIG_KEY_AUTOCRLF);
  119. }
  120. }
  121. @Test
  122. public void testPatchWithCrLf() throws Exception {
  123. try {
  124. db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  125. ConfigConstants.CONFIG_KEY_AUTOCRLF, false);
  126. ApplyResult result = init("crlf2", true, true);
  127. assertEquals(1, result.getUpdatedFiles().size());
  128. assertEquals(new File(db.getWorkTree(), "crlf2"),
  129. result.getUpdatedFiles().get(0));
  130. checkFile(new File(db.getWorkTree(), "crlf2"),
  131. b.getString(0, b.size(), false));
  132. } finally {
  133. db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null,
  134. ConfigConstants.CONFIG_KEY_AUTOCRLF);
  135. }
  136. }
  137. @Test
  138. public void testPatchWithCrLf2() throws Exception {
  139. String name = "crlf2";
  140. try (Git git = new Git(db)) {
  141. db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  142. ConfigConstants.CONFIG_KEY_AUTOCRLF, false);
  143. a = new RawText(readFile(name + "_PreImage"));
  144. write(new File(db.getWorkTree(), name),
  145. a.getString(0, a.size(), false));
  146. git.add().addFilepattern(name).call();
  147. git.commit().setMessage("PreImage").call();
  148. b = new RawText(readFile(name + "_PostImage"));
  149. db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  150. ConfigConstants.CONFIG_KEY_AUTOCRLF, true);
  151. ApplyResult result = git.apply()
  152. .setPatch(getTestResource(name + ".patch")).call();
  153. assertEquals(1, result.getUpdatedFiles().size());
  154. assertEquals(new File(db.getWorkTree(), name),
  155. result.getUpdatedFiles().get(0));
  156. checkFile(new File(db.getWorkTree(), name),
  157. b.getString(0, b.size(), false));
  158. } finally {
  159. db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null,
  160. ConfigConstants.CONFIG_KEY_AUTOCRLF);
  161. }
  162. }
  163. // Clean/smudge filter for testFiltering. The smudgetest test resources were
  164. // created with C git using a clean filter sed -e "s/A/E/g" and the smudge
  165. // filter sed -e "s/E/A/g". To keep the test independent of the presence of
  166. // sed, implement this with a built-in filter.
  167. private static class ReplaceFilter extends FilterCommand {
  168. private final char toReplace;
  169. private final char replacement;
  170. ReplaceFilter(InputStream in, OutputStream out, char toReplace,
  171. char replacement) {
  172. super(in, out);
  173. this.toReplace = toReplace;
  174. this.replacement = replacement;
  175. }
  176. @Override
  177. public int run() throws IOException {
  178. int b = in.read();
  179. if (b < 0) {
  180. in.close();
  181. out.close();
  182. return -1;
  183. }
  184. if ((b & 0xFF) == toReplace) {
  185. b = replacement;
  186. }
  187. out.write(b);
  188. return 1;
  189. }
  190. }
  191. @Test
  192. public void testFiltering() throws Exception {
  193. // Set up filter
  194. FilterCommandFactory clean = (repo, in, out) -> {
  195. return new ReplaceFilter(in, out, 'A', 'E');
  196. };
  197. FilterCommandFactory smudge = (repo, in, out) -> {
  198. return new ReplaceFilter(in, out, 'E', 'A');
  199. };
  200. FilterCommandRegistry.register("jgit://builtin/a2e/clean", clean);
  201. FilterCommandRegistry.register("jgit://builtin/a2e/smudge", smudge);
  202. try (Git git = new Git(db)) {
  203. Config config = db.getConfig();
  204. config.setString(ConfigConstants.CONFIG_FILTER_SECTION, "a2e",
  205. "clean", "jgit://builtin/a2e/clean");
  206. config.setString(ConfigConstants.CONFIG_FILTER_SECTION, "a2e",
  207. "smudge", "jgit://builtin/a2e/smudge");
  208. write(new File(db.getWorkTree(), ".gitattributes"),
  209. "smudgetest filter=a2e");
  210. git.add().addFilepattern(".gitattributes").call();
  211. git.commit().setMessage("Attributes").call();
  212. ApplyResult result = init("smudgetest", true, true);
  213. assertEquals(1, result.getUpdatedFiles().size());
  214. assertEquals(new File(db.getWorkTree(), "smudgetest"),
  215. result.getUpdatedFiles().get(0));
  216. checkFile(new File(db.getWorkTree(), "smudgetest"),
  217. b.getString(0, b.size(), false));
  218. } finally {
  219. // Tear down filter
  220. FilterCommandRegistry.unregister("jgit://builtin/a2e/clean");
  221. FilterCommandRegistry.unregister("jgit://builtin/a2e/smudge");
  222. }
  223. }
  224. private void checkBinary(String name, boolean hasPreImage)
  225. throws Exception {
  226. try (Git git = new Git(db)) {
  227. byte[] post = IO
  228. .readWholeStream(getTestResource(name + "_PostImage"), 0)
  229. .array();
  230. File f = new File(db.getWorkTree(), name);
  231. if (hasPreImage) {
  232. byte[] pre = IO
  233. .readWholeStream(getTestResource(name + "_PreImage"), 0)
  234. .array();
  235. Files.write(f.toPath(), pre);
  236. git.add().addFilepattern(name).call();
  237. git.commit().setMessage("PreImage").call();
  238. }
  239. ApplyResult result = git.apply()
  240. .setPatch(getTestResource(name + ".patch")).call();
  241. assertEquals(1, result.getUpdatedFiles().size());
  242. assertEquals(f, result.getUpdatedFiles().get(0));
  243. assertArrayEquals(post, Files.readAllBytes(f.toPath()));
  244. }
  245. }
  246. @Test
  247. public void testBinaryDelta() throws Exception {
  248. checkBinary("delta", true);
  249. }
  250. @Test
  251. public void testBinaryLiteral() throws Exception {
  252. checkBinary("literal", true);
  253. }
  254. @Test
  255. public void testBinaryLiteralAdd() throws Exception {
  256. checkBinary("literal_add", false);
  257. }
  258. @Test
  259. public void testAddA1() throws Exception {
  260. ApplyResult result = init("A1", false, true);
  261. assertEquals(1, result.getUpdatedFiles().size());
  262. assertEquals(new File(db.getWorkTree(), "A1"), result.getUpdatedFiles()
  263. .get(0));
  264. checkFile(new File(db.getWorkTree(), "A1"),
  265. b.getString(0, b.size(), false));
  266. }
  267. @Test
  268. public void testAddA2() throws Exception {
  269. ApplyResult result = init("A2", false, true);
  270. assertEquals(1, result.getUpdatedFiles().size());
  271. assertEquals(new File(db.getWorkTree(), "A2"), result.getUpdatedFiles()
  272. .get(0));
  273. checkFile(new File(db.getWorkTree(), "A2"),
  274. b.getString(0, b.size(), false));
  275. }
  276. @Test
  277. public void testAddA3() throws Exception {
  278. ApplyResult result = init("A3", false, true);
  279. assertEquals(1, result.getUpdatedFiles().size());
  280. assertEquals(new File(db.getWorkTree(), "A3"),
  281. result.getUpdatedFiles().get(0));
  282. checkFile(new File(db.getWorkTree(), "A3"),
  283. b.getString(0, b.size(), false));
  284. }
  285. @Test
  286. public void testAddA1Sub() throws Exception {
  287. ApplyResult result = init("A1_sub", false, false);
  288. assertEquals(1, result.getUpdatedFiles().size());
  289. assertEquals(new File(db.getWorkTree(), "sub/A1"), result
  290. .getUpdatedFiles().get(0));
  291. }
  292. @Test
  293. public void testDeleteD() throws Exception {
  294. ApplyResult result = init("D", true, false);
  295. assertEquals(1, result.getUpdatedFiles().size());
  296. assertEquals(new File(db.getWorkTree(), "D"), result.getUpdatedFiles()
  297. .get(0));
  298. assertFalse(new File(db.getWorkTree(), "D").exists());
  299. }
  300. @Test(expected = PatchFormatException.class)
  301. public void testFailureF1() throws Exception {
  302. init("F1", true, false);
  303. }
  304. @Test(expected = PatchApplyException.class)
  305. public void testFailureF2() throws Exception {
  306. init("F2", true, false);
  307. }
  308. @Test
  309. public void testModifyE() throws Exception {
  310. ApplyResult result = init("E");
  311. assertEquals(1, result.getUpdatedFiles().size());
  312. assertEquals(new File(db.getWorkTree(), "E"), result.getUpdatedFiles()
  313. .get(0));
  314. checkFile(new File(db.getWorkTree(), "E"),
  315. b.getString(0, b.size(), false));
  316. }
  317. @Test
  318. public void testModifyW() throws Exception {
  319. ApplyResult result = init("W");
  320. assertEquals(1, result.getUpdatedFiles().size());
  321. assertEquals(new File(db.getWorkTree(), "W"),
  322. result.getUpdatedFiles().get(0));
  323. checkFile(new File(db.getWorkTree(), "W"),
  324. b.getString(0, b.size(), false));
  325. }
  326. @Test
  327. public void testAddM1() throws Exception {
  328. ApplyResult result = init("M1", false, true);
  329. assertEquals(1, result.getUpdatedFiles().size());
  330. assertTrue(result.getUpdatedFiles().get(0).canExecute());
  331. checkFile(new File(db.getWorkTree(), "M1"),
  332. b.getString(0, b.size(), false));
  333. }
  334. @Test
  335. public void testModifyM2() throws Exception {
  336. ApplyResult result = init("M2", true, true);
  337. assertEquals(1, result.getUpdatedFiles().size());
  338. assertTrue(result.getUpdatedFiles().get(0).canExecute());
  339. checkFile(new File(db.getWorkTree(), "M2"),
  340. b.getString(0, b.size(), false));
  341. }
  342. @Test
  343. public void testModifyM3() throws Exception {
  344. ApplyResult result = init("M3", true, true);
  345. assertEquals(1, result.getUpdatedFiles().size());
  346. assertFalse(result.getUpdatedFiles().get(0).canExecute());
  347. checkFile(new File(db.getWorkTree(), "M3"),
  348. b.getString(0, b.size(), false));
  349. }
  350. @Test
  351. public void testModifyX() throws Exception {
  352. ApplyResult result = init("X");
  353. assertEquals(1, result.getUpdatedFiles().size());
  354. assertEquals(new File(db.getWorkTree(), "X"), result.getUpdatedFiles()
  355. .get(0));
  356. checkFile(new File(db.getWorkTree(), "X"),
  357. b.getString(0, b.size(), false));
  358. }
  359. @Test
  360. public void testModifyY() throws Exception {
  361. ApplyResult result = init("Y");
  362. assertEquals(1, result.getUpdatedFiles().size());
  363. assertEquals(new File(db.getWorkTree(), "Y"), result.getUpdatedFiles()
  364. .get(0));
  365. checkFile(new File(db.getWorkTree(), "Y"),
  366. b.getString(0, b.size(), false));
  367. }
  368. @Test
  369. public void testModifyZ() throws Exception {
  370. ApplyResult result = init("Z");
  371. assertEquals(1, result.getUpdatedFiles().size());
  372. assertEquals(new File(db.getWorkTree(), "Z"), result.getUpdatedFiles()
  373. .get(0));
  374. checkFile(new File(db.getWorkTree(), "Z"),
  375. b.getString(0, b.size(), false));
  376. }
  377. @Test
  378. public void testModifyNL1() throws Exception {
  379. ApplyResult result = init("NL1");
  380. assertEquals(1, result.getUpdatedFiles().size());
  381. assertEquals(new File(db.getWorkTree(), "NL1"), result
  382. .getUpdatedFiles().get(0));
  383. checkFile(new File(db.getWorkTree(), "NL1"),
  384. b.getString(0, b.size(), false));
  385. }
  386. @Test
  387. public void testNonASCII() throws Exception {
  388. ApplyResult result = init("NonASCII");
  389. assertEquals(1, result.getUpdatedFiles().size());
  390. assertEquals(new File(db.getWorkTree(), "NonASCII"),
  391. result.getUpdatedFiles().get(0));
  392. checkFile(new File(db.getWorkTree(), "NonASCII"),
  393. b.getString(0, b.size(), false));
  394. }
  395. @Test
  396. public void testNonASCII2() throws Exception {
  397. ApplyResult result = init("NonASCII2");
  398. assertEquals(1, result.getUpdatedFiles().size());
  399. assertEquals(new File(db.getWorkTree(), "NonASCII2"),
  400. result.getUpdatedFiles().get(0));
  401. checkFile(new File(db.getWorkTree(), "NonASCII2"),
  402. b.getString(0, b.size(), false));
  403. }
  404. @Test
  405. public void testNonASCIIAdd() throws Exception {
  406. ApplyResult result = init("NonASCIIAdd");
  407. assertEquals(1, result.getUpdatedFiles().size());
  408. assertEquals(new File(db.getWorkTree(), "NonASCIIAdd"),
  409. result.getUpdatedFiles().get(0));
  410. checkFile(new File(db.getWorkTree(), "NonASCIIAdd"),
  411. b.getString(0, b.size(), false));
  412. }
  413. @Test
  414. public void testNonASCIIAdd2() throws Exception {
  415. ApplyResult result = init("NonASCIIAdd2", false, true);
  416. assertEquals(1, result.getUpdatedFiles().size());
  417. assertEquals(new File(db.getWorkTree(), "NonASCIIAdd2"),
  418. result.getUpdatedFiles().get(0));
  419. checkFile(new File(db.getWorkTree(), "NonASCIIAdd2"),
  420. b.getString(0, b.size(), false));
  421. }
  422. @Test
  423. public void testNonASCIIDel() throws Exception {
  424. ApplyResult result = init("NonASCIIDel", true, false);
  425. assertEquals(1, result.getUpdatedFiles().size());
  426. assertEquals(new File(db.getWorkTree(), "NonASCIIDel"),
  427. result.getUpdatedFiles().get(0));
  428. assertFalse(new File(db.getWorkTree(), "NonASCIIDel").exists());
  429. }
  430. @Test
  431. public void testRenameNoHunks() throws Exception {
  432. ApplyResult result = init("RenameNoHunks", true, true);
  433. assertEquals(1, result.getUpdatedFiles().size());
  434. assertEquals(new File(db.getWorkTree(), "RenameNoHunks"), result.getUpdatedFiles()
  435. .get(0));
  436. checkFile(new File(db.getWorkTree(), "nested/subdir/Renamed"),
  437. b.getString(0, b.size(), false));
  438. }
  439. @Test
  440. public void testRenameWithHunks() throws Exception {
  441. ApplyResult result = init("RenameWithHunks", true, true);
  442. assertEquals(1, result.getUpdatedFiles().size());
  443. assertEquals(new File(db.getWorkTree(), "RenameWithHunks"), result.getUpdatedFiles()
  444. .get(0));
  445. checkFile(new File(db.getWorkTree(), "nested/subdir/Renamed"),
  446. b.getString(0, b.size(), false));
  447. }
  448. @Test
  449. public void testCopyWithHunks() throws Exception {
  450. ApplyResult result = init("CopyWithHunks", true, true);
  451. assertEquals(1, result.getUpdatedFiles().size());
  452. assertEquals(new File(db.getWorkTree(), "CopyWithHunks"), result.getUpdatedFiles()
  453. .get(0));
  454. checkFile(new File(db.getWorkTree(), "CopyResult"),
  455. b.getString(0, b.size(), false));
  456. }
  457. @Test
  458. public void testShiftUp() throws Exception {
  459. ApplyResult result = init("ShiftUp");
  460. assertEquals(1, result.getUpdatedFiles().size());
  461. assertEquals(new File(db.getWorkTree(), "ShiftUp"),
  462. result.getUpdatedFiles().get(0));
  463. checkFile(new File(db.getWorkTree(), "ShiftUp"),
  464. b.getString(0, b.size(), false));
  465. }
  466. @Test
  467. public void testShiftUp2() throws Exception {
  468. ApplyResult result = init("ShiftUp2");
  469. assertEquals(1, result.getUpdatedFiles().size());
  470. assertEquals(new File(db.getWorkTree(), "ShiftUp2"),
  471. result.getUpdatedFiles().get(0));
  472. checkFile(new File(db.getWorkTree(), "ShiftUp2"),
  473. b.getString(0, b.size(), false));
  474. }
  475. @Test
  476. public void testShiftDown() throws Exception {
  477. ApplyResult result = init("ShiftDown");
  478. assertEquals(1, result.getUpdatedFiles().size());
  479. assertEquals(new File(db.getWorkTree(), "ShiftDown"),
  480. result.getUpdatedFiles().get(0));
  481. checkFile(new File(db.getWorkTree(), "ShiftDown"),
  482. b.getString(0, b.size(), false));
  483. }
  484. @Test
  485. public void testShiftDown2() throws Exception {
  486. ApplyResult result = init("ShiftDown2");
  487. assertEquals(1, result.getUpdatedFiles().size());
  488. assertEquals(new File(db.getWorkTree(), "ShiftDown2"),
  489. result.getUpdatedFiles().get(0));
  490. checkFile(new File(db.getWorkTree(), "ShiftDown2"),
  491. b.getString(0, b.size(), false));
  492. }
  493. private static byte[] readFile(String patchFile) throws IOException {
  494. final InputStream in = getTestResource(patchFile);
  495. if (in == null) {
  496. fail("No " + patchFile + " test vector");
  497. return null; // Never happens
  498. }
  499. try {
  500. final byte[] buf = new byte[1024];
  501. final ByteArrayOutputStream temp = new ByteArrayOutputStream();
  502. int n;
  503. while ((n = in.read(buf)) > 0)
  504. temp.write(buf, 0, n);
  505. return temp.toByteArray();
  506. } finally {
  507. in.close();
  508. }
  509. }
  510. private static InputStream getTestResource(String patchFile) {
  511. return ApplyCommandTest.class.getClassLoader()
  512. .getResourceAsStream("org/eclipse/jgit/diff/" + patchFile);
  513. }
  514. }