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

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