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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 java.util.TreeSet;
  59. import org.eclipse.jgit.lib.Ref.Storage;
  60. import org.eclipse.jgit.lib.RefUpdate.Result;
  61. import org.eclipse.jgit.storage.file.FileBasedConfig;
  62. import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
  63. import org.junit.Test;
  64. /**
  65. * Misc tests for refs. A lot of things are tested elsewhere so not having a
  66. * test for a ref related method, does not mean it is untested.
  67. */
  68. public class RefTest extends SampleDataRepositoryTestCase {
  69. private void writeSymref(String src, String dst) throws IOException {
  70. RefUpdate u = db.updateRef(src);
  71. switch (u.link(dst)) {
  72. case NEW:
  73. case FORCED:
  74. case NO_CHANGE:
  75. break;
  76. default:
  77. fail("link " + src + " to " + dst);
  78. }
  79. }
  80. @Test
  81. public void testRemoteNames() throws Exception {
  82. FileBasedConfig config = db.getConfig();
  83. config.setBoolean(ConfigConstants.CONFIG_REMOTE_SECTION,
  84. "origin", "dummy", true);
  85. config.setBoolean(ConfigConstants.CONFIG_REMOTE_SECTION,
  86. "ab/c", "dummy", true);
  87. config.save();
  88. assertEquals("[ab/c, origin]",
  89. new TreeSet<String>(db.getRemoteNames()).toString());
  90. // one-level deep remote branch
  91. assertEquals("master",
  92. db.shortenRemoteBranchName("refs/remotes/origin/master"));
  93. assertEquals("origin", db.getRemoteName("refs/remotes/origin/master"));
  94. // two-level deep remote branch
  95. assertEquals("masta/r",
  96. db.shortenRemoteBranchName("refs/remotes/origin/masta/r"));
  97. assertEquals("origin", db.getRemoteName("refs/remotes/origin/masta/r"));
  98. // Remote with slash and one-level deep branch name
  99. assertEquals("xmaster",
  100. db.shortenRemoteBranchName("refs/remotes/ab/c/xmaster"));
  101. assertEquals("ab/c", db.getRemoteName("refs/remotes/ab/c/xmaster"));
  102. // Remote with slash and two-level deep branch name
  103. assertEquals("xmasta/r",
  104. db.shortenRemoteBranchName("refs/remotes/ab/c/xmasta/r"));
  105. assertEquals("ab/c", db.getRemoteName("refs/remotes/ab/c/xmasta/r"));
  106. // no such remote
  107. assertNull(db.getRemoteName("refs/remotes/nosuchremote/x"));
  108. assertNull(db.shortenRemoteBranchName("refs/remotes/nosuchremote/x"));
  109. // no such remote too, no branch name either
  110. assertNull(db.getRemoteName("refs/remotes/abranch"));
  111. assertNull(db.shortenRemoteBranchName("refs/remotes/abranch"));
  112. // // local branch
  113. assertNull(db.getRemoteName("refs/heads/abranch"));
  114. assertNull(db.shortenRemoteBranchName("refs/heads/abranch"));
  115. }
  116. @Test
  117. public void testReadAllIncludingSymrefs() throws Exception {
  118. ObjectId masterId = db.resolve("refs/heads/master");
  119. RefUpdate updateRef = db.updateRef("refs/remotes/origin/master");
  120. updateRef.setNewObjectId(masterId);
  121. updateRef.setForceUpdate(true);
  122. updateRef.update();
  123. writeSymref("refs/remotes/origin/HEAD",
  124. "refs/remotes/origin/master");
  125. ObjectId r = db.resolve("refs/remotes/origin/HEAD");
  126. assertEquals(masterId, r);
  127. Map<String, Ref> allRefs = db.getAllRefs();
  128. Ref refHEAD = allRefs.get("refs/remotes/origin/HEAD");
  129. assertNotNull(refHEAD);
  130. assertEquals(masterId, refHEAD.getObjectId());
  131. assertFalse(refHEAD.isPeeled());
  132. assertNull(refHEAD.getPeeledObjectId());
  133. Ref refmaster = allRefs.get("refs/remotes/origin/master");
  134. assertEquals(masterId, refmaster.getObjectId());
  135. assertFalse(refmaster.isPeeled());
  136. assertNull(refmaster.getPeeledObjectId());
  137. }
  138. @Test
  139. public void testReadSymRefToPacked() throws IOException {
  140. writeSymref("HEAD", "refs/heads/b");
  141. Ref ref = db.getRef("HEAD");
  142. assertEquals(Ref.Storage.LOOSE, ref.getStorage());
  143. assertTrue("is symref", ref.isSymbolic());
  144. ref = ref.getTarget();
  145. assertEquals("refs/heads/b", ref.getName());
  146. assertEquals(Ref.Storage.PACKED, ref.getStorage());
  147. }
  148. @Test
  149. public void testReadSymRefToLoosePacked() throws IOException {
  150. ObjectId pid = db.resolve("refs/heads/master^");
  151. RefUpdate updateRef = db.updateRef("refs/heads/master");
  152. updateRef.setNewObjectId(pid);
  153. updateRef.setForceUpdate(true);
  154. Result update = updateRef.update();
  155. assertEquals(Result.FORCED, update); // internal
  156. writeSymref("HEAD", "refs/heads/master");
  157. Ref ref = db.getRef("HEAD");
  158. assertEquals(Ref.Storage.LOOSE, ref.getStorage());
  159. ref = ref.getTarget();
  160. assertEquals("refs/heads/master", ref.getName());
  161. assertEquals(Ref.Storage.LOOSE, ref.getStorage());
  162. }
  163. @Test
  164. public void testReadLooseRef() throws IOException {
  165. RefUpdate updateRef = db.updateRef("ref/heads/new");
  166. updateRef.setNewObjectId(db.resolve("refs/heads/master"));
  167. Result update = updateRef.update();
  168. assertEquals(Result.NEW, update);
  169. Ref ref = db.getRef("ref/heads/new");
  170. assertEquals(Storage.LOOSE, ref.getStorage());
  171. }
  172. /**
  173. * Let an "outsider" create a loose ref with the same name as a packed one
  174. *
  175. * @throws IOException
  176. * @throws InterruptedException
  177. */
  178. @Test
  179. public void testReadLoosePackedRef() throws IOException,
  180. InterruptedException {
  181. Ref ref = db.getRef("refs/heads/master");
  182. assertEquals(Storage.PACKED, ref.getStorage());
  183. FileOutputStream os = new FileOutputStream(new File(db.getDirectory(),
  184. "refs/heads/master"));
  185. os.write(ref.getObjectId().name().getBytes());
  186. os.write('\n');
  187. os.close();
  188. ref = db.getRef("refs/heads/master");
  189. assertEquals(Storage.LOOSE, ref.getStorage());
  190. }
  191. /**
  192. * Modify a packed ref using the API. This creates a loose ref too, ie.
  193. * LOOSE_PACKED
  194. *
  195. * @throws IOException
  196. */
  197. @Test
  198. public void testReadSimplePackedRefSameRepo() throws IOException {
  199. Ref ref = db.getRef("refs/heads/master");
  200. ObjectId pid = db.resolve("refs/heads/master^");
  201. assertEquals(Storage.PACKED, ref.getStorage());
  202. RefUpdate updateRef = db.updateRef("refs/heads/master");
  203. updateRef.setNewObjectId(pid);
  204. updateRef.setForceUpdate(true);
  205. Result update = updateRef.update();
  206. assertEquals(Result.FORCED, update);
  207. ref = db.getRef("refs/heads/master");
  208. assertEquals(Storage.LOOSE, ref.getStorage());
  209. }
  210. @Test
  211. public void testResolvedNamesBranch() throws IOException {
  212. Ref ref = db.getRef("a");
  213. assertEquals("refs/heads/a", ref.getName());
  214. }
  215. @Test
  216. public void testResolvedSymRef() throws IOException {
  217. Ref ref = db.getRef(Constants.HEAD);
  218. assertEquals(Constants.HEAD, ref.getName());
  219. assertTrue("is symbolic ref", ref.isSymbolic());
  220. assertSame(Ref.Storage.LOOSE, ref.getStorage());
  221. Ref dst = ref.getTarget();
  222. assertNotNull("has target", dst);
  223. assertEquals("refs/heads/master", dst.getName());
  224. assertSame(dst.getObjectId(), ref.getObjectId());
  225. assertSame(dst.getPeeledObjectId(), ref.getPeeledObjectId());
  226. assertEquals(dst.isPeeled(), ref.isPeeled());
  227. }
  228. }