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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.eclipse.jgit.lib.Constants.OBJ_BLOB;
  46. import static org.junit.Assert.assertEquals;
  47. import static org.junit.Assert.assertFalse;
  48. import static org.junit.Assert.assertTrue;
  49. import org.eclipse.jgit.dircache.DirCache;
  50. import org.eclipse.jgit.dircache.DirCacheBuilder;
  51. import org.eclipse.jgit.dircache.DirCacheEntry;
  52. import org.eclipse.jgit.lib.CommitBuilder;
  53. import org.eclipse.jgit.lib.Constants;
  54. import org.eclipse.jgit.lib.FileMode;
  55. import org.eclipse.jgit.lib.ObjectId;
  56. import org.eclipse.jgit.lib.ObjectInserter;
  57. import org.eclipse.jgit.lib.PersonIdent;
  58. import org.eclipse.jgit.lib.RepositoryTestCase;
  59. import org.eclipse.jgit.treewalk.TreeWalk;
  60. import org.junit.Test;
  61. public class CherryPickTest extends RepositoryTestCase {
  62. @Test
  63. public void testPick() throws Exception {
  64. // B---O
  65. // \----P---T
  66. //
  67. // Cherry-pick "T" onto "O". This shouldn't introduce "p-fail", which
  68. // was created by "P", nor should it modify "a", which was done by "P".
  69. //
  70. final DirCache treeB = db.readDirCache();
  71. final DirCache treeO = db.readDirCache();
  72. final DirCache treeP = db.readDirCache();
  73. final DirCache treeT = db.readDirCache();
  74. {
  75. final DirCacheBuilder b = treeB.builder();
  76. final DirCacheBuilder o = treeO.builder();
  77. final DirCacheBuilder p = treeP.builder();
  78. final DirCacheBuilder t = treeT.builder();
  79. b.add(makeEntry("a", FileMode.REGULAR_FILE));
  80. o.add(makeEntry("a", FileMode.REGULAR_FILE));
  81. o.add(makeEntry("o", FileMode.REGULAR_FILE));
  82. p.add(makeEntry("a", FileMode.REGULAR_FILE, "q"));
  83. p.add(makeEntry("p-fail", FileMode.REGULAR_FILE));
  84. t.add(makeEntry("a", FileMode.REGULAR_FILE));
  85. t.add(makeEntry("t", FileMode.REGULAR_FILE));
  86. b.finish();
  87. o.finish();
  88. p.finish();
  89. t.finish();
  90. }
  91. final ObjectInserter ow = db.newObjectInserter();
  92. final ObjectId B = commit(ow, treeB, new ObjectId[] {});
  93. final ObjectId O = commit(ow, treeO, new ObjectId[] { B });
  94. final ObjectId P = commit(ow, treeP, new ObjectId[] { B });
  95. final ObjectId T = commit(ow, treeT, new ObjectId[] { P });
  96. ThreeWayMerger twm = MergeStrategy.SIMPLE_TWO_WAY_IN_CORE.newMerger(db);
  97. twm.setBase(P);
  98. boolean merge = twm.merge(new ObjectId[] { O, T });
  99. assertTrue(merge);
  100. final TreeWalk tw = new TreeWalk(db);
  101. tw.setRecursive(true);
  102. tw.reset(twm.getResultTreeId());
  103. assertTrue(tw.next());
  104. assertEquals("a", tw.getPathString());
  105. assertCorrectId(treeO, tw);
  106. assertTrue(tw.next());
  107. assertEquals("o", tw.getPathString());
  108. assertCorrectId(treeO, tw);
  109. assertTrue(tw.next());
  110. assertEquals("t", tw.getPathString());
  111. assertCorrectId(treeT, tw);
  112. assertFalse(tw.next());
  113. }
  114. private void assertCorrectId(final DirCache treeT, final TreeWalk tw) {
  115. assertEquals(treeT.getEntry(tw.getPathString()).getObjectId(), tw
  116. .getObjectId(0));
  117. }
  118. private ObjectId commit(final ObjectInserter odi, final DirCache treeB,
  119. final ObjectId[] parentIds) throws Exception {
  120. final CommitBuilder c = new CommitBuilder();
  121. c.setTreeId(treeB.writeTree(odi));
  122. c.setAuthor(new PersonIdent("A U Thor", "a.u.thor", 1L, 0));
  123. c.setCommitter(c.getAuthor());
  124. c.setParentIds(parentIds);
  125. c.setMessage("Tree " + c.getTreeId().name());
  126. ObjectId id = odi.insert(c);
  127. odi.flush();
  128. return id;
  129. }
  130. private DirCacheEntry makeEntry(final String path, final FileMode mode)
  131. throws Exception {
  132. return makeEntry(path, mode, path);
  133. }
  134. private DirCacheEntry makeEntry(final String path, final FileMode mode,
  135. final String content) throws Exception {
  136. final DirCacheEntry ent = new DirCacheEntry(path);
  137. ent.setFileMode(mode);
  138. ent.setObjectId(new ObjectInserter.Formatter().idFor(OBJ_BLOB,
  139. Constants.encode(content)));
  140. return ent;
  141. }
  142. }