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.

RebaseCommandTest.java 51KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  1. /*
  2. * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
  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.BufferedReader;
  50. import java.io.File;
  51. import java.io.FileInputStream;
  52. import java.io.IOException;
  53. import java.io.InputStreamReader;
  54. import org.eclipse.jgit.api.MergeResult.MergeStatus;
  55. import org.eclipse.jgit.api.RebaseCommand.Action;
  56. import org.eclipse.jgit.api.RebaseCommand.Operation;
  57. import org.eclipse.jgit.api.RebaseResult.Status;
  58. import org.eclipse.jgit.api.errors.JGitInternalException;
  59. import org.eclipse.jgit.api.errors.RefNotFoundException;
  60. import org.eclipse.jgit.api.errors.UnmergedPathsException;
  61. import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
  62. import org.eclipse.jgit.dircache.DirCacheCheckout;
  63. import org.eclipse.jgit.lib.Constants;
  64. import org.eclipse.jgit.lib.ObjectId;
  65. import org.eclipse.jgit.lib.PersonIdent;
  66. import org.eclipse.jgit.lib.RefUpdate;
  67. import org.eclipse.jgit.lib.RepositoryState;
  68. import org.eclipse.jgit.lib.RepositoryTestCase;
  69. import org.eclipse.jgit.merge.MergeStrategy;
  70. import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason;
  71. import org.eclipse.jgit.revwalk.RevCommit;
  72. import org.eclipse.jgit.revwalk.RevWalk;
  73. import org.junit.Before;
  74. import org.junit.Test;
  75. public class RebaseCommandTest extends RepositoryTestCase {
  76. private static final String FILE1 = "file1";
  77. protected Git git;
  78. @Override
  79. @Before
  80. public void setUp() throws Exception {
  81. super.setUp();
  82. this.git = new Git(db);
  83. }
  84. private void checkoutCommit(RevCommit commit) throws IllegalStateException,
  85. IOException {
  86. RevWalk walk = new RevWalk(db);
  87. RevCommit head = walk.parseCommit(db.resolve(Constants.HEAD));
  88. DirCacheCheckout dco = new DirCacheCheckout(db, head.getTree(), db
  89. .lockDirCache(), commit.getTree());
  90. dco.setFailOnConflict(true);
  91. dco.checkout();
  92. walk.release();
  93. // update the HEAD
  94. RefUpdate refUpdate = db.updateRef(Constants.HEAD, true);
  95. refUpdate.setNewObjectId(commit);
  96. refUpdate.forceUpdate();
  97. }
  98. @Test
  99. public void testFastForwardWithNewFile() throws Exception {
  100. // create file1 on master
  101. writeTrashFile(FILE1, FILE1);
  102. git.add().addFilepattern(FILE1).call();
  103. RevCommit first = git.commit().setMessage("Add file1").call();
  104. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  105. // create a topic branch
  106. createBranch(first, "refs/heads/topic");
  107. // create file2 on master
  108. File file2 = writeTrashFile("file2", "file2");
  109. git.add().addFilepattern("file2").call();
  110. git.commit().setMessage("Add file2").call();
  111. assertTrue(new File(db.getWorkTree(), "file2").exists());
  112. checkoutBranch("refs/heads/topic");
  113. assertFalse(new File(db.getWorkTree(), "file2").exists());
  114. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  115. assertTrue(new File(db.getWorkTree(), "file2").exists());
  116. checkFile(file2, "file2");
  117. assertEquals(Status.FAST_FORWARD, res.getStatus());
  118. }
  119. @Test
  120. public void testFastForwardWithMultipleCommits() throws Exception {
  121. // create file1 on master
  122. writeTrashFile(FILE1, FILE1);
  123. git.add().addFilepattern(FILE1).call();
  124. RevCommit first = git.commit().setMessage("Add file1").call();
  125. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  126. // create a topic branch
  127. createBranch(first, "refs/heads/topic");
  128. // create file2 on master
  129. File file2 = writeTrashFile("file2", "file2");
  130. git.add().addFilepattern("file2").call();
  131. git.commit().setMessage("Add file2").call();
  132. assertTrue(new File(db.getWorkTree(), "file2").exists());
  133. // write a second commit
  134. writeTrashFile("file2", "file2 new content");
  135. git.add().addFilepattern("file2").call();
  136. git.commit().setMessage("Change content of file2").call();
  137. checkoutBranch("refs/heads/topic");
  138. assertFalse(new File(db.getWorkTree(), "file2").exists());
  139. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  140. assertTrue(new File(db.getWorkTree(), "file2").exists());
  141. checkFile(file2, "file2 new content");
  142. assertEquals(Status.FAST_FORWARD, res.getStatus());
  143. }
  144. @Test
  145. public void testRebaseFailsCantCherryPickMergeCommits()
  146. throws Exception {
  147. /**
  148. * Create the following commits and then attempt to rebase topic onto
  149. * master. This will fail as the cherry-pick list C, D, E an F contains
  150. * a merge commit (F).
  151. *
  152. * <pre>
  153. * A - B (master)
  154. * \
  155. * C - D - F (topic)
  156. * \ /
  157. * E - (side)
  158. * </pre>
  159. */
  160. // create file1 on master
  161. writeTrashFile(FILE1, FILE1);
  162. git.add().addFilepattern(FILE1).call();
  163. RevCommit first = git.commit().setMessage("Add file1").call();
  164. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  165. // create a topic branch
  166. createBranch(first, "refs/heads/topic");
  167. // update FILE1 on master
  168. writeTrashFile(FILE1, "blah");
  169. git.add().addFilepattern(FILE1).call();
  170. git.commit().setMessage("updated file1 on master").call();
  171. checkoutBranch("refs/heads/topic");
  172. writeTrashFile("file3", "more changess");
  173. git.add().addFilepattern("file3").call();
  174. RevCommit topicCommit = git.commit()
  175. .setMessage("update file3 on topic").call();
  176. // create a branch from the topic commit
  177. createBranch(topicCommit, "refs/heads/side");
  178. // second commit on topic
  179. writeTrashFile("file2", "file2");
  180. git.add().addFilepattern("file2").call();
  181. git.commit().setMessage("Add file2").call();
  182. assertTrue(new File(db.getWorkTree(), "file2").exists());
  183. // switch to side branch and update file2
  184. checkoutBranch("refs/heads/side");
  185. writeTrashFile("file3", "more change");
  186. git.add().addFilepattern("file3").call();
  187. RevCommit sideCommit = git.commit().setMessage("update file2 on side")
  188. .call();
  189. // switch back to topic and merge in side
  190. checkoutBranch("refs/heads/topic");
  191. MergeResult result = git.merge().include(sideCommit.getId())
  192. .setStrategy(MergeStrategy.RESOLVE).call();
  193. assertEquals(MergeStatus.MERGED, result.getMergeStatus());
  194. try {
  195. RebaseResult rebase = git.rebase().setUpstream("refs/heads/master")
  196. .call();
  197. fail("MultipleParentsNotAllowedException expected: "
  198. + rebase.getStatus());
  199. } catch (JGitInternalException e) {
  200. // expected
  201. }
  202. }
  203. @Test
  204. public void testRebaseParentOntoHeadShouldBeUptoDate() throws Exception {
  205. writeTrashFile(FILE1, FILE1);
  206. git.add().addFilepattern(FILE1).call();
  207. RevCommit parent = git.commit().setMessage("parent comment").call();
  208. writeTrashFile(FILE1, "another change");
  209. git.add().addFilepattern(FILE1).call();
  210. git.commit().setMessage("head commit").call();
  211. RebaseResult result = git.rebase().setUpstream(parent).call();
  212. assertEquals(Status.UP_TO_DATE, result.getStatus());
  213. }
  214. @Test
  215. public void testUpToDate() throws Exception {
  216. // create file1 on master
  217. writeTrashFile(FILE1, FILE1);
  218. git.add().addFilepattern(FILE1).call();
  219. RevCommit first = git.commit().setMessage("Add file1").call();
  220. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  221. RebaseResult res = git.rebase().setUpstream(first).call();
  222. assertEquals(Status.UP_TO_DATE, res.getStatus());
  223. }
  224. @Test
  225. public void testUnknownUpstream() throws Exception {
  226. // create file1 on master
  227. writeTrashFile(FILE1, FILE1);
  228. git.add().addFilepattern(FILE1).call();
  229. git.commit().setMessage("Add file1").call();
  230. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  231. try {
  232. git.rebase().setUpstream("refs/heads/xyz").call();
  233. fail("expected exception was not thrown");
  234. } catch (RefNotFoundException e) {
  235. // expected exception
  236. }
  237. }
  238. @Test
  239. public void testConflictFreeWithSingleFile() throws Exception {
  240. // create file1 on master
  241. File theFile = writeTrashFile(FILE1, "1\n2\n3\n");
  242. git.add().addFilepattern(FILE1).call();
  243. RevCommit second = git.commit().setMessage("Add file1").call();
  244. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  245. // change first line in master and commit
  246. writeTrashFile(FILE1, "1master\n2\n3\n");
  247. checkFile(theFile, "1master\n2\n3\n");
  248. git.add().addFilepattern(FILE1).call();
  249. RevCommit lastMasterChange = git.commit().setMessage(
  250. "change file1 in master").call();
  251. // create a topic branch based on second commit
  252. createBranch(second, "refs/heads/topic");
  253. checkoutBranch("refs/heads/topic");
  254. // we have the old content again
  255. checkFile(theFile, "1\n2\n3\n");
  256. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  257. // change third line in topic branch
  258. writeTrashFile(FILE1, "1\n2\n3\ntopic\n");
  259. git.add().addFilepattern(FILE1).call();
  260. git.commit().setMessage("change file1 in topic").call();
  261. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  262. assertEquals(Status.OK, res.getStatus());
  263. checkFile(theFile, "1master\n2\n3\ntopic\n");
  264. // our old branch should be checked out again
  265. assertEquals("refs/heads/topic", db.getFullBranch());
  266. assertEquals(lastMasterChange, new RevWalk(db).parseCommit(
  267. db.resolve(Constants.HEAD)).getParent(0));
  268. }
  269. @Test
  270. public void testDetachedHead() throws Exception {
  271. // create file1 on master
  272. File theFile = writeTrashFile(FILE1, "1\n2\n3\n");
  273. git.add().addFilepattern(FILE1).call();
  274. RevCommit second = git.commit().setMessage("Add file1").call();
  275. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  276. // change first line in master and commit
  277. writeTrashFile(FILE1, "1master\n2\n3\n");
  278. checkFile(theFile, "1master\n2\n3\n");
  279. git.add().addFilepattern(FILE1).call();
  280. RevCommit lastMasterChange = git.commit().setMessage(
  281. "change file1 in master").call();
  282. // create a topic branch based on second commit
  283. createBranch(second, "refs/heads/topic");
  284. checkoutBranch("refs/heads/topic");
  285. // we have the old content again
  286. checkFile(theFile, "1\n2\n3\n");
  287. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  288. // change third line in topic branch
  289. writeTrashFile(FILE1, "1\n2\n3\ntopic\n");
  290. git.add().addFilepattern(FILE1).call();
  291. RevCommit topicCommit = git.commit()
  292. .setMessage("change file1 in topic").call();
  293. checkoutBranch("refs/heads/master");
  294. checkoutCommit(topicCommit);
  295. assertEquals(topicCommit.getId().getName(), db.getFullBranch());
  296. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  297. assertEquals(Status.OK, res.getStatus());
  298. checkFile(theFile, "1master\n2\n3\ntopic\n");
  299. assertEquals(lastMasterChange, new RevWalk(db).parseCommit(
  300. db.resolve(Constants.HEAD)).getParent(0));
  301. }
  302. @Test
  303. public void testFilesAddedFromTwoBranches() throws Exception {
  304. // create file1 on master
  305. writeTrashFile(FILE1, FILE1);
  306. git.add().addFilepattern(FILE1).call();
  307. RevCommit masterCommit = git.commit().setMessage("Add file1 to master")
  308. .call();
  309. // create a branch named file2 and add file2
  310. createBranch(masterCommit, "refs/heads/file2");
  311. checkoutBranch("refs/heads/file2");
  312. writeTrashFile("file2", "file2");
  313. git.add().addFilepattern("file2").call();
  314. RevCommit addFile2 = git.commit().setMessage(
  315. "Add file2 to branch file2").call();
  316. // create a branch named file3 and add file3
  317. createBranch(masterCommit, "refs/heads/file3");
  318. checkoutBranch("refs/heads/file3");
  319. writeTrashFile("file3", "file3");
  320. git.add().addFilepattern("file3").call();
  321. git.commit().setMessage("Add file3 to branch file3").call();
  322. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  323. assertFalse(new File(db.getWorkTree(), "file2").exists());
  324. assertTrue(new File(db.getWorkTree(), "file3").exists());
  325. RebaseResult res = git.rebase().setUpstream("refs/heads/file2").call();
  326. assertEquals(Status.OK, res.getStatus());
  327. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  328. assertTrue(new File(db.getWorkTree(), "file2").exists());
  329. assertTrue(new File(db.getWorkTree(), "file3").exists());
  330. // our old branch should be checked out again
  331. assertEquals("refs/heads/file3", db.getFullBranch());
  332. assertEquals(addFile2, new RevWalk(db).parseCommit(
  333. db.resolve(Constants.HEAD)).getParent(0));
  334. checkoutBranch("refs/heads/file2");
  335. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  336. assertTrue(new File(db.getWorkTree(), "file2").exists());
  337. assertFalse(new File(db.getWorkTree(), "file3").exists());
  338. }
  339. @Test
  340. public void testStopOnConflict() throws Exception {
  341. // create file1 on master
  342. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  343. "2", "3");
  344. // change first line in master
  345. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  346. checkFile(FILE1, "1master", "2", "3");
  347. // create a topic branch based on second commit
  348. createBranch(firstInMaster, "refs/heads/topic");
  349. checkoutBranch("refs/heads/topic");
  350. // we have the old content again
  351. checkFile(FILE1, "1", "2", "3");
  352. // add a line (non-conflicting)
  353. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  354. "3", "topic4");
  355. // change first line (conflicting)
  356. RevCommit conflicting = writeFileAndCommit(FILE1,
  357. "change file1 in topic", "1topic", "2", "3", "topic4");
  358. RevCommit lastTopicCommit = writeFileAndCommit(FILE1,
  359. "change file1 in topic again", "1topic", "2", "3", "topic4");
  360. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  361. assertEquals(Status.STOPPED, res.getStatus());
  362. assertEquals(conflicting, res.getCurrentCommit());
  363. checkFile(FILE1,
  364. "<<<<<<< OURS\n1master\n=======\n1topic\n>>>>>>> THEIRS\n2\n3\ntopic4");
  365. assertEquals(RepositoryState.REBASING_INTERACTIVE, db
  366. .getRepositoryState());
  367. assertTrue(new File(db.getDirectory(), "rebase-merge").exists());
  368. // the first one should be included, so we should have left two picks in
  369. // the file
  370. assertEquals(1, countPicks());
  371. // rebase should not succeed in this state
  372. try {
  373. git.rebase().setUpstream("refs/heads/master").call();
  374. fail("Expected exception was not thrown");
  375. } catch (WrongRepositoryStateException e) {
  376. // expected
  377. }
  378. // abort should reset to topic branch
  379. res = git.rebase().setOperation(Operation.ABORT).call();
  380. assertEquals(res.getStatus(), Status.ABORTED);
  381. assertEquals("refs/heads/topic", db.getFullBranch());
  382. checkFile(FILE1, "1topic", "2", "3", "topic4");
  383. RevWalk rw = new RevWalk(db);
  384. assertEquals(lastTopicCommit, rw
  385. .parseCommit(db.resolve(Constants.HEAD)));
  386. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  387. // rebase- dir in .git must be deleted
  388. assertFalse(new File(db.getDirectory(), "rebase-merge").exists());
  389. }
  390. @Test
  391. public void testStopOnConflictAndContinue() throws Exception {
  392. // create file1 on master
  393. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  394. "2", "3");
  395. // change in master
  396. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  397. checkFile(FILE1, "1master", "2", "3");
  398. // create a topic branch based on the first commit
  399. createBranch(firstInMaster, "refs/heads/topic");
  400. checkoutBranch("refs/heads/topic");
  401. // we have the old content again
  402. checkFile(FILE1, "1", "2", "3");
  403. // add a line (non-conflicting)
  404. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  405. "3", "4topic");
  406. // change first line (conflicting)
  407. writeFileAndCommit(FILE1,
  408. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  409. "3", "4topic");
  410. // change second line (not conflicting)
  411. writeFileAndCommit(FILE1, "change file1 in topic again", "1topic",
  412. "2topic", "3", "4topic");
  413. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  414. assertEquals(Status.STOPPED, res.getStatus());
  415. // continue should throw a meaningful exception
  416. try {
  417. res = git.rebase().setOperation(Operation.CONTINUE).call();
  418. fail("Expected Exception not thrown");
  419. } catch (UnmergedPathsException e) {
  420. // expected
  421. }
  422. // merge the file; the second topic commit should go through
  423. writeFileAndAdd(FILE1, "1topic", "2", "3", "4topic");
  424. res = git.rebase().setOperation(Operation.CONTINUE).call();
  425. assertNotNull(res);
  426. assertEquals(Status.OK, res.getStatus());
  427. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  428. ObjectId headId = db.resolve(Constants.HEAD);
  429. RevWalk rw = new RevWalk(db);
  430. RevCommit rc = rw.parseCommit(headId);
  431. RevCommit parent = rw.parseCommit(rc.getParent(0));
  432. assertEquals("change file1 in topic\n\nThis is conflicting", parent
  433. .getFullMessage());
  434. }
  435. @Test
  436. public void testStopOnConflictAndContinueWithNoDeltaToMaster()
  437. throws Exception {
  438. // create file1 on master
  439. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  440. "2", "3");
  441. // change in master
  442. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  443. checkFile(FILE1, "1master", "2", "3");
  444. // create a topic branch based on the first commit
  445. createBranch(firstInMaster, "refs/heads/topic");
  446. checkoutBranch("refs/heads/topic");
  447. // we have the old content again
  448. checkFile(FILE1, "1", "2", "3");
  449. // change first line (conflicting)
  450. writeFileAndCommit(FILE1,
  451. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  452. "3", "4topic");
  453. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  454. assertEquals(Status.STOPPED, res.getStatus());
  455. // continue should throw a meaningful exception
  456. try {
  457. res = git.rebase().setOperation(Operation.CONTINUE).call();
  458. fail("Expected Exception not thrown");
  459. } catch (UnmergedPathsException e) {
  460. // expected
  461. }
  462. // merge the file; the second topic commit should go through
  463. writeFileAndAdd(FILE1, "1master", "2", "3");
  464. res = git.rebase().setOperation(Operation.CONTINUE).call();
  465. assertNotNull(res);
  466. assertEquals(Status.NOTHING_TO_COMMIT, res.getStatus());
  467. assertEquals(RepositoryState.REBASING_INTERACTIVE,
  468. db.getRepositoryState());
  469. git.rebase().setOperation(Operation.SKIP).call();
  470. ObjectId headId = db.resolve(Constants.HEAD);
  471. RevWalk rw = new RevWalk(db);
  472. RevCommit rc = rw.parseCommit(headId);
  473. assertEquals("change file1 in master", rc.getFullMessage());
  474. }
  475. @Test
  476. public void testStopOnConflictAndFailContinueIfFileIsDirty()
  477. throws Exception {
  478. // create file1 on master
  479. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  480. "2", "3");
  481. // change in master
  482. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  483. checkFile(FILE1, "1master", "2", "3");
  484. // create a topic branch based on the first commit
  485. createBranch(firstInMaster, "refs/heads/topic");
  486. checkoutBranch("refs/heads/topic");
  487. // we have the old content again
  488. checkFile(FILE1, "1", "2", "3");
  489. // add a line (non-conflicting)
  490. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  491. "3", "4topic");
  492. // change first line (conflicting)
  493. writeFileAndCommit(FILE1,
  494. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  495. "3", "4topic");
  496. // change second line (not conflicting)
  497. writeFileAndCommit(FILE1, "change file1 in topic again", "1topic",
  498. "2topic", "3", "4topic");
  499. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  500. assertEquals(Status.STOPPED, res.getStatus());
  501. git.add().addFilepattern(FILE1).call();
  502. File trashFile = writeTrashFile(FILE1, "Some local change");
  503. res = git.rebase().setOperation(Operation.CONTINUE).call();
  504. assertNotNull(res);
  505. assertEquals(Status.STOPPED, res.getStatus());
  506. checkFile(trashFile, "Some local change");
  507. }
  508. @Test
  509. public void testStopOnLastConflictAndContinue() throws Exception {
  510. // create file1 on master
  511. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  512. "2", "3");
  513. // change in master
  514. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  515. checkFile(FILE1, "1master", "2", "3");
  516. // create a topic branch based on the first commit
  517. createBranch(firstInMaster, "refs/heads/topic");
  518. checkoutBranch("refs/heads/topic");
  519. // we have the old content again
  520. checkFile(FILE1, "1", "2", "3");
  521. // add a line (non-conflicting)
  522. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  523. "3", "4topic");
  524. // change first line (conflicting)
  525. writeFileAndCommit(FILE1,
  526. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  527. "3", "4topic");
  528. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  529. assertEquals(Status.STOPPED, res.getStatus());
  530. // merge the file; the second topic commit should go through
  531. writeFileAndAdd(FILE1, "1topic", "2", "3", "4topic");
  532. res = git.rebase().setOperation(Operation.CONTINUE).call();
  533. assertNotNull(res);
  534. assertEquals(Status.OK, res.getStatus());
  535. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  536. }
  537. @Test
  538. public void testStopOnLastConflictAndSkip() throws Exception {
  539. // create file1 on master
  540. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  541. "2", "3");
  542. // change in master
  543. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  544. checkFile(FILE1, "1master", "2", "3");
  545. // create a topic branch based on the first commit
  546. createBranch(firstInMaster, "refs/heads/topic");
  547. checkoutBranch("refs/heads/topic");
  548. // we have the old content again
  549. checkFile(FILE1, "1", "2", "3");
  550. // add a line (non-conflicting)
  551. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  552. "3", "4topic");
  553. // change first line (conflicting)
  554. writeFileAndCommit(FILE1,
  555. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  556. "3", "4topic");
  557. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  558. assertEquals(Status.STOPPED, res.getStatus());
  559. // merge the file; the second topic commit should go through
  560. writeFileAndAdd(FILE1, "1topic", "2", "3", "4topic");
  561. res = git.rebase().setOperation(Operation.SKIP).call();
  562. assertNotNull(res);
  563. assertEquals(Status.OK, res.getStatus());
  564. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  565. }
  566. @Test
  567. public void testMergeFirstStopOnLastConflictAndSkip() throws Exception {
  568. // create file1 on master
  569. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  570. "2", "3");
  571. // change in master
  572. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  573. checkFile(FILE1, "1master", "2", "3");
  574. // create a topic branch based on the first commit
  575. createBranch(firstInMaster, "refs/heads/topic");
  576. checkoutBranch("refs/heads/topic");
  577. // we have the old content again
  578. checkFile(FILE1, "1", "2", "3");
  579. // add a line (conflicting)
  580. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1topic",
  581. "2", "3", "4topic");
  582. // change first line (conflicting again)
  583. writeFileAndCommit(FILE1,
  584. "change file1 in topic\n\nThis is conflicting", "1topicagain",
  585. "2", "3", "4topic");
  586. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  587. assertEquals(Status.STOPPED, res.getStatus());
  588. writeFileAndAdd(FILE1, "merged");
  589. res = git.rebase().setOperation(Operation.CONTINUE).call();
  590. assertEquals(Status.STOPPED, res.getStatus());
  591. res = git.rebase().setOperation(Operation.SKIP).call();
  592. assertNotNull(res);
  593. assertEquals(Status.OK, res.getStatus());
  594. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  595. checkFile(FILE1, "merged");
  596. }
  597. @Test
  598. public void testStopOnConflictAndSkipNoConflict() throws Exception {
  599. // create file1 on master
  600. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  601. "2", "3");
  602. // change in master
  603. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  604. checkFile(FILE1, "1master", "2", "3");
  605. // create a topic branch based on the first commit
  606. createBranch(firstInMaster, "refs/heads/topic");
  607. checkoutBranch("refs/heads/topic");
  608. // we have the old content again
  609. checkFile(FILE1, "1", "2", "3");
  610. // add a line (non-conflicting)
  611. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  612. "3", "4topic");
  613. // change first line (conflicting)
  614. writeFileAndCommit(FILE1,
  615. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  616. "3", "4topic");
  617. // change third line (not conflicting)
  618. writeFileAndCommit(FILE1, "change file1 in topic again", "1topic", "2",
  619. "3topic", "4topic");
  620. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  621. assertEquals(Status.STOPPED, res.getStatus());
  622. res = git.rebase().setOperation(Operation.SKIP).call();
  623. checkFile(FILE1, "1master", "2", "3topic", "4topic");
  624. assertEquals(Status.OK, res.getStatus());
  625. }
  626. @Test
  627. public void testStopOnConflictAndSkipWithConflict() throws Exception {
  628. // create file1 on master
  629. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  630. "2", "3", "4");
  631. // change in master
  632. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2",
  633. "3master", "4");
  634. checkFile(FILE1, "1master", "2", "3master", "4");
  635. // create a topic branch based on the first commit
  636. createBranch(firstInMaster, "refs/heads/topic");
  637. checkoutBranch("refs/heads/topic");
  638. // we have the old content again
  639. checkFile(FILE1, "1", "2", "3", "4");
  640. // add a line (non-conflicting)
  641. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  642. "3", "4", "5topic");
  643. // change first line (conflicting)
  644. writeFileAndCommit(FILE1,
  645. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  646. "3", "4", "5topic");
  647. // change third line (conflicting)
  648. writeFileAndCommit(FILE1, "change file1 in topic again", "1topic", "2",
  649. "3topic", "4", "5topic");
  650. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  651. assertEquals(Status.STOPPED, res.getStatus());
  652. res = git.rebase().setOperation(Operation.SKIP).call();
  653. // TODO is this correct? It is what the command line returns
  654. checkFile(FILE1,
  655. "1master\n2\n<<<<<<< OURS\n3master\n=======\n3topic\n>>>>>>> THEIRS\n4\n5topic");
  656. assertEquals(Status.STOPPED, res.getStatus());
  657. }
  658. @Test
  659. public void testStopOnConflictCommitAndContinue() throws Exception {
  660. // create file1 on master
  661. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  662. "2", "3");
  663. // change in master
  664. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  665. checkFile(FILE1, "1master", "2", "3");
  666. // create a topic branch based on the first commit
  667. createBranch(firstInMaster, "refs/heads/topic");
  668. checkoutBranch("refs/heads/topic");
  669. // we have the old content again
  670. checkFile(FILE1, "1", "2", "3");
  671. // add a line (non-conflicting)
  672. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  673. "3", "4topic");
  674. // change first line (conflicting)
  675. writeFileAndCommit(FILE1,
  676. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  677. "3", "4topic");
  678. // change second line (not conflicting)
  679. writeFileAndCommit(FILE1, "change file1 in topic again", "1topic", "2",
  680. "3topic", "4topic");
  681. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  682. assertEquals(Status.STOPPED, res.getStatus());
  683. // continue should throw a meaningful exception
  684. try {
  685. res = git.rebase().setOperation(Operation.CONTINUE).call();
  686. fail("Expected Exception not thrown");
  687. } catch (UnmergedPathsException e) {
  688. // expected
  689. }
  690. // merge the file; the second topic commit should go through
  691. writeFileAndCommit(FILE1, "A different commit message", "1topic", "2",
  692. "3", "4topic");
  693. res = git.rebase().setOperation(Operation.CONTINUE).call();
  694. assertNotNull(res);
  695. // nothing to commit. this leaves the repo state in rebase, so that the
  696. // user can decide what to do. if he accidentally committed, reset soft,
  697. // and continue, if he really has nothing to commit, skip.
  698. assertEquals(Status.NOTHING_TO_COMMIT, res.getStatus());
  699. assertEquals(RepositoryState.REBASING_INTERACTIVE,
  700. db.getRepositoryState());
  701. git.rebase().setOperation(Operation.SKIP).call();
  702. ObjectId headId = db.resolve(Constants.HEAD);
  703. RevWalk rw = new RevWalk(db);
  704. RevCommit rc = rw.parseCommit(headId);
  705. RevCommit parent = rw.parseCommit(rc.getParent(0));
  706. assertEquals("A different commit message", parent.getFullMessage());
  707. }
  708. private RevCommit writeFileAndCommit(String fileName, String commitMessage,
  709. String... lines) throws Exception {
  710. StringBuilder sb = new StringBuilder();
  711. for (String line : lines) {
  712. sb.append(line);
  713. sb.append('\n');
  714. }
  715. writeTrashFile(fileName, sb.toString());
  716. git.add().addFilepattern(fileName).call();
  717. return git.commit().setMessage(commitMessage).call();
  718. }
  719. private void writeFileAndAdd(String fileName, String... lines)
  720. throws Exception {
  721. StringBuilder sb = new StringBuilder();
  722. for (String line : lines) {
  723. sb.append(line);
  724. sb.append('\n');
  725. }
  726. writeTrashFile(fileName, sb.toString());
  727. git.add().addFilepattern(fileName).call();
  728. }
  729. private void checkFile(String fileName, String... lines) throws Exception {
  730. File file = new File(db.getWorkTree(), fileName);
  731. StringBuilder sb = new StringBuilder();
  732. for (String line : lines) {
  733. sb.append(line);
  734. sb.append('\n');
  735. }
  736. checkFile(file, sb.toString());
  737. }
  738. @Test
  739. public void testStopOnConflictFileCreationAndDeletion() throws Exception {
  740. // create file1 on master
  741. writeTrashFile(FILE1, "Hello World");
  742. git.add().addFilepattern(FILE1).call();
  743. // create file2 on master
  744. File file2 = writeTrashFile("file2", "Hello World 2");
  745. git.add().addFilepattern("file2").call();
  746. // create file3 on master
  747. File file3 = writeTrashFile("file3", "Hello World 3");
  748. git.add().addFilepattern("file3").call();
  749. RevCommit firstInMaster = git.commit()
  750. .setMessage("Add file 1, 2 and 3").call();
  751. // create file4 on master
  752. File file4 = writeTrashFile("file4", "Hello World 4");
  753. git.add().addFilepattern("file4").call();
  754. deleteTrashFile("file2");
  755. git.add().setUpdate(true).addFilepattern("file2").call();
  756. // create folder folder6 on topic (conflicts with file folder6 on topic
  757. // later on)
  758. writeTrashFile("folder6/file1", "Hello World folder6");
  759. git.add().addFilepattern("folder6/file1").call();
  760. git.commit().setMessage(
  761. "Add file 4 and folder folder6, delete file2 on master").call();
  762. // create a topic branch based on second commit
  763. createBranch(firstInMaster, "refs/heads/topic");
  764. checkoutBranch("refs/heads/topic");
  765. deleteTrashFile("file3");
  766. git.add().setUpdate(true).addFilepattern("file3").call();
  767. // create file5 on topic
  768. File file5 = writeTrashFile("file5", "Hello World 5");
  769. git.add().addFilepattern("file5").call();
  770. git.commit().setMessage("Delete file3 and add file5 in topic").call();
  771. // create file folder6 on topic (conflicts with folder6 on master)
  772. writeTrashFile("folder6", "Hello World 6");
  773. git.add().addFilepattern("folder6").call();
  774. // create file7 on topic
  775. File file7 = writeTrashFile("file7", "Hello World 7");
  776. git.add().addFilepattern("file7").call();
  777. deleteTrashFile("file5");
  778. git.add().setUpdate(true).addFilepattern("file5").call();
  779. RevCommit conflicting = git.commit().setMessage(
  780. "Delete file5, add file folder6 and file7 in topic").call();
  781. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  782. assertEquals(Status.STOPPED, res.getStatus());
  783. assertEquals(conflicting, res.getCurrentCommit());
  784. assertEquals(RepositoryState.REBASING_INTERACTIVE, db
  785. .getRepositoryState());
  786. assertTrue(new File(db.getDirectory(), "rebase-merge").exists());
  787. // the first one should be included, so we should have left two picks in
  788. // the file
  789. assertEquals(0, countPicks());
  790. assertFalse(file2.exists());
  791. assertFalse(file3.exists());
  792. assertTrue(file4.exists());
  793. assertFalse(file5.exists());
  794. assertTrue(file7.exists());
  795. // abort should reset to topic branch
  796. res = git.rebase().setOperation(Operation.ABORT).call();
  797. assertEquals(res.getStatus(), Status.ABORTED);
  798. assertEquals("refs/heads/topic", db.getFullBranch());
  799. RevWalk rw = new RevWalk(db);
  800. assertEquals(conflicting, rw.parseCommit(db.resolve(Constants.HEAD)));
  801. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  802. // rebase- dir in .git must be deleted
  803. assertFalse(new File(db.getDirectory(), "rebase-merge").exists());
  804. assertTrue(file2.exists());
  805. assertFalse(file3.exists());
  806. assertFalse(file4.exists());
  807. assertFalse(file5.exists());
  808. assertTrue(file7.exists());
  809. }
  810. @Test
  811. public void testAuthorScriptConverter() throws Exception {
  812. // -1 h timezone offset
  813. PersonIdent ident = new PersonIdent("Author name", "a.mail@some.com",
  814. 123456789123L, -60);
  815. String convertedAuthor = git.rebase().toAuthorScript(ident);
  816. String[] lines = convertedAuthor.split("\n");
  817. assertEquals("GIT_AUTHOR_NAME='Author name'", lines[0]);
  818. assertEquals("GIT_AUTHOR_EMAIL='a.mail@some.com'", lines[1]);
  819. assertEquals("GIT_AUTHOR_DATE='123456789 -0100'", lines[2]);
  820. PersonIdent parsedIdent = git.rebase().parseAuthor(
  821. convertedAuthor.getBytes("UTF-8"));
  822. assertEquals(ident.getName(), parsedIdent.getName());
  823. assertEquals(ident.getEmailAddress(), parsedIdent.getEmailAddress());
  824. // this is rounded to the last second
  825. assertEquals(123456789000L, parsedIdent.getWhen().getTime());
  826. assertEquals(ident.getTimeZoneOffset(), parsedIdent.getTimeZoneOffset());
  827. // + 9.5h timezone offset
  828. ident = new PersonIdent("Author name", "a.mail@some.com",
  829. 123456789123L, +570);
  830. convertedAuthor = git.rebase().toAuthorScript(ident);
  831. lines = convertedAuthor.split("\n");
  832. assertEquals("GIT_AUTHOR_NAME='Author name'", lines[0]);
  833. assertEquals("GIT_AUTHOR_EMAIL='a.mail@some.com'", lines[1]);
  834. assertEquals("GIT_AUTHOR_DATE='123456789 +0930'", lines[2]);
  835. parsedIdent = git.rebase().parseAuthor(
  836. convertedAuthor.getBytes("UTF-8"));
  837. assertEquals(ident.getName(), parsedIdent.getName());
  838. assertEquals(ident.getEmailAddress(), parsedIdent.getEmailAddress());
  839. assertEquals(123456789000L, parsedIdent.getWhen().getTime());
  840. assertEquals(ident.getTimeZoneOffset(), parsedIdent.getTimeZoneOffset());
  841. }
  842. @Test
  843. public void testRepositoryStateChecks() throws Exception {
  844. try {
  845. git.rebase().setOperation(Operation.ABORT).call();
  846. fail("Expected Exception not thrown");
  847. } catch (WrongRepositoryStateException e) {
  848. // expected
  849. }
  850. try {
  851. git.rebase().setOperation(Operation.SKIP).call();
  852. fail("Expected Exception not thrown");
  853. } catch (WrongRepositoryStateException e) {
  854. // expected
  855. }
  856. try {
  857. git.rebase().setOperation(Operation.CONTINUE).call();
  858. fail("Expected Exception not thrown");
  859. } catch (WrongRepositoryStateException e) {
  860. // expected
  861. }
  862. }
  863. @Test
  864. public void testRebaseWithUntrackedFile() throws Exception {
  865. // create file1, add and commit
  866. writeTrashFile(FILE1, "file1");
  867. git.add().addFilepattern(FILE1).call();
  868. RevCommit commit = git.commit().setMessage("commit1").call();
  869. // create topic branch and checkout / create file2, add and commit
  870. createBranch(commit, "refs/heads/topic");
  871. checkoutBranch("refs/heads/topic");
  872. writeTrashFile("file2", "file2");
  873. git.add().addFilepattern("file2").call();
  874. git.commit().setMessage("commit2").call();
  875. // checkout master branch / modify file1, add and commit
  876. checkoutBranch("refs/heads/master");
  877. writeTrashFile(FILE1, "modified file1");
  878. git.add().addFilepattern(FILE1).call();
  879. git.commit().setMessage("commit3").call();
  880. // checkout topic branch / create untracked file3
  881. checkoutBranch("refs/heads/topic");
  882. writeTrashFile("file3", "untracked file3");
  883. // rebase
  884. assertEquals(Status.OK, git.rebase().setUpstream("refs/heads/master")
  885. .call().getStatus());
  886. }
  887. @Test
  888. @SuppressWarnings("null")
  889. public void testRebaseWithUnstagedTopicChange() throws Exception {
  890. // create file1, add and commit
  891. writeTrashFile(FILE1, "file1");
  892. git.add().addFilepattern(FILE1).call();
  893. RevCommit commit = git.commit().setMessage("commit1").call();
  894. // create topic branch and checkout / create file2, add and commit
  895. createBranch(commit, "refs/heads/topic");
  896. checkoutBranch("refs/heads/topic");
  897. writeTrashFile("file2", "file2");
  898. git.add().addFilepattern("file2").call();
  899. git.commit().setMessage("commit2").call();
  900. // checkout master branch / modify file1, add and commit
  901. checkoutBranch("refs/heads/master");
  902. writeTrashFile(FILE1, "modified file1");
  903. git.add().addFilepattern(FILE1).call();
  904. git.commit().setMessage("commit3").call();
  905. // checkout topic branch / modify file2
  906. checkoutBranch("refs/heads/topic");
  907. writeTrashFile("file2", "unstaged file2");
  908. // rebase
  909. JGitInternalException exception = null;
  910. try {
  911. git.rebase().setUpstream("refs/heads/master").call();
  912. } catch (JGitInternalException e) {
  913. exception = e;
  914. }
  915. assertNotNull(exception);
  916. assertEquals("Checkout conflict with files: \nfile2",
  917. exception.getMessage());
  918. }
  919. @Test
  920. @SuppressWarnings("null")
  921. public void testRebaseWithUncommittedTopicChange() throws Exception {
  922. // create file1, add and commit
  923. writeTrashFile(FILE1, "file1");
  924. git.add().addFilepattern(FILE1).call();
  925. RevCommit commit = git.commit().setMessage("commit1").call();
  926. // create topic branch and checkout / create file2, add and commit
  927. createBranch(commit, "refs/heads/topic");
  928. checkoutBranch("refs/heads/topic");
  929. writeTrashFile("file2", "file2");
  930. git.add().addFilepattern("file2").call();
  931. git.commit().setMessage("commit2").call();
  932. // checkout master branch / modify file1, add and commit
  933. checkoutBranch("refs/heads/master");
  934. writeTrashFile(FILE1, "modified file1");
  935. git.add().addFilepattern(FILE1).call();
  936. git.commit().setMessage("commit3").call();
  937. // checkout topic branch / modify file2 and add
  938. checkoutBranch("refs/heads/topic");
  939. File uncommittedFile = writeTrashFile("file2", "uncommitted file2");
  940. git.add().addFilepattern("file2").call();
  941. // do not commit
  942. // rebase
  943. JGitInternalException exception = null;
  944. try {
  945. git.rebase().setUpstream("refs/heads/master").call();
  946. } catch (JGitInternalException e) {
  947. exception = e;
  948. }
  949. assertNotNull(exception);
  950. assertEquals("Checkout conflict with files: \nfile2",
  951. exception.getMessage());
  952. checkFile(uncommittedFile, "uncommitted file2");
  953. assertEquals(RepositoryState.SAFE, git.getRepository().getRepositoryState());
  954. }
  955. @Test
  956. @SuppressWarnings("null")
  957. public void testRebaseWithUnstagedMasterChange() throws Exception {
  958. // create file1, add and commit
  959. writeTrashFile(FILE1, "file1");
  960. git.add().addFilepattern(FILE1).call();
  961. RevCommit commit = git.commit().setMessage("commit1").call();
  962. // create topic branch and checkout / create file2, add and commit
  963. createBranch(commit, "refs/heads/topic");
  964. checkoutBranch("refs/heads/topic");
  965. writeTrashFile("file2", "file2");
  966. git.add().addFilepattern("file2").call();
  967. git.commit().setMessage("commit2").call();
  968. // checkout master branch / modify file1, add and commit
  969. checkoutBranch("refs/heads/master");
  970. writeTrashFile(FILE1, "modified file1");
  971. git.add().addFilepattern(FILE1).call();
  972. git.commit().setMessage("commit3").call();
  973. // checkout topic branch / modify file1
  974. checkoutBranch("refs/heads/topic");
  975. writeTrashFile(FILE1, "unstaged modified file1");
  976. // rebase
  977. JGitInternalException exception = null;
  978. try {
  979. git.rebase().setUpstream("refs/heads/master").call();
  980. } catch (JGitInternalException e) {
  981. exception = e;
  982. }
  983. assertNotNull(exception);
  984. assertEquals("Checkout conflict with files: \nfile1",
  985. exception.getMessage());
  986. }
  987. @Test
  988. @SuppressWarnings("null")
  989. public void testRebaseWithUncommittedMasterChange() throws Exception {
  990. // create file1, add and commit
  991. writeTrashFile(FILE1, "file1");
  992. git.add().addFilepattern(FILE1).call();
  993. RevCommit commit = git.commit().setMessage("commit1").call();
  994. // create topic branch and checkout / create file2, add and commit
  995. createBranch(commit, "refs/heads/topic");
  996. checkoutBranch("refs/heads/topic");
  997. writeTrashFile("file2", "file2");
  998. git.add().addFilepattern("file2").call();
  999. git.commit().setMessage("commit2").call();
  1000. // checkout master branch / modify file1, add and commit
  1001. checkoutBranch("refs/heads/master");
  1002. writeTrashFile(FILE1, "modified file1");
  1003. git.add().addFilepattern(FILE1).call();
  1004. git.commit().setMessage("commit3").call();
  1005. // checkout topic branch / modify file1 and add
  1006. checkoutBranch("refs/heads/topic");
  1007. writeTrashFile(FILE1, "uncommitted modified file1");
  1008. git.add().addFilepattern(FILE1).call();
  1009. // do not commit
  1010. // rebase
  1011. JGitInternalException exception = null;
  1012. try {
  1013. git.rebase().setUpstream("refs/heads/master").call();
  1014. } catch (JGitInternalException e) {
  1015. exception = e;
  1016. }
  1017. assertNotNull(exception);
  1018. assertEquals("Checkout conflict with files: \nfile1",
  1019. exception.getMessage());
  1020. }
  1021. @Test
  1022. public void testRebaseWithUnstagedMasterChangeBaseCommit() throws Exception {
  1023. // create file0 + file1, add and commit
  1024. writeTrashFile("file0", "file0");
  1025. writeTrashFile(FILE1, "file1");
  1026. git.add().addFilepattern("file0").addFilepattern(FILE1).call();
  1027. RevCommit commit = git.commit().setMessage("commit1").call();
  1028. // create topic branch and checkout / create file2, add and commit
  1029. createBranch(commit, "refs/heads/topic");
  1030. checkoutBranch("refs/heads/topic");
  1031. writeTrashFile("file2", "file2");
  1032. git.add().addFilepattern("file2").call();
  1033. git.commit().setMessage("commit2").call();
  1034. // checkout master branch / modify file1, add and commit
  1035. checkoutBranch("refs/heads/master");
  1036. writeTrashFile(FILE1, "modified file1");
  1037. git.add().addFilepattern(FILE1).call();
  1038. git.commit().setMessage("commit3").call();
  1039. // checkout topic branch / modify file0
  1040. checkoutBranch("refs/heads/topic");
  1041. writeTrashFile("file0", "unstaged modified file0");
  1042. // rebase
  1043. assertEquals(Status.OK, git.rebase().setUpstream("refs/heads/master")
  1044. .call().getStatus());
  1045. }
  1046. @Test
  1047. public void testRebaseWithUncommittedMasterChangeBaseCommit()
  1048. throws Exception {
  1049. // create file0 + file1, add and commit
  1050. File file0 = writeTrashFile("file0", "file0");
  1051. writeTrashFile(FILE1, "file1");
  1052. git.add().addFilepattern("file0").addFilepattern(FILE1).call();
  1053. RevCommit commit = git.commit().setMessage("commit1").call();
  1054. // create topic branch and checkout / create file2, add and commit
  1055. createBranch(commit, "refs/heads/topic");
  1056. checkoutBranch("refs/heads/topic");
  1057. writeTrashFile("file2", "file2");
  1058. git.add().addFilepattern("file2").call();
  1059. git.commit().setMessage("commit2").call();
  1060. // checkout master branch / modify file1, add and commit
  1061. checkoutBranch("refs/heads/master");
  1062. writeTrashFile(FILE1, "modified file1");
  1063. git.add().addFilepattern(FILE1).call();
  1064. git.commit().setMessage("commit3").call();
  1065. // checkout topic branch / modify file0 and add
  1066. checkoutBranch("refs/heads/topic");
  1067. write(file0, "unstaged modified file0");
  1068. git.add().addFilepattern("file0").call();
  1069. // do not commit
  1070. // get current index state
  1071. String indexState = indexState(CONTENT);
  1072. // rebase
  1073. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1074. .call();
  1075. assertEquals(Status.FAILED, result.getStatus());
  1076. // staged file0 causes DIRTY_INDEX
  1077. assertEquals(1, result.getFailingPaths().size());
  1078. assertEquals(MergeFailureReason.DIRTY_INDEX, result.getFailingPaths()
  1079. .get("file0"));
  1080. assertEquals("unstaged modified file0", read(file0));
  1081. // index shall be unchanged
  1082. assertEquals(indexState, indexState(CONTENT));
  1083. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  1084. }
  1085. @Test
  1086. public void testRebaseWithUnstagedMasterChangeOtherCommit()
  1087. throws Exception {
  1088. // create file0, add and commit
  1089. writeTrashFile("file0", "file0");
  1090. git.add().addFilepattern("file0").call();
  1091. git.commit().setMessage("commit0").call();
  1092. // create file1, add and commit
  1093. writeTrashFile(FILE1, "file1");
  1094. git.add().addFilepattern(FILE1).call();
  1095. RevCommit commit = git.commit().setMessage("commit1").call();
  1096. // create topic branch and checkout / create file2, add and commit
  1097. createBranch(commit, "refs/heads/topic");
  1098. checkoutBranch("refs/heads/topic");
  1099. writeTrashFile("file2", "file2");
  1100. git.add().addFilepattern("file2").call();
  1101. git.commit().setMessage("commit2").call();
  1102. // checkout master branch / modify file1, add and commit
  1103. checkoutBranch("refs/heads/master");
  1104. writeTrashFile(FILE1, "modified file1");
  1105. git.add().addFilepattern(FILE1).call();
  1106. git.commit().setMessage("commit3").call();
  1107. // checkout topic branch / modify file0
  1108. checkoutBranch("refs/heads/topic");
  1109. writeTrashFile("file0", "unstaged modified file0");
  1110. // rebase
  1111. assertEquals(Status.OK, git.rebase().setUpstream("refs/heads/master")
  1112. .call().getStatus());
  1113. }
  1114. @Test
  1115. public void testRebaseWithUncommittedMasterChangeOtherCommit()
  1116. throws Exception {
  1117. // create file0, add and commit
  1118. File file0 = writeTrashFile("file0", "file0");
  1119. git.add().addFilepattern("file0").call();
  1120. git.commit().setMessage("commit0").call();
  1121. // create file1, add and commit
  1122. writeTrashFile(FILE1, "file1");
  1123. git.add().addFilepattern(FILE1).call();
  1124. RevCommit commit = git.commit().setMessage("commit1").call();
  1125. // create topic branch and checkout / create file2, add and commit
  1126. createBranch(commit, "refs/heads/topic");
  1127. checkoutBranch("refs/heads/topic");
  1128. writeTrashFile("file2", "file2");
  1129. git.add().addFilepattern("file2").call();
  1130. git.commit().setMessage("commit2").call();
  1131. // checkout master branch / modify file1, add and commit
  1132. checkoutBranch("refs/heads/master");
  1133. writeTrashFile(FILE1, "modified file1");
  1134. git.add().addFilepattern(FILE1).call();
  1135. git.commit().setMessage("commit3").call();
  1136. // checkout topic branch / modify file0 and add
  1137. checkoutBranch("refs/heads/topic");
  1138. write(file0, "unstaged modified file0");
  1139. git.add().addFilepattern("file0").call();
  1140. // do not commit
  1141. // get current index state
  1142. String indexState = indexState(CONTENT);
  1143. // rebase
  1144. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1145. .call();
  1146. assertEquals(Status.FAILED, result.getStatus());
  1147. // staged file0 causes DIRTY_INDEX
  1148. assertEquals(1, result.getFailingPaths().size());
  1149. assertEquals(MergeFailureReason.DIRTY_INDEX, result.getFailingPaths()
  1150. .get("file0"));
  1151. assertEquals("unstaged modified file0", read(file0));
  1152. // index shall be unchanged
  1153. assertEquals(indexState, indexState(CONTENT));
  1154. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  1155. }
  1156. private int countPicks() throws IOException {
  1157. int count = 0;
  1158. File todoFile = new File(db.getDirectory(),
  1159. "rebase-merge/git-rebase-todo");
  1160. BufferedReader br = new BufferedReader(new InputStreamReader(
  1161. new FileInputStream(todoFile), "UTF-8"));
  1162. try {
  1163. String line = br.readLine();
  1164. while (line != null) {
  1165. String actionToken = line.substring(0, line.indexOf(' '));
  1166. Action action = null;
  1167. try {
  1168. action = Action.parse(actionToken);
  1169. } catch (Exception e) {
  1170. // ignore
  1171. }
  1172. if (action != null)
  1173. count++;
  1174. line = br.readLine();
  1175. }
  1176. return count;
  1177. } finally {
  1178. br.close();
  1179. }
  1180. }
  1181. @Test
  1182. public void testFastForwardWithMultipleCommitsOnDifferentBranches()
  1183. throws Exception {
  1184. // create file1 on master
  1185. writeTrashFile(FILE1, FILE1);
  1186. git.add().addFilepattern(FILE1).call();
  1187. RevCommit first = git.commit().setMessage("Add file1").call();
  1188. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1189. // create a topic branch
  1190. createBranch(first, "refs/heads/topic");
  1191. // create file2 on master
  1192. writeTrashFile("file2", "file2");
  1193. git.add().addFilepattern("file2").call();
  1194. RevCommit second = git.commit().setMessage("Add file2").call();
  1195. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1196. // create side branch
  1197. createBranch(second, "refs/heads/side");
  1198. // update FILE1 on master
  1199. writeTrashFile(FILE1, "blah");
  1200. git.add().addFilepattern(FILE1).call();
  1201. git.commit().setMessage("updated file1 on master")
  1202. .call();
  1203. // switch to side branch and update file2
  1204. checkoutBranch("refs/heads/side");
  1205. writeTrashFile("file2", "more change");
  1206. git.add().addFilepattern("file2").call();
  1207. RevCommit fourth = git.commit().setMessage("update file2 on side")
  1208. .call();
  1209. // switch back to master and merge in side
  1210. checkoutBranch("refs/heads/master");
  1211. MergeResult result = git.merge().include(fourth.getId())
  1212. .setStrategy(MergeStrategy.RESOLVE).call();
  1213. assertEquals(MergeStatus.MERGED, result.getMergeStatus());
  1214. // switch back to topic branch and rebase it onto master
  1215. checkoutBranch("refs/heads/topic");
  1216. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  1217. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1218. checkFile(new File(db.getWorkTree(), "file2"), "more change");
  1219. assertEquals(Status.FAST_FORWARD, res.getStatus());
  1220. }
  1221. @Test
  1222. public void testRebaseShouldLeaveWorkspaceUntouchedWithUnstagedChangesConflict()
  1223. throws Exception {
  1224. writeTrashFile(FILE1, "initial file");
  1225. git.add().addFilepattern(FILE1).call();
  1226. RevCommit initial = git.commit().setMessage("initial commit").call();
  1227. createBranch(initial, "refs/heads/side");
  1228. writeTrashFile(FILE1, "updated file");
  1229. git.add().addFilepattern(FILE1).call();
  1230. git.commit().setMessage("updated FILE1 on master").call();
  1231. // switch to side, modify the file
  1232. checkoutBranch("refs/heads/side");
  1233. writeTrashFile(FILE1, "side update");
  1234. git.add().addFilepattern(FILE1).call();
  1235. git.commit().setMessage("updated FILE1 on side").call();
  1236. File theFile = writeTrashFile(FILE1, "dirty the file");
  1237. // and attempt to rebase
  1238. try {
  1239. RebaseResult rebaseResult = git.rebase()
  1240. .setUpstream("refs/heads/master").call();
  1241. fail("Checkout with conflict should have occured, not "
  1242. + rebaseResult.getStatus());
  1243. } catch (JGitInternalException e) {
  1244. checkFile(theFile, "dirty the file");
  1245. }
  1246. assertEquals(RepositoryState.SAFE, git.getRepository()
  1247. .getRepositoryState());
  1248. }
  1249. }