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

RebaseCommandTest.java 116KB

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