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.

ReftableTest.java 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. /*
  2. * Copyright (C) 2017, 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.reftable;
  44. import static org.eclipse.jgit.lib.Constants.HEAD;
  45. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
  46. import static org.eclipse.jgit.lib.Constants.R_HEADS;
  47. import static org.eclipse.jgit.lib.MoreAsserts.assertThrows;
  48. import static org.eclipse.jgit.lib.Ref.Storage.NEW;
  49. import static org.eclipse.jgit.lib.Ref.Storage.PACKED;
  50. import static org.hamcrest.CoreMatchers.containsString;
  51. import static org.junit.Assert.assertEquals;
  52. import static org.junit.Assert.assertFalse;
  53. import static org.junit.Assert.assertNotNull;
  54. import static org.junit.Assert.assertNull;
  55. import static org.junit.Assert.assertSame;
  56. import static org.junit.Assert.assertThat;
  57. import static org.junit.Assert.assertTrue;
  58. import static org.junit.Assert.fail;
  59. import java.io.ByteArrayOutputStream;
  60. import java.io.IOException;
  61. import java.util.ArrayList;
  62. import java.util.Arrays;
  63. import java.util.Collection;
  64. import java.util.Collections;
  65. import java.util.List;
  66. import java.util.concurrent.locks.ReentrantLock;
  67. import java.util.stream.Collectors;
  68. import org.eclipse.jgit.internal.JGitText;
  69. import org.eclipse.jgit.internal.storage.io.BlockSource;
  70. import org.eclipse.jgit.internal.storage.reftable.ReftableWriter.Stats;
  71. import org.eclipse.jgit.lib.ObjectId;
  72. import org.eclipse.jgit.lib.ObjectIdRef;
  73. import org.eclipse.jgit.lib.PersonIdent;
  74. import org.eclipse.jgit.lib.Ref;
  75. import org.eclipse.jgit.lib.ReflogEntry;
  76. import org.eclipse.jgit.lib.SymbolicRef;
  77. import org.hamcrest.Matchers;
  78. import org.junit.Test;
  79. public class ReftableTest {
  80. private static final String MASTER = "refs/heads/master";
  81. private static final String NEXT = "refs/heads/next";
  82. private static final String V1_0 = "refs/tags/v1.0";
  83. private Stats stats;
  84. @Test
  85. public void emptyTable() throws IOException {
  86. byte[] table = write();
  87. assertEquals(92 /* header, footer */, table.length);
  88. assertEquals('R', table[0]);
  89. assertEquals('E', table[1]);
  90. assertEquals('F', table[2]);
  91. assertEquals('T', table[3]);
  92. assertEquals(0x01, table[4]);
  93. assertTrue(ReftableConstants.isFileHeaderMagic(table, 0, 8));
  94. assertTrue(ReftableConstants.isFileHeaderMagic(table, 24, 92));
  95. Reftable t = read(table);
  96. try (RefCursor rc = t.allRefs()) {
  97. assertFalse(rc.next());
  98. }
  99. try (RefCursor rc = t.seekRef(HEAD)) {
  100. assertFalse(rc.next());
  101. }
  102. try (RefCursor rc = t.seekRefsWithPrefix(R_HEADS)) {
  103. assertFalse(rc.next());
  104. }
  105. try (LogCursor rc = t.allLogs()) {
  106. assertFalse(rc.next());
  107. }
  108. }
  109. @Test
  110. public void emptyVirtualTableFromRefs() throws IOException {
  111. Reftable t = Reftable.from(Collections.emptyList());
  112. try (RefCursor rc = t.allRefs()) {
  113. assertFalse(rc.next());
  114. }
  115. try (RefCursor rc = t.seekRef(HEAD)) {
  116. assertFalse(rc.next());
  117. }
  118. try (LogCursor rc = t.allLogs()) {
  119. assertFalse(rc.next());
  120. }
  121. }
  122. @Test
  123. public void estimateCurrentBytesOneRef() throws IOException {
  124. Ref exp = ref(MASTER, 1);
  125. int expBytes = 24 + 4 + 5 + 4 + MASTER.length() + 20 + 68;
  126. byte[] table;
  127. ReftableConfig cfg = new ReftableConfig();
  128. cfg.setIndexObjects(false);
  129. try (ByteArrayOutputStream buf = new ByteArrayOutputStream()) {
  130. ReftableWriter writer = new ReftableWriter(buf).setConfig(cfg);
  131. writer.begin();
  132. assertEquals(92, writer.estimateTotalBytes());
  133. writer.writeRef(exp);
  134. assertEquals(expBytes, writer.estimateTotalBytes());
  135. writer.finish();
  136. table = buf.toByteArray();
  137. }
  138. assertEquals(expBytes, table.length);
  139. }
  140. @SuppressWarnings("boxing")
  141. @Test
  142. public void estimateCurrentBytesWithIndex() throws IOException {
  143. List<Ref> refs = new ArrayList<>();
  144. for (int i = 1; i <= 5670; i++) {
  145. refs.add(ref(String.format("refs/heads/%04d", i), i));
  146. }
  147. ReftableConfig cfg = new ReftableConfig();
  148. cfg.setIndexObjects(false);
  149. cfg.setMaxIndexLevels(1);
  150. int expBytes = 147860;
  151. byte[] table;
  152. try (ByteArrayOutputStream buf = new ByteArrayOutputStream()) {
  153. ReftableWriter writer = new ReftableWriter(buf).setConfig(cfg);
  154. writer.begin();
  155. writer.sortAndWriteRefs(refs);
  156. assertEquals(expBytes, writer.estimateTotalBytes());
  157. writer.finish();
  158. stats = writer.getStats();
  159. table = buf.toByteArray();
  160. }
  161. assertEquals(1, stats.refIndexLevels());
  162. assertEquals(expBytes, table.length);
  163. }
  164. @Test
  165. public void oneIdRef() throws IOException {
  166. Ref exp = ref(MASTER, 1);
  167. byte[] table = write(exp);
  168. assertEquals(24 + 4 + 5 + 4 + MASTER.length() + 20 + 68, table.length);
  169. ReftableReader t = read(table);
  170. try (RefCursor rc = t.allRefs()) {
  171. assertTrue(rc.next());
  172. Ref act = rc.getRef();
  173. assertNotNull(act);
  174. assertEquals(PACKED, act.getStorage());
  175. assertTrue(act.isPeeled());
  176. assertFalse(act.isSymbolic());
  177. assertEquals(exp.getName(), act.getName());
  178. assertEquals(exp.getObjectId(), act.getObjectId());
  179. assertEquals(0, act.getUpdateIndex());
  180. assertNull(act.getPeeledObjectId());
  181. assertFalse(rc.wasDeleted());
  182. assertFalse(rc.next());
  183. }
  184. try (RefCursor rc = t.seekRef(MASTER)) {
  185. assertTrue(rc.next());
  186. Ref act = rc.getRef();
  187. assertNotNull(act);
  188. assertEquals(exp.getName(), act.getName());
  189. assertEquals(0, act.getUpdateIndex());
  190. assertFalse(rc.next());
  191. }
  192. }
  193. @Test
  194. public void oneTagRef() throws IOException {
  195. Ref exp = tag(V1_0, 1, 2);
  196. byte[] table = write(exp);
  197. assertEquals(24 + 4 + 5 + 3 + V1_0.length() + 40 + 68, table.length);
  198. ReftableReader t = read(table);
  199. try (RefCursor rc = t.allRefs()) {
  200. assertTrue(rc.next());
  201. Ref act = rc.getRef();
  202. assertNotNull(act);
  203. assertEquals(PACKED, act.getStorage());
  204. assertTrue(act.isPeeled());
  205. assertFalse(act.isSymbolic());
  206. assertEquals(exp.getName(), act.getName());
  207. assertEquals(exp.getObjectId(), act.getObjectId());
  208. assertEquals(exp.getPeeledObjectId(), act.getPeeledObjectId());
  209. assertEquals(0, act.getUpdateIndex());
  210. }
  211. }
  212. @Test
  213. public void oneSymbolicRef() throws IOException {
  214. Ref exp = sym(HEAD, MASTER);
  215. byte[] table = write(exp);
  216. assertEquals(
  217. 24 + 4 + 5 + 2 + HEAD.length() + 2 + MASTER.length() + 68,
  218. table.length);
  219. ReftableReader t = read(table);
  220. try (RefCursor rc = t.allRefs()) {
  221. assertTrue(rc.next());
  222. Ref act = rc.getRef();
  223. assertNotNull(act);
  224. assertTrue(act.isSymbolic());
  225. assertEquals(exp.getName(), act.getName());
  226. assertNotNull(act.getLeaf());
  227. assertEquals(MASTER, act.getTarget().getName());
  228. assertNull(act.getObjectId());
  229. assertEquals(0, act.getUpdateIndex());
  230. }
  231. }
  232. @Test
  233. public void resolveSymbolicRef() throws IOException {
  234. Reftable t = read(write(
  235. sym(HEAD, "refs/heads/tmp"),
  236. sym("refs/heads/tmp", MASTER),
  237. ref(MASTER, 1)));
  238. Ref head = t.exactRef(HEAD);
  239. assertNull(head.getObjectId());
  240. assertEquals("refs/heads/tmp", head.getTarget().getName());
  241. assertEquals(0, head.getUpdateIndex());
  242. head = t.resolve(head);
  243. assertNotNull(head);
  244. assertEquals(id(1), head.getObjectId());
  245. assertEquals(0, head.getUpdateIndex());
  246. Ref master = t.exactRef(MASTER);
  247. assertNotNull(master);
  248. assertSame(master, t.resolve(master));
  249. assertEquals(0, master.getUpdateIndex());
  250. }
  251. @Test
  252. public void failDeepChainOfSymbolicRef() throws IOException {
  253. Reftable t = read(write(
  254. sym(HEAD, "refs/heads/1"),
  255. sym("refs/heads/1", "refs/heads/2"),
  256. sym("refs/heads/2", "refs/heads/3"),
  257. sym("refs/heads/3", "refs/heads/4"),
  258. sym("refs/heads/4", "refs/heads/5"),
  259. sym("refs/heads/5", MASTER),
  260. ref(MASTER, 1)));
  261. Ref head = t.exactRef(HEAD);
  262. assertNull(head.getObjectId());
  263. assertNull(t.resolve(head));
  264. }
  265. @Test
  266. public void oneDeletedRef() throws IOException {
  267. String name = "refs/heads/gone";
  268. Ref exp = newRef(name);
  269. byte[] table = write(exp);
  270. assertEquals(24 + 4 + 5 + 3 + name.length() + 68, table.length);
  271. ReftableReader t = read(table);
  272. try (RefCursor rc = t.allRefs()) {
  273. assertFalse(rc.next());
  274. }
  275. t.setIncludeDeletes(true);
  276. try (RefCursor rc = t.allRefs()) {
  277. assertTrue(rc.next());
  278. Ref act = rc.getRef();
  279. assertNotNull(act);
  280. assertFalse(act.isSymbolic());
  281. assertEquals(name, act.getName());
  282. assertEquals(NEW, act.getStorage());
  283. assertNull(act.getObjectId());
  284. assertTrue(rc.wasDeleted());
  285. }
  286. }
  287. @Test
  288. public void seekNotFound() throws IOException {
  289. Ref exp = ref(MASTER, 1);
  290. ReftableReader t = read(write(exp));
  291. try (RefCursor rc = t.seekRef("refs/heads/a")) {
  292. assertFalse(rc.next());
  293. }
  294. try (RefCursor rc = t.seekRef("refs/heads/n")) {
  295. assertFalse(rc.next());
  296. }
  297. }
  298. @Test
  299. public void namespaceNotFound() throws IOException {
  300. Ref exp = ref(MASTER, 1);
  301. ReftableReader t = read(write(exp));
  302. try (RefCursor rc = t.seekRefsWithPrefix("refs/changes/")) {
  303. assertFalse(rc.next());
  304. }
  305. try (RefCursor rc = t.seekRefsWithPrefix("refs/tags/")) {
  306. assertFalse(rc.next());
  307. }
  308. }
  309. @Test
  310. public void namespaceHeads() throws IOException {
  311. Ref master = ref(MASTER, 1);
  312. Ref next = ref(NEXT, 2);
  313. Ref v1 = tag(V1_0, 3, 4);
  314. ReftableReader t = read(write(master, next, v1));
  315. try (RefCursor rc = t.seekRefsWithPrefix("refs/tags/")) {
  316. assertTrue(rc.next());
  317. assertEquals(V1_0, rc.getRef().getName());
  318. assertEquals(0, rc.getRef().getUpdateIndex());
  319. assertFalse(rc.next());
  320. }
  321. try (RefCursor rc = t.seekRefsWithPrefix("refs/heads/")) {
  322. assertTrue(rc.next());
  323. assertEquals(MASTER, rc.getRef().getName());
  324. assertEquals(0, rc.getRef().getUpdateIndex());
  325. assertTrue(rc.next());
  326. assertEquals(NEXT, rc.getRef().getName());
  327. assertEquals(0, rc.getRef().getUpdateIndex());
  328. assertFalse(rc.next());
  329. }
  330. }
  331. @SuppressWarnings("boxing")
  332. @Test
  333. public void indexScan() throws IOException {
  334. List<Ref> refs = new ArrayList<>();
  335. for (int i = 1; i <= 5670; i++) {
  336. refs.add(ref(String.format("refs/heads/%04d", i), i));
  337. }
  338. byte[] table = write(refs);
  339. assertTrue(stats.refIndexLevels() > 0);
  340. assertTrue(stats.refIndexSize() > 0);
  341. assertScan(refs, read(table));
  342. }
  343. @SuppressWarnings("boxing")
  344. @Test
  345. public void indexSeek() throws IOException {
  346. List<Ref> refs = new ArrayList<>();
  347. for (int i = 1; i <= 5670; i++) {
  348. refs.add(ref(String.format("refs/heads/%04d", i), i));
  349. }
  350. byte[] table = write(refs);
  351. assertTrue(stats.refIndexLevels() > 0);
  352. assertTrue(stats.refIndexSize() > 0);
  353. assertSeek(refs, read(table));
  354. }
  355. @SuppressWarnings("boxing")
  356. @Test
  357. public void noIndexScan() throws IOException {
  358. List<Ref> refs = new ArrayList<>();
  359. for (int i = 1; i <= 567; i++) {
  360. refs.add(ref(String.format("refs/heads/%03d", i), i));
  361. }
  362. byte[] table = write(refs);
  363. assertEquals(0, stats.refIndexLevels());
  364. assertEquals(0, stats.refIndexSize());
  365. assertEquals(table.length, stats.totalBytes());
  366. assertScan(refs, read(table));
  367. }
  368. @SuppressWarnings("boxing")
  369. @Test
  370. public void noIndexSeek() throws IOException {
  371. List<Ref> refs = new ArrayList<>();
  372. for (int i = 1; i <= 567; i++) {
  373. refs.add(ref(String.format("refs/heads/%03d", i), i));
  374. }
  375. byte[] table = write(refs);
  376. assertEquals(0, stats.refIndexLevels());
  377. assertSeek(refs, read(table));
  378. }
  379. @Test
  380. public void invalidRefWriteOrderSortAndWrite() {
  381. Ref master = ref(MASTER, 1);
  382. ReftableWriter writer = new ReftableWriter(new ByteArrayOutputStream())
  383. .setMinUpdateIndex(1)
  384. .setMaxUpdateIndex(1)
  385. .begin();
  386. List<Ref> refs = new ArrayList<>();
  387. refs.add(master);
  388. refs.add(master);
  389. IllegalArgumentException e = assertThrows(
  390. IllegalArgumentException.class,
  391. () -> writer.sortAndWriteRefs(refs));
  392. assertThat(e.getMessage(), containsString("records must be increasing"));
  393. }
  394. @Test
  395. public void invalidReflogWriteOrderUpdateIndex() throws IOException {
  396. ReftableWriter writer = new ReftableWriter(new ByteArrayOutputStream())
  397. .setMinUpdateIndex(1)
  398. .setMaxUpdateIndex(2)
  399. .begin();
  400. PersonIdent who = new PersonIdent("Log", "Ger", 1500079709, -8 * 60);
  401. String msg = "test";
  402. writer.writeLog(MASTER, 1, who, ObjectId.zeroId(), id(1), msg);
  403. IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
  404. () -> writer.writeLog(
  405. MASTER, 2, who, ObjectId.zeroId(), id(2), msg));
  406. assertThat(e.getMessage(), containsString("records must be increasing"));
  407. }
  408. @Test
  409. public void invalidReflogWriteOrderName() throws IOException {
  410. ReftableWriter writer = new ReftableWriter(new ByteArrayOutputStream())
  411. .setMinUpdateIndex(1)
  412. .setMaxUpdateIndex(1)
  413. .begin();
  414. PersonIdent who = new PersonIdent("Log", "Ger", 1500079709, -8 * 60);
  415. String msg = "test";
  416. writer.writeLog(NEXT, 1, who, ObjectId.zeroId(), id(1), msg);
  417. IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
  418. () -> writer.writeLog(
  419. MASTER, 1, who, ObjectId.zeroId(), id(2), msg));
  420. assertThat(e.getMessage(), containsString("records must be increasing"));
  421. }
  422. @Test
  423. public void withReflog() throws IOException {
  424. Ref master = ref(MASTER, 1);
  425. Ref next = ref(NEXT, 2);
  426. PersonIdent who = new PersonIdent("Log", "Ger", 1500079709, -8 * 60);
  427. String msg = "test";
  428. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  429. ReftableWriter writer = new ReftableWriter(buffer)
  430. .setMinUpdateIndex(1)
  431. .setMaxUpdateIndex(1)
  432. .begin();
  433. writer.writeRef(master);
  434. writer.writeRef(next);
  435. writer.writeLog(MASTER, 1, who, ObjectId.zeroId(), id(1), msg);
  436. writer.writeLog(NEXT, 1, who, ObjectId.zeroId(), id(2), msg);
  437. writer.finish();
  438. byte[] table = buffer.toByteArray();
  439. assertEquals(247, table.length);
  440. ReftableReader t = read(table);
  441. try (RefCursor rc = t.allRefs()) {
  442. assertTrue(rc.next());
  443. assertEquals(MASTER, rc.getRef().getName());
  444. assertEquals(id(1), rc.getRef().getObjectId());
  445. assertEquals(1, rc.getRef().getUpdateIndex());
  446. assertTrue(rc.next());
  447. assertEquals(NEXT, rc.getRef().getName());
  448. assertEquals(id(2), rc.getRef().getObjectId());
  449. assertEquals(1, rc.getRef().getUpdateIndex());
  450. assertFalse(rc.next());
  451. }
  452. try (LogCursor lc = t.allLogs()) {
  453. assertTrue(lc.next());
  454. assertEquals(MASTER, lc.getRefName());
  455. assertEquals(1, lc.getUpdateIndex());
  456. assertEquals(ObjectId.zeroId(), lc.getReflogEntry().getOldId());
  457. assertEquals(id(1), lc.getReflogEntry().getNewId());
  458. assertEquals(who, lc.getReflogEntry().getWho());
  459. assertEquals(msg, lc.getReflogEntry().getComment());
  460. assertTrue(lc.next());
  461. assertEquals(NEXT, lc.getRefName());
  462. assertEquals(1, lc.getUpdateIndex());
  463. assertEquals(ObjectId.zeroId(), lc.getReflogEntry().getOldId());
  464. assertEquals(id(2), lc.getReflogEntry().getNewId());
  465. assertEquals(who, lc.getReflogEntry().getWho());
  466. assertEquals(msg, lc.getReflogEntry().getComment());
  467. assertFalse(lc.next());
  468. }
  469. }
  470. @Test
  471. public void reflogReader() throws IOException {
  472. Ref master = ref(MASTER, 1);
  473. Ref next = ref(NEXT, 2);
  474. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  475. ReftableWriter writer = new ReftableWriter(buffer).setMinUpdateIndex(1)
  476. .setMaxUpdateIndex(1).begin();
  477. writer.writeRef(master);
  478. writer.writeRef(next);
  479. PersonIdent who1 = new PersonIdent("Log", "Ger", 1500079709, -8 * 60);
  480. writer.writeLog(MASTER, 3, who1, ObjectId.zeroId(), id(1), "1");
  481. PersonIdent who2 = new PersonIdent("Log", "Ger", 1500079710, -8 * 60);
  482. writer.writeLog(MASTER, 2, who2, id(1), id(2), "2");
  483. PersonIdent who3 = new PersonIdent("Log", "Ger", 1500079711, -8 * 60);
  484. writer.writeLog(MASTER, 1, who3, id(2), id(3), "3");
  485. writer.finish();
  486. byte[] table = buffer.toByteArray();
  487. ReentrantLock lock = new ReentrantLock();
  488. ReftableReader t = read(table);
  489. ReftableReflogReader rlr = new ReftableReflogReader(lock, t, MASTER);
  490. assertEquals(rlr.getLastEntry().getWho(), who1);
  491. List<PersonIdent> all = rlr.getReverseEntries().stream()
  492. .map(x -> x.getWho()).collect(Collectors.toList());
  493. Matchers.contains(all, who3, who2, who1);
  494. assertEquals(rlr.getReverseEntry(1).getWho(), who2);
  495. List<ReflogEntry> reverse2 = rlr.getReverseEntries(2);
  496. Matchers.contains(reverse2, who3, who2);
  497. List<PersonIdent> more = rlr.getReverseEntries(4).stream()
  498. .map(x -> x.getWho()).collect(Collectors.toList());
  499. assertEquals(all, more);
  500. }
  501. @Test
  502. public void allRefs() throws IOException {
  503. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  504. ReftableConfig cfg = new ReftableConfig();
  505. cfg.setRefBlockSize(1024);
  506. cfg.setLogBlockSize(1024);
  507. cfg.setAlignBlocks(true);
  508. ReftableWriter writer = new ReftableWriter(buffer)
  509. .setMinUpdateIndex(1)
  510. .setMaxUpdateIndex(1)
  511. .setConfig(cfg)
  512. .begin();
  513. PersonIdent who = new PersonIdent("Log", "Ger", 1500079709, -8 * 60);
  514. // Fill out the 1st ref block.
  515. List<String> names = new ArrayList<>();
  516. for (int i = 0; i < 4; i++) {
  517. String name = new String(new char[220]).replace("\0", String.format("%c", i + 'a'));
  518. names.add(name);
  519. writer.writeRef(ref(name, i));
  520. }
  521. // Add some log data.
  522. writer.writeLog(MASTER, 1, who, ObjectId.zeroId(), id(1), "msg");
  523. writer.finish();
  524. byte[] table = buffer.toByteArray();
  525. ReftableReader t = read(table);
  526. RefCursor c = t.allRefs();
  527. int j = 0;
  528. while (c.next()) {
  529. assertEquals(names.get(j), c.getRef().getName());
  530. j++;
  531. }
  532. }
  533. @Test
  534. public void reflogSeek() throws IOException {
  535. PersonIdent who = new PersonIdent("Log", "Ger", 1500079709, -8 * 60);
  536. String msg = "test";
  537. String msgNext = "test next";
  538. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  539. ReftableWriter writer = new ReftableWriter(buffer)
  540. .setMinUpdateIndex(1)
  541. .setMaxUpdateIndex(1)
  542. .begin();
  543. writer.writeLog(MASTER, 1, who, ObjectId.zeroId(), id(1), msg);
  544. writer.writeLog(NEXT, 1, who, ObjectId.zeroId(), id(2), msgNext);
  545. writer.finish();
  546. byte[] table = buffer.toByteArray();
  547. ReftableReader t = read(table);
  548. try (LogCursor c = t.seekLog(MASTER, Long.MAX_VALUE)) {
  549. assertTrue(c.next());
  550. assertEquals(c.getReflogEntry().getComment(), msg);
  551. }
  552. try (LogCursor c = t.seekLog(MASTER, 0)) {
  553. assertFalse(c.next());
  554. }
  555. try (LogCursor c = t.seekLog(MASTER, 1)) {
  556. assertTrue(c.next());
  557. assertEquals(c.getUpdateIndex(), 1);
  558. assertEquals(c.getReflogEntry().getComment(), msg);
  559. }
  560. try (LogCursor c = t.seekLog(NEXT, Long.MAX_VALUE)) {
  561. assertTrue(c.next());
  562. assertEquals(c.getReflogEntry().getComment(), msgNext);
  563. }
  564. try (LogCursor c = t.seekLog(NEXT, 0)) {
  565. assertFalse(c.next());
  566. }
  567. try (LogCursor c = t.seekLog(NEXT, 1)) {
  568. assertTrue(c.next());
  569. assertEquals(c.getUpdateIndex(), 1);
  570. assertEquals(c.getReflogEntry().getComment(), msgNext);
  571. }
  572. }
  573. @Test
  574. public void reflogSeekPrefix() throws IOException {
  575. PersonIdent who = new PersonIdent("Log", "Ger", 1500079709, -8 * 60);
  576. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  577. ReftableWriter writer = new ReftableWriter(buffer)
  578. .setMinUpdateIndex(1)
  579. .setMaxUpdateIndex(1)
  580. .begin();
  581. writer.writeLog("branchname", 1, who, ObjectId.zeroId(), id(1), "branchname");
  582. writer.finish();
  583. byte[] table = buffer.toByteArray();
  584. ReftableReader t = read(table);
  585. try (LogCursor c = t.seekLog("branch", Long.MAX_VALUE)) {
  586. // We find a reflog block, but the iteration won't confuse branchname
  587. // and branch.
  588. assertFalse(c.next());
  589. }
  590. }
  591. @Test
  592. public void onlyReflog() throws IOException {
  593. PersonIdent who = new PersonIdent("Log", "Ger", 1500079709, -8 * 60);
  594. String msg = "test";
  595. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  596. ReftableWriter writer = new ReftableWriter(buffer)
  597. .setMinUpdateIndex(1)
  598. .setMaxUpdateIndex(1)
  599. .begin();
  600. writer.writeLog(MASTER, 1, who, ObjectId.zeroId(), id(1), msg);
  601. writer.writeLog(NEXT, 1, who, ObjectId.zeroId(), id(2), msg);
  602. writer.finish();
  603. byte[] table = buffer.toByteArray();
  604. stats = writer.getStats();
  605. assertEquals(170, table.length);
  606. assertEquals(0, stats.refCount());
  607. assertEquals(0, stats.refBytes());
  608. assertEquals(0, stats.refIndexLevels());
  609. ReftableReader t = read(table);
  610. try (RefCursor rc = t.allRefs()) {
  611. assertFalse(rc.next());
  612. }
  613. try (RefCursor rc = t.seekRefsWithPrefix("refs/heads/")) {
  614. assertFalse(rc.next());
  615. }
  616. try (LogCursor lc = t.allLogs()) {
  617. assertTrue(lc.next());
  618. assertEquals(MASTER, lc.getRefName());
  619. assertEquals(1, lc.getUpdateIndex());
  620. assertEquals(ObjectId.zeroId(), lc.getReflogEntry().getOldId());
  621. assertEquals(id(1), lc.getReflogEntry().getNewId());
  622. assertEquals(who, lc.getReflogEntry().getWho());
  623. // compare string too, to catch tz differences.
  624. assertEquals(who.toExternalString(), lc.getReflogEntry().getWho().toExternalString());
  625. assertEquals(msg, lc.getReflogEntry().getComment());
  626. assertTrue(lc.next());
  627. assertEquals(NEXT, lc.getRefName());
  628. assertEquals(1, lc.getUpdateIndex());
  629. assertEquals(ObjectId.zeroId(), lc.getReflogEntry().getOldId());
  630. assertEquals(id(2), lc.getReflogEntry().getNewId());
  631. assertEquals(who, lc.getReflogEntry().getWho());
  632. assertEquals(msg, lc.getReflogEntry().getComment());
  633. assertFalse(lc.next());
  634. }
  635. }
  636. @SuppressWarnings("boxing")
  637. @Test
  638. public void logScan() throws IOException {
  639. ReftableConfig cfg = new ReftableConfig();
  640. cfg.setRefBlockSize(256);
  641. cfg.setLogBlockSize(2048);
  642. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  643. ReftableWriter writer = new ReftableWriter(cfg, buffer);
  644. writer.setMinUpdateIndex(1).setMaxUpdateIndex(1).begin();
  645. List<Ref> refs = new ArrayList<>();
  646. for (int i = 1; i <= 5670; i++) {
  647. Ref ref = ref(String.format("refs/heads/%04d", i), i);
  648. refs.add(ref);
  649. writer.writeRef(ref);
  650. }
  651. PersonIdent who = new PersonIdent("Log", "Ger", 1500079709, -8 * 60);
  652. for (Ref ref : refs) {
  653. writer.writeLog(ref.getName(), 1, who,
  654. ObjectId.zeroId(), ref.getObjectId(),
  655. "create " + ref.getName());
  656. }
  657. writer.finish();
  658. stats = writer.getStats();
  659. assertTrue(stats.logBytes() > 4096);
  660. byte[] table = buffer.toByteArray();
  661. ReftableReader t = read(table);
  662. try (LogCursor lc = t.allLogs()) {
  663. for (Ref exp : refs) {
  664. assertTrue("has " + exp.getName(), lc.next());
  665. assertEquals(exp.getName(), lc.getRefName());
  666. ReflogEntry entry = lc.getReflogEntry();
  667. assertNotNull(entry);
  668. assertEquals(who, entry.getWho());
  669. assertEquals(ObjectId.zeroId(), entry.getOldId());
  670. assertEquals(exp.getObjectId(), entry.getNewId());
  671. assertEquals("create " + exp.getName(), entry.getComment());
  672. }
  673. assertFalse(lc.next());
  674. }
  675. }
  676. @SuppressWarnings("boxing")
  677. @Test
  678. public void byObjectIdOneRefNoIndex() throws IOException {
  679. List<Ref> refs = new ArrayList<>();
  680. for (int i = 1; i <= 200; i++) {
  681. refs.add(ref(String.format("refs/heads/%02d", i), i));
  682. }
  683. refs.add(ref("refs/heads/master", 100));
  684. ReftableReader t = read(write(refs));
  685. assertEquals(0, stats.objIndexSize());
  686. try (RefCursor rc = t.byObjectId(id(42))) {
  687. assertTrue("has 42", rc.next());
  688. assertEquals("refs/heads/42", rc.getRef().getName());
  689. assertEquals(id(42), rc.getRef().getObjectId());
  690. assertEquals(0, rc.getRef().getUpdateIndex());
  691. assertFalse(rc.next());
  692. }
  693. try (RefCursor rc = t.byObjectId(id(100))) {
  694. assertTrue("has 100", rc.next());
  695. assertEquals("refs/heads/100", rc.getRef().getName());
  696. assertEquals(id(100), rc.getRef().getObjectId());
  697. assertTrue("has master", rc.next());
  698. assertEquals("refs/heads/master", rc.getRef().getName());
  699. assertEquals(id(100), rc.getRef().getObjectId());
  700. assertEquals(0, rc.getRef().getUpdateIndex());
  701. assertFalse(rc.next());
  702. }
  703. }
  704. @SuppressWarnings("boxing")
  705. @Test
  706. public void byObjectIdOneRefWithIndex() throws IOException {
  707. List<Ref> refs = new ArrayList<>();
  708. for (int i = 1; i <= 5200; i++) {
  709. refs.add(ref(String.format("refs/heads/%02d", i), i));
  710. }
  711. refs.add(ref("refs/heads/master", 100));
  712. ReftableReader t = read(write(refs));
  713. assertTrue(stats.objIndexSize() > 0);
  714. try (RefCursor rc = t.byObjectId(id(42))) {
  715. assertTrue("has 42", rc.next());
  716. assertEquals("refs/heads/42", rc.getRef().getName());
  717. assertEquals(id(42), rc.getRef().getObjectId());
  718. assertEquals(0, rc.getRef().getUpdateIndex());
  719. assertFalse(rc.next());
  720. }
  721. try (RefCursor rc = t.byObjectId(id(100))) {
  722. assertTrue("has 100", rc.next());
  723. assertEquals("refs/heads/100", rc.getRef().getName());
  724. assertEquals(id(100), rc.getRef().getObjectId());
  725. assertTrue("has master", rc.next());
  726. assertEquals("refs/heads/master", rc.getRef().getName());
  727. assertEquals(id(100), rc.getRef().getObjectId());
  728. assertEquals(0, rc.getRef().getUpdateIndex());
  729. assertFalse(rc.next());
  730. }
  731. }
  732. @Test
  733. public void unpeeledDoesNotWrite() {
  734. try {
  735. write(new ObjectIdRef.Unpeeled(PACKED, MASTER, id(1)));
  736. fail("expected IOException");
  737. } catch (IOException e) {
  738. assertEquals(JGitText.get().peeledRefIsRequired, e.getMessage());
  739. }
  740. }
  741. @Test
  742. public void nameTooLongDoesNotWrite() throws IOException {
  743. try {
  744. ReftableConfig cfg = new ReftableConfig();
  745. cfg.setRefBlockSize(64);
  746. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  747. ReftableWriter writer = new ReftableWriter(cfg, buffer).begin();
  748. writer.writeRef(ref("refs/heads/i-am-not-a-teapot", 1));
  749. writer.finish();
  750. fail("expected BlockSizeTooSmallException");
  751. } catch (BlockSizeTooSmallException e) {
  752. assertEquals(85, e.getMinimumBlockSize());
  753. }
  754. }
  755. @Test
  756. public void badCrc32() throws IOException {
  757. byte[] table = write();
  758. table[table.length - 1] = 0x42;
  759. try {
  760. read(table).seekRef(HEAD);
  761. fail("expected IOException");
  762. } catch (IOException e) {
  763. assertEquals(JGitText.get().invalidReftableCRC, e.getMessage());
  764. }
  765. }
  766. private static void assertScan(List<Ref> refs, Reftable t)
  767. throws IOException {
  768. try (RefCursor rc = t.allRefs()) {
  769. for (Ref exp : refs) {
  770. assertTrue("has " + exp.getName(), rc.next());
  771. Ref act = rc.getRef();
  772. assertEquals(exp.getName(), act.getName());
  773. assertEquals(exp.getObjectId(), act.getObjectId());
  774. assertEquals(0, rc.getRef().getUpdateIndex());
  775. }
  776. assertFalse(rc.next());
  777. }
  778. }
  779. private static void assertSeek(List<Ref> refs, Reftable t)
  780. throws IOException {
  781. for (Ref exp : refs) {
  782. try (RefCursor rc = t.seekRef(exp.getName())) {
  783. assertTrue("has " + exp.getName(), rc.next());
  784. Ref act = rc.getRef();
  785. assertEquals(exp.getName(), act.getName());
  786. assertEquals(exp.getObjectId(), act.getObjectId());
  787. assertEquals(0, rc.getRef().getUpdateIndex());
  788. assertFalse(rc.next());
  789. }
  790. }
  791. }
  792. private static Ref ref(String name, int id) {
  793. return new ObjectIdRef.PeeledNonTag(PACKED, name, id(id));
  794. }
  795. private static Ref tag(String name, int id1, int id2) {
  796. return new ObjectIdRef.PeeledTag(PACKED, name, id(id1), id(id2));
  797. }
  798. private static Ref sym(String name, String target) {
  799. return new SymbolicRef(name, newRef(target));
  800. }
  801. private static Ref newRef(String name) {
  802. return new ObjectIdRef.Unpeeled(NEW, name, null);
  803. }
  804. private static ObjectId id(int i) {
  805. byte[] buf = new byte[OBJECT_ID_LENGTH];
  806. buf[0] = (byte) (i & 0xff);
  807. buf[1] = (byte) ((i >>> 8) & 0xff);
  808. buf[2] = (byte) ((i >>> 16) & 0xff);
  809. buf[3] = (byte) (i >>> 24);
  810. return ObjectId.fromRaw(buf);
  811. }
  812. private static ReftableReader read(byte[] table) {
  813. return new ReftableReader(BlockSource.from(table));
  814. }
  815. private byte[] write(Ref... refs) throws IOException {
  816. return write(Arrays.asList(refs));
  817. }
  818. private byte[] write(Collection<Ref> refs) throws IOException {
  819. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  820. stats = new ReftableWriter(buffer)
  821. .begin()
  822. .sortAndWriteRefs(refs)
  823. .finish()
  824. .getStats();
  825. return buffer.toByteArray();
  826. }
  827. }