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

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