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 118KB

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