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.

RefTest.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
  61. import org.junit.Test;
  62. /**
  63. * Misc tests for refs. A lot of things are tested elsewhere so not having a
  64. * test for a ref related method, does not mean it is untested.
  65. */
  66. public class RefTest extends SampleDataRepositoryTestCase {
  67. private void writeSymref(String src, String dst) throws IOException {
  68. RefUpdate u = db.updateRef(src);
  69. switch (u.link(dst)) {
  70. case NEW:
  71. case FORCED:
  72. case NO_CHANGE:
  73. break;
  74. default:
  75. fail("link " + src + " to " + dst);
  76. }
  77. }
  78. @Test
  79. public void testReadAllIncludingSymrefs() throws Exception {
  80. ObjectId masterId = db.resolve("refs/heads/master");
  81. RefUpdate updateRef = db.updateRef("refs/remotes/origin/master");
  82. updateRef.setNewObjectId(masterId);
  83. updateRef.setForceUpdate(true);
  84. updateRef.update();
  85. writeSymref("refs/remotes/origin/HEAD",
  86. "refs/remotes/origin/master");
  87. ObjectId r = db.resolve("refs/remotes/origin/HEAD");
  88. assertEquals(masterId, r);
  89. Map<String, Ref> allRefs = db.getAllRefs();
  90. Ref refHEAD = allRefs.get("refs/remotes/origin/HEAD");
  91. assertNotNull(refHEAD);
  92. assertEquals(masterId, refHEAD.getObjectId());
  93. assertFalse(refHEAD.isPeeled());
  94. assertNull(refHEAD.getPeeledObjectId());
  95. Ref refmaster = allRefs.get("refs/remotes/origin/master");
  96. assertEquals(masterId, refmaster.getObjectId());
  97. assertFalse(refmaster.isPeeled());
  98. assertNull(refmaster.getPeeledObjectId());
  99. }
  100. @Test
  101. public void testReadSymRefToPacked() throws IOException {
  102. writeSymref("HEAD", "refs/heads/b");
  103. Ref ref = db.getRef("HEAD");
  104. assertEquals(Ref.Storage.LOOSE, ref.getStorage());
  105. assertTrue("is symref", ref.isSymbolic());
  106. ref = ref.getTarget();
  107. assertEquals("refs/heads/b", ref.getName());
  108. assertEquals(Ref.Storage.PACKED, ref.getStorage());
  109. }
  110. @Test
  111. public void testReadSymRefToLoosePacked() throws IOException {
  112. ObjectId pid = db.resolve("refs/heads/master^");
  113. RefUpdate updateRef = db.updateRef("refs/heads/master");
  114. updateRef.setNewObjectId(pid);
  115. updateRef.setForceUpdate(true);
  116. Result update = updateRef.update();
  117. assertEquals(Result.FORCED, update); // internal
  118. writeSymref("HEAD", "refs/heads/master");
  119. Ref ref = db.getRef("HEAD");
  120. assertEquals(Ref.Storage.LOOSE, ref.getStorage());
  121. ref = ref.getTarget();
  122. assertEquals("refs/heads/master", ref.getName());
  123. assertEquals(Ref.Storage.LOOSE, ref.getStorage());
  124. }
  125. @Test
  126. public void testReadLooseRef() throws IOException {
  127. RefUpdate updateRef = db.updateRef("ref/heads/new");
  128. updateRef.setNewObjectId(db.resolve("refs/heads/master"));
  129. Result update = updateRef.update();
  130. assertEquals(Result.NEW, update);
  131. Ref ref = db.getRef("ref/heads/new");
  132. assertEquals(Storage.LOOSE, ref.getStorage());
  133. }
  134. /**
  135. * Let an "outsider" create a loose ref with the same name as a packed one
  136. *
  137. * @throws IOException
  138. * @throws InterruptedException
  139. */
  140. @Test
  141. public void testReadLoosePackedRef() throws IOException,
  142. InterruptedException {
  143. Ref ref = db.getRef("refs/heads/master");
  144. assertEquals(Storage.PACKED, ref.getStorage());
  145. FileOutputStream os = new FileOutputStream(new File(db.getDirectory(),
  146. "refs/heads/master"));
  147. os.write(ref.getObjectId().name().getBytes());
  148. os.write('\n');
  149. os.close();
  150. ref = db.getRef("refs/heads/master");
  151. assertEquals(Storage.LOOSE, ref.getStorage());
  152. }
  153. /**
  154. * Modify a packed ref using the API. This creates a loose ref too, ie.
  155. * LOOSE_PACKED
  156. *
  157. * @throws IOException
  158. */
  159. @Test
  160. public void testReadSimplePackedRefSameRepo() throws IOException {
  161. Ref ref = db.getRef("refs/heads/master");
  162. ObjectId pid = db.resolve("refs/heads/master^");
  163. assertEquals(Storage.PACKED, ref.getStorage());
  164. RefUpdate updateRef = db.updateRef("refs/heads/master");
  165. updateRef.setNewObjectId(pid);
  166. updateRef.setForceUpdate(true);
  167. Result update = updateRef.update();
  168. assertEquals(Result.FORCED, update);
  169. ref = db.getRef("refs/heads/master");
  170. assertEquals(Storage.LOOSE, ref.getStorage());
  171. }
  172. @Test
  173. public void testResolvedNamesBranch() throws IOException {
  174. Ref ref = db.getRef("a");
  175. assertEquals("refs/heads/a", ref.getName());
  176. }
  177. @Test
  178. public void testResolvedSymRef() throws IOException {
  179. Ref ref = db.getRef(Constants.HEAD);
  180. assertEquals(Constants.HEAD, ref.getName());
  181. assertTrue("is symbolic ref", ref.isSymbolic());
  182. assertSame(Ref.Storage.LOOSE, ref.getStorage());
  183. Ref dst = ref.getTarget();
  184. assertNotNull("has target", dst);
  185. assertEquals("refs/heads/master", dst.getName());
  186. assertSame(dst.getObjectId(), ref.getObjectId());
  187. assertSame(dst.getPeeledObjectId(), ref.getPeeledObjectId());
  188. assertEquals(dst.isPeeled(), ref.isPeeled());
  189. }
  190. }