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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554
  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.Iterator;
  58. import java.util.List;
  59. import org.eclipse.jgit.api.MergeResult.MergeStatus;
  60. import org.eclipse.jgit.api.RebaseCommand.InteractiveHandler;
  61. import org.eclipse.jgit.api.RebaseCommand.Operation;
  62. import org.eclipse.jgit.api.RebaseResult.Status;
  63. import org.eclipse.jgit.api.errors.InvalidRebaseStepException;
  64. import org.eclipse.jgit.api.errors.JGitInternalException;
  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.dircache.DirCacheCheckout;
  69. import org.eclipse.jgit.junit.RepositoryTestCase;
  70. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  71. import org.eclipse.jgit.lib.Constants;
  72. import org.eclipse.jgit.lib.ObjectId;
  73. import org.eclipse.jgit.lib.PersonIdent;
  74. import org.eclipse.jgit.lib.RebaseTodoLine;
  75. import org.eclipse.jgit.lib.RebaseTodoLine.Action;
  76. import org.eclipse.jgit.lib.RefUpdate;
  77. import org.eclipse.jgit.lib.ReflogEntry;
  78. import org.eclipse.jgit.lib.RepositoryState;
  79. import org.eclipse.jgit.merge.MergeStrategy;
  80. import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason;
  81. import org.eclipse.jgit.revwalk.RevCommit;
  82. import org.eclipse.jgit.revwalk.RevWalk;
  83. import org.eclipse.jgit.util.FileUtils;
  84. import org.eclipse.jgit.util.IO;
  85. import org.eclipse.jgit.util.RawParseUtils;
  86. import org.junit.Before;
  87. import org.junit.Test;
  88. public class RebaseCommandTest extends RepositoryTestCase {
  89. private static final String GIT_REBASE_TODO = "rebase-merge/git-rebase-todo";
  90. private static final String FILE1 = "file1";
  91. protected Git git;
  92. @Override
  93. @Before
  94. public void setUp() throws Exception {
  95. super.setUp();
  96. this.git = new Git(db);
  97. }
  98. private void checkoutCommit(RevCommit commit) throws IllegalStateException,
  99. IOException {
  100. RevWalk walk = new RevWalk(db);
  101. RevCommit head = walk.parseCommit(db.resolve(Constants.HEAD));
  102. DirCacheCheckout dco = new DirCacheCheckout(db, head.getTree(), db
  103. .lockDirCache(), commit.getTree());
  104. dco.setFailOnConflict(true);
  105. dco.checkout();
  106. walk.release();
  107. // update the HEAD
  108. RefUpdate refUpdate = db.updateRef(Constants.HEAD, true);
  109. refUpdate.setNewObjectId(commit);
  110. refUpdate.setRefLogMessage("checkout: moving to " + head.getName(),
  111. false);
  112. refUpdate.forceUpdate();
  113. }
  114. @Test
  115. public void testFastForwardWithNewFile() throws Exception {
  116. // create file1 on master
  117. writeTrashFile(FILE1, FILE1);
  118. git.add().addFilepattern(FILE1).call();
  119. RevCommit first = git.commit().setMessage("Add file1").call();
  120. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  121. // create a topic branch
  122. createBranch(first, "refs/heads/topic");
  123. // create file2 on master
  124. File file2 = writeTrashFile("file2", "file2");
  125. git.add().addFilepattern("file2").call();
  126. RevCommit second = git.commit().setMessage("Add file2").call();
  127. assertTrue(new File(db.getWorkTree(), "file2").exists());
  128. checkoutBranch("refs/heads/topic");
  129. assertFalse(new File(db.getWorkTree(), "file2").exists());
  130. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  131. assertTrue(new File(db.getWorkTree(), "file2").exists());
  132. checkFile(file2, "file2");
  133. assertEquals(Status.FAST_FORWARD, res.getStatus());
  134. List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
  135. .getReverseEntries();
  136. List<ReflogEntry> topicLog = db.getReflogReader("refs/heads/topic")
  137. .getReverseEntries();
  138. List<ReflogEntry> masterLog = db.getReflogReader("refs/heads/master")
  139. .getReverseEntries();
  140. assertEquals("rebase finished: returning to refs/heads/topic", headLog
  141. .get(0).getComment());
  142. assertEquals("checkout: moving from topic to " + second.getName(),
  143. headLog.get(1).getComment());
  144. assertEquals(2, masterLog.size());
  145. assertEquals(2, topicLog.size());
  146. assertEquals(
  147. "rebase finished: refs/heads/topic onto " + second.getName(),
  148. topicLog.get(0).getComment());
  149. }
  150. @Test
  151. public void testFastForwardWithMultipleCommits() throws Exception {
  152. // create file1 on master
  153. writeTrashFile(FILE1, FILE1);
  154. git.add().addFilepattern(FILE1).call();
  155. RevCommit first = git.commit().setMessage("Add file1").call();
  156. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  157. // create a topic branch
  158. createBranch(first, "refs/heads/topic");
  159. // create file2 on master
  160. File file2 = writeTrashFile("file2", "file2");
  161. git.add().addFilepattern("file2").call();
  162. git.commit().setMessage("Add file2").call();
  163. assertTrue(new File(db.getWorkTree(), "file2").exists());
  164. // write a second commit
  165. writeTrashFile("file2", "file2 new content");
  166. git.add().addFilepattern("file2").call();
  167. RevCommit second = git.commit().setMessage("Change content of file2")
  168. .call();
  169. checkoutBranch("refs/heads/topic");
  170. assertFalse(new File(db.getWorkTree(), "file2").exists());
  171. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  172. assertTrue(new File(db.getWorkTree(), "file2").exists());
  173. checkFile(file2, "file2 new content");
  174. assertEquals(Status.FAST_FORWARD, res.getStatus());
  175. List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
  176. .getReverseEntries();
  177. List<ReflogEntry> topicLog = db.getReflogReader("refs/heads/topic")
  178. .getReverseEntries();
  179. List<ReflogEntry> masterLog = db.getReflogReader("refs/heads/master")
  180. .getReverseEntries();
  181. assertEquals("rebase finished: returning to refs/heads/topic", headLog
  182. .get(0).getComment());
  183. assertEquals("checkout: moving from topic to " + second.getName(),
  184. headLog.get(1).getComment());
  185. assertEquals(3, masterLog.size());
  186. assertEquals(2, topicLog.size());
  187. assertEquals(
  188. "rebase finished: refs/heads/topic onto " + second.getName(),
  189. topicLog.get(0).getComment());
  190. }
  191. /**
  192. * Create the following commits and then attempt to rebase topic onto
  193. * master. This will serialize the branches.
  194. *
  195. * <pre>
  196. * A - B (master)
  197. * \
  198. * C - D - F (topic)
  199. * \ /
  200. * E - (side)
  201. * </pre>
  202. *
  203. * into
  204. *
  205. * <pre>
  206. * A - B - (master) C' - D' - E' (topic')
  207. * \
  208. * C - D - F (topic)
  209. * \ /
  210. * E - (side)
  211. * </pre>
  212. *
  213. * @throws Exception
  214. */
  215. @Test
  216. public void testRebaseShouldIgnoreMergeCommits()
  217. throws Exception {
  218. // create file1 on master
  219. writeTrashFile(FILE1, FILE1);
  220. git.add().addFilepattern(FILE1).call();
  221. RevCommit a = git.commit().setMessage("Add file1").call();
  222. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  223. // create a topic branch
  224. createBranch(a, "refs/heads/topic");
  225. // update FILE1 on master
  226. writeTrashFile(FILE1, "blah");
  227. git.add().addFilepattern(FILE1).call();
  228. RevCommit b = git.commit().setMessage("updated file1 on master").call();
  229. checkoutBranch("refs/heads/topic");
  230. writeTrashFile("file3", "more changess");
  231. git.add().addFilepattern("file3").call();
  232. RevCommit c = git.commit()
  233. .setMessage("update file3 on topic").call();
  234. // create a branch from the topic commit
  235. createBranch(c, "refs/heads/side");
  236. // second commit on topic
  237. writeTrashFile("file2", "file2");
  238. git.add().addFilepattern("file2").call();
  239. RevCommit d = git.commit().setMessage("Add file2").call();
  240. assertTrue(new File(db.getWorkTree(), "file2").exists());
  241. // switch to side branch and update file2
  242. checkoutBranch("refs/heads/side");
  243. writeTrashFile("file3", "more change");
  244. git.add().addFilepattern("file3").call();
  245. RevCommit e = git.commit().setMessage("update file2 on side")
  246. .call();
  247. // switch back to topic and merge in side, creating f
  248. checkoutBranch("refs/heads/topic");
  249. MergeResult result = git.merge().include(e.getId())
  250. .setStrategy(MergeStrategy.RESOLVE).call();
  251. assertEquals(MergeStatus.MERGED, result.getMergeStatus());
  252. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  253. assertEquals(Status.OK, res.getStatus());
  254. RevWalk rw = new RevWalk(db);
  255. rw.markStart(rw.parseCommit(db.resolve("refs/heads/topic")));
  256. assertDerivedFrom(rw.next(), e);
  257. assertDerivedFrom(rw.next(), d);
  258. assertDerivedFrom(rw.next(), c);
  259. assertEquals(b, rw.next());
  260. assertEquals(a, rw.next());
  261. List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
  262. .getReverseEntries();
  263. List<ReflogEntry> sideLog = db.getReflogReader("refs/heads/side")
  264. .getReverseEntries();
  265. List<ReflogEntry> topicLog = db.getReflogReader("refs/heads/topic")
  266. .getReverseEntries();
  267. List<ReflogEntry> masterLog = db.getReflogReader("refs/heads/master")
  268. .getReverseEntries();
  269. assertEquals("rebase finished: returning to refs/heads/topic", headLog
  270. .get(0).getComment());
  271. assertEquals("rebase: update file2 on side", headLog.get(1)
  272. .getComment());
  273. assertEquals("rebase: Add file2", headLog.get(2).getComment());
  274. assertEquals("rebase: update file3 on topic", headLog.get(3)
  275. .getComment());
  276. assertEquals("checkout: moving from topic to " + b.getName(), headLog
  277. .get(4).getComment());
  278. assertEquals(2, masterLog.size());
  279. assertEquals(2, sideLog.size());
  280. assertEquals(5, topicLog.size());
  281. assertEquals("rebase finished: refs/heads/topic onto " + b.getName(),
  282. topicLog.get(0).getComment());
  283. }
  284. static void assertDerivedFrom(RevCommit derived, RevCommit original) {
  285. assertThat(derived, not(equalTo(original)));
  286. assertEquals(original.getFullMessage(), derived.getFullMessage());
  287. }
  288. @Test
  289. public void testRebaseParentOntoHeadShouldBeUptoDate() throws Exception {
  290. writeTrashFile(FILE1, FILE1);
  291. git.add().addFilepattern(FILE1).call();
  292. RevCommit parent = git.commit().setMessage("parent comment").call();
  293. writeTrashFile(FILE1, "another change");
  294. git.add().addFilepattern(FILE1).call();
  295. git.commit().setMessage("head commit").call();
  296. RebaseResult result = git.rebase().setUpstream(parent).call();
  297. assertEquals(Status.UP_TO_DATE, result.getStatus());
  298. assertEquals(2, db.getReflogReader(Constants.HEAD).getReverseEntries()
  299. .size());
  300. assertEquals(2, db.getReflogReader("refs/heads/master")
  301. .getReverseEntries().size());
  302. }
  303. @Test
  304. public void testUpToDate() throws Exception {
  305. // create file1 on master
  306. writeTrashFile(FILE1, FILE1);
  307. git.add().addFilepattern(FILE1).call();
  308. RevCommit first = git.commit().setMessage("Add file1").call();
  309. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  310. RebaseResult res = git.rebase().setUpstream(first).call();
  311. assertEquals(Status.UP_TO_DATE, res.getStatus());
  312. assertEquals(1, db.getReflogReader(Constants.HEAD).getReverseEntries()
  313. .size());
  314. assertEquals(1, db.getReflogReader("refs/heads/master")
  315. .getReverseEntries().size());
  316. }
  317. @Test
  318. public void testUnknownUpstream() throws Exception {
  319. // create file1 on master
  320. writeTrashFile(FILE1, FILE1);
  321. git.add().addFilepattern(FILE1).call();
  322. git.commit().setMessage("Add file1").call();
  323. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  324. try {
  325. git.rebase().setUpstream("refs/heads/xyz").call();
  326. fail("expected exception was not thrown");
  327. } catch (RefNotFoundException e) {
  328. // expected exception
  329. }
  330. }
  331. @Test
  332. public void testConflictFreeWithSingleFile() throws Exception {
  333. // create file1 on master
  334. File theFile = writeTrashFile(FILE1, "1\n2\n3\n");
  335. git.add().addFilepattern(FILE1).call();
  336. RevCommit second = git.commit().setMessage("Add file1").call();
  337. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  338. // change first line in master and commit
  339. writeTrashFile(FILE1, "1master\n2\n3\n");
  340. checkFile(theFile, "1master\n2\n3\n");
  341. git.add().addFilepattern(FILE1).call();
  342. RevCommit lastMasterChange = git.commit().setMessage(
  343. "change file1 in master").call();
  344. // create a topic branch based on second commit
  345. createBranch(second, "refs/heads/topic");
  346. checkoutBranch("refs/heads/topic");
  347. // we have the old content again
  348. checkFile(theFile, "1\n2\n3\n");
  349. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  350. // change third line in topic branch
  351. writeTrashFile(FILE1, "1\n2\n3\ntopic\n");
  352. git.add().addFilepattern(FILE1).call();
  353. RevCommit origHead = git.commit().setMessage("change file1 in topic")
  354. .call();
  355. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  356. assertEquals(Status.OK, res.getStatus());
  357. checkFile(theFile, "1master\n2\n3\ntopic\n");
  358. // our old branch should be checked out again
  359. assertEquals("refs/heads/topic", db.getFullBranch());
  360. assertEquals(lastMasterChange, new RevWalk(db).parseCommit(
  361. db.resolve(Constants.HEAD)).getParent(0));
  362. assertEquals(origHead, db.readOrigHead());
  363. List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
  364. .getReverseEntries();
  365. List<ReflogEntry> topicLog = db.getReflogReader("refs/heads/topic")
  366. .getReverseEntries();
  367. List<ReflogEntry> masterLog = db.getReflogReader("refs/heads/master")
  368. .getReverseEntries();
  369. assertEquals(2, masterLog.size());
  370. assertEquals(3, topicLog.size());
  371. assertEquals("rebase finished: refs/heads/topic onto "
  372. + lastMasterChange.getName(), topicLog.get(0).getComment());
  373. assertEquals("rebase finished: returning to refs/heads/topic", headLog
  374. .get(0).getComment());
  375. }
  376. @Test
  377. public void testDetachedHead() throws Exception {
  378. // create file1 on master
  379. File theFile = writeTrashFile(FILE1, "1\n2\n3\n");
  380. git.add().addFilepattern(FILE1).call();
  381. RevCommit second = git.commit().setMessage("Add file1").call();
  382. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  383. // change first line in master and commit
  384. writeTrashFile(FILE1, "1master\n2\n3\n");
  385. checkFile(theFile, "1master\n2\n3\n");
  386. git.add().addFilepattern(FILE1).call();
  387. RevCommit lastMasterChange = git.commit().setMessage(
  388. "change file1 in master").call();
  389. // create a topic branch based on second commit
  390. createBranch(second, "refs/heads/topic");
  391. checkoutBranch("refs/heads/topic");
  392. // we have the old content again
  393. checkFile(theFile, "1\n2\n3\n");
  394. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  395. // change third line in topic branch
  396. writeTrashFile(FILE1, "1\n2\n3\ntopic\n");
  397. git.add().addFilepattern(FILE1).call();
  398. RevCommit topicCommit = git.commit()
  399. .setMessage("change file1 in topic").call();
  400. checkoutBranch("refs/heads/master");
  401. checkoutCommit(topicCommit);
  402. assertEquals(topicCommit.getId().getName(), db.getFullBranch());
  403. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  404. assertEquals(Status.OK, res.getStatus());
  405. checkFile(theFile, "1master\n2\n3\ntopic\n");
  406. assertEquals(lastMasterChange, new RevWalk(db).parseCommit(
  407. db.resolve(Constants.HEAD)).getParent(0));
  408. List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
  409. .getReverseEntries();
  410. assertEquals(8, headLog.size());
  411. assertEquals("rebase: change file1 in topic", headLog.get(0)
  412. .getComment());
  413. assertEquals("checkout: moving from " + topicCommit.getName() + " to "
  414. + lastMasterChange.getName(), headLog.get(1).getComment());
  415. }
  416. @Test
  417. public void testFilesAddedFromTwoBranches() throws Exception {
  418. // create file1 on master
  419. writeTrashFile(FILE1, FILE1);
  420. git.add().addFilepattern(FILE1).call();
  421. RevCommit masterCommit = git.commit().setMessage("Add file1 to master")
  422. .call();
  423. // create a branch named file2 and add file2
  424. createBranch(masterCommit, "refs/heads/file2");
  425. checkoutBranch("refs/heads/file2");
  426. writeTrashFile("file2", "file2");
  427. git.add().addFilepattern("file2").call();
  428. RevCommit addFile2 = git.commit().setMessage(
  429. "Add file2 to branch file2").call();
  430. // create a branch named file3 and add file3
  431. createBranch(masterCommit, "refs/heads/file3");
  432. checkoutBranch("refs/heads/file3");
  433. writeTrashFile("file3", "file3");
  434. git.add().addFilepattern("file3").call();
  435. git.commit().setMessage("Add file3 to branch file3").call();
  436. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  437. assertFalse(new File(db.getWorkTree(), "file2").exists());
  438. assertTrue(new File(db.getWorkTree(), "file3").exists());
  439. RebaseResult res = git.rebase().setUpstream("refs/heads/file2").call();
  440. assertEquals(Status.OK, res.getStatus());
  441. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  442. assertTrue(new File(db.getWorkTree(), "file2").exists());
  443. assertTrue(new File(db.getWorkTree(), "file3").exists());
  444. // our old branch should be checked out again
  445. assertEquals("refs/heads/file3", db.getFullBranch());
  446. assertEquals(addFile2, new RevWalk(db).parseCommit(
  447. db.resolve(Constants.HEAD)).getParent(0));
  448. checkoutBranch("refs/heads/file2");
  449. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  450. assertTrue(new File(db.getWorkTree(), "file2").exists());
  451. assertFalse(new File(db.getWorkTree(), "file3").exists());
  452. }
  453. @Test
  454. public void testStopOnConflict() throws Exception {
  455. // create file1 on master
  456. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  457. "2", "3");
  458. // change first line in master
  459. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  460. checkFile(FILE1, "1master", "2", "3");
  461. // create a topic branch based on second commit
  462. createBranch(firstInMaster, "refs/heads/topic");
  463. checkoutBranch("refs/heads/topic");
  464. // we have the old content again
  465. checkFile(FILE1, "1", "2", "3");
  466. // add a line (non-conflicting)
  467. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  468. "3", "topic4");
  469. // change first line (conflicting)
  470. RevCommit conflicting = writeFileAndCommit(FILE1,
  471. "change file1 in topic", "1topic", "2", "3", "topic4");
  472. RevCommit lastTopicCommit = writeFileAndCommit(FILE1,
  473. "change file1 in topic again", "1topic", "2", "3", "topic4");
  474. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  475. assertEquals(Status.STOPPED, res.getStatus());
  476. assertEquals(conflicting, res.getCurrentCommit());
  477. checkFile(FILE1,
  478. "<<<<<<< Upstream, based on master\n1master\n=======\n1topic",
  479. ">>>>>>> e0d1dea change file1 in topic\n2\n3\ntopic4");
  480. assertEquals(RepositoryState.REBASING_INTERACTIVE, db
  481. .getRepositoryState());
  482. assertTrue(new File(db.getDirectory(), "rebase-merge").exists());
  483. // the first one should be included, so we should have left two picks in
  484. // the file
  485. assertEquals(1, countPicks());
  486. // rebase should not succeed in this state
  487. try {
  488. git.rebase().setUpstream("refs/heads/master").call();
  489. fail("Expected exception was not thrown");
  490. } catch (WrongRepositoryStateException e) {
  491. // expected
  492. }
  493. // abort should reset to topic branch
  494. res = git.rebase().setOperation(Operation.ABORT).call();
  495. assertEquals(res.getStatus(), Status.ABORTED);
  496. assertEquals("refs/heads/topic", db.getFullBranch());
  497. checkFile(FILE1, "1topic", "2", "3", "topic4");
  498. RevWalk rw = new RevWalk(db);
  499. assertEquals(lastTopicCommit, rw
  500. .parseCommit(db.resolve(Constants.HEAD)));
  501. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  502. // rebase- dir in .git must be deleted
  503. assertFalse(new File(db.getDirectory(), "rebase-merge").exists());
  504. }
  505. @Test
  506. public void testStopOnConflictAndContinue() throws Exception {
  507. // create file1 on master
  508. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  509. "2", "3");
  510. // change in master
  511. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  512. checkFile(FILE1, "1master", "2", "3");
  513. // create a topic branch based on the first commit
  514. createBranch(firstInMaster, "refs/heads/topic");
  515. checkoutBranch("refs/heads/topic");
  516. // we have the old content again
  517. checkFile(FILE1, "1", "2", "3");
  518. // add a line (non-conflicting)
  519. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  520. "3", "4topic");
  521. // change first line (conflicting)
  522. writeFileAndCommit(FILE1,
  523. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  524. "3", "4topic");
  525. // change second line (not conflicting)
  526. writeFileAndCommit(FILE1, "change file1 in topic again", "1topic",
  527. "2topic", "3", "4topic");
  528. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  529. assertEquals(Status.STOPPED, res.getStatus());
  530. // continue should throw a meaningful exception
  531. try {
  532. res = git.rebase().setOperation(Operation.CONTINUE).call();
  533. fail("Expected Exception not thrown");
  534. } catch (UnmergedPathsException e) {
  535. // expected
  536. }
  537. // merge the file; the second topic commit should go through
  538. writeFileAndAdd(FILE1, "1topic", "2", "3", "4topic");
  539. res = git.rebase().setOperation(Operation.CONTINUE).call();
  540. assertNotNull(res);
  541. assertEquals(Status.OK, res.getStatus());
  542. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  543. ObjectId headId = db.resolve(Constants.HEAD);
  544. RevWalk rw = new RevWalk(db);
  545. RevCommit rc = rw.parseCommit(headId);
  546. RevCommit parent = rw.parseCommit(rc.getParent(0));
  547. assertEquals("change file1 in topic\n\nThis is conflicting", parent
  548. .getFullMessage());
  549. }
  550. @Test
  551. public void testStopOnConflictAndContinueWithNoDeltaToMaster()
  552. throws Exception {
  553. // create file1 on master
  554. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  555. "2", "3");
  556. // change in master
  557. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  558. checkFile(FILE1, "1master", "2", "3");
  559. // create a topic branch based on the first commit
  560. createBranch(firstInMaster, "refs/heads/topic");
  561. checkoutBranch("refs/heads/topic");
  562. // we have the old content again
  563. checkFile(FILE1, "1", "2", "3");
  564. // change first line (conflicting)
  565. writeFileAndCommit(FILE1,
  566. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  567. "3", "4topic");
  568. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  569. assertEquals(Status.STOPPED, res.getStatus());
  570. // continue should throw a meaningful exception
  571. try {
  572. res = git.rebase().setOperation(Operation.CONTINUE).call();
  573. fail("Expected Exception not thrown");
  574. } catch (UnmergedPathsException e) {
  575. // expected
  576. }
  577. // merge the file; the second topic commit should go through
  578. writeFileAndAdd(FILE1, "1master", "2", "3");
  579. res = git.rebase().setOperation(Operation.CONTINUE).call();
  580. assertNotNull(res);
  581. assertEquals(Status.NOTHING_TO_COMMIT, res.getStatus());
  582. assertEquals(RepositoryState.REBASING_INTERACTIVE,
  583. db.getRepositoryState());
  584. git.rebase().setOperation(Operation.SKIP).call();
  585. ObjectId headId = db.resolve(Constants.HEAD);
  586. RevWalk rw = new RevWalk(db);
  587. RevCommit rc = rw.parseCommit(headId);
  588. assertEquals("change file1 in master", rc.getFullMessage());
  589. }
  590. @Test
  591. public void testStopOnConflictAndFailContinueIfFileIsDirty()
  592. throws Exception {
  593. // create file1 on master
  594. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  595. "2", "3");
  596. // change in master
  597. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  598. checkFile(FILE1, "1master", "2", "3");
  599. // create a topic branch based on the first commit
  600. createBranch(firstInMaster, "refs/heads/topic");
  601. checkoutBranch("refs/heads/topic");
  602. // we have the old content again
  603. checkFile(FILE1, "1", "2", "3");
  604. // add a line (non-conflicting)
  605. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  606. "3", "4topic");
  607. // change first line (conflicting)
  608. writeFileAndCommit(FILE1,
  609. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  610. "3", "4topic");
  611. // change second line (not conflicting)
  612. writeFileAndCommit(FILE1, "change file1 in topic again", "1topic",
  613. "2topic", "3", "4topic");
  614. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  615. assertEquals(Status.STOPPED, res.getStatus());
  616. git.add().addFilepattern(FILE1).call();
  617. File trashFile = writeTrashFile(FILE1, "Some local change");
  618. res = git.rebase().setOperation(Operation.CONTINUE).call();
  619. assertNotNull(res);
  620. assertEquals(Status.STOPPED, res.getStatus());
  621. checkFile(trashFile, "Some local change");
  622. }
  623. @Test
  624. public void testStopOnLastConflictAndContinue() throws Exception {
  625. // create file1 on master
  626. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  627. "2", "3");
  628. // change in master
  629. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  630. checkFile(FILE1, "1master", "2", "3");
  631. // create a topic branch based on the first commit
  632. createBranch(firstInMaster, "refs/heads/topic");
  633. checkoutBranch("refs/heads/topic");
  634. // we have the old content again
  635. checkFile(FILE1, "1", "2", "3");
  636. // add a line (non-conflicting)
  637. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  638. "3", "4topic");
  639. // change first line (conflicting)
  640. writeFileAndCommit(FILE1,
  641. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  642. "3", "4topic");
  643. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  644. assertEquals(Status.STOPPED, res.getStatus());
  645. // merge the file; the second topic commit should go through
  646. writeFileAndAdd(FILE1, "1topic", "2", "3", "4topic");
  647. res = git.rebase().setOperation(Operation.CONTINUE).call();
  648. assertNotNull(res);
  649. assertEquals(Status.OK, res.getStatus());
  650. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  651. }
  652. @Test
  653. public void testStopOnLastConflictAndSkip() throws Exception {
  654. // create file1 on master
  655. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  656. "2", "3");
  657. // change in master
  658. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  659. checkFile(FILE1, "1master", "2", "3");
  660. // create a topic branch based on the first commit
  661. createBranch(firstInMaster, "refs/heads/topic");
  662. checkoutBranch("refs/heads/topic");
  663. // we have the old content again
  664. checkFile(FILE1, "1", "2", "3");
  665. // add a line (non-conflicting)
  666. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  667. "3", "4topic");
  668. // change first line (conflicting)
  669. writeFileAndCommit(FILE1,
  670. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  671. "3", "4topic");
  672. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  673. assertEquals(Status.STOPPED, res.getStatus());
  674. // merge the file; the second topic commit should go through
  675. writeFileAndAdd(FILE1, "1topic", "2", "3", "4topic");
  676. res = git.rebase().setOperation(Operation.SKIP).call();
  677. assertNotNull(res);
  678. assertEquals(Status.OK, res.getStatus());
  679. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  680. }
  681. @Test
  682. public void testMergeFirstStopOnLastConflictAndSkip() throws Exception {
  683. // create file1 on master
  684. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  685. "2", "3");
  686. // change in master
  687. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  688. checkFile(FILE1, "1master", "2", "3");
  689. // create a topic branch based on the first commit
  690. createBranch(firstInMaster, "refs/heads/topic");
  691. checkoutBranch("refs/heads/topic");
  692. // we have the old content again
  693. checkFile(FILE1, "1", "2", "3");
  694. // add a line (conflicting)
  695. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1topic",
  696. "2", "3", "4topic");
  697. // change first line (conflicting again)
  698. writeFileAndCommit(FILE1,
  699. "change file1 in topic\n\nThis is conflicting", "1topicagain",
  700. "2", "3", "4topic");
  701. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  702. assertEquals(Status.STOPPED, res.getStatus());
  703. writeFileAndAdd(FILE1, "merged");
  704. res = git.rebase().setOperation(Operation.CONTINUE).call();
  705. assertEquals(Status.STOPPED, res.getStatus());
  706. res = git.rebase().setOperation(Operation.SKIP).call();
  707. assertNotNull(res);
  708. assertEquals(Status.OK, res.getStatus());
  709. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  710. checkFile(FILE1, "merged");
  711. }
  712. @Test
  713. public void testStopOnConflictAndSkipNoConflict() throws Exception {
  714. // create file1 on master
  715. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  716. "2", "3");
  717. // change in master
  718. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  719. checkFile(FILE1, "1master", "2", "3");
  720. // create a topic branch based on the first commit
  721. createBranch(firstInMaster, "refs/heads/topic");
  722. checkoutBranch("refs/heads/topic");
  723. // we have the old content again
  724. checkFile(FILE1, "1", "2", "3");
  725. // add a line (non-conflicting)
  726. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  727. "3", "4topic");
  728. // change first line (conflicting)
  729. writeFileAndCommit(FILE1,
  730. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  731. "3", "4topic");
  732. // change third line (not conflicting)
  733. writeFileAndCommit(FILE1, "change file1 in topic again", "1topic", "2",
  734. "3topic", "4topic");
  735. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  736. assertEquals(Status.STOPPED, res.getStatus());
  737. res = git.rebase().setOperation(Operation.SKIP).call();
  738. checkFile(FILE1, "1master", "2", "3topic", "4topic");
  739. assertEquals(Status.OK, res.getStatus());
  740. }
  741. @Test
  742. public void testStopOnConflictAndSkipWithConflict() throws Exception {
  743. // create file1 on master
  744. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  745. "2", "3", "4");
  746. // change in master
  747. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2",
  748. "3master", "4");
  749. checkFile(FILE1, "1master", "2", "3master", "4");
  750. // create a topic branch based on the first commit
  751. createBranch(firstInMaster, "refs/heads/topic");
  752. checkoutBranch("refs/heads/topic");
  753. // we have the old content again
  754. checkFile(FILE1, "1", "2", "3", "4");
  755. // add a line (non-conflicting)
  756. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  757. "3", "4", "5topic");
  758. // change first line (conflicting)
  759. writeFileAndCommit(FILE1,
  760. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  761. "3", "4", "5topic");
  762. // change third line (conflicting)
  763. writeFileAndCommit(FILE1, "change file1 in topic again", "1topic", "2",
  764. "3topic", "4", "5topic");
  765. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  766. assertEquals(Status.STOPPED, res.getStatus());
  767. res = git.rebase().setOperation(Operation.SKIP).call();
  768. // TODO is this correct? It is what the command line returns
  769. checkFile(
  770. FILE1,
  771. "1master\n2\n<<<<<<< Upstream, based on master\n3master\n=======\n3topic",
  772. ">>>>>>> 5afc8df change file1 in topic again\n4\n5topic");
  773. assertEquals(Status.STOPPED, res.getStatus());
  774. }
  775. @Test
  776. public void testStopOnConflictCommitAndContinue() throws Exception {
  777. // create file1 on master
  778. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  779. "2", "3");
  780. // change in master
  781. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  782. checkFile(FILE1, "1master", "2", "3");
  783. // create a topic branch based on the first commit
  784. createBranch(firstInMaster, "refs/heads/topic");
  785. checkoutBranch("refs/heads/topic");
  786. // we have the old content again
  787. checkFile(FILE1, "1", "2", "3");
  788. // add a line (non-conflicting)
  789. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  790. "3", "4topic");
  791. // change first line (conflicting)
  792. writeFileAndCommit(FILE1,
  793. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  794. "3", "4topic");
  795. // change second line (not conflicting)
  796. writeFileAndCommit(FILE1, "change file1 in topic again", "1topic", "2",
  797. "3topic", "4topic");
  798. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  799. assertEquals(Status.STOPPED, res.getStatus());
  800. // continue should throw a meaningful exception
  801. try {
  802. res = git.rebase().setOperation(Operation.CONTINUE).call();
  803. fail("Expected Exception not thrown");
  804. } catch (UnmergedPathsException e) {
  805. // expected
  806. }
  807. // merge the file; the second topic commit should go through
  808. writeFileAndCommit(FILE1, "A different commit message", "1topic", "2",
  809. "3", "4topic");
  810. res = git.rebase().setOperation(Operation.CONTINUE).call();
  811. assertNotNull(res);
  812. // nothing to commit. this leaves the repo state in rebase, so that the
  813. // user can decide what to do. if he accidentally committed, reset soft,
  814. // and continue, if he really has nothing to commit, skip.
  815. assertEquals(Status.NOTHING_TO_COMMIT, res.getStatus());
  816. assertEquals(RepositoryState.REBASING_INTERACTIVE,
  817. db.getRepositoryState());
  818. git.rebase().setOperation(Operation.SKIP).call();
  819. ObjectId headId = db.resolve(Constants.HEAD);
  820. RevWalk rw = new RevWalk(db);
  821. RevCommit rc = rw.parseCommit(headId);
  822. RevCommit parent = rw.parseCommit(rc.getParent(0));
  823. assertEquals("A different commit message", parent.getFullMessage());
  824. }
  825. private RevCommit writeFileAndCommit(String fileName, String commitMessage,
  826. String... lines) throws Exception {
  827. StringBuilder sb = new StringBuilder();
  828. for (String line : lines) {
  829. sb.append(line);
  830. sb.append('\n');
  831. }
  832. writeTrashFile(fileName, sb.toString());
  833. git.add().addFilepattern(fileName).call();
  834. return git.commit().setMessage(commitMessage).call();
  835. }
  836. private void writeFileAndAdd(String fileName, String... lines)
  837. throws Exception {
  838. StringBuilder sb = new StringBuilder();
  839. for (String line : lines) {
  840. sb.append(line);
  841. sb.append('\n');
  842. }
  843. writeTrashFile(fileName, sb.toString());
  844. git.add().addFilepattern(fileName).call();
  845. }
  846. private void checkFile(String fileName, String... lines) throws Exception {
  847. File file = new File(db.getWorkTree(), fileName);
  848. StringBuilder sb = new StringBuilder();
  849. for (String line : lines) {
  850. sb.append(line);
  851. sb.append('\n');
  852. }
  853. checkFile(file, sb.toString());
  854. }
  855. @Test
  856. public void testStopOnConflictFileCreationAndDeletion() throws Exception {
  857. // create file1 on master
  858. writeTrashFile(FILE1, "Hello World");
  859. git.add().addFilepattern(FILE1).call();
  860. // create file2 on master
  861. File file2 = writeTrashFile("file2", "Hello World 2");
  862. git.add().addFilepattern("file2").call();
  863. // create file3 on master
  864. File file3 = writeTrashFile("file3", "Hello World 3");
  865. git.add().addFilepattern("file3").call();
  866. RevCommit firstInMaster = git.commit()
  867. .setMessage("Add file 1, 2 and 3").call();
  868. // create file4 on master
  869. File file4 = writeTrashFile("file4", "Hello World 4");
  870. git.add().addFilepattern("file4").call();
  871. deleteTrashFile("file2");
  872. git.add().setUpdate(true).addFilepattern("file2").call();
  873. // create folder folder6 on topic (conflicts with file folder6 on topic
  874. // later on)
  875. writeTrashFile("folder6/file1", "Hello World folder6");
  876. git.add().addFilepattern("folder6/file1").call();
  877. git.commit().setMessage(
  878. "Add file 4 and folder folder6, delete file2 on master").call();
  879. // create a topic branch based on second commit
  880. createBranch(firstInMaster, "refs/heads/topic");
  881. checkoutBranch("refs/heads/topic");
  882. deleteTrashFile("file3");
  883. git.add().setUpdate(true).addFilepattern("file3").call();
  884. // create file5 on topic
  885. File file5 = writeTrashFile("file5", "Hello World 5");
  886. git.add().addFilepattern("file5").call();
  887. git.commit().setMessage("Delete file3 and add file5 in topic").call();
  888. // create file folder6 on topic (conflicts with folder6 on master)
  889. writeTrashFile("folder6", "Hello World 6");
  890. git.add().addFilepattern("folder6").call();
  891. // create file7 on topic
  892. File file7 = writeTrashFile("file7", "Hello World 7");
  893. git.add().addFilepattern("file7").call();
  894. deleteTrashFile("file5");
  895. git.add().setUpdate(true).addFilepattern("file5").call();
  896. RevCommit conflicting = git.commit().setMessage(
  897. "Delete file5, add file folder6 and file7 in topic").call();
  898. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  899. assertEquals(Status.STOPPED, res.getStatus());
  900. assertEquals(conflicting, res.getCurrentCommit());
  901. assertEquals(RepositoryState.REBASING_INTERACTIVE, db
  902. .getRepositoryState());
  903. assertTrue(new File(db.getDirectory(), "rebase-merge").exists());
  904. // the first one should be included, so we should have left two picks in
  905. // the file
  906. assertEquals(0, countPicks());
  907. assertFalse(file2.exists());
  908. assertFalse(file3.exists());
  909. assertTrue(file4.exists());
  910. assertFalse(file5.exists());
  911. assertTrue(file7.exists());
  912. // abort should reset to topic branch
  913. res = git.rebase().setOperation(Operation.ABORT).call();
  914. assertEquals(res.getStatus(), Status.ABORTED);
  915. assertEquals("refs/heads/topic", db.getFullBranch());
  916. RevWalk rw = new RevWalk(db);
  917. assertEquals(conflicting, rw.parseCommit(db.resolve(Constants.HEAD)));
  918. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  919. // rebase- dir in .git must be deleted
  920. assertFalse(new File(db.getDirectory(), "rebase-merge").exists());
  921. assertTrue(file2.exists());
  922. assertFalse(file3.exists());
  923. assertFalse(file4.exists());
  924. assertFalse(file5.exists());
  925. assertTrue(file7.exists());
  926. }
  927. @Test
  928. public void testAuthorScriptConverter() throws Exception {
  929. // -1 h timezone offset
  930. PersonIdent ident = new PersonIdent("Author name", "a.mail@some.com",
  931. 123456789123L, -60);
  932. String convertedAuthor = git.rebase().toAuthorScript(ident);
  933. String[] lines = convertedAuthor.split("\n");
  934. assertEquals("GIT_AUTHOR_NAME='Author name'", lines[0]);
  935. assertEquals("GIT_AUTHOR_EMAIL='a.mail@some.com'", lines[1]);
  936. assertEquals("GIT_AUTHOR_DATE='@123456789 -0100'", lines[2]);
  937. PersonIdent parsedIdent = git.rebase().parseAuthor(
  938. convertedAuthor.getBytes("UTF-8"));
  939. assertEquals(ident.getName(), parsedIdent.getName());
  940. assertEquals(ident.getEmailAddress(), parsedIdent.getEmailAddress());
  941. // this is rounded to the last second
  942. assertEquals(123456789000L, parsedIdent.getWhen().getTime());
  943. assertEquals(ident.getTimeZoneOffset(), parsedIdent.getTimeZoneOffset());
  944. // + 9.5h timezone offset
  945. ident = new PersonIdent("Author name", "a.mail@some.com",
  946. 123456789123L, +570);
  947. convertedAuthor = git.rebase().toAuthorScript(ident);
  948. lines = convertedAuthor.split("\n");
  949. assertEquals("GIT_AUTHOR_NAME='Author name'", lines[0]);
  950. assertEquals("GIT_AUTHOR_EMAIL='a.mail@some.com'", lines[1]);
  951. assertEquals("GIT_AUTHOR_DATE='@123456789 +0930'", lines[2]);
  952. parsedIdent = git.rebase().parseAuthor(
  953. convertedAuthor.getBytes("UTF-8"));
  954. assertEquals(ident.getName(), parsedIdent.getName());
  955. assertEquals(ident.getEmailAddress(), parsedIdent.getEmailAddress());
  956. assertEquals(123456789000L, parsedIdent.getWhen().getTime());
  957. assertEquals(ident.getTimeZoneOffset(), parsedIdent.getTimeZoneOffset());
  958. }
  959. @Test
  960. public void testRepositoryStateChecks() throws Exception {
  961. try {
  962. git.rebase().setOperation(Operation.ABORT).call();
  963. fail("Expected Exception not thrown");
  964. } catch (WrongRepositoryStateException e) {
  965. // expected
  966. }
  967. try {
  968. git.rebase().setOperation(Operation.SKIP).call();
  969. fail("Expected Exception not thrown");
  970. } catch (WrongRepositoryStateException e) {
  971. // expected
  972. }
  973. try {
  974. git.rebase().setOperation(Operation.CONTINUE).call();
  975. fail("Expected Exception not thrown");
  976. } catch (WrongRepositoryStateException e) {
  977. // expected
  978. }
  979. }
  980. @Test
  981. public void testRebaseWithUntrackedFile() throws Exception {
  982. // create file1, add and commit
  983. writeTrashFile(FILE1, "file1");
  984. git.add().addFilepattern(FILE1).call();
  985. RevCommit commit = git.commit().setMessage("commit1").call();
  986. // create topic branch and checkout / create file2, add and commit
  987. createBranch(commit, "refs/heads/topic");
  988. checkoutBranch("refs/heads/topic");
  989. writeTrashFile("file2", "file2");
  990. git.add().addFilepattern("file2").call();
  991. git.commit().setMessage("commit2").call();
  992. // checkout master branch / modify file1, add and commit
  993. checkoutBranch("refs/heads/master");
  994. writeTrashFile(FILE1, "modified file1");
  995. git.add().addFilepattern(FILE1).call();
  996. git.commit().setMessage("commit3").call();
  997. // checkout topic branch / create untracked file3
  998. checkoutBranch("refs/heads/topic");
  999. writeTrashFile("file3", "untracked file3");
  1000. // rebase
  1001. assertEquals(Status.OK, git.rebase().setUpstream("refs/heads/master")
  1002. .call().getStatus());
  1003. }
  1004. @Test
  1005. public void testRebaseWithUnstagedTopicChange() throws Exception {
  1006. // create file1, add and commit
  1007. writeTrashFile(FILE1, "file1");
  1008. git.add().addFilepattern(FILE1).call();
  1009. RevCommit commit = git.commit().setMessage("commit1").call();
  1010. // create topic branch and checkout / create file2, add and commit
  1011. createBranch(commit, "refs/heads/topic");
  1012. checkoutBranch("refs/heads/topic");
  1013. writeTrashFile("file2", "file2");
  1014. git.add().addFilepattern("file2").call();
  1015. git.commit().setMessage("commit2").call();
  1016. // checkout master branch / modify file1, add and commit
  1017. checkoutBranch("refs/heads/master");
  1018. writeTrashFile(FILE1, "modified file1");
  1019. git.add().addFilepattern(FILE1).call();
  1020. git.commit().setMessage("commit3").call();
  1021. // checkout topic branch / modify file2
  1022. checkoutBranch("refs/heads/topic");
  1023. writeTrashFile("file2", "unstaged file2");
  1024. // rebase
  1025. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1026. .call();
  1027. assertEquals(Status.CONFLICTS, result.getStatus());
  1028. assertEquals(1, result.getConflicts().size());
  1029. assertEquals("file2", result.getConflicts().get(0));
  1030. }
  1031. @Test
  1032. public void testRebaseWithUncommittedTopicChange() throws Exception {
  1033. // create file1, add and commit
  1034. writeTrashFile(FILE1, "file1");
  1035. git.add().addFilepattern(FILE1).call();
  1036. RevCommit commit = git.commit().setMessage("commit1").call();
  1037. // create topic branch and checkout / create file2, add and commit
  1038. createBranch(commit, "refs/heads/topic");
  1039. checkoutBranch("refs/heads/topic");
  1040. writeTrashFile("file2", "file2");
  1041. git.add().addFilepattern("file2").call();
  1042. git.commit().setMessage("commit2").call();
  1043. // checkout master branch / modify file1, add and commit
  1044. checkoutBranch("refs/heads/master");
  1045. writeTrashFile(FILE1, "modified file1");
  1046. git.add().addFilepattern(FILE1).call();
  1047. git.commit().setMessage("commit3").call();
  1048. // checkout topic branch / modify file2 and add
  1049. checkoutBranch("refs/heads/topic");
  1050. File uncommittedFile = writeTrashFile("file2", "uncommitted file2");
  1051. git.add().addFilepattern("file2").call();
  1052. // do not commit
  1053. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1054. .call();
  1055. assertEquals(Status.CONFLICTS, result.getStatus());
  1056. assertEquals(1, result.getConflicts().size());
  1057. assertEquals("file2", result.getConflicts().get(0));
  1058. checkFile(uncommittedFile, "uncommitted file2");
  1059. assertEquals(RepositoryState.SAFE, git.getRepository().getRepositoryState());
  1060. }
  1061. @Test
  1062. public void testRebaseWithUnstagedMasterChange() throws Exception {
  1063. // create file1, add and commit
  1064. writeTrashFile(FILE1, "file1");
  1065. git.add().addFilepattern(FILE1).call();
  1066. RevCommit commit = git.commit().setMessage("commit1").call();
  1067. // create topic branch and checkout / create file2, add and commit
  1068. createBranch(commit, "refs/heads/topic");
  1069. checkoutBranch("refs/heads/topic");
  1070. writeTrashFile("file2", "file2");
  1071. git.add().addFilepattern("file2").call();
  1072. git.commit().setMessage("commit2").call();
  1073. // checkout master branch / modify file1, add and commit
  1074. checkoutBranch("refs/heads/master");
  1075. writeTrashFile(FILE1, "modified file1");
  1076. git.add().addFilepattern(FILE1).call();
  1077. git.commit().setMessage("commit3").call();
  1078. // checkout topic branch / modify file1
  1079. checkoutBranch("refs/heads/topic");
  1080. writeTrashFile(FILE1, "unstaged modified file1");
  1081. // rebase
  1082. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1083. .call();
  1084. assertEquals(Status.CONFLICTS, result.getStatus());
  1085. assertEquals(1, result.getConflicts().size());
  1086. assertEquals(FILE1, result.getConflicts().get(0));
  1087. }
  1088. @Test
  1089. public void testRebaseWithUncommittedMasterChange() throws Exception {
  1090. // create file1, add and commit
  1091. writeTrashFile(FILE1, "file1");
  1092. git.add().addFilepattern(FILE1).call();
  1093. RevCommit commit = git.commit().setMessage("commit1").call();
  1094. // create topic branch and checkout / create file2, add and commit
  1095. createBranch(commit, "refs/heads/topic");
  1096. checkoutBranch("refs/heads/topic");
  1097. writeTrashFile("file2", "file2");
  1098. git.add().addFilepattern("file2").call();
  1099. git.commit().setMessage("commit2").call();
  1100. // checkout master branch / modify file1, add and commit
  1101. checkoutBranch("refs/heads/master");
  1102. writeTrashFile(FILE1, "modified file1");
  1103. git.add().addFilepattern(FILE1).call();
  1104. git.commit().setMessage("commit3").call();
  1105. // checkout topic branch / modify file1 and add
  1106. checkoutBranch("refs/heads/topic");
  1107. writeTrashFile(FILE1, "uncommitted modified file1");
  1108. git.add().addFilepattern(FILE1).call();
  1109. // do not commit
  1110. // rebase
  1111. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1112. .call();
  1113. assertEquals(Status.CONFLICTS, result.getStatus());
  1114. assertEquals(1, result.getConflicts().size());
  1115. assertEquals(FILE1, result.getConflicts().get(0));
  1116. }
  1117. @Test
  1118. public void testRebaseWithUnstagedMasterChangeBaseCommit() throws Exception {
  1119. // create file0 + file1, add and commit
  1120. writeTrashFile("file0", "file0");
  1121. writeTrashFile(FILE1, "file1");
  1122. git.add().addFilepattern("file0").addFilepattern(FILE1).call();
  1123. RevCommit commit = git.commit().setMessage("commit1").call();
  1124. // create topic branch and checkout / create file2, add and commit
  1125. createBranch(commit, "refs/heads/topic");
  1126. checkoutBranch("refs/heads/topic");
  1127. writeTrashFile("file2", "file2");
  1128. git.add().addFilepattern("file2").call();
  1129. git.commit().setMessage("commit2").call();
  1130. // checkout master branch / modify file1, add and commit
  1131. checkoutBranch("refs/heads/master");
  1132. writeTrashFile(FILE1, "modified file1");
  1133. git.add().addFilepattern(FILE1).call();
  1134. git.commit().setMessage("commit3").call();
  1135. // checkout topic branch / modify file0
  1136. checkoutBranch("refs/heads/topic");
  1137. writeTrashFile("file0", "unstaged modified file0");
  1138. // rebase
  1139. assertEquals(Status.OK, git.rebase().setUpstream("refs/heads/master")
  1140. .call().getStatus());
  1141. }
  1142. @Test
  1143. public void testRebaseWithUncommittedMasterChangeBaseCommit()
  1144. throws Exception {
  1145. // create file0 + file1, add and commit
  1146. File file0 = writeTrashFile("file0", "file0");
  1147. writeTrashFile(FILE1, "file1");
  1148. git.add().addFilepattern("file0").addFilepattern(FILE1).call();
  1149. RevCommit commit = git.commit().setMessage("commit1").call();
  1150. // create topic branch and checkout / create file2, add and commit
  1151. createBranch(commit, "refs/heads/topic");
  1152. checkoutBranch("refs/heads/topic");
  1153. writeTrashFile("file2", "file2");
  1154. git.add().addFilepattern("file2").call();
  1155. git.commit().setMessage("commit2").call();
  1156. // checkout master branch / modify file1, add and commit
  1157. checkoutBranch("refs/heads/master");
  1158. writeTrashFile(FILE1, "modified file1");
  1159. git.add().addFilepattern(FILE1).call();
  1160. git.commit().setMessage("commit3").call();
  1161. // checkout topic branch / modify file0 and add
  1162. checkoutBranch("refs/heads/topic");
  1163. write(file0, "unstaged modified file0");
  1164. git.add().addFilepattern("file0").call();
  1165. // do not commit
  1166. // get current index state
  1167. String indexState = indexState(CONTENT);
  1168. // rebase
  1169. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1170. .call();
  1171. assertEquals(Status.FAILED, result.getStatus());
  1172. // staged file0 causes DIRTY_INDEX
  1173. assertEquals(1, result.getFailingPaths().size());
  1174. assertEquals(MergeFailureReason.DIRTY_INDEX, result.getFailingPaths()
  1175. .get("file0"));
  1176. assertEquals("unstaged modified file0", read(file0));
  1177. // index shall be unchanged
  1178. assertEquals(indexState, indexState(CONTENT));
  1179. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  1180. }
  1181. @Test
  1182. public void testRebaseWithUnstagedMasterChangeOtherCommit()
  1183. throws Exception {
  1184. // create file0, add and commit
  1185. writeTrashFile("file0", "file0");
  1186. git.add().addFilepattern("file0").call();
  1187. git.commit().setMessage("commit0").call();
  1188. // create file1, add and commit
  1189. writeTrashFile(FILE1, "file1");
  1190. git.add().addFilepattern(FILE1).call();
  1191. RevCommit commit = git.commit().setMessage("commit1").call();
  1192. // create topic branch and checkout / create file2, add and commit
  1193. createBranch(commit, "refs/heads/topic");
  1194. checkoutBranch("refs/heads/topic");
  1195. writeTrashFile("file2", "file2");
  1196. git.add().addFilepattern("file2").call();
  1197. git.commit().setMessage("commit2").call();
  1198. // checkout master branch / modify file1, add and commit
  1199. checkoutBranch("refs/heads/master");
  1200. writeTrashFile(FILE1, "modified file1");
  1201. git.add().addFilepattern(FILE1).call();
  1202. git.commit().setMessage("commit3").call();
  1203. // checkout topic branch / modify file0
  1204. checkoutBranch("refs/heads/topic");
  1205. writeTrashFile("file0", "unstaged modified file0");
  1206. // rebase
  1207. assertEquals(Status.OK, git.rebase().setUpstream("refs/heads/master")
  1208. .call().getStatus());
  1209. }
  1210. @Test
  1211. public void testRebaseWithUncommittedMasterChangeOtherCommit()
  1212. throws Exception {
  1213. // create file0, add and commit
  1214. File file0 = writeTrashFile("file0", "file0");
  1215. git.add().addFilepattern("file0").call();
  1216. git.commit().setMessage("commit0").call();
  1217. // create file1, add and commit
  1218. writeTrashFile(FILE1, "file1");
  1219. git.add().addFilepattern(FILE1).call();
  1220. RevCommit commit = git.commit().setMessage("commit1").call();
  1221. // create topic branch and checkout / create file2, add and commit
  1222. createBranch(commit, "refs/heads/topic");
  1223. checkoutBranch("refs/heads/topic");
  1224. writeTrashFile("file2", "file2");
  1225. git.add().addFilepattern("file2").call();
  1226. git.commit().setMessage("commit2").call();
  1227. // checkout master branch / modify file1, add and commit
  1228. checkoutBranch("refs/heads/master");
  1229. writeTrashFile(FILE1, "modified file1");
  1230. git.add().addFilepattern(FILE1).call();
  1231. git.commit().setMessage("commit3").call();
  1232. // checkout topic branch / modify file0 and add
  1233. checkoutBranch("refs/heads/topic");
  1234. write(file0, "unstaged modified file0");
  1235. git.add().addFilepattern("file0").call();
  1236. // do not commit
  1237. // get current index state
  1238. String indexState = indexState(CONTENT);
  1239. // rebase
  1240. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1241. .call();
  1242. assertEquals(Status.FAILED, result.getStatus());
  1243. // staged file0 causes DIRTY_INDEX
  1244. assertEquals(1, result.getFailingPaths().size());
  1245. assertEquals(MergeFailureReason.DIRTY_INDEX, result.getFailingPaths()
  1246. .get("file0"));
  1247. assertEquals("unstaged modified file0", read(file0));
  1248. // index shall be unchanged
  1249. assertEquals(indexState, indexState(CONTENT));
  1250. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  1251. }
  1252. @Test
  1253. public void testRebaseWithUncommittedDelete() throws Exception {
  1254. // create file0 + file1, add and commit
  1255. File file0 = writeTrashFile("file0", "file0");
  1256. writeTrashFile(FILE1, "file1");
  1257. git.add().addFilepattern("file0").addFilepattern(FILE1).call();
  1258. RevCommit commit = git.commit().setMessage("commit1").call();
  1259. // create topic branch
  1260. createBranch(commit, "refs/heads/topic");
  1261. // still on master / modify file1, add and commit
  1262. writeTrashFile(FILE1, "modified file1");
  1263. git.add().addFilepattern(FILE1).call();
  1264. git.commit().setMessage("commit2").call();
  1265. // checkout topic branch / delete file0 and add to index
  1266. checkoutBranch("refs/heads/topic");
  1267. git.rm().addFilepattern("file0").call();
  1268. // do not commit
  1269. // rebase
  1270. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1271. .call();
  1272. assertEquals(Status.FAST_FORWARD, result.getStatus());
  1273. assertFalse("File should still be deleted", file0.exists());
  1274. // index should only have updated file1
  1275. assertEquals("[file1, mode:100644, content:modified file1]",
  1276. indexState(CONTENT));
  1277. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  1278. }
  1279. private int countPicks() throws IOException {
  1280. int count = 0;
  1281. File todoFile = getTodoFile();
  1282. BufferedReader br = new BufferedReader(new InputStreamReader(
  1283. new FileInputStream(todoFile), "UTF-8"));
  1284. try {
  1285. String line = br.readLine();
  1286. while (line != null) {
  1287. int firstBlank = line.indexOf(' ');
  1288. if (firstBlank != -1) {
  1289. String actionToken = line.substring(0, firstBlank);
  1290. Action action = null;
  1291. try {
  1292. action = Action.parse(actionToken);
  1293. } catch (Exception e) {
  1294. // ignore
  1295. }
  1296. if (Action.PICK.equals(action))
  1297. count++;
  1298. }
  1299. line = br.readLine();
  1300. }
  1301. return count;
  1302. } finally {
  1303. br.close();
  1304. }
  1305. }
  1306. @Test
  1307. public void testFastForwardWithMultipleCommitsOnDifferentBranches()
  1308. throws Exception {
  1309. // create file1 on master
  1310. writeTrashFile(FILE1, FILE1);
  1311. git.add().addFilepattern(FILE1).call();
  1312. RevCommit first = git.commit().setMessage("Add file1").call();
  1313. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1314. // create a topic branch
  1315. createBranch(first, "refs/heads/topic");
  1316. // create file2 on master
  1317. writeTrashFile("file2", "file2");
  1318. git.add().addFilepattern("file2").call();
  1319. RevCommit second = git.commit().setMessage("Add file2").call();
  1320. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1321. // create side branch
  1322. createBranch(second, "refs/heads/side");
  1323. // update FILE1 on master
  1324. writeTrashFile(FILE1, "blah");
  1325. git.add().addFilepattern(FILE1).call();
  1326. git.commit().setMessage("updated file1 on master")
  1327. .call();
  1328. // switch to side branch and update file2
  1329. checkoutBranch("refs/heads/side");
  1330. writeTrashFile("file2", "more change");
  1331. git.add().addFilepattern("file2").call();
  1332. RevCommit fourth = git.commit().setMessage("update file2 on side")
  1333. .call();
  1334. // switch back to master and merge in side
  1335. checkoutBranch("refs/heads/master");
  1336. MergeResult result = git.merge().include(fourth.getId())
  1337. .setStrategy(MergeStrategy.RESOLVE).call();
  1338. assertEquals(MergeStatus.MERGED, result.getMergeStatus());
  1339. // switch back to topic branch and rebase it onto master
  1340. checkoutBranch("refs/heads/topic");
  1341. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  1342. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1343. checkFile(new File(db.getWorkTree(), "file2"), "more change");
  1344. assertEquals(Status.FAST_FORWARD, res.getStatus());
  1345. }
  1346. @Test
  1347. public void testRebaseShouldLeaveWorkspaceUntouchedWithUnstagedChangesConflict()
  1348. throws Exception {
  1349. writeTrashFile(FILE1, "initial file");
  1350. git.add().addFilepattern(FILE1).call();
  1351. RevCommit initial = git.commit().setMessage("initial commit").call();
  1352. createBranch(initial, "refs/heads/side");
  1353. writeTrashFile(FILE1, "updated file");
  1354. git.add().addFilepattern(FILE1).call();
  1355. git.commit().setMessage("updated FILE1 on master").call();
  1356. // switch to side, modify the file
  1357. checkoutBranch("refs/heads/side");
  1358. writeTrashFile(FILE1, "side update");
  1359. git.add().addFilepattern(FILE1).call();
  1360. git.commit().setMessage("updated FILE1 on side").call();
  1361. File theFile = writeTrashFile(FILE1, "dirty the file");
  1362. // and attempt to rebase
  1363. RebaseResult rebaseResult = git.rebase()
  1364. .setUpstream("refs/heads/master").call();
  1365. assertEquals(Status.CONFLICTS, rebaseResult.getStatus());
  1366. assertEquals(1, rebaseResult.getConflicts().size());
  1367. assertEquals(FILE1, rebaseResult.getConflicts().get(0));
  1368. checkFile(theFile, "dirty the file");
  1369. assertEquals(RepositoryState.SAFE, git.getRepository()
  1370. .getRepositoryState());
  1371. }
  1372. @Test
  1373. public void testAbortShouldAlsoAbortNonInteractiveRebaseWithRebaseApplyDir()
  1374. throws Exception {
  1375. writeTrashFile(FILE1, "initial file");
  1376. git.add().addFilepattern(FILE1).call();
  1377. git.commit().setMessage("initial commit").call();
  1378. File applyDir = new File(db.getDirectory(), "rebase-apply");
  1379. File headName = new File(applyDir, "head-name");
  1380. FileUtils.mkdir(applyDir);
  1381. write(headName, "master");
  1382. db.writeOrigHead(db.resolve(Constants.HEAD));
  1383. git.rebase().setOperation(Operation.ABORT).call();
  1384. assertFalse("Abort should clean up .git/rebase-apply",
  1385. applyDir.exists());
  1386. assertEquals(RepositoryState.SAFE, git.getRepository()
  1387. .getRepositoryState());
  1388. }
  1389. @Test
  1390. public void testRebaseShouldBeAbleToHandleEmptyLinesInRebaseTodoFile()
  1391. throws IOException {
  1392. String emptyLine = "\n";
  1393. String todo = "pick 1111111 Commit 1\n" + emptyLine
  1394. + "pick 2222222 Commit 2\n" + emptyLine
  1395. + "# Comment line at end\n";
  1396. write(getTodoFile(), todo);
  1397. List<RebaseTodoLine> steps = db.readRebaseTodo(GIT_REBASE_TODO, false);
  1398. assertEquals(2, steps.size());
  1399. assertEquals("1111111", steps.get(0).getCommit().name());
  1400. assertEquals("2222222", steps.get(1).getCommit().name());
  1401. }
  1402. @Test
  1403. public void testRebaseShouldBeAbleToHandleLinesWithoutCommitMessageInRebaseTodoFile()
  1404. throws IOException {
  1405. String todo = "pick 1111111 \n" + "pick 2222222 Commit 2\n"
  1406. + "# Comment line at end\n";
  1407. write(getTodoFile(), todo);
  1408. List<RebaseTodoLine> steps = db.readRebaseTodo(GIT_REBASE_TODO, false);
  1409. assertEquals(2, steps.size());
  1410. assertEquals("1111111", steps.get(0).getCommit().name());
  1411. assertEquals("2222222", steps.get(1).getCommit().name());
  1412. }
  1413. @Test
  1414. public void testRebaseShouldNotFailIfUserAddCommentLinesInPrepareSteps()
  1415. throws Exception {
  1416. commitFile(FILE1, FILE1, "master");
  1417. RevCommit c2 = commitFile("file2", "file2", "master");
  1418. // update files on master
  1419. commitFile(FILE1, "blah", "master");
  1420. RevCommit c4 = commitFile("file2", "more change", "master");
  1421. RebaseResult res = git.rebase().setUpstream("HEAD~2")
  1422. .runInteractively(new InteractiveHandler() {
  1423. public void prepareSteps(List<RebaseTodoLine> steps) {
  1424. steps.add(0, new RebaseTodoLine(
  1425. "# Comment that should not be processed"));
  1426. }
  1427. public String modifyCommitMessage(String commit) {
  1428. fail("modifyCommitMessage() was not expected to be called");
  1429. return commit;
  1430. }
  1431. }).call();
  1432. assertEquals(RebaseResult.Status.FAST_FORWARD, res.getStatus());
  1433. RebaseResult res2 = git.rebase().setUpstream("HEAD~2")
  1434. .runInteractively(new InteractiveHandler() {
  1435. public void prepareSteps(List<RebaseTodoLine> steps) {
  1436. steps.get(0).setAction(Action.COMMENT); // delete
  1437. // RevCommit c4
  1438. }
  1439. public String modifyCommitMessage(String commit) {
  1440. fail("modifyCommitMessage() was not expected to be called");
  1441. return commit;
  1442. }
  1443. }).call();
  1444. assertEquals(RebaseResult.Status.OK, res2.getStatus());
  1445. ObjectId headId = db.resolve(Constants.HEAD);
  1446. RevWalk rw = new RevWalk(db);
  1447. RevCommit rc = rw.parseCommit(headId);
  1448. ObjectId head1Id = db.resolve(Constants.HEAD + "~1");
  1449. RevCommit rc1 = rw.parseCommit(head1Id);
  1450. assertEquals(rc.getFullMessage(), c4.getFullMessage());
  1451. assertEquals(rc1.getFullMessage(), c2.getFullMessage());
  1452. }
  1453. @Test
  1454. public void testParseRewordCommand() throws Exception {
  1455. String todo = "pick 1111111 Commit 1\n"
  1456. + "reword 2222222 Commit 2\n";
  1457. write(getTodoFile(), todo);
  1458. List<RebaseTodoLine> steps = db.readRebaseTodo(GIT_REBASE_TODO, false);
  1459. assertEquals(2, steps.size());
  1460. assertEquals("1111111", steps.get(0).getCommit().name());
  1461. assertEquals("2222222", steps.get(1).getCommit().name());
  1462. assertEquals(Action.REWORD, steps.get(1).getAction());
  1463. }
  1464. @Test
  1465. public void testEmptyRebaseTodo() throws Exception {
  1466. write(getTodoFile(), "");
  1467. assertEquals(0, db.readRebaseTodo(GIT_REBASE_TODO, true).size());
  1468. assertEquals(0, db.readRebaseTodo(GIT_REBASE_TODO, false).size());
  1469. }
  1470. @Test
  1471. public void testOnlyCommentRebaseTodo() throws Exception {
  1472. write(getTodoFile(), "# a b c d e\n# e f");
  1473. assertEquals(0, db.readRebaseTodo(GIT_REBASE_TODO, false).size());
  1474. List<RebaseTodoLine> lines = db.readRebaseTodo(GIT_REBASE_TODO, true);
  1475. assertEquals(2, lines.size());
  1476. for (RebaseTodoLine line : lines)
  1477. assertEquals(Action.COMMENT, line.getAction());
  1478. write(getTodoFile(), "# a b c d e\n# e f\n");
  1479. assertEquals(0, db.readRebaseTodo(GIT_REBASE_TODO, false).size());
  1480. lines = db.readRebaseTodo(GIT_REBASE_TODO, true);
  1481. assertEquals(2, lines.size());
  1482. for (RebaseTodoLine line : lines)
  1483. assertEquals(Action.COMMENT, line.getAction());
  1484. write(getTodoFile(), " \r\n# a b c d e\r\n# e f\r\n#");
  1485. assertEquals(0, db.readRebaseTodo(GIT_REBASE_TODO, false).size());
  1486. lines = db.readRebaseTodo(GIT_REBASE_TODO, true);
  1487. assertEquals(4, lines.size());
  1488. for (RebaseTodoLine line : lines)
  1489. assertEquals(Action.COMMENT, line.getAction());
  1490. }
  1491. @Test
  1492. public void testLeadingSpacesRebaseTodo() throws Exception {
  1493. String todo = " \t\t pick 1111111 Commit 1\n"
  1494. + "\t\n"
  1495. + "\treword 2222222 Commit 2\n";
  1496. write(getTodoFile(), todo);
  1497. List<RebaseTodoLine> steps = db.readRebaseTodo(GIT_REBASE_TODO, false);
  1498. assertEquals(2, steps.size());
  1499. assertEquals("1111111", steps.get(0).getCommit().name());
  1500. assertEquals("2222222", steps.get(1).getCommit().name());
  1501. assertEquals(Action.REWORD, steps.get(1).getAction());
  1502. }
  1503. @Test
  1504. public void testRebaseShouldTryToParseValidLineMarkedAsComment()
  1505. throws IOException {
  1506. String todo = "# pick 1111111 Valid line commented out with space\n"
  1507. + "#edit 2222222 Valid line commented out without space\n"
  1508. + "# pick invalidLine Comment line at end\n";
  1509. write(getTodoFile(), todo);
  1510. List<RebaseTodoLine> steps = db.readRebaseTodo(GIT_REBASE_TODO, true);
  1511. assertEquals(3, steps.size());
  1512. RebaseTodoLine firstLine = steps.get(0);
  1513. assertEquals("1111111", firstLine.getCommit().name());
  1514. assertEquals("Valid line commented out with space",
  1515. firstLine.getShortMessage());
  1516. assertEquals("comment", firstLine.getAction().toToken());
  1517. try {
  1518. firstLine.setAction(Action.PICK);
  1519. assertEquals("1111111", firstLine.getCommit().name());
  1520. assertEquals("pick", firstLine.getAction().toToken());
  1521. } catch (Exception e) {
  1522. fail("Valid parsable RebaseTodoLine that has been commented out should allow to change the action, but failed");
  1523. }
  1524. assertEquals("2222222", steps.get(1).getCommit().name());
  1525. assertEquals("comment", steps.get(1).getAction().toToken());
  1526. assertEquals(null, steps.get(2).getCommit());
  1527. assertEquals(null, steps.get(2).getShortMessage());
  1528. assertEquals("comment", steps.get(2).getAction().toToken());
  1529. assertEquals("# pick invalidLine Comment line at end", steps.get(2)
  1530. .getComment());
  1531. try {
  1532. steps.get(2).setAction(Action.PICK);
  1533. fail("A comment RebaseTodoLine that doesn't contain a valid parsable line should fail, but doesn't");
  1534. } catch (Exception e) {
  1535. // expected
  1536. }
  1537. }
  1538. @SuppressWarnings("unused")
  1539. @Test
  1540. public void testRebaseTodoLineSetComment() throws Exception {
  1541. try {
  1542. new RebaseTodoLine("This is a invalid comment");
  1543. fail("Constructing a comment line with invalid comment string should fail, but doesn't");
  1544. } catch (JGitInternalException e) {
  1545. // expected
  1546. }
  1547. RebaseTodoLine validCommentLine = new RebaseTodoLine(
  1548. "# This is a comment");
  1549. assertEquals(Action.COMMENT, validCommentLine.getAction());
  1550. assertEquals("# This is a comment", validCommentLine.getComment());
  1551. RebaseTodoLine actionLineToBeChanged = new RebaseTodoLine(Action.EDIT,
  1552. AbbreviatedObjectId.fromString("1111111"), "short Message");
  1553. assertEquals(null, actionLineToBeChanged.getComment());
  1554. try {
  1555. actionLineToBeChanged.setComment("invalid comment");
  1556. fail("Setting a invalid comment string should fail but doesn't");
  1557. } catch (JGitInternalException e) {
  1558. assertEquals(null, actionLineToBeChanged.getComment());
  1559. }
  1560. actionLineToBeChanged.setComment("# valid comment");
  1561. assertEquals("# valid comment", actionLineToBeChanged.getComment());
  1562. try {
  1563. actionLineToBeChanged.setComment("invalid comment");
  1564. fail("Setting a invalid comment string should fail but doesn't");
  1565. } catch (JGitInternalException e) {
  1566. // expected
  1567. // setting comment failed, but was successfully set before,
  1568. // therefore it may not be altered since then
  1569. assertEquals("# valid comment", actionLineToBeChanged.getComment());
  1570. }
  1571. try {
  1572. actionLineToBeChanged.setComment("# line1 \n line2");
  1573. actionLineToBeChanged.setComment("line1 \n line2");
  1574. actionLineToBeChanged.setComment("\n");
  1575. actionLineToBeChanged.setComment("# line1 \r line2");
  1576. actionLineToBeChanged.setComment("line1 \r line2");
  1577. actionLineToBeChanged.setComment("\r");
  1578. actionLineToBeChanged.setComment("# line1 \n\r line2");
  1579. actionLineToBeChanged.setComment("line1 \n\r line2");
  1580. actionLineToBeChanged.setComment("\n\r");
  1581. fail("Setting a multiline comment string should fail but doesn't");
  1582. } catch (JGitInternalException e) {
  1583. // expected
  1584. }
  1585. // Try setting valid comments
  1586. actionLineToBeChanged.setComment("# valid comment");
  1587. assertEquals("# valid comment", actionLineToBeChanged.getComment());
  1588. actionLineToBeChanged.setComment("# \t \t valid comment");
  1589. assertEquals("# \t \t valid comment",
  1590. actionLineToBeChanged.getComment());
  1591. actionLineToBeChanged.setComment("# ");
  1592. assertEquals("# ", actionLineToBeChanged.getComment());
  1593. actionLineToBeChanged.setComment("");
  1594. assertEquals("", actionLineToBeChanged.getComment());
  1595. actionLineToBeChanged.setComment(" ");
  1596. assertEquals(" ", actionLineToBeChanged.getComment());
  1597. actionLineToBeChanged.setComment("\t\t");
  1598. assertEquals("\t\t", actionLineToBeChanged.getComment());
  1599. actionLineToBeChanged.setComment(null);
  1600. assertEquals(null, actionLineToBeChanged.getComment());
  1601. }
  1602. @Test
  1603. public void testRebaseInteractiveReword() throws Exception {
  1604. // create file1 on master
  1605. writeTrashFile(FILE1, FILE1);
  1606. git.add().addFilepattern(FILE1).call();
  1607. git.commit().setMessage("Add file1").call();
  1608. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1609. // create file2 on master
  1610. writeTrashFile("file2", "file2");
  1611. git.add().addFilepattern("file2").call();
  1612. git.commit().setMessage("Add file2").call();
  1613. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1614. // update FILE1 on master
  1615. writeTrashFile(FILE1, "blah");
  1616. git.add().addFilepattern(FILE1).call();
  1617. git.commit().setMessage("updated file1 on master").call();
  1618. writeTrashFile("file2", "more change");
  1619. git.add().addFilepattern("file2").call();
  1620. git.commit().setMessage("update file2 on side").call();
  1621. RebaseResult res = git.rebase().setUpstream("HEAD~2")
  1622. .runInteractively(new InteractiveHandler() {
  1623. public void prepareSteps(List<RebaseTodoLine> steps) {
  1624. steps.get(0).setAction(Action.REWORD);
  1625. }
  1626. public String modifyCommitMessage(String commit) {
  1627. return "rewritten commit message";
  1628. }
  1629. }).call();
  1630. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1631. checkFile(new File(db.getWorkTree(), "file2"), "more change");
  1632. assertEquals(Status.OK, res.getStatus());
  1633. Iterator<RevCommit> logIterator = git.log().all().call().iterator();
  1634. logIterator.next(); // skip first commit;
  1635. String actualCommitMag = logIterator.next().getShortMessage();
  1636. assertEquals("rewritten commit message", actualCommitMag);
  1637. }
  1638. @Test
  1639. public void testRebaseInteractiveEdit() throws Exception {
  1640. // create file1 on master
  1641. writeTrashFile(FILE1, FILE1);
  1642. git.add().addFilepattern(FILE1).call();
  1643. git.commit().setMessage("Add file1").call();
  1644. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1645. // create file2 on master
  1646. writeTrashFile("file2", "file2");
  1647. git.add().addFilepattern("file2").call();
  1648. git.commit().setMessage("Add file2").call();
  1649. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1650. // update FILE1 on master
  1651. writeTrashFile(FILE1, "blah");
  1652. git.add().addFilepattern(FILE1).call();
  1653. git.commit().setMessage("updated file1 on master").call();
  1654. writeTrashFile("file2", "more change");
  1655. git.add().addFilepattern("file2").call();
  1656. git.commit().setMessage("update file2 on side").call();
  1657. RebaseResult res = git.rebase().setUpstream("HEAD~2")
  1658. .runInteractively(new InteractiveHandler() {
  1659. public void prepareSteps(List<RebaseTodoLine> steps) {
  1660. steps.get(0).setAction(Action.EDIT);
  1661. }
  1662. public String modifyCommitMessage(String commit) {
  1663. return ""; // not used
  1664. }
  1665. }).call();
  1666. assertEquals(Status.EDIT, res.getStatus());
  1667. RevCommit toBeEditted = git.log().call().iterator().next();
  1668. assertEquals("updated file1 on master", toBeEditted.getFullMessage());
  1669. // change file and commit with new commit message
  1670. writeTrashFile("file1", "edited");
  1671. git.commit().setAll(true).setAmend(true)
  1672. .setMessage("edited commit message").call();
  1673. // resume rebase
  1674. res = git.rebase().setOperation(Operation.CONTINUE).call();
  1675. checkFile(new File(db.getWorkTree(), "file1"), "edited");
  1676. assertEquals(Status.OK, res.getStatus());
  1677. Iterator<RevCommit> logIterator = git.log().all().call().iterator();
  1678. logIterator.next(); // skip first commit;
  1679. String actualCommitMag = logIterator.next().getShortMessage();
  1680. assertEquals("edited commit message", actualCommitMag);
  1681. }
  1682. @Test
  1683. public void testParseSquashFixupSequenceCount() {
  1684. int count = RebaseCommand
  1685. .parseSquashFixupSequenceCount("# This is a combination of 3 commits.\n# newline");
  1686. assertEquals(3, count);
  1687. }
  1688. @Test
  1689. public void testRebaseInteractiveSingleSquashAndModifyMessage() throws Exception {
  1690. // create file1 on master
  1691. writeTrashFile(FILE1, FILE1);
  1692. git.add().addFilepattern(FILE1).call();
  1693. git.commit().setMessage("Add file1\nnew line").call();
  1694. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1695. // create file2 on master
  1696. writeTrashFile("file2", "file2");
  1697. git.add().addFilepattern("file2").call();
  1698. git.commit().setMessage("Add file2\nnew line").call();
  1699. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1700. // update FILE1 on master
  1701. writeTrashFile(FILE1, "blah");
  1702. git.add().addFilepattern(FILE1).call();
  1703. git.commit().setMessage("updated file1 on master\nnew line").call();
  1704. writeTrashFile("file2", "more change");
  1705. git.add().addFilepattern("file2").call();
  1706. git.commit().setMessage("update file2 on master\nnew line").call();
  1707. git.rebase().setUpstream("HEAD~3")
  1708. .runInteractively(new InteractiveHandler() {
  1709. public void prepareSteps(List<RebaseTodoLine> steps) {
  1710. steps.get(1).setAction(Action.SQUASH);
  1711. }
  1712. public String modifyCommitMessage(String commit) {
  1713. final File messageSquashFile = new File(db
  1714. .getDirectory(), "rebase-merge/message-squash");
  1715. final File messageFixupFile = new File(db
  1716. .getDirectory(), "rebase-merge/message-fixup");
  1717. assertFalse(messageFixupFile.exists());
  1718. assertTrue(messageSquashFile.exists());
  1719. assertEquals(
  1720. "# This is a combination of 2 commits.\n# This is the 2nd commit message:\nupdated file1 on master\nnew line\n# The first commit's message is:\nAdd file2\nnew line",
  1721. commit);
  1722. try {
  1723. byte[] messageSquashBytes = IO
  1724. .readFully(messageSquashFile);
  1725. int end = RawParseUtils.prevLF(messageSquashBytes,
  1726. messageSquashBytes.length);
  1727. String messageSquashContent = RawParseUtils.decode(
  1728. messageSquashBytes, 0, end + 1);
  1729. assertEquals(messageSquashContent, commit);
  1730. } catch (Throwable t) {
  1731. fail(t.getMessage());
  1732. }
  1733. return "changed";
  1734. }
  1735. }).call();
  1736. RevWalk walk = new RevWalk(db);
  1737. ObjectId headId = db.resolve(Constants.HEAD);
  1738. RevCommit headCommit = walk.parseCommit(headId);
  1739. assertEquals(headCommit.getFullMessage(),
  1740. "update file2 on master\nnew line");
  1741. ObjectId head2Id = db.resolve(Constants.HEAD + "^1");
  1742. RevCommit head1Commit = walk.parseCommit(head2Id);
  1743. assertEquals("changed", head1Commit.getFullMessage());
  1744. }
  1745. @Test
  1746. public void testRebaseInteractiveMultipleSquash() throws Exception {
  1747. // create file0 on master
  1748. writeTrashFile("file0", "file0");
  1749. git.add().addFilepattern("file0").call();
  1750. git.commit().setMessage("Add file0\nnew line").call();
  1751. assertTrue(new File(db.getWorkTree(), "file0").exists());
  1752. // create file1 on master
  1753. writeTrashFile(FILE1, FILE1);
  1754. git.add().addFilepattern(FILE1).call();
  1755. git.commit().setMessage("Add file1\nnew line").call();
  1756. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1757. // create file2 on master
  1758. writeTrashFile("file2", "file2");
  1759. git.add().addFilepattern("file2").call();
  1760. git.commit().setMessage("Add file2\nnew line").call();
  1761. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1762. // update FILE1 on master
  1763. writeTrashFile(FILE1, "blah");
  1764. git.add().addFilepattern(FILE1).call();
  1765. git.commit().setMessage("updated file1 on master\nnew line").call();
  1766. writeTrashFile("file2", "more change");
  1767. git.add().addFilepattern("file2").call();
  1768. git.commit().setMessage("update file2 on master\nnew line").call();
  1769. git.rebase().setUpstream("HEAD~4")
  1770. .runInteractively(new InteractiveHandler() {
  1771. public void prepareSteps(List<RebaseTodoLine> steps) {
  1772. steps.get(1).setAction(Action.SQUASH);
  1773. steps.get(2).setAction(Action.SQUASH);
  1774. }
  1775. public String modifyCommitMessage(String commit) {
  1776. final File messageSquashFile = new File(db.getDirectory(),
  1777. "rebase-merge/message-squash");
  1778. final File messageFixupFile = new File(db.getDirectory(),
  1779. "rebase-merge/message-fixup");
  1780. assertFalse(messageFixupFile.exists());
  1781. assertTrue(messageSquashFile.exists());
  1782. assertEquals(
  1783. "# This is a combination of 3 commits.\n# This is the 3rd commit message:\nupdated file1 on master\nnew line\n# This is the 2nd commit message:\nAdd file2\nnew line\n# The first commit's message is:\nAdd file1\nnew line",
  1784. commit);
  1785. try {
  1786. byte[] messageSquashBytes = IO
  1787. .readFully(messageSquashFile);
  1788. int end = RawParseUtils.prevLF(messageSquashBytes,
  1789. messageSquashBytes.length);
  1790. String messageSquashContend = RawParseUtils.decode(
  1791. messageSquashBytes, 0, end + 1);
  1792. assertEquals(messageSquashContend, commit);
  1793. } catch (Throwable t) {
  1794. fail(t.getMessage());
  1795. }
  1796. return "# This is a combination of 3 commits.\n# This is the 3rd commit message:\nupdated file1 on master\nnew line\n# This is the 2nd commit message:\nAdd file2\nnew line\n# The first commit's message is:\nAdd file1\nnew line";
  1797. }
  1798. }).call();
  1799. RevWalk walk = new RevWalk(db);
  1800. ObjectId headId = db.resolve(Constants.HEAD);
  1801. RevCommit headCommit = walk.parseCommit(headId);
  1802. assertEquals(headCommit.getFullMessage(),
  1803. "update file2 on master\nnew line");
  1804. ObjectId head2Id = db.resolve(Constants.HEAD + "^1");
  1805. RevCommit head1Commit = walk.parseCommit(head2Id);
  1806. assertEquals(
  1807. "updated file1 on master\nnew line\nAdd file2\nnew line\nAdd file1\nnew line",
  1808. head1Commit.getFullMessage());
  1809. }
  1810. @Test
  1811. public void testRebaseInteractiveMixedSquashAndFixup() throws Exception {
  1812. // create file0 on master
  1813. writeTrashFile("file0", "file0");
  1814. git.add().addFilepattern("file0").call();
  1815. git.commit().setMessage("Add file0\nnew line").call();
  1816. assertTrue(new File(db.getWorkTree(), "file0").exists());
  1817. // create file1 on master
  1818. writeTrashFile(FILE1, FILE1);
  1819. git.add().addFilepattern(FILE1).call();
  1820. git.commit().setMessage("Add file1\nnew line").call();
  1821. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1822. // create file2 on master
  1823. writeTrashFile("file2", "file2");
  1824. git.add().addFilepattern("file2").call();
  1825. git.commit().setMessage("Add file2\nnew line").call();
  1826. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1827. // update FILE1 on master
  1828. writeTrashFile(FILE1, "blah");
  1829. git.add().addFilepattern(FILE1).call();
  1830. git.commit().setMessage("updated file1 on master\nnew line").call();
  1831. writeTrashFile("file2", "more change");
  1832. git.add().addFilepattern("file2").call();
  1833. git.commit().setMessage("update file2 on master\nnew line").call();
  1834. git.rebase().setUpstream("HEAD~4")
  1835. .runInteractively(new InteractiveHandler() {
  1836. public void prepareSteps(List<RebaseTodoLine> steps) {
  1837. steps.get(1).setAction(Action.FIXUP);
  1838. steps.get(2).setAction(Action.SQUASH);
  1839. }
  1840. public String modifyCommitMessage(String commit) {
  1841. final File messageSquashFile = new File(db
  1842. .getDirectory(), "rebase-merge/message-squash");
  1843. final File messageFixupFile = new File(db
  1844. .getDirectory(), "rebase-merge/message-fixup");
  1845. assertFalse(messageFixupFile.exists());
  1846. assertTrue(messageSquashFile.exists());
  1847. assertEquals(
  1848. "# This is a combination of 3 commits.\n# This is the 3rd commit message:\nupdated file1 on master\nnew line\n# The 2nd commit message will be skipped:\n# Add file2\n# new line\n# The first commit's message is:\nAdd file1\nnew line",
  1849. commit);
  1850. try {
  1851. byte[] messageSquashBytes = IO
  1852. .readFully(messageSquashFile);
  1853. int end = RawParseUtils.prevLF(messageSquashBytes,
  1854. messageSquashBytes.length);
  1855. String messageSquashContend = RawParseUtils.decode(
  1856. messageSquashBytes, 0, end + 1);
  1857. assertEquals(messageSquashContend, commit);
  1858. } catch (Throwable t) {
  1859. fail(t.getMessage());
  1860. }
  1861. return "changed";
  1862. }
  1863. }).call();
  1864. RevWalk walk = new RevWalk(db);
  1865. ObjectId headId = db.resolve(Constants.HEAD);
  1866. RevCommit headCommit = walk.parseCommit(headId);
  1867. assertEquals(headCommit.getFullMessage(),
  1868. "update file2 on master\nnew line");
  1869. ObjectId head2Id = db.resolve(Constants.HEAD + "^1");
  1870. RevCommit head1Commit = walk.parseCommit(head2Id);
  1871. assertEquals("changed", head1Commit.getFullMessage());
  1872. }
  1873. @Test
  1874. public void testRebaseInteractiveSingleFixup() throws Exception {
  1875. // create file1 on master
  1876. writeTrashFile(FILE1, FILE1);
  1877. git.add().addFilepattern(FILE1).call();
  1878. git.commit().setMessage("Add file1\nnew line").call();
  1879. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1880. // create file2 on master
  1881. writeTrashFile("file2", "file2");
  1882. git.add().addFilepattern("file2").call();
  1883. git.commit().setMessage("Add file2\nnew line").call();
  1884. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1885. // update FILE1 on master
  1886. writeTrashFile(FILE1, "blah");
  1887. git.add().addFilepattern(FILE1).call();
  1888. git.commit().setMessage("updated file1 on master\nnew line").call();
  1889. writeTrashFile("file2", "more change");
  1890. git.add().addFilepattern("file2").call();
  1891. git.commit().setMessage("update file2 on master\nnew line").call();
  1892. git.rebase().setUpstream("HEAD~3")
  1893. .runInteractively(new InteractiveHandler() {
  1894. public void prepareSteps(List<RebaseTodoLine> steps) {
  1895. steps.get(1).setAction(Action.FIXUP);
  1896. }
  1897. public String modifyCommitMessage(String commit) {
  1898. fail("No callback to modify commit message expected for single fixup");
  1899. return commit;
  1900. }
  1901. }).call();
  1902. RevWalk walk = new RevWalk(db);
  1903. ObjectId headId = db.resolve(Constants.HEAD);
  1904. RevCommit headCommit = walk.parseCommit(headId);
  1905. assertEquals("update file2 on master\nnew line",
  1906. headCommit.getFullMessage());
  1907. ObjectId head1Id = db.resolve(Constants.HEAD + "^1");
  1908. RevCommit head1Commit = walk.parseCommit(head1Id);
  1909. assertEquals("Add file2\nnew line",
  1910. head1Commit.getFullMessage());
  1911. }
  1912. @Test(expected = InvalidRebaseStepException.class)
  1913. public void testRebaseInteractiveFixupFirstCommitShouldFail()
  1914. throws Exception {
  1915. // create file1 on master
  1916. writeTrashFile(FILE1, FILE1);
  1917. git.add().addFilepattern(FILE1).call();
  1918. git.commit().setMessage("Add file1\nnew line").call();
  1919. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1920. // create file2 on master
  1921. writeTrashFile("file2", "file2");
  1922. git.add().addFilepattern("file2").call();
  1923. git.commit().setMessage("Add file2\nnew line").call();
  1924. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1925. git.rebase().setUpstream("HEAD~1")
  1926. .runInteractively(new InteractiveHandler() {
  1927. public void prepareSteps(List<RebaseTodoLine> steps) {
  1928. steps.get(0).setAction(Action.FIXUP);
  1929. }
  1930. public String modifyCommitMessage(String commit) {
  1931. return commit;
  1932. }
  1933. }).call();
  1934. }
  1935. @Test(expected = InvalidRebaseStepException.class)
  1936. public void testRebaseInteractiveSquashFirstCommitShouldFail()
  1937. throws Exception {
  1938. // create file1 on master
  1939. writeTrashFile(FILE1, FILE1);
  1940. git.add().addFilepattern(FILE1).call();
  1941. git.commit().setMessage("Add file1\nnew line").call();
  1942. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1943. // create file2 on master
  1944. writeTrashFile("file2", "file2");
  1945. git.add().addFilepattern("file2").call();
  1946. git.commit().setMessage("Add file2\nnew line").call();
  1947. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1948. git.rebase().setUpstream("HEAD~1")
  1949. .runInteractively(new InteractiveHandler() {
  1950. public void prepareSteps(List<RebaseTodoLine> steps) {
  1951. steps.get(0).setAction(Action.SQUASH);
  1952. }
  1953. public String modifyCommitMessage(String commit) {
  1954. return commit;
  1955. }
  1956. }).call();
  1957. }
  1958. @Test
  1959. public void testRebaseEndsIfLastStepIsEdit() throws Exception {
  1960. // create file1 on master
  1961. writeTrashFile(FILE1, FILE1);
  1962. git.add().addFilepattern(FILE1).call();
  1963. git.commit().setMessage("Add file1\nnew line").call();
  1964. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1965. // create file2 on master
  1966. writeTrashFile("file2", "file2");
  1967. git.add().addFilepattern("file2").call();
  1968. git.commit().setMessage("Add file2\nnew line").call();
  1969. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1970. git.rebase().setUpstream("HEAD~1")
  1971. .runInteractively(new InteractiveHandler() {
  1972. public void prepareSteps(List<RebaseTodoLine> steps) {
  1973. steps.get(0).setAction(Action.EDIT);
  1974. }
  1975. public String modifyCommitMessage(String commit) {
  1976. return commit;
  1977. }
  1978. }).call();
  1979. git.commit().setAmend(true)
  1980. .setMessage("Add file2\nnew line\nanother line").call();
  1981. RebaseResult result = git.rebase().setOperation(Operation.CONTINUE)
  1982. .call();
  1983. assertEquals(Status.OK, result.getStatus());
  1984. }
  1985. @Test
  1986. public void testRebaseShouldStopForEditInCaseOfConflict()
  1987. throws Exception {
  1988. // create file1 on master
  1989. writeTrashFile(FILE1, FILE1);
  1990. git.add().addFilepattern(FILE1).call();
  1991. git.commit().setMessage("Add file1\nnew line").call();
  1992. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1993. //change file1
  1994. writeTrashFile(FILE1, FILE1 + "a");
  1995. git.add().addFilepattern(FILE1).call();
  1996. git.commit().setMessage("Change file1").call();
  1997. //change file1
  1998. writeTrashFile(FILE1, FILE1 + "b");
  1999. git.add().addFilepattern(FILE1).call();
  2000. git.commit().setMessage("Change file1").call();
  2001. RebaseResult result = git.rebase().setUpstream("HEAD~2")
  2002. .runInteractively(new InteractiveHandler() {
  2003. public void prepareSteps(List<RebaseTodoLine> steps) {
  2004. steps.remove(0);
  2005. steps.get(0).setAction(Action.EDIT);
  2006. }
  2007. public String modifyCommitMessage(String commit) {
  2008. return commit;
  2009. }
  2010. }).call();
  2011. assertEquals(Status.STOPPED, result.getStatus());
  2012. git.add().addFilepattern(FILE1).call();
  2013. result = git.rebase().setOperation(Operation.CONTINUE).call();
  2014. assertEquals(Status.EDIT, result.getStatus());
  2015. }
  2016. @Test
  2017. public void testRebaseShouldStopForRewordInCaseOfConflict()
  2018. throws Exception {
  2019. // create file1 on master
  2020. writeTrashFile(FILE1, FILE1);
  2021. git.add().addFilepattern(FILE1).call();
  2022. git.commit().setMessage("Add file1\nnew line").call();
  2023. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  2024. // change file1
  2025. writeTrashFile(FILE1, FILE1 + "a");
  2026. git.add().addFilepattern(FILE1).call();
  2027. git.commit().setMessage("Change file1").call();
  2028. // change file1
  2029. writeTrashFile(FILE1, FILE1 + "b");
  2030. git.add().addFilepattern(FILE1).call();
  2031. git.commit().setMessage("Change file1").call();
  2032. RebaseResult result = git.rebase().setUpstream("HEAD~2")
  2033. .runInteractively(new InteractiveHandler() {
  2034. public void prepareSteps(List<RebaseTodoLine> steps) {
  2035. steps.remove(0);
  2036. steps.get(0).setAction(Action.REWORD);
  2037. }
  2038. public String modifyCommitMessage(String commit) {
  2039. return "rewritten commit message";
  2040. }
  2041. }).call();
  2042. assertEquals(Status.STOPPED, result.getStatus());
  2043. git.add().addFilepattern(FILE1).call();
  2044. result = git.rebase().runInteractively(new InteractiveHandler() {
  2045. public void prepareSteps(List<RebaseTodoLine> steps) {
  2046. steps.remove(0);
  2047. steps.get(0).setAction(Action.REWORD);
  2048. }
  2049. public String modifyCommitMessage(String commit) {
  2050. return "rewritten commit message";
  2051. }
  2052. }).setOperation(Operation.CONTINUE).call();
  2053. assertEquals(Status.OK, result.getStatus());
  2054. Iterator<RevCommit> logIterator = git.log().all().call().iterator();
  2055. String actualCommitMag = logIterator.next().getShortMessage();
  2056. assertEquals("rewritten commit message", actualCommitMag);
  2057. }
  2058. @Test
  2059. public void testRebaseShouldSquashInCaseOfConflict() throws Exception {
  2060. // create file1 on master
  2061. writeTrashFile(FILE1, FILE1);
  2062. git.add().addFilepattern(FILE1).call();
  2063. git.commit().setMessage("Add file1\nnew line").call();
  2064. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  2065. // change file2
  2066. writeTrashFile("file2", "file2");
  2067. git.add().addFilepattern("file2").call();
  2068. git.commit().setMessage("Change file2").call();
  2069. // change file1
  2070. writeTrashFile(FILE1, FILE1 + "a");
  2071. git.add().addFilepattern(FILE1).call();
  2072. git.commit().setMessage("Change file1").call();
  2073. // change file1
  2074. writeTrashFile(FILE1, FILE1 + "b");
  2075. git.add().addFilepattern(FILE1).call();
  2076. git.commit().setMessage("Change file1").call();
  2077. RebaseResult result = git.rebase().setUpstream("HEAD~3")
  2078. .runInteractively(new InteractiveHandler() {
  2079. public void prepareSteps(List<RebaseTodoLine> steps) {
  2080. steps.get(0).setAction(Action.PICK);
  2081. steps.remove(1);
  2082. steps.get(1).setAction(Action.SQUASH);
  2083. }
  2084. public String modifyCommitMessage(String commit) {
  2085. return "squashed message";
  2086. }
  2087. }).call();
  2088. assertEquals(Status.STOPPED, result.getStatus());
  2089. git.add().addFilepattern(FILE1).call();
  2090. result = git.rebase().runInteractively(new InteractiveHandler() {
  2091. public void prepareSteps(List<RebaseTodoLine> steps) {
  2092. steps.get(0).setAction(Action.PICK);
  2093. steps.remove(1);
  2094. steps.get(1).setAction(Action.SQUASH);
  2095. }
  2096. public String modifyCommitMessage(String commit) {
  2097. return "squashed message";
  2098. }
  2099. }).setOperation(Operation.CONTINUE).call();
  2100. assertEquals(Status.OK, result.getStatus());
  2101. Iterator<RevCommit> logIterator = git.log().all().call().iterator();
  2102. String actualCommitMag = logIterator.next().getShortMessage();
  2103. assertEquals("squashed message", actualCommitMag);
  2104. }
  2105. @Test
  2106. public void testRebaseShouldFixupInCaseOfConflict() throws Exception {
  2107. // create file1 on master
  2108. writeTrashFile(FILE1, FILE1);
  2109. git.add().addFilepattern(FILE1).call();
  2110. git.commit().setMessage("Add file1").call();
  2111. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  2112. // change file2
  2113. writeTrashFile("file2", "file2");
  2114. git.add().addFilepattern("file2").call();
  2115. git.commit().setMessage("Change file2").call();
  2116. // change file1
  2117. writeTrashFile(FILE1, FILE1 + "a");
  2118. git.add().addFilepattern(FILE1).call();
  2119. git.commit().setMessage("Change file1").call();
  2120. // change file1, add file3
  2121. writeTrashFile(FILE1, FILE1 + "b");
  2122. writeTrashFile("file3", "file3");
  2123. git.add().addFilepattern(FILE1).call();
  2124. git.add().addFilepattern("file3").call();
  2125. git.commit().setMessage("Change file1, add file3").call();
  2126. RebaseResult result = git.rebase().setUpstream("HEAD~3")
  2127. .runInteractively(new InteractiveHandler() {
  2128. public void prepareSteps(List<RebaseTodoLine> steps) {
  2129. steps.get(0).setAction(Action.PICK);
  2130. steps.remove(1);
  2131. steps.get(1).setAction(Action.FIXUP);
  2132. }
  2133. public String modifyCommitMessage(String commit) {
  2134. return commit;
  2135. }
  2136. }).call();
  2137. assertEquals(Status.STOPPED, result.getStatus());
  2138. git.add().addFilepattern(FILE1).call();
  2139. result = git.rebase().runInteractively(new InteractiveHandler() {
  2140. public void prepareSteps(List<RebaseTodoLine> steps) {
  2141. steps.get(0).setAction(Action.PICK);
  2142. steps.remove(1);
  2143. steps.get(1).setAction(Action.FIXUP);
  2144. }
  2145. public String modifyCommitMessage(String commit) {
  2146. return "commit";
  2147. }
  2148. }).setOperation(Operation.CONTINUE).call();
  2149. assertEquals(Status.OK, result.getStatus());
  2150. Iterator<RevCommit> logIterator = git.log().all().call().iterator();
  2151. String actualCommitMsg = logIterator.next().getShortMessage();
  2152. assertEquals("Change file2", actualCommitMsg);
  2153. actualCommitMsg = logIterator.next().getShortMessage();
  2154. assertEquals("Add file1", actualCommitMsg);
  2155. assertTrue(new File(db.getWorkTree(), "file3").exists());
  2156. }
  2157. private File getTodoFile() {
  2158. File todoFile = new File(db.getDirectory(), GIT_REBASE_TODO);
  2159. return todoFile;
  2160. }
  2161. }