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 6.7KB

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