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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.dircache.DirCacheCheckout;
  52. import org.eclipse.jgit.lib.Constants;
  53. import org.eclipse.jgit.lib.ObjectId;
  54. import org.eclipse.jgit.lib.RefUpdate;
  55. import org.eclipse.jgit.lib.RepositoryState;
  56. import org.eclipse.jgit.lib.RepositoryTestCase;
  57. import org.eclipse.jgit.revwalk.RevCommit;
  58. import org.eclipse.jgit.revwalk.RevWalk;
  59. public class RebaseCommandTest extends RepositoryTestCase {
  60. private void createBranch(ObjectId objectId, String branchName)
  61. throws IOException {
  62. RefUpdate updateRef = db.updateRef(branchName);
  63. updateRef.setNewObjectId(objectId);
  64. updateRef.update();
  65. }
  66. private void checkoutBranch(String branchName)
  67. throws IllegalStateException, IOException {
  68. RevWalk walk = new RevWalk(db);
  69. RevCommit head = walk.parseCommit(db.resolve(Constants.HEAD));
  70. RevCommit branch = walk.parseCommit(db.resolve(branchName));
  71. DirCacheCheckout dco = new DirCacheCheckout(db, head.getTree().getId(),
  72. db.lockDirCache(), branch.getTree().getId());
  73. dco.setFailOnConflict(true);
  74. dco.checkout();
  75. walk.release();
  76. // update the HEAD
  77. RefUpdate refUpdate = db.updateRef(Constants.HEAD);
  78. refUpdate.link(branchName);
  79. }
  80. public void testFastForwardWithNewFile() throws Exception {
  81. Git git = new Git(db);
  82. // create file1 on master
  83. writeTrashFile("file1", "file1");
  84. git.add().addFilepattern("file1").call();
  85. RevCommit first = git.commit().setMessage("Add file1").call();
  86. assertTrue(new File(db.getWorkTree(), "file1").exists());
  87. // create a topic branch
  88. createBranch(first, "refs/heads/topic");
  89. // create file2 on master
  90. writeTrashFile("file2", "file2");
  91. git.add().addFilepattern("file2").call();
  92. git.commit().setMessage("Add file2").call();
  93. assertTrue(new File(db.getWorkTree(), "file2").exists());
  94. checkoutBranch("refs/heads/topic");
  95. assertFalse(new File(db.getWorkTree(), "file2").exists());
  96. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  97. assertEquals(Status.UP_TO_DATE, res.getStatus());
  98. }
  99. public void testConflictFreeWithSingleFile() throws Exception {
  100. Git git = new Git(db);
  101. // create file1 on master
  102. File theFile = writeTrashFile("file1", "1\n2\n3\n");
  103. git.add().addFilepattern("file1").call();
  104. RevCommit second = git.commit().setMessage("Add file1").call();
  105. assertTrue(new File(db.getWorkTree(), "file1").exists());
  106. // change first line in master and commit
  107. writeTrashFile("file1", "1master\n2\n3\n");
  108. checkFile(theFile, "1master\n2\n3\n");
  109. git.add().addFilepattern("file1").call();
  110. RevCommit lastMasterChange = git.commit().setMessage(
  111. "change file1 in master").call();
  112. // create a topic branch based on second commit
  113. createBranch(second, "refs/heads/topic");
  114. checkoutBranch("refs/heads/topic");
  115. // we have the old content again
  116. checkFile(theFile, "1\n2\n3\n");
  117. assertTrue(new File(db.getWorkTree(), "file1").exists());
  118. // change third line in topic branch
  119. writeTrashFile("file1", "1\n2\n3\ntopic\n");
  120. git.add().addFilepattern("file1").call();
  121. git.commit().setMessage("change file1 in topic").call();
  122. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  123. assertEquals(Status.OK, res.getStatus());
  124. checkFile(theFile, "1master\n2\n3\ntopic\n");
  125. // our old branch should be checked out again
  126. assertEquals("refs/heads/topic", db.getFullBranch());
  127. assertEquals(lastMasterChange, new RevWalk(db).parseCommit(
  128. db.resolve(Constants.HEAD)).getParent(0));
  129. }
  130. public void testFilesAddedFromTwoBranches() throws Exception {
  131. Git git = new Git(db);
  132. // create file1 on master
  133. writeTrashFile("file1", "file1");
  134. git.add().addFilepattern("file1").call();
  135. RevCommit masterCommit = git.commit().setMessage("Add file1 to master")
  136. .call();
  137. // create a branch named file2 and add file2
  138. createBranch(masterCommit, "refs/heads/file2");
  139. checkoutBranch("refs/heads/file2");
  140. writeTrashFile("file2", "file2");
  141. git.add().addFilepattern("file2").call();
  142. RevCommit addFile2 = git.commit().setMessage(
  143. "Add file2 to branch file2").call();
  144. // create a branch named file3 and add file3
  145. createBranch(masterCommit, "refs/heads/file3");
  146. checkoutBranch("refs/heads/file3");
  147. writeTrashFile("file3", "file3");
  148. git.add().addFilepattern("file3").call();
  149. git.commit().setMessage("Add file3 to branch file3").call();
  150. assertTrue(new File(db.getWorkTree(), "file1").exists());
  151. assertFalse(new File(db.getWorkTree(), "file2").exists());
  152. assertTrue(new File(db.getWorkTree(), "file3").exists());
  153. RebaseResult res = git.rebase().setUpstream("refs/heads/file2").call();
  154. assertEquals(Status.OK, res.getStatus());
  155. assertTrue(new File(db.getWorkTree(), "file1").exists());
  156. assertTrue(new File(db.getWorkTree(), "file2").exists());
  157. assertTrue(new File(db.getWorkTree(), "file3").exists());
  158. // our old branch should be checked out again
  159. assertEquals("refs/heads/file3", db.getFullBranch());
  160. assertEquals(addFile2, new RevWalk(db).parseCommit(
  161. db.resolve(Constants.HEAD)).getParent(0));
  162. checkoutBranch("refs/heads/file2");
  163. assertTrue(new File(db.getWorkTree(), "file1").exists());
  164. assertTrue(new File(db.getWorkTree(), "file2").exists());
  165. assertFalse(new File(db.getWorkTree(), "file3").exists());
  166. }
  167. public void testAbortOnConflict() throws Exception {
  168. Git git = new Git(db);
  169. // create file1 on master
  170. File theFile = writeTrashFile("file1", "1\n2\n3\n");
  171. git.add().addFilepattern("file1").call();
  172. RevCommit second = git.commit().setMessage("Add file1").call();
  173. assertTrue(new File(db.getWorkTree(), "file1").exists());
  174. // change first line in master and commit
  175. writeTrashFile("file1", "1master\n2\n3\n");
  176. checkFile(theFile, "1master\n2\n3\n");
  177. git.add().addFilepattern("file1").call();
  178. git.commit().setMessage("change file1 in master").call();
  179. // create a topic branch based on second commit
  180. createBranch(second, "refs/heads/topic");
  181. checkoutBranch("refs/heads/topic");
  182. // we have the old content again
  183. checkFile(theFile, "1\n2\n3\n");
  184. assertTrue(new File(db.getWorkTree(), "file1").exists());
  185. // add a line (non-conflicting)
  186. writeTrashFile("file1", "1\n2\n3\n4\n");
  187. git.add().addFilepattern("file1").call();
  188. git.commit().setMessage("add a line to file1 in topic").call();
  189. // change first line (conflicting)
  190. writeTrashFile("file1", "1topic\n2\n3\n4\n");
  191. git.add().addFilepattern("file1").call();
  192. git.commit().setMessage("change file1 in topic").call();
  193. // change second line (not conflicting)
  194. writeTrashFile("file1", "1topic\n2topic\n3\n4\n");
  195. git.add().addFilepattern("file1").call();
  196. RevCommit lastTopicCommit = git.commit().setMessage(
  197. "change file1 in topic again").call();
  198. RebaseResult res = git.rebase().setUpstream("refs/heads/master").call();
  199. assertEquals(Status.STOPPED, res.getStatus());
  200. checkFile(theFile,
  201. "<<<<<<< OURS\n1master\n=======\n1topic\n>>>>>>> THEIRS\n2\n3\n4\n");
  202. assertEquals(RepositoryState.REBASING_MERGE, db.getRepositoryState());
  203. // the first one should be included, so we should have left two picks in
  204. // the file
  205. assertEquals(countPicks(), 2);
  206. // abort should reset to topic branch
  207. res = git.rebase().setOperation(Operation.ABORT).call();
  208. assertEquals(res.getStatus(), Status.ABORTED);
  209. assertEquals("refs/heads/topic", db.getFullBranch());
  210. checkFile(theFile, "1topic\n2topic\n3\n4\n");
  211. RevWalk rw = new RevWalk(db);
  212. assertEquals(lastTopicCommit, rw
  213. .parseCommit(db.resolve(Constants.HEAD)));
  214. }
  215. private int countPicks() throws IOException {
  216. int count = 0;
  217. File todoFile = new File(db.getDirectory(),
  218. "rebase-merge/git-rebase-todo");
  219. BufferedReader br = new BufferedReader(new InputStreamReader(
  220. new FileInputStream(todoFile), "UTF-8"));
  221. try {
  222. String line = br.readLine();
  223. while (line != null) {
  224. if (line.startsWith("pick "))
  225. count++;
  226. line = br.readLine();
  227. }
  228. return count;
  229. } finally {
  230. br.close();
  231. }
  232. }
  233. }