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.

MergeCommandTest.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2010, Stefan Lay <stefan.lay@sap.com>
  3. * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.api;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import org.eclipse.jgit.errors.CheckoutConflictException;
  48. import org.eclipse.jgit.errors.CorruptObjectException;
  49. import org.eclipse.jgit.lib.Constants;
  50. import org.eclipse.jgit.lib.GitIndex;
  51. import org.eclipse.jgit.lib.ObjectId;
  52. import org.eclipse.jgit.lib.RefUpdate;
  53. import org.eclipse.jgit.lib.RepositoryTestCase;
  54. import org.eclipse.jgit.lib.WorkDirCheckout;
  55. import org.eclipse.jgit.lib.GitIndex.Entry;
  56. import org.eclipse.jgit.revwalk.RevCommit;
  57. public class MergeCommandTest extends RepositoryTestCase {
  58. public void testMergeInItself() throws Exception {
  59. Git git = new Git(db);
  60. git.commit().setMessage("initial commit").call();
  61. MergeResult result = git.merge().include(db.getRef(Constants.HEAD)).call();
  62. assertEquals(MergeResult.MergeStatus.ALREADY_UP_TO_DATE, result.getMergeStatus());
  63. }
  64. public void testAlreadyUpToDate() throws Exception {
  65. Git git = new Git(db);
  66. RevCommit first = git.commit().setMessage("initial commit").call();
  67. createBranch(first, "refs/heads/branch1");
  68. RevCommit second = git.commit().setMessage("second commit").call();
  69. MergeResult result = git.merge().include(db.getRef("refs/heads/branch1")).call();
  70. assertEquals(MergeResult.MergeStatus.ALREADY_UP_TO_DATE, result.getMergeStatus());
  71. assertEquals(second, result.getNewHead());
  72. }
  73. public void testFastForward() throws Exception {
  74. Git git = new Git(db);
  75. RevCommit first = git.commit().setMessage("initial commit").call();
  76. createBranch(first, "refs/heads/branch1");
  77. RevCommit second = git.commit().setMessage("second commit").call();
  78. checkoutBranch("refs/heads/branch1");
  79. MergeResult result = git.merge().include(db.getRef(Constants.MASTER)).call();
  80. assertEquals(MergeResult.MergeStatus.FAST_FORWARD, result.getMergeStatus());
  81. assertEquals(second, result.getNewHead());
  82. }
  83. public void testFastForwardWithFiles() throws Exception {
  84. Git git = new Git(db);
  85. addNewFileToIndex("file1");
  86. RevCommit first = git.commit().setMessage("initial commit").call();
  87. assertTrue(new File(db.getWorkDir(), "file1").exists());
  88. createBranch(first, "refs/heads/branch1");
  89. addNewFileToIndex("file2");
  90. RevCommit second = git.commit().setMessage("second commit").call();
  91. assertTrue(new File(db.getWorkDir(), "file2").exists());
  92. checkoutBranch("refs/heads/branch1");
  93. assertFalse(new File(db.getWorkDir(), "file2").exists());
  94. MergeResult result = git.merge().include(db.getRef(Constants.MASTER)).call();
  95. assertTrue(new File(db.getWorkDir(), "file1").exists());
  96. assertTrue(new File(db.getWorkDir(), "file2").exists());
  97. assertEquals(MergeResult.MergeStatus.FAST_FORWARD, result.getMergeStatus());
  98. assertEquals(second, result.getNewHead());
  99. }
  100. public void testMultipleHeads() throws Exception {
  101. Git git = new Git(db);
  102. addNewFileToIndex("file1");
  103. RevCommit first = git.commit().setMessage("initial commit").call();
  104. createBranch(first, "refs/heads/branch1");
  105. addNewFileToIndex("file2");
  106. RevCommit second = git.commit().setMessage("second commit").call();
  107. addNewFileToIndex("file3");
  108. git.commit().setMessage("third commit").call();
  109. checkoutBranch("refs/heads/branch1");
  110. assertFalse(new File(db.getWorkDir(), "file2").exists());
  111. assertFalse(new File(db.getWorkDir(), "file3").exists());
  112. MergeCommand merge = git.merge();
  113. merge.include(second.getId());
  114. merge.include(db.getRef(Constants.MASTER));
  115. try {
  116. merge.call();
  117. fail("Expected exception not thrown when merging multiple heads");
  118. } catch (InvalidMergeHeadsException e) {
  119. }
  120. }
  121. private void createBranch(ObjectId objectId, String branchName) throws IOException {
  122. RefUpdate updateRef = db.updateRef(branchName);
  123. updateRef.setNewObjectId(objectId);
  124. updateRef.update();
  125. }
  126. private void checkoutBranch(String branchName) throws Exception {
  127. File workDir = db.getWorkDir();
  128. if (workDir != null) {
  129. WorkDirCheckout workDirCheckout = new WorkDirCheckout(db,
  130. workDir, db.mapCommit(Constants.HEAD).getTree(),
  131. db.getIndex(), db.mapCommit(branchName).getTree());
  132. workDirCheckout.setFailOnConflict(true);
  133. try {
  134. workDirCheckout.checkout();
  135. } catch (CheckoutConflictException e) {
  136. throw new JGitInternalException(
  137. "Couldn't check out because of conflicts", e);
  138. }
  139. }
  140. // update the HEAD
  141. RefUpdate refUpdate = db.updateRef(Constants.HEAD);
  142. refUpdate.link(branchName);
  143. }
  144. private void addNewFileToIndex(String filename) throws IOException,
  145. CorruptObjectException {
  146. File writeTrashFile = writeTrashFile(filename, filename);
  147. GitIndex index = db.getIndex();
  148. Entry entry = index.add(db.getWorkDir(), writeTrashFile);
  149. entry.update(writeTrashFile);
  150. index.write();
  151. }
  152. }