Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

RefTreeTest.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (C) 2016, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.internal.storage.reftree;
  11. import static org.eclipse.jgit.lib.Constants.HEAD;
  12. import static org.eclipse.jgit.lib.Constants.R_HEADS;
  13. import static org.eclipse.jgit.lib.Constants.R_TAGS;
  14. import static org.eclipse.jgit.lib.Ref.Storage.LOOSE;
  15. import static org.eclipse.jgit.lib.Ref.Storage.NEW;
  16. import static org.eclipse.jgit.transport.ReceiveCommand.Result.LOCK_FAILURE;
  17. import static org.eclipse.jgit.transport.ReceiveCommand.Result.NOT_ATTEMPTED;
  18. import static org.eclipse.jgit.transport.ReceiveCommand.Result.REJECTED_OTHER_REASON;
  19. import static org.junit.Assert.assertEquals;
  20. import static org.junit.Assert.assertFalse;
  21. import static org.junit.Assert.assertNotNull;
  22. import static org.junit.Assert.assertNull;
  23. import static org.junit.Assert.assertSame;
  24. import static org.junit.Assert.assertTrue;
  25. import java.io.IOException;
  26. import java.util.Arrays;
  27. import java.util.Collections;
  28. import org.eclipse.jgit.errors.MissingObjectException;
  29. import org.eclipse.jgit.internal.JGitText;
  30. import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
  31. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  32. import org.eclipse.jgit.junit.TestRepository;
  33. import org.eclipse.jgit.lib.ObjectId;
  34. import org.eclipse.jgit.lib.ObjectIdRef;
  35. import org.eclipse.jgit.lib.ObjectInserter;
  36. import org.eclipse.jgit.lib.ObjectReader;
  37. import org.eclipse.jgit.lib.Ref;
  38. import org.eclipse.jgit.lib.SymbolicRef;
  39. import org.eclipse.jgit.revwalk.RevBlob;
  40. import org.eclipse.jgit.revwalk.RevTag;
  41. import org.eclipse.jgit.revwalk.RevWalk;
  42. import org.eclipse.jgit.transport.ReceiveCommand;
  43. import org.junit.Before;
  44. import org.junit.Test;
  45. public class RefTreeTest {
  46. private static final String R_MASTER = R_HEADS + "master";
  47. private InMemoryRepository repo;
  48. private TestRepository<InMemoryRepository> git;
  49. @Before
  50. public void setUp() throws IOException {
  51. repo = new InMemoryRepository(new DfsRepositoryDescription("RefTree"));
  52. git = new TestRepository<>(repo);
  53. }
  54. @Test
  55. public void testEmptyTree() throws IOException {
  56. RefTree tree = RefTree.newEmptyTree();
  57. try (ObjectReader reader = repo.newObjectReader()) {
  58. assertNull(HEAD, tree.exactRef(reader, HEAD));
  59. assertNull("master", tree.exactRef(reader, R_MASTER));
  60. }
  61. }
  62. @Test
  63. public void testApplyThenReadMaster() throws Exception {
  64. RefTree tree = RefTree.newEmptyTree();
  65. RevBlob id = git.blob("A");
  66. Command cmd = new Command(null, ref(R_MASTER, id));
  67. assertTrue(tree.apply(Collections.singletonList(cmd)));
  68. assertSame(NOT_ATTEMPTED, cmd.getResult());
  69. try (ObjectReader reader = repo.newObjectReader()) {
  70. Ref m = tree.exactRef(reader, R_MASTER);
  71. assertNotNull(R_MASTER, m);
  72. assertEquals(R_MASTER, m.getName());
  73. assertEquals(id, m.getObjectId());
  74. assertTrue("peeled", m.isPeeled());
  75. }
  76. }
  77. @Test
  78. public void testUpdateMaster() throws Exception {
  79. RefTree tree = RefTree.newEmptyTree();
  80. RevBlob id1 = git.blob("A");
  81. Command cmd1 = new Command(null, ref(R_MASTER, id1));
  82. assertTrue(tree.apply(Collections.singletonList(cmd1)));
  83. assertSame(NOT_ATTEMPTED, cmd1.getResult());
  84. RevBlob id2 = git.blob("B");
  85. Command cmd2 = new Command(ref(R_MASTER, id1), ref(R_MASTER, id2));
  86. assertTrue(tree.apply(Collections.singletonList(cmd2)));
  87. assertSame(NOT_ATTEMPTED, cmd2.getResult());
  88. try (ObjectReader reader = repo.newObjectReader()) {
  89. Ref m = tree.exactRef(reader, R_MASTER);
  90. assertNotNull(R_MASTER, m);
  91. assertEquals(R_MASTER, m.getName());
  92. assertEquals(id2, m.getObjectId());
  93. assertTrue("peeled", m.isPeeled());
  94. }
  95. }
  96. @Test
  97. public void testHeadSymref() throws Exception {
  98. RefTree tree = RefTree.newEmptyTree();
  99. RevBlob id = git.blob("A");
  100. Command cmd1 = new Command(null, ref(R_MASTER, id));
  101. Command cmd2 = new Command(null, symref(HEAD, R_MASTER));
  102. assertTrue(tree.apply(Arrays.asList(new Command[] { cmd1, cmd2 })));
  103. assertSame(NOT_ATTEMPTED, cmd1.getResult());
  104. assertSame(NOT_ATTEMPTED, cmd2.getResult());
  105. try (ObjectReader reader = repo.newObjectReader()) {
  106. Ref m = tree.exactRef(reader, HEAD);
  107. assertNotNull(HEAD, m);
  108. assertEquals(HEAD, m.getName());
  109. assertTrue("symbolic", m.isSymbolic());
  110. assertNotNull(m.getTarget());
  111. assertEquals(R_MASTER, m.getTarget().getName());
  112. assertEquals(id, m.getTarget().getObjectId());
  113. }
  114. // Writing flushes some buffers, re-read from blob.
  115. ObjectId newId = write(tree);
  116. try (ObjectReader reader = repo.newObjectReader();
  117. RevWalk rw = new RevWalk(reader)) {
  118. tree = RefTree.read(reader, rw.parseTree(newId));
  119. Ref m = tree.exactRef(reader, HEAD);
  120. assertEquals(R_MASTER, m.getTarget().getName());
  121. }
  122. }
  123. @Test
  124. public void testTagIsPeeled() throws Exception {
  125. String name = "v1.0";
  126. RefTree tree = RefTree.newEmptyTree();
  127. RevBlob id = git.blob("A");
  128. RevTag tag = git.tag(name, id);
  129. String ref = R_TAGS + name;
  130. Command cmd = create(ref, tag);
  131. assertTrue(tree.apply(Collections.singletonList(cmd)));
  132. assertSame(NOT_ATTEMPTED, cmd.getResult());
  133. try (ObjectReader reader = repo.newObjectReader()) {
  134. Ref m = tree.exactRef(reader, ref);
  135. assertNotNull(ref, m);
  136. assertEquals(ref, m.getName());
  137. assertEquals(tag, m.getObjectId());
  138. assertTrue("peeled", m.isPeeled());
  139. assertEquals(id, m.getPeeledObjectId());
  140. }
  141. }
  142. @Test
  143. public void testApplyAlreadyExists() throws Exception {
  144. RefTree tree = RefTree.newEmptyTree();
  145. RevBlob a = git.blob("A");
  146. Command cmd = new Command(null, ref(R_MASTER, a));
  147. assertTrue(tree.apply(Collections.singletonList(cmd)));
  148. ObjectId treeId = write(tree);
  149. RevBlob b = git.blob("B");
  150. Command cmd1 = create(R_MASTER, b);
  151. Command cmd2 = create(R_MASTER, b);
  152. assertFalse(tree.apply(Arrays.asList(new Command[] { cmd1, cmd2 })));
  153. assertSame(LOCK_FAILURE, cmd1.getResult());
  154. assertSame(REJECTED_OTHER_REASON, cmd2.getResult());
  155. assertEquals(JGitText.get().transactionAborted, cmd2.getMessage());
  156. assertEquals(treeId, write(tree));
  157. }
  158. @Test
  159. public void testApplyWrongOldId() throws Exception {
  160. RefTree tree = RefTree.newEmptyTree();
  161. RevBlob a = git.blob("A");
  162. Command cmd = new Command(null, ref(R_MASTER, a));
  163. assertTrue(tree.apply(Collections.singletonList(cmd)));
  164. ObjectId treeId = write(tree);
  165. RevBlob b = git.blob("B");
  166. RevBlob c = git.blob("C");
  167. Command cmd1 = update(R_MASTER, b, c);
  168. Command cmd2 = create(R_MASTER, b);
  169. assertFalse(tree.apply(Arrays.asList(new Command[] { cmd1, cmd2 })));
  170. assertSame(LOCK_FAILURE, cmd1.getResult());
  171. assertSame(REJECTED_OTHER_REASON, cmd2.getResult());
  172. assertEquals(JGitText.get().transactionAborted, cmd2.getMessage());
  173. assertEquals(treeId, write(tree));
  174. }
  175. @Test
  176. public void testApplyWrongOldIdButAlreadyCurrentIsNoOp() 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. cmd = update(R_MASTER, b, a);
  184. assertTrue(tree.apply(Collections.singletonList(cmd)));
  185. assertEquals(treeId, write(tree));
  186. }
  187. @Test
  188. public void testApplyCannotCreateSubdirectory() throws Exception {
  189. RefTree tree = RefTree.newEmptyTree();
  190. RevBlob a = git.blob("A");
  191. Command cmd = new Command(null, ref(R_MASTER, a));
  192. assertTrue(tree.apply(Collections.singletonList(cmd)));
  193. ObjectId treeId = write(tree);
  194. RevBlob b = git.blob("B");
  195. Command cmd1 = create(R_MASTER + "/fail", b);
  196. assertFalse(tree.apply(Collections.singletonList(cmd1)));
  197. assertSame(LOCK_FAILURE, cmd1.getResult());
  198. assertEquals(treeId, write(tree));
  199. }
  200. @Test
  201. public void testApplyCannotCreateParentRef() throws Exception {
  202. RefTree tree = RefTree.newEmptyTree();
  203. RevBlob a = git.blob("A");
  204. Command cmd = new Command(null, ref(R_MASTER, a));
  205. assertTrue(tree.apply(Collections.singletonList(cmd)));
  206. ObjectId treeId = write(tree);
  207. RevBlob b = git.blob("B");
  208. Command cmd1 = create("refs/heads", b);
  209. assertFalse(tree.apply(Collections.singletonList(cmd1)));
  210. assertSame(LOCK_FAILURE, cmd1.getResult());
  211. assertEquals(treeId, write(tree));
  212. }
  213. private static Ref ref(String name, ObjectId id) {
  214. return new ObjectIdRef.PeeledNonTag(LOOSE, name, id);
  215. }
  216. private static Ref symref(String name, String dest) {
  217. Ref d = new ObjectIdRef.PeeledNonTag(NEW, dest, null);
  218. return new SymbolicRef(name, d);
  219. }
  220. private Command create(String name, ObjectId id)
  221. throws MissingObjectException, IOException {
  222. return update(name, ObjectId.zeroId(), id);
  223. }
  224. private Command update(String name, ObjectId oldId, ObjectId newId)
  225. throws MissingObjectException, IOException {
  226. try (RevWalk rw = new RevWalk(repo)) {
  227. return new Command(rw, new ReceiveCommand(oldId, newId, name));
  228. }
  229. }
  230. private ObjectId write(RefTree tree) throws IOException {
  231. try (ObjectInserter ins = repo.newObjectInserter()) {
  232. ObjectId id = tree.writeTree(ins);
  233. ins.flush();
  234. return id;
  235. }
  236. }
  237. }