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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716
  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.CoreMatchers.equalTo;
  45. import static org.hamcrest.CoreMatchers.not;
  46. import static org.hamcrest.MatcherAssert.assertThat;
  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.InteractiveHandler;
  61. import org.eclipse.jgit.api.RebaseCommand.Operation;
  62. import org.eclipse.jgit.api.RebaseResult.Status;
  63. import org.eclipse.jgit.api.errors.InvalidRebaseStepException;
  64. import org.eclipse.jgit.api.errors.JGitInternalException;
  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.AbbreviatedObjectId;
  71. import org.eclipse.jgit.lib.Constants;
  72. import org.eclipse.jgit.lib.ObjectId;
  73. import org.eclipse.jgit.lib.PersonIdent;
  74. import org.eclipse.jgit.lib.RebaseTodoLine;
  75. import org.eclipse.jgit.lib.RebaseTodoLine.Action;
  76. import org.eclipse.jgit.lib.RefUpdate;
  77. import org.eclipse.jgit.lib.ReflogEntry;
  78. import org.eclipse.jgit.lib.RepositoryState;
  79. import org.eclipse.jgit.merge.MergeStrategy;
  80. import org.eclipse.jgit.revwalk.RevCommit;
  81. import org.eclipse.jgit.revwalk.RevWalk;
  82. import org.eclipse.jgit.util.FileUtils;
  83. import org.eclipse.jgit.util.IO;
  84. import org.eclipse.jgit.util.RawParseUtils;
  85. import org.junit.Before;
  86. import org.junit.Test;
  87. public class RebaseCommandTest extends RepositoryTestCase {
  88. private static final String GIT_REBASE_TODO = "rebase-merge/git-rebase-todo";
  89. private static final String FILE1 = "file1";
  90. protected Git git;
  91. @Override
  92. @Before
  93. public void setUp() throws Exception {
  94. super.setUp();
  95. this.git = new Git(db);
  96. }
  97. private void checkoutCommit(RevCommit commit) throws IllegalStateException,
  98. IOException {
  99. RevWalk walk = new RevWalk(db);
  100. RevCommit head = walk.parseCommit(db.resolve(Constants.HEAD));
  101. DirCacheCheckout dco = new DirCacheCheckout(db, head.getTree(), db
  102. .lockDirCache(), commit.getTree());
  103. dco.setFailOnConflict(true);
  104. dco.checkout();
  105. walk.release();
  106. // update the HEAD
  107. RefUpdate refUpdate = db.updateRef(Constants.HEAD, true);
  108. refUpdate.setNewObjectId(commit);
  109. refUpdate.setRefLogMessage("checkout: moving to " + head.getName(),
  110. false);
  111. refUpdate.forceUpdate();
  112. }
  113. @Test
  114. public void testFastForwardWithNewFile() throws Exception {
  115. // create file1 on master
  116. writeTrashFile(FILE1, FILE1);
  117. git.add().addFilepattern(FILE1).call();
  118. RevCommit first = git.commit().setMessage("Add file1").call();
  119. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  120. // create a topic branch
  121. createBranch(first, "refs/heads/topic");
  122. // create file2 on master
  123. File file2 = writeTrashFile("file2", "file2");
  124. git.add().addFilepattern("file2").call();
  125. RevCommit second = git.commit().setMessage("Add file2").call();
  126. assertTrue(new File(db.getWorkTree(), "file2").exists());
  127. checkoutBranch("refs/heads/topic");
  128. assertFalse(new File(db.getWorkTree(), "file2").exists());
  129. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  130. assertTrue(new File(db.getWorkTree(), "file2").exists());
  131. checkFile(file2, "file2");
  132. assertEquals(Status.FAST_FORWARD, res.getStatus());
  133. List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
  134. .getReverseEntries();
  135. List<ReflogEntry> topicLog = db.getReflogReader("refs/heads/topic")
  136. .getReverseEntries();
  137. List<ReflogEntry> masterLog = db.getReflogReader("refs/heads/master")
  138. .getReverseEntries();
  139. assertEquals("rebase finished: returning to refs/heads/topic", headLog
  140. .get(0).getComment());
  141. assertEquals("checkout: moving from topic to " + second.getName(),
  142. headLog.get(1).getComment());
  143. assertEquals(2, masterLog.size());
  144. assertEquals(2, topicLog.size());
  145. assertEquals(
  146. "rebase finished: refs/heads/topic onto " + second.getName(),
  147. topicLog.get(0).getComment());
  148. }
  149. @Test
  150. public void testFastForwardWithMultipleCommits() throws Exception {
  151. // create file1 on master
  152. writeTrashFile(FILE1, FILE1);
  153. git.add().addFilepattern(FILE1).call();
  154. RevCommit first = git.commit().setMessage("Add file1").call();
  155. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  156. // create a topic branch
  157. createBranch(first, "refs/heads/topic");
  158. // create file2 on master
  159. File file2 = writeTrashFile("file2", "file2");
  160. git.add().addFilepattern("file2").call();
  161. git.commit().setMessage("Add file2").call();
  162. assertTrue(new File(db.getWorkTree(), "file2").exists());
  163. // write a second commit
  164. writeTrashFile("file2", "file2 new content");
  165. git.add().addFilepattern("file2").call();
  166. RevCommit second = git.commit().setMessage("Change content of file2")
  167. .call();
  168. checkoutBranch("refs/heads/topic");
  169. assertFalse(new File(db.getWorkTree(), "file2").exists());
  170. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  171. assertTrue(new File(db.getWorkTree(), "file2").exists());
  172. checkFile(file2, "file2 new content");
  173. assertEquals(Status.FAST_FORWARD, res.getStatus());
  174. List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
  175. .getReverseEntries();
  176. List<ReflogEntry> topicLog = db.getReflogReader("refs/heads/topic")
  177. .getReverseEntries();
  178. List<ReflogEntry> masterLog = db.getReflogReader("refs/heads/master")
  179. .getReverseEntries();
  180. assertEquals("rebase finished: returning to refs/heads/topic", headLog
  181. .get(0).getComment());
  182. assertEquals("checkout: moving from topic to " + second.getName(),
  183. headLog.get(1).getComment());
  184. assertEquals(3, masterLog.size());
  185. assertEquals(2, topicLog.size());
  186. assertEquals(
  187. "rebase finished: refs/heads/topic onto " + second.getName(),
  188. topicLog.get(0).getComment());
  189. }
  190. /**
  191. * Create the following commits and then attempt to rebase topic onto
  192. * master. This will serialize the branches.
  193. *
  194. * <pre>
  195. * A - B (master)
  196. * \
  197. * C - D - F (topic)
  198. * \ /
  199. * E - (side)
  200. * </pre>
  201. *
  202. * into
  203. *
  204. * <pre>
  205. * A - B - (master) C' - D' - E' (topic')
  206. * \
  207. * C - D - F (topic)
  208. * \ /
  209. * E - (side)
  210. * </pre>
  211. *
  212. * @throws Exception
  213. */
  214. @Test
  215. public void testRebaseShouldIgnoreMergeCommits()
  216. throws Exception {
  217. // create file1 on master
  218. writeTrashFile(FILE1, FILE1);
  219. git.add().addFilepattern(FILE1).call();
  220. RevCommit a = git.commit().setMessage("Add file1").call();
  221. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  222. // create a topic branch
  223. createBranch(a, "refs/heads/topic");
  224. // update FILE1 on master
  225. writeTrashFile(FILE1, "blah");
  226. git.add().addFilepattern(FILE1).call();
  227. RevCommit b = git.commit().setMessage("updated file1 on master").call();
  228. checkoutBranch("refs/heads/topic");
  229. writeTrashFile("file3", "more changess");
  230. git.add().addFilepattern("file3").call();
  231. RevCommit c = git.commit()
  232. .setMessage("update file3 on topic").call();
  233. // create a branch from the topic commit
  234. createBranch(c, "refs/heads/side");
  235. // second commit on topic
  236. writeTrashFile("file2", "file2");
  237. git.add().addFilepattern("file2").call();
  238. RevCommit d = git.commit().setMessage("Add file2").call();
  239. assertTrue(new File(db.getWorkTree(), "file2").exists());
  240. // switch to side branch and update file2
  241. checkoutBranch("refs/heads/side");
  242. writeTrashFile("file3", "more change");
  243. git.add().addFilepattern("file3").call();
  244. RevCommit e = git.commit().setMessage("update file2 on side")
  245. .call();
  246. // switch back to topic and merge in side, creating f
  247. checkoutBranch("refs/heads/topic");
  248. MergeResult result = git.merge().include(e.getId())
  249. .setStrategy(MergeStrategy.RESOLVE).call();
  250. assertEquals(MergeStatus.MERGED, result.getMergeStatus());
  251. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  252. assertEquals(Status.OK, res.getStatus());
  253. RevWalk rw = new RevWalk(db);
  254. rw.markStart(rw.parseCommit(db.resolve("refs/heads/topic")));
  255. assertDerivedFrom(rw.next(), e);
  256. assertDerivedFrom(rw.next(), d);
  257. assertDerivedFrom(rw.next(), c);
  258. assertEquals(b, rw.next());
  259. assertEquals(a, rw.next());
  260. List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
  261. .getReverseEntries();
  262. List<ReflogEntry> sideLog = db.getReflogReader("refs/heads/side")
  263. .getReverseEntries();
  264. List<ReflogEntry> topicLog = db.getReflogReader("refs/heads/topic")
  265. .getReverseEntries();
  266. List<ReflogEntry> masterLog = db.getReflogReader("refs/heads/master")
  267. .getReverseEntries();
  268. assertEquals("rebase finished: returning to refs/heads/topic", headLog
  269. .get(0).getComment());
  270. assertEquals("rebase: update file2 on side", headLog.get(1)
  271. .getComment());
  272. assertEquals("rebase: Add file2", headLog.get(2).getComment());
  273. assertEquals("rebase: update file3 on topic", headLog.get(3)
  274. .getComment());
  275. assertEquals("checkout: moving from topic to " + b.getName(), headLog
  276. .get(4).getComment());
  277. assertEquals(2, masterLog.size());
  278. assertEquals(2, sideLog.size());
  279. assertEquals(5, topicLog.size());
  280. assertEquals("rebase finished: refs/heads/topic onto " + b.getName(),
  281. topicLog.get(0).getComment());
  282. }
  283. static void assertDerivedFrom(RevCommit derived, RevCommit original) {
  284. assertThat(derived, not(equalTo(original)));
  285. assertEquals(original.getFullMessage(), derived.getFullMessage());
  286. }
  287. @Test
  288. public void testRebaseParentOntoHeadShouldBeUptoDate() throws Exception {
  289. writeTrashFile(FILE1, FILE1);
  290. git.add().addFilepattern(FILE1).call();
  291. RevCommit parent = git.commit().setMessage("parent comment").call();
  292. writeTrashFile(FILE1, "another change");
  293. git.add().addFilepattern(FILE1).call();
  294. git.commit().setMessage("head commit").call();
  295. RebaseResult result = git.rebase().setUpstream(parent).call();
  296. assertEquals(Status.UP_TO_DATE, result.getStatus());
  297. assertEquals(2, db.getReflogReader(Constants.HEAD).getReverseEntries()
  298. .size());
  299. assertEquals(2, db.getReflogReader("refs/heads/master")
  300. .getReverseEntries().size());
  301. }
  302. @Test
  303. public void testUpToDate() throws Exception {
  304. // create file1 on master
  305. writeTrashFile(FILE1, FILE1);
  306. git.add().addFilepattern(FILE1).call();
  307. RevCommit first = git.commit().setMessage("Add file1").call();
  308. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  309. RebaseResult res = git.rebase().setUpstream(first).call();
  310. assertEquals(Status.UP_TO_DATE, res.getStatus());
  311. assertEquals(1, db.getReflogReader(Constants.HEAD).getReverseEntries()
  312. .size());
  313. assertEquals(1, db.getReflogReader("refs/heads/master")
  314. .getReverseEntries().size());
  315. }
  316. @Test
  317. public void testUnknownUpstream() throws Exception {
  318. // create file1 on master
  319. writeTrashFile(FILE1, FILE1);
  320. git.add().addFilepattern(FILE1).call();
  321. git.commit().setMessage("Add file1").call();
  322. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  323. try {
  324. git.rebase().setUpstream("refs/heads/xyz").call();
  325. fail("expected exception was not thrown");
  326. } catch (RefNotFoundException e) {
  327. // expected exception
  328. }
  329. }
  330. @Test
  331. public void testConflictFreeWithSingleFile() throws Exception {
  332. // create file1 on master
  333. File theFile = writeTrashFile(FILE1, "1\n2\n3\n");
  334. git.add().addFilepattern(FILE1).call();
  335. RevCommit second = git.commit().setMessage("Add file1").call();
  336. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  337. // change first line in master and commit
  338. writeTrashFile(FILE1, "1master\n2\n3\n");
  339. checkFile(theFile, "1master\n2\n3\n");
  340. git.add().addFilepattern(FILE1).call();
  341. RevCommit lastMasterChange = git.commit().setMessage(
  342. "change file1 in master").call();
  343. // create a topic branch based on second commit
  344. createBranch(second, "refs/heads/topic");
  345. checkoutBranch("refs/heads/topic");
  346. // we have the old content again
  347. checkFile(theFile, "1\n2\n3\n");
  348. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  349. // change third line in topic branch
  350. writeTrashFile(FILE1, "1\n2\n3\ntopic\n");
  351. git.add().addFilepattern(FILE1).call();
  352. RevCommit origHead = git.commit().setMessage("change file1 in topic")
  353. .call();
  354. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  355. assertEquals(Status.OK, res.getStatus());
  356. checkFile(theFile, "1master\n2\n3\ntopic\n");
  357. // our old branch should be checked out again
  358. assertEquals("refs/heads/topic", db.getFullBranch());
  359. assertEquals(lastMasterChange, new RevWalk(db).parseCommit(
  360. db.resolve(Constants.HEAD)).getParent(0));
  361. assertEquals(origHead, db.readOrigHead());
  362. List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
  363. .getReverseEntries();
  364. List<ReflogEntry> topicLog = db.getReflogReader("refs/heads/topic")
  365. .getReverseEntries();
  366. List<ReflogEntry> masterLog = db.getReflogReader("refs/heads/master")
  367. .getReverseEntries();
  368. assertEquals(2, masterLog.size());
  369. assertEquals(3, topicLog.size());
  370. assertEquals("rebase finished: refs/heads/topic onto "
  371. + lastMasterChange.getName(), topicLog.get(0).getComment());
  372. assertEquals("rebase finished: returning to refs/heads/topic", headLog
  373. .get(0).getComment());
  374. }
  375. @Test
  376. public void testDetachedHead() throws Exception {
  377. // create file1 on master
  378. File theFile = writeTrashFile(FILE1, "1\n2\n3\n");
  379. git.add().addFilepattern(FILE1).call();
  380. RevCommit second = git.commit().setMessage("Add file1").call();
  381. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  382. // change first line in master and commit
  383. writeTrashFile(FILE1, "1master\n2\n3\n");
  384. checkFile(theFile, "1master\n2\n3\n");
  385. git.add().addFilepattern(FILE1).call();
  386. RevCommit lastMasterChange = git.commit().setMessage(
  387. "change file1 in master").call();
  388. // create a topic branch based on second commit
  389. createBranch(second, "refs/heads/topic");
  390. checkoutBranch("refs/heads/topic");
  391. // we have the old content again
  392. checkFile(theFile, "1\n2\n3\n");
  393. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  394. // change third line in topic branch
  395. writeTrashFile(FILE1, "1\n2\n3\ntopic\n");
  396. git.add().addFilepattern(FILE1).call();
  397. RevCommit topicCommit = git.commit()
  398. .setMessage("change file1 in topic").call();
  399. checkoutBranch("refs/heads/master");
  400. checkoutCommit(topicCommit);
  401. assertEquals(topicCommit.getId().getName(), db.getFullBranch());
  402. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  403. assertEquals(Status.OK, res.getStatus());
  404. checkFile(theFile, "1master\n2\n3\ntopic\n");
  405. assertEquals(lastMasterChange, new RevWalk(db).parseCommit(
  406. db.resolve(Constants.HEAD)).getParent(0));
  407. List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
  408. .getReverseEntries();
  409. assertEquals(8, headLog.size());
  410. assertEquals("rebase: change file1 in topic", headLog.get(0)
  411. .getComment());
  412. assertEquals("checkout: moving from " + topicCommit.getName() + " to "
  413. + lastMasterChange.getName(), headLog.get(1).getComment());
  414. }
  415. @Test
  416. public void testFilesAddedFromTwoBranches() throws Exception {
  417. // create file1 on master
  418. writeTrashFile(FILE1, FILE1);
  419. git.add().addFilepattern(FILE1).call();
  420. RevCommit masterCommit = git.commit().setMessage("Add file1 to master")
  421. .call();
  422. // create a branch named file2 and add file2
  423. createBranch(masterCommit, "refs/heads/file2");
  424. checkoutBranch("refs/heads/file2");
  425. writeTrashFile("file2", "file2");
  426. git.add().addFilepattern("file2").call();
  427. RevCommit addFile2 = git.commit().setMessage(
  428. "Add file2 to branch file2").call();
  429. // create a branch named file3 and add file3
  430. createBranch(masterCommit, "refs/heads/file3");
  431. checkoutBranch("refs/heads/file3");
  432. writeTrashFile("file3", "file3");
  433. git.add().addFilepattern("file3").call();
  434. git.commit().setMessage("Add file3 to branch file3").call();
  435. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  436. assertFalse(new File(db.getWorkTree(), "file2").exists());
  437. assertTrue(new File(db.getWorkTree(), "file3").exists());
  438. RebaseResult res = git.rebase().setUpstream("refs/heads/file2").call();
  439. assertEquals(Status.OK, res.getStatus());
  440. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  441. assertTrue(new File(db.getWorkTree(), "file2").exists());
  442. assertTrue(new File(db.getWorkTree(), "file3").exists());
  443. // our old branch should be checked out again
  444. assertEquals("refs/heads/file3", db.getFullBranch());
  445. assertEquals(addFile2, new RevWalk(db).parseCommit(
  446. db.resolve(Constants.HEAD)).getParent(0));
  447. checkoutBranch("refs/heads/file2");
  448. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  449. assertTrue(new File(db.getWorkTree(), "file2").exists());
  450. assertFalse(new File(db.getWorkTree(), "file3").exists());
  451. }
  452. @Test
  453. public void testStopOnConflict() throws Exception {
  454. // create file1 on master
  455. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  456. "2", "3");
  457. // change first line in master
  458. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  459. checkFile(FILE1, "1master", "2", "3");
  460. // create a topic branch based on second commit
  461. createBranch(firstInMaster, "refs/heads/topic");
  462. checkoutBranch("refs/heads/topic");
  463. // we have the old content again
  464. checkFile(FILE1, "1", "2", "3");
  465. // add a line (non-conflicting)
  466. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  467. "3", "topic4");
  468. // change first line (conflicting)
  469. RevCommit conflicting = writeFileAndCommit(FILE1,
  470. "change file1 in topic", "1topic", "2", "3", "topic4");
  471. RevCommit lastTopicCommit = writeFileAndCommit(FILE1,
  472. "change file1 in topic again", "1topic", "2", "3", "topic4");
  473. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  474. assertEquals(Status.STOPPED, res.getStatus());
  475. assertEquals(conflicting, res.getCurrentCommit());
  476. checkFile(FILE1,
  477. "<<<<<<< Upstream, based on master\n1master\n=======\n1topic",
  478. ">>>>>>> e0d1dea change file1 in topic\n2\n3\ntopic4");
  479. assertEquals(RepositoryState.REBASING_INTERACTIVE, db
  480. .getRepositoryState());
  481. assertTrue(new File(db.getDirectory(), "rebase-merge").exists());
  482. // the first one should be included, so we should have left two picks in
  483. // the file
  484. assertEquals(1, countPicks());
  485. // rebase should not succeed in this state
  486. try {
  487. git.rebase().setUpstream("refs/heads/master").call();
  488. fail("Expected exception was not thrown");
  489. } catch (WrongRepositoryStateException e) {
  490. // expected
  491. }
  492. // abort should reset to topic branch
  493. res = git.rebase().setOperation(Operation.ABORT).call();
  494. assertEquals(res.getStatus(), Status.ABORTED);
  495. assertEquals("refs/heads/topic", db.getFullBranch());
  496. checkFile(FILE1, "1topic", "2", "3", "topic4");
  497. RevWalk rw = new RevWalk(db);
  498. assertEquals(lastTopicCommit, rw
  499. .parseCommit(db.resolve(Constants.HEAD)));
  500. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  501. // rebase- dir in .git must be deleted
  502. assertFalse(new File(db.getDirectory(), "rebase-merge").exists());
  503. }
  504. @Test
  505. public void testStopOnConflictAndContinue() throws Exception {
  506. // create file1 on master
  507. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  508. "2", "3");
  509. // change in master
  510. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  511. checkFile(FILE1, "1master", "2", "3");
  512. // create a topic branch based on the first commit
  513. createBranch(firstInMaster, "refs/heads/topic");
  514. checkoutBranch("refs/heads/topic");
  515. // we have the old content again
  516. checkFile(FILE1, "1", "2", "3");
  517. // add a line (non-conflicting)
  518. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  519. "3", "4topic");
  520. // change first line (conflicting)
  521. writeFileAndCommit(FILE1,
  522. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  523. "3", "4topic");
  524. // change second line (not conflicting)
  525. writeFileAndCommit(FILE1, "change file1 in topic again", "1topic",
  526. "2topic", "3", "4topic");
  527. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  528. assertEquals(Status.STOPPED, res.getStatus());
  529. // continue should throw a meaningful exception
  530. try {
  531. res = git.rebase().setOperation(Operation.CONTINUE).call();
  532. fail("Expected Exception not thrown");
  533. } catch (UnmergedPathsException e) {
  534. // expected
  535. }
  536. // merge the file; the second topic commit should go through
  537. writeFileAndAdd(FILE1, "1topic", "2", "3", "4topic");
  538. res = git.rebase().setOperation(Operation.CONTINUE).call();
  539. assertNotNull(res);
  540. assertEquals(Status.OK, res.getStatus());
  541. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  542. ObjectId headId = db.resolve(Constants.HEAD);
  543. RevWalk rw = new RevWalk(db);
  544. RevCommit rc = rw.parseCommit(headId);
  545. RevCommit parent = rw.parseCommit(rc.getParent(0));
  546. assertEquals("change file1 in topic\n\nThis is conflicting", parent
  547. .getFullMessage());
  548. }
  549. @Test
  550. public void testStopOnConflictAndContinueWithNoDeltaToMaster()
  551. throws Exception {
  552. // create file1 on master
  553. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  554. "2", "3");
  555. // change in master
  556. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  557. checkFile(FILE1, "1master", "2", "3");
  558. // create a topic branch based on the first commit
  559. createBranch(firstInMaster, "refs/heads/topic");
  560. checkoutBranch("refs/heads/topic");
  561. // we have the old content again
  562. checkFile(FILE1, "1", "2", "3");
  563. // change first line (conflicting)
  564. writeFileAndCommit(FILE1,
  565. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  566. "3", "4topic");
  567. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  568. assertEquals(Status.STOPPED, res.getStatus());
  569. // continue should throw a meaningful exception
  570. try {
  571. res = git.rebase().setOperation(Operation.CONTINUE).call();
  572. fail("Expected Exception not thrown");
  573. } catch (UnmergedPathsException e) {
  574. // expected
  575. }
  576. // merge the file; the second topic commit should go through
  577. writeFileAndAdd(FILE1, "1master", "2", "3");
  578. res = git.rebase().setOperation(Operation.CONTINUE).call();
  579. assertNotNull(res);
  580. assertEquals(Status.NOTHING_TO_COMMIT, res.getStatus());
  581. assertEquals(RepositoryState.REBASING_INTERACTIVE,
  582. db.getRepositoryState());
  583. git.rebase().setOperation(Operation.SKIP).call();
  584. ObjectId headId = db.resolve(Constants.HEAD);
  585. RevWalk rw = new RevWalk(db);
  586. RevCommit rc = rw.parseCommit(headId);
  587. assertEquals("change file1 in master", rc.getFullMessage());
  588. }
  589. @Test
  590. public void testStopOnConflictAndFailContinueIfFileIsDirty()
  591. throws Exception {
  592. // create file1 on master
  593. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  594. "2", "3");
  595. // change in master
  596. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  597. checkFile(FILE1, "1master", "2", "3");
  598. // create a topic branch based on the first commit
  599. createBranch(firstInMaster, "refs/heads/topic");
  600. checkoutBranch("refs/heads/topic");
  601. // we have the old content again
  602. checkFile(FILE1, "1", "2", "3");
  603. // add a line (non-conflicting)
  604. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  605. "3", "4topic");
  606. // change first line (conflicting)
  607. writeFileAndCommit(FILE1,
  608. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  609. "3", "4topic");
  610. // change second line (not conflicting)
  611. writeFileAndCommit(FILE1, "change file1 in topic again", "1topic",
  612. "2topic", "3", "4topic");
  613. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  614. assertEquals(Status.STOPPED, res.getStatus());
  615. git.add().addFilepattern(FILE1).call();
  616. File trashFile = writeTrashFile(FILE1, "Some local change");
  617. res = git.rebase().setOperation(Operation.CONTINUE).call();
  618. assertNotNull(res);
  619. assertEquals(Status.STOPPED, res.getStatus());
  620. checkFile(trashFile, "Some local change");
  621. }
  622. @Test
  623. public void testStopOnLastConflictAndContinue() throws Exception {
  624. // create file1 on master
  625. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  626. "2", "3");
  627. // change in master
  628. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  629. checkFile(FILE1, "1master", "2", "3");
  630. // create a topic branch based on the first commit
  631. createBranch(firstInMaster, "refs/heads/topic");
  632. checkoutBranch("refs/heads/topic");
  633. // we have the old content again
  634. checkFile(FILE1, "1", "2", "3");
  635. // add a line (non-conflicting)
  636. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  637. "3", "4topic");
  638. // change first line (conflicting)
  639. writeFileAndCommit(FILE1,
  640. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  641. "3", "4topic");
  642. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  643. assertEquals(Status.STOPPED, res.getStatus());
  644. // merge the file; the second topic commit should go through
  645. writeFileAndAdd(FILE1, "1topic", "2", "3", "4topic");
  646. res = git.rebase().setOperation(Operation.CONTINUE).call();
  647. assertNotNull(res);
  648. assertEquals(Status.OK, res.getStatus());
  649. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  650. }
  651. @Test
  652. public void testStopOnLastConflictAndSkip() throws Exception {
  653. // create file1 on master
  654. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  655. "2", "3");
  656. // change in master
  657. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  658. checkFile(FILE1, "1master", "2", "3");
  659. // create a topic branch based on the first commit
  660. createBranch(firstInMaster, "refs/heads/topic");
  661. checkoutBranch("refs/heads/topic");
  662. // we have the old content again
  663. checkFile(FILE1, "1", "2", "3");
  664. // add a line (non-conflicting)
  665. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  666. "3", "4topic");
  667. // change first line (conflicting)
  668. writeFileAndCommit(FILE1,
  669. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  670. "3", "4topic");
  671. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  672. assertEquals(Status.STOPPED, res.getStatus());
  673. // merge the file; the second topic commit should go through
  674. writeFileAndAdd(FILE1, "1topic", "2", "3", "4topic");
  675. res = git.rebase().setOperation(Operation.SKIP).call();
  676. assertNotNull(res);
  677. assertEquals(Status.OK, res.getStatus());
  678. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  679. }
  680. @Test
  681. public void testMergeFirstStopOnLastConflictAndSkip() throws Exception {
  682. // create file1 on master
  683. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  684. "2", "3");
  685. // change in master
  686. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  687. checkFile(FILE1, "1master", "2", "3");
  688. // create a topic branch based on the first commit
  689. createBranch(firstInMaster, "refs/heads/topic");
  690. checkoutBranch("refs/heads/topic");
  691. // we have the old content again
  692. checkFile(FILE1, "1", "2", "3");
  693. // add a line (conflicting)
  694. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1topic",
  695. "2", "3", "4topic");
  696. // change first line (conflicting again)
  697. writeFileAndCommit(FILE1,
  698. "change file1 in topic\n\nThis is conflicting", "1topicagain",
  699. "2", "3", "4topic");
  700. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  701. assertEquals(Status.STOPPED, res.getStatus());
  702. writeFileAndAdd(FILE1, "merged");
  703. res = git.rebase().setOperation(Operation.CONTINUE).call();
  704. assertEquals(Status.STOPPED, res.getStatus());
  705. res = git.rebase().setOperation(Operation.SKIP).call();
  706. assertNotNull(res);
  707. assertEquals(Status.OK, res.getStatus());
  708. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  709. checkFile(FILE1, "merged");
  710. }
  711. @Test
  712. public void testStopOnConflictAndSkipNoConflict() throws Exception {
  713. // create file1 on master
  714. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  715. "2", "3");
  716. // change in master
  717. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  718. checkFile(FILE1, "1master", "2", "3");
  719. // create a topic branch based on the first commit
  720. createBranch(firstInMaster, "refs/heads/topic");
  721. checkoutBranch("refs/heads/topic");
  722. // we have the old content again
  723. checkFile(FILE1, "1", "2", "3");
  724. // add a line (non-conflicting)
  725. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  726. "3", "4topic");
  727. // change first line (conflicting)
  728. writeFileAndCommit(FILE1,
  729. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  730. "3", "4topic");
  731. // change third line (not conflicting)
  732. writeFileAndCommit(FILE1, "change file1 in topic again", "1topic", "2",
  733. "3topic", "4topic");
  734. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  735. assertEquals(Status.STOPPED, res.getStatus());
  736. res = git.rebase().setOperation(Operation.SKIP).call();
  737. checkFile(FILE1, "1master", "2", "3topic", "4topic");
  738. assertEquals(Status.OK, res.getStatus());
  739. }
  740. @Test
  741. public void testStopOnConflictAndSkipWithConflict() throws Exception {
  742. // create file1 on master
  743. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  744. "2", "3", "4");
  745. // change in master
  746. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2",
  747. "3master", "4");
  748. checkFile(FILE1, "1master", "2", "3master", "4");
  749. // create a topic branch based on the first commit
  750. createBranch(firstInMaster, "refs/heads/topic");
  751. checkoutBranch("refs/heads/topic");
  752. // we have the old content again
  753. checkFile(FILE1, "1", "2", "3", "4");
  754. // add a line (non-conflicting)
  755. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  756. "3", "4", "5topic");
  757. // change first line (conflicting)
  758. writeFileAndCommit(FILE1,
  759. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  760. "3", "4", "5topic");
  761. // change third line (conflicting)
  762. writeFileAndCommit(FILE1, "change file1 in topic again", "1topic", "2",
  763. "3topic", "4", "5topic");
  764. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  765. assertEquals(Status.STOPPED, res.getStatus());
  766. res = git.rebase().setOperation(Operation.SKIP).call();
  767. // TODO is this correct? It is what the command line returns
  768. checkFile(
  769. FILE1,
  770. "1master\n2\n<<<<<<< Upstream, based on master\n3master\n=======\n3topic",
  771. ">>>>>>> 5afc8df change file1 in topic again\n4\n5topic");
  772. assertEquals(Status.STOPPED, res.getStatus());
  773. }
  774. @Test
  775. public void testStopOnConflictCommitAndContinue() throws Exception {
  776. // create file1 on master
  777. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  778. "2", "3");
  779. // change in master
  780. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  781. checkFile(FILE1, "1master", "2", "3");
  782. // create a topic branch based on the first commit
  783. createBranch(firstInMaster, "refs/heads/topic");
  784. checkoutBranch("refs/heads/topic");
  785. // we have the old content again
  786. checkFile(FILE1, "1", "2", "3");
  787. // add a line (non-conflicting)
  788. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  789. "3", "4topic");
  790. // change first line (conflicting)
  791. writeFileAndCommit(FILE1,
  792. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  793. "3", "4topic");
  794. // change second line (not conflicting)
  795. writeFileAndCommit(FILE1, "change file1 in topic again", "1topic", "2",
  796. "3topic", "4topic");
  797. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  798. assertEquals(Status.STOPPED, res.getStatus());
  799. // continue should throw a meaningful exception
  800. try {
  801. res = git.rebase().setOperation(Operation.CONTINUE).call();
  802. fail("Expected Exception not thrown");
  803. } catch (UnmergedPathsException e) {
  804. // expected
  805. }
  806. // merge the file; the second topic commit should go through
  807. writeFileAndCommit(FILE1, "A different commit message", "1topic", "2",
  808. "3", "4topic");
  809. res = git.rebase().setOperation(Operation.CONTINUE).call();
  810. assertNotNull(res);
  811. // nothing to commit. this leaves the repo state in rebase, so that the
  812. // user can decide what to do. if he accidentally committed, reset soft,
  813. // and continue, if he really has nothing to commit, skip.
  814. assertEquals(Status.NOTHING_TO_COMMIT, res.getStatus());
  815. assertEquals(RepositoryState.REBASING_INTERACTIVE,
  816. db.getRepositoryState());
  817. git.rebase().setOperation(Operation.SKIP).call();
  818. ObjectId headId = db.resolve(Constants.HEAD);
  819. RevWalk rw = new RevWalk(db);
  820. RevCommit rc = rw.parseCommit(headId);
  821. RevCommit parent = rw.parseCommit(rc.getParent(0));
  822. assertEquals("A different commit message", parent.getFullMessage());
  823. }
  824. private RevCommit writeFileAndCommit(String fileName, String commitMessage,
  825. String... lines) throws Exception {
  826. StringBuilder sb = new StringBuilder();
  827. for (String line : lines) {
  828. sb.append(line);
  829. sb.append('\n');
  830. }
  831. writeTrashFile(fileName, sb.toString());
  832. git.add().addFilepattern(fileName).call();
  833. return git.commit().setMessage(commitMessage).call();
  834. }
  835. private void writeFileAndAdd(String fileName, String... lines)
  836. throws Exception {
  837. StringBuilder sb = new StringBuilder();
  838. for (String line : lines) {
  839. sb.append(line);
  840. sb.append('\n');
  841. }
  842. writeTrashFile(fileName, sb.toString());
  843. git.add().addFilepattern(fileName).call();
  844. }
  845. private void checkFile(String fileName, String... lines) throws Exception {
  846. File file = new File(db.getWorkTree(), fileName);
  847. StringBuilder sb = new StringBuilder();
  848. for (String line : lines) {
  849. sb.append(line);
  850. sb.append('\n');
  851. }
  852. checkFile(file, sb.toString());
  853. }
  854. @Test
  855. public void testStopOnConflictFileCreationAndDeletion() throws Exception {
  856. // create file1 on master
  857. writeTrashFile(FILE1, "Hello World");
  858. git.add().addFilepattern(FILE1).call();
  859. // create file2 on master
  860. File file2 = writeTrashFile("file2", "Hello World 2");
  861. git.add().addFilepattern("file2").call();
  862. // create file3 on master
  863. File file3 = writeTrashFile("file3", "Hello World 3");
  864. git.add().addFilepattern("file3").call();
  865. RevCommit firstInMaster = git.commit()
  866. .setMessage("Add file 1, 2 and 3").call();
  867. // create file4 on master
  868. File file4 = writeTrashFile("file4", "Hello World 4");
  869. git.add().addFilepattern("file4").call();
  870. deleteTrashFile("file2");
  871. git.add().setUpdate(true).addFilepattern("file2").call();
  872. // create folder folder6 on topic (conflicts with file folder6 on topic
  873. // later on)
  874. writeTrashFile("folder6/file1", "Hello World folder6");
  875. git.add().addFilepattern("folder6/file1").call();
  876. git.commit().setMessage(
  877. "Add file 4 and folder folder6, delete file2 on master").call();
  878. // create a topic branch based on second commit
  879. createBranch(firstInMaster, "refs/heads/topic");
  880. checkoutBranch("refs/heads/topic");
  881. deleteTrashFile("file3");
  882. git.add().setUpdate(true).addFilepattern("file3").call();
  883. // create file5 on topic
  884. File file5 = writeTrashFile("file5", "Hello World 5");
  885. git.add().addFilepattern("file5").call();
  886. git.commit().setMessage("Delete file3 and add file5 in topic").call();
  887. // create file folder6 on topic (conflicts with folder6 on master)
  888. writeTrashFile("folder6", "Hello World 6");
  889. git.add().addFilepattern("folder6").call();
  890. // create file7 on topic
  891. File file7 = writeTrashFile("file7", "Hello World 7");
  892. git.add().addFilepattern("file7").call();
  893. deleteTrashFile("file5");
  894. git.add().setUpdate(true).addFilepattern("file5").call();
  895. RevCommit conflicting = git.commit().setMessage(
  896. "Delete file5, add file folder6 and file7 in topic").call();
  897. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  898. assertEquals(Status.STOPPED, res.getStatus());
  899. assertEquals(conflicting, res.getCurrentCommit());
  900. assertEquals(RepositoryState.REBASING_INTERACTIVE, db
  901. .getRepositoryState());
  902. assertTrue(new File(db.getDirectory(), "rebase-merge").exists());
  903. // the first one should be included, so we should have left two picks in
  904. // the file
  905. assertEquals(0, countPicks());
  906. assertFalse(file2.exists());
  907. assertFalse(file3.exists());
  908. assertTrue(file4.exists());
  909. assertFalse(file5.exists());
  910. assertTrue(file7.exists());
  911. // abort should reset to topic branch
  912. res = git.rebase().setOperation(Operation.ABORT).call();
  913. assertEquals(res.getStatus(), Status.ABORTED);
  914. assertEquals("refs/heads/topic", db.getFullBranch());
  915. RevWalk rw = new RevWalk(db);
  916. assertEquals(conflicting, rw.parseCommit(db.resolve(Constants.HEAD)));
  917. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  918. // rebase- dir in .git must be deleted
  919. assertFalse(new File(db.getDirectory(), "rebase-merge").exists());
  920. assertTrue(file2.exists());
  921. assertFalse(file3.exists());
  922. assertFalse(file4.exists());
  923. assertFalse(file5.exists());
  924. assertTrue(file7.exists());
  925. }
  926. @Test
  927. public void testAuthorScriptConverter() throws Exception {
  928. // -1 h timezone offset
  929. PersonIdent ident = new PersonIdent("Author name", "a.mail@some.com",
  930. 123456789123L, -60);
  931. String convertedAuthor = git.rebase().toAuthorScript(ident);
  932. String[] lines = convertedAuthor.split("\n");
  933. assertEquals("GIT_AUTHOR_NAME='Author name'", lines[0]);
  934. assertEquals("GIT_AUTHOR_EMAIL='a.mail@some.com'", lines[1]);
  935. assertEquals("GIT_AUTHOR_DATE='@123456789 -0100'", lines[2]);
  936. PersonIdent parsedIdent = git.rebase().parseAuthor(
  937. convertedAuthor.getBytes("UTF-8"));
  938. assertEquals(ident.getName(), parsedIdent.getName());
  939. assertEquals(ident.getEmailAddress(), parsedIdent.getEmailAddress());
  940. // this is rounded to the last second
  941. assertEquals(123456789000L, parsedIdent.getWhen().getTime());
  942. assertEquals(ident.getTimeZoneOffset(), parsedIdent.getTimeZoneOffset());
  943. // + 9.5h timezone offset
  944. ident = new PersonIdent("Author name", "a.mail@some.com",
  945. 123456789123L, +570);
  946. convertedAuthor = git.rebase().toAuthorScript(ident);
  947. lines = convertedAuthor.split("\n");
  948. assertEquals("GIT_AUTHOR_NAME='Author name'", lines[0]);
  949. assertEquals("GIT_AUTHOR_EMAIL='a.mail@some.com'", lines[1]);
  950. assertEquals("GIT_AUTHOR_DATE='@123456789 +0930'", lines[2]);
  951. parsedIdent = git.rebase().parseAuthor(
  952. convertedAuthor.getBytes("UTF-8"));
  953. assertEquals(ident.getName(), parsedIdent.getName());
  954. assertEquals(ident.getEmailAddress(), parsedIdent.getEmailAddress());
  955. assertEquals(123456789000L, parsedIdent.getWhen().getTime());
  956. assertEquals(ident.getTimeZoneOffset(), parsedIdent.getTimeZoneOffset());
  957. }
  958. @Test
  959. public void testRepositoryStateChecks() throws Exception {
  960. try {
  961. git.rebase().setOperation(Operation.ABORT).call();
  962. fail("Expected Exception not thrown");
  963. } catch (WrongRepositoryStateException e) {
  964. // expected
  965. }
  966. try {
  967. git.rebase().setOperation(Operation.SKIP).call();
  968. fail("Expected Exception not thrown");
  969. } catch (WrongRepositoryStateException e) {
  970. // expected
  971. }
  972. try {
  973. git.rebase().setOperation(Operation.CONTINUE).call();
  974. fail("Expected Exception not thrown");
  975. } catch (WrongRepositoryStateException e) {
  976. // expected
  977. }
  978. }
  979. @Test
  980. public void testRebaseWithUntrackedFile() throws Exception {
  981. // create file1, add and commit
  982. writeTrashFile(FILE1, "file1");
  983. git.add().addFilepattern(FILE1).call();
  984. RevCommit commit = git.commit().setMessage("commit1").call();
  985. // create topic branch and checkout / create file2, add and commit
  986. createBranch(commit, "refs/heads/topic");
  987. checkoutBranch("refs/heads/topic");
  988. writeTrashFile("file2", "file2");
  989. git.add().addFilepattern("file2").call();
  990. git.commit().setMessage("commit2").call();
  991. // checkout master branch / modify file1, add and commit
  992. checkoutBranch("refs/heads/master");
  993. writeTrashFile(FILE1, "modified file1");
  994. git.add().addFilepattern(FILE1).call();
  995. git.commit().setMessage("commit3").call();
  996. // checkout topic branch / create untracked file3
  997. checkoutBranch("refs/heads/topic");
  998. writeTrashFile("file3", "untracked file3");
  999. // rebase
  1000. assertEquals(Status.OK, git.rebase().setUpstream("refs/heads/master")
  1001. .call().getStatus());
  1002. }
  1003. @Test
  1004. public void testRebaseWithUnstagedTopicChange() throws Exception {
  1005. // create file1, add and commit
  1006. writeTrashFile(FILE1, "file1");
  1007. git.add().addFilepattern(FILE1).call();
  1008. RevCommit commit = git.commit().setMessage("commit1").call();
  1009. // create topic branch and checkout / create file2, add and commit
  1010. createBranch(commit, "refs/heads/topic");
  1011. checkoutBranch("refs/heads/topic");
  1012. writeTrashFile("file2", "file2");
  1013. git.add().addFilepattern("file2").call();
  1014. git.commit().setMessage("commit2").call();
  1015. // checkout master branch / modify file1, add and commit
  1016. checkoutBranch("refs/heads/master");
  1017. writeTrashFile(FILE1, "modified file1");
  1018. git.add().addFilepattern(FILE1).call();
  1019. git.commit().setMessage("commit3").call();
  1020. // checkout topic branch / modify file2
  1021. checkoutBranch("refs/heads/topic");
  1022. writeTrashFile("file2", "unstaged file2");
  1023. // rebase
  1024. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1025. .call();
  1026. assertEquals(Status.UNCOMMITTED_CHANGES, result.getStatus());
  1027. assertEquals(1, result.getUncommittedChanges().size());
  1028. assertEquals("file2", result.getUncommittedChanges().get(0));
  1029. }
  1030. @Test
  1031. public void testRebaseWithUncommittedTopicChange() throws Exception {
  1032. // create file1, add and commit
  1033. writeTrashFile(FILE1, "file1");
  1034. git.add().addFilepattern(FILE1).call();
  1035. RevCommit commit = git.commit().setMessage("commit1").call();
  1036. // create topic branch and checkout / create file2, add and commit
  1037. createBranch(commit, "refs/heads/topic");
  1038. checkoutBranch("refs/heads/topic");
  1039. writeTrashFile("file2", "file2");
  1040. git.add().addFilepattern("file2").call();
  1041. git.commit().setMessage("commit2").call();
  1042. // checkout master branch / modify file1, add and commit
  1043. checkoutBranch("refs/heads/master");
  1044. writeTrashFile(FILE1, "modified file1");
  1045. git.add().addFilepattern(FILE1).call();
  1046. git.commit().setMessage("commit3").call();
  1047. // checkout topic branch / modify file2 and add
  1048. checkoutBranch("refs/heads/topic");
  1049. File uncommittedFile = writeTrashFile("file2", "uncommitted file2");
  1050. git.add().addFilepattern("file2").call();
  1051. // do not commit
  1052. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1053. .call();
  1054. assertEquals(Status.UNCOMMITTED_CHANGES, result.getStatus());
  1055. assertEquals(1, result.getUncommittedChanges().size());
  1056. assertEquals("file2", result.getUncommittedChanges().get(0));
  1057. checkFile(uncommittedFile, "uncommitted file2");
  1058. assertEquals(RepositoryState.SAFE, git.getRepository().getRepositoryState());
  1059. }
  1060. @Test
  1061. public void testRebaseWithUnstagedMasterChange() throws Exception {
  1062. // create file1, add and commit
  1063. writeTrashFile(FILE1, "file1");
  1064. git.add().addFilepattern(FILE1).call();
  1065. RevCommit commit = git.commit().setMessage("commit1").call();
  1066. // create topic branch and checkout / create file2, add and commit
  1067. createBranch(commit, "refs/heads/topic");
  1068. checkoutBranch("refs/heads/topic");
  1069. writeTrashFile("file2", "file2");
  1070. git.add().addFilepattern("file2").call();
  1071. git.commit().setMessage("commit2").call();
  1072. // checkout master branch / modify file1, add and commit
  1073. checkoutBranch("refs/heads/master");
  1074. writeTrashFile(FILE1, "modified file1");
  1075. git.add().addFilepattern(FILE1).call();
  1076. git.commit().setMessage("commit3").call();
  1077. // checkout topic branch / modify file1
  1078. checkoutBranch("refs/heads/topic");
  1079. writeTrashFile(FILE1, "unstaged modified file1");
  1080. // rebase
  1081. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1082. .call();
  1083. assertEquals(Status.UNCOMMITTED_CHANGES, result.getStatus());
  1084. assertEquals(1, result.getUncommittedChanges().size());
  1085. assertEquals(FILE1, result.getUncommittedChanges().get(0));
  1086. }
  1087. @Test
  1088. public void testRebaseWithUncommittedMasterChange() throws Exception {
  1089. // create file1, add and commit
  1090. writeTrashFile(FILE1, "file1");
  1091. git.add().addFilepattern(FILE1).call();
  1092. RevCommit commit = git.commit().setMessage("commit1").call();
  1093. // create topic branch and checkout / create file2, add and commit
  1094. createBranch(commit, "refs/heads/topic");
  1095. checkoutBranch("refs/heads/topic");
  1096. writeTrashFile("file2", "file2");
  1097. git.add().addFilepattern("file2").call();
  1098. git.commit().setMessage("commit2").call();
  1099. // checkout master branch / modify file1, add and commit
  1100. checkoutBranch("refs/heads/master");
  1101. writeTrashFile(FILE1, "modified file1");
  1102. git.add().addFilepattern(FILE1).call();
  1103. git.commit().setMessage("commit3").call();
  1104. // checkout topic branch / modify file1 and add
  1105. checkoutBranch("refs/heads/topic");
  1106. writeTrashFile(FILE1, "uncommitted modified file1");
  1107. git.add().addFilepattern(FILE1).call();
  1108. // do not commit
  1109. // rebase
  1110. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1111. .call();
  1112. assertEquals(Status.UNCOMMITTED_CHANGES, result.getStatus());
  1113. assertEquals(1, result.getUncommittedChanges().size());
  1114. assertEquals(FILE1, result.getUncommittedChanges().get(0));
  1115. }
  1116. @Test
  1117. public void testRebaseWithUnstagedMasterChangeBaseCommit() throws Exception {
  1118. // create file0 + file1, add and commit
  1119. writeTrashFile("file0", "file0");
  1120. writeTrashFile(FILE1, "file1");
  1121. git.add().addFilepattern("file0").addFilepattern(FILE1).call();
  1122. RevCommit commit = git.commit().setMessage("commit1").call();
  1123. // create topic branch and checkout / create file2, add and commit
  1124. createBranch(commit, "refs/heads/topic");
  1125. checkoutBranch("refs/heads/topic");
  1126. writeTrashFile("file2", "file2");
  1127. git.add().addFilepattern("file2").call();
  1128. git.commit().setMessage("commit2").call();
  1129. // checkout master branch / modify file1, add and commit
  1130. checkoutBranch("refs/heads/master");
  1131. writeTrashFile(FILE1, "modified file1");
  1132. git.add().addFilepattern(FILE1).call();
  1133. git.commit().setMessage("commit3").call();
  1134. // checkout topic branch / modify file0
  1135. checkoutBranch("refs/heads/topic");
  1136. writeTrashFile("file0", "unstaged modified file0");
  1137. // rebase
  1138. assertEquals(Status.UNCOMMITTED_CHANGES,
  1139. git.rebase().setUpstream("refs/heads/master")
  1140. .call().getStatus());
  1141. }
  1142. @Test
  1143. public void testRebaseWithUncommittedMasterChangeBaseCommit()
  1144. throws Exception {
  1145. // create file0 + file1, add and commit
  1146. File file0 = writeTrashFile("file0", "file0");
  1147. writeTrashFile(FILE1, "file1");
  1148. git.add().addFilepattern("file0").addFilepattern(FILE1).call();
  1149. RevCommit commit = git.commit().setMessage("commit1").call();
  1150. // create topic branch and checkout / create file2, add and commit
  1151. createBranch(commit, "refs/heads/topic");
  1152. checkoutBranch("refs/heads/topic");
  1153. writeTrashFile("file2", "file2");
  1154. git.add().addFilepattern("file2").call();
  1155. git.commit().setMessage("commit2").call();
  1156. // checkout master branch / modify file1, add and commit
  1157. checkoutBranch("refs/heads/master");
  1158. writeTrashFile(FILE1, "modified file1");
  1159. git.add().addFilepattern(FILE1).call();
  1160. git.commit().setMessage("commit3").call();
  1161. // checkout topic branch / modify file0 and add
  1162. checkoutBranch("refs/heads/topic");
  1163. write(file0, "unstaged modified file0");
  1164. git.add().addFilepattern("file0").call();
  1165. // do not commit
  1166. // get current index state
  1167. String indexState = indexState(CONTENT);
  1168. // rebase
  1169. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1170. .call();
  1171. assertEquals(Status.UNCOMMITTED_CHANGES, result.getStatus());
  1172. assertEquals(1, result.getUncommittedChanges().size());
  1173. // index shall be unchanged
  1174. assertEquals(indexState, indexState(CONTENT));
  1175. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  1176. }
  1177. @Test
  1178. public void testRebaseWithUnstagedMasterChangeOtherCommit()
  1179. throws Exception {
  1180. // create file0, add and commit
  1181. writeTrashFile("file0", "file0");
  1182. git.add().addFilepattern("file0").call();
  1183. git.commit().setMessage("commit0").call();
  1184. // create file1, add and commit
  1185. writeTrashFile(FILE1, "file1");
  1186. git.add().addFilepattern(FILE1).call();
  1187. RevCommit commit = git.commit().setMessage("commit1").call();
  1188. // create topic branch and checkout / create file2, add and commit
  1189. createBranch(commit, "refs/heads/topic");
  1190. checkoutBranch("refs/heads/topic");
  1191. writeTrashFile("file2", "file2");
  1192. git.add().addFilepattern("file2").call();
  1193. git.commit().setMessage("commit2").call();
  1194. // checkout master branch / modify file1, add and commit
  1195. checkoutBranch("refs/heads/master");
  1196. writeTrashFile(FILE1, "modified file1");
  1197. git.add().addFilepattern(FILE1).call();
  1198. git.commit().setMessage("commit3").call();
  1199. // checkout topic branch / modify file0
  1200. checkoutBranch("refs/heads/topic");
  1201. writeTrashFile("file0", "unstaged modified file0");
  1202. // rebase
  1203. assertEquals(Status.UNCOMMITTED_CHANGES,
  1204. git.rebase().setUpstream("refs/heads/master")
  1205. .call().getStatus());
  1206. }
  1207. @Test
  1208. public void testRebaseWithUncommittedMasterChangeOtherCommit()
  1209. throws Exception {
  1210. // create file0, add and commit
  1211. File file0 = writeTrashFile("file0", "file0");
  1212. git.add().addFilepattern("file0").call();
  1213. git.commit().setMessage("commit0").call();
  1214. // create file1, add and commit
  1215. writeTrashFile(FILE1, "file1");
  1216. git.add().addFilepattern(FILE1).call();
  1217. RevCommit commit = git.commit().setMessage("commit1").call();
  1218. // create topic branch and checkout / create file2, add and commit
  1219. createBranch(commit, "refs/heads/topic");
  1220. checkoutBranch("refs/heads/topic");
  1221. writeTrashFile("file2", "file2");
  1222. git.add().addFilepattern("file2").call();
  1223. git.commit().setMessage("commit2").call();
  1224. // checkout master branch / modify file1, add and commit
  1225. checkoutBranch("refs/heads/master");
  1226. writeTrashFile(FILE1, "modified file1");
  1227. git.add().addFilepattern(FILE1).call();
  1228. git.commit().setMessage("commit3").call();
  1229. // checkout topic branch / modify file0 and add
  1230. checkoutBranch("refs/heads/topic");
  1231. write(file0, "unstaged modified file0");
  1232. git.add().addFilepattern("file0").call();
  1233. // do not commit
  1234. // get current index state
  1235. String indexState = indexState(CONTENT);
  1236. // rebase
  1237. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1238. .call();
  1239. assertEquals(Status.UNCOMMITTED_CHANGES, result.getStatus());
  1240. // staged file0 causes DIRTY_INDEX
  1241. assertEquals(1, result.getUncommittedChanges().size());
  1242. assertEquals("unstaged modified file0", read(file0));
  1243. // index shall be unchanged
  1244. assertEquals(indexState, indexState(CONTENT));
  1245. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  1246. }
  1247. @Test
  1248. public void testFastForwardRebaseWithModification() throws Exception {
  1249. // create file0 + file1, add and commit
  1250. writeTrashFile("file0", "file0");
  1251. writeTrashFile(FILE1, "file1");
  1252. git.add().addFilepattern("file0").addFilepattern(FILE1).call();
  1253. RevCommit commit = git.commit().setMessage("commit1").call();
  1254. // create topic branch
  1255. createBranch(commit, "refs/heads/topic");
  1256. // still on master / modify file1, add and commit
  1257. writeTrashFile(FILE1, "modified file1");
  1258. git.add().addFilepattern(FILE1).call();
  1259. git.commit().setMessage("commit2").call();
  1260. // checkout topic branch / modify file0 and add to index
  1261. checkoutBranch("refs/heads/topic");
  1262. writeTrashFile("file0", "modified file0 in index");
  1263. git.add().addFilepattern("file0").addFilepattern(FILE1).call();
  1264. // modify once more
  1265. writeTrashFile("file0", "modified file0");
  1266. // rebase
  1267. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1268. .call();
  1269. assertEquals(Status.FAST_FORWARD, result.getStatus());
  1270. checkFile(new File(db.getWorkTree(), "file0"), "modified file0");
  1271. checkFile(new File(db.getWorkTree(), FILE1), "modified file1");
  1272. assertEquals("[file0, mode:100644, content:modified file0 in index]"
  1273. + "[file1, mode:100644, content:modified file1]",
  1274. indexState(CONTENT));
  1275. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  1276. }
  1277. @Test
  1278. public void testRebaseWithModificationShouldNotDeleteData()
  1279. throws Exception {
  1280. // create file0 + file1, add and commit
  1281. writeTrashFile("file0", "file0");
  1282. writeTrashFile(FILE1, "file1");
  1283. git.add().addFilepattern("file0").addFilepattern(FILE1).call();
  1284. RevCommit commit = git.commit().setMessage("commit1").call();
  1285. // create topic branch
  1286. createBranch(commit, "refs/heads/topic");
  1287. // still on master / modify file1, add and commit
  1288. writeTrashFile(FILE1, "modified file1");
  1289. git.add().addFilepattern(FILE1).call();
  1290. git.commit().setMessage("commit2").call();
  1291. // checkout topic branch / modify file1, add and commit
  1292. checkoutBranch("refs/heads/topic");
  1293. writeTrashFile(FILE1, "modified file1 on topic");
  1294. git.add().addFilepattern(FILE1).call();
  1295. git.commit().setMessage("commit3").call();
  1296. writeTrashFile("file0", "modified file0");
  1297. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1298. .call();
  1299. // the following condition was true before commit 83b6ab233:
  1300. // jgit started the rebase and deleted the change on abort
  1301. // This test should verify that content was deleted
  1302. if (result.getStatus() == Status.STOPPED)
  1303. git.rebase().setOperation(Operation.ABORT).call();
  1304. checkFile(new File(db.getWorkTree(), "file0"), "modified file0");
  1305. checkFile(new File(db.getWorkTree(), FILE1),
  1306. "modified file1 on topic");
  1307. assertEquals("[file0, mode:100644, content:file0]"
  1308. + "[file1, mode:100644, content:modified file1 on topic]",
  1309. indexState(CONTENT));
  1310. }
  1311. @Test
  1312. public void testRebaseWithUncommittedDelete() throws Exception {
  1313. // create file0 + file1, add and commit
  1314. File file0 = writeTrashFile("file0", "file0");
  1315. writeTrashFile(FILE1, "file1");
  1316. git.add().addFilepattern("file0").addFilepattern(FILE1).call();
  1317. RevCommit commit = git.commit().setMessage("commit1").call();
  1318. // create topic branch
  1319. createBranch(commit, "refs/heads/topic");
  1320. // still on master / modify file1, add and commit
  1321. writeTrashFile(FILE1, "modified file1");
  1322. git.add().addFilepattern(FILE1).call();
  1323. git.commit().setMessage("commit2").call();
  1324. // checkout topic branch / delete file0 and add to index
  1325. checkoutBranch("refs/heads/topic");
  1326. git.rm().addFilepattern("file0").call();
  1327. // do not commit
  1328. // rebase
  1329. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1330. .call();
  1331. assertEquals(Status.FAST_FORWARD, result.getStatus());
  1332. assertFalse("File should still be deleted", file0.exists());
  1333. // index should only have updated file1
  1334. assertEquals("[file1, mode:100644, content:modified file1]",
  1335. indexState(CONTENT));
  1336. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  1337. }
  1338. private int countPicks() throws IOException {
  1339. int count = 0;
  1340. File todoFile = getTodoFile();
  1341. BufferedReader br = new BufferedReader(new InputStreamReader(
  1342. new FileInputStream(todoFile), "UTF-8"));
  1343. try {
  1344. String line = br.readLine();
  1345. while (line != null) {
  1346. int firstBlank = line.indexOf(' ');
  1347. if (firstBlank != -1) {
  1348. String actionToken = line.substring(0, firstBlank);
  1349. Action action = null;
  1350. try {
  1351. action = Action.parse(actionToken);
  1352. } catch (Exception e) {
  1353. // ignore
  1354. }
  1355. if (Action.PICK.equals(action))
  1356. count++;
  1357. }
  1358. line = br.readLine();
  1359. }
  1360. return count;
  1361. } finally {
  1362. br.close();
  1363. }
  1364. }
  1365. @Test
  1366. public void testFastForwardWithMultipleCommitsOnDifferentBranches()
  1367. throws Exception {
  1368. // create file1 on master
  1369. writeTrashFile(FILE1, FILE1);
  1370. git.add().addFilepattern(FILE1).call();
  1371. RevCommit first = git.commit().setMessage("Add file1").call();
  1372. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1373. // create a topic branch
  1374. createBranch(first, "refs/heads/topic");
  1375. // create file2 on master
  1376. writeTrashFile("file2", "file2");
  1377. git.add().addFilepattern("file2").call();
  1378. RevCommit second = git.commit().setMessage("Add file2").call();
  1379. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1380. // create side branch
  1381. createBranch(second, "refs/heads/side");
  1382. // update FILE1 on master
  1383. writeTrashFile(FILE1, "blah");
  1384. git.add().addFilepattern(FILE1).call();
  1385. git.commit().setMessage("updated file1 on master")
  1386. .call();
  1387. // switch to side branch and update file2
  1388. checkoutBranch("refs/heads/side");
  1389. writeTrashFile("file2", "more change");
  1390. git.add().addFilepattern("file2").call();
  1391. RevCommit fourth = git.commit().setMessage("update file2 on side")
  1392. .call();
  1393. // switch back to master and merge in side
  1394. checkoutBranch("refs/heads/master");
  1395. MergeResult result = git.merge().include(fourth.getId())
  1396. .setStrategy(MergeStrategy.RESOLVE).call();
  1397. assertEquals(MergeStatus.MERGED, result.getMergeStatus());
  1398. // switch back to topic branch and rebase it onto master
  1399. checkoutBranch("refs/heads/topic");
  1400. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  1401. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1402. checkFile(new File(db.getWorkTree(), "file2"), "more change");
  1403. assertEquals(Status.FAST_FORWARD, res.getStatus());
  1404. }
  1405. @Test
  1406. public void testRebaseShouldLeaveWorkspaceUntouchedWithUnstagedChangesConflict()
  1407. throws Exception {
  1408. writeTrashFile(FILE1, "initial file");
  1409. git.add().addFilepattern(FILE1).call();
  1410. RevCommit initial = git.commit().setMessage("initial commit").call();
  1411. createBranch(initial, "refs/heads/side");
  1412. writeTrashFile(FILE1, "updated file");
  1413. git.add().addFilepattern(FILE1).call();
  1414. git.commit().setMessage("updated FILE1 on master").call();
  1415. // switch to side, modify the file
  1416. checkoutBranch("refs/heads/side");
  1417. writeTrashFile(FILE1, "side update");
  1418. git.add().addFilepattern(FILE1).call();
  1419. git.commit().setMessage("updated FILE1 on side").call();
  1420. File theFile = writeTrashFile(FILE1, "dirty the file");
  1421. // and attempt to rebase
  1422. RebaseResult rebaseResult = git.rebase()
  1423. .setUpstream("refs/heads/master").call();
  1424. assertEquals(Status.UNCOMMITTED_CHANGES, rebaseResult.getStatus());
  1425. assertEquals(1, rebaseResult.getUncommittedChanges().size());
  1426. assertEquals(FILE1, rebaseResult.getUncommittedChanges().get(0));
  1427. checkFile(theFile, "dirty the file");
  1428. assertEquals(RepositoryState.SAFE, git.getRepository()
  1429. .getRepositoryState());
  1430. }
  1431. @Test
  1432. public void testAbortShouldAlsoAbortNonInteractiveRebaseWithRebaseApplyDir()
  1433. throws Exception {
  1434. writeTrashFile(FILE1, "initial file");
  1435. git.add().addFilepattern(FILE1).call();
  1436. git.commit().setMessage("initial commit").call();
  1437. File applyDir = new File(db.getDirectory(), "rebase-apply");
  1438. File headName = new File(applyDir, "head-name");
  1439. FileUtils.mkdir(applyDir);
  1440. write(headName, "master");
  1441. db.writeOrigHead(db.resolve(Constants.HEAD));
  1442. git.rebase().setOperation(Operation.ABORT).call();
  1443. assertFalse("Abort should clean up .git/rebase-apply",
  1444. applyDir.exists());
  1445. assertEquals(RepositoryState.SAFE, git.getRepository()
  1446. .getRepositoryState());
  1447. }
  1448. @Test
  1449. public void testRebaseShouldBeAbleToHandleEmptyLinesInRebaseTodoFile()
  1450. throws IOException {
  1451. String emptyLine = "\n";
  1452. String todo = "pick 1111111 Commit 1\n" + emptyLine
  1453. + "pick 2222222 Commit 2\n" + emptyLine
  1454. + "# Comment line at end\n";
  1455. write(getTodoFile(), todo);
  1456. List<RebaseTodoLine> steps = db.readRebaseTodo(GIT_REBASE_TODO, false);
  1457. assertEquals(2, steps.size());
  1458. assertEquals("1111111", steps.get(0).getCommit().name());
  1459. assertEquals("2222222", steps.get(1).getCommit().name());
  1460. }
  1461. @Test
  1462. public void testRebaseShouldBeAbleToHandleLinesWithoutCommitMessageInRebaseTodoFile()
  1463. throws IOException {
  1464. String todo = "pick 1111111 \n" + "pick 2222222 Commit 2\n"
  1465. + "# Comment line at end\n";
  1466. write(getTodoFile(), todo);
  1467. List<RebaseTodoLine> steps = db.readRebaseTodo(GIT_REBASE_TODO, false);
  1468. assertEquals(2, steps.size());
  1469. assertEquals("1111111", steps.get(0).getCommit().name());
  1470. assertEquals("2222222", steps.get(1).getCommit().name());
  1471. }
  1472. @Test
  1473. public void testRebaseShouldNotFailIfUserAddCommentLinesInPrepareSteps()
  1474. throws Exception {
  1475. commitFile(FILE1, FILE1, "master");
  1476. RevCommit c2 = commitFile("file2", "file2", "master");
  1477. // update files on master
  1478. commitFile(FILE1, "blah", "master");
  1479. RevCommit c4 = commitFile("file2", "more change", "master");
  1480. RebaseResult res = git.rebase().setUpstream("HEAD~2")
  1481. .runInteractively(new InteractiveHandler() {
  1482. public void prepareSteps(List<RebaseTodoLine> steps) {
  1483. steps.add(0, new RebaseTodoLine(
  1484. "# Comment that should not be processed"));
  1485. }
  1486. public String modifyCommitMessage(String commit) {
  1487. fail("modifyCommitMessage() was not expected to be called");
  1488. return commit;
  1489. }
  1490. }).call();
  1491. assertEquals(RebaseResult.Status.FAST_FORWARD, res.getStatus());
  1492. RebaseResult res2 = git.rebase().setUpstream("HEAD~2")
  1493. .runInteractively(new InteractiveHandler() {
  1494. public void prepareSteps(List<RebaseTodoLine> steps) {
  1495. steps.get(0).setAction(Action.COMMENT); // delete
  1496. // RevCommit c4
  1497. }
  1498. public String modifyCommitMessage(String commit) {
  1499. fail("modifyCommitMessage() was not expected to be called");
  1500. return commit;
  1501. }
  1502. }).call();
  1503. assertEquals(RebaseResult.Status.OK, res2.getStatus());
  1504. ObjectId headId = db.resolve(Constants.HEAD);
  1505. RevWalk rw = new RevWalk(db);
  1506. RevCommit rc = rw.parseCommit(headId);
  1507. ObjectId head1Id = db.resolve(Constants.HEAD + "~1");
  1508. RevCommit rc1 = rw.parseCommit(head1Id);
  1509. assertEquals(rc.getFullMessage(), c4.getFullMessage());
  1510. assertEquals(rc1.getFullMessage(), c2.getFullMessage());
  1511. }
  1512. @Test
  1513. public void testParseRewordCommand() throws Exception {
  1514. String todo = "pick 1111111 Commit 1\n"
  1515. + "reword 2222222 Commit 2\n";
  1516. write(getTodoFile(), todo);
  1517. List<RebaseTodoLine> steps = db.readRebaseTodo(GIT_REBASE_TODO, false);
  1518. assertEquals(2, steps.size());
  1519. assertEquals("1111111", steps.get(0).getCommit().name());
  1520. assertEquals("2222222", steps.get(1).getCommit().name());
  1521. assertEquals(Action.REWORD, steps.get(1).getAction());
  1522. }
  1523. @Test
  1524. public void testEmptyRebaseTodo() throws Exception {
  1525. write(getTodoFile(), "");
  1526. assertEquals(0, db.readRebaseTodo(GIT_REBASE_TODO, true).size());
  1527. assertEquals(0, db.readRebaseTodo(GIT_REBASE_TODO, false).size());
  1528. }
  1529. @Test
  1530. public void testOnlyCommentRebaseTodo() throws Exception {
  1531. write(getTodoFile(), "# a b c d e\n# e f");
  1532. assertEquals(0, db.readRebaseTodo(GIT_REBASE_TODO, false).size());
  1533. List<RebaseTodoLine> lines = db.readRebaseTodo(GIT_REBASE_TODO, true);
  1534. assertEquals(2, lines.size());
  1535. for (RebaseTodoLine line : lines)
  1536. assertEquals(Action.COMMENT, line.getAction());
  1537. write(getTodoFile(), "# a b c d e\n# e f\n");
  1538. assertEquals(0, db.readRebaseTodo(GIT_REBASE_TODO, false).size());
  1539. lines = db.readRebaseTodo(GIT_REBASE_TODO, true);
  1540. assertEquals(2, lines.size());
  1541. for (RebaseTodoLine line : lines)
  1542. assertEquals(Action.COMMENT, line.getAction());
  1543. write(getTodoFile(), " \r\n# a b c d e\r\n# e f\r\n#");
  1544. assertEquals(0, db.readRebaseTodo(GIT_REBASE_TODO, false).size());
  1545. lines = db.readRebaseTodo(GIT_REBASE_TODO, true);
  1546. assertEquals(4, lines.size());
  1547. for (RebaseTodoLine line : lines)
  1548. assertEquals(Action.COMMENT, line.getAction());
  1549. }
  1550. @Test
  1551. public void testLeadingSpacesRebaseTodo() throws Exception {
  1552. String todo = " \t\t pick 1111111 Commit 1\n"
  1553. + "\t\n"
  1554. + "\treword 2222222 Commit 2\n";
  1555. write(getTodoFile(), todo);
  1556. List<RebaseTodoLine> steps = db.readRebaseTodo(GIT_REBASE_TODO, false);
  1557. assertEquals(2, steps.size());
  1558. assertEquals("1111111", steps.get(0).getCommit().name());
  1559. assertEquals("2222222", steps.get(1).getCommit().name());
  1560. assertEquals(Action.REWORD, steps.get(1).getAction());
  1561. }
  1562. @Test
  1563. public void testRebaseShouldTryToParseValidLineMarkedAsComment()
  1564. throws IOException {
  1565. String todo = "# pick 1111111 Valid line commented out with space\n"
  1566. + "#edit 2222222 Valid line commented out without space\n"
  1567. + "# pick invalidLine Comment line at end\n";
  1568. write(getTodoFile(), todo);
  1569. List<RebaseTodoLine> steps = db.readRebaseTodo(GIT_REBASE_TODO, true);
  1570. assertEquals(3, steps.size());
  1571. RebaseTodoLine firstLine = steps.get(0);
  1572. assertEquals("1111111", firstLine.getCommit().name());
  1573. assertEquals("Valid line commented out with space",
  1574. firstLine.getShortMessage());
  1575. assertEquals("comment", firstLine.getAction().toToken());
  1576. try {
  1577. firstLine.setAction(Action.PICK);
  1578. assertEquals("1111111", firstLine.getCommit().name());
  1579. assertEquals("pick", firstLine.getAction().toToken());
  1580. } catch (Exception e) {
  1581. fail("Valid parsable RebaseTodoLine that has been commented out should allow to change the action, but failed");
  1582. }
  1583. assertEquals("2222222", steps.get(1).getCommit().name());
  1584. assertEquals("comment", steps.get(1).getAction().toToken());
  1585. assertEquals(null, steps.get(2).getCommit());
  1586. assertEquals(null, steps.get(2).getShortMessage());
  1587. assertEquals("comment", steps.get(2).getAction().toToken());
  1588. assertEquals("# pick invalidLine Comment line at end", steps.get(2)
  1589. .getComment());
  1590. try {
  1591. steps.get(2).setAction(Action.PICK);
  1592. fail("A comment RebaseTodoLine that doesn't contain a valid parsable line should fail, but doesn't");
  1593. } catch (Exception e) {
  1594. // expected
  1595. }
  1596. }
  1597. @SuppressWarnings("unused")
  1598. @Test
  1599. public void testRebaseTodoLineSetComment() throws Exception {
  1600. try {
  1601. new RebaseTodoLine("This is a invalid comment");
  1602. fail("Constructing a comment line with invalid comment string should fail, but doesn't");
  1603. } catch (JGitInternalException e) {
  1604. // expected
  1605. }
  1606. RebaseTodoLine validCommentLine = new RebaseTodoLine(
  1607. "# This is a comment");
  1608. assertEquals(Action.COMMENT, validCommentLine.getAction());
  1609. assertEquals("# This is a comment", validCommentLine.getComment());
  1610. RebaseTodoLine actionLineToBeChanged = new RebaseTodoLine(Action.EDIT,
  1611. AbbreviatedObjectId.fromString("1111111"), "short Message");
  1612. assertEquals(null, actionLineToBeChanged.getComment());
  1613. try {
  1614. actionLineToBeChanged.setComment("invalid comment");
  1615. fail("Setting a invalid comment string should fail but doesn't");
  1616. } catch (JGitInternalException e) {
  1617. assertEquals(null, actionLineToBeChanged.getComment());
  1618. }
  1619. actionLineToBeChanged.setComment("# valid comment");
  1620. assertEquals("# valid comment", actionLineToBeChanged.getComment());
  1621. try {
  1622. actionLineToBeChanged.setComment("invalid comment");
  1623. fail("Setting a invalid comment string should fail but doesn't");
  1624. } catch (JGitInternalException e) {
  1625. // expected
  1626. // setting comment failed, but was successfully set before,
  1627. // therefore it may not be altered since then
  1628. assertEquals("# valid comment", actionLineToBeChanged.getComment());
  1629. }
  1630. try {
  1631. actionLineToBeChanged.setComment("# line1 \n line2");
  1632. actionLineToBeChanged.setComment("line1 \n line2");
  1633. actionLineToBeChanged.setComment("\n");
  1634. actionLineToBeChanged.setComment("# line1 \r line2");
  1635. actionLineToBeChanged.setComment("line1 \r line2");
  1636. actionLineToBeChanged.setComment("\r");
  1637. actionLineToBeChanged.setComment("# line1 \n\r line2");
  1638. actionLineToBeChanged.setComment("line1 \n\r line2");
  1639. actionLineToBeChanged.setComment("\n\r");
  1640. fail("Setting a multiline comment string should fail but doesn't");
  1641. } catch (JGitInternalException e) {
  1642. // expected
  1643. }
  1644. // Try setting valid comments
  1645. actionLineToBeChanged.setComment("# valid comment");
  1646. assertEquals("# valid comment", actionLineToBeChanged.getComment());
  1647. actionLineToBeChanged.setComment("# \t \t valid comment");
  1648. assertEquals("# \t \t valid comment",
  1649. actionLineToBeChanged.getComment());
  1650. actionLineToBeChanged.setComment("# ");
  1651. assertEquals("# ", actionLineToBeChanged.getComment());
  1652. actionLineToBeChanged.setComment("");
  1653. assertEquals("", actionLineToBeChanged.getComment());
  1654. actionLineToBeChanged.setComment(" ");
  1655. assertEquals(" ", actionLineToBeChanged.getComment());
  1656. actionLineToBeChanged.setComment("\t\t");
  1657. assertEquals("\t\t", actionLineToBeChanged.getComment());
  1658. actionLineToBeChanged.setComment(null);
  1659. assertEquals(null, actionLineToBeChanged.getComment());
  1660. }
  1661. @Test
  1662. public void testRebaseInteractiveReword() throws Exception {
  1663. // create file1 on master
  1664. writeTrashFile(FILE1, FILE1);
  1665. git.add().addFilepattern(FILE1).call();
  1666. git.commit().setMessage("Add file1").call();
  1667. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1668. // create file2 on master
  1669. writeTrashFile("file2", "file2");
  1670. git.add().addFilepattern("file2").call();
  1671. git.commit().setMessage("Add file2").call();
  1672. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1673. // update FILE1 on master
  1674. writeTrashFile(FILE1, "blah");
  1675. git.add().addFilepattern(FILE1).call();
  1676. git.commit().setMessage("updated file1 on master").call();
  1677. writeTrashFile("file2", "more change");
  1678. git.add().addFilepattern("file2").call();
  1679. git.commit().setMessage("update file2 on side").call();
  1680. RebaseResult res = git.rebase().setUpstream("HEAD~2")
  1681. .runInteractively(new InteractiveHandler() {
  1682. public void prepareSteps(List<RebaseTodoLine> steps) {
  1683. steps.get(0).setAction(Action.REWORD);
  1684. }
  1685. public String modifyCommitMessage(String commit) {
  1686. return "rewritten commit message";
  1687. }
  1688. }).call();
  1689. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1690. checkFile(new File(db.getWorkTree(), "file2"), "more change");
  1691. assertEquals(Status.OK, res.getStatus());
  1692. Iterator<RevCommit> logIterator = git.log().all().call().iterator();
  1693. logIterator.next(); // skip first commit;
  1694. String actualCommitMag = logIterator.next().getShortMessage();
  1695. assertEquals("rewritten commit message", actualCommitMag);
  1696. }
  1697. @Test
  1698. public void testRebaseInteractiveEdit() throws Exception {
  1699. // create file1 on master
  1700. writeTrashFile(FILE1, FILE1);
  1701. git.add().addFilepattern(FILE1).call();
  1702. git.commit().setMessage("Add file1").call();
  1703. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1704. // create file2 on master
  1705. writeTrashFile("file2", "file2");
  1706. git.add().addFilepattern("file2").call();
  1707. git.commit().setMessage("Add file2").call();
  1708. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1709. // update FILE1 on master
  1710. writeTrashFile(FILE1, "blah");
  1711. git.add().addFilepattern(FILE1).call();
  1712. git.commit().setMessage("updated file1 on master").call();
  1713. writeTrashFile("file2", "more change");
  1714. git.add().addFilepattern("file2").call();
  1715. git.commit().setMessage("update file2 on side").call();
  1716. RebaseResult res = git.rebase().setUpstream("HEAD~2")
  1717. .runInteractively(new InteractiveHandler() {
  1718. public void prepareSteps(List<RebaseTodoLine> steps) {
  1719. steps.get(0).setAction(Action.EDIT);
  1720. }
  1721. public String modifyCommitMessage(String commit) {
  1722. return ""; // not used
  1723. }
  1724. }).call();
  1725. assertEquals(Status.EDIT, res.getStatus());
  1726. RevCommit toBeEditted = git.log().call().iterator().next();
  1727. assertEquals("updated file1 on master", toBeEditted.getFullMessage());
  1728. // change file and commit with new commit message
  1729. writeTrashFile("file1", "edited");
  1730. git.commit().setAll(true).setAmend(true)
  1731. .setMessage("edited commit message").call();
  1732. // resume rebase
  1733. res = git.rebase().setOperation(Operation.CONTINUE).call();
  1734. checkFile(new File(db.getWorkTree(), "file1"), "edited");
  1735. assertEquals(Status.OK, res.getStatus());
  1736. Iterator<RevCommit> logIterator = git.log().all().call().iterator();
  1737. logIterator.next(); // skip first commit;
  1738. String actualCommitMag = logIterator.next().getShortMessage();
  1739. assertEquals("edited commit message", actualCommitMag);
  1740. }
  1741. @Test
  1742. public void testParseSquashFixupSequenceCount() {
  1743. int count = RebaseCommand
  1744. .parseSquashFixupSequenceCount("# This is a combination of 3 commits.\n# newline");
  1745. assertEquals(3, count);
  1746. }
  1747. @Test
  1748. public void testRebaseInteractiveSingleSquashAndModifyMessage() throws Exception {
  1749. // create file1 on master
  1750. writeTrashFile(FILE1, FILE1);
  1751. git.add().addFilepattern(FILE1).call();
  1752. git.commit().setMessage("Add file1\nnew line").call();
  1753. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1754. // create file2 on master
  1755. writeTrashFile("file2", "file2");
  1756. git.add().addFilepattern("file2").call();
  1757. git.commit().setMessage("Add file2\nnew line").call();
  1758. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1759. // update FILE1 on master
  1760. writeTrashFile(FILE1, "blah");
  1761. git.add().addFilepattern(FILE1).call();
  1762. git.commit().setMessage("updated file1 on master\nnew line").call();
  1763. writeTrashFile("file2", "more change");
  1764. git.add().addFilepattern("file2").call();
  1765. git.commit().setMessage("update file2 on master\nnew line").call();
  1766. git.rebase().setUpstream("HEAD~3")
  1767. .runInteractively(new InteractiveHandler() {
  1768. public void prepareSteps(List<RebaseTodoLine> steps) {
  1769. steps.get(1).setAction(Action.SQUASH);
  1770. }
  1771. public String modifyCommitMessage(String commit) {
  1772. final File messageSquashFile = new File(db
  1773. .getDirectory(), "rebase-merge/message-squash");
  1774. final File messageFixupFile = new File(db
  1775. .getDirectory(), "rebase-merge/message-fixup");
  1776. assertFalse(messageFixupFile.exists());
  1777. assertTrue(messageSquashFile.exists());
  1778. assertEquals(
  1779. "# This is a combination of 2 commits.\n# This is the 2nd commit message:\nupdated file1 on master\nnew line\n# The first commit's message is:\nAdd file2\nnew line",
  1780. commit);
  1781. try {
  1782. byte[] messageSquashBytes = IO
  1783. .readFully(messageSquashFile);
  1784. int end = RawParseUtils.prevLF(messageSquashBytes,
  1785. messageSquashBytes.length);
  1786. String messageSquashContent = RawParseUtils.decode(
  1787. messageSquashBytes, 0, end + 1);
  1788. assertEquals(messageSquashContent, commit);
  1789. } catch (Throwable t) {
  1790. fail(t.getMessage());
  1791. }
  1792. return "changed";
  1793. }
  1794. }).call();
  1795. RevWalk walk = new RevWalk(db);
  1796. ObjectId headId = db.resolve(Constants.HEAD);
  1797. RevCommit headCommit = walk.parseCommit(headId);
  1798. assertEquals(headCommit.getFullMessage(),
  1799. "update file2 on master\nnew line");
  1800. ObjectId head2Id = db.resolve(Constants.HEAD + "^1");
  1801. RevCommit head1Commit = walk.parseCommit(head2Id);
  1802. assertEquals("changed", head1Commit.getFullMessage());
  1803. }
  1804. @Test
  1805. public void testRebaseInteractiveMultipleSquash() throws Exception {
  1806. // create file0 on master
  1807. writeTrashFile("file0", "file0");
  1808. git.add().addFilepattern("file0").call();
  1809. git.commit().setMessage("Add file0\nnew line").call();
  1810. assertTrue(new File(db.getWorkTree(), "file0").exists());
  1811. // create file1 on master
  1812. writeTrashFile(FILE1, FILE1);
  1813. git.add().addFilepattern(FILE1).call();
  1814. git.commit().setMessage("Add file1\nnew line").call();
  1815. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1816. // create file2 on master
  1817. writeTrashFile("file2", "file2");
  1818. git.add().addFilepattern("file2").call();
  1819. git.commit().setMessage("Add file2\nnew line").call();
  1820. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1821. // update FILE1 on master
  1822. writeTrashFile(FILE1, "blah");
  1823. git.add().addFilepattern(FILE1).call();
  1824. git.commit().setMessage("updated file1 on master\nnew line").call();
  1825. writeTrashFile("file2", "more change");
  1826. git.add().addFilepattern("file2").call();
  1827. git.commit().setMessage("update file2 on master\nnew line").call();
  1828. git.rebase().setUpstream("HEAD~4")
  1829. .runInteractively(new InteractiveHandler() {
  1830. public void prepareSteps(List<RebaseTodoLine> steps) {
  1831. steps.get(1).setAction(Action.SQUASH);
  1832. steps.get(2).setAction(Action.SQUASH);
  1833. }
  1834. public String modifyCommitMessage(String commit) {
  1835. final File messageSquashFile = new File(db.getDirectory(),
  1836. "rebase-merge/message-squash");
  1837. final File messageFixupFile = new File(db.getDirectory(),
  1838. "rebase-merge/message-fixup");
  1839. assertFalse(messageFixupFile.exists());
  1840. assertTrue(messageSquashFile.exists());
  1841. assertEquals(
  1842. "# This is a combination of 3 commits.\n# This is the 3rd commit message:\nupdated file1 on master\nnew line\n# This is the 2nd commit message:\nAdd file2\nnew line\n# The first commit's message is:\nAdd file1\nnew line",
  1843. commit);
  1844. try {
  1845. byte[] messageSquashBytes = IO
  1846. .readFully(messageSquashFile);
  1847. int end = RawParseUtils.prevLF(messageSquashBytes,
  1848. messageSquashBytes.length);
  1849. String messageSquashContend = RawParseUtils.decode(
  1850. messageSquashBytes, 0, end + 1);
  1851. assertEquals(messageSquashContend, commit);
  1852. } catch (Throwable t) {
  1853. fail(t.getMessage());
  1854. }
  1855. return "# This is a combination of 3 commits.\n# This is the 3rd commit message:\nupdated file1 on master\nnew line\n# This is the 2nd commit message:\nAdd file2\nnew line\n# The first commit's message is:\nAdd file1\nnew line";
  1856. }
  1857. }).call();
  1858. RevWalk walk = new RevWalk(db);
  1859. ObjectId headId = db.resolve(Constants.HEAD);
  1860. RevCommit headCommit = walk.parseCommit(headId);
  1861. assertEquals(headCommit.getFullMessage(),
  1862. "update file2 on master\nnew line");
  1863. ObjectId head2Id = db.resolve(Constants.HEAD + "^1");
  1864. RevCommit head1Commit = walk.parseCommit(head2Id);
  1865. assertEquals(
  1866. "updated file1 on master\nnew line\nAdd file2\nnew line\nAdd file1\nnew line",
  1867. head1Commit.getFullMessage());
  1868. }
  1869. @Test
  1870. public void testRebaseInteractiveMixedSquashAndFixup() throws Exception {
  1871. // create file0 on master
  1872. writeTrashFile("file0", "file0");
  1873. git.add().addFilepattern("file0").call();
  1874. git.commit().setMessage("Add file0\nnew line").call();
  1875. assertTrue(new File(db.getWorkTree(), "file0").exists());
  1876. // create file1 on master
  1877. writeTrashFile(FILE1, FILE1);
  1878. git.add().addFilepattern(FILE1).call();
  1879. git.commit().setMessage("Add file1\nnew line").call();
  1880. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1881. // create file2 on master
  1882. writeTrashFile("file2", "file2");
  1883. git.add().addFilepattern("file2").call();
  1884. git.commit().setMessage("Add file2\nnew line").call();
  1885. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1886. // update FILE1 on master
  1887. writeTrashFile(FILE1, "blah");
  1888. git.add().addFilepattern(FILE1).call();
  1889. git.commit().setMessage("updated file1 on master\nnew line").call();
  1890. writeTrashFile("file2", "more change");
  1891. git.add().addFilepattern("file2").call();
  1892. git.commit().setMessage("update file2 on master\nnew line").call();
  1893. git.rebase().setUpstream("HEAD~4")
  1894. .runInteractively(new InteractiveHandler() {
  1895. public void prepareSteps(List<RebaseTodoLine> steps) {
  1896. steps.get(1).setAction(Action.FIXUP);
  1897. steps.get(2).setAction(Action.SQUASH);
  1898. }
  1899. public String modifyCommitMessage(String commit) {
  1900. final File messageSquashFile = new File(db
  1901. .getDirectory(), "rebase-merge/message-squash");
  1902. final File messageFixupFile = new File(db
  1903. .getDirectory(), "rebase-merge/message-fixup");
  1904. assertFalse(messageFixupFile.exists());
  1905. assertTrue(messageSquashFile.exists());
  1906. assertEquals(
  1907. "# This is a combination of 3 commits.\n# This is the 3rd commit message:\nupdated file1 on master\nnew line\n# The 2nd commit message will be skipped:\n# Add file2\n# new line\n# The first commit's message is:\nAdd file1\nnew line",
  1908. commit);
  1909. try {
  1910. byte[] messageSquashBytes = IO
  1911. .readFully(messageSquashFile);
  1912. int end = RawParseUtils.prevLF(messageSquashBytes,
  1913. messageSquashBytes.length);
  1914. String messageSquashContend = RawParseUtils.decode(
  1915. messageSquashBytes, 0, end + 1);
  1916. assertEquals(messageSquashContend, commit);
  1917. } catch (Throwable t) {
  1918. fail(t.getMessage());
  1919. }
  1920. return "changed";
  1921. }
  1922. }).call();
  1923. RevWalk walk = new RevWalk(db);
  1924. ObjectId headId = db.resolve(Constants.HEAD);
  1925. RevCommit headCommit = walk.parseCommit(headId);
  1926. assertEquals(headCommit.getFullMessage(),
  1927. "update file2 on master\nnew line");
  1928. ObjectId head2Id = db.resolve(Constants.HEAD + "^1");
  1929. RevCommit head1Commit = walk.parseCommit(head2Id);
  1930. assertEquals("changed", head1Commit.getFullMessage());
  1931. }
  1932. @Test
  1933. public void testRebaseInteractiveSingleFixup() throws Exception {
  1934. // create file1 on master
  1935. writeTrashFile(FILE1, FILE1);
  1936. git.add().addFilepattern(FILE1).call();
  1937. git.commit().setMessage("Add file1\nnew line").call();
  1938. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1939. // create file2 on master
  1940. writeTrashFile("file2", "file2");
  1941. git.add().addFilepattern("file2").call();
  1942. git.commit().setMessage("Add file2\nnew line").call();
  1943. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1944. // update FILE1 on master
  1945. writeTrashFile(FILE1, "blah");
  1946. git.add().addFilepattern(FILE1).call();
  1947. git.commit().setMessage("updated file1 on master\nnew line").call();
  1948. writeTrashFile("file2", "more change");
  1949. git.add().addFilepattern("file2").call();
  1950. git.commit().setMessage("update file2 on master\nnew line").call();
  1951. git.rebase().setUpstream("HEAD~3")
  1952. .runInteractively(new InteractiveHandler() {
  1953. public void prepareSteps(List<RebaseTodoLine> steps) {
  1954. steps.get(1).setAction(Action.FIXUP);
  1955. }
  1956. public String modifyCommitMessage(String commit) {
  1957. fail("No callback to modify commit message expected for single fixup");
  1958. return commit;
  1959. }
  1960. }).call();
  1961. RevWalk walk = new RevWalk(db);
  1962. ObjectId headId = db.resolve(Constants.HEAD);
  1963. RevCommit headCommit = walk.parseCommit(headId);
  1964. assertEquals("update file2 on master\nnew line",
  1965. headCommit.getFullMessage());
  1966. ObjectId head1Id = db.resolve(Constants.HEAD + "^1");
  1967. RevCommit head1Commit = walk.parseCommit(head1Id);
  1968. assertEquals("Add file2\nnew line",
  1969. head1Commit.getFullMessage());
  1970. }
  1971. @Test
  1972. public void testRebaseInteractiveFixupWithBlankLines() throws Exception {
  1973. // create file1 on master
  1974. writeTrashFile(FILE1, FILE1);
  1975. git.add().addFilepattern(FILE1).call();
  1976. git.commit().setMessage("Add file1\nnew line").call();
  1977. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1978. // create file2 on master
  1979. writeTrashFile("file2", "file2");
  1980. git.add().addFilepattern("file2").call();
  1981. git.commit().setMessage("Add file2").call();
  1982. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1983. // update FILE1 on master
  1984. writeTrashFile(FILE1, "blah");
  1985. git.add().addFilepattern(FILE1).call();
  1986. git.commit().setMessage("updated file1 on master\n\nsome text").call();
  1987. git.rebase().setUpstream("HEAD~2")
  1988. .runInteractively(new InteractiveHandler() {
  1989. public void prepareSteps(List<RebaseTodoLine> steps) {
  1990. steps.get(1).setAction(Action.FIXUP);
  1991. }
  1992. public String modifyCommitMessage(String commit) {
  1993. fail("No callback to modify commit message expected for single fixup");
  1994. return commit;
  1995. }
  1996. }).call();
  1997. RevWalk walk = new RevWalk(db);
  1998. ObjectId headId = db.resolve(Constants.HEAD);
  1999. RevCommit headCommit = walk.parseCommit(headId);
  2000. assertEquals("Add file2",
  2001. headCommit.getFullMessage());
  2002. }
  2003. @Test(expected = InvalidRebaseStepException.class)
  2004. public void testRebaseInteractiveFixupFirstCommitShouldFail()
  2005. throws Exception {
  2006. // create file1 on master
  2007. writeTrashFile(FILE1, FILE1);
  2008. git.add().addFilepattern(FILE1).call();
  2009. git.commit().setMessage("Add file1\nnew line").call();
  2010. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  2011. // create file2 on master
  2012. writeTrashFile("file2", "file2");
  2013. git.add().addFilepattern("file2").call();
  2014. git.commit().setMessage("Add file2\nnew line").call();
  2015. assertTrue(new File(db.getWorkTree(), "file2").exists());
  2016. git.rebase().setUpstream("HEAD~1")
  2017. .runInteractively(new InteractiveHandler() {
  2018. public void prepareSteps(List<RebaseTodoLine> steps) {
  2019. steps.get(0).setAction(Action.FIXUP);
  2020. }
  2021. public String modifyCommitMessage(String commit) {
  2022. return commit;
  2023. }
  2024. }).call();
  2025. }
  2026. @Test(expected = InvalidRebaseStepException.class)
  2027. public void testRebaseInteractiveSquashFirstCommitShouldFail()
  2028. throws Exception {
  2029. // create file1 on master
  2030. writeTrashFile(FILE1, FILE1);
  2031. git.add().addFilepattern(FILE1).call();
  2032. git.commit().setMessage("Add file1\nnew line").call();
  2033. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  2034. // create file2 on master
  2035. writeTrashFile("file2", "file2");
  2036. git.add().addFilepattern("file2").call();
  2037. git.commit().setMessage("Add file2\nnew line").call();
  2038. assertTrue(new File(db.getWorkTree(), "file2").exists());
  2039. git.rebase().setUpstream("HEAD~1")
  2040. .runInteractively(new InteractiveHandler() {
  2041. public void prepareSteps(List<RebaseTodoLine> steps) {
  2042. steps.get(0).setAction(Action.SQUASH);
  2043. }
  2044. public String modifyCommitMessage(String commit) {
  2045. return commit;
  2046. }
  2047. }).call();
  2048. }
  2049. @Test
  2050. public void testRebaseEndsIfLastStepIsEdit() throws Exception {
  2051. // create file1 on master
  2052. writeTrashFile(FILE1, FILE1);
  2053. git.add().addFilepattern(FILE1).call();
  2054. git.commit().setMessage("Add file1\nnew line").call();
  2055. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  2056. // create file2 on master
  2057. writeTrashFile("file2", "file2");
  2058. git.add().addFilepattern("file2").call();
  2059. git.commit().setMessage("Add file2\nnew line").call();
  2060. assertTrue(new File(db.getWorkTree(), "file2").exists());
  2061. git.rebase().setUpstream("HEAD~1")
  2062. .runInteractively(new InteractiveHandler() {
  2063. public void prepareSteps(List<RebaseTodoLine> steps) {
  2064. steps.get(0).setAction(Action.EDIT);
  2065. }
  2066. public String modifyCommitMessage(String commit) {
  2067. return commit;
  2068. }
  2069. }).call();
  2070. git.commit().setAmend(true)
  2071. .setMessage("Add file2\nnew line\nanother line").call();
  2072. RebaseResult result = git.rebase().setOperation(Operation.CONTINUE)
  2073. .call();
  2074. assertEquals(Status.OK, result.getStatus());
  2075. }
  2076. @Test
  2077. public void testRebaseShouldStopForEditInCaseOfConflict()
  2078. throws Exception {
  2079. // create file1 on master
  2080. writeTrashFile(FILE1, FILE1);
  2081. git.add().addFilepattern(FILE1).call();
  2082. git.commit().setMessage("Add file1\nnew line").call();
  2083. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  2084. //change file1
  2085. writeTrashFile(FILE1, FILE1 + "a");
  2086. git.add().addFilepattern(FILE1).call();
  2087. git.commit().setMessage("Change file1").call();
  2088. //change file1
  2089. writeTrashFile(FILE1, FILE1 + "b");
  2090. git.add().addFilepattern(FILE1).call();
  2091. git.commit().setMessage("Change file1").call();
  2092. RebaseResult result = git.rebase().setUpstream("HEAD~2")
  2093. .runInteractively(new InteractiveHandler() {
  2094. public void prepareSteps(List<RebaseTodoLine> steps) {
  2095. steps.remove(0);
  2096. steps.get(0).setAction(Action.EDIT);
  2097. }
  2098. public String modifyCommitMessage(String commit) {
  2099. return commit;
  2100. }
  2101. }).call();
  2102. assertEquals(Status.STOPPED, result.getStatus());
  2103. git.add().addFilepattern(FILE1).call();
  2104. result = git.rebase().setOperation(Operation.CONTINUE).call();
  2105. assertEquals(Status.EDIT, result.getStatus());
  2106. }
  2107. @Test
  2108. public void testRebaseShouldStopForRewordInCaseOfConflict()
  2109. throws Exception {
  2110. // create file1 on master
  2111. writeTrashFile(FILE1, FILE1);
  2112. git.add().addFilepattern(FILE1).call();
  2113. git.commit().setMessage("Add file1\nnew line").call();
  2114. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  2115. // change file1
  2116. writeTrashFile(FILE1, FILE1 + "a");
  2117. git.add().addFilepattern(FILE1).call();
  2118. git.commit().setMessage("Change file1").call();
  2119. // change file1
  2120. writeTrashFile(FILE1, FILE1 + "b");
  2121. git.add().addFilepattern(FILE1).call();
  2122. git.commit().setMessage("Change file1").call();
  2123. RebaseResult result = git.rebase().setUpstream("HEAD~2")
  2124. .runInteractively(new InteractiveHandler() {
  2125. public void prepareSteps(List<RebaseTodoLine> steps) {
  2126. steps.remove(0);
  2127. steps.get(0).setAction(Action.REWORD);
  2128. }
  2129. public String modifyCommitMessage(String commit) {
  2130. return "rewritten commit message";
  2131. }
  2132. }).call();
  2133. assertEquals(Status.STOPPED, result.getStatus());
  2134. git.add().addFilepattern(FILE1).call();
  2135. result = git.rebase().runInteractively(new InteractiveHandler() {
  2136. public void prepareSteps(List<RebaseTodoLine> steps) {
  2137. steps.remove(0);
  2138. steps.get(0).setAction(Action.REWORD);
  2139. }
  2140. public String modifyCommitMessage(String commit) {
  2141. return "rewritten commit message";
  2142. }
  2143. }).setOperation(Operation.CONTINUE).call();
  2144. assertEquals(Status.OK, result.getStatus());
  2145. Iterator<RevCommit> logIterator = git.log().all().call().iterator();
  2146. String actualCommitMag = logIterator.next().getShortMessage();
  2147. assertEquals("rewritten commit message", actualCommitMag);
  2148. }
  2149. @Test
  2150. public void testRebaseShouldSquashInCaseOfConflict() throws Exception {
  2151. // create file1 on master
  2152. writeTrashFile(FILE1, FILE1);
  2153. git.add().addFilepattern(FILE1).call();
  2154. git.commit().setMessage("Add file1\nnew line").call();
  2155. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  2156. // change file2
  2157. writeTrashFile("file2", "file2");
  2158. git.add().addFilepattern("file2").call();
  2159. git.commit().setMessage("Change file2").call();
  2160. // change file1
  2161. writeTrashFile(FILE1, FILE1 + "a");
  2162. git.add().addFilepattern(FILE1).call();
  2163. git.commit().setMessage("Change file1").call();
  2164. // change file1
  2165. writeTrashFile(FILE1, FILE1 + "b");
  2166. git.add().addFilepattern(FILE1).call();
  2167. git.commit().setMessage("Change file1").call();
  2168. RebaseResult result = git.rebase().setUpstream("HEAD~3")
  2169. .runInteractively(new InteractiveHandler() {
  2170. public void prepareSteps(List<RebaseTodoLine> steps) {
  2171. steps.get(0).setAction(Action.PICK);
  2172. steps.remove(1);
  2173. steps.get(1).setAction(Action.SQUASH);
  2174. }
  2175. public String modifyCommitMessage(String commit) {
  2176. return "squashed message";
  2177. }
  2178. }).call();
  2179. assertEquals(Status.STOPPED, result.getStatus());
  2180. git.add().addFilepattern(FILE1).call();
  2181. result = git.rebase().runInteractively(new InteractiveHandler() {
  2182. public void prepareSteps(List<RebaseTodoLine> steps) {
  2183. steps.get(0).setAction(Action.PICK);
  2184. steps.remove(1);
  2185. steps.get(1).setAction(Action.SQUASH);
  2186. }
  2187. public String modifyCommitMessage(String commit) {
  2188. return "squashed message";
  2189. }
  2190. }).setOperation(Operation.CONTINUE).call();
  2191. assertEquals(Status.OK, result.getStatus());
  2192. Iterator<RevCommit> logIterator = git.log().all().call().iterator();
  2193. String actualCommitMag = logIterator.next().getShortMessage();
  2194. assertEquals("squashed message", actualCommitMag);
  2195. }
  2196. @Test
  2197. public void testRebaseShouldFixupInCaseOfConflict() throws Exception {
  2198. // create file1 on master
  2199. writeTrashFile(FILE1, FILE1);
  2200. git.add().addFilepattern(FILE1).call();
  2201. git.commit().setMessage("Add file1").call();
  2202. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  2203. // change file2
  2204. writeTrashFile("file2", "file2");
  2205. git.add().addFilepattern("file2").call();
  2206. git.commit().setMessage("Change file2").call();
  2207. // change file1
  2208. writeTrashFile(FILE1, FILE1 + "a");
  2209. git.add().addFilepattern(FILE1).call();
  2210. git.commit().setMessage("Change file1").call();
  2211. // change file1, add file3
  2212. writeTrashFile(FILE1, FILE1 + "b");
  2213. writeTrashFile("file3", "file3");
  2214. git.add().addFilepattern(FILE1).call();
  2215. git.add().addFilepattern("file3").call();
  2216. git.commit().setMessage("Change file1, add file3").call();
  2217. RebaseResult result = git.rebase().setUpstream("HEAD~3")
  2218. .runInteractively(new InteractiveHandler() {
  2219. public void prepareSteps(List<RebaseTodoLine> steps) {
  2220. steps.get(0).setAction(Action.PICK);
  2221. steps.remove(1);
  2222. steps.get(1).setAction(Action.FIXUP);
  2223. }
  2224. public String modifyCommitMessage(String commit) {
  2225. return commit;
  2226. }
  2227. }).call();
  2228. assertEquals(Status.STOPPED, result.getStatus());
  2229. git.add().addFilepattern(FILE1).call();
  2230. result = git.rebase().runInteractively(new InteractiveHandler() {
  2231. public void prepareSteps(List<RebaseTodoLine> steps) {
  2232. steps.get(0).setAction(Action.PICK);
  2233. steps.remove(1);
  2234. steps.get(1).setAction(Action.FIXUP);
  2235. }
  2236. public String modifyCommitMessage(String commit) {
  2237. return "commit";
  2238. }
  2239. }).setOperation(Operation.CONTINUE).call();
  2240. assertEquals(Status.OK, result.getStatus());
  2241. Iterator<RevCommit> logIterator = git.log().all().call().iterator();
  2242. String actualCommitMsg = logIterator.next().getShortMessage();
  2243. assertEquals("Change file2", actualCommitMsg);
  2244. actualCommitMsg = logIterator.next().getShortMessage();
  2245. assertEquals("Add file1", actualCommitMsg);
  2246. assertTrue(new File(db.getWorkTree(), "file3").exists());
  2247. }
  2248. @Test
  2249. public void testInteractiveRebaseWithModificationShouldNotDeleteDataOnAbort()
  2250. throws Exception {
  2251. // create file0 + file1, add and commit
  2252. writeTrashFile("file0", "file0");
  2253. writeTrashFile(FILE1, "file1");
  2254. git.add().addFilepattern("file0").addFilepattern(FILE1).call();
  2255. git.commit().setMessage("commit1").call();
  2256. // modify file1, add and commit
  2257. writeTrashFile(FILE1, "modified file1");
  2258. git.add().addFilepattern(FILE1).call();
  2259. git.commit().setMessage("commit2").call();
  2260. // modify file1, add and commit
  2261. writeTrashFile(FILE1, "modified file1 a second time");
  2262. git.add().addFilepattern(FILE1).call();
  2263. git.commit().setMessage("commit3").call();
  2264. // modify file0, but do not commit
  2265. writeTrashFile("file0", "modified file0 in index");
  2266. git.add().addFilepattern("file0").addFilepattern(FILE1).call();
  2267. // do not commit
  2268. writeTrashFile("file0", "modified file0");
  2269. // start rebase
  2270. RebaseResult result = git.rebase().setUpstream("HEAD~2")
  2271. .runInteractively(new InteractiveHandler() {
  2272. public void prepareSteps(List<RebaseTodoLine> steps) {
  2273. steps.get(0).setAction(Action.EDIT);
  2274. steps.get(1).setAction(Action.PICK);
  2275. }
  2276. public String modifyCommitMessage(String commit) {
  2277. return commit;
  2278. }
  2279. }).call();
  2280. // the following condition was true before commit 83b6ab233:
  2281. // jgit started the rebase and deleted the change on abort
  2282. // This test should verify that content was deleted
  2283. if (result.getStatus() == Status.EDIT)
  2284. git.rebase().setOperation(Operation.ABORT).call();
  2285. checkFile(new File(db.getWorkTree(), "file0"), "modified file0");
  2286. checkFile(new File(db.getWorkTree(), "file1"),
  2287. "modified file1 a second time");
  2288. assertEquals("[file0, mode:100644, content:modified file0 in index]"
  2289. + "[file1, mode:100644, content:modified file1 a second time]",
  2290. indexState(CONTENT));
  2291. }
  2292. private File getTodoFile() {
  2293. File todoFile = new File(db.getDirectory(), GIT_REBASE_TODO);
  2294. return todoFile;
  2295. }
  2296. }