Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

RebaseCommandTest.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 java.io.BufferedReader;
  45. import java.io.File;
  46. import java.io.FileInputStream;
  47. import java.io.IOException;
  48. import java.io.InputStreamReader;
  49. import org.eclipse.jgit.api.RebaseCommand.Operation;
  50. import org.eclipse.jgit.api.RebaseResult.Status;
  51. import org.eclipse.jgit.api.errors.RefNotFoundException;
  52. import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
  53. import org.eclipse.jgit.dircache.DirCacheCheckout;
  54. import org.eclipse.jgit.lib.Constants;
  55. import org.eclipse.jgit.lib.ObjectId;
  56. import org.eclipse.jgit.lib.RefUpdate;
  57. import org.eclipse.jgit.lib.RepositoryState;
  58. import org.eclipse.jgit.lib.RepositoryTestCase;
  59. import org.eclipse.jgit.revwalk.RevCommit;
  60. import org.eclipse.jgit.revwalk.RevWalk;
  61. public class RebaseCommandTest extends RepositoryTestCase {
  62. private void createBranch(ObjectId objectId, String branchName)
  63. throws IOException {
  64. RefUpdate updateRef = db.updateRef(branchName);
  65. updateRef.setNewObjectId(objectId);
  66. updateRef.update();
  67. }
  68. private void checkoutBranch(String branchName)
  69. throws IllegalStateException, IOException {
  70. RevWalk walk = new RevWalk(db);
  71. RevCommit head = walk.parseCommit(db.resolve(Constants.HEAD));
  72. RevCommit branch = walk.parseCommit(db.resolve(branchName));
  73. DirCacheCheckout dco = new DirCacheCheckout(db, head.getTree().getId(),
  74. db.lockDirCache(), branch.getTree().getId());
  75. dco.setFailOnConflict(true);
  76. dco.checkout();
  77. walk.release();
  78. // update the HEAD
  79. RefUpdate refUpdate = db.updateRef(Constants.HEAD);
  80. refUpdate.link(branchName);
  81. }
  82. private void checkoutCommit(RevCommit commit) throws IllegalStateException,
  83. IOException {
  84. RevWalk walk = new RevWalk(db);
  85. RevCommit head = walk.parseCommit(db.resolve(Constants.HEAD));
  86. DirCacheCheckout dco = new DirCacheCheckout(db, head.getTree(),
  87. db.lockDirCache(), commit.getTree());
  88. dco.setFailOnConflict(true);
  89. dco.checkout();
  90. walk.release();
  91. // update the HEAD
  92. RefUpdate refUpdate = db.updateRef(Constants.HEAD, true);
  93. refUpdate.setNewObjectId(commit);
  94. refUpdate.forceUpdate();
  95. }
  96. public void testFastForwardWithNewFile() throws Exception {
  97. Git git = new Git(db);
  98. // create file1 on master
  99. writeTrashFile("file1", "file1");
  100. git.add().addFilepattern("file1").call();
  101. RevCommit first = git.commit().setMessage("Add file1").call();
  102. assertTrue(new File(db.getWorkTree(), "file1").exists());
  103. // create a topic branch
  104. createBranch(first, "refs/heads/topic");
  105. // create file2 on master
  106. writeTrashFile("file2", "file2");
  107. git.add().addFilepattern("file2").call();
  108. git.commit().setMessage("Add file2").call();
  109. assertTrue(new File(db.getWorkTree(), "file2").exists());
  110. checkoutBranch("refs/heads/topic");
  111. assertFalse(new File(db.getWorkTree(), "file2").exists());
  112. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  113. assertEquals(Status.UP_TO_DATE, res.getStatus());
  114. }
  115. public void testUpToDate() throws Exception {
  116. Git git = new Git(db);
  117. // create file1 on master
  118. writeTrashFile("file1", "file1");
  119. git.add().addFilepattern("file1").call();
  120. RevCommit first = git.commit().setMessage("Add file1").call();
  121. assertTrue(new File(db.getWorkTree(), "file1").exists());
  122. RebaseResult res = git.rebase().setUpstream(first).call();
  123. assertEquals(Status.UP_TO_DATE, res.getStatus());
  124. }
  125. public void testUnknownUpstream() throws Exception {
  126. Git git = new Git(db);
  127. // create file1 on master
  128. writeTrashFile("file1", "file1");
  129. git.add().addFilepattern("file1").call();
  130. git.commit().setMessage("Add file1").call();
  131. assertTrue(new File(db.getWorkTree(), "file1").exists());
  132. try {
  133. git.rebase().setUpstream("refs/heads/xyz")
  134. .call();
  135. fail("expected exception was not thrown");
  136. } catch (RefNotFoundException e) {
  137. // expected exception
  138. }
  139. }
  140. public void testConflictFreeWithSingleFile() throws Exception {
  141. Git git = new Git(db);
  142. // create file1 on master
  143. File theFile = writeTrashFile("file1", "1\n2\n3\n");
  144. git.add().addFilepattern("file1").call();
  145. RevCommit second = git.commit().setMessage("Add file1").call();
  146. assertTrue(new File(db.getWorkTree(), "file1").exists());
  147. // change first line in master and commit
  148. writeTrashFile("file1", "1master\n2\n3\n");
  149. checkFile(theFile, "1master\n2\n3\n");
  150. git.add().addFilepattern("file1").call();
  151. RevCommit lastMasterChange = git.commit().setMessage(
  152. "change file1 in master").call();
  153. // create a topic branch based on second commit
  154. createBranch(second, "refs/heads/topic");
  155. checkoutBranch("refs/heads/topic");
  156. // we have the old content again
  157. checkFile(theFile, "1\n2\n3\n");
  158. assertTrue(new File(db.getWorkTree(), "file1").exists());
  159. // change third line in topic branch
  160. writeTrashFile("file1", "1\n2\n3\ntopic\n");
  161. git.add().addFilepattern("file1").call();
  162. git.commit().setMessage("change file1 in topic").call();
  163. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  164. assertEquals(Status.OK, res.getStatus());
  165. checkFile(theFile, "1master\n2\n3\ntopic\n");
  166. // our old branch should be checked out again
  167. assertEquals("refs/heads/topic", db.getFullBranch());
  168. assertEquals(lastMasterChange, new RevWalk(db).parseCommit(
  169. db.resolve(Constants.HEAD)).getParent(0));
  170. }
  171. public void testDetachedHead() throws Exception {
  172. Git git = new Git(db);
  173. // create file1 on master
  174. File theFile = writeTrashFile("file1", "1\n2\n3\n");
  175. git.add().addFilepattern("file1").call();
  176. RevCommit second = git.commit().setMessage("Add file1").call();
  177. assertTrue(new File(db.getWorkTree(), "file1").exists());
  178. // change first line in master and commit
  179. writeTrashFile("file1", "1master\n2\n3\n");
  180. checkFile(theFile, "1master\n2\n3\n");
  181. git.add().addFilepattern("file1").call();
  182. RevCommit lastMasterChange = git.commit()
  183. .setMessage("change file1 in master").call();
  184. // create a topic branch based on second commit
  185. createBranch(second, "refs/heads/topic");
  186. checkoutBranch("refs/heads/topic");
  187. // we have the old content again
  188. checkFile(theFile, "1\n2\n3\n");
  189. assertTrue(new File(db.getWorkTree(), "file1").exists());
  190. // change third line in topic branch
  191. writeTrashFile("file1", "1\n2\n3\ntopic\n");
  192. git.add().addFilepattern("file1").call();
  193. RevCommit topicCommit = git.commit()
  194. .setMessage("change file1 in topic").call();
  195. checkoutBranch("refs/heads/master");
  196. checkoutCommit(topicCommit);
  197. assertEquals(topicCommit.getId().getName(), db.getFullBranch());
  198. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  199. assertEquals(Status.OK, res.getStatus());
  200. checkFile(theFile, "1master\n2\n3\ntopic\n");
  201. assertEquals(lastMasterChange,
  202. new RevWalk(db).parseCommit(db.resolve(Constants.HEAD))
  203. .getParent(0));
  204. }
  205. public void testFilesAddedFromTwoBranches() throws Exception {
  206. Git git = new Git(db);
  207. // create file1 on master
  208. writeTrashFile("file1", "file1");
  209. git.add().addFilepattern("file1").call();
  210. RevCommit masterCommit = git.commit().setMessage("Add file1 to master")
  211. .call();
  212. // create a branch named file2 and add file2
  213. createBranch(masterCommit, "refs/heads/file2");
  214. checkoutBranch("refs/heads/file2");
  215. writeTrashFile("file2", "file2");
  216. git.add().addFilepattern("file2").call();
  217. RevCommit addFile2 = git.commit().setMessage(
  218. "Add file2 to branch file2").call();
  219. // create a branch named file3 and add file3
  220. createBranch(masterCommit, "refs/heads/file3");
  221. checkoutBranch("refs/heads/file3");
  222. writeTrashFile("file3", "file3");
  223. git.add().addFilepattern("file3").call();
  224. git.commit().setMessage("Add file3 to branch file3").call();
  225. assertTrue(new File(db.getWorkTree(), "file1").exists());
  226. assertFalse(new File(db.getWorkTree(), "file2").exists());
  227. assertTrue(new File(db.getWorkTree(), "file3").exists());
  228. RebaseResult res = git.rebase().setUpstream("refs/heads/file2").call();
  229. assertEquals(Status.OK, res.getStatus());
  230. assertTrue(new File(db.getWorkTree(), "file1").exists());
  231. assertTrue(new File(db.getWorkTree(), "file2").exists());
  232. assertTrue(new File(db.getWorkTree(), "file3").exists());
  233. // our old branch should be checked out again
  234. assertEquals("refs/heads/file3", db.getFullBranch());
  235. assertEquals(addFile2, new RevWalk(db).parseCommit(
  236. db.resolve(Constants.HEAD)).getParent(0));
  237. checkoutBranch("refs/heads/file2");
  238. assertTrue(new File(db.getWorkTree(), "file1").exists());
  239. assertTrue(new File(db.getWorkTree(), "file2").exists());
  240. assertFalse(new File(db.getWorkTree(), "file3").exists());
  241. }
  242. public void testAbortOnConflict() throws Exception {
  243. Git git = new Git(db);
  244. // create file1 on master
  245. File theFile = writeTrashFile("file1", "1\n2\n3\n");
  246. git.add().addFilepattern("file1").call();
  247. RevCommit firstInMaster = 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. git.commit().setMessage("change file1 in master").call();
  254. // create a topic branch based on second commit
  255. createBranch(firstInMaster, "refs/heads/topic");
  256. checkoutBranch("refs/heads/topic");
  257. // we have the old content again
  258. checkFile(theFile, "1\n2\n3\n");
  259. assertTrue(new File(db.getWorkTree(), "file1").exists());
  260. // add a line (non-conflicting)
  261. writeTrashFile("file1", "1\n2\n3\ntopic4\n");
  262. git.add().addFilepattern("file1").call();
  263. git.commit().setMessage("add a line to file1 in topic").call();
  264. // change first line (conflicting)
  265. writeTrashFile("file1", "1topic\n2\n3\ntopic4\n");
  266. git.add().addFilepattern("file1").call();
  267. RevCommit conflicting = git.commit()
  268. .setMessage("change file1 in topic").call();
  269. // change second line (not conflicting)
  270. writeTrashFile("file1", "1topic\n2topic\n3\ntopic4\n");
  271. git.add().addFilepattern("file1").call();
  272. RevCommit lastTopicCommit = git.commit().setMessage(
  273. "change file1 in topic again").call();
  274. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  275. assertEquals(Status.STOPPED, res.getStatus());
  276. assertEquals(conflicting, res.getCurrentCommit());
  277. checkFile(theFile,
  278. "<<<<<<< OURS\n1master\n=======\n1topic\n>>>>>>> THEIRS\n2\n3\ntopic4\n");
  279. assertEquals(RepositoryState.REBASING_INTERACTIVE, db
  280. .getRepositoryState());
  281. assertTrue(new File(db.getDirectory(), "rebase-merge").exists());
  282. // the first one should be included, so we should have left two picks in
  283. // the file
  284. assertEquals(1, countPicks());
  285. // rebase should not succeed in this state
  286. try {
  287. git.rebase().setUpstream("refs/heads/master").call();
  288. fail("Expected exception was not thrown");
  289. } catch (WrongRepositoryStateException e) {
  290. // expected
  291. }
  292. // abort should reset to topic branch
  293. res = git.rebase().setOperation(Operation.ABORT).call();
  294. assertEquals(res.getStatus(), Status.ABORTED);
  295. assertEquals("refs/heads/topic", db.getFullBranch());
  296. checkFile(theFile, "1topic\n2topic\n3\ntopic4\n");
  297. RevWalk rw = new RevWalk(db);
  298. assertEquals(lastTopicCommit, rw
  299. .parseCommit(db.resolve(Constants.HEAD)));
  300. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  301. // rebase- dir in .git must be deleted
  302. assertFalse(new File(db.getDirectory(), "rebase-merge").exists());
  303. }
  304. public void testAbortOnConflictFileCreationAndDeletion() throws Exception {
  305. Git git = new Git(db);
  306. // create file1 on master
  307. writeTrashFile("file1", "Hello World");
  308. git.add().addFilepattern("file1").call();
  309. // create file2 on master
  310. File file2 = writeTrashFile("file2", "Hello World 2");
  311. git.add().addFilepattern("file2").call();
  312. // create file3 on master
  313. File file3 = writeTrashFile("file3", "Hello World 3");
  314. git.add().addFilepattern("file3").call();
  315. RevCommit firstInMaster = git.commit()
  316. .setMessage("Add file 1, 2 and 3").call();
  317. // create file4 on master
  318. File file4 = writeTrashFile("file4", "Hello World 4");
  319. git.add().addFilepattern("file4").call();
  320. deleteTrashFile("file2");
  321. git.add().setUpdate(true).addFilepattern("file2").call();
  322. // create folder folder6 on topic (conflicts with file folder6 on topic
  323. // later on)
  324. writeTrashFile("folder6/file1", "Hello World folder6");
  325. git.add().addFilepattern("folder6/file1").call();
  326. git.commit()
  327. .setMessage(
  328. "Add file 4 and folder folder6, delete file2 on master")
  329. .call();
  330. // create a topic branch based on second commit
  331. createBranch(firstInMaster, "refs/heads/topic");
  332. checkoutBranch("refs/heads/topic");
  333. deleteTrashFile("file3");
  334. git.add().setUpdate(true).addFilepattern("file3").call();
  335. // create file5 on topic
  336. File file5 = writeTrashFile("file5", "Hello World 5");
  337. git.add().addFilepattern("file5").call();
  338. git.commit().setMessage("Delete file3 and add file5 in topic").call();
  339. // create file folder6 on topic (conflicts with folder6 on master)
  340. writeTrashFile("folder6", "Hello World 6");
  341. git.add().addFilepattern("folder6").call();
  342. // create file7 on topic
  343. File file7 = writeTrashFile("file7", "Hello World 7");
  344. git.add().addFilepattern("file7").call();
  345. deleteTrashFile("file5");
  346. git.add().setUpdate(true).addFilepattern("file5").call();
  347. RevCommit conflicting = git.commit()
  348. .setMessage("Delete file5, add file folder6 and file7 in topic")
  349. .call();
  350. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  351. assertEquals(Status.STOPPED, res.getStatus());
  352. assertEquals(conflicting, res.getCurrentCommit());
  353. assertEquals(RepositoryState.REBASING_INTERACTIVE, db
  354. .getRepositoryState());
  355. assertTrue(new File(db.getDirectory(), "rebase-merge").exists());
  356. // the first one should be included, so we should have left two picks in
  357. // the file
  358. assertEquals(0, countPicks());
  359. assertFalse(file2.exists());
  360. assertFalse(file3.exists());
  361. assertTrue(file4.exists());
  362. assertFalse(file5.exists());
  363. assertTrue(file7.exists());
  364. // abort should reset to topic branch
  365. res = git.rebase().setOperation(Operation.ABORT).call();
  366. assertEquals(res.getStatus(), Status.ABORTED);
  367. assertEquals("refs/heads/topic", db.getFullBranch());
  368. RevWalk rw = new RevWalk(db);
  369. assertEquals(conflicting,
  370. rw.parseCommit(db.resolve(Constants.HEAD)));
  371. assertEquals(RepositoryState.SAFE, db.getRepositoryState());
  372. // rebase- dir in .git must be deleted
  373. assertFalse(new File(db.getDirectory(), "rebase-merge").exists());
  374. assertTrue(file2.exists());
  375. assertFalse(file3.exists());
  376. assertFalse(file4.exists());
  377. assertFalse(file5.exists());
  378. assertTrue(file7.exists());
  379. }
  380. private int countPicks() throws IOException {
  381. int count = 0;
  382. File todoFile = new File(db.getDirectory(),
  383. "rebase-merge/git-rebase-todo");
  384. BufferedReader br = new BufferedReader(new InputStreamReader(
  385. new FileInputStream(todoFile), "UTF-8"));
  386. try {
  387. String line = br.readLine();
  388. while (line != null) {
  389. if (line.startsWith("pick "))
  390. count++;
  391. line = br.readLine();
  392. }
  393. return count;
  394. } finally {
  395. br.close();
  396. }
  397. }
  398. }