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.

DirCacheCheckoutTest.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com> and
  3. * other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available under the
  6. * terms of the Eclipse Distribution License v1.0 which accompanies this
  7. * distribution, is reproduced below, and is available at
  8. * 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 without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice, this
  16. * list of conditions and the following disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * - Neither the name of the Eclipse Foundation, Inc. nor the names of its
  23. * contributors may be used to endorse or promote products derived from this
  24. * software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. package org.eclipse.jgit.lib;
  39. import static org.junit.Assert.assertEquals;
  40. import static org.junit.Assert.assertTrue;
  41. import java.io.File;
  42. import java.io.IOException;
  43. import java.util.List;
  44. import java.util.Map;
  45. import org.eclipse.jgit.api.Git;
  46. import org.eclipse.jgit.api.MergeResult.MergeStatus;
  47. import org.eclipse.jgit.api.errors.GitAPIException;
  48. import org.eclipse.jgit.api.errors.NoFilepatternException;
  49. import org.eclipse.jgit.dircache.DirCache;
  50. import org.eclipse.jgit.dircache.DirCacheCheckout;
  51. import org.eclipse.jgit.errors.CorruptObjectException;
  52. import org.eclipse.jgit.errors.NoWorkTreeException;
  53. import org.eclipse.jgit.lib.RefUpdate.Result;
  54. import org.eclipse.jgit.revwalk.RevCommit;
  55. import org.eclipse.jgit.revwalk.RevWalk;
  56. import org.junit.Test;
  57. public class DirCacheCheckoutTest extends ReadTreeTest {
  58. private DirCacheCheckout dco;
  59. @Override
  60. public void prescanTwoTrees(Tree head, Tree merge)
  61. throws IllegalStateException, IOException {
  62. DirCache dc = db.lockDirCache();
  63. try {
  64. dco = new DirCacheCheckout(db, head.getTreeId(), dc, merge.getTreeId());
  65. dco.preScanTwoTrees();
  66. } finally {
  67. dc.unlock();
  68. }
  69. }
  70. @Override
  71. public void checkout() throws IOException {
  72. DirCache dc = db.lockDirCache();
  73. try {
  74. dco = new DirCacheCheckout(db, theHead.getTreeId(), dc, theMerge.getTreeId());
  75. dco.checkout();
  76. } finally {
  77. dc.unlock();
  78. }
  79. }
  80. @Override
  81. public List<String> getRemoved() {
  82. return dco.getRemoved();
  83. }
  84. @Override
  85. public Map<String, ObjectId> getUpdated() {
  86. return dco.getUpdated();
  87. }
  88. @Override
  89. public List<String> getConflicts() {
  90. return dco.getConflicts();
  91. }
  92. @Test
  93. public void testResetHard() throws IOException, NoFilepatternException,
  94. GitAPIException {
  95. Git git = new Git(db);
  96. writeTrashFile("f", "f()");
  97. writeTrashFile("D/g", "g()");
  98. git.add().addFilepattern(".").call();
  99. git.commit().setMessage("inital").call();
  100. assertIndex(mkmap("f", "f()", "D/g", "g()"));
  101. git.branchCreate().setName("topic").call();
  102. writeTrashFile("f", "f()\nmaster");
  103. writeTrashFile("D/g", "g()\ng2()");
  104. writeTrashFile("E/h", "h()");
  105. git.add().addFilepattern(".").call();
  106. RevCommit master = git.commit().setMessage("master-1").call();
  107. assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
  108. checkoutBranch("refs/heads/topic");
  109. assertIndex(mkmap("f", "f()", "D/g", "g()"));
  110. writeTrashFile("f", "f()\nside");
  111. assertTrue(new File(db.getWorkTree(), "D/g").delete());
  112. writeTrashFile("G/i", "i()");
  113. git.add().addFilepattern(".").call();
  114. git.add().addFilepattern(".").setUpdate(true).call();
  115. RevCommit topic = git.commit().setMessage("topic-1").call();
  116. assertIndex(mkmap("f", "f()\nside", "G/i", "i()"));
  117. resetHard(master);
  118. assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
  119. resetHard(topic);
  120. assertIndex(mkmap("f", "f()\nside", "G/i", "i()"));
  121. assertWorkDir(mkmap("f", "f()\nside", "G/i", "i()"));
  122. assertEquals(MergeStatus.CONFLICTING, git.merge().include(master)
  123. .call().getMergeStatus());
  124. assertEquals(
  125. "[E/h, mode:100644][G/i, mode:100644][f, mode:100644, stage:1][f, mode:100644, stage:2][f, mode:100644, stage:3]",
  126. indexState(0));
  127. resetHard(master);
  128. assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
  129. assertWorkDir(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h",
  130. "h()"));
  131. }
  132. private DirCacheCheckout resetHard(RevCommit commit)
  133. throws NoWorkTreeException,
  134. CorruptObjectException, IOException {
  135. DirCacheCheckout dc;
  136. dc = new DirCacheCheckout(db, null, db.lockDirCache(),
  137. commit.getTree());
  138. dc.setFailOnConflict(true);
  139. assertTrue(dc.checkout());
  140. return dc;
  141. }
  142. private void checkoutBranch(String branchName)
  143. throws IllegalStateException, IOException {
  144. RevWalk walk = new RevWalk(db);
  145. RevCommit head = walk.parseCommit(db.resolve(Constants.HEAD));
  146. RevCommit branch = walk.parseCommit(db.resolve(branchName));
  147. DirCacheCheckout dco = new DirCacheCheckout(db, head.getTree(),
  148. db.lockDirCache(), branch.getTree());
  149. dco.setFailOnConflict(true);
  150. assertTrue(dco.checkout());
  151. walk.release();
  152. // update the HEAD
  153. RefUpdate refUpdate = db.updateRef(Constants.HEAD);
  154. assertEquals(Result.FORCED, refUpdate.link(branchName));
  155. }
  156. }