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 19KB

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