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

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