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.

MergeGitAttributeTest.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * Copyright (C) 2017, Obeo (mathieu.cartaud@obeo.fr)
  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.attributes.merge;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertNotNull;
  46. import static org.junit.Assert.assertNull;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.assertFalse;
  49. import java.io.BufferedInputStream;
  50. import java.io.File;
  51. import java.io.FileInputStream;
  52. import java.io.IOException;
  53. import java.io.InputStream;
  54. import java.nio.file.Files;
  55. import java.util.function.Consumer;
  56. import org.eclipse.jgit.api.Git;
  57. import org.eclipse.jgit.api.MergeResult;
  58. import org.eclipse.jgit.api.MergeResult.MergeStatus;
  59. import org.eclipse.jgit.api.errors.CheckoutConflictException;
  60. import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
  61. import org.eclipse.jgit.api.errors.GitAPIException;
  62. import org.eclipse.jgit.api.errors.InvalidMergeHeadsException;
  63. import org.eclipse.jgit.api.errors.NoFilepatternException;
  64. import org.eclipse.jgit.api.errors.NoHeadException;
  65. import org.eclipse.jgit.api.errors.NoMessageException;
  66. import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
  67. import org.eclipse.jgit.attributes.Attribute;
  68. import org.eclipse.jgit.attributes.Attributes;
  69. import org.eclipse.jgit.errors.NoWorkTreeException;
  70. import org.eclipse.jgit.junit.RepositoryTestCase;
  71. import org.eclipse.jgit.revwalk.RevCommit;
  72. import org.eclipse.jgit.treewalk.FileTreeIterator;
  73. import org.eclipse.jgit.treewalk.TreeWalk;
  74. import org.eclipse.jgit.treewalk.filter.PathFilter;
  75. import org.junit.Ignore;
  76. import org.junit.Test;
  77. public class MergeGitAttributeTest extends RepositoryTestCase {
  78. private static final String REFS_HEADS_RIGHT = "refs/heads/right";
  79. private static final String REFS_HEADS_MASTER = "refs/heads/master";
  80. private static final String REFS_HEADS_LEFT = "refs/heads/left";
  81. private static final String DISABLE_CHECK_BRANCH = "refs/heads/disabled_checked";
  82. private static final String ENABLE_CHECKED_BRANCH = "refs/heads/enabled_checked";
  83. private static final String ENABLED_CHECKED_GIF = "enabled_checked.gif";
  84. public Git createRepositoryBinaryConflict(Consumer<Git> initialCommit,
  85. Consumer<Git> leftCommit, Consumer<Git> rightCommit)
  86. throws NoFilepatternException, GitAPIException, NoWorkTreeException,
  87. IOException {
  88. // Set up a git whith conflict commits on images
  89. Git git = new Git(db);
  90. // First commit
  91. initialCommit.accept(git);
  92. git.add().addFilepattern(".").call();
  93. RevCommit firstCommit = git.commit().setAll(true)
  94. .setMessage("initial commit adding git attribute file").call();
  95. // Create branch and add an icon Checked_Boxe (enabled_checked)
  96. createBranch(firstCommit, REFS_HEADS_LEFT);
  97. checkoutBranch(REFS_HEADS_LEFT);
  98. leftCommit.accept(git);
  99. git.add().addFilepattern(".").call();
  100. git.commit().setMessage("Left").call();
  101. // Create a second branch from master Unchecked_Boxe
  102. checkoutBranch(REFS_HEADS_MASTER);
  103. createBranch(firstCommit, REFS_HEADS_RIGHT);
  104. checkoutBranch(REFS_HEADS_RIGHT);
  105. rightCommit.accept(git);
  106. git.add().addFilepattern(".").call();
  107. git.commit().setMessage("Right").call();
  108. checkoutBranch(REFS_HEADS_LEFT);
  109. return git;
  110. }
  111. @Test
  112. public void mergeTextualFile_NoAttr() throws NoWorkTreeException,
  113. NoFilepatternException, GitAPIException, IOException {
  114. try (Git git = createRepositoryBinaryConflict(g -> {
  115. try {
  116. writeTrashFile("main.cat", "A\n" + "B\n" + "C\n" + "D\n");
  117. } catch (IOException e) {
  118. e.printStackTrace();
  119. }
  120. }, g -> {
  121. try {
  122. writeTrashFile("main.cat", "A\n" + "B\n" + "C\n" + "F\n");
  123. } catch (IOException e) {
  124. e.printStackTrace();
  125. }
  126. }, g -> {
  127. try {
  128. writeTrashFile("main.cat", "A\n" + "E\n" + "C\n" + "D\n");
  129. } catch (IOException e) {
  130. e.printStackTrace();
  131. }
  132. })) {
  133. checkoutBranch(REFS_HEADS_LEFT);
  134. // Merge refs/heads/enabled_checked -> refs/heads/disabled_checked
  135. MergeResult mergeResult = git.merge()
  136. .include(git.getRepository().resolve(REFS_HEADS_RIGHT))
  137. .call();
  138. assertEquals(MergeStatus.MERGED, mergeResult.getMergeStatus());
  139. assertNull(mergeResult.getConflicts());
  140. // Check that the image was not modified (not conflict marker added)
  141. String result = read(
  142. writeTrashFile("res.cat", "A\n" + "E\n" + "C\n" + "F\n"));
  143. assertEquals(result, read(git.getRepository().getWorkTree().toPath()
  144. .resolve("main.cat").toFile()));
  145. }
  146. }
  147. @Test
  148. public void mergeTextualFile_UnsetMerge_Conflict()
  149. throws NoWorkTreeException, NoFilepatternException, GitAPIException,
  150. IOException {
  151. try (Git git = createRepositoryBinaryConflict(g -> {
  152. try {
  153. writeTrashFile(".gitattributes", "*.cat -merge");
  154. writeTrashFile("main.cat", "A\n" + "B\n" + "C\n" + "D\n");
  155. } catch (IOException e) {
  156. e.printStackTrace();
  157. }
  158. }, g -> {
  159. try {
  160. writeTrashFile("main.cat", "A\n" + "B\n" + "C\n" + "F\n");
  161. } catch (IOException e) {
  162. e.printStackTrace();
  163. }
  164. }, g -> {
  165. try {
  166. writeTrashFile("main.cat", "A\n" + "E\n" + "C\n" + "D\n");
  167. } catch (IOException e) {
  168. e.printStackTrace();
  169. }
  170. })) {
  171. // Check that the merge attribute is unset
  172. assertAddMergeAttributeUnset(REFS_HEADS_LEFT, "main.cat");
  173. assertAddMergeAttributeUnset(REFS_HEADS_RIGHT, "main.cat");
  174. checkoutBranch(REFS_HEADS_LEFT);
  175. // Merge refs/heads/enabled_checked -> refs/heads/disabled_checked
  176. String catContent = read(git.getRepository().getWorkTree().toPath()
  177. .resolve("main.cat").toFile());
  178. MergeResult mergeResult = git.merge()
  179. .include(git.getRepository().resolve(REFS_HEADS_RIGHT))
  180. .call();
  181. assertEquals(MergeStatus.CONFLICTING, mergeResult.getMergeStatus());
  182. // Check that the image was not modified (not conflict marker added)
  183. assertEquals(catContent, read(git.getRepository().getWorkTree()
  184. .toPath().resolve("main.cat").toFile()));
  185. }
  186. }
  187. @Test
  188. public void mergeTextualFile_UnsetMerge_NoConflict()
  189. throws NoWorkTreeException, NoFilepatternException, GitAPIException,
  190. IOException {
  191. try (Git git = createRepositoryBinaryConflict(g -> {
  192. try {
  193. writeTrashFile(".gitattributes", "*.txt -merge");
  194. writeTrashFile("main.cat", "A\n" + "B\n" + "C\n" + "D\n");
  195. } catch (IOException e) {
  196. e.printStackTrace();
  197. }
  198. }, g -> {
  199. try {
  200. writeTrashFile("main.cat", "A\n" + "B\n" + "C\n" + "F\n");
  201. } catch (IOException e) {
  202. e.printStackTrace();
  203. }
  204. }, g -> {
  205. try {
  206. writeTrashFile("main.cat", "A\n" + "E\n" + "C\n" + "D\n");
  207. } catch (IOException e) {
  208. e.printStackTrace();
  209. }
  210. })) {
  211. // Check that the merge attribute is unset
  212. assertAddMergeAttributeUndefined(REFS_HEADS_LEFT, "main.cat");
  213. assertAddMergeAttributeUndefined(REFS_HEADS_RIGHT, "main.cat");
  214. checkoutBranch(REFS_HEADS_LEFT);
  215. // Merge refs/heads/enabled_checked -> refs/heads/disabled_checked
  216. MergeResult mergeResult = git.merge()
  217. .include(git.getRepository().resolve(REFS_HEADS_RIGHT))
  218. .call();
  219. assertEquals(MergeStatus.MERGED, mergeResult.getMergeStatus());
  220. // Check that the image was not modified (not conflict marker added)
  221. String result = read(
  222. writeTrashFile("res.cat", "A\n" + "E\n" + "C\n" + "F\n"));
  223. assertEquals(result, read(git.getRepository().getWorkTree()
  224. .toPath().resolve("main.cat").toFile()));
  225. }
  226. }
  227. @Test
  228. public void mergeTextualFile_SetBinaryMerge_Conflict()
  229. throws NoWorkTreeException, NoFilepatternException, GitAPIException,
  230. IOException {
  231. try (Git git = createRepositoryBinaryConflict(g -> {
  232. try {
  233. writeTrashFile(".gitattributes", "*.cat merge=binary");
  234. writeTrashFile("main.cat", "A\n" + "B\n" + "C\n" + "D\n");
  235. } catch (IOException e) {
  236. e.printStackTrace();
  237. }
  238. }, g -> {
  239. try {
  240. writeTrashFile("main.cat", "A\n" + "B\n" + "C\n" + "F\n");
  241. } catch (IOException e) {
  242. e.printStackTrace();
  243. }
  244. }, g -> {
  245. try {
  246. writeTrashFile("main.cat", "A\n" + "E\n" + "C\n" + "D\n");
  247. } catch (IOException e) {
  248. e.printStackTrace();
  249. }
  250. })) {
  251. // Check that the merge attribute is set to binary
  252. assertAddMergeAttributeCustom(REFS_HEADS_LEFT, "main.cat",
  253. "binary");
  254. assertAddMergeAttributeCustom(REFS_HEADS_RIGHT, "main.cat",
  255. "binary");
  256. checkoutBranch(REFS_HEADS_LEFT);
  257. // Merge refs/heads/enabled_checked -> refs/heads/disabled_checked
  258. String catContent = read(git.getRepository().getWorkTree().toPath()
  259. .resolve("main.cat").toFile());
  260. MergeResult mergeResult = git.merge()
  261. .include(git.getRepository().resolve(REFS_HEADS_RIGHT))
  262. .call();
  263. assertEquals(MergeStatus.CONFLICTING, mergeResult.getMergeStatus());
  264. // Check that the image was not modified (not conflict marker added)
  265. assertEquals(catContent, read(git.getRepository().getWorkTree()
  266. .toPath().resolve("main.cat").toFile()));
  267. }
  268. }
  269. /*
  270. * This test is commented because JGit add conflict markers in binary files.
  271. * cf. https://www.eclipse.org/forums/index.php/t/1086511/
  272. */
  273. @Test
  274. @Ignore
  275. public void mergeBinaryFile_NoAttr_Conflict() throws IllegalStateException,
  276. IOException, NoHeadException, ConcurrentRefUpdateException,
  277. CheckoutConflictException, InvalidMergeHeadsException,
  278. WrongRepositoryStateException, NoMessageException, GitAPIException {
  279. RevCommit disableCheckedCommit;
  280. FileInputStream mergeResultFile = null;
  281. // Set up a git with conflict commits on images
  282. try (Git git = new Git(db)) {
  283. // First commit
  284. write(new File(db.getWorkTree(), ".gitattributes"), "");
  285. git.add().addFilepattern(".gitattributes").call();
  286. RevCommit firstCommit = git.commit()
  287. .setMessage("initial commit adding git attribute file")
  288. .call();
  289. // Create branch and add an icon Checked_Boxe (enabled_checked)
  290. createBranch(firstCommit, ENABLE_CHECKED_BRANCH);
  291. checkoutBranch(ENABLE_CHECKED_BRANCH);
  292. copy(ENABLED_CHECKED_GIF, ENABLED_CHECKED_GIF, "");
  293. git.add().addFilepattern(ENABLED_CHECKED_GIF).call();
  294. git.commit().setMessage("enabled_checked commit").call();
  295. // Create a second branch from master Unchecked_Boxe
  296. checkoutBranch(REFS_HEADS_MASTER);
  297. createBranch(firstCommit, DISABLE_CHECK_BRANCH);
  298. checkoutBranch(DISABLE_CHECK_BRANCH);
  299. copy("disabled_checked.gif", ENABLED_CHECKED_GIF, "");
  300. git.add().addFilepattern(ENABLED_CHECKED_GIF).call();
  301. disableCheckedCommit = git.commit()
  302. .setMessage("disabled_checked commit").call();
  303. // Check that the merge attribute is unset
  304. assertAddMergeAttributeUndefined(ENABLE_CHECKED_BRANCH,
  305. ENABLED_CHECKED_GIF);
  306. assertAddMergeAttributeUndefined(DISABLE_CHECK_BRANCH,
  307. ENABLED_CHECKED_GIF);
  308. checkoutBranch(ENABLE_CHECKED_BRANCH);
  309. // Merge refs/heads/enabled_checked -> refs/heads/disabled_checked
  310. MergeResult mergeResult = git.merge().include(disableCheckedCommit)
  311. .call();
  312. assertEquals(MergeStatus.CONFLICTING, mergeResult.getMergeStatus());
  313. // Check that the image was not modified (no conflict marker added)
  314. mergeResultFile = new FileInputStream(
  315. db.getWorkTree().toPath().resolve(ENABLED_CHECKED_GIF)
  316. .toFile());
  317. assertTrue(contentEquals(
  318. getClass().getResourceAsStream(ENABLED_CHECKED_GIF),
  319. mergeResultFile));
  320. } finally {
  321. if (mergeResultFile != null) {
  322. mergeResultFile.close();
  323. }
  324. }
  325. }
  326. @Test
  327. public void mergeBinaryFile_UnsetMerge_Conflict()
  328. throws IllegalStateException,
  329. IOException, NoHeadException, ConcurrentRefUpdateException,
  330. CheckoutConflictException, InvalidMergeHeadsException,
  331. WrongRepositoryStateException, NoMessageException, GitAPIException {
  332. RevCommit disableCheckedCommit;
  333. FileInputStream mergeResultFile = null;
  334. // Set up a git whith conflict commits on images
  335. try (Git git = new Git(db)) {
  336. // First commit
  337. write(new File(db.getWorkTree(), ".gitattributes"), "*.gif -merge");
  338. git.add().addFilepattern(".gitattributes").call();
  339. RevCommit firstCommit = git.commit()
  340. .setMessage("initial commit adding git attribute file")
  341. .call();
  342. // Create branch and add an icon Checked_Boxe (enabled_checked)
  343. createBranch(firstCommit, ENABLE_CHECKED_BRANCH);
  344. checkoutBranch(ENABLE_CHECKED_BRANCH);
  345. copy(ENABLED_CHECKED_GIF, ENABLED_CHECKED_GIF, "");
  346. git.add().addFilepattern(ENABLED_CHECKED_GIF).call();
  347. git.commit().setMessage("enabled_checked commit").call();
  348. // Create a second branch from master Unchecked_Boxe
  349. checkoutBranch(REFS_HEADS_MASTER);
  350. createBranch(firstCommit, DISABLE_CHECK_BRANCH);
  351. checkoutBranch(DISABLE_CHECK_BRANCH);
  352. copy("disabled_checked.gif", ENABLED_CHECKED_GIF, "");
  353. git.add().addFilepattern(ENABLED_CHECKED_GIF).call();
  354. disableCheckedCommit = git.commit()
  355. .setMessage("disabled_checked commit").call();
  356. // Check that the merge attribute is unset
  357. assertAddMergeAttributeUnset(ENABLE_CHECKED_BRANCH,
  358. ENABLED_CHECKED_GIF);
  359. assertAddMergeAttributeUnset(DISABLE_CHECK_BRANCH,
  360. ENABLED_CHECKED_GIF);
  361. checkoutBranch(ENABLE_CHECKED_BRANCH);
  362. // Merge refs/heads/enabled_checked -> refs/heads/disabled_checked
  363. MergeResult mergeResult = git.merge().include(disableCheckedCommit)
  364. .call();
  365. assertEquals(MergeStatus.CONFLICTING, mergeResult.getMergeStatus());
  366. // Check that the image was not modified (not conflict marker added)
  367. mergeResultFile = new FileInputStream(db.getWorkTree().toPath()
  368. .resolve(ENABLED_CHECKED_GIF).toFile());
  369. assertTrue(contentEquals(
  370. getClass().getResourceAsStream(ENABLED_CHECKED_GIF),
  371. mergeResultFile));
  372. } finally {
  373. if (mergeResultFile != null) {
  374. mergeResultFile.close();
  375. }
  376. }
  377. }
  378. @Test
  379. public void mergeBinaryFile_SetMerge_Conflict()
  380. throws IllegalStateException, IOException, NoHeadException,
  381. ConcurrentRefUpdateException, CheckoutConflictException,
  382. InvalidMergeHeadsException, WrongRepositoryStateException,
  383. NoMessageException, GitAPIException {
  384. RevCommit disableCheckedCommit;
  385. FileInputStream mergeResultFile = null;
  386. // Set up a git whith conflict commits on images
  387. try (Git git = new Git(db)) {
  388. // First commit
  389. write(new File(db.getWorkTree(), ".gitattributes"), "*.gif merge");
  390. git.add().addFilepattern(".gitattributes").call();
  391. RevCommit firstCommit = git.commit()
  392. .setMessage("initial commit adding git attribute file")
  393. .call();
  394. // Create branch and add an icon Checked_Boxe (enabled_checked)
  395. createBranch(firstCommit, ENABLE_CHECKED_BRANCH);
  396. checkoutBranch(ENABLE_CHECKED_BRANCH);
  397. copy(ENABLED_CHECKED_GIF, ENABLED_CHECKED_GIF, "");
  398. git.add().addFilepattern(ENABLED_CHECKED_GIF).call();
  399. git.commit().setMessage("enabled_checked commit").call();
  400. // Create a second branch from master Unchecked_Boxe
  401. checkoutBranch(REFS_HEADS_MASTER);
  402. createBranch(firstCommit, DISABLE_CHECK_BRANCH);
  403. checkoutBranch(DISABLE_CHECK_BRANCH);
  404. copy("disabled_checked.gif", ENABLED_CHECKED_GIF, "");
  405. git.add().addFilepattern(ENABLED_CHECKED_GIF).call();
  406. disableCheckedCommit = git.commit()
  407. .setMessage("disabled_checked commit").call();
  408. // Check that the merge attribute is set
  409. assertAddMergeAttributeSet(ENABLE_CHECKED_BRANCH,
  410. ENABLED_CHECKED_GIF);
  411. assertAddMergeAttributeSet(DISABLE_CHECK_BRANCH,
  412. ENABLED_CHECKED_GIF);
  413. checkoutBranch(ENABLE_CHECKED_BRANCH);
  414. // Merge refs/heads/enabled_checked -> refs/heads/disabled_checked
  415. MergeResult mergeResult = git.merge().include(disableCheckedCommit)
  416. .call();
  417. assertEquals(MergeStatus.CONFLICTING, mergeResult.getMergeStatus());
  418. // Check that the image was not modified (not conflict marker added)
  419. mergeResultFile = new FileInputStream(db.getWorkTree().toPath()
  420. .resolve(ENABLED_CHECKED_GIF).toFile());
  421. assertFalse(contentEquals(
  422. getClass().getResourceAsStream(ENABLED_CHECKED_GIF),
  423. mergeResultFile));
  424. } finally {
  425. if (mergeResultFile != null) {
  426. mergeResultFile.close();
  427. }
  428. }
  429. }
  430. /*
  431. * Copied from org.apache.commons.io.IOUtils
  432. */
  433. private boolean contentEquals(InputStream input1, InputStream input2)
  434. throws IOException {
  435. if (input1 == input2) {
  436. return true;
  437. }
  438. if (!(input1 instanceof BufferedInputStream)) {
  439. input1 = new BufferedInputStream(input1);
  440. }
  441. if (!(input2 instanceof BufferedInputStream)) {
  442. input2 = new BufferedInputStream(input2);
  443. }
  444. int ch = input1.read();
  445. while (-1 != ch) {
  446. final int ch2 = input2.read();
  447. if (ch != ch2) {
  448. return false;
  449. }
  450. ch = input1.read();
  451. }
  452. final int ch2 = input2.read();
  453. return ch2 == -1;
  454. }
  455. private void assertAddMergeAttributeUnset(String branch, String fileName)
  456. throws IllegalStateException, IOException {
  457. checkoutBranch(branch);
  458. try (TreeWalk treeWaklEnableChecked = new TreeWalk(db)) {
  459. treeWaklEnableChecked.addTree(new FileTreeIterator(db));
  460. treeWaklEnableChecked.setFilter(PathFilter.create(fileName));
  461. assertTrue(treeWaklEnableChecked.next());
  462. Attributes attributes = treeWaklEnableChecked.getAttributes();
  463. Attribute mergeAttribute = attributes.get("merge");
  464. assertNotNull(mergeAttribute);
  465. assertEquals(Attribute.State.UNSET, mergeAttribute.getState());
  466. }
  467. }
  468. private void assertAddMergeAttributeSet(String branch, String fileName)
  469. throws IllegalStateException, IOException {
  470. checkoutBranch(branch);
  471. try (TreeWalk treeWaklEnableChecked = new TreeWalk(db)) {
  472. treeWaklEnableChecked.addTree(new FileTreeIterator(db));
  473. treeWaklEnableChecked.setFilter(PathFilter.create(fileName));
  474. assertTrue(treeWaklEnableChecked.next());
  475. Attributes attributes = treeWaklEnableChecked.getAttributes();
  476. Attribute mergeAttribute = attributes.get("merge");
  477. assertNotNull(mergeAttribute);
  478. assertEquals(Attribute.State.SET, mergeAttribute.getState());
  479. }
  480. }
  481. private void assertAddMergeAttributeUndefined(String branch,
  482. String fileName) throws IllegalStateException, IOException {
  483. checkoutBranch(branch);
  484. try (TreeWalk treeWaklEnableChecked = new TreeWalk(db)) {
  485. treeWaklEnableChecked.addTree(new FileTreeIterator(db));
  486. treeWaklEnableChecked.setFilter(PathFilter.create(fileName));
  487. assertTrue(treeWaklEnableChecked.next());
  488. Attributes attributes = treeWaklEnableChecked.getAttributes();
  489. Attribute mergeAttribute = attributes.get("merge");
  490. assertNull(mergeAttribute);
  491. }
  492. }
  493. private void assertAddMergeAttributeCustom(String branch, String fileName,
  494. String value) throws IllegalStateException, IOException {
  495. checkoutBranch(branch);
  496. try (TreeWalk treeWaklEnableChecked = new TreeWalk(db)) {
  497. treeWaklEnableChecked.addTree(new FileTreeIterator(db));
  498. treeWaklEnableChecked.setFilter(PathFilter.create(fileName));
  499. assertTrue(treeWaklEnableChecked.next());
  500. Attributes attributes = treeWaklEnableChecked.getAttributes();
  501. Attribute mergeAttribute = attributes.get("merge");
  502. assertNotNull(mergeAttribute);
  503. assertEquals(Attribute.State.CUSTOM, mergeAttribute.getState());
  504. assertEquals(value, mergeAttribute.getValue());
  505. }
  506. }
  507. private void copy(String resourcePath, String resourceNewName,
  508. String pathInRepo) throws IOException {
  509. InputStream input = getClass().getResourceAsStream(resourcePath);
  510. Files.copy(input, db.getWorkTree().toPath().resolve(pathInRepo)
  511. .resolve(resourceNewName));
  512. }
  513. }