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

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