You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RebaseCommandTest.java 115KB

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