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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. private void writeNewRef(String name, ObjectId value) throws IOException {
  81. RefUpdate updateRef = db.updateRef(name);
  82. updateRef.setNewObjectId(value);
  83. assertEquals(RefUpdate.Result.NEW, updateRef.update());
  84. }
  85. @Test
  86. public void testRemoteNames() throws Exception {
  87. FileBasedConfig config = db.getConfig();
  88. config.setBoolean(ConfigConstants.CONFIG_REMOTE_SECTION,
  89. "origin", "dummy", true);
  90. config.setBoolean(ConfigConstants.CONFIG_REMOTE_SECTION,
  91. "ab/c", "dummy", true);
  92. config.save();
  93. assertEquals("[ab/c, origin]",
  94. new TreeSet<>(db.getRemoteNames()).toString());
  95. // one-level deep remote branch
  96. assertEquals("master",
  97. db.shortenRemoteBranchName("refs/remotes/origin/master"));
  98. assertEquals("origin", db.getRemoteName("refs/remotes/origin/master"));
  99. // two-level deep remote branch
  100. assertEquals("masta/r",
  101. db.shortenRemoteBranchName("refs/remotes/origin/masta/r"));
  102. assertEquals("origin", db.getRemoteName("refs/remotes/origin/masta/r"));
  103. // Remote with slash and one-level deep branch name
  104. assertEquals("xmaster",
  105. db.shortenRemoteBranchName("refs/remotes/ab/c/xmaster"));
  106. assertEquals("ab/c", db.getRemoteName("refs/remotes/ab/c/xmaster"));
  107. // Remote with slash and two-level deep branch name
  108. assertEquals("xmasta/r",
  109. db.shortenRemoteBranchName("refs/remotes/ab/c/xmasta/r"));
  110. assertEquals("ab/c", db.getRemoteName("refs/remotes/ab/c/xmasta/r"));
  111. // no such remote
  112. assertNull(db.getRemoteName("refs/remotes/nosuchremote/x"));
  113. assertNull(db.shortenRemoteBranchName("refs/remotes/nosuchremote/x"));
  114. // no such remote too, no branch name either
  115. assertNull(db.getRemoteName("refs/remotes/abranch"));
  116. assertNull(db.shortenRemoteBranchName("refs/remotes/abranch"));
  117. // // local branch
  118. assertNull(db.getRemoteName("refs/heads/abranch"));
  119. assertNull(db.shortenRemoteBranchName("refs/heads/abranch"));
  120. }
  121. @Test
  122. public void testReadAllIncludingSymrefs() throws Exception {
  123. ObjectId masterId = db.resolve("refs/heads/master");
  124. RefUpdate updateRef = db.updateRef("refs/remotes/origin/master");
  125. updateRef.setNewObjectId(masterId);
  126. updateRef.setForceUpdate(true);
  127. updateRef.update();
  128. writeSymref("refs/remotes/origin/HEAD",
  129. "refs/remotes/origin/master");
  130. ObjectId r = db.resolve("refs/remotes/origin/HEAD");
  131. assertEquals(masterId, r);
  132. Map<String, Ref> allRefs = db.getAllRefs();
  133. Ref refHEAD = allRefs.get("refs/remotes/origin/HEAD");
  134. assertNotNull(refHEAD);
  135. assertEquals(masterId, refHEAD.getObjectId());
  136. assertFalse(refHEAD.isPeeled());
  137. assertNull(refHEAD.getPeeledObjectId());
  138. Ref refmaster = allRefs.get("refs/remotes/origin/master");
  139. assertEquals(masterId, refmaster.getObjectId());
  140. assertFalse(refmaster.isPeeled());
  141. assertNull(refmaster.getPeeledObjectId());
  142. }
  143. @Test
  144. public void testReadSymRefToPacked() throws IOException {
  145. writeSymref("HEAD", "refs/heads/b");
  146. Ref ref = db.exactRef("HEAD");
  147. assertEquals(Ref.Storage.LOOSE, ref.getStorage());
  148. assertTrue("is symref", ref.isSymbolic());
  149. ref = ref.getTarget();
  150. assertEquals("refs/heads/b", ref.getName());
  151. assertEquals(Ref.Storage.PACKED, ref.getStorage());
  152. }
  153. @Test
  154. public void testReadSymRefToLoosePacked() throws IOException {
  155. ObjectId pid = db.resolve("refs/heads/master^");
  156. RefUpdate updateRef = db.updateRef("refs/heads/master");
  157. updateRef.setNewObjectId(pid);
  158. updateRef.setForceUpdate(true);
  159. Result update = updateRef.update();
  160. assertEquals(Result.FORCED, update); // internal
  161. writeSymref("HEAD", "refs/heads/master");
  162. Ref ref = db.exactRef("HEAD");
  163. assertEquals(Ref.Storage.LOOSE, ref.getStorage());
  164. ref = ref.getTarget();
  165. assertEquals("refs/heads/master", ref.getName());
  166. assertEquals(Ref.Storage.LOOSE, ref.getStorage());
  167. }
  168. @Test
  169. public void testReadLooseRef() throws IOException {
  170. RefUpdate updateRef = db.updateRef("ref/heads/new");
  171. updateRef.setNewObjectId(db.resolve("refs/heads/master"));
  172. Result update = updateRef.update();
  173. assertEquals(Result.NEW, update);
  174. Ref ref = db.exactRef("ref/heads/new");
  175. assertEquals(Storage.LOOSE, ref.getStorage());
  176. }
  177. @Test
  178. public void testGetShortRef() throws IOException {
  179. Ref ref = db.exactRef("refs/heads/master");
  180. assertEquals("refs/heads/master", ref.getName());
  181. assertEquals(db.resolve("refs/heads/master"), ref.getObjectId());
  182. }
  183. @Test
  184. public void testGetShortExactRef() throws IOException {
  185. assertNull(db.getRefDatabase().exactRef("master"));
  186. Ref ref = db.getRefDatabase().exactRef("HEAD");
  187. assertEquals("HEAD", ref.getName());
  188. assertEquals("refs/heads/master", ref.getTarget().getName());
  189. assertEquals(db.resolve("refs/heads/master"), ref.getObjectId());
  190. }
  191. @Test
  192. public void testRefsUnderRefs() throws IOException {
  193. ObjectId masterId = db.resolve("refs/heads/master");
  194. writeNewRef("refs/heads/refs/foo/bar", masterId);
  195. assertNull(db.getRefDatabase().exactRef("refs/foo/bar"));
  196. Ref ref = db.findRef("refs/foo/bar");
  197. assertEquals("refs/heads/refs/foo/bar", ref.getName());
  198. assertEquals(db.resolve("refs/heads/master"), ref.getObjectId());
  199. }
  200. @Test
  201. public void testAmbiguousRefsUnderRefs() throws IOException {
  202. ObjectId masterId = db.resolve("refs/heads/master");
  203. writeNewRef("refs/foo/bar", masterId);
  204. writeNewRef("refs/heads/refs/foo/bar", masterId);
  205. Ref exactRef = db.getRefDatabase().exactRef("refs/foo/bar");
  206. assertEquals("refs/foo/bar", exactRef.getName());
  207. assertEquals(masterId, exactRef.getObjectId());
  208. Ref ref = db.findRef("refs/foo/bar");
  209. assertEquals("refs/foo/bar", ref.getName());
  210. assertEquals(masterId, ref.getObjectId());
  211. }
  212. /**
  213. * Let an "outsider" create a loose ref with the same name as a packed one
  214. *
  215. * @throws IOException
  216. * @throws InterruptedException
  217. */
  218. @Test
  219. public void testReadLoosePackedRef() throws IOException,
  220. InterruptedException {
  221. Ref ref = db.exactRef("refs/heads/master");
  222. assertEquals(Storage.PACKED, ref.getStorage());
  223. FileOutputStream os = new FileOutputStream(new File(db.getDirectory(),
  224. "refs/heads/master"));
  225. os.write(ref.getObjectId().name().getBytes());
  226. os.write('\n');
  227. os.close();
  228. ref = db.exactRef("refs/heads/master");
  229. assertEquals(Storage.LOOSE, ref.getStorage());
  230. }
  231. /**
  232. * Modify a packed ref using the API. This creates a loose ref too, ie.
  233. * LOOSE_PACKED
  234. *
  235. * @throws IOException
  236. */
  237. @Test
  238. public void testReadSimplePackedRefSameRepo() throws IOException {
  239. Ref ref = db.exactRef("refs/heads/master");
  240. ObjectId pid = db.resolve("refs/heads/master^");
  241. assertEquals(Storage.PACKED, ref.getStorage());
  242. RefUpdate updateRef = db.updateRef("refs/heads/master");
  243. updateRef.setNewObjectId(pid);
  244. updateRef.setForceUpdate(true);
  245. Result update = updateRef.update();
  246. assertEquals(Result.FORCED, update);
  247. ref = db.exactRef("refs/heads/master");
  248. assertEquals(Storage.LOOSE, ref.getStorage());
  249. }
  250. @Test
  251. public void testResolvedNamesBranch() throws IOException {
  252. Ref ref = db.findRef("a");
  253. assertEquals("refs/heads/a", ref.getName());
  254. }
  255. @Test
  256. public void testResolvedSymRef() throws IOException {
  257. Ref ref = db.exactRef(Constants.HEAD);
  258. assertEquals(Constants.HEAD, ref.getName());
  259. assertTrue("is symbolic ref", ref.isSymbolic());
  260. assertSame(Ref.Storage.LOOSE, ref.getStorage());
  261. Ref dst = ref.getTarget();
  262. assertNotNull("has target", dst);
  263. assertEquals("refs/heads/master", dst.getName());
  264. assertSame(dst.getObjectId(), ref.getObjectId());
  265. assertSame(dst.getPeeledObjectId(), ref.getPeeledObjectId());
  266. assertEquals(dst.isPeeled(), ref.isPeeled());
  267. }
  268. }