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.

RefTreeTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (C) 2016, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.internal.storage.reftree;
  44. import static org.eclipse.jgit.lib.Constants.HEAD;
  45. import static org.eclipse.jgit.lib.Constants.R_HEADS;
  46. import static org.eclipse.jgit.lib.Constants.R_TAGS;
  47. import static org.eclipse.jgit.lib.Ref.Storage.LOOSE;
  48. import static org.eclipse.jgit.lib.Ref.Storage.NEW;
  49. import static org.eclipse.jgit.transport.ReceiveCommand.Result.LOCK_FAILURE;
  50. import static org.eclipse.jgit.transport.ReceiveCommand.Result.NOT_ATTEMPTED;
  51. import static org.eclipse.jgit.transport.ReceiveCommand.Result.REJECTED_OTHER_REASON;
  52. import static org.junit.Assert.assertEquals;
  53. import static org.junit.Assert.assertFalse;
  54. import static org.junit.Assert.assertNotNull;
  55. import static org.junit.Assert.assertNull;
  56. import static org.junit.Assert.assertSame;
  57. import static org.junit.Assert.assertTrue;
  58. import java.io.IOException;
  59. import java.util.Arrays;
  60. import java.util.Collections;
  61. import org.eclipse.jgit.errors.MissingObjectException;
  62. import org.eclipse.jgit.internal.JGitText;
  63. import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
  64. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  65. import org.eclipse.jgit.junit.TestRepository;
  66. import org.eclipse.jgit.lib.ObjectId;
  67. import org.eclipse.jgit.lib.ObjectIdRef;
  68. import org.eclipse.jgit.lib.ObjectInserter;
  69. import org.eclipse.jgit.lib.ObjectReader;
  70. import org.eclipse.jgit.lib.Ref;
  71. import org.eclipse.jgit.lib.SymbolicRef;
  72. import org.eclipse.jgit.revwalk.RevBlob;
  73. import org.eclipse.jgit.revwalk.RevTag;
  74. import org.eclipse.jgit.revwalk.RevWalk;
  75. import org.eclipse.jgit.transport.ReceiveCommand;
  76. import org.junit.Before;
  77. import org.junit.Test;
  78. public class RefTreeTest {
  79. private static final String R_MASTER = R_HEADS + "master";
  80. private InMemoryRepository repo;
  81. private TestRepository<InMemoryRepository> git;
  82. @Before
  83. public void setUp() throws IOException {
  84. repo = new InMemoryRepository(new DfsRepositoryDescription("RefTree"));
  85. git = new TestRepository<>(repo);
  86. }
  87. @Test
  88. public void testEmptyTree() throws IOException {
  89. RefTree tree = RefTree.newEmptyTree();
  90. try (ObjectReader reader = repo.newObjectReader()) {
  91. assertNull(HEAD, tree.exactRef(reader, HEAD));
  92. assertNull("master", tree.exactRef(reader, R_MASTER));
  93. }
  94. }
  95. @Test
  96. public void testApplyThenReadMaster() throws Exception {
  97. RefTree tree = RefTree.newEmptyTree();
  98. RevBlob id = git.blob("A");
  99. Command cmd = new Command(null, ref(R_MASTER, id));
  100. assertTrue(tree.apply(Collections.singletonList(cmd)));
  101. assertSame(NOT_ATTEMPTED, cmd.getResult());
  102. try (ObjectReader reader = repo.newObjectReader()) {
  103. Ref m = tree.exactRef(reader, R_MASTER);
  104. assertNotNull(R_MASTER, m);
  105. assertEquals(R_MASTER, m.getName());
  106. assertEquals(id, m.getObjectId());
  107. assertTrue("peeled", m.isPeeled());
  108. }
  109. }
  110. @Test
  111. public void testUpdateMaster() throws Exception {
  112. RefTree tree = RefTree.newEmptyTree();
  113. RevBlob id1 = git.blob("A");
  114. Command cmd1 = new Command(null, ref(R_MASTER, id1));
  115. assertTrue(tree.apply(Collections.singletonList(cmd1)));
  116. assertSame(NOT_ATTEMPTED, cmd1.getResult());
  117. RevBlob id2 = git.blob("B");
  118. Command cmd2 = new Command(ref(R_MASTER, id1), ref(R_MASTER, id2));
  119. assertTrue(tree.apply(Collections.singletonList(cmd2)));
  120. assertSame(NOT_ATTEMPTED, cmd2.getResult());
  121. try (ObjectReader reader = repo.newObjectReader()) {
  122. Ref m = tree.exactRef(reader, R_MASTER);
  123. assertNotNull(R_MASTER, m);
  124. assertEquals(R_MASTER, m.getName());
  125. assertEquals(id2, m.getObjectId());
  126. assertTrue("peeled", m.isPeeled());
  127. }
  128. }
  129. @Test
  130. public void testHeadSymref() throws Exception {
  131. RefTree tree = RefTree.newEmptyTree();
  132. RevBlob id = git.blob("A");
  133. Command cmd1 = new Command(null, ref(R_MASTER, id));
  134. Command cmd2 = new Command(null, symref(HEAD, R_MASTER));
  135. assertTrue(tree.apply(Arrays.asList(new Command[] { cmd1, cmd2 })));
  136. assertSame(NOT_ATTEMPTED, cmd1.getResult());
  137. assertSame(NOT_ATTEMPTED, cmd2.getResult());
  138. try (ObjectReader reader = repo.newObjectReader()) {
  139. Ref m = tree.exactRef(reader, HEAD);
  140. assertNotNull(HEAD, m);
  141. assertEquals(HEAD, m.getName());
  142. assertTrue("symbolic", m.isSymbolic());
  143. assertNotNull(m.getTarget());
  144. assertEquals(R_MASTER, m.getTarget().getName());
  145. assertEquals(id, m.getTarget().getObjectId());
  146. }
  147. // Writing flushes some buffers, re-read from blob.
  148. ObjectId newId = write(tree);
  149. try (ObjectReader reader = repo.newObjectReader();
  150. RevWalk rw = new RevWalk(reader)) {
  151. tree = RefTree.read(reader, rw.parseTree(newId));
  152. Ref m = tree.exactRef(reader, HEAD);
  153. assertEquals(R_MASTER, m.getTarget().getName());
  154. }
  155. }
  156. @Test
  157. public void testTagIsPeeled() throws Exception {
  158. String name = "v1.0";
  159. RefTree tree = RefTree.newEmptyTree();
  160. RevBlob id = git.blob("A");
  161. RevTag tag = git.tag(name, id);
  162. String ref = R_TAGS + name;
  163. Command cmd = create(ref, tag);
  164. assertTrue(tree.apply(Collections.singletonList(cmd)));
  165. assertSame(NOT_ATTEMPTED, cmd.getResult());
  166. try (ObjectReader reader = repo.newObjectReader()) {
  167. Ref m = tree.exactRef(reader, ref);
  168. assertNotNull(ref, m);
  169. assertEquals(ref, m.getName());
  170. assertEquals(tag, m.getObjectId());
  171. assertTrue("peeled", m.isPeeled());
  172. assertEquals(id, m.getPeeledObjectId());
  173. }
  174. }
  175. @Test
  176. public void testApplyAlreadyExists() throws Exception {
  177. RefTree tree = RefTree.newEmptyTree();
  178. RevBlob a = git.blob("A");
  179. Command cmd = new Command(null, ref(R_MASTER, a));
  180. assertTrue(tree.apply(Collections.singletonList(cmd)));
  181. ObjectId treeId = write(tree);
  182. RevBlob b = git.blob("B");
  183. Command cmd1 = create(R_MASTER, b);
  184. Command cmd2 = create(R_MASTER, b);
  185. assertFalse(tree.apply(Arrays.asList(new Command[] { cmd1, cmd2 })));
  186. assertSame(LOCK_FAILURE, cmd1.getResult());
  187. assertSame(REJECTED_OTHER_REASON, cmd2.getResult());
  188. assertEquals(JGitText.get().transactionAborted, cmd2.getMessage());
  189. assertEquals(treeId, write(tree));
  190. }
  191. @Test
  192. public void testApplyWrongOldId() throws Exception {
  193. RefTree tree = RefTree.newEmptyTree();
  194. RevBlob a = git.blob("A");
  195. Command cmd = new Command(null, ref(R_MASTER, a));
  196. assertTrue(tree.apply(Collections.singletonList(cmd)));
  197. ObjectId treeId = write(tree);
  198. RevBlob b = git.blob("B");
  199. RevBlob c = git.blob("C");
  200. Command cmd1 = update(R_MASTER, b, c);
  201. Command cmd2 = create(R_MASTER, b);
  202. assertFalse(tree.apply(Arrays.asList(new Command[] { cmd1, cmd2 })));
  203. assertSame(LOCK_FAILURE, cmd1.getResult());
  204. assertSame(REJECTED_OTHER_REASON, cmd2.getResult());
  205. assertEquals(JGitText.get().transactionAborted, cmd2.getMessage());
  206. assertEquals(treeId, write(tree));
  207. }
  208. @Test
  209. public void testApplyWrongOldIdButAlreadyCurrentIsNoOp() throws Exception {
  210. RefTree tree = RefTree.newEmptyTree();
  211. RevBlob a = git.blob("A");
  212. Command cmd = new Command(null, ref(R_MASTER, a));
  213. assertTrue(tree.apply(Collections.singletonList(cmd)));
  214. ObjectId treeId = write(tree);
  215. RevBlob b = git.blob("B");
  216. cmd = update(R_MASTER, b, a);
  217. assertTrue(tree.apply(Collections.singletonList(cmd)));
  218. assertEquals(treeId, write(tree));
  219. }
  220. @Test
  221. public void testApplyCannotCreateSubdirectory() throws Exception {
  222. RefTree tree = RefTree.newEmptyTree();
  223. RevBlob a = git.blob("A");
  224. Command cmd = new Command(null, ref(R_MASTER, a));
  225. assertTrue(tree.apply(Collections.singletonList(cmd)));
  226. ObjectId treeId = write(tree);
  227. RevBlob b = git.blob("B");
  228. Command cmd1 = create(R_MASTER + "/fail", b);
  229. assertFalse(tree.apply(Collections.singletonList(cmd1)));
  230. assertSame(LOCK_FAILURE, cmd1.getResult());
  231. assertEquals(treeId, write(tree));
  232. }
  233. @Test
  234. public void testApplyCannotCreateParentRef() throws Exception {
  235. RefTree tree = RefTree.newEmptyTree();
  236. RevBlob a = git.blob("A");
  237. Command cmd = new Command(null, ref(R_MASTER, a));
  238. assertTrue(tree.apply(Collections.singletonList(cmd)));
  239. ObjectId treeId = write(tree);
  240. RevBlob b = git.blob("B");
  241. Command cmd1 = create("refs/heads", b);
  242. assertFalse(tree.apply(Collections.singletonList(cmd1)));
  243. assertSame(LOCK_FAILURE, cmd1.getResult());
  244. assertEquals(treeId, write(tree));
  245. }
  246. private static Ref ref(String name, ObjectId id) {
  247. return new ObjectIdRef.PeeledNonTag(LOOSE, name, id);
  248. }
  249. private static Ref symref(String name, String dest) {
  250. Ref d = new ObjectIdRef.PeeledNonTag(NEW, dest, null);
  251. return new SymbolicRef(name, d);
  252. }
  253. private Command create(String name, ObjectId id)
  254. throws MissingObjectException, IOException {
  255. return update(name, ObjectId.zeroId(), id);
  256. }
  257. private Command update(String name, ObjectId oldId, ObjectId newId)
  258. throws MissingObjectException, IOException {
  259. try (RevWalk rw = new RevWalk(repo)) {
  260. return new Command(rw, new ReceiveCommand(oldId, newId, name));
  261. }
  262. }
  263. private ObjectId write(RefTree tree) throws IOException {
  264. try (ObjectInserter ins = repo.newObjectInserter()) {
  265. ObjectId id = tree.writeTree(ins);
  266. ins.flush();
  267. return id;
  268. }
  269. }
  270. }