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

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