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.

RefTest.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (C) 2009-2010, Google Inc.
  3. * Copyright (C) 2009, Robin Rosenberg
  4. * Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.lib;
  46. import static org.eclipse.jgit.junit.Assert.assertEquals;
  47. import static org.junit.Assert.assertEquals;
  48. import static org.junit.Assert.assertFalse;
  49. import static org.junit.Assert.assertNotNull;
  50. import static org.junit.Assert.assertNull;
  51. import static org.junit.Assert.assertSame;
  52. import static org.junit.Assert.assertTrue;
  53. import static org.junit.Assert.fail;
  54. import java.io.File;
  55. import java.io.FileOutputStream;
  56. import java.io.IOException;
  57. import java.util.Map;
  58. import org.eclipse.jgit.lib.Ref.Storage;
  59. import org.eclipse.jgit.lib.RefUpdate.Result;
  60. import org.junit.Test;
  61. /**
  62. * Misc tests for refs. A lot of things are tested elsewhere so not having a
  63. * test for a ref related method, does not mean it is untested.
  64. */
  65. public class RefTest extends SampleDataRepositoryTestCase {
  66. private void writeSymref(String src, String dst) throws IOException {
  67. RefUpdate u = db.updateRef(src);
  68. switch (u.link(dst)) {
  69. case NEW:
  70. case FORCED:
  71. case NO_CHANGE:
  72. break;
  73. default:
  74. fail("link " + src + " to " + dst);
  75. }
  76. }
  77. @Test
  78. public void testReadAllIncludingSymrefs() throws Exception {
  79. ObjectId masterId = db.resolve("refs/heads/master");
  80. RefUpdate updateRef = db.updateRef("refs/remotes/origin/master");
  81. updateRef.setNewObjectId(masterId);
  82. updateRef.setForceUpdate(true);
  83. updateRef.update();
  84. writeSymref("refs/remotes/origin/HEAD",
  85. "refs/remotes/origin/master");
  86. ObjectId r = db.resolve("refs/remotes/origin/HEAD");
  87. assertEquals(masterId, r);
  88. Map<String, Ref> allRefs = db.getAllRefs();
  89. Ref refHEAD = allRefs.get("refs/remotes/origin/HEAD");
  90. assertNotNull(refHEAD);
  91. assertEquals(masterId, refHEAD.getObjectId());
  92. assertFalse(refHEAD.isPeeled());
  93. assertNull(refHEAD.getPeeledObjectId());
  94. Ref refmaster = allRefs.get("refs/remotes/origin/master");
  95. assertEquals(masterId, refmaster.getObjectId());
  96. assertFalse(refmaster.isPeeled());
  97. assertNull(refmaster.getPeeledObjectId());
  98. }
  99. @Test
  100. public void testReadSymRefToPacked() throws IOException {
  101. writeSymref("HEAD", "refs/heads/b");
  102. Ref ref = db.getRef("HEAD");
  103. assertEquals(Ref.Storage.LOOSE, ref.getStorage());
  104. assertTrue("is symref", ref.isSymbolic());
  105. ref = ref.getTarget();
  106. assertEquals("refs/heads/b", ref.getName());
  107. assertEquals(Ref.Storage.PACKED, ref.getStorage());
  108. }
  109. @Test
  110. public void testReadSymRefToLoosePacked() throws IOException {
  111. ObjectId pid = db.resolve("refs/heads/master^");
  112. RefUpdate updateRef = db.updateRef("refs/heads/master");
  113. updateRef.setNewObjectId(pid);
  114. updateRef.setForceUpdate(true);
  115. Result update = updateRef.update();
  116. assertEquals(Result.FORCED, update); // internal
  117. writeSymref("HEAD", "refs/heads/master");
  118. Ref ref = db.getRef("HEAD");
  119. assertEquals(Ref.Storage.LOOSE, ref.getStorage());
  120. ref = ref.getTarget();
  121. assertEquals("refs/heads/master", ref.getName());
  122. assertEquals(Ref.Storage.LOOSE, ref.getStorage());
  123. }
  124. @Test
  125. public void testReadLooseRef() throws IOException {
  126. RefUpdate updateRef = db.updateRef("ref/heads/new");
  127. updateRef.setNewObjectId(db.resolve("refs/heads/master"));
  128. Result update = updateRef.update();
  129. assertEquals(Result.NEW, update);
  130. Ref ref = db.getRef("ref/heads/new");
  131. assertEquals(Storage.LOOSE, ref.getStorage());
  132. }
  133. /**
  134. * Let an "outsider" create a loose ref with the same name as a packed one
  135. *
  136. * @throws IOException
  137. * @throws InterruptedException
  138. */
  139. @Test
  140. public void testReadLoosePackedRef() throws IOException,
  141. InterruptedException {
  142. Ref ref = db.getRef("refs/heads/master");
  143. assertEquals(Storage.PACKED, ref.getStorage());
  144. FileOutputStream os = new FileOutputStream(new File(db.getDirectory(),
  145. "refs/heads/master"));
  146. os.write(ref.getObjectId().name().getBytes());
  147. os.write('\n');
  148. os.close();
  149. ref = db.getRef("refs/heads/master");
  150. assertEquals(Storage.LOOSE, ref.getStorage());
  151. }
  152. /**
  153. * Modify a packed ref using the API. This creates a loose ref too, ie.
  154. * LOOSE_PACKED
  155. *
  156. * @throws IOException
  157. */
  158. @Test
  159. public void testReadSimplePackedRefSameRepo() throws IOException {
  160. Ref ref = db.getRef("refs/heads/master");
  161. ObjectId pid = db.resolve("refs/heads/master^");
  162. assertEquals(Storage.PACKED, ref.getStorage());
  163. RefUpdate updateRef = db.updateRef("refs/heads/master");
  164. updateRef.setNewObjectId(pid);
  165. updateRef.setForceUpdate(true);
  166. Result update = updateRef.update();
  167. assertEquals(Result.FORCED, update);
  168. ref = db.getRef("refs/heads/master");
  169. assertEquals(Storage.LOOSE, ref.getStorage());
  170. }
  171. @Test
  172. public void testResolvedNamesBranch() throws IOException {
  173. Ref ref = db.getRef("a");
  174. assertEquals("refs/heads/a", ref.getName());
  175. }
  176. @Test
  177. public void testResolvedSymRef() throws IOException {
  178. Ref ref = db.getRef(Constants.HEAD);
  179. assertEquals(Constants.HEAD, ref.getName());
  180. assertTrue("is symbolic ref", ref.isSymbolic());
  181. assertSame(Ref.Storage.LOOSE, ref.getStorage());
  182. Ref dst = ref.getTarget();
  183. assertNotNull("has target", dst);
  184. assertEquals("refs/heads/master", dst.getName());
  185. assertSame(dst.getObjectId(), ref.getObjectId());
  186. assertSame(dst.getPeeledObjectId(), ref.getPeeledObjectId());
  187. assertEquals(dst.isPeeled(), ref.isPeeled());
  188. }
  189. }