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.

StashApplyCommandTest.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /*
  2. * Copyright (C) 2012, GitHub Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.api;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.fail;
  49. import java.io.File;
  50. import java.text.MessageFormat;
  51. import org.eclipse.jgit.api.errors.InvalidRefNameException;
  52. import org.eclipse.jgit.api.errors.JGitInternalException;
  53. import org.eclipse.jgit.api.errors.NoHeadException;
  54. import org.eclipse.jgit.api.errors.StashApplyFailureException;
  55. import org.eclipse.jgit.internal.JGitText;
  56. import org.eclipse.jgit.junit.RepositoryTestCase;
  57. import org.eclipse.jgit.lib.ObjectId;
  58. import org.eclipse.jgit.lib.Repository;
  59. import org.eclipse.jgit.revwalk.RevCommit;
  60. import org.eclipse.jgit.util.FileUtils;
  61. import org.junit.Before;
  62. import org.junit.Test;
  63. /**
  64. * Unit tests of {@link StashApplyCommand}
  65. */
  66. public class StashApplyCommandTest extends RepositoryTestCase {
  67. private static final String PATH = "file.txt";
  68. private RevCommit head;
  69. private Git git;
  70. private File committedFile;
  71. @Override
  72. @Before
  73. public void setUp() throws Exception {
  74. super.setUp();
  75. git = Git.wrap(db);
  76. committedFile = writeTrashFile(PATH, "content");
  77. git.add().addFilepattern(PATH).call();
  78. head = git.commit().setMessage("add file").call();
  79. assertNotNull(head);
  80. }
  81. @Test
  82. public void workingDirectoryDelete() throws Exception {
  83. deleteTrashFile(PATH);
  84. assertFalse(committedFile.exists());
  85. RevCommit stashed = git.stashCreate().call();
  86. assertNotNull(stashed);
  87. assertEquals("content", read(committedFile));
  88. ObjectId unstashed = git.stashApply().call();
  89. assertEquals(stashed, unstashed);
  90. assertFalse(committedFile.exists());
  91. Status status = git.status().call();
  92. assertTrue(status.getAdded().isEmpty());
  93. assertTrue(status.getChanged().isEmpty());
  94. assertTrue(status.getConflicting().isEmpty());
  95. assertTrue(status.getModified().isEmpty());
  96. assertTrue(status.getUntracked().isEmpty());
  97. assertTrue(status.getRemoved().isEmpty());
  98. assertEquals(1, status.getMissing().size());
  99. assertTrue(status.getMissing().contains(PATH));
  100. }
  101. @Test
  102. public void indexAdd() throws Exception {
  103. String addedPath = "file2.txt";
  104. File addedFile = writeTrashFile(addedPath, "content2");
  105. git.add().addFilepattern(addedPath).call();
  106. RevCommit stashed = git.stashCreate().call();
  107. assertNotNull(stashed);
  108. assertFalse(addedFile.exists());
  109. ObjectId unstashed = git.stashApply().call();
  110. assertEquals(stashed, unstashed);
  111. assertTrue(addedFile.exists());
  112. assertEquals("content2", read(addedFile));
  113. Status status = git.status().call();
  114. assertTrue(status.getChanged().isEmpty());
  115. assertTrue(status.getConflicting().isEmpty());
  116. assertTrue(status.getMissing().isEmpty());
  117. assertTrue(status.getModified().isEmpty());
  118. assertTrue(status.getRemoved().isEmpty());
  119. assertTrue(status.getUntracked().isEmpty());
  120. assertEquals(1, status.getAdded().size());
  121. assertTrue(status.getAdded().contains(addedPath));
  122. }
  123. @Test
  124. public void indexDelete() throws Exception {
  125. git.rm().addFilepattern("file.txt").call();
  126. RevCommit stashed = git.stashCreate().call();
  127. assertNotNull(stashed);
  128. assertEquals("content", read(committedFile));
  129. ObjectId unstashed = git.stashApply().call();
  130. assertEquals(stashed, unstashed);
  131. assertFalse(committedFile.exists());
  132. Status status = git.status().call();
  133. assertTrue(status.getAdded().isEmpty());
  134. assertTrue(status.getChanged().isEmpty());
  135. assertTrue(status.getConflicting().isEmpty());
  136. assertTrue(status.getModified().isEmpty());
  137. assertTrue(status.getMissing().isEmpty());
  138. assertTrue(status.getUntracked().isEmpty());
  139. assertEquals(1, status.getRemoved().size());
  140. assertTrue(status.getRemoved().contains(PATH));
  141. }
  142. @Test
  143. public void workingDirectoryModify() throws Exception {
  144. writeTrashFile("file.txt", "content2");
  145. RevCommit stashed = git.stashCreate().call();
  146. assertNotNull(stashed);
  147. assertEquals("content", read(committedFile));
  148. ObjectId unstashed = git.stashApply().call();
  149. assertEquals(stashed, unstashed);
  150. assertEquals("content2", read(committedFile));
  151. Status status = git.status().call();
  152. assertTrue(status.getAdded().isEmpty());
  153. assertTrue(status.getChanged().isEmpty());
  154. assertTrue(status.getConflicting().isEmpty());
  155. assertTrue(status.getMissing().isEmpty());
  156. assertTrue(status.getRemoved().isEmpty());
  157. assertTrue(status.getUntracked().isEmpty());
  158. assertEquals(1, status.getModified().size());
  159. assertTrue(status.getModified().contains(PATH));
  160. }
  161. @Test
  162. public void workingDirectoryModifyInSubfolder() throws Exception {
  163. String path = "d1/d2/f.txt";
  164. File subfolderFile = writeTrashFile(path, "content");
  165. git.add().addFilepattern(path).call();
  166. head = git.commit().setMessage("add file").call();
  167. writeTrashFile(path, "content2");
  168. RevCommit stashed = git.stashCreate().call();
  169. assertNotNull(stashed);
  170. assertEquals("content", read(subfolderFile));
  171. ObjectId unstashed = git.stashApply().call();
  172. assertEquals(stashed, unstashed);
  173. assertEquals("content2", read(subfolderFile));
  174. Status status = git.status().call();
  175. assertTrue(status.getAdded().isEmpty());
  176. assertTrue(status.getChanged().isEmpty());
  177. assertTrue(status.getConflicting().isEmpty());
  178. assertTrue(status.getMissing().isEmpty());
  179. assertTrue(status.getRemoved().isEmpty());
  180. assertTrue(status.getUntracked().isEmpty());
  181. assertEquals(1, status.getModified().size());
  182. assertTrue(status.getModified().contains(path));
  183. }
  184. @Test
  185. public void workingDirectoryModifyIndexChanged() throws Exception {
  186. writeTrashFile("file.txt", "content2");
  187. git.add().addFilepattern("file.txt").call();
  188. writeTrashFile("file.txt", "content3");
  189. RevCommit stashed = git.stashCreate().call();
  190. assertNotNull(stashed);
  191. assertEquals("content", read(committedFile));
  192. ObjectId unstashed = git.stashApply().call();
  193. assertEquals(stashed, unstashed);
  194. assertEquals("content3", read(committedFile));
  195. Status status = git.status().call();
  196. assertTrue(status.getAdded().isEmpty());
  197. assertTrue(status.getConflicting().isEmpty());
  198. assertTrue(status.getMissing().isEmpty());
  199. assertTrue(status.getRemoved().isEmpty());
  200. assertTrue(status.getUntracked().isEmpty());
  201. assertEquals(1, status.getChanged().size());
  202. assertTrue(status.getChanged().contains(PATH));
  203. assertEquals(1, status.getModified().size());
  204. assertTrue(status.getModified().contains(PATH));
  205. }
  206. @Test
  207. public void workingDirectoryCleanIndexModify() throws Exception {
  208. writeTrashFile("file.txt", "content2");
  209. git.add().addFilepattern("file.txt").call();
  210. writeTrashFile("file.txt", "content");
  211. RevCommit stashed = git.stashCreate().call();
  212. assertNotNull(stashed);
  213. assertEquals("content", read(committedFile));
  214. ObjectId unstashed = git.stashApply().call();
  215. assertEquals(stashed, unstashed);
  216. assertEquals("content2", read(committedFile));
  217. Status status = git.status().call();
  218. assertTrue(status.getAdded().isEmpty());
  219. assertTrue(status.getConflicting().isEmpty());
  220. assertTrue(status.getMissing().isEmpty());
  221. assertTrue(status.getModified().isEmpty());
  222. assertTrue(status.getRemoved().isEmpty());
  223. assertTrue(status.getUntracked().isEmpty());
  224. assertEquals(1, status.getChanged().size());
  225. assertTrue(status.getChanged().contains(PATH));
  226. }
  227. @Test
  228. public void workingDirectoryDeleteIndexAdd() throws Exception {
  229. String path = "file2.txt";
  230. File added = writeTrashFile(path, "content2");
  231. assertTrue(added.exists());
  232. git.add().addFilepattern(path).call();
  233. FileUtils.delete(added);
  234. assertFalse(added.exists());
  235. RevCommit stashed = git.stashCreate().call();
  236. assertNotNull(stashed);
  237. assertFalse(added.exists());
  238. ObjectId unstashed = git.stashApply().call();
  239. assertEquals(stashed, unstashed);
  240. assertEquals("content2", read(added));
  241. Status status = git.status().call();
  242. assertTrue(status.getChanged().isEmpty());
  243. assertTrue(status.getConflicting().isEmpty());
  244. assertTrue(status.getMissing().isEmpty());
  245. assertTrue(status.getModified().isEmpty());
  246. assertTrue(status.getRemoved().isEmpty());
  247. assertTrue(status.getUntracked().isEmpty());
  248. assertEquals(1, status.getAdded().size());
  249. assertTrue(status.getAdded().contains(path));
  250. }
  251. @Test
  252. public void workingDirectoryDeleteIndexEdit() throws Exception {
  253. writeTrashFile(PATH, "content2");
  254. git.add().addFilepattern(PATH).call();
  255. FileUtils.delete(committedFile);
  256. assertFalse(committedFile.exists());
  257. RevCommit stashed = git.stashCreate().call();
  258. assertNotNull(stashed);
  259. assertEquals("content", read(committedFile));
  260. ObjectId unstashed = git.stashApply().call();
  261. assertEquals(stashed, unstashed);
  262. assertFalse(committedFile.exists());
  263. Status status = git.status().call();
  264. assertTrue(status.getAdded().isEmpty());
  265. assertEquals(1, status.getChanged().size());
  266. assertTrue(status.getChanged().contains(PATH));
  267. assertTrue(status.getConflicting().isEmpty());
  268. assertEquals(1, status.getMissing().size());
  269. assertTrue(status.getMissing().contains(PATH));
  270. assertTrue(status.getModified().isEmpty());
  271. assertTrue(status.getUntracked().isEmpty());
  272. assertTrue(status.getRemoved().isEmpty());
  273. }
  274. @Test
  275. public void multipleEdits() throws Exception {
  276. String addedPath = "file2.txt";
  277. git.rm().addFilepattern(PATH).call();
  278. File addedFile = writeTrashFile(addedPath, "content2");
  279. git.add().addFilepattern(addedPath).call();
  280. RevCommit stashed = git.stashCreate().call();
  281. assertNotNull(stashed);
  282. assertTrue(committedFile.exists());
  283. assertFalse(addedFile.exists());
  284. ObjectId unstashed = git.stashApply().call();
  285. assertEquals(stashed, unstashed);
  286. Status status = git.status().call();
  287. assertTrue(status.getChanged().isEmpty());
  288. assertTrue(status.getConflicting().isEmpty());
  289. assertTrue(status.getMissing().isEmpty());
  290. assertTrue(status.getModified().isEmpty());
  291. assertTrue(status.getUntracked().isEmpty());
  292. assertEquals(1, status.getRemoved().size());
  293. assertTrue(status.getRemoved().contains(PATH));
  294. assertEquals(1, status.getAdded().size());
  295. assertTrue(status.getAdded().contains(addedPath));
  296. }
  297. @Test
  298. public void workingDirectoryContentConflict() throws Exception {
  299. writeTrashFile(PATH, "content2");
  300. RevCommit stashed = git.stashCreate().call();
  301. assertNotNull(stashed);
  302. assertEquals("content", read(committedFile));
  303. assertTrue(git.status().call().isClean());
  304. writeTrashFile(PATH, "content3");
  305. try {
  306. git.stashApply().call();
  307. fail("Exception not thrown");
  308. } catch (StashApplyFailureException e) {
  309. // expected
  310. }
  311. assertEquals("content3", read(PATH));
  312. }
  313. @Test
  314. public void stashedContentMerge() throws Exception {
  315. writeTrashFile(PATH, "content\nmore content\n");
  316. git.add().addFilepattern(PATH).call();
  317. git.commit().setMessage("more content").call();
  318. writeTrashFile(PATH, "content\nhead change\nmore content\n");
  319. git.add().addFilepattern(PATH).call();
  320. git.commit().setMessage("even content").call();
  321. writeTrashFile(PATH, "content\nstashed change\nmore content\n");
  322. RevCommit stashed = git.stashCreate().call();
  323. assertNotNull(stashed);
  324. assertEquals("content\nhead change\nmore content\n",
  325. read(committedFile));
  326. assertTrue(git.status().call().isClean());
  327. writeTrashFile(PATH, "content\nmore content\ncommitted change\n");
  328. git.add().addFilepattern(PATH).call();
  329. git.commit().setMessage("committed change").call();
  330. try {
  331. git.stashApply().call();
  332. fail("Expected conflict");
  333. } catch (StashApplyFailureException e) {
  334. // expected
  335. }
  336. Status status = new StatusCommand(db).call();
  337. assertEquals(1, status.getConflicting().size());
  338. assertEquals(
  339. "content\n<<<<<<< HEAD\n=======\nstashed change\n>>>>>>> stash\nmore content\ncommitted change\n",
  340. read(PATH));
  341. }
  342. @Test
  343. public void stashedApplyOnOtherBranch() throws Exception {
  344. writeTrashFile(PATH, "content\nmore content\n");
  345. git.add().addFilepattern(PATH).call();
  346. git.commit().setMessage("more content").call();
  347. String path2 = "file2.txt";
  348. File file2 = writeTrashFile(path2, "content\nmore content\n");
  349. git.add().addFilepattern(PATH).call();
  350. git.add().addFilepattern(path2).call();
  351. git.commit().setMessage("even content").call();
  352. String otherBranch = "otherBranch";
  353. git.branchCreate().setName(otherBranch).call();
  354. writeTrashFile(PATH, "master content");
  355. git.add().addFilepattern(PATH).call();
  356. git.commit().setMessage("even content").call();
  357. git.checkout().setName(otherBranch).call();
  358. writeTrashFile(PATH, "otherBranch content");
  359. git.add().addFilepattern(PATH).call();
  360. git.commit().setMessage("even more content").call();
  361. writeTrashFile(path2, "content\nstashed change\nmore content\n");
  362. RevCommit stashed = git.stashCreate().call();
  363. assertNotNull(stashed);
  364. assertEquals("content\nmore content\n", read(file2));
  365. assertEquals("otherBranch content",
  366. read(committedFile));
  367. assertTrue(git.status().call().isClean());
  368. git.checkout().setName("master").call();
  369. git.stashApply().call();
  370. assertEquals("content\nstashed change\nmore content\n", read(file2));
  371. assertEquals("master content",
  372. read(committedFile));
  373. }
  374. @Test
  375. public void stashedApplyOnOtherBranchWithStagedChange() throws Exception {
  376. writeTrashFile(PATH, "content\nmore content\n");
  377. git.add().addFilepattern(PATH).call();
  378. git.commit().setMessage("more content").call();
  379. String path2 = "file2.txt";
  380. File file2 = writeTrashFile(path2, "content\nmore content\n");
  381. git.add().addFilepattern(PATH).call();
  382. git.add().addFilepattern(path2).call();
  383. git.commit().setMessage("even content").call();
  384. String otherBranch = "otherBranch";
  385. git.branchCreate().setName(otherBranch).call();
  386. writeTrashFile(PATH, "master content");
  387. git.add().addFilepattern(PATH).call();
  388. git.commit().setMessage("even content").call();
  389. git.checkout().setName(otherBranch).call();
  390. writeTrashFile(PATH, "otherBranch content");
  391. git.add().addFilepattern(PATH).call();
  392. git.commit().setMessage("even more content").call();
  393. writeTrashFile(path2,
  394. "content\nstashed change in index\nmore content\n");
  395. git.add().addFilepattern(path2).call();
  396. writeTrashFile(path2, "content\nstashed change\nmore content\n");
  397. RevCommit stashed = git.stashCreate().call();
  398. assertNotNull(stashed);
  399. assertEquals("content\nmore content\n", read(file2));
  400. assertEquals("otherBranch content", read(committedFile));
  401. assertTrue(git.status().call().isClean());
  402. git.checkout().setName("master").call();
  403. git.stashApply().call();
  404. assertEquals("content\nstashed change\nmore content\n", read(file2));
  405. assertEquals(
  406. "[file.txt, mode:100644, content:master content]"
  407. + "[file2.txt, mode:100644, content:content\nstashed change in index\nmore content\n]",
  408. indexState(CONTENT));
  409. assertEquals("master content", read(committedFile));
  410. }
  411. @Test
  412. public void workingDirectoryContentMerge() throws Exception {
  413. writeTrashFile(PATH, "content\nmore content\n");
  414. git.add().addFilepattern(PATH).call();
  415. git.commit().setMessage("more content").call();
  416. writeTrashFile(PATH, "content\nstashed change\nmore content\n");
  417. RevCommit stashed = git.stashCreate().call();
  418. assertNotNull(stashed);
  419. assertEquals("content\nmore content\n", read(committedFile));
  420. assertTrue(git.status().call().isClean());
  421. writeTrashFile(PATH, "content\nmore content\ncommitted change\n");
  422. git.add().addFilepattern(PATH).call();
  423. git.commit().setMessage("committed change").call();
  424. git.stashApply().call();
  425. assertEquals(
  426. "content\nstashed change\nmore content\ncommitted change\n",
  427. read(committedFile));
  428. }
  429. @Test
  430. public void indexContentConflict() throws Exception {
  431. writeTrashFile(PATH, "content2");
  432. RevCommit stashed = git.stashCreate().call();
  433. assertNotNull(stashed);
  434. assertEquals("content", read(committedFile));
  435. assertTrue(git.status().call().isClean());
  436. writeTrashFile(PATH, "content3");
  437. git.add().addFilepattern(PATH).call();
  438. writeTrashFile(PATH, "content2");
  439. try {
  440. git.stashApply().call();
  441. fail("Exception not thrown");
  442. } catch (StashApplyFailureException e) {
  443. // expected
  444. }
  445. assertEquals("content2", read(PATH));
  446. }
  447. @Test
  448. public void workingDirectoryEditPreCommit() throws Exception {
  449. writeTrashFile(PATH, "content2");
  450. RevCommit stashed = git.stashCreate().call();
  451. assertNotNull(stashed);
  452. assertEquals("content", read(committedFile));
  453. assertTrue(git.status().call().isClean());
  454. String path2 = "file2.txt";
  455. writeTrashFile(path2, "content3");
  456. git.add().addFilepattern(path2).call();
  457. assertNotNull(git.commit().setMessage("adding file").call());
  458. ObjectId unstashed = git.stashApply().call();
  459. assertEquals(stashed, unstashed);
  460. Status status = git.status().call();
  461. assertTrue(status.getAdded().isEmpty());
  462. assertTrue(status.getChanged().isEmpty());
  463. assertTrue(status.getConflicting().isEmpty());
  464. assertTrue(status.getMissing().isEmpty());
  465. assertTrue(status.getRemoved().isEmpty());
  466. assertTrue(status.getUntracked().isEmpty());
  467. assertEquals(1, status.getModified().size());
  468. assertTrue(status.getModified().contains(PATH));
  469. }
  470. @Test
  471. public void stashChangeInANewSubdirectory() throws Exception {
  472. String subdir = "subdir";
  473. String fname = "file2.txt";
  474. String path = subdir + "/" + fname;
  475. String otherBranch = "otherbranch";
  476. writeTrashFile(subdir, fname, "content2");
  477. git.add().addFilepattern(path).call();
  478. RevCommit stashed = git.stashCreate().call();
  479. assertNotNull(stashed);
  480. assertTrue(git.status().call().isClean());
  481. git.branchCreate().setName(otherBranch).call();
  482. git.checkout().setName(otherBranch).call();
  483. ObjectId unstashed = git.stashApply().call();
  484. assertEquals(stashed, unstashed);
  485. Status status = git.status().call();
  486. assertTrue(status.getChanged().isEmpty());
  487. assertTrue(status.getConflicting().isEmpty());
  488. assertTrue(status.getMissing().isEmpty());
  489. assertTrue(status.getRemoved().isEmpty());
  490. assertTrue(status.getModified().isEmpty());
  491. assertTrue(status.getUntracked().isEmpty());
  492. assertEquals(1, status.getAdded().size());
  493. assertTrue(status.getAdded().contains(path));
  494. }
  495. @Test
  496. public void unstashNonStashCommit() throws Exception {
  497. try {
  498. git.stashApply().setStashRef(head.name()).call();
  499. fail("Exception not thrown");
  500. } catch (JGitInternalException e) {
  501. assertEquals(MessageFormat.format(
  502. JGitText.get().stashCommitIncorrectNumberOfParents,
  503. head.name(), "0"),
  504. e.getMessage());
  505. }
  506. }
  507. @Test
  508. public void unstashNoHead() throws Exception {
  509. Repository repo = createWorkRepository();
  510. try {
  511. Git.wrap(repo).stashApply().call();
  512. fail("Exception not thrown");
  513. } catch (NoHeadException e) {
  514. assertNotNull(e.getMessage());
  515. }
  516. }
  517. @Test
  518. public void noStashedCommits() throws Exception {
  519. try {
  520. git.stashApply().call();
  521. fail("Exception not thrown");
  522. } catch (InvalidRefNameException e) {
  523. assertNotNull(e.getMessage());
  524. }
  525. }
  526. @Test
  527. public void testApplyStashWithDeletedFile() throws Exception {
  528. File file = writeTrashFile("file", "content");
  529. git.add().addFilepattern("file").call();
  530. git.commit().setMessage("x").call();
  531. file.delete();
  532. git.rm().addFilepattern("file").call();
  533. git.stashCreate().call();
  534. file.delete();
  535. git.stashApply().setStashRef("stash@{0}").call();
  536. assertFalse(file.exists());
  537. }
  538. @Test
  539. public void untrackedFileNotIncluded() throws Exception {
  540. String untrackedPath = "untracked.txt";
  541. File untrackedFile = writeTrashFile(untrackedPath, "content");
  542. // at least one modification needed
  543. writeTrashFile(PATH, "content2");
  544. git.add().addFilepattern(PATH).call();
  545. git.stashCreate().call();
  546. assertTrue(untrackedFile.exists());
  547. git.stashApply().setStashRef("stash@{0}").call();
  548. assertTrue(untrackedFile.exists());
  549. Status status = git.status().call();
  550. assertEquals(1, status.getUntracked().size());
  551. assertTrue(status.getUntracked().contains(untrackedPath));
  552. assertEquals(1, status.getChanged().size());
  553. assertTrue(status.getChanged().contains(PATH));
  554. assertTrue(status.getAdded().isEmpty());
  555. assertTrue(status.getConflicting().isEmpty());
  556. assertTrue(status.getMissing().isEmpty());
  557. assertTrue(status.getRemoved().isEmpty());
  558. assertTrue(status.getModified().isEmpty());
  559. }
  560. @Test
  561. public void untrackedFileIncluded() throws Exception {
  562. String path = "a/b/untracked.txt";
  563. File untrackedFile = writeTrashFile(path, "content");
  564. RevCommit stashedCommit = git.stashCreate().setIncludeUntracked(true)
  565. .call();
  566. assertNotNull(stashedCommit);
  567. assertFalse(untrackedFile.exists());
  568. deleteTrashFile("a/b"); // checkout should create parent dirs
  569. git.stashApply().setStashRef("stash@{0}").call();
  570. assertTrue(untrackedFile.exists());
  571. assertEquals("content", read(path));
  572. Status status = git.status().call();
  573. assertEquals(1, status.getUntracked().size());
  574. assertTrue(status.getAdded().isEmpty());
  575. assertTrue(status.getChanged().isEmpty());
  576. assertTrue(status.getConflicting().isEmpty());
  577. assertTrue(status.getMissing().isEmpty());
  578. assertTrue(status.getRemoved().isEmpty());
  579. assertTrue(status.getModified().isEmpty());
  580. assertTrue(status.getUntracked().contains(path));
  581. }
  582. @Test
  583. public void untrackedFileConflictsWithCommit() throws Exception {
  584. String path = "untracked.txt";
  585. writeTrashFile(path, "untracked");
  586. git.stashCreate().setIncludeUntracked(true).call();
  587. writeTrashFile(path, "committed");
  588. head = git.commit().setMessage("add file").call();
  589. git.add().addFilepattern(path).call();
  590. git.commit().setMessage("conflicting commit").call();
  591. try {
  592. git.stashApply().setStashRef("stash@{0}").call();
  593. fail("StashApplyFailureException should be thrown.");
  594. } catch (StashApplyFailureException e) {
  595. assertEquals(e.getMessage(), JGitText.get().stashApplyConflict);
  596. }
  597. assertEquals("committed", read(path));
  598. }
  599. @Test
  600. public void untrackedFileConflictsWithWorkingDirectory()
  601. throws Exception {
  602. String path = "untracked.txt";
  603. writeTrashFile(path, "untracked");
  604. git.stashCreate().setIncludeUntracked(true).call();
  605. writeTrashFile(path, "working-directory");
  606. try {
  607. git.stashApply().setStashRef("stash@{0}").call();
  608. fail("StashApplyFailureException should be thrown.");
  609. } catch (StashApplyFailureException e) {
  610. assertEquals(e.getMessage(), JGitText.get().stashApplyConflict);
  611. }
  612. assertEquals("working-directory", read(path));
  613. }
  614. @Test
  615. public void untrackedAndTrackedChanges() throws Exception {
  616. writeTrashFile(PATH, "changed");
  617. String path = "untracked.txt";
  618. writeTrashFile(path, "untracked");
  619. git.stashCreate().setIncludeUntracked(true).call();
  620. assertTrue(PATH + " should exist", check(PATH));
  621. assertEquals(PATH + " should have been reset", "content", read(PATH));
  622. assertFalse(path + " should not exist", check(path));
  623. git.stashApply().setStashRef("stash@{0}").call();
  624. assertTrue(PATH + " should exist", check(PATH));
  625. assertEquals(PATH + " should have new content", "changed", read(PATH));
  626. assertTrue(path + " should exist", check(path));
  627. assertEquals(path + " should have new content", "untracked",
  628. read(path));
  629. }
  630. }