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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. @Before
  72. public void setUp() throws Exception {
  73. super.setUp();
  74. git = Git.wrap(db);
  75. committedFile = writeTrashFile(PATH, "content");
  76. git.add().addFilepattern(PATH).call();
  77. head = git.commit().setMessage("add file").call();
  78. assertNotNull(head);
  79. }
  80. @Test
  81. public void workingDirectoryDelete() throws Exception {
  82. deleteTrashFile(PATH);
  83. assertFalse(committedFile.exists());
  84. RevCommit stashed = git.stashCreate().call();
  85. assertNotNull(stashed);
  86. assertEquals("content", read(committedFile));
  87. ObjectId unstashed = git.stashApply().call();
  88. assertEquals(stashed, unstashed);
  89. assertFalse(committedFile.exists());
  90. Status status = git.status().call();
  91. assertTrue(status.getAdded().isEmpty());
  92. assertTrue(status.getChanged().isEmpty());
  93. assertTrue(status.getConflicting().isEmpty());
  94. assertTrue(status.getModified().isEmpty());
  95. assertTrue(status.getUntracked().isEmpty());
  96. assertTrue(status.getRemoved().isEmpty());
  97. assertEquals(1, status.getMissing().size());
  98. assertTrue(status.getMissing().contains(PATH));
  99. }
  100. @Test
  101. public void indexAdd() throws Exception {
  102. String addedPath = "file2.txt";
  103. File addedFile = writeTrashFile(addedPath, "content2");
  104. git.add().addFilepattern(addedPath).call();
  105. RevCommit stashed = git.stashCreate().call();
  106. assertNotNull(stashed);
  107. assertFalse(addedFile.exists());
  108. ObjectId unstashed = git.stashApply().call();
  109. assertEquals(stashed, unstashed);
  110. assertTrue(addedFile.exists());
  111. assertEquals("content2", read(addedFile));
  112. Status status = git.status().call();
  113. assertTrue(status.getChanged().isEmpty());
  114. assertTrue(status.getConflicting().isEmpty());
  115. assertTrue(status.getMissing().isEmpty());
  116. assertTrue(status.getModified().isEmpty());
  117. assertTrue(status.getRemoved().isEmpty());
  118. assertTrue(status.getUntracked().isEmpty());
  119. assertEquals(1, status.getAdded().size());
  120. assertTrue(status.getAdded().contains(addedPath));
  121. }
  122. @Test
  123. public void indexDelete() throws Exception {
  124. git.rm().addFilepattern("file.txt").call();
  125. RevCommit stashed = git.stashCreate().call();
  126. assertNotNull(stashed);
  127. assertEquals("content", read(committedFile));
  128. ObjectId unstashed = git.stashApply().call();
  129. assertEquals(stashed, unstashed);
  130. assertFalse(committedFile.exists());
  131. Status status = git.status().call();
  132. assertTrue(status.getAdded().isEmpty());
  133. assertTrue(status.getChanged().isEmpty());
  134. assertTrue(status.getConflicting().isEmpty());
  135. assertTrue(status.getModified().isEmpty());
  136. assertTrue(status.getMissing().isEmpty());
  137. assertTrue(status.getUntracked().isEmpty());
  138. assertEquals(1, status.getRemoved().size());
  139. assertTrue(status.getRemoved().contains(PATH));
  140. }
  141. @Test
  142. public void workingDirectoryModify() throws Exception {
  143. writeTrashFile("file.txt", "content2");
  144. RevCommit stashed = git.stashCreate().call();
  145. assertNotNull(stashed);
  146. assertEquals("content", read(committedFile));
  147. ObjectId unstashed = git.stashApply().call();
  148. assertEquals(stashed, unstashed);
  149. assertEquals("content2", read(committedFile));
  150. Status status = git.status().call();
  151. assertTrue(status.getAdded().isEmpty());
  152. assertTrue(status.getChanged().isEmpty());
  153. assertTrue(status.getConflicting().isEmpty());
  154. assertTrue(status.getMissing().isEmpty());
  155. assertTrue(status.getRemoved().isEmpty());
  156. assertTrue(status.getUntracked().isEmpty());
  157. assertEquals(1, status.getModified().size());
  158. assertTrue(status.getModified().contains(PATH));
  159. }
  160. @Test
  161. public void workingDirectoryModifyInSubfolder() throws Exception {
  162. String path = "d1/d2/f.txt";
  163. File subfolderFile = writeTrashFile(path, "content");
  164. git.add().addFilepattern(path).call();
  165. head = git.commit().setMessage("add file").call();
  166. writeTrashFile(path, "content2");
  167. RevCommit stashed = git.stashCreate().call();
  168. assertNotNull(stashed);
  169. assertEquals("content", read(subfolderFile));
  170. ObjectId unstashed = git.stashApply().call();
  171. assertEquals(stashed, unstashed);
  172. assertEquals("content2", read(subfolderFile));
  173. Status status = git.status().call();
  174. assertTrue(status.getAdded().isEmpty());
  175. assertTrue(status.getChanged().isEmpty());
  176. assertTrue(status.getConflicting().isEmpty());
  177. assertTrue(status.getMissing().isEmpty());
  178. assertTrue(status.getRemoved().isEmpty());
  179. assertTrue(status.getUntracked().isEmpty());
  180. assertEquals(1, status.getModified().size());
  181. assertTrue(status.getModified().contains(path));
  182. }
  183. @Test
  184. public void workingDirectoryModifyIndexChanged() throws Exception {
  185. writeTrashFile("file.txt", "content2");
  186. git.add().addFilepattern("file.txt").call();
  187. writeTrashFile("file.txt", "content3");
  188. RevCommit stashed = git.stashCreate().call();
  189. assertNotNull(stashed);
  190. assertEquals("content", read(committedFile));
  191. ObjectId unstashed = git.stashApply().call();
  192. assertEquals(stashed, unstashed);
  193. assertEquals("content3", read(committedFile));
  194. Status status = git.status().call();
  195. assertTrue(status.getAdded().isEmpty());
  196. assertTrue(status.getConflicting().isEmpty());
  197. assertTrue(status.getMissing().isEmpty());
  198. assertTrue(status.getRemoved().isEmpty());
  199. assertTrue(status.getUntracked().isEmpty());
  200. assertEquals(1, status.getChanged().size());
  201. assertTrue(status.getChanged().contains(PATH));
  202. assertEquals(1, status.getModified().size());
  203. assertTrue(status.getModified().contains(PATH));
  204. }
  205. @Test
  206. public void workingDirectoryCleanIndexModify() throws Exception {
  207. writeTrashFile("file.txt", "content2");
  208. git.add().addFilepattern("file.txt").call();
  209. writeTrashFile("file.txt", "content");
  210. RevCommit stashed = git.stashCreate().call();
  211. assertNotNull(stashed);
  212. assertEquals("content", read(committedFile));
  213. ObjectId unstashed = git.stashApply().call();
  214. assertEquals(stashed, unstashed);
  215. assertEquals("content2", read(committedFile));
  216. Status status = git.status().call();
  217. assertTrue(status.getAdded().isEmpty());
  218. assertTrue(status.getConflicting().isEmpty());
  219. assertTrue(status.getMissing().isEmpty());
  220. assertTrue(status.getModified().isEmpty());
  221. assertTrue(status.getRemoved().isEmpty());
  222. assertTrue(status.getUntracked().isEmpty());
  223. assertEquals(1, status.getChanged().size());
  224. assertTrue(status.getChanged().contains(PATH));
  225. }
  226. @Test
  227. public void workingDirectoryDeleteIndexAdd() throws Exception {
  228. String path = "file2.txt";
  229. File added = writeTrashFile(path, "content2");
  230. assertTrue(added.exists());
  231. git.add().addFilepattern(path).call();
  232. FileUtils.delete(added);
  233. assertFalse(added.exists());
  234. RevCommit stashed = git.stashCreate().call();
  235. assertNotNull(stashed);
  236. assertFalse(added.exists());
  237. ObjectId unstashed = git.stashApply().call();
  238. assertEquals(stashed, unstashed);
  239. assertEquals("content2", read(added));
  240. Status status = git.status().call();
  241. assertTrue(status.getChanged().isEmpty());
  242. assertTrue(status.getConflicting().isEmpty());
  243. assertTrue(status.getMissing().isEmpty());
  244. assertTrue(status.getModified().isEmpty());
  245. assertTrue(status.getRemoved().isEmpty());
  246. assertTrue(status.getUntracked().isEmpty());
  247. assertEquals(1, status.getAdded().size());
  248. assertTrue(status.getAdded().contains(path));
  249. }
  250. @Test
  251. public void workingDirectoryDeleteIndexEdit() throws Exception {
  252. writeTrashFile(PATH, "content2");
  253. git.add().addFilepattern(PATH).call();
  254. FileUtils.delete(committedFile);
  255. assertFalse(committedFile.exists());
  256. RevCommit stashed = git.stashCreate().call();
  257. assertNotNull(stashed);
  258. assertEquals("content", read(committedFile));
  259. ObjectId unstashed = git.stashApply().call();
  260. assertEquals(stashed, unstashed);
  261. assertFalse(committedFile.exists());
  262. Status status = git.status().call();
  263. assertTrue(status.getAdded().isEmpty());
  264. assertEquals(1, status.getChanged().size());
  265. assertTrue(status.getChanged().contains(PATH));
  266. assertTrue(status.getConflicting().isEmpty());
  267. assertEquals(1, status.getMissing().size());
  268. assertTrue(status.getMissing().contains(PATH));
  269. assertTrue(status.getModified().isEmpty());
  270. assertTrue(status.getUntracked().isEmpty());
  271. assertTrue(status.getRemoved().isEmpty());
  272. }
  273. @Test
  274. public void multipleEdits() throws Exception {
  275. String addedPath = "file2.txt";
  276. git.rm().addFilepattern(PATH).call();
  277. File addedFile = writeTrashFile(addedPath, "content2");
  278. git.add().addFilepattern(addedPath).call();
  279. RevCommit stashed = git.stashCreate().call();
  280. assertNotNull(stashed);
  281. assertTrue(committedFile.exists());
  282. assertFalse(addedFile.exists());
  283. ObjectId unstashed = git.stashApply().call();
  284. assertEquals(stashed, unstashed);
  285. Status status = git.status().call();
  286. assertTrue(status.getChanged().isEmpty());
  287. assertTrue(status.getConflicting().isEmpty());
  288. assertTrue(status.getMissing().isEmpty());
  289. assertTrue(status.getModified().isEmpty());
  290. assertTrue(status.getUntracked().isEmpty());
  291. assertEquals(1, status.getRemoved().size());
  292. assertTrue(status.getRemoved().contains(PATH));
  293. assertEquals(1, status.getAdded().size());
  294. assertTrue(status.getAdded().contains(addedPath));
  295. }
  296. @Test
  297. public void workingDirectoryContentConflict() throws Exception {
  298. writeTrashFile(PATH, "content2");
  299. RevCommit stashed = git.stashCreate().call();
  300. assertNotNull(stashed);
  301. assertEquals("content", read(committedFile));
  302. assertTrue(git.status().call().isClean());
  303. writeTrashFile(PATH, "content3");
  304. try {
  305. git.stashApply().call();
  306. fail("Exception not thrown");
  307. } catch (StashApplyFailureException e) {
  308. // expected
  309. }
  310. assertEquals("content3", read(PATH));
  311. }
  312. @Test
  313. public void stashedContentMerge() throws Exception {
  314. writeTrashFile(PATH, "content\nmore content\n");
  315. git.add().addFilepattern(PATH).call();
  316. git.commit().setMessage("more content").call();
  317. writeTrashFile(PATH, "content\nhead change\nmore content\n");
  318. git.add().addFilepattern(PATH).call();
  319. git.commit().setMessage("even content").call();
  320. writeTrashFile(PATH, "content\nstashed change\nmore content\n");
  321. RevCommit stashed = git.stashCreate().call();
  322. assertNotNull(stashed);
  323. assertEquals("content\nhead change\nmore content\n",
  324. read(committedFile));
  325. assertTrue(git.status().call().isClean());
  326. writeTrashFile(PATH, "content\nmore content\ncommitted change\n");
  327. git.add().addFilepattern(PATH).call();
  328. git.commit().setMessage("committed change").call();
  329. try {
  330. git.stashApply().call();
  331. fail("Expected conflict");
  332. } catch (StashApplyFailureException e) {
  333. // expected
  334. }
  335. Status status = new StatusCommand(db).call();
  336. assertEquals(1, status.getConflicting().size());
  337. assertEquals(
  338. "content\n<<<<<<< HEAD\n=======\nstashed change\n>>>>>>> stash\nmore content\ncommitted change\n",
  339. read(PATH));
  340. }
  341. @Test
  342. public void stashedApplyOnOtherBranch() throws Exception {
  343. writeTrashFile(PATH, "content\nmore content\n");
  344. git.add().addFilepattern(PATH).call();
  345. git.commit().setMessage("more content").call();
  346. String path2 = "file2.txt";
  347. File file2 = writeTrashFile(path2, "content\nmore content\n");
  348. git.add().addFilepattern(PATH).call();
  349. git.add().addFilepattern(path2).call();
  350. git.commit().setMessage("even content").call();
  351. String otherBranch = "otherBranch";
  352. git.branchCreate().setName(otherBranch).call();
  353. writeTrashFile(PATH, "master content");
  354. git.add().addFilepattern(PATH).call();
  355. git.commit().setMessage("even content").call();
  356. git.checkout().setName(otherBranch).call();
  357. writeTrashFile(PATH, "otherBranch content");
  358. git.add().addFilepattern(PATH).call();
  359. git.commit().setMessage("even more content").call();
  360. writeTrashFile(path2, "content\nstashed change\nmore content\n");
  361. RevCommit stashed = git.stashCreate().call();
  362. assertNotNull(stashed);
  363. assertEquals("content\nmore content\n", read(file2));
  364. assertEquals("otherBranch content",
  365. read(committedFile));
  366. assertTrue(git.status().call().isClean());
  367. git.checkout().setName("master").call();
  368. git.stashApply().call();
  369. assertEquals("content\nstashed change\nmore content\n", read(file2));
  370. assertEquals("master content",
  371. read(committedFile));
  372. }
  373. @Test
  374. public void stashedApplyOnOtherBranchWithStagedChange() throws Exception {
  375. writeTrashFile(PATH, "content\nmore content\n");
  376. git.add().addFilepattern(PATH).call();
  377. git.commit().setMessage("more content").call();
  378. String path2 = "file2.txt";
  379. File file2 = writeTrashFile(path2, "content\nmore content\n");
  380. git.add().addFilepattern(PATH).call();
  381. git.add().addFilepattern(path2).call();
  382. git.commit().setMessage("even content").call();
  383. String otherBranch = "otherBranch";
  384. git.branchCreate().setName(otherBranch).call();
  385. writeTrashFile(PATH, "master content");
  386. git.add().addFilepattern(PATH).call();
  387. git.commit().setMessage("even content").call();
  388. git.checkout().setName(otherBranch).call();
  389. writeTrashFile(PATH, "otherBranch content");
  390. git.add().addFilepattern(PATH).call();
  391. git.commit().setMessage("even more content").call();
  392. writeTrashFile(path2,
  393. "content\nstashed change in index\nmore content\n");
  394. git.add().addFilepattern(path2).call();
  395. writeTrashFile(path2, "content\nstashed change\nmore content\n");
  396. RevCommit stashed = git.stashCreate().call();
  397. assertNotNull(stashed);
  398. assertEquals("content\nmore content\n", read(file2));
  399. assertEquals("otherBranch content", read(committedFile));
  400. assertTrue(git.status().call().isClean());
  401. git.checkout().setName("master").call();
  402. git.stashApply().call();
  403. assertEquals("content\nstashed change\nmore content\n", read(file2));
  404. assertEquals(
  405. "[file.txt, mode:100644, content:master content]"
  406. + "[file2.txt, mode:100644, content:content\nstashed change in index\nmore content\n]",
  407. indexState(CONTENT));
  408. assertEquals("master content", read(committedFile));
  409. }
  410. @Test
  411. public void workingDirectoryContentMerge() throws Exception {
  412. writeTrashFile(PATH, "content\nmore content\n");
  413. git.add().addFilepattern(PATH).call();
  414. git.commit().setMessage("more content").call();
  415. writeTrashFile(PATH, "content\nstashed change\nmore content\n");
  416. RevCommit stashed = git.stashCreate().call();
  417. assertNotNull(stashed);
  418. assertEquals("content\nmore content\n", read(committedFile));
  419. assertTrue(git.status().call().isClean());
  420. writeTrashFile(PATH, "content\nmore content\ncommitted change\n");
  421. git.add().addFilepattern(PATH).call();
  422. git.commit().setMessage("committed change").call();
  423. git.stashApply().call();
  424. assertEquals(
  425. "content\nstashed change\nmore content\ncommitted change\n",
  426. read(committedFile));
  427. }
  428. @Test
  429. public void indexContentConflict() throws Exception {
  430. writeTrashFile(PATH, "content2");
  431. RevCommit stashed = git.stashCreate().call();
  432. assertNotNull(stashed);
  433. assertEquals("content", read(committedFile));
  434. assertTrue(git.status().call().isClean());
  435. writeTrashFile(PATH, "content3");
  436. git.add().addFilepattern(PATH).call();
  437. writeTrashFile(PATH, "content2");
  438. try {
  439. git.stashApply().call();
  440. fail("Exception not thrown");
  441. } catch (StashApplyFailureException e) {
  442. // expected
  443. }
  444. assertEquals("content2", read(PATH));
  445. }
  446. @Test
  447. public void workingDirectoryEditPreCommit() throws Exception {
  448. writeTrashFile(PATH, "content2");
  449. RevCommit stashed = git.stashCreate().call();
  450. assertNotNull(stashed);
  451. assertEquals("content", read(committedFile));
  452. assertTrue(git.status().call().isClean());
  453. String path2 = "file2.txt";
  454. writeTrashFile(path2, "content3");
  455. git.add().addFilepattern(path2).call();
  456. assertNotNull(git.commit().setMessage("adding file").call());
  457. ObjectId unstashed = git.stashApply().call();
  458. assertEquals(stashed, unstashed);
  459. Status status = git.status().call();
  460. assertTrue(status.getAdded().isEmpty());
  461. assertTrue(status.getChanged().isEmpty());
  462. assertTrue(status.getConflicting().isEmpty());
  463. assertTrue(status.getMissing().isEmpty());
  464. assertTrue(status.getRemoved().isEmpty());
  465. assertTrue(status.getUntracked().isEmpty());
  466. assertEquals(1, status.getModified().size());
  467. assertTrue(status.getModified().contains(PATH));
  468. }
  469. @Test
  470. public void stashChangeInANewSubdirectory() throws Exception {
  471. String subdir = "subdir";
  472. String fname = "file2.txt";
  473. String path = subdir + "/" + fname;
  474. String otherBranch = "otherbranch";
  475. writeTrashFile(subdir, fname, "content2");
  476. git.add().addFilepattern(path).call();
  477. RevCommit stashed = git.stashCreate().call();
  478. assertNotNull(stashed);
  479. assertTrue(git.status().call().isClean());
  480. git.branchCreate().setName(otherBranch).call();
  481. git.checkout().setName(otherBranch).call();
  482. ObjectId unstashed = git.stashApply().call();
  483. assertEquals(stashed, unstashed);
  484. Status status = git.status().call();
  485. assertTrue(status.getChanged().isEmpty());
  486. assertTrue(status.getConflicting().isEmpty());
  487. assertTrue(status.getMissing().isEmpty());
  488. assertTrue(status.getRemoved().isEmpty());
  489. assertTrue(status.getModified().isEmpty());
  490. assertTrue(status.getUntracked().isEmpty());
  491. assertEquals(1, status.getAdded().size());
  492. assertTrue(status.getAdded().contains(path));
  493. }
  494. @Test
  495. public void unstashNonStashCommit() throws Exception {
  496. try {
  497. git.stashApply().setStashRef(head.name()).call();
  498. fail("Exception not thrown");
  499. } catch (JGitInternalException e) {
  500. assertEquals(MessageFormat.format(
  501. JGitText.get().stashCommitMissingTwoParents, head.name()),
  502. e.getMessage());
  503. }
  504. }
  505. @Test
  506. public void unstashNoHead() throws Exception {
  507. Repository repo = createWorkRepository();
  508. try {
  509. Git.wrap(repo).stashApply().call();
  510. fail("Exception not thrown");
  511. } catch (NoHeadException e) {
  512. assertNotNull(e.getMessage());
  513. }
  514. }
  515. @Test
  516. public void noStashedCommits() throws Exception {
  517. try {
  518. git.stashApply().call();
  519. fail("Exception not thrown");
  520. } catch (InvalidRefNameException e) {
  521. assertNotNull(e.getMessage());
  522. }
  523. }
  524. @Test
  525. public void testApplyStashWithDeletedFile() throws Exception {
  526. File file = writeTrashFile("file", "content");
  527. git.add().addFilepattern("file").call();
  528. git.commit().setMessage("x").call();
  529. file.delete();
  530. git.rm().addFilepattern("file").call();
  531. git.stashCreate().call();
  532. file.delete();
  533. git.stashApply().setStashRef("stash@{0}").call();
  534. assertFalse(file.exists());
  535. }
  536. }