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.

CheckoutTest.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*
  2. * Copyright (C) 2012, IBM Corporation
  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.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertNotNull;
  48. import static org.junit.Assert.assertTrue;
  49. import java.io.File;
  50. import java.nio.file.Files;
  51. import java.nio.file.Path;
  52. import java.util.Arrays;
  53. import java.util.List;
  54. import org.eclipse.jgit.api.Git;
  55. import org.eclipse.jgit.api.errors.CheckoutConflictException;
  56. import org.eclipse.jgit.diff.DiffEntry;
  57. import org.eclipse.jgit.lib.CLIRepositoryTestCase;
  58. import org.eclipse.jgit.lib.FileMode;
  59. import org.eclipse.jgit.lib.Ref;
  60. import org.eclipse.jgit.revwalk.RevCommit;
  61. import org.eclipse.jgit.treewalk.FileTreeIterator;
  62. import org.eclipse.jgit.treewalk.FileTreeIterator.FileEntry;
  63. import org.eclipse.jgit.treewalk.TreeWalk;
  64. import org.eclipse.jgit.util.FS;
  65. import org.eclipse.jgit.util.FileUtils;
  66. import org.junit.Assume;
  67. import org.junit.Test;
  68. public class CheckoutTest extends CLIRepositoryTestCase {
  69. @Test
  70. public void testCheckoutSelf() throws Exception {
  71. try (Git git = new Git(db)) {
  72. git.commit().setMessage("initial commit").call();
  73. assertStringArrayEquals("Already on 'master'",
  74. execute("git checkout master"));
  75. }
  76. }
  77. @Test
  78. public void testCheckoutBranch() throws Exception {
  79. try (Git git = new Git(db)) {
  80. git.commit().setMessage("initial commit").call();
  81. git.branchCreate().setName("side").call();
  82. assertStringArrayEquals("Switched to branch 'side'",
  83. execute("git checkout side"));
  84. }
  85. }
  86. @Test
  87. public void testCheckoutNewBranch() throws Exception {
  88. try (Git git = new Git(db)) {
  89. git.commit().setMessage("initial commit").call();
  90. assertStringArrayEquals("Switched to a new branch 'side'",
  91. execute("git checkout -b side"));
  92. }
  93. }
  94. @Test
  95. public void testCheckoutNonExistingBranch() throws Exception {
  96. assertStringArrayEquals(
  97. "error: pathspec 'side' did not match any file(s) known to git.",
  98. execute("git checkout side"));
  99. }
  100. @Test
  101. public void testCheckoutNewBranchThatAlreadyExists() throws Exception {
  102. try (Git git = new Git(db)) {
  103. git.commit().setMessage("initial commit").call();
  104. assertStringArrayEquals(
  105. "fatal: A branch named 'master' already exists.",
  106. executeUnchecked("git checkout -b master"));
  107. }
  108. }
  109. @Test
  110. public void testCheckoutNewBranchOnBranchToBeBorn() throws Exception {
  111. assertStringArrayEquals("fatal: You are on a branch yet to be born",
  112. executeUnchecked("git checkout -b side"));
  113. }
  114. @Test
  115. public void testCheckoutUnresolvedHead() throws Exception {
  116. assertStringArrayEquals(
  117. "error: pathspec 'HEAD' did not match any file(s) known to git.",
  118. execute("git checkout HEAD"));
  119. }
  120. @Test
  121. public void testCheckoutHead() throws Exception {
  122. try (Git git = new Git(db)) {
  123. git.commit().setMessage("initial commit").call();
  124. assertStringArrayEquals("", execute("git checkout HEAD"));
  125. }
  126. }
  127. @Test
  128. public void testCheckoutExistingBranchWithConflict() throws Exception {
  129. try (Git git = new Git(db)) {
  130. writeTrashFile("a", "Hello world a");
  131. git.add().addFilepattern(".").call();
  132. git.commit().setMessage("commit file a").call();
  133. git.branchCreate().setName("branch_1").call();
  134. git.rm().addFilepattern("a").call();
  135. FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
  136. writeTrashFile("a/b", "Hello world b");
  137. git.add().addFilepattern("a/b").call();
  138. git.commit().setMessage("commit folder a").call();
  139. git.rm().addFilepattern("a").call();
  140. writeTrashFile("a", "New Hello world a");
  141. git.add().addFilepattern(".").call();
  142. String[] execute = execute("git checkout branch_1");
  143. assertEquals(
  144. "error: Your local changes to the following files would be overwritten by checkout:",
  145. execute[0]);
  146. assertEquals("\ta", execute[1]);
  147. }
  148. }
  149. /**
  150. * Steps:
  151. * <ol>
  152. * <li>Add file 'a' and 'b'
  153. * <li>Commit
  154. * <li>Create branch '1'
  155. * <li>modify file 'a'
  156. * <li>Commit
  157. * <li>Delete file 'a' in the working tree
  158. * <li>Checkout branch '1'
  159. * </ol>
  160. * <p>
  161. * The working tree should contain 'a' with FileMode.REGULAR_FILE after the
  162. * checkout.
  163. *
  164. * @throws Exception
  165. */
  166. @Test
  167. public void testCheckoutWithMissingWorkingTreeFile() throws Exception {
  168. try (Git git = new Git(db)) {
  169. File fileA = writeTrashFile("a", "Hello world a");
  170. writeTrashFile("b", "Hello world b");
  171. git.add().addFilepattern(".").call();
  172. git.commit().setMessage("add files a & b").call();
  173. Ref branch_1 = git.branchCreate().setName("branch_1").call();
  174. writeTrashFile("a", "b");
  175. git.add().addFilepattern("a").call();
  176. git.commit().setMessage("modify file a").call();
  177. FileEntry entry = new FileTreeIterator.FileEntry(new File(
  178. db.getWorkTree(), "a"), db.getFS());
  179. assertEquals(FileMode.REGULAR_FILE, entry.getMode());
  180. FileUtils.delete(fileA);
  181. git.checkout().setName(branch_1.getName()).call();
  182. entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
  183. db.getFS());
  184. assertEquals(FileMode.REGULAR_FILE, entry.getMode());
  185. assertEquals("Hello world a", read(fileA));
  186. }
  187. }
  188. @Test
  189. public void testCheckoutOrphan() throws Exception {
  190. try (Git git = new Git(db)) {
  191. git.commit().setMessage("initial commit").call();
  192. assertStringArrayEquals("Switched to a new branch 'new_branch'",
  193. execute("git checkout --orphan new_branch"));
  194. assertEquals("refs/heads/new_branch",
  195. db.exactRef("HEAD").getTarget().getName());
  196. RevCommit commit = git.commit().setMessage("orphan commit").call();
  197. assertEquals(0, commit.getParentCount());
  198. }
  199. }
  200. /**
  201. * Steps:
  202. * <ol>
  203. * <li>Add file 'b'
  204. * <li>Commit
  205. * <li>Create branch '1'
  206. * <li>Add folder 'a'
  207. * <li>Commit
  208. * <li>Replace folder 'a' by file 'a' in the working tree
  209. * <li>Checkout branch '1'
  210. * </ol>
  211. * <p>
  212. * The working tree should contain 'a' with FileMode.REGULAR_FILE after the
  213. * checkout.
  214. *
  215. * @throws Exception
  216. */
  217. @Test
  218. public void fileModeTestMissingThenFolderWithFileInWorkingTree()
  219. throws Exception {
  220. try (Git git = new Git(db)) {
  221. writeTrashFile("b", "Hello world b");
  222. git.add().addFilepattern(".").call();
  223. git.commit().setMessage("add file b").call();
  224. Ref branch_1 = git.branchCreate().setName("branch_1").call();
  225. File folderA = new File(db.getWorkTree(), "a");
  226. FileUtils.mkdirs(folderA);
  227. writeTrashFile("a/c", "Hello world c");
  228. git.add().addFilepattern(".").call();
  229. git.commit().setMessage("add folder a").call();
  230. FileEntry entry = new FileTreeIterator.FileEntry(new File(
  231. db.getWorkTree(), "a"), db.getFS());
  232. assertEquals(FileMode.TREE, entry.getMode());
  233. FileUtils.delete(folderA, FileUtils.RECURSIVE);
  234. writeTrashFile("a", "b");
  235. entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
  236. db.getFS());
  237. assertEquals(FileMode.REGULAR_FILE, entry.getMode());
  238. git.checkout().setName(branch_1.getName()).call();
  239. entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
  240. db.getFS());
  241. assertEquals(FileMode.REGULAR_FILE, entry.getMode());
  242. }
  243. }
  244. /**
  245. * Steps:
  246. * <ol>
  247. * <li>Add file 'a'
  248. * <li>Commit
  249. * <li>Create branch '1'
  250. * <li>Replace file 'a' by folder 'a'
  251. * <li>Commit
  252. * <li>Delete folder 'a' in the working tree
  253. * <li>Checkout branch '1'
  254. * </ol>
  255. * <p>
  256. * The working tree should contain 'a' with FileMode.REGULAR_FILE after the
  257. * checkout.
  258. *
  259. * @throws Exception
  260. */
  261. @Test
  262. public void fileModeTestFolderWithMissingInWorkingTree() throws Exception {
  263. try (Git git = new Git(db)) {
  264. writeTrashFile("b", "Hello world b");
  265. writeTrashFile("a", "b");
  266. git.add().addFilepattern(".").call();
  267. git.commit().setMessage("add file b & file a").call();
  268. Ref branch_1 = git.branchCreate().setName("branch_1").call();
  269. git.rm().addFilepattern("a").call();
  270. File folderA = new File(db.getWorkTree(), "a");
  271. FileUtils.mkdirs(folderA);
  272. writeTrashFile("a/c", "Hello world c");
  273. git.add().addFilepattern(".").call();
  274. git.commit().setMessage("add folder a").call();
  275. FileEntry entry = new FileTreeIterator.FileEntry(new File(
  276. db.getWorkTree(), "a"), db.getFS());
  277. assertEquals(FileMode.TREE, entry.getMode());
  278. FileUtils.delete(folderA, FileUtils.RECURSIVE);
  279. git.checkout().setName(branch_1.getName()).call();
  280. entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
  281. db.getFS());
  282. assertEquals(FileMode.REGULAR_FILE, entry.getMode());
  283. }
  284. }
  285. /**
  286. * Steps:
  287. * <ol>
  288. * <li>Add file 'a'
  289. * <li>Commit
  290. * <li>Create branch '1'
  291. * <li>Delete file 'a'
  292. * <li>Commit
  293. * <li>Add folder 'a' in the working tree
  294. * <li>Checkout branch '1'
  295. * </ol>
  296. * <p>
  297. * The checkout command should raise an error. The conflicting paths are 'a'
  298. * and 'a/c'.
  299. *
  300. * @throws Exception
  301. */
  302. @Test
  303. public void fileModeTestMissingWithFolderInWorkingTree() throws Exception {
  304. try (Git git = new Git(db)) {
  305. writeTrashFile("b", "Hello world b");
  306. writeTrashFile("a", "b");
  307. git.add().addFilepattern(".").call();
  308. git.commit().setMessage("add file b & file a").call();
  309. Ref branch_1 = git.branchCreate().setName("branch_1").call();
  310. git.rm().addFilepattern("a").call();
  311. git.commit().setMessage("delete file a").call();
  312. FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
  313. writeTrashFile("a/c", "Hello world c");
  314. FileEntry entry = new FileTreeIterator.FileEntry(new File(
  315. db.getWorkTree(), "a"), db.getFS());
  316. assertEquals(FileMode.TREE, entry.getMode());
  317. CheckoutConflictException exception = null;
  318. try {
  319. git.checkout().setName(branch_1.getName()).call();
  320. } catch (CheckoutConflictException e) {
  321. exception = e;
  322. }
  323. assertNotNull(exception);
  324. assertEquals(2, exception.getConflictingPaths().size());
  325. assertEquals("a", exception.getConflictingPaths().get(0));
  326. assertEquals("a/c", exception.getConflictingPaths().get(1));
  327. }
  328. }
  329. /**
  330. * Steps:
  331. * <ol>
  332. * <li>Add folder 'a'
  333. * <li>Commit
  334. * <li>Create branch '1'
  335. * <li>Delete folder 'a'
  336. * <li>Commit
  337. * <li>Add file 'a' in the working tree
  338. * <li>Checkout branch '1'
  339. * </ol>
  340. * <p>
  341. * The checkout command should raise an error. The conflicting path is 'a'.
  342. *
  343. * @throws Exception
  344. */
  345. @Test
  346. public void fileModeTestFolderThenMissingWithFileInWorkingTree()
  347. throws Exception {
  348. try (Git git = new Git(db)) {
  349. FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
  350. writeTrashFile("a/c", "Hello world c");
  351. writeTrashFile("b", "Hello world b");
  352. git.add().addFilepattern(".").call();
  353. RevCommit commit1 = git.commit().setMessage("add folder a & file b")
  354. .call();
  355. Ref branch_1 = git.branchCreate().setName("branch_1").call();
  356. git.rm().addFilepattern("a").call();
  357. RevCommit commit2 = git.commit().setMessage("delete folder a").call();
  358. TreeWalk tw = new TreeWalk(db);
  359. tw.addTree(commit1.getTree());
  360. tw.addTree(commit2.getTree());
  361. List<DiffEntry> scan = DiffEntry.scan(tw);
  362. assertEquals(1, scan.size());
  363. assertEquals(FileMode.MISSING, scan.get(0).getNewMode());
  364. assertEquals(FileMode.TREE, scan.get(0).getOldMode());
  365. writeTrashFile("a", "b");
  366. FileEntry entry = new FileTreeIterator.FileEntry(new File(
  367. db.getWorkTree(), "a"), db.getFS());
  368. assertEquals(FileMode.REGULAR_FILE, entry.getMode());
  369. CheckoutConflictException exception = null;
  370. try {
  371. git.checkout().setName(branch_1.getName()).call();
  372. } catch (CheckoutConflictException e) {
  373. exception = e;
  374. }
  375. assertNotNull(exception);
  376. assertEquals(1, exception.getConflictingPaths().size());
  377. assertEquals("a", exception.getConflictingPaths().get(0));
  378. }
  379. }
  380. /**
  381. * Steps:
  382. * <ol>
  383. * <li>Add folder 'a'
  384. * <li>Commit
  385. * <li>Create branch '1'
  386. * <li>Replace folder 'a'by file 'a'
  387. * <li>Commit
  388. * <li>Delete file 'a' in the working tree
  389. * <li>Checkout branch '1'
  390. * </ol>
  391. * <p>
  392. * The working tree should contain 'a' with FileMode.TREE after the
  393. * checkout.
  394. *
  395. * @throws Exception
  396. */
  397. @Test
  398. public void fileModeTestFolderThenFileWithMissingInWorkingTree()
  399. throws Exception {
  400. try (Git git = new Git(db)) {
  401. FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
  402. writeTrashFile("a/c", "Hello world c");
  403. writeTrashFile("b", "Hello world b");
  404. git.add().addFilepattern(".").call();
  405. git.commit().setMessage("add folder a & file b").call();
  406. Ref branch_1 = git.branchCreate().setName("branch_1").call();
  407. git.rm().addFilepattern("a").call();
  408. File fileA = new File(db.getWorkTree(), "a");
  409. writeTrashFile("a", "b");
  410. git.add().addFilepattern("a").call();
  411. git.commit().setMessage("add file a").call();
  412. FileEntry entry = new FileTreeIterator.FileEntry(new File(
  413. db.getWorkTree(), "a"), db.getFS());
  414. assertEquals(FileMode.REGULAR_FILE, entry.getMode());
  415. FileUtils.delete(fileA);
  416. git.checkout().setName(branch_1.getName()).call();
  417. entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
  418. db.getFS());
  419. assertEquals(FileMode.TREE, entry.getMode());
  420. }
  421. }
  422. /**
  423. * Steps:
  424. * <ol>
  425. * <li>Add file 'a'
  426. * <li>Commit
  427. * <li>Create branch '1'
  428. * <li>Modify file 'a'
  429. * <li>Commit
  430. * <li>Delete file 'a' & replace by folder 'a' in the working tree & index
  431. * <li>Checkout branch '1'
  432. * </ol>
  433. * <p>
  434. * The checkout command should raise an error. The conflicting path is 'a'.
  435. *
  436. * @throws Exception
  437. */
  438. @Test
  439. public void fileModeTestFileThenFileWithFolderInIndex() throws Exception {
  440. try (Git git = new Git(db)) {
  441. writeTrashFile("a", "Hello world a");
  442. writeTrashFile("b", "Hello world b");
  443. git.add().addFilepattern(".").call();
  444. git.commit().setMessage("add files a & b").call();
  445. Ref branch_1 = git.branchCreate().setName("branch_1").call();
  446. writeTrashFile("a", "b");
  447. git.add().addFilepattern("a").call();
  448. git.commit().setMessage("add file a").call();
  449. FileEntry entry = new FileTreeIterator.FileEntry(new File(
  450. db.getWorkTree(), "a"), db.getFS());
  451. assertEquals(FileMode.REGULAR_FILE, entry.getMode());
  452. git.rm().addFilepattern("a").call();
  453. FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
  454. writeTrashFile("a/c", "Hello world c");
  455. git.add().addFilepattern(".").call();
  456. entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
  457. db.getFS());
  458. assertEquals(FileMode.TREE, entry.getMode());
  459. CheckoutConflictException exception = null;
  460. try {
  461. git.checkout().setName(branch_1.getName()).call();
  462. } catch (CheckoutConflictException e) {
  463. exception = e;
  464. }
  465. assertNotNull(exception);
  466. assertEquals(1, exception.getConflictingPaths().size());
  467. assertEquals("a", exception.getConflictingPaths().get(0));
  468. }
  469. }
  470. /**
  471. * Steps:
  472. * <ol>
  473. * <li>Add file 'a'
  474. * <li>Commit
  475. * <li>Create branch '1'
  476. * <li>Modify file 'a'
  477. * <li>Commit
  478. * <li>Delete file 'a' & replace by folder 'a' in the working tree & index
  479. * <li>Checkout branch '1'
  480. * </ol>
  481. * <p>
  482. * The checkout command should raise an error. The conflicting paths are 'a'
  483. * and 'a/c'.
  484. *
  485. * @throws Exception
  486. */
  487. @Test
  488. public void fileModeTestFileWithFolderInIndex() throws Exception {
  489. try (Git git = new Git(db)) {
  490. writeTrashFile("b", "Hello world b");
  491. writeTrashFile("a", "b");
  492. git.add().addFilepattern(".").call();
  493. git.commit().setMessage("add file b & file a").call();
  494. Ref branch_1 = git.branchCreate().setName("branch_1").call();
  495. git.rm().addFilepattern("a").call();
  496. writeTrashFile("a", "Hello world a");
  497. git.add().addFilepattern("a").call();
  498. git.commit().setMessage("add file a").call();
  499. FileEntry entry = new FileTreeIterator.FileEntry(new File(
  500. db.getWorkTree(), "a"), db.getFS());
  501. assertEquals(FileMode.REGULAR_FILE, entry.getMode());
  502. git.rm().addFilepattern("a").call();
  503. FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
  504. writeTrashFile("a/c", "Hello world c");
  505. git.add().addFilepattern(".").call();
  506. entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
  507. db.getFS());
  508. assertEquals(FileMode.TREE, entry.getMode());
  509. CheckoutConflictException exception = null;
  510. try {
  511. git.checkout().setName(branch_1.getName()).call();
  512. } catch (CheckoutConflictException e) {
  513. exception = e;
  514. }
  515. assertNotNull(exception);
  516. assertEquals(1, exception.getConflictingPaths().size());
  517. assertEquals("a", exception.getConflictingPaths().get(0));
  518. // TODO: ideally we'd like to get two paths from this exception
  519. // assertEquals(2, exception.getConflictingPaths().size());
  520. // assertEquals("a", exception.getConflictingPaths().get(0));
  521. // assertEquals("a/c", exception.getConflictingPaths().get(1));
  522. }
  523. }
  524. @Test
  525. public void testCheckoutPath() throws Exception {
  526. try (Git git = new Git(db)) {
  527. writeTrashFile("a", "Hello world a");
  528. git.add().addFilepattern(".").call();
  529. git.commit().setMessage("commit file a").call();
  530. git.branchCreate().setName("branch_1").call();
  531. git.checkout().setName("branch_1").call();
  532. File b = writeTrashFile("b", "Hello world b");
  533. git.add().addFilepattern("b").call();
  534. git.commit().setMessage("commit file b").call();
  535. File a = writeTrashFile("a", "New Hello world a");
  536. git.add().addFilepattern(".").call();
  537. git.commit().setMessage("modified a").call();
  538. assertArrayEquals(new String[] { "" },
  539. execute("git checkout HEAD~2 -- a"));
  540. assertEquals("Hello world a", read(a));
  541. assertArrayEquals(new String[] { "* branch_1", " master", "" },
  542. execute("git branch"));
  543. assertEquals("Hello world b", read(b));
  544. }
  545. }
  546. @Test
  547. public void testCheckouSingleFile() throws Exception {
  548. try (Git git = new Git(db)) {
  549. File a = writeTrashFile("a", "file a");
  550. git.add().addFilepattern(".").call();
  551. git.commit().setMessage("commit file a").call();
  552. writeTrashFile("a", "b");
  553. assertEquals("b", read(a));
  554. assertEquals("[]", Arrays.toString(execute("git checkout -- a")));
  555. assertEquals("file a", read(a));
  556. }
  557. }
  558. @Test
  559. public void testCheckoutLink() throws Exception {
  560. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  561. try (Git git = new Git(db)) {
  562. Path path = writeLink("a", "link_a");
  563. assertTrue(Files.isSymbolicLink(path));
  564. git.add().addFilepattern(".").call();
  565. git.commit().setMessage("commit link a").call();
  566. deleteTrashFile("a");
  567. writeTrashFile("a", "Hello world a");
  568. assertFalse(Files.isSymbolicLink(path));
  569. assertEquals("[]", Arrays.toString(execute("git checkout -- a")));
  570. assertEquals("link_a", FileUtils.readSymLink(path.toFile()));
  571. assertTrue(Files.isSymbolicLink(path));
  572. }
  573. }
  574. }