Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

CherryPickTest.java 5.5KB

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