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.

CherryPickTest.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * Copyright (C) 2008, Robin Rosenberg
  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.merge;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertTrue;
  48. import org.eclipse.jgit.dircache.DirCache;
  49. import org.eclipse.jgit.dircache.DirCacheBuilder;
  50. import org.eclipse.jgit.junit.RepositoryTestCase;
  51. import org.eclipse.jgit.lib.CommitBuilder;
  52. import org.eclipse.jgit.lib.FileMode;
  53. import org.eclipse.jgit.lib.ObjectId;
  54. import org.eclipse.jgit.lib.ObjectInserter;
  55. import org.eclipse.jgit.lib.PersonIdent;
  56. import org.eclipse.jgit.treewalk.TreeWalk;
  57. import org.junit.Test;
  58. public class CherryPickTest extends RepositoryTestCase {
  59. @Test
  60. public void testPick() throws Exception {
  61. // B---O
  62. // \----P---T
  63. //
  64. // Cherry-pick "T" onto "O". This shouldn't introduce "p-fail", which
  65. // was created by "P", nor should it modify "a", which was done by "P".
  66. //
  67. final DirCache treeB = db.readDirCache();
  68. final DirCache treeO = db.readDirCache();
  69. final DirCache treeP = db.readDirCache();
  70. final DirCache treeT = db.readDirCache();
  71. {
  72. final DirCacheBuilder b = treeB.builder();
  73. final DirCacheBuilder o = treeO.builder();
  74. final DirCacheBuilder p = treeP.builder();
  75. final DirCacheBuilder t = treeT.builder();
  76. b.add(createEntry("a", FileMode.REGULAR_FILE));
  77. o.add(createEntry("a", FileMode.REGULAR_FILE));
  78. o.add(createEntry("o", FileMode.REGULAR_FILE));
  79. p.add(createEntry("a", FileMode.REGULAR_FILE, "q"));
  80. p.add(createEntry("p-fail", FileMode.REGULAR_FILE));
  81. t.add(createEntry("a", FileMode.REGULAR_FILE));
  82. t.add(createEntry("t", FileMode.REGULAR_FILE));
  83. b.finish();
  84. o.finish();
  85. p.finish();
  86. t.finish();
  87. }
  88. final ObjectInserter ow = db.newObjectInserter();
  89. final ObjectId B = commit(ow, treeB, new ObjectId[] {});
  90. final ObjectId O = commit(ow, treeO, new ObjectId[] { B });
  91. final ObjectId P = commit(ow, treeP, new ObjectId[] { B });
  92. final ObjectId T = commit(ow, treeT, new ObjectId[] { P });
  93. ThreeWayMerger twm = MergeStrategy.SIMPLE_TWO_WAY_IN_CORE.newMerger(db);
  94. twm.setBase(P);
  95. boolean merge = twm.merge(new ObjectId[] { O, T });
  96. assertTrue(merge);
  97. final TreeWalk tw = new TreeWalk(db);
  98. tw.setRecursive(true);
  99. tw.reset(twm.getResultTreeId());
  100. assertTrue(tw.next());
  101. assertEquals("a", tw.getPathString());
  102. assertCorrectId(treeO, tw);
  103. assertTrue(tw.next());
  104. assertEquals("o", tw.getPathString());
  105. assertCorrectId(treeO, tw);
  106. assertTrue(tw.next());
  107. assertEquals("t", tw.getPathString());
  108. assertCorrectId(treeT, tw);
  109. assertFalse(tw.next());
  110. }
  111. @Test
  112. public void testRevert() throws Exception {
  113. // B---P---T
  114. //
  115. // Revert P, this should result in a tree with a
  116. // from B and t from T as the change to a in P
  117. // and addition of t in P is reverted.
  118. //
  119. // We use the standard merge, but change the order
  120. // of the sources.
  121. //
  122. final DirCache treeB = db.readDirCache();
  123. final DirCache treeP = db.readDirCache();
  124. final DirCache treeT = db.readDirCache();
  125. {
  126. final DirCacheBuilder b = treeB.builder();
  127. final DirCacheBuilder p = treeP.builder();
  128. final DirCacheBuilder t = treeT.builder();
  129. b.add(createEntry("a", FileMode.REGULAR_FILE));
  130. p.add(createEntry("a", FileMode.REGULAR_FILE, "q"));
  131. p.add(createEntry("p-fail", FileMode.REGULAR_FILE));
  132. t.add(createEntry("a", FileMode.REGULAR_FILE, "q"));
  133. t.add(createEntry("p-fail", FileMode.REGULAR_FILE));
  134. t.add(createEntry("t", FileMode.REGULAR_FILE));
  135. b.finish();
  136. p.finish();
  137. t.finish();
  138. }
  139. final ObjectInserter ow = db.newObjectInserter();
  140. final ObjectId B = commit(ow, treeB, new ObjectId[] {});
  141. final ObjectId P = commit(ow, treeP, new ObjectId[] { B });
  142. final ObjectId T = commit(ow, treeT, new ObjectId[] { P });
  143. ThreeWayMerger twm = MergeStrategy.SIMPLE_TWO_WAY_IN_CORE.newMerger(db);
  144. twm.setBase(P);
  145. boolean merge = twm.merge(new ObjectId[] { B, T });
  146. assertTrue(merge);
  147. final TreeWalk tw = new TreeWalk(db);
  148. tw.setRecursive(true);
  149. tw.reset(twm.getResultTreeId());
  150. assertTrue(tw.next());
  151. assertEquals("a", tw.getPathString());
  152. assertCorrectId(treeB, tw);
  153. assertTrue(tw.next());
  154. assertEquals("t", tw.getPathString());
  155. assertCorrectId(treeT, tw);
  156. assertFalse(tw.next());
  157. }
  158. private static void assertCorrectId(final DirCache treeT, final TreeWalk tw) {
  159. assertEquals(treeT.getEntry(tw.getPathString()).getObjectId(), tw
  160. .getObjectId(0));
  161. }
  162. private static ObjectId commit(final ObjectInserter odi,
  163. final DirCache treeB,
  164. final ObjectId[] parentIds) throws Exception {
  165. final CommitBuilder c = new CommitBuilder();
  166. c.setTreeId(treeB.writeTree(odi));
  167. c.setAuthor(new PersonIdent("A U Thor", "a.u.thor", 1L, 0));
  168. c.setCommitter(c.getAuthor());
  169. c.setParentIds(parentIds);
  170. c.setMessage("Tree " + c.getTreeId().name());
  171. ObjectId id = odi.insert(c);
  172. odi.flush();
  173. return id;
  174. }
  175. }