Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

RebaseCommandTest.java 89KB

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