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.

CheckoutCommandTest.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at 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
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.api;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertNull;
  48. import static org.junit.Assert.assertTrue;
  49. import static org.junit.Assert.fail;
  50. import java.io.File;
  51. import java.io.FileInputStream;
  52. import java.io.IOException;
  53. import org.eclipse.jgit.api.CheckoutResult.Status;
  54. import org.eclipse.jgit.api.errors.InvalidRefNameException;
  55. import org.eclipse.jgit.api.errors.JGitInternalException;
  56. import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
  57. import org.eclipse.jgit.api.errors.RefNotFoundException;
  58. import org.eclipse.jgit.lib.Constants;
  59. import org.eclipse.jgit.lib.Ref;
  60. import org.eclipse.jgit.lib.RefUpdate;
  61. import org.eclipse.jgit.lib.RepositoryTestCase;
  62. import org.eclipse.jgit.revwalk.RevCommit;
  63. import org.eclipse.jgit.util.FileUtils;
  64. import org.junit.Before;
  65. import org.junit.Test;
  66. public class CheckoutCommandTest extends RepositoryTestCase {
  67. private Git git;
  68. RevCommit initialCommit;
  69. RevCommit secondCommit;
  70. @Override
  71. @Before
  72. public void setUp() throws Exception {
  73. super.setUp();
  74. git = new Git(db);
  75. // commit something
  76. writeTrashFile("Test.txt", "Hello world");
  77. git.add().addFilepattern("Test.txt").call();
  78. initialCommit = git.commit().setMessage("Initial commit").call();
  79. // create a master branch and switch to it
  80. git.branchCreate().setName("test").call();
  81. RefUpdate rup = db.updateRef(Constants.HEAD);
  82. rup.link("refs/heads/test");
  83. // commit something on the test branch
  84. writeTrashFile("Test.txt", "Some change");
  85. git.add().addFilepattern("Test.txt").call();
  86. secondCommit = git.commit().setMessage("Second commit").call();
  87. }
  88. @Test
  89. public void testSimpleCheckout() {
  90. try {
  91. git.checkout().setName("test").call();
  92. } catch (Exception e) {
  93. fail(e.getMessage());
  94. }
  95. }
  96. @Test
  97. public void testCheckout() {
  98. try {
  99. git.checkout().setName("test").call();
  100. assertEquals("[Test.txt, mode:100644, content:Some change]",
  101. indexState(CONTENT));
  102. Ref result = git.checkout().setName("master").call();
  103. assertEquals("[Test.txt, mode:100644, content:Hello world]",
  104. indexState(CONTENT));
  105. assertEquals("refs/heads/master", result.getName());
  106. assertEquals("refs/heads/master", git.getRepository()
  107. .getFullBranch());
  108. } catch (Exception e) {
  109. fail(e.getMessage());
  110. }
  111. }
  112. @Test
  113. public void testCreateBranchOnCheckout() throws IOException {
  114. try {
  115. git.checkout().setCreateBranch(true).setName("test2").call();
  116. } catch (Exception e) {
  117. fail(e.getMessage());
  118. }
  119. assertNotNull(db.getRef("test2"));
  120. }
  121. @Test
  122. public void testCheckoutToNonExistingBranch() throws JGitInternalException,
  123. RefAlreadyExistsException, InvalidRefNameException {
  124. try {
  125. git.checkout().setName("badbranch").call();
  126. fail("Should have failed");
  127. } catch (RefNotFoundException e) {
  128. // except to hit here
  129. }
  130. }
  131. @Test
  132. public void testCheckoutWithConflict() {
  133. CheckoutCommand co = git.checkout();
  134. try {
  135. writeTrashFile("Test.txt", "Another change");
  136. assertEquals(Status.NOT_TRIED, co.getResult().getStatus());
  137. co.setName("master").call();
  138. fail("Should have failed");
  139. } catch (Exception e) {
  140. assertEquals(Status.CONFLICTS, co.getResult().getStatus());
  141. assertTrue(co.getResult().getConflictList().contains("Test.txt"));
  142. }
  143. }
  144. @Test
  145. public void testCheckoutWithNonDeletedFiles() throws Exception {
  146. File testFile = writeTrashFile("temp", "");
  147. FileInputStream fis = new FileInputStream(testFile);
  148. try {
  149. FileUtils.delete(testFile);
  150. return;
  151. } catch (IOException e) {
  152. // the test makes only sense if deletion of
  153. // a file with open stream fails
  154. }
  155. fis.close();
  156. FileUtils.delete(testFile);
  157. CheckoutCommand co = git.checkout();
  158. // delete Test.txt in branch test
  159. testFile = new File(db.getWorkTree(), "Test.txt");
  160. assertTrue(testFile.exists());
  161. FileUtils.delete(testFile);
  162. assertFalse(testFile.exists());
  163. git.add().addFilepattern("Test.txt");
  164. git.commit().setMessage("Delete Test.txt").setAll(true).call();
  165. git.checkout().setName("master").call();
  166. assertTrue(testFile.exists());
  167. // lock the file so it can't be deleted (in Windows, that is)
  168. fis = new FileInputStream(testFile);
  169. try {
  170. assertEquals(Status.NOT_TRIED, co.getResult().getStatus());
  171. co.setName("test").call();
  172. assertTrue(testFile.exists());
  173. assertEquals(Status.NONDELETED, co.getResult().getStatus());
  174. assertTrue(co.getResult().getUndeletedList().contains("Test.txt"));
  175. } finally {
  176. fis.close();
  177. }
  178. }
  179. @Test
  180. public void testCheckoutCommit() {
  181. try {
  182. Ref result = git.checkout().setName(initialCommit.name()).call();
  183. assertEquals("[Test.txt, mode:100644, content:Hello world]",
  184. indexState(CONTENT));
  185. assertNull(result);
  186. assertEquals(initialCommit.name(), git.getRepository()
  187. .getFullBranch());
  188. } catch (Exception e) {
  189. fail(e.getMessage());
  190. }
  191. }
  192. }