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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748
  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.MatcherAssert.assertThat;
  45. import static org.hamcrest.CoreMatchers.equalTo;
  46. import static org.hamcrest.CoreMatchers.not;
  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.Action;
  61. import org.eclipse.jgit.api.RebaseCommand.InteractiveHandler;
  62. import org.eclipse.jgit.api.RebaseCommand.Operation;
  63. import org.eclipse.jgit.api.RebaseCommand.Step;
  64. import org.eclipse.jgit.api.RebaseResult.Status;
  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.Constants;
  71. import org.eclipse.jgit.lib.ObjectId;
  72. import org.eclipse.jgit.lib.PersonIdent;
  73. import org.eclipse.jgit.lib.RefUpdate;
  74. import org.eclipse.jgit.lib.ReflogEntry;
  75. import org.eclipse.jgit.lib.RepositoryState;
  76. import org.eclipse.jgit.merge.MergeStrategy;
  77. import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason;
  78. import org.eclipse.jgit.revwalk.RevCommit;
  79. import org.eclipse.jgit.revwalk.RevWalk;
  80. import org.eclipse.jgit.util.FileUtils;
  81. import org.junit.Before;
  82. import org.junit.Test;
  83. public class RebaseCommandTest extends RepositoryTestCase {
  84. private static final String FILE1 = "file1";
  85. protected Git git;
  86. @Override
  87. @Before
  88. public void setUp() throws Exception {
  89. super.setUp();
  90. this.git = new Git(db);
  91. }
  92. private void checkoutCommit(RevCommit commit) throws IllegalStateException,
  93. IOException {
  94. RevWalk walk = new RevWalk(db);
  95. RevCommit head = walk.parseCommit(db.resolve(Constants.HEAD));
  96. DirCacheCheckout dco = new DirCacheCheckout(db, head.getTree(), db
  97. .lockDirCache(), commit.getTree());
  98. dco.setFailOnConflict(true);
  99. dco.checkout();
  100. walk.release();
  101. // update the HEAD
  102. RefUpdate refUpdate = db.updateRef(Constants.HEAD, true);
  103. refUpdate.setNewObjectId(commit);
  104. refUpdate.setRefLogMessage("checkout: moving to " + head.getName(),
  105. false);
  106. refUpdate.forceUpdate();
  107. }
  108. @Test
  109. public void testFastForwardWithNewFile() throws Exception {
  110. // create file1 on master
  111. writeTrashFile(FILE1, FILE1);
  112. git.add().addFilepattern(FILE1).call();
  113. RevCommit first = git.commit().setMessage("Add file1").call();
  114. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  115. // create a topic branch
  116. createBranch(first, "refs/heads/topic");
  117. // create file2 on master
  118. File file2 = writeTrashFile("file2", "file2");
  119. git.add().addFilepattern("file2").call();
  120. RevCommit second = git.commit().setMessage("Add file2").call();
  121. assertTrue(new File(db.getWorkTree(), "file2").exists());
  122. checkoutBranch("refs/heads/topic");
  123. assertFalse(new File(db.getWorkTree(), "file2").exists());
  124. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  125. assertTrue(new File(db.getWorkTree(), "file2").exists());
  126. checkFile(file2, "file2");
  127. assertEquals(Status.FAST_FORWARD, res.getStatus());
  128. List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
  129. .getReverseEntries();
  130. List<ReflogEntry> topicLog = db.getReflogReader("refs/heads/topic")
  131. .getReverseEntries();
  132. List<ReflogEntry> masterLog = db.getReflogReader("refs/heads/master")
  133. .getReverseEntries();
  134. assertEquals("rebase finished: returning to refs/heads/topic", headLog
  135. .get(0).getComment());
  136. assertEquals("checkout: moving from topic to " + second.getName(),
  137. headLog.get(1).getComment());
  138. assertEquals(2, masterLog.size());
  139. assertEquals(2, topicLog.size());
  140. assertEquals(
  141. "rebase finished: refs/heads/topic onto " + second.getName(),
  142. topicLog.get(0).getComment());
  143. }
  144. @Test
  145. public void testFastForwardWithMultipleCommits() throws Exception {
  146. // create file1 on master
  147. writeTrashFile(FILE1, FILE1);
  148. git.add().addFilepattern(FILE1).call();
  149. RevCommit first = git.commit().setMessage("Add file1").call();
  150. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  151. // create a topic branch
  152. createBranch(first, "refs/heads/topic");
  153. // create file2 on master
  154. File file2 = writeTrashFile("file2", "file2");
  155. git.add().addFilepattern("file2").call();
  156. git.commit().setMessage("Add file2").call();
  157. assertTrue(new File(db.getWorkTree(), "file2").exists());
  158. // write a second commit
  159. writeTrashFile("file2", "file2 new content");
  160. git.add().addFilepattern("file2").call();
  161. RevCommit second = git.commit().setMessage("Change content of file2")
  162. .call();
  163. checkoutBranch("refs/heads/topic");
  164. assertFalse(new File(db.getWorkTree(), "file2").exists());
  165. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  166. assertTrue(new File(db.getWorkTree(), "file2").exists());
  167. checkFile(file2, "file2 new content");
  168. assertEquals(Status.FAST_FORWARD, res.getStatus());
  169. List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
  170. .getReverseEntries();
  171. List<ReflogEntry> topicLog = db.getReflogReader("refs/heads/topic")
  172. .getReverseEntries();
  173. List<ReflogEntry> masterLog = db.getReflogReader("refs/heads/master")
  174. .getReverseEntries();
  175. assertEquals("rebase finished: returning to refs/heads/topic", headLog
  176. .get(0).getComment());
  177. assertEquals("checkout: moving from topic to " + second.getName(),
  178. headLog.get(1).getComment());
  179. assertEquals(3, masterLog.size());
  180. assertEquals(2, topicLog.size());
  181. assertEquals(
  182. "rebase finished: refs/heads/topic onto " + second.getName(),
  183. topicLog.get(0).getComment());
  184. }
  185. /**
  186. * Create the following commits and then attempt to rebase topic onto
  187. * master. This will serialize the branches.
  188. *
  189. * <pre>
  190. * A - B (master)
  191. * \
  192. * C - D - F (topic)
  193. * \ /
  194. * E - (side)
  195. * </pre>
  196. *
  197. * into
  198. *
  199. * <pre>
  200. * A - B - (master) C' - D' - E' (topic')
  201. * \
  202. * C - D - F (topic)
  203. * \ /
  204. * E - (side)
  205. * </pre>
  206. *
  207. * @throws Exception
  208. */
  209. @Test
  210. public void testRebaseShouldIgnoreMergeCommits()
  211. throws Exception {
  212. // create file1 on master
  213. writeTrashFile(FILE1, FILE1);
  214. git.add().addFilepattern(FILE1).call();
  215. RevCommit a = git.commit().setMessage("Add file1").call();
  216. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  217. // create a topic branch
  218. createBranch(a, "refs/heads/topic");
  219. // update FILE1 on master
  220. writeTrashFile(FILE1, "blah");
  221. git.add().addFilepattern(FILE1).call();
  222. RevCommit b = git.commit().setMessage("updated file1 on master").call();
  223. checkoutBranch("refs/heads/topic");
  224. writeTrashFile("file3", "more changess");
  225. git.add().addFilepattern("file3").call();
  226. RevCommit c = git.commit()
  227. .setMessage("update file3 on topic").call();
  228. // create a branch from the topic commit
  229. createBranch(c, "refs/heads/side");
  230. // second commit on topic
  231. writeTrashFile("file2", "file2");
  232. git.add().addFilepattern("file2").call();
  233. RevCommit d = git.commit().setMessage("Add file2").call();
  234. assertTrue(new File(db.getWorkTree(), "file2").exists());
  235. // switch to side branch and update file2
  236. checkoutBranch("refs/heads/side");
  237. writeTrashFile("file3", "more change");
  238. git.add().addFilepattern("file3").call();
  239. RevCommit e = git.commit().setMessage("update file2 on side")
  240. .call();
  241. // switch back to topic and merge in side, creating f
  242. checkoutBranch("refs/heads/topic");
  243. MergeResult result = git.merge().include(e.getId())
  244. .setStrategy(MergeStrategy.RESOLVE).call();
  245. assertEquals(MergeStatus.MERGED, result.getMergeStatus());
  246. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  247. assertEquals(Status.OK, res.getStatus());
  248. RevWalk rw = new RevWalk(db);
  249. rw.markStart(rw.parseCommit(db.resolve("refs/heads/topic")));
  250. assertDerivedFrom(rw.next(), e);
  251. assertDerivedFrom(rw.next(), d);
  252. assertDerivedFrom(rw.next(), c);
  253. assertEquals(b, rw.next());
  254. assertEquals(a, rw.next());
  255. List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
  256. .getReverseEntries();
  257. List<ReflogEntry> sideLog = db.getReflogReader("refs/heads/side")
  258. .getReverseEntries();
  259. List<ReflogEntry> topicLog = db.getReflogReader("refs/heads/topic")
  260. .getReverseEntries();
  261. List<ReflogEntry> masterLog = db.getReflogReader("refs/heads/master")
  262. .getReverseEntries();
  263. assertEquals("rebase finished: returning to refs/heads/topic", headLog
  264. .get(0).getComment());
  265. assertEquals("rebase: update file2 on side", headLog.get(1)
  266. .getComment());
  267. assertEquals("rebase: Add file2", headLog.get(2).getComment());
  268. assertEquals("rebase: update file3 on topic", headLog.get(3)
  269. .getComment());
  270. assertEquals("checkout: moving from topic to " + b.getName(), headLog
  271. .get(4).getComment());
  272. assertEquals(2, masterLog.size());
  273. assertEquals(2, sideLog.size());
  274. assertEquals(5, topicLog.size());
  275. assertEquals("rebase finished: refs/heads/topic onto " + b.getName(),
  276. topicLog.get(0).getComment());
  277. }
  278. static void assertDerivedFrom(RevCommit derived, RevCommit original) {
  279. assertThat(derived, not(equalTo(original)));
  280. assertEquals(original.getFullMessage(), derived.getFullMessage());
  281. }
  282. @Test
  283. public void testRebaseParentOntoHeadShouldBeUptoDate() throws Exception {
  284. writeTrashFile(FILE1, FILE1);
  285. git.add().addFilepattern(FILE1).call();
  286. RevCommit parent = git.commit().setMessage("parent comment").call();
  287. writeTrashFile(FILE1, "another change");
  288. git.add().addFilepattern(FILE1).call();
  289. git.commit().setMessage("head commit").call();
  290. RebaseResult result = git.rebase().setUpstream(parent).call();
  291. assertEquals(Status.UP_TO_DATE, result.getStatus());
  292. assertEquals(2, db.getReflogReader(Constants.HEAD).getReverseEntries()
  293. .size());
  294. assertEquals(2, db.getReflogReader("refs/heads/master")
  295. .getReverseEntries().size());
  296. }
  297. @Test
  298. public void testUpToDate() throws Exception {
  299. // create file1 on master
  300. writeTrashFile(FILE1, FILE1);
  301. git.add().addFilepattern(FILE1).call();
  302. RevCommit first = git.commit().setMessage("Add file1").call();
  303. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  304. RebaseResult res = git.rebase().setUpstream(first).call();
  305. assertEquals(Status.UP_TO_DATE, res.getStatus());
  306. assertEquals(1, db.getReflogReader(Constants.HEAD).getReverseEntries()
  307. .size());
  308. assertEquals(1, db.getReflogReader("refs/heads/master")
  309. .getReverseEntries().size());
  310. }
  311. @Test
  312. public void testUnknownUpstream() throws Exception {
  313. // create file1 on master
  314. writeTrashFile(FILE1, FILE1);
  315. git.add().addFilepattern(FILE1).call();
  316. git.commit().setMessage("Add file1").call();
  317. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  318. try {
  319. git.rebase().setUpstream("refs/heads/xyz").call();
  320. fail("expected exception was not thrown");
  321. } catch (RefNotFoundException e) {
  322. // expected exception
  323. }
  324. }
  325. @Test
  326. public void testConflictFreeWithSingleFile() throws Exception {
  327. // create file1 on master
  328. File theFile = writeTrashFile(FILE1, "1\n2\n3\n");
  329. git.add().addFilepattern(FILE1).call();
  330. RevCommit second = git.commit().setMessage("Add file1").call();
  331. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  332. // change first line in master and commit
  333. writeTrashFile(FILE1, "1master\n2\n3\n");
  334. checkFile(theFile, "1master\n2\n3\n");
  335. git.add().addFilepattern(FILE1).call();
  336. RevCommit lastMasterChange = git.commit().setMessage(
  337. "change file1 in master").call();
  338. // create a topic branch based on second commit
  339. createBranch(second, "refs/heads/topic");
  340. checkoutBranch("refs/heads/topic");
  341. // we have the old content again
  342. checkFile(theFile, "1\n2\n3\n");
  343. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  344. // change third line in topic branch
  345. writeTrashFile(FILE1, "1\n2\n3\ntopic\n");
  346. git.add().addFilepattern(FILE1).call();
  347. RevCommit origHead = git.commit().setMessage("change file1 in topic")
  348. .call();
  349. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  350. assertEquals(Status.OK, res.getStatus());
  351. checkFile(theFile, "1master\n2\n3\ntopic\n");
  352. // our old branch should be checked out again
  353. assertEquals("refs/heads/topic", db.getFullBranch());
  354. assertEquals(lastMasterChange, new RevWalk(db).parseCommit(
  355. db.resolve(Constants.HEAD)).getParent(0));
  356. assertEquals(origHead, db.readOrigHead());
  357. List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
  358. .getReverseEntries();
  359. List<ReflogEntry> topicLog = db.getReflogReader("refs/heads/topic")
  360. .getReverseEntries();
  361. List<ReflogEntry> masterLog = db.getReflogReader("refs/heads/master")
  362. .getReverseEntries();
  363. assertEquals(2, masterLog.size());
  364. assertEquals(3, topicLog.size());
  365. assertEquals("rebase finished: refs/heads/topic onto "
  366. + lastMasterChange.getName(), topicLog.get(0).getComment());
  367. assertEquals("rebase finished: returning to refs/heads/topic", headLog
  368. .get(0).getComment());
  369. }
  370. @Test
  371. public void testDetachedHead() throws Exception {
  372. // create file1 on master
  373. File theFile = writeTrashFile(FILE1, "1\n2\n3\n");
  374. git.add().addFilepattern(FILE1).call();
  375. RevCommit second = git.commit().setMessage("Add file1").call();
  376. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  377. // change first line in master and commit
  378. writeTrashFile(FILE1, "1master\n2\n3\n");
  379. checkFile(theFile, "1master\n2\n3\n");
  380. git.add().addFilepattern(FILE1).call();
  381. RevCommit lastMasterChange = git.commit().setMessage(
  382. "change file1 in master").call();
  383. // create a topic branch based on second commit
  384. createBranch(second, "refs/heads/topic");
  385. checkoutBranch("refs/heads/topic");
  386. // we have the old content again
  387. checkFile(theFile, "1\n2\n3\n");
  388. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  389. // change third line in topic branch
  390. writeTrashFile(FILE1, "1\n2\n3\ntopic\n");
  391. git.add().addFilepattern(FILE1).call();
  392. RevCommit topicCommit = git.commit()
  393. .setMessage("change file1 in topic").call();
  394. checkoutBranch("refs/heads/master");
  395. checkoutCommit(topicCommit);
  396. assertEquals(topicCommit.getId().getName(), db.getFullBranch());
  397. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  398. assertEquals(Status.OK, res.getStatus());
  399. checkFile(theFile, "1master\n2\n3\ntopic\n");
  400. assertEquals(lastMasterChange, new RevWalk(db).parseCommit(
  401. db.resolve(Constants.HEAD)).getParent(0));
  402. List<ReflogEntry> headLog = db.getReflogReader(Constants.HEAD)
  403. .getReverseEntries();
  404. assertEquals(8, headLog.size());
  405. assertEquals("rebase: change file1 in topic", headLog.get(0)
  406. .getComment());
  407. assertEquals("checkout: moving from " + topicCommit.getName() + " to "
  408. + lastMasterChange.getName(), headLog.get(1).getComment());
  409. }
  410. @Test
  411. public void testFilesAddedFromTwoBranches() throws Exception {
  412. // create file1 on master
  413. writeTrashFile(FILE1, FILE1);
  414. git.add().addFilepattern(FILE1).call();
  415. RevCommit masterCommit = git.commit().setMessage("Add file1 to master")
  416. .call();
  417. // create a branch named file2 and add file2
  418. createBranch(masterCommit, "refs/heads/file2");
  419. checkoutBranch("refs/heads/file2");
  420. writeTrashFile("file2", "file2");
  421. git.add().addFilepattern("file2").call();
  422. RevCommit addFile2 = git.commit().setMessage(
  423. "Add file2 to branch file2").call();
  424. // create a branch named file3 and add file3
  425. createBranch(masterCommit, "refs/heads/file3");
  426. checkoutBranch("refs/heads/file3");
  427. writeTrashFile("file3", "file3");
  428. git.add().addFilepattern("file3").call();
  429. git.commit().setMessage("Add file3 to branch file3").call();
  430. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  431. assertFalse(new File(db.getWorkTree(), "file2").exists());
  432. assertTrue(new File(db.getWorkTree(), "file3").exists());
  433. RebaseResult res = git.rebase().setUpstream("refs/heads/file2").call();
  434. assertEquals(Status.OK, res.getStatus());
  435. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  436. assertTrue(new File(db.getWorkTree(), "file2").exists());
  437. assertTrue(new File(db.getWorkTree(), "file3").exists());
  438. // our old branch should be checked out again
  439. assertEquals("refs/heads/file3", db.getFullBranch());
  440. assertEquals(addFile2, new RevWalk(db).parseCommit(
  441. db.resolve(Constants.HEAD)).getParent(0));
  442. checkoutBranch("refs/heads/file2");
  443. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  444. assertTrue(new File(db.getWorkTree(), "file2").exists());
  445. assertFalse(new File(db.getWorkTree(), "file3").exists());
  446. }
  447. @Test
  448. public void testStopOnConflict() throws Exception {
  449. // create file1 on master
  450. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  451. "2", "3");
  452. // change first line in master
  453. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  454. checkFile(FILE1, "1master", "2", "3");
  455. // create a topic branch based on second commit
  456. createBranch(firstInMaster, "refs/heads/topic");
  457. checkoutBranch("refs/heads/topic");
  458. // we have the old content again
  459. checkFile(FILE1, "1", "2", "3");
  460. // add a line (non-conflicting)
  461. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  462. "3", "topic4");
  463. // change first line (conflicting)
  464. RevCommit conflicting = writeFileAndCommit(FILE1,
  465. "change file1 in topic", "1topic", "2", "3", "topic4");
  466. RevCommit lastTopicCommit = writeFileAndCommit(FILE1,
  467. "change file1 in topic again", "1topic", "2", "3", "topic4");
  468. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  469. assertEquals(Status.STOPPED, res.getStatus());
  470. assertEquals(conflicting, res.getCurrentCommit());
  471. checkFile(FILE1,
  472. "<<<<<<< Upstream, based on master\n1master\n=======\n1topic",
  473. ">>>>>>> e0d1dea change file1 in topic\n2\n3\ntopic4");
  474. assertEquals(RepositoryState.REBASING_INTERACTIVE, db
  475. .getRepositoryState());
  476. assertTrue(new File(db.getDirectory(), "rebase-merge").exists());
  477. // the first one should be included, so we should have left two picks in
  478. // the file
  479. assertEquals(1, countPicks());
  480. // rebase should not succeed in this state
  481. try {
  482. git.rebase().setUpstream("refs/heads/master").call();
  483. fail("Expected exception was not thrown");
  484. } catch (WrongRepositoryStateException e) {
  485. // expected
  486. }
  487. // abort should reset to topic branch
  488. res = git.rebase().setOperation(Operation.ABORT).call();
  489. assertEquals(res.getStatus(), Status.ABORTED);
  490. assertEquals("refs/heads/topic", db.getFullBranch());
  491. checkFile(FILE1, "1topic", "2", "3", "topic4");
  492. RevWalk rw = new RevWalk(db);
  493. assertEquals(lastTopicCommit, rw
  494. .parseCommit(db.resolve(Constants.HEAD)));
  495. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  496. // rebase- dir in .git must be deleted
  497. assertFalse(new File(db.getDirectory(), "rebase-merge").exists());
  498. }
  499. @Test
  500. public void testStopOnConflictAndContinue() throws Exception {
  501. // create file1 on master
  502. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  503. "2", "3");
  504. // change in master
  505. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  506. checkFile(FILE1, "1master", "2", "3");
  507. // create a topic branch based on the first commit
  508. createBranch(firstInMaster, "refs/heads/topic");
  509. checkoutBranch("refs/heads/topic");
  510. // we have the old content again
  511. checkFile(FILE1, "1", "2", "3");
  512. // add a line (non-conflicting)
  513. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  514. "3", "4topic");
  515. // change first line (conflicting)
  516. writeFileAndCommit(FILE1,
  517. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  518. "3", "4topic");
  519. // change second line (not conflicting)
  520. writeFileAndCommit(FILE1, "change file1 in topic again", "1topic",
  521. "2topic", "3", "4topic");
  522. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  523. assertEquals(Status.STOPPED, res.getStatus());
  524. // continue should throw a meaningful exception
  525. try {
  526. res = git.rebase().setOperation(Operation.CONTINUE).call();
  527. fail("Expected Exception not thrown");
  528. } catch (UnmergedPathsException e) {
  529. // expected
  530. }
  531. // merge the file; the second topic commit should go through
  532. writeFileAndAdd(FILE1, "1topic", "2", "3", "4topic");
  533. res = git.rebase().setOperation(Operation.CONTINUE).call();
  534. assertNotNull(res);
  535. assertEquals(Status.OK, res.getStatus());
  536. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  537. ObjectId headId = db.resolve(Constants.HEAD);
  538. RevWalk rw = new RevWalk(db);
  539. RevCommit rc = rw.parseCommit(headId);
  540. RevCommit parent = rw.parseCommit(rc.getParent(0));
  541. assertEquals("change file1 in topic\n\nThis is conflicting", parent
  542. .getFullMessage());
  543. }
  544. @Test
  545. public void testStopOnConflictAndContinueWithNoDeltaToMaster()
  546. throws Exception {
  547. // create file1 on master
  548. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  549. "2", "3");
  550. // change in master
  551. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  552. checkFile(FILE1, "1master", "2", "3");
  553. // create a topic branch based on the first commit
  554. createBranch(firstInMaster, "refs/heads/topic");
  555. checkoutBranch("refs/heads/topic");
  556. // we have the old content again
  557. checkFile(FILE1, "1", "2", "3");
  558. // change first line (conflicting)
  559. writeFileAndCommit(FILE1,
  560. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  561. "3", "4topic");
  562. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  563. assertEquals(Status.STOPPED, res.getStatus());
  564. // continue should throw a meaningful exception
  565. try {
  566. res = git.rebase().setOperation(Operation.CONTINUE).call();
  567. fail("Expected Exception not thrown");
  568. } catch (UnmergedPathsException e) {
  569. // expected
  570. }
  571. // merge the file; the second topic commit should go through
  572. writeFileAndAdd(FILE1, "1master", "2", "3");
  573. res = git.rebase().setOperation(Operation.CONTINUE).call();
  574. assertNotNull(res);
  575. assertEquals(Status.NOTHING_TO_COMMIT, res.getStatus());
  576. assertEquals(RepositoryState.REBASING_INTERACTIVE,
  577. db.getRepositoryState());
  578. git.rebase().setOperation(Operation.SKIP).call();
  579. ObjectId headId = db.resolve(Constants.HEAD);
  580. RevWalk rw = new RevWalk(db);
  581. RevCommit rc = rw.parseCommit(headId);
  582. assertEquals("change file1 in master", rc.getFullMessage());
  583. }
  584. @Test
  585. public void testStopOnConflictAndFailContinueIfFileIsDirty()
  586. throws Exception {
  587. // create file1 on master
  588. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  589. "2", "3");
  590. // change in master
  591. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  592. checkFile(FILE1, "1master", "2", "3");
  593. // create a topic branch based on the first commit
  594. createBranch(firstInMaster, "refs/heads/topic");
  595. checkoutBranch("refs/heads/topic");
  596. // we have the old content again
  597. checkFile(FILE1, "1", "2", "3");
  598. // add a line (non-conflicting)
  599. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  600. "3", "4topic");
  601. // change first line (conflicting)
  602. writeFileAndCommit(FILE1,
  603. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  604. "3", "4topic");
  605. // change second line (not conflicting)
  606. writeFileAndCommit(FILE1, "change file1 in topic again", "1topic",
  607. "2topic", "3", "4topic");
  608. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  609. assertEquals(Status.STOPPED, res.getStatus());
  610. git.add().addFilepattern(FILE1).call();
  611. File trashFile = writeTrashFile(FILE1, "Some local change");
  612. res = git.rebase().setOperation(Operation.CONTINUE).call();
  613. assertNotNull(res);
  614. assertEquals(Status.STOPPED, res.getStatus());
  615. checkFile(trashFile, "Some local change");
  616. }
  617. @Test
  618. public void testStopOnLastConflictAndContinue() throws Exception {
  619. // create file1 on master
  620. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  621. "2", "3");
  622. // change in master
  623. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  624. checkFile(FILE1, "1master", "2", "3");
  625. // create a topic branch based on the first commit
  626. createBranch(firstInMaster, "refs/heads/topic");
  627. checkoutBranch("refs/heads/topic");
  628. // we have the old content again
  629. checkFile(FILE1, "1", "2", "3");
  630. // add a line (non-conflicting)
  631. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  632. "3", "4topic");
  633. // change first line (conflicting)
  634. writeFileAndCommit(FILE1,
  635. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  636. "3", "4topic");
  637. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  638. assertEquals(Status.STOPPED, res.getStatus());
  639. // merge the file; the second topic commit should go through
  640. writeFileAndAdd(FILE1, "1topic", "2", "3", "4topic");
  641. res = git.rebase().setOperation(Operation.CONTINUE).call();
  642. assertNotNull(res);
  643. assertEquals(Status.OK, res.getStatus());
  644. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  645. }
  646. @Test
  647. public void testStopOnLastConflictAndSkip() throws Exception {
  648. // create file1 on master
  649. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  650. "2", "3");
  651. // change in master
  652. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  653. checkFile(FILE1, "1master", "2", "3");
  654. // create a topic branch based on the first commit
  655. createBranch(firstInMaster, "refs/heads/topic");
  656. checkoutBranch("refs/heads/topic");
  657. // we have the old content again
  658. checkFile(FILE1, "1", "2", "3");
  659. // add a line (non-conflicting)
  660. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  661. "3", "4topic");
  662. // change first line (conflicting)
  663. writeFileAndCommit(FILE1,
  664. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  665. "3", "4topic");
  666. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  667. assertEquals(Status.STOPPED, res.getStatus());
  668. // merge the file; the second topic commit should go through
  669. writeFileAndAdd(FILE1, "1topic", "2", "3", "4topic");
  670. res = git.rebase().setOperation(Operation.SKIP).call();
  671. assertNotNull(res);
  672. assertEquals(Status.OK, res.getStatus());
  673. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  674. }
  675. @Test
  676. public void testMergeFirstStopOnLastConflictAndSkip() throws Exception {
  677. // create file1 on master
  678. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  679. "2", "3");
  680. // change in master
  681. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  682. checkFile(FILE1, "1master", "2", "3");
  683. // create a topic branch based on the first commit
  684. createBranch(firstInMaster, "refs/heads/topic");
  685. checkoutBranch("refs/heads/topic");
  686. // we have the old content again
  687. checkFile(FILE1, "1", "2", "3");
  688. // add a line (conflicting)
  689. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1topic",
  690. "2", "3", "4topic");
  691. // change first line (conflicting again)
  692. writeFileAndCommit(FILE1,
  693. "change file1 in topic\n\nThis is conflicting", "1topicagain",
  694. "2", "3", "4topic");
  695. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  696. assertEquals(Status.STOPPED, res.getStatus());
  697. writeFileAndAdd(FILE1, "merged");
  698. res = git.rebase().setOperation(Operation.CONTINUE).call();
  699. assertEquals(Status.STOPPED, res.getStatus());
  700. res = git.rebase().setOperation(Operation.SKIP).call();
  701. assertNotNull(res);
  702. assertEquals(Status.OK, res.getStatus());
  703. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  704. checkFile(FILE1, "merged");
  705. }
  706. @Test
  707. public void testStopOnConflictAndSkipNoConflict() throws Exception {
  708. // create file1 on master
  709. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  710. "2", "3");
  711. // change in master
  712. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  713. checkFile(FILE1, "1master", "2", "3");
  714. // create a topic branch based on the first commit
  715. createBranch(firstInMaster, "refs/heads/topic");
  716. checkoutBranch("refs/heads/topic");
  717. // we have the old content again
  718. checkFile(FILE1, "1", "2", "3");
  719. // add a line (non-conflicting)
  720. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  721. "3", "4topic");
  722. // change first line (conflicting)
  723. writeFileAndCommit(FILE1,
  724. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  725. "3", "4topic");
  726. // change third line (not conflicting)
  727. writeFileAndCommit(FILE1, "change file1 in topic again", "1topic", "2",
  728. "3topic", "4topic");
  729. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  730. assertEquals(Status.STOPPED, res.getStatus());
  731. res = git.rebase().setOperation(Operation.SKIP).call();
  732. checkFile(FILE1, "1master", "2", "3topic", "4topic");
  733. assertEquals(Status.OK, res.getStatus());
  734. }
  735. @Test
  736. public void testStopOnConflictAndSkipWithConflict() throws Exception {
  737. // create file1 on master
  738. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  739. "2", "3", "4");
  740. // change in master
  741. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2",
  742. "3master", "4");
  743. checkFile(FILE1, "1master", "2", "3master", "4");
  744. // create a topic branch based on the first commit
  745. createBranch(firstInMaster, "refs/heads/topic");
  746. checkoutBranch("refs/heads/topic");
  747. // we have the old content again
  748. checkFile(FILE1, "1", "2", "3", "4");
  749. // add a line (non-conflicting)
  750. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  751. "3", "4", "5topic");
  752. // change first line (conflicting)
  753. writeFileAndCommit(FILE1,
  754. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  755. "3", "4", "5topic");
  756. // change third line (conflicting)
  757. writeFileAndCommit(FILE1, "change file1 in topic again", "1topic", "2",
  758. "3topic", "4", "5topic");
  759. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  760. assertEquals(Status.STOPPED, res.getStatus());
  761. res = git.rebase().setOperation(Operation.SKIP).call();
  762. // TODO is this correct? It is what the command line returns
  763. checkFile(
  764. FILE1,
  765. "1master\n2\n<<<<<<< Upstream, based on master\n3master\n=======\n3topic",
  766. ">>>>>>> 5afc8df change file1 in topic again\n4\n5topic");
  767. assertEquals(Status.STOPPED, res.getStatus());
  768. }
  769. @Test
  770. public void testStopOnConflictCommitAndContinue() throws Exception {
  771. // create file1 on master
  772. RevCommit firstInMaster = writeFileAndCommit(FILE1, "Add file1", "1",
  773. "2", "3");
  774. // change in master
  775. writeFileAndCommit(FILE1, "change file1 in master", "1master", "2", "3");
  776. checkFile(FILE1, "1master", "2", "3");
  777. // create a topic branch based on the first commit
  778. createBranch(firstInMaster, "refs/heads/topic");
  779. checkoutBranch("refs/heads/topic");
  780. // we have the old content again
  781. checkFile(FILE1, "1", "2", "3");
  782. // add a line (non-conflicting)
  783. writeFileAndCommit(FILE1, "add a line to file1 in topic", "1", "2",
  784. "3", "4topic");
  785. // change first line (conflicting)
  786. writeFileAndCommit(FILE1,
  787. "change file1 in topic\n\nThis is conflicting", "1topic", "2",
  788. "3", "4topic");
  789. // change second line (not conflicting)
  790. writeFileAndCommit(FILE1, "change file1 in topic again", "1topic", "2",
  791. "3topic", "4topic");
  792. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  793. assertEquals(Status.STOPPED, res.getStatus());
  794. // continue should throw a meaningful exception
  795. try {
  796. res = git.rebase().setOperation(Operation.CONTINUE).call();
  797. fail("Expected Exception not thrown");
  798. } catch (UnmergedPathsException e) {
  799. // expected
  800. }
  801. // merge the file; the second topic commit should go through
  802. writeFileAndCommit(FILE1, "A different commit message", "1topic", "2",
  803. "3", "4topic");
  804. res = git.rebase().setOperation(Operation.CONTINUE).call();
  805. assertNotNull(res);
  806. // nothing to commit. this leaves the repo state in rebase, so that the
  807. // user can decide what to do. if he accidentally committed, reset soft,
  808. // and continue, if he really has nothing to commit, skip.
  809. assertEquals(Status.NOTHING_TO_COMMIT, res.getStatus());
  810. assertEquals(RepositoryState.REBASING_INTERACTIVE,
  811. db.getRepositoryState());
  812. git.rebase().setOperation(Operation.SKIP).call();
  813. ObjectId headId = db.resolve(Constants.HEAD);
  814. RevWalk rw = new RevWalk(db);
  815. RevCommit rc = rw.parseCommit(headId);
  816. RevCommit parent = rw.parseCommit(rc.getParent(0));
  817. assertEquals("A different commit message", parent.getFullMessage());
  818. }
  819. private RevCommit writeFileAndCommit(String fileName, String commitMessage,
  820. String... lines) throws Exception {
  821. StringBuilder sb = new StringBuilder();
  822. for (String line : lines) {
  823. sb.append(line);
  824. sb.append('\n');
  825. }
  826. writeTrashFile(fileName, sb.toString());
  827. git.add().addFilepattern(fileName).call();
  828. return git.commit().setMessage(commitMessage).call();
  829. }
  830. private void writeFileAndAdd(String fileName, String... lines)
  831. throws Exception {
  832. StringBuilder sb = new StringBuilder();
  833. for (String line : lines) {
  834. sb.append(line);
  835. sb.append('\n');
  836. }
  837. writeTrashFile(fileName, sb.toString());
  838. git.add().addFilepattern(fileName).call();
  839. }
  840. private void checkFile(String fileName, String... lines) throws Exception {
  841. File file = new File(db.getWorkTree(), fileName);
  842. StringBuilder sb = new StringBuilder();
  843. for (String line : lines) {
  844. sb.append(line);
  845. sb.append('\n');
  846. }
  847. checkFile(file, sb.toString());
  848. }
  849. @Test
  850. public void testStopOnConflictFileCreationAndDeletion() throws Exception {
  851. // create file1 on master
  852. writeTrashFile(FILE1, "Hello World");
  853. git.add().addFilepattern(FILE1).call();
  854. // create file2 on master
  855. File file2 = writeTrashFile("file2", "Hello World 2");
  856. git.add().addFilepattern("file2").call();
  857. // create file3 on master
  858. File file3 = writeTrashFile("file3", "Hello World 3");
  859. git.add().addFilepattern("file3").call();
  860. RevCommit firstInMaster = git.commit()
  861. .setMessage("Add file 1, 2 and 3").call();
  862. // create file4 on master
  863. File file4 = writeTrashFile("file4", "Hello World 4");
  864. git.add().addFilepattern("file4").call();
  865. deleteTrashFile("file2");
  866. git.add().setUpdate(true).addFilepattern("file2").call();
  867. // create folder folder6 on topic (conflicts with file folder6 on topic
  868. // later on)
  869. writeTrashFile("folder6/file1", "Hello World folder6");
  870. git.add().addFilepattern("folder6/file1").call();
  871. git.commit().setMessage(
  872. "Add file 4 and folder folder6, delete file2 on master").call();
  873. // create a topic branch based on second commit
  874. createBranch(firstInMaster, "refs/heads/topic");
  875. checkoutBranch("refs/heads/topic");
  876. deleteTrashFile("file3");
  877. git.add().setUpdate(true).addFilepattern("file3").call();
  878. // create file5 on topic
  879. File file5 = writeTrashFile("file5", "Hello World 5");
  880. git.add().addFilepattern("file5").call();
  881. git.commit().setMessage("Delete file3 and add file5 in topic").call();
  882. // create file folder6 on topic (conflicts with folder6 on master)
  883. writeTrashFile("folder6", "Hello World 6");
  884. git.add().addFilepattern("folder6").call();
  885. // create file7 on topic
  886. File file7 = writeTrashFile("file7", "Hello World 7");
  887. git.add().addFilepattern("file7").call();
  888. deleteTrashFile("file5");
  889. git.add().setUpdate(true).addFilepattern("file5").call();
  890. RevCommit conflicting = git.commit().setMessage(
  891. "Delete file5, add file folder6 and file7 in topic").call();
  892. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  893. assertEquals(Status.STOPPED, res.getStatus());
  894. assertEquals(conflicting, res.getCurrentCommit());
  895. assertEquals(RepositoryState.REBASING_INTERACTIVE, db
  896. .getRepositoryState());
  897. assertTrue(new File(db.getDirectory(), "rebase-merge").exists());
  898. // the first one should be included, so we should have left two picks in
  899. // the file
  900. assertEquals(0, countPicks());
  901. assertFalse(file2.exists());
  902. assertFalse(file3.exists());
  903. assertTrue(file4.exists());
  904. assertFalse(file5.exists());
  905. assertTrue(file7.exists());
  906. // abort should reset to topic branch
  907. res = git.rebase().setOperation(Operation.ABORT).call();
  908. assertEquals(res.getStatus(), Status.ABORTED);
  909. assertEquals("refs/heads/topic", db.getFullBranch());
  910. RevWalk rw = new RevWalk(db);
  911. assertEquals(conflicting, rw.parseCommit(db.resolve(Constants.HEAD)));
  912. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  913. // rebase- dir in .git must be deleted
  914. assertFalse(new File(db.getDirectory(), "rebase-merge").exists());
  915. assertTrue(file2.exists());
  916. assertFalse(file3.exists());
  917. assertFalse(file4.exists());
  918. assertFalse(file5.exists());
  919. assertTrue(file7.exists());
  920. }
  921. @Test
  922. public void testAuthorScriptConverter() throws Exception {
  923. // -1 h timezone offset
  924. PersonIdent ident = new PersonIdent("Author name", "a.mail@some.com",
  925. 123456789123L, -60);
  926. String convertedAuthor = git.rebase().toAuthorScript(ident);
  927. String[] lines = convertedAuthor.split("\n");
  928. assertEquals("GIT_AUTHOR_NAME='Author name'", lines[0]);
  929. assertEquals("GIT_AUTHOR_EMAIL='a.mail@some.com'", lines[1]);
  930. assertEquals("GIT_AUTHOR_DATE='@123456789 -0100'", lines[2]);
  931. PersonIdent parsedIdent = git.rebase().parseAuthor(
  932. convertedAuthor.getBytes("UTF-8"));
  933. assertEquals(ident.getName(), parsedIdent.getName());
  934. assertEquals(ident.getEmailAddress(), parsedIdent.getEmailAddress());
  935. // this is rounded to the last second
  936. assertEquals(123456789000L, parsedIdent.getWhen().getTime());
  937. assertEquals(ident.getTimeZoneOffset(), parsedIdent.getTimeZoneOffset());
  938. // + 9.5h timezone offset
  939. ident = new PersonIdent("Author name", "a.mail@some.com",
  940. 123456789123L, +570);
  941. convertedAuthor = git.rebase().toAuthorScript(ident);
  942. lines = convertedAuthor.split("\n");
  943. assertEquals("GIT_AUTHOR_NAME='Author name'", lines[0]);
  944. assertEquals("GIT_AUTHOR_EMAIL='a.mail@some.com'", lines[1]);
  945. assertEquals("GIT_AUTHOR_DATE='@123456789 +0930'", lines[2]);
  946. parsedIdent = git.rebase().parseAuthor(
  947. convertedAuthor.getBytes("UTF-8"));
  948. assertEquals(ident.getName(), parsedIdent.getName());
  949. assertEquals(ident.getEmailAddress(), parsedIdent.getEmailAddress());
  950. assertEquals(123456789000L, parsedIdent.getWhen().getTime());
  951. assertEquals(ident.getTimeZoneOffset(), parsedIdent.getTimeZoneOffset());
  952. }
  953. @Test
  954. public void testRepositoryStateChecks() throws Exception {
  955. try {
  956. git.rebase().setOperation(Operation.ABORT).call();
  957. fail("Expected Exception not thrown");
  958. } catch (WrongRepositoryStateException e) {
  959. // expected
  960. }
  961. try {
  962. git.rebase().setOperation(Operation.SKIP).call();
  963. fail("Expected Exception not thrown");
  964. } catch (WrongRepositoryStateException e) {
  965. // expected
  966. }
  967. try {
  968. git.rebase().setOperation(Operation.CONTINUE).call();
  969. fail("Expected Exception not thrown");
  970. } catch (WrongRepositoryStateException e) {
  971. // expected
  972. }
  973. }
  974. @Test
  975. public void testRebaseWithUntrackedFile() throws Exception {
  976. // create file1, add and commit
  977. writeTrashFile(FILE1, "file1");
  978. git.add().addFilepattern(FILE1).call();
  979. RevCommit commit = git.commit().setMessage("commit1").call();
  980. // create topic branch and checkout / create file2, add and commit
  981. createBranch(commit, "refs/heads/topic");
  982. checkoutBranch("refs/heads/topic");
  983. writeTrashFile("file2", "file2");
  984. git.add().addFilepattern("file2").call();
  985. git.commit().setMessage("commit2").call();
  986. // checkout master branch / modify file1, add and commit
  987. checkoutBranch("refs/heads/master");
  988. writeTrashFile(FILE1, "modified file1");
  989. git.add().addFilepattern(FILE1).call();
  990. git.commit().setMessage("commit3").call();
  991. // checkout topic branch / create untracked file3
  992. checkoutBranch("refs/heads/topic");
  993. writeTrashFile("file3", "untracked file3");
  994. // rebase
  995. assertEquals(Status.OK, git.rebase().setUpstream("refs/heads/master")
  996. .call().getStatus());
  997. }
  998. @Test
  999. public void testRebaseWithUnstagedTopicChange() throws Exception {
  1000. // create file1, add and commit
  1001. writeTrashFile(FILE1, "file1");
  1002. git.add().addFilepattern(FILE1).call();
  1003. RevCommit commit = git.commit().setMessage("commit1").call();
  1004. // create topic branch and checkout / create file2, add and commit
  1005. createBranch(commit, "refs/heads/topic");
  1006. checkoutBranch("refs/heads/topic");
  1007. writeTrashFile("file2", "file2");
  1008. git.add().addFilepattern("file2").call();
  1009. git.commit().setMessage("commit2").call();
  1010. // checkout master branch / modify file1, add and commit
  1011. checkoutBranch("refs/heads/master");
  1012. writeTrashFile(FILE1, "modified file1");
  1013. git.add().addFilepattern(FILE1).call();
  1014. git.commit().setMessage("commit3").call();
  1015. // checkout topic branch / modify file2
  1016. checkoutBranch("refs/heads/topic");
  1017. writeTrashFile("file2", "unstaged file2");
  1018. // rebase
  1019. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1020. .call();
  1021. assertEquals(Status.CONFLICTS, result.getStatus());
  1022. assertEquals(1, result.getConflicts().size());
  1023. assertEquals("file2", result.getConflicts().get(0));
  1024. }
  1025. @Test
  1026. public void testRebaseWithUncommittedTopicChange() throws Exception {
  1027. // create file1, add and commit
  1028. writeTrashFile(FILE1, "file1");
  1029. git.add().addFilepattern(FILE1).call();
  1030. RevCommit commit = git.commit().setMessage("commit1").call();
  1031. // create topic branch and checkout / create file2, add and commit
  1032. createBranch(commit, "refs/heads/topic");
  1033. checkoutBranch("refs/heads/topic");
  1034. writeTrashFile("file2", "file2");
  1035. git.add().addFilepattern("file2").call();
  1036. git.commit().setMessage("commit2").call();
  1037. // checkout master branch / modify file1, add and commit
  1038. checkoutBranch("refs/heads/master");
  1039. writeTrashFile(FILE1, "modified file1");
  1040. git.add().addFilepattern(FILE1).call();
  1041. git.commit().setMessage("commit3").call();
  1042. // checkout topic branch / modify file2 and add
  1043. checkoutBranch("refs/heads/topic");
  1044. File uncommittedFile = writeTrashFile("file2", "uncommitted file2");
  1045. git.add().addFilepattern("file2").call();
  1046. // do not commit
  1047. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1048. .call();
  1049. assertEquals(Status.CONFLICTS, result.getStatus());
  1050. assertEquals(1, result.getConflicts().size());
  1051. assertEquals("file2", result.getConflicts().get(0));
  1052. checkFile(uncommittedFile, "uncommitted file2");
  1053. assertEquals(RepositoryState.SAFE, git.getRepository().getRepositoryState());
  1054. }
  1055. @Test
  1056. public void testRebaseWithUnstagedMasterChange() throws Exception {
  1057. // create file1, add and commit
  1058. writeTrashFile(FILE1, "file1");
  1059. git.add().addFilepattern(FILE1).call();
  1060. RevCommit commit = git.commit().setMessage("commit1").call();
  1061. // create topic branch and checkout / create file2, add and commit
  1062. createBranch(commit, "refs/heads/topic");
  1063. checkoutBranch("refs/heads/topic");
  1064. writeTrashFile("file2", "file2");
  1065. git.add().addFilepattern("file2").call();
  1066. git.commit().setMessage("commit2").call();
  1067. // checkout master branch / modify file1, add and commit
  1068. checkoutBranch("refs/heads/master");
  1069. writeTrashFile(FILE1, "modified file1");
  1070. git.add().addFilepattern(FILE1).call();
  1071. git.commit().setMessage("commit3").call();
  1072. // checkout topic branch / modify file1
  1073. checkoutBranch("refs/heads/topic");
  1074. writeTrashFile(FILE1, "unstaged modified file1");
  1075. // rebase
  1076. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1077. .call();
  1078. assertEquals(Status.CONFLICTS, result.getStatus());
  1079. assertEquals(1, result.getConflicts().size());
  1080. assertEquals(FILE1, result.getConflicts().get(0));
  1081. }
  1082. @Test
  1083. public void testRebaseWithUncommittedMasterChange() throws Exception {
  1084. // create file1, add and commit
  1085. writeTrashFile(FILE1, "file1");
  1086. git.add().addFilepattern(FILE1).call();
  1087. RevCommit commit = git.commit().setMessage("commit1").call();
  1088. // create topic branch and checkout / create file2, add and commit
  1089. createBranch(commit, "refs/heads/topic");
  1090. checkoutBranch("refs/heads/topic");
  1091. writeTrashFile("file2", "file2");
  1092. git.add().addFilepattern("file2").call();
  1093. git.commit().setMessage("commit2").call();
  1094. // checkout master branch / modify file1, add and commit
  1095. checkoutBranch("refs/heads/master");
  1096. writeTrashFile(FILE1, "modified file1");
  1097. git.add().addFilepattern(FILE1).call();
  1098. git.commit().setMessage("commit3").call();
  1099. // checkout topic branch / modify file1 and add
  1100. checkoutBranch("refs/heads/topic");
  1101. writeTrashFile(FILE1, "uncommitted modified file1");
  1102. git.add().addFilepattern(FILE1).call();
  1103. // do not commit
  1104. // rebase
  1105. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1106. .call();
  1107. assertEquals(Status.CONFLICTS, result.getStatus());
  1108. assertEquals(1, result.getConflicts().size());
  1109. assertEquals(FILE1, result.getConflicts().get(0));
  1110. }
  1111. @Test
  1112. public void testRebaseWithUnstagedMasterChangeBaseCommit() throws Exception {
  1113. // create file0 + file1, add and commit
  1114. writeTrashFile("file0", "file0");
  1115. writeTrashFile(FILE1, "file1");
  1116. git.add().addFilepattern("file0").addFilepattern(FILE1).call();
  1117. RevCommit commit = git.commit().setMessage("commit1").call();
  1118. // create topic branch and checkout / create file2, add and commit
  1119. createBranch(commit, "refs/heads/topic");
  1120. checkoutBranch("refs/heads/topic");
  1121. writeTrashFile("file2", "file2");
  1122. git.add().addFilepattern("file2").call();
  1123. git.commit().setMessage("commit2").call();
  1124. // checkout master branch / modify file1, add and commit
  1125. checkoutBranch("refs/heads/master");
  1126. writeTrashFile(FILE1, "modified file1");
  1127. git.add().addFilepattern(FILE1).call();
  1128. git.commit().setMessage("commit3").call();
  1129. // checkout topic branch / modify file0
  1130. checkoutBranch("refs/heads/topic");
  1131. writeTrashFile("file0", "unstaged modified file0");
  1132. // rebase
  1133. assertEquals(Status.OK, git.rebase().setUpstream("refs/heads/master")
  1134. .call().getStatus());
  1135. }
  1136. @Test
  1137. public void testRebaseWithUncommittedMasterChangeBaseCommit()
  1138. throws Exception {
  1139. // create file0 + file1, add and commit
  1140. File file0 = writeTrashFile("file0", "file0");
  1141. writeTrashFile(FILE1, "file1");
  1142. git.add().addFilepattern("file0").addFilepattern(FILE1).call();
  1143. RevCommit commit = git.commit().setMessage("commit1").call();
  1144. // create topic branch and checkout / create file2, add and commit
  1145. createBranch(commit, "refs/heads/topic");
  1146. checkoutBranch("refs/heads/topic");
  1147. writeTrashFile("file2", "file2");
  1148. git.add().addFilepattern("file2").call();
  1149. git.commit().setMessage("commit2").call();
  1150. // checkout master branch / modify file1, add and commit
  1151. checkoutBranch("refs/heads/master");
  1152. writeTrashFile(FILE1, "modified file1");
  1153. git.add().addFilepattern(FILE1).call();
  1154. git.commit().setMessage("commit3").call();
  1155. // checkout topic branch / modify file0 and add
  1156. checkoutBranch("refs/heads/topic");
  1157. write(file0, "unstaged modified file0");
  1158. git.add().addFilepattern("file0").call();
  1159. // do not commit
  1160. // get current index state
  1161. String indexState = indexState(CONTENT);
  1162. // rebase
  1163. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1164. .call();
  1165. assertEquals(Status.FAILED, result.getStatus());
  1166. // staged file0 causes DIRTY_INDEX
  1167. assertEquals(1, result.getFailingPaths().size());
  1168. assertEquals(MergeFailureReason.DIRTY_INDEX, result.getFailingPaths()
  1169. .get("file0"));
  1170. assertEquals("unstaged modified file0", read(file0));
  1171. // index shall be unchanged
  1172. assertEquals(indexState, indexState(CONTENT));
  1173. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  1174. }
  1175. @Test
  1176. public void testRebaseWithUnstagedMasterChangeOtherCommit()
  1177. throws Exception {
  1178. // create file0, add and commit
  1179. writeTrashFile("file0", "file0");
  1180. git.add().addFilepattern("file0").call();
  1181. git.commit().setMessage("commit0").call();
  1182. // create file1, add and commit
  1183. writeTrashFile(FILE1, "file1");
  1184. git.add().addFilepattern(FILE1).call();
  1185. RevCommit commit = git.commit().setMessage("commit1").call();
  1186. // create topic branch and checkout / create file2, add and commit
  1187. createBranch(commit, "refs/heads/topic");
  1188. checkoutBranch("refs/heads/topic");
  1189. writeTrashFile("file2", "file2");
  1190. git.add().addFilepattern("file2").call();
  1191. git.commit().setMessage("commit2").call();
  1192. // checkout master branch / modify file1, add and commit
  1193. checkoutBranch("refs/heads/master");
  1194. writeTrashFile(FILE1, "modified file1");
  1195. git.add().addFilepattern(FILE1).call();
  1196. git.commit().setMessage("commit3").call();
  1197. // checkout topic branch / modify file0
  1198. checkoutBranch("refs/heads/topic");
  1199. writeTrashFile("file0", "unstaged modified file0");
  1200. // rebase
  1201. assertEquals(Status.OK, git.rebase().setUpstream("refs/heads/master")
  1202. .call().getStatus());
  1203. }
  1204. @Test
  1205. public void testRebaseWithUncommittedMasterChangeOtherCommit()
  1206. throws Exception {
  1207. // create file0, add and commit
  1208. File file0 = writeTrashFile("file0", "file0");
  1209. git.add().addFilepattern("file0").call();
  1210. git.commit().setMessage("commit0").call();
  1211. // create file1, add and commit
  1212. writeTrashFile(FILE1, "file1");
  1213. git.add().addFilepattern(FILE1).call();
  1214. RevCommit commit = git.commit().setMessage("commit1").call();
  1215. // create topic branch and checkout / create file2, add and commit
  1216. createBranch(commit, "refs/heads/topic");
  1217. checkoutBranch("refs/heads/topic");
  1218. writeTrashFile("file2", "file2");
  1219. git.add().addFilepattern("file2").call();
  1220. git.commit().setMessage("commit2").call();
  1221. // checkout master branch / modify file1, add and commit
  1222. checkoutBranch("refs/heads/master");
  1223. writeTrashFile(FILE1, "modified file1");
  1224. git.add().addFilepattern(FILE1).call();
  1225. git.commit().setMessage("commit3").call();
  1226. // checkout topic branch / modify file0 and add
  1227. checkoutBranch("refs/heads/topic");
  1228. write(file0, "unstaged modified file0");
  1229. git.add().addFilepattern("file0").call();
  1230. // do not commit
  1231. // get current index state
  1232. String indexState = indexState(CONTENT);
  1233. // rebase
  1234. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1235. .call();
  1236. assertEquals(Status.FAILED, result.getStatus());
  1237. // staged file0 causes DIRTY_INDEX
  1238. assertEquals(1, result.getFailingPaths().size());
  1239. assertEquals(MergeFailureReason.DIRTY_INDEX, result.getFailingPaths()
  1240. .get("file0"));
  1241. assertEquals("unstaged modified file0", read(file0));
  1242. // index shall be unchanged
  1243. assertEquals(indexState, indexState(CONTENT));
  1244. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  1245. }
  1246. @Test
  1247. public void testRebaseWithUncommittedDelete() throws Exception {
  1248. // create file0 + file1, add and commit
  1249. File file0 = writeTrashFile("file0", "file0");
  1250. writeTrashFile(FILE1, "file1");
  1251. git.add().addFilepattern("file0").addFilepattern(FILE1).call();
  1252. RevCommit commit = git.commit().setMessage("commit1").call();
  1253. // create topic branch
  1254. createBranch(commit, "refs/heads/topic");
  1255. // still on master / modify file1, add and commit
  1256. writeTrashFile(FILE1, "modified file1");
  1257. git.add().addFilepattern(FILE1).call();
  1258. git.commit().setMessage("commit2").call();
  1259. // checkout topic branch / delete file0 and add to index
  1260. checkoutBranch("refs/heads/topic");
  1261. git.rm().addFilepattern("file0").call();
  1262. // do not commit
  1263. // rebase
  1264. RebaseResult result = git.rebase().setUpstream("refs/heads/master")
  1265. .call();
  1266. assertEquals(Status.FAST_FORWARD, result.getStatus());
  1267. assertFalse("File should still be deleted", file0.exists());
  1268. // index should only have updated file1
  1269. assertEquals("[file1, mode:100644, content:modified file1]",
  1270. indexState(CONTENT));
  1271. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  1272. }
  1273. private int countPicks() throws IOException {
  1274. int count = 0;
  1275. File todoFile = getTodoFile();
  1276. BufferedReader br = new BufferedReader(new InputStreamReader(
  1277. new FileInputStream(todoFile), "UTF-8"));
  1278. try {
  1279. String line = br.readLine();
  1280. while (line != null) {
  1281. String actionToken = line.substring(0, line.indexOf(' '));
  1282. Action action = null;
  1283. try {
  1284. action = Action.parse(actionToken);
  1285. } catch (Exception e) {
  1286. // ignore
  1287. }
  1288. if (action != null)
  1289. count++;
  1290. line = br.readLine();
  1291. }
  1292. return count;
  1293. } finally {
  1294. br.close();
  1295. }
  1296. }
  1297. @Test
  1298. public void testFastForwardWithMultipleCommitsOnDifferentBranches()
  1299. throws Exception {
  1300. // create file1 on master
  1301. writeTrashFile(FILE1, FILE1);
  1302. git.add().addFilepattern(FILE1).call();
  1303. RevCommit first = git.commit().setMessage("Add file1").call();
  1304. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1305. // create a topic branch
  1306. createBranch(first, "refs/heads/topic");
  1307. // create file2 on master
  1308. writeTrashFile("file2", "file2");
  1309. git.add().addFilepattern("file2").call();
  1310. RevCommit second = git.commit().setMessage("Add file2").call();
  1311. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1312. // create side branch
  1313. createBranch(second, "refs/heads/side");
  1314. // update FILE1 on master
  1315. writeTrashFile(FILE1, "blah");
  1316. git.add().addFilepattern(FILE1).call();
  1317. git.commit().setMessage("updated file1 on master")
  1318. .call();
  1319. // switch to side branch and update file2
  1320. checkoutBranch("refs/heads/side");
  1321. writeTrashFile("file2", "more change");
  1322. git.add().addFilepattern("file2").call();
  1323. RevCommit fourth = git.commit().setMessage("update file2 on side")
  1324. .call();
  1325. // switch back to master and merge in side
  1326. checkoutBranch("refs/heads/master");
  1327. MergeResult result = git.merge().include(fourth.getId())
  1328. .setStrategy(MergeStrategy.RESOLVE).call();
  1329. assertEquals(MergeStatus.MERGED, result.getMergeStatus());
  1330. // switch back to topic branch and rebase it onto master
  1331. checkoutBranch("refs/heads/topic");
  1332. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  1333. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1334. checkFile(new File(db.getWorkTree(), "file2"), "more change");
  1335. assertEquals(Status.FAST_FORWARD, res.getStatus());
  1336. }
  1337. @Test
  1338. public void testRebaseShouldLeaveWorkspaceUntouchedWithUnstagedChangesConflict()
  1339. throws Exception {
  1340. writeTrashFile(FILE1, "initial file");
  1341. git.add().addFilepattern(FILE1).call();
  1342. RevCommit initial = git.commit().setMessage("initial commit").call();
  1343. createBranch(initial, "refs/heads/side");
  1344. writeTrashFile(FILE1, "updated file");
  1345. git.add().addFilepattern(FILE1).call();
  1346. git.commit().setMessage("updated FILE1 on master").call();
  1347. // switch to side, modify the file
  1348. checkoutBranch("refs/heads/side");
  1349. writeTrashFile(FILE1, "side update");
  1350. git.add().addFilepattern(FILE1).call();
  1351. git.commit().setMessage("updated FILE1 on side").call();
  1352. File theFile = writeTrashFile(FILE1, "dirty the file");
  1353. // and attempt to rebase
  1354. RebaseResult rebaseResult = git.rebase()
  1355. .setUpstream("refs/heads/master").call();
  1356. assertEquals(Status.CONFLICTS, rebaseResult.getStatus());
  1357. assertEquals(1, rebaseResult.getConflicts().size());
  1358. assertEquals(FILE1, rebaseResult.getConflicts().get(0));
  1359. checkFile(theFile, "dirty the file");
  1360. assertEquals(RepositoryState.SAFE, git.getRepository()
  1361. .getRepositoryState());
  1362. }
  1363. @Test
  1364. public void testAbortShouldAlsoAbortNonInteractiveRebaseWithRebaseApplyDir()
  1365. throws Exception {
  1366. writeTrashFile(FILE1, "initial file");
  1367. git.add().addFilepattern(FILE1).call();
  1368. git.commit().setMessage("initial commit").call();
  1369. File applyDir = new File(db.getDirectory(), "rebase-apply");
  1370. File headName = new File(applyDir, "head-name");
  1371. FileUtils.mkdir(applyDir);
  1372. write(headName, "master");
  1373. db.writeOrigHead(db.resolve(Constants.HEAD));
  1374. git.rebase().setOperation(Operation.ABORT).call();
  1375. assertFalse("Abort should clean up .git/rebase-apply",
  1376. applyDir.exists());
  1377. assertEquals(RepositoryState.SAFE, git.getRepository()
  1378. .getRepositoryState());
  1379. }
  1380. @Test
  1381. public void testRebaseShouldBeAbleToHandleEmptyLinesInRebaseTodoFile()
  1382. throws IOException {
  1383. String emptyLine = "\n";
  1384. String todo = "pick 1111111 Commit 1\n" + emptyLine
  1385. + "pick 2222222 Commit 2\n" + emptyLine
  1386. + "# Comment line at end\n";
  1387. write(getTodoFile(), todo);
  1388. RebaseCommand rebaseCommand = git.rebase();
  1389. List<Step> steps = rebaseCommand.loadSteps();
  1390. assertEquals(2, steps.size());
  1391. assertEquals("1111111", steps.get(0).commit.name());
  1392. assertEquals("2222222", steps.get(1).commit.name());
  1393. }
  1394. @Test
  1395. public void testParseRewordCommand() throws Exception {
  1396. String todo = "pick 1111111 Commit 1\n"
  1397. + "reword 2222222 Commit 2\n";
  1398. write(getTodoFile(), todo);
  1399. RebaseCommand rebaseCommand = git.rebase();
  1400. List<Step> steps = rebaseCommand.loadSteps();
  1401. assertEquals(2, steps.size());
  1402. assertEquals("1111111", steps.get(0).commit.name());
  1403. assertEquals("2222222", steps.get(1).commit.name());
  1404. assertEquals(Action.REWORD, steps.get(1).action);
  1405. }
  1406. @Test
  1407. public void testRebaseInteractiveReword() throws Exception {
  1408. // create file1 on master
  1409. writeTrashFile(FILE1, FILE1);
  1410. git.add().addFilepattern(FILE1).call();
  1411. git.commit().setMessage("Add file1").call();
  1412. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1413. // create file2 on master
  1414. writeTrashFile("file2", "file2");
  1415. git.add().addFilepattern("file2").call();
  1416. git.commit().setMessage("Add file2").call();
  1417. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1418. // update FILE1 on master
  1419. writeTrashFile(FILE1, "blah");
  1420. git.add().addFilepattern(FILE1).call();
  1421. git.commit().setMessage("updated file1 on master").call();
  1422. writeTrashFile("file2", "more change");
  1423. git.add().addFilepattern("file2").call();
  1424. git.commit().setMessage("update file2 on side").call();
  1425. RebaseResult res = git.rebase().setUpstream("HEAD~2")
  1426. .runInteractively(new InteractiveHandler() {
  1427. public void prepareSteps(List<Step> steps) {
  1428. steps.get(0).action = Action.REWORD;
  1429. }
  1430. public String modifyCommitMessage(String commit) {
  1431. return "rewritten commit message";
  1432. }
  1433. }).call();
  1434. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1435. checkFile(new File(db.getWorkTree(), "file2"), "more change");
  1436. assertEquals(Status.OK, res.getStatus());
  1437. Iterator<RevCommit> logIterator = git.log().all().call().iterator();
  1438. logIterator.next(); // skip first commit;
  1439. String actualCommitMag = logIterator.next().getShortMessage();
  1440. assertEquals("rewritten commit message", actualCommitMag);
  1441. }
  1442. @Test
  1443. public void testRebaseInteractiveEdit() throws Exception {
  1444. // create file1 on master
  1445. writeTrashFile(FILE1, FILE1);
  1446. git.add().addFilepattern(FILE1).call();
  1447. git.commit().setMessage("Add file1").call();
  1448. assertTrue(new File(db.getWorkTree(), FILE1).exists());
  1449. // create file2 on master
  1450. writeTrashFile("file2", "file2");
  1451. git.add().addFilepattern("file2").call();
  1452. git.commit().setMessage("Add file2").call();
  1453. assertTrue(new File(db.getWorkTree(), "file2").exists());
  1454. // update FILE1 on master
  1455. writeTrashFile(FILE1, "blah");
  1456. git.add().addFilepattern(FILE1).call();
  1457. git.commit().setMessage("updated file1 on master").call();
  1458. writeTrashFile("file2", "more change");
  1459. git.add().addFilepattern("file2").call();
  1460. git.commit().setMessage("update file2 on side").call();
  1461. RebaseResult res = git.rebase().setUpstream("HEAD~2")
  1462. .runInteractively(new InteractiveHandler() {
  1463. public void prepareSteps(List<Step> steps) {
  1464. steps.get(0).action = Action.EDIT;
  1465. }
  1466. public String modifyCommitMessage(String commit) {
  1467. return ""; // not used
  1468. }
  1469. }).call();
  1470. assertEquals(Status.STOPPED, res.getStatus());
  1471. RevCommit toBeEditted = git.log().call().iterator().next();
  1472. assertEquals("updated file1 on master", toBeEditted.getFullMessage());
  1473. // change file and commit with new commit message
  1474. writeTrashFile("file1", "edited");
  1475. git.commit().setAll(true).setAmend(true)
  1476. .setMessage("edited commit message").call();
  1477. // resume rebase
  1478. res = git.rebase().setOperation(Operation.CONTINUE).call();
  1479. checkFile(new File(db.getWorkTree(), "file1"), "edited");
  1480. assertEquals(Status.OK, res.getStatus());
  1481. Iterator<RevCommit> logIterator = git.log().all().call().iterator();
  1482. logIterator.next(); // skip first commit;
  1483. String actualCommitMag = logIterator.next().getShortMessage();
  1484. assertEquals("edited commit message", actualCommitMag);
  1485. }
  1486. private File getTodoFile() {
  1487. File todoFile = new File(db.getDirectory(),
  1488. "rebase-merge/git-rebase-todo");
  1489. return todoFile;
  1490. }
  1491. }