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

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