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

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