Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

StashApplyCommandTest.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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 workingDirectoryContentMerge() throws Exception {
  343. writeTrashFile(PATH, "content\nmore content\n");
  344. git.add().addFilepattern(PATH).call();
  345. git.commit().setMessage("more content").call();
  346. writeTrashFile(PATH, "content\nstashed change\nmore content\n");
  347. RevCommit stashed = git.stashCreate().call();
  348. assertNotNull(stashed);
  349. assertEquals("content\nmore content\n", read(committedFile));
  350. assertTrue(git.status().call().isClean());
  351. writeTrashFile(PATH, "content\nmore content\ncommitted change\n");
  352. git.add().addFilepattern(PATH).call();
  353. git.commit().setMessage("committed change").call();
  354. git.stashApply().call();
  355. assertEquals(
  356. "content\nstashed change\nmore content\ncommitted change\n",
  357. read(committedFile));
  358. }
  359. @Test
  360. public void indexContentConflict() throws Exception {
  361. writeTrashFile(PATH, "content2");
  362. RevCommit stashed = git.stashCreate().call();
  363. assertNotNull(stashed);
  364. assertEquals("content", read(committedFile));
  365. assertTrue(git.status().call().isClean());
  366. writeTrashFile(PATH, "content3");
  367. git.add().addFilepattern(PATH).call();
  368. writeTrashFile(PATH, "content2");
  369. try {
  370. git.stashApply().call();
  371. fail("Exception not thrown");
  372. } catch (StashApplyFailureException e) {
  373. // expected
  374. }
  375. assertEquals("content2", read(PATH));
  376. }
  377. @Test
  378. public void workingDirectoryEditPreCommit() throws Exception {
  379. writeTrashFile(PATH, "content2");
  380. RevCommit stashed = git.stashCreate().call();
  381. assertNotNull(stashed);
  382. assertEquals("content", read(committedFile));
  383. assertTrue(git.status().call().isClean());
  384. String path2 = "file2.txt";
  385. writeTrashFile(path2, "content3");
  386. git.add().addFilepattern(path2).call();
  387. assertNotNull(git.commit().setMessage("adding file").call());
  388. ObjectId unstashed = git.stashApply().call();
  389. assertEquals(stashed, unstashed);
  390. Status status = git.status().call();
  391. assertTrue(status.getAdded().isEmpty());
  392. assertTrue(status.getChanged().isEmpty());
  393. assertTrue(status.getConflicting().isEmpty());
  394. assertTrue(status.getMissing().isEmpty());
  395. assertTrue(status.getRemoved().isEmpty());
  396. assertTrue(status.getUntracked().isEmpty());
  397. assertEquals(1, status.getModified().size());
  398. assertTrue(status.getModified().contains(PATH));
  399. }
  400. @Test
  401. public void stashChangeInANewSubdirectory() throws Exception {
  402. String subdir = "subdir";
  403. String fname = "file2.txt";
  404. String path = subdir + "/" + fname;
  405. String otherBranch = "otherbranch";
  406. writeTrashFile(subdir, fname, "content2");
  407. git.add().addFilepattern(path).call();
  408. RevCommit stashed = git.stashCreate().call();
  409. assertNotNull(stashed);
  410. assertTrue(git.status().call().isClean());
  411. git.branchCreate().setName(otherBranch).call();
  412. git.checkout().setName(otherBranch).call();
  413. ObjectId unstashed = git.stashApply().call();
  414. assertEquals(stashed, unstashed);
  415. Status status = git.status().call();
  416. assertTrue(status.getChanged().isEmpty());
  417. assertTrue(status.getConflicting().isEmpty());
  418. assertTrue(status.getMissing().isEmpty());
  419. assertTrue(status.getRemoved().isEmpty());
  420. assertTrue(status.getModified().isEmpty());
  421. assertTrue(status.getUntracked().isEmpty());
  422. assertEquals(1, status.getAdded().size());
  423. assertTrue(status.getAdded().contains(path));
  424. }
  425. @Test
  426. public void unstashNonStashCommit() throws Exception {
  427. try {
  428. git.stashApply().setStashRef(head.name()).call();
  429. fail("Exception not thrown");
  430. } catch (JGitInternalException e) {
  431. assertEquals(MessageFormat.format(
  432. JGitText.get().stashCommitMissingTwoParents, head.name()),
  433. e.getMessage());
  434. }
  435. }
  436. @Test
  437. public void unstashNoHead() throws Exception {
  438. Repository repo = createWorkRepository();
  439. try {
  440. Git.wrap(repo).stashApply().call();
  441. fail("Exception not thrown");
  442. } catch (NoHeadException e) {
  443. assertNotNull(e.getMessage());
  444. }
  445. }
  446. @Test
  447. public void noStashedCommits() throws Exception {
  448. try {
  449. git.stashApply().call();
  450. fail("Exception not thrown");
  451. } catch (InvalidRefNameException e) {
  452. assertNotNull(e.getMessage());
  453. }
  454. }
  455. @Test
  456. public void testApplyStashWithDeletedFile() throws Exception {
  457. File file = writeTrashFile("file", "content");
  458. git.add().addFilepattern("file").call();
  459. git.commit().setMessage("x").call();
  460. file.delete();
  461. git.rm().addFilepattern("file").call();
  462. git.stashCreate().call();
  463. file.delete();
  464. git.stashApply().setStashRef("stash@{0}").call();
  465. assertFalse(file.exists());
  466. }
  467. }