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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 java.io.File;
  40. import java.io.IOException;
  41. import java.util.List;
  42. import java.util.Map;
  43. import org.eclipse.jgit.api.Git;
  44. import org.eclipse.jgit.api.MergeResult.MergeStatus;
  45. import org.eclipse.jgit.api.errors.GitAPIException;
  46. import org.eclipse.jgit.api.errors.NoFilepatternException;
  47. import org.eclipse.jgit.dircache.DirCache;
  48. import org.eclipse.jgit.dircache.DirCacheCheckout;
  49. import org.eclipse.jgit.errors.CorruptObjectException;
  50. import org.eclipse.jgit.errors.NoWorkTreeException;
  51. import org.eclipse.jgit.lib.RefUpdate.Result;
  52. import org.eclipse.jgit.revwalk.RevCommit;
  53. import org.eclipse.jgit.revwalk.RevWalk;
  54. public class DirCacheCheckoutTest extends ReadTreeTest {
  55. private DirCacheCheckout dco;
  56. @Override
  57. public void prescanTwoTrees(Tree head, Tree merge)
  58. throws IllegalStateException, IOException {
  59. DirCache dc = db.lockDirCache();
  60. try {
  61. dco = new DirCacheCheckout(db, head.getTreeId(), dc, merge.getTreeId());
  62. dco.preScanTwoTrees();
  63. } finally {
  64. dc.unlock();
  65. }
  66. }
  67. @Override
  68. public void checkout() throws IOException {
  69. DirCache dc = db.lockDirCache();
  70. try {
  71. dco = new DirCacheCheckout(db, theHead.getTreeId(), dc, theMerge.getTreeId());
  72. dco.checkout();
  73. } finally {
  74. dc.unlock();
  75. }
  76. }
  77. @Override
  78. public List<String> getRemoved() {
  79. return dco.getRemoved();
  80. }
  81. @Override
  82. public Map<String, ObjectId> getUpdated() {
  83. return dco.getUpdated();
  84. }
  85. @Override
  86. public List<String> getConflicts() {
  87. return dco.getConflicts();
  88. }
  89. public void testResetHard() throws IOException, NoFilepatternException,
  90. GitAPIException {
  91. Git git = new Git(db);
  92. writeTrashFile("f", "f()");
  93. writeTrashFile("D/g", "g()");
  94. git.add().addFilepattern(".").call();
  95. git.commit().setMessage("inital").call();
  96. assertIndex(mkmap("f", "f()", "D/g", "g()"));
  97. git.branchCreate().setName("topic").call();
  98. writeTrashFile("f", "f()\nmaster");
  99. writeTrashFile("D/g", "g()\ng2()");
  100. writeTrashFile("E/h", "h()");
  101. git.add().addFilepattern(".").call();
  102. RevCommit master = git.commit().setMessage("master-1").call();
  103. assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
  104. checkoutBranch("refs/heads/topic");
  105. assertIndex(mkmap("f", "f()", "D/g", "g()"));
  106. writeTrashFile("f", "f()\nside");
  107. assertTrue(new File(db.getWorkTree(), "D/g").delete());
  108. writeTrashFile("G/i", "i()");
  109. git.add().addFilepattern(".").call();
  110. git.add().addFilepattern(".").setUpdate(true).call();
  111. RevCommit topic = git.commit().setMessage("topic-1").call();
  112. assertIndex(mkmap("f", "f()\nside", "G/i", "i()"));
  113. resetHard(master);
  114. assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
  115. resetHard(topic);
  116. assertIndex(mkmap("f", "f()\nside", "G/i", "i()"));
  117. assertWorkDir(mkmap("f", "f()\nside", "G/i", "i()"));
  118. assertEquals(MergeStatus.CONFLICTING, git.merge().include(master)
  119. .call().getMergeStatus());
  120. assertEquals(
  121. "[E/h, mode:100644][G/i, mode:100644][f, mode:100644, stage:1][f, mode:100644, stage:2][f, mode:100644, stage:3]",
  122. indexState(0));
  123. resetHard(master);
  124. assertIndex(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h", "h()"));
  125. assertWorkDir(mkmap("f", "f()\nmaster", "D/g", "g()\ng2()", "E/h",
  126. "h()"));
  127. }
  128. private DirCacheCheckout resetHard(RevCommit commit)
  129. throws NoWorkTreeException,
  130. CorruptObjectException, IOException {
  131. DirCacheCheckout dc;
  132. dc = new DirCacheCheckout(db, null, db.lockDirCache(),
  133. commit.getTree());
  134. dc.setFailOnConflict(true);
  135. assertTrue(dc.checkout());
  136. return dc;
  137. }
  138. private void checkoutBranch(String branchName)
  139. throws IllegalStateException, IOException {
  140. RevWalk walk = new RevWalk(db);
  141. RevCommit head = walk.parseCommit(db.resolve(Constants.HEAD));
  142. RevCommit branch = walk.parseCommit(db.resolve(branchName));
  143. DirCacheCheckout dco = new DirCacheCheckout(db, head.getTree(),
  144. db.lockDirCache(), branch.getTree());
  145. dco.setFailOnConflict(true);
  146. assertTrue(dco.checkout());
  147. walk.release();
  148. // update the HEAD
  149. RefUpdate refUpdate = db.updateRef(Constants.HEAD);
  150. assertEquals(Result.FORCED, refUpdate.link(branchName));
  151. }
  152. }