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

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