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.

BatchRefUpdateTest.java 45KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  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.file;
  44. import static java.nio.charset.StandardCharsets.UTF_8;
  45. import static java.util.concurrent.TimeUnit.NANOSECONDS;
  46. import static java.util.concurrent.TimeUnit.SECONDS;
  47. import static org.eclipse.jgit.internal.storage.file.BatchRefUpdateTest.Result.LOCK_FAILURE;
  48. import static org.eclipse.jgit.internal.storage.file.BatchRefUpdateTest.Result.OK;
  49. import static org.eclipse.jgit.internal.storage.file.BatchRefUpdateTest.Result.REJECTED_MISSING_OBJECT;
  50. import static org.eclipse.jgit.internal.storage.file.BatchRefUpdateTest.Result.REJECTED_NONFASTFORWARD;
  51. import static org.eclipse.jgit.internal.storage.file.BatchRefUpdateTest.Result.TRANSACTION_ABORTED;
  52. import static org.eclipse.jgit.lib.ObjectId.zeroId;
  53. import static org.eclipse.jgit.transport.ReceiveCommand.Type.CREATE;
  54. import static org.eclipse.jgit.transport.ReceiveCommand.Type.DELETE;
  55. import static org.eclipse.jgit.transport.ReceiveCommand.Type.UPDATE;
  56. import static org.eclipse.jgit.transport.ReceiveCommand.Type.UPDATE_NONFASTFORWARD;
  57. import static org.junit.Assert.assertEquals;
  58. import static org.junit.Assert.assertFalse;
  59. import static org.junit.Assert.assertNotNull;
  60. import static org.junit.Assert.assertNull;
  61. import static org.junit.Assert.assertTrue;
  62. import static org.junit.Assume.assumeFalse;
  63. import static org.junit.Assume.assumeTrue;
  64. import java.io.File;
  65. import java.io.IOException;
  66. import java.nio.file.Files;
  67. import java.util.Arrays;
  68. import java.util.Collection;
  69. import java.util.Collections;
  70. import java.util.LinkedHashMap;
  71. import java.util.List;
  72. import java.util.Map;
  73. import java.util.concurrent.locks.ReentrantLock;
  74. import java.util.function.Predicate;
  75. import org.eclipse.jgit.events.ListenerHandle;
  76. import org.eclipse.jgit.events.RefsChangedListener;
  77. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  78. import org.eclipse.jgit.junit.StrictWorkMonitor;
  79. import org.eclipse.jgit.junit.TestRepository;
  80. import org.eclipse.jgit.lib.AnyObjectId;
  81. import org.eclipse.jgit.lib.BatchRefUpdate;
  82. import org.eclipse.jgit.lib.CheckoutEntry;
  83. import org.eclipse.jgit.lib.ConfigConstants;
  84. import org.eclipse.jgit.lib.Constants;
  85. import org.eclipse.jgit.lib.NullProgressMonitor;
  86. import org.eclipse.jgit.lib.ObjectId;
  87. import org.eclipse.jgit.lib.PersonIdent;
  88. import org.eclipse.jgit.lib.Ref;
  89. import org.eclipse.jgit.lib.RefDatabase;
  90. import org.eclipse.jgit.lib.RefUpdate;
  91. import org.eclipse.jgit.lib.ReflogEntry;
  92. import org.eclipse.jgit.lib.ReflogReader;
  93. import org.eclipse.jgit.lib.Repository;
  94. import org.eclipse.jgit.lib.StoredConfig;
  95. import org.eclipse.jgit.revwalk.RevCommit;
  96. import org.eclipse.jgit.revwalk.RevWalk;
  97. import org.eclipse.jgit.transport.ReceiveCommand;
  98. import org.junit.After;
  99. import org.junit.Before;
  100. import org.junit.Test;
  101. import org.junit.runner.RunWith;
  102. import org.junit.runners.Parameterized;
  103. import org.junit.runners.Parameterized.Parameter;
  104. import org.junit.runners.Parameterized.Parameters;
  105. @SuppressWarnings("boxing")
  106. @RunWith(Parameterized.class)
  107. public class BatchRefUpdateTest extends LocalDiskRepositoryTestCase {
  108. @Parameter(0)
  109. public boolean atomic;
  110. @Parameter(1)
  111. public boolean useReftable;
  112. @Parameters(name = "atomic={0} reftable={1}")
  113. public static Collection<Object[]> data() {
  114. return Arrays.asList(new Object[][] { { Boolean.FALSE, Boolean.FALSE },
  115. { Boolean.TRUE, Boolean.FALSE },
  116. { Boolean.FALSE, Boolean.TRUE },
  117. { Boolean.TRUE, Boolean.TRUE }, });
  118. }
  119. private Repository diskRepo;
  120. private TestRepository<Repository> repo;
  121. private RefDirectory refdir;
  122. private RevCommit A;
  123. private RevCommit B; // B descends from A.
  124. /**
  125. * When asserting the number of RefsChangedEvents you must account for one
  126. * additional event due to the initial ref setup via a number of calls to
  127. * {@link #writeLooseRef(String, AnyObjectId)} (will be fired in execute()
  128. * when it is detected that the on-disk loose refs have changed), or for one
  129. * additional event per {@link #writeRef(String, AnyObjectId)}.
  130. */
  131. private int refsChangedEvents;
  132. private ListenerHandle handle;
  133. private RefsChangedListener refsChangedListener = event -> {
  134. refsChangedEvents++;
  135. };
  136. @Override
  137. @Before
  138. public void setUp() throws Exception {
  139. super.setUp();
  140. FileRepository fileRepo = createBareRepository();
  141. if (useReftable) {
  142. fileRepo.convertToReftable(false, false);
  143. }
  144. diskRepo = fileRepo;
  145. setLogAllRefUpdates(true);
  146. if (!useReftable) {
  147. refdir = (RefDirectory) diskRepo.getRefDatabase();
  148. refdir.setRetrySleepMs(Arrays.asList(0, 0));
  149. }
  150. repo = new TestRepository<>(diskRepo);
  151. A = repo.commit().create();
  152. B = repo.commit(repo.getRevWalk().parseCommit(A));
  153. refsChangedEvents = 0;
  154. handle = diskRepo.getListenerList()
  155. .addRefsChangedListener(refsChangedListener);
  156. }
  157. @After
  158. public void removeListener() {
  159. handle.remove();
  160. refsChangedEvents = 0;
  161. }
  162. @Test
  163. public void packedRefsFileIsSorted() throws IOException {
  164. assumeTrue(atomic);
  165. assumeFalse(useReftable);
  166. for (int i = 0; i < 2; i++) {
  167. BatchRefUpdate bu = diskRepo.getRefDatabase().newBatchUpdate();
  168. String b1 = String.format("refs/heads/a%d", i);
  169. String b2 = String.format("refs/heads/b%d", i);
  170. bu.setAtomic(atomic);
  171. ReceiveCommand c1 = new ReceiveCommand(ObjectId.zeroId(), A, b1);
  172. ReceiveCommand c2 = new ReceiveCommand(ObjectId.zeroId(), B, b2);
  173. bu.addCommand(c1, c2);
  174. try (RevWalk rw = new RevWalk(diskRepo)) {
  175. bu.execute(rw, NullProgressMonitor.INSTANCE);
  176. }
  177. assertEquals(c1.getResult(), ReceiveCommand.Result.OK);
  178. assertEquals(c2.getResult(), ReceiveCommand.Result.OK);
  179. }
  180. File packed = new File(diskRepo.getDirectory(), "packed-refs");
  181. String packedStr = new String(Files.readAllBytes(packed.toPath()),
  182. UTF_8);
  183. int a2 = packedStr.indexOf("refs/heads/a1");
  184. int b1 = packedStr.indexOf("refs/heads/b0");
  185. assertTrue(a2 < b1);
  186. }
  187. @Test
  188. public void simpleNoForce() throws IOException {
  189. writeLooseRefs("refs/heads/master", A, "refs/heads/masters", B);
  190. List<ReceiveCommand> cmds = Arrays.asList(
  191. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  192. new ReceiveCommand(B, A, "refs/heads/masters",
  193. UPDATE_NONFASTFORWARD));
  194. execute(newBatchUpdate(cmds));
  195. if (atomic) {
  196. assertResults(cmds, TRANSACTION_ABORTED, REJECTED_NONFASTFORWARD);
  197. assertRefs("refs/heads/master", A, "refs/heads/masters", B);
  198. } else {
  199. assertResults(cmds, OK, REJECTED_NONFASTFORWARD);
  200. assertRefs("refs/heads/master", B, "refs/heads/masters", B);
  201. }
  202. }
  203. @Test
  204. public void simpleNoForceRefsChangedEvents() throws IOException {
  205. writeLooseRefs("refs/heads/master", A, "refs/heads/masters", B);
  206. int initialRefsChangedEvents = refsChangedEvents;
  207. List<ReceiveCommand> cmds = Arrays.asList(
  208. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  209. new ReceiveCommand(B, A, "refs/heads/masters",
  210. UPDATE_NONFASTFORWARD));
  211. execute(newBatchUpdate(cmds));
  212. assertEquals(atomic ? initialRefsChangedEvents
  213. : initialRefsChangedEvents + 1, refsChangedEvents);
  214. }
  215. @Test
  216. public void simpleForce() throws IOException {
  217. writeLooseRefs("refs/heads/master", A, "refs/heads/masters", B);
  218. List<ReceiveCommand> cmds = Arrays.asList(
  219. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  220. new ReceiveCommand(B, A, "refs/heads/masters",
  221. UPDATE_NONFASTFORWARD));
  222. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
  223. assertResults(cmds, OK, OK);
  224. assertRefs("refs/heads/master", B, "refs/heads/masters", A);
  225. }
  226. @Test
  227. public void simpleForceRefsChangedEvents() throws IOException {
  228. writeLooseRefs("refs/heads/master", A, "refs/heads/masters", B);
  229. int initialRefsChangedEvents = refsChangedEvents;
  230. List<ReceiveCommand> cmds = Arrays.asList(
  231. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  232. new ReceiveCommand(B, A, "refs/heads/masters",
  233. UPDATE_NONFASTFORWARD));
  234. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
  235. assertEquals(batchesRefUpdates() ? initialRefsChangedEvents + 1
  236. : initialRefsChangedEvents + 2, refsChangedEvents);
  237. }
  238. @Test
  239. public void nonFastForwardDoesNotDoExpensiveMergeCheck()
  240. throws IOException {
  241. writeLooseRef("refs/heads/master", B);
  242. List<ReceiveCommand> cmds = Arrays.asList(new ReceiveCommand(B, A,
  243. "refs/heads/master", UPDATE_NONFASTFORWARD));
  244. try (RevWalk rw = new RevWalk(diskRepo) {
  245. @Override
  246. public boolean isMergedInto(RevCommit base, RevCommit tip) {
  247. throw new AssertionError("isMergedInto() should not be called");
  248. }
  249. }) {
  250. newBatchUpdate(cmds).setAllowNonFastForwards(true).execute(rw,
  251. new StrictWorkMonitor());
  252. }
  253. assertResults(cmds, OK);
  254. assertRefs("refs/heads/master", A);
  255. }
  256. @Test
  257. public void nonFastForwardDoesNotDoExpensiveMergeCheckRefsChangedEvents()
  258. throws IOException {
  259. writeLooseRef("refs/heads/master", B);
  260. int initialRefsChangedEvents = refsChangedEvents;
  261. List<ReceiveCommand> cmds = Arrays.asList(new ReceiveCommand(B, A,
  262. "refs/heads/master", UPDATE_NONFASTFORWARD));
  263. try (RevWalk rw = new RevWalk(diskRepo) {
  264. @Override
  265. public boolean isMergedInto(RevCommit base, RevCommit tip) {
  266. throw new AssertionError("isMergedInto() should not be called");
  267. }
  268. }) {
  269. newBatchUpdate(cmds).setAllowNonFastForwards(true).execute(rw,
  270. new StrictWorkMonitor());
  271. }
  272. assertEquals(initialRefsChangedEvents + 1, refsChangedEvents);
  273. }
  274. @Test
  275. public void fileDirectoryConflict() throws IOException {
  276. writeLooseRefs("refs/heads/master", A, "refs/heads/masters", B);
  277. List<ReceiveCommand> cmds = Arrays.asList(
  278. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  279. new ReceiveCommand(zeroId(), A, "refs/heads/master/x", CREATE),
  280. new ReceiveCommand(zeroId(), A, "refs/heads", CREATE));
  281. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true), false);
  282. if (atomic) {
  283. // Atomic update sees that master and master/x are conflicting, then
  284. // marks the first one in the list as LOCK_FAILURE and aborts the rest.
  285. assertResults(cmds, LOCK_FAILURE, TRANSACTION_ABORTED,
  286. TRANSACTION_ABORTED);
  287. assertRefs("refs/heads/master", A, "refs/heads/masters", B);
  288. } else {
  289. // Non-atomic updates are applied in order: master succeeds, then
  290. // master/x fails due to conflict.
  291. assertResults(cmds, OK, LOCK_FAILURE, LOCK_FAILURE);
  292. assertRefs("refs/heads/master", B, "refs/heads/masters", B);
  293. }
  294. }
  295. @Test
  296. public void fileDirectoryConflictRefsChangedEvents() throws IOException {
  297. writeLooseRefs("refs/heads/master", A, "refs/heads/masters", B);
  298. int initialRefsChangedEvents = refsChangedEvents;
  299. List<ReceiveCommand> cmds = Arrays.asList(
  300. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  301. new ReceiveCommand(zeroId(), A, "refs/heads/master/x", CREATE),
  302. new ReceiveCommand(zeroId(), A, "refs/heads", CREATE));
  303. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true), false);
  304. assertEquals(atomic ? initialRefsChangedEvents
  305. : initialRefsChangedEvents + 1, refsChangedEvents);
  306. }
  307. @Test
  308. public void conflictThanksToDelete() throws IOException {
  309. writeLooseRefs("refs/heads/master", A, "refs/heads/masters", B);
  310. List<ReceiveCommand> cmds = Arrays.asList(
  311. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  312. new ReceiveCommand(zeroId(), A, "refs/heads/masters/x", CREATE),
  313. new ReceiveCommand(B, zeroId(), "refs/heads/masters", DELETE));
  314. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
  315. assertResults(cmds, OK, OK, OK);
  316. assertRefs("refs/heads/master", B, "refs/heads/masters/x", A);
  317. }
  318. @Test
  319. public void conflictThanksToDeleteRefsChangedEvents() throws IOException {
  320. writeLooseRefs("refs/heads/master", A, "refs/heads/masters", B);
  321. int initialRefsChangedEvents = refsChangedEvents;
  322. List<ReceiveCommand> cmds = Arrays.asList(
  323. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  324. new ReceiveCommand(zeroId(), A, "refs/heads/masters/x", CREATE),
  325. new ReceiveCommand(B, zeroId(), "refs/heads/masters", DELETE));
  326. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
  327. assertEquals(batchesRefUpdates() ? initialRefsChangedEvents + 1
  328. : initialRefsChangedEvents + 3, refsChangedEvents);
  329. }
  330. @Test
  331. public void updateToMissingObject() throws IOException {
  332. writeLooseRef("refs/heads/master", A);
  333. ObjectId bad = ObjectId
  334. .fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
  335. List<ReceiveCommand> cmds = Arrays.asList(
  336. new ReceiveCommand(A, bad, "refs/heads/master", UPDATE),
  337. new ReceiveCommand(zeroId(), B, "refs/heads/foo2", CREATE));
  338. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true), false);
  339. if (atomic) {
  340. assertResults(cmds, REJECTED_MISSING_OBJECT, TRANSACTION_ABORTED);
  341. assertRefs("refs/heads/master", A);
  342. } else {
  343. assertResults(cmds, REJECTED_MISSING_OBJECT, OK);
  344. assertRefs("refs/heads/master", A, "refs/heads/foo2", B);
  345. }
  346. }
  347. @Test
  348. public void updateToMissingObjectRefsChangedEvents() throws IOException {
  349. writeLooseRef("refs/heads/master", A);
  350. int initialRefsChangedEvents = refsChangedEvents;
  351. ObjectId bad = ObjectId
  352. .fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
  353. List<ReceiveCommand> cmds = Arrays.asList(
  354. new ReceiveCommand(A, bad, "refs/heads/master", UPDATE),
  355. new ReceiveCommand(zeroId(), B, "refs/heads/foo2", CREATE));
  356. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true), false);
  357. assertEquals(atomic ? initialRefsChangedEvents
  358. : initialRefsChangedEvents + 1, refsChangedEvents);
  359. }
  360. @Test
  361. public void addMissingObject() throws IOException {
  362. writeLooseRef("refs/heads/master", A);
  363. ObjectId bad = ObjectId
  364. .fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
  365. List<ReceiveCommand> cmds = Arrays.asList(
  366. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  367. new ReceiveCommand(zeroId(), bad, "refs/heads/foo2", CREATE));
  368. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true), false);
  369. if (atomic) {
  370. assertResults(cmds, TRANSACTION_ABORTED, REJECTED_MISSING_OBJECT);
  371. assertRefs("refs/heads/master", A);
  372. } else {
  373. assertResults(cmds, OK, REJECTED_MISSING_OBJECT);
  374. assertRefs("refs/heads/master", B);
  375. }
  376. }
  377. @Test
  378. public void addMissingObjectRefsChangedEvents() throws IOException {
  379. writeLooseRef("refs/heads/master", A);
  380. int initialRefsChangedEvents = refsChangedEvents;
  381. ObjectId bad = ObjectId
  382. .fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
  383. List<ReceiveCommand> cmds = Arrays.asList(
  384. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  385. new ReceiveCommand(zeroId(), bad, "refs/heads/foo2", CREATE));
  386. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true), false);
  387. assertEquals(atomic ? initialRefsChangedEvents
  388. : initialRefsChangedEvents + 1, refsChangedEvents);
  389. }
  390. @Test
  391. public void oneNonExistentRef() throws IOException {
  392. List<ReceiveCommand> cmds = Arrays.asList(
  393. new ReceiveCommand(A, B, "refs/heads/foo1", UPDATE),
  394. new ReceiveCommand(zeroId(), B, "refs/heads/foo2", CREATE));
  395. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
  396. if (atomic) {
  397. assertResults(cmds, LOCK_FAILURE, TRANSACTION_ABORTED);
  398. assertRefs();
  399. assertEquals(0, refsChangedEvents);
  400. } else {
  401. assertResults(cmds, LOCK_FAILURE, OK);
  402. assertRefs("refs/heads/foo2", B);
  403. assertEquals(1, refsChangedEvents);
  404. }
  405. }
  406. @Test
  407. public void oneRefWrongOldValue() throws IOException {
  408. writeLooseRef("refs/heads/master", A);
  409. List<ReceiveCommand> cmds = Arrays.asList(
  410. new ReceiveCommand(B, B, "refs/heads/master", UPDATE),
  411. new ReceiveCommand(zeroId(), B, "refs/heads/foo2", CREATE));
  412. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
  413. if (atomic) {
  414. assertResults(cmds, LOCK_FAILURE, TRANSACTION_ABORTED);
  415. assertRefs("refs/heads/master", A);
  416. } else {
  417. assertResults(cmds, LOCK_FAILURE, OK);
  418. assertRefs("refs/heads/master", A, "refs/heads/foo2", B);
  419. }
  420. }
  421. @Test
  422. public void oneRefWrongOldValueRefsChangedEvents() throws IOException {
  423. writeLooseRef("refs/heads/master", A);
  424. int initialRefsChangedEvents = refsChangedEvents;
  425. List<ReceiveCommand> cmds = Arrays.asList(
  426. new ReceiveCommand(B, B, "refs/heads/master", UPDATE),
  427. new ReceiveCommand(zeroId(), B, "refs/heads/foo2", CREATE));
  428. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
  429. assertEquals(atomic ? initialRefsChangedEvents
  430. : initialRefsChangedEvents + 1, refsChangedEvents);
  431. }
  432. @Test
  433. public void nonExistentRef() throws IOException {
  434. writeLooseRef("refs/heads/master", A);
  435. List<ReceiveCommand> cmds = Arrays.asList(
  436. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  437. new ReceiveCommand(A, zeroId(), "refs/heads/foo2", DELETE));
  438. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
  439. if (atomic) {
  440. assertResults(cmds, TRANSACTION_ABORTED, LOCK_FAILURE);
  441. assertRefs("refs/heads/master", A);
  442. } else {
  443. assertResults(cmds, OK, LOCK_FAILURE);
  444. assertRefs("refs/heads/master", B);
  445. }
  446. }
  447. @Test
  448. public void nonExistentRefRefsChangedEvents() throws IOException {
  449. writeLooseRef("refs/heads/master", A);
  450. int initialRefsChangedEvents = refsChangedEvents;
  451. List<ReceiveCommand> cmds = Arrays.asList(
  452. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  453. new ReceiveCommand(A, zeroId(), "refs/heads/foo2", DELETE));
  454. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
  455. assertEquals(atomic ? initialRefsChangedEvents
  456. : initialRefsChangedEvents + 1, refsChangedEvents);
  457. }
  458. @Test
  459. public void noRefLog() throws IOException {
  460. writeRef("refs/heads/master", A);
  461. int initialRefsChangedEvents = refsChangedEvents;
  462. Map<String, ReflogEntry> oldLogs = getLastReflogs("refs/heads/master",
  463. "refs/heads/branch");
  464. assertEquals(Collections.singleton("refs/heads/master"),
  465. oldLogs.keySet());
  466. List<ReceiveCommand> cmds = Arrays.asList(
  467. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  468. new ReceiveCommand(zeroId(), B, "refs/heads/branch", CREATE));
  469. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
  470. assertResults(cmds, OK, OK);
  471. assertRefs("refs/heads/master", B, "refs/heads/branch", B);
  472. assertEquals(batchesRefUpdates() ? initialRefsChangedEvents + 1
  473. : initialRefsChangedEvents + 2, refsChangedEvents);
  474. assertReflogUnchanged(oldLogs, "refs/heads/master");
  475. assertReflogUnchanged(oldLogs, "refs/heads/branch");
  476. }
  477. @Test
  478. public void reflogDefaultIdent() throws IOException {
  479. writeRef("refs/heads/master", A);
  480. writeRef("refs/heads/branch2", A);
  481. int initialRefsChangedEvents = refsChangedEvents;
  482. Map<String, ReflogEntry> oldLogs = getLastReflogs("refs/heads/master",
  483. "refs/heads/branch1", "refs/heads/branch2");
  484. List<ReceiveCommand> cmds = Arrays.asList(
  485. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  486. new ReceiveCommand(zeroId(), B, "refs/heads/branch1", CREATE));
  487. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true)
  488. .setRefLogMessage("a reflog", false));
  489. assertResults(cmds, OK, OK);
  490. assertRefs("refs/heads/master", B, "refs/heads/branch1", B,
  491. "refs/heads/branch2", A);
  492. assertEquals(batchesRefUpdates() ? initialRefsChangedEvents + 1
  493. : initialRefsChangedEvents + 2, refsChangedEvents);
  494. assertReflogEquals(reflog(A, B, new PersonIdent(diskRepo), "a reflog"),
  495. getLastReflog("refs/heads/master"));
  496. assertReflogEquals(
  497. reflog(zeroId(), B, new PersonIdent(diskRepo), "a reflog"),
  498. getLastReflog("refs/heads/branch1"));
  499. assertReflogUnchanged(oldLogs, "refs/heads/branch2");
  500. }
  501. @Test
  502. public void reflogAppendStatusNoMessage() throws IOException {
  503. writeRef("refs/heads/master", A);
  504. writeRef("refs/heads/branch1", B);
  505. int initialRefsChangedEvents = refsChangedEvents;
  506. List<ReceiveCommand> cmds = Arrays.asList(
  507. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  508. new ReceiveCommand(B, A, "refs/heads/branch1",
  509. UPDATE_NONFASTFORWARD),
  510. new ReceiveCommand(zeroId(), A, "refs/heads/branch2", CREATE));
  511. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true)
  512. .setRefLogMessage(null, true));
  513. assertResults(cmds, OK, OK, OK);
  514. assertRefs("refs/heads/master", B, "refs/heads/branch1", A,
  515. "refs/heads/branch2", A);
  516. assertEquals(batchesRefUpdates() ? initialRefsChangedEvents + 1
  517. : initialRefsChangedEvents + 3, refsChangedEvents);
  518. assertReflogEquals(
  519. // Always forced; setAllowNonFastForwards(true) bypasses the
  520. // check.
  521. reflog(A, B, new PersonIdent(diskRepo), "forced-update"),
  522. getLastReflog("refs/heads/master"));
  523. assertReflogEquals(
  524. reflog(B, A, new PersonIdent(diskRepo), "forced-update"),
  525. getLastReflog("refs/heads/branch1"));
  526. assertReflogEquals(
  527. reflog(zeroId(), A, new PersonIdent(diskRepo), "created"),
  528. getLastReflog("refs/heads/branch2"));
  529. }
  530. @Test
  531. public void reflogAppendStatusFastForward() throws IOException {
  532. writeRef("refs/heads/master", A);
  533. int initialRefsChangedEvents = refsChangedEvents;
  534. List<ReceiveCommand> cmds = Arrays
  535. .asList(new ReceiveCommand(A, B, "refs/heads/master", UPDATE));
  536. execute(newBatchUpdate(cmds).setRefLogMessage(null, true));
  537. assertResults(cmds, OK);
  538. assertRefs("refs/heads/master", B);
  539. assertEquals(initialRefsChangedEvents + 1, refsChangedEvents);
  540. assertReflogEquals(
  541. reflog(A, B, new PersonIdent(diskRepo), "fast-forward"),
  542. getLastReflog("refs/heads/master"));
  543. }
  544. @Test
  545. public void reflogAppendStatusWithMessage() throws IOException {
  546. writeRef("refs/heads/master", A);
  547. int initialRefsChangedEvents = refsChangedEvents;
  548. List<ReceiveCommand> cmds = Arrays.asList(
  549. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  550. new ReceiveCommand(zeroId(), A, "refs/heads/branch", CREATE));
  551. execute(newBatchUpdate(cmds).setRefLogMessage("a reflog", true));
  552. assertResults(cmds, OK, OK);
  553. assertRefs("refs/heads/master", B, "refs/heads/branch", A);
  554. assertEquals(batchesRefUpdates() ? initialRefsChangedEvents + 1
  555. : initialRefsChangedEvents + 2, refsChangedEvents);
  556. assertReflogEquals(
  557. reflog(A, B, new PersonIdent(diskRepo),
  558. "a reflog: fast-forward"),
  559. getLastReflog("refs/heads/master"));
  560. assertReflogEquals(
  561. reflog(zeroId(), A, new PersonIdent(diskRepo),
  562. "a reflog: created"),
  563. getLastReflog("refs/heads/branch"));
  564. }
  565. @Test
  566. public void reflogCustomIdent() throws IOException {
  567. writeRef("refs/heads/master", A);
  568. int initialRefsChangedEvents = refsChangedEvents;
  569. List<ReceiveCommand> cmds = Arrays.asList(
  570. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  571. new ReceiveCommand(zeroId(), B, "refs/heads/branch", CREATE));
  572. PersonIdent ident = new PersonIdent("A Reflog User",
  573. "reflog@example.com");
  574. execute(newBatchUpdate(cmds).setRefLogMessage("a reflog", false)
  575. .setRefLogIdent(ident));
  576. assertResults(cmds, OK, OK);
  577. assertEquals(batchesRefUpdates() ? initialRefsChangedEvents + 1
  578. : initialRefsChangedEvents + 2, refsChangedEvents);
  579. assertRefs("refs/heads/master", B, "refs/heads/branch", B);
  580. assertReflogEquals(reflog(A, B, ident, "a reflog"),
  581. getLastReflog("refs/heads/master"), true);
  582. assertReflogEquals(reflog(zeroId(), B, ident, "a reflog"),
  583. getLastReflog("refs/heads/branch"), true);
  584. }
  585. @Test
  586. public void reflogDelete() throws IOException {
  587. writeRef("refs/heads/master", A);
  588. writeRef("refs/heads/branch", A);
  589. int initialRefsChangedEvents = refsChangedEvents;
  590. assertEquals(2, getLastReflogs("refs/heads/master", "refs/heads/branch")
  591. .size());
  592. List<ReceiveCommand> cmds = Arrays.asList(
  593. new ReceiveCommand(A, zeroId(), "refs/heads/master", DELETE),
  594. new ReceiveCommand(A, B, "refs/heads/branch", UPDATE));
  595. execute(newBatchUpdate(cmds).setRefLogMessage("a reflog", false));
  596. assertResults(cmds, OK, OK);
  597. assertRefs("refs/heads/branch", B);
  598. assertEquals(batchesRefUpdates() ? initialRefsChangedEvents + 1
  599. : initialRefsChangedEvents + 2, refsChangedEvents);
  600. if (useReftable) {
  601. // reftable retains reflog entries for deleted branches.
  602. assertReflogEquals(
  603. reflog(A, zeroId(), new PersonIdent(diskRepo), "a reflog"),
  604. getLastReflog("refs/heads/master"));
  605. } else {
  606. assertNull(getLastReflog("refs/heads/master"));
  607. }
  608. assertReflogEquals(reflog(A, B, new PersonIdent(diskRepo), "a reflog"),
  609. getLastReflog("refs/heads/branch"));
  610. }
  611. @Test
  612. public void reflogFileDirectoryConflict() throws IOException {
  613. writeRef("refs/heads/master", A);
  614. int initialRefsChangedEvents = refsChangedEvents;
  615. List<ReceiveCommand> cmds = Arrays.asList(
  616. new ReceiveCommand(A, zeroId(), "refs/heads/master", DELETE),
  617. new ReceiveCommand(zeroId(), A, "refs/heads/master/x", CREATE));
  618. execute(newBatchUpdate(cmds).setRefLogMessage("a reflog", false));
  619. assertResults(cmds, OK, OK);
  620. assertRefs("refs/heads/master/x", A);
  621. assertEquals(batchesRefUpdates() ? initialRefsChangedEvents + 1
  622. : initialRefsChangedEvents + 2, refsChangedEvents);
  623. if (!useReftable) {
  624. // reftable retains reflog entries for deleted branches.
  625. assertNull(getLastReflog("refs/heads/master"));
  626. }
  627. assertReflogEquals(
  628. reflog(zeroId(), A, new PersonIdent(diskRepo), "a reflog"),
  629. getLastReflog("refs/heads/master/x"));
  630. }
  631. @Test
  632. public void reflogOnLockFailure() throws IOException {
  633. writeRef("refs/heads/master", A);
  634. int initialRefsChangedEvents = refsChangedEvents;
  635. Map<String, ReflogEntry> oldLogs = getLastReflogs("refs/heads/master",
  636. "refs/heads/branch");
  637. List<ReceiveCommand> cmds = Arrays.asList(
  638. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  639. new ReceiveCommand(A, B, "refs/heads/branch", UPDATE));
  640. execute(newBatchUpdate(cmds).setRefLogMessage("a reflog", false));
  641. if (atomic) {
  642. assertResults(cmds, TRANSACTION_ABORTED, LOCK_FAILURE);
  643. assertEquals(initialRefsChangedEvents, refsChangedEvents);
  644. assertReflogUnchanged(oldLogs, "refs/heads/master");
  645. assertReflogUnchanged(oldLogs, "refs/heads/branch");
  646. } else {
  647. assertResults(cmds, OK, LOCK_FAILURE);
  648. assertEquals(initialRefsChangedEvents + 1, refsChangedEvents);
  649. assertReflogEquals(
  650. reflog(A, B, new PersonIdent(diskRepo), "a reflog"),
  651. getLastReflog("refs/heads/master"));
  652. assertReflogUnchanged(oldLogs, "refs/heads/branch");
  653. }
  654. }
  655. @Test
  656. public void overrideRefLogMessage() throws Exception {
  657. writeRef("refs/heads/master", A);
  658. int initialRefsChangedEvents = refsChangedEvents;
  659. List<ReceiveCommand> cmds = Arrays.asList(
  660. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  661. new ReceiveCommand(zeroId(), B, "refs/heads/branch", CREATE));
  662. cmds.get(0).setRefLogMessage("custom log", false);
  663. PersonIdent ident = new PersonIdent(diskRepo);
  664. execute(newBatchUpdate(cmds).setRefLogIdent(ident)
  665. .setRefLogMessage("a reflog", true));
  666. assertResults(cmds, OK, OK);
  667. assertEquals(batchesRefUpdates() ? initialRefsChangedEvents + 1
  668. : initialRefsChangedEvents + 2, refsChangedEvents);
  669. assertReflogEquals(reflog(A, B, ident, "custom log"),
  670. getLastReflog("refs/heads/master"), true);
  671. assertReflogEquals(reflog(zeroId(), B, ident, "a reflog: created"),
  672. getLastReflog("refs/heads/branch"), true);
  673. }
  674. @Test
  675. public void overrideDisableRefLog() throws Exception {
  676. writeRef("refs/heads/master", A);
  677. int initialRefsChangedEvents = refsChangedEvents;
  678. Map<String, ReflogEntry> oldLogs = getLastReflogs("refs/heads/master",
  679. "refs/heads/branch");
  680. List<ReceiveCommand> cmds = Arrays.asList(
  681. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  682. new ReceiveCommand(zeroId(), B, "refs/heads/branch", CREATE));
  683. cmds.get(0).disableRefLog();
  684. execute(newBatchUpdate(cmds).setRefLogMessage("a reflog", true));
  685. assertResults(cmds, OK, OK);
  686. assertEquals(batchesRefUpdates() ? initialRefsChangedEvents + 1
  687. : initialRefsChangedEvents + 2, refsChangedEvents);
  688. assertReflogUnchanged(oldLogs, "refs/heads/master");
  689. assertReflogEquals(
  690. reflog(zeroId(), B, new PersonIdent(diskRepo),
  691. "a reflog: created"),
  692. getLastReflog("refs/heads/branch"));
  693. }
  694. @Test
  695. public void refLogNotWrittenWithoutConfigOption() throws Exception {
  696. assumeFalse(useReftable);
  697. setLogAllRefUpdates(false);
  698. writeRef("refs/heads/master", A);
  699. Map<String, ReflogEntry> oldLogs = getLastReflogs("refs/heads/master",
  700. "refs/heads/branch");
  701. assertTrue(oldLogs.isEmpty());
  702. List<ReceiveCommand> cmds = Arrays.asList(
  703. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  704. new ReceiveCommand(zeroId(), B, "refs/heads/branch", CREATE));
  705. execute(newBatchUpdate(cmds).setRefLogMessage("a reflog", false));
  706. assertResults(cmds, OK, OK);
  707. assertReflogUnchanged(oldLogs, "refs/heads/master");
  708. assertReflogUnchanged(oldLogs, "refs/heads/branch");
  709. }
  710. @Test
  711. public void forceRefLogInUpdate() throws Exception {
  712. assumeFalse(useReftable);
  713. setLogAllRefUpdates(false);
  714. writeRef("refs/heads/master", A);
  715. assertTrue(getLastReflogs("refs/heads/master", "refs/heads/branch")
  716. .isEmpty());
  717. List<ReceiveCommand> cmds = Arrays.asList(
  718. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  719. new ReceiveCommand(zeroId(), B, "refs/heads/branch", CREATE));
  720. execute(newBatchUpdate(cmds).setRefLogMessage("a reflog", false)
  721. .setForceRefLog(true));
  722. assertResults(cmds, OK, OK);
  723. assertReflogEquals(reflog(A, B, new PersonIdent(diskRepo), "a reflog"),
  724. getLastReflog("refs/heads/master"));
  725. assertReflogEquals(
  726. reflog(zeroId(), B, new PersonIdent(diskRepo), "a reflog"),
  727. getLastReflog("refs/heads/branch"));
  728. }
  729. @Test
  730. public void forceRefLogInCommand() throws Exception {
  731. assumeFalse(useReftable);
  732. setLogAllRefUpdates(false);
  733. writeRef("refs/heads/master", A);
  734. Map<String, ReflogEntry> oldLogs = getLastReflogs("refs/heads/master",
  735. "refs/heads/branch");
  736. assertTrue(oldLogs.isEmpty());
  737. List<ReceiveCommand> cmds = Arrays.asList(
  738. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  739. new ReceiveCommand(zeroId(), B, "refs/heads/branch", CREATE));
  740. cmds.get(1).setForceRefLog(true);
  741. execute(newBatchUpdate(cmds).setRefLogMessage("a reflog", false));
  742. assertResults(cmds, OK, OK);
  743. assertReflogUnchanged(oldLogs, "refs/heads/master");
  744. assertReflogEquals(
  745. reflog(zeroId(), B, new PersonIdent(diskRepo), "a reflog"),
  746. getLastReflog("refs/heads/branch"));
  747. }
  748. @Test
  749. public void packedRefsLockFailure() throws Exception {
  750. assumeFalse(useReftable);
  751. writeLooseRef("refs/heads/master", A);
  752. List<ReceiveCommand> cmds = Arrays.asList(
  753. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  754. new ReceiveCommand(zeroId(), B, "refs/heads/branch", CREATE));
  755. LockFile myLock = refdir.lockPackedRefs();
  756. try {
  757. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
  758. assertFalse(getLockFile("refs/heads/master").exists());
  759. assertFalse(getLockFile("refs/heads/branch").exists());
  760. if (atomic) {
  761. assertResults(cmds, LOCK_FAILURE, TRANSACTION_ABORTED);
  762. assertRefs("refs/heads/master", A);
  763. } else {
  764. // Only operates on loose refs, doesn't care that packed-refs is
  765. // locked.
  766. assertResults(cmds, OK, OK);
  767. assertRefs("refs/heads/master", B, "refs/heads/branch", B);
  768. }
  769. } finally {
  770. myLock.unlock();
  771. }
  772. }
  773. @Test
  774. public void packedRefsLockFailureRefsChangedEvents() throws Exception {
  775. assumeFalse(useReftable);
  776. writeLooseRef("refs/heads/master", A);
  777. int initialRefsChangedEvents = refsChangedEvents;
  778. List<ReceiveCommand> cmds = Arrays.asList(
  779. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  780. new ReceiveCommand(zeroId(), B, "refs/heads/branch", CREATE));
  781. LockFile myLock = refdir.lockPackedRefs();
  782. try {
  783. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
  784. assertEquals(atomic ? initialRefsChangedEvents
  785. : initialRefsChangedEvents + 2, refsChangedEvents);
  786. } finally {
  787. myLock.unlock();
  788. }
  789. }
  790. @Test
  791. public void oneRefLockFailure() throws Exception {
  792. assumeFalse(useReftable);
  793. writeLooseRef("refs/heads/master", A);
  794. List<ReceiveCommand> cmds = Arrays.asList(
  795. new ReceiveCommand(zeroId(), B, "refs/heads/branch", CREATE),
  796. new ReceiveCommand(A, B, "refs/heads/master", UPDATE));
  797. LockFile myLock = new LockFile(refdir.fileFor("refs/heads/master"));
  798. assertTrue(myLock.lock());
  799. try {
  800. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
  801. assertFalse(LockFile.getLockFile(refdir.packedRefsFile).exists());
  802. assertFalse(getLockFile("refs/heads/branch").exists());
  803. if (atomic) {
  804. assertResults(cmds, TRANSACTION_ABORTED, LOCK_FAILURE);
  805. assertRefs("refs/heads/master", A);
  806. } else {
  807. assertResults(cmds, OK, LOCK_FAILURE);
  808. assertRefs("refs/heads/branch", B, "refs/heads/master", A);
  809. }
  810. } finally {
  811. myLock.unlock();
  812. }
  813. }
  814. @Test
  815. public void oneRefLockFailureRefsChangedEvents() throws Exception {
  816. assumeFalse(useReftable);
  817. writeLooseRef("refs/heads/master", A);
  818. int initialRefsChangedEvents = refsChangedEvents;
  819. List<ReceiveCommand> cmds = Arrays.asList(
  820. new ReceiveCommand(zeroId(), B, "refs/heads/branch", CREATE),
  821. new ReceiveCommand(A, B, "refs/heads/master", UPDATE));
  822. LockFile myLock = new LockFile(refdir.fileFor("refs/heads/master"));
  823. assertTrue(myLock.lock());
  824. try {
  825. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
  826. assertEquals(atomic ? initialRefsChangedEvents
  827. : initialRefsChangedEvents + 1, refsChangedEvents);
  828. } finally {
  829. myLock.unlock();
  830. }
  831. }
  832. @Test
  833. public void singleRefUpdateDoesNotRequirePackedRefsLock() throws Exception {
  834. assumeFalse(useReftable);
  835. writeLooseRef("refs/heads/master", A);
  836. List<ReceiveCommand> cmds = Arrays
  837. .asList(new ReceiveCommand(A, B, "refs/heads/master", UPDATE));
  838. LockFile myLock = refdir.lockPackedRefs();
  839. try {
  840. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
  841. assertFalse(getLockFile("refs/heads/master").exists());
  842. assertResults(cmds, OK);
  843. assertRefs("refs/heads/master", B);
  844. } finally {
  845. myLock.unlock();
  846. }
  847. }
  848. @Test
  849. public void singleRefUpdateDoesNotRequirePackedRefsLockRefsChangedEvents()
  850. throws Exception {
  851. assumeFalse(useReftable);
  852. writeLooseRef("refs/heads/master", A);
  853. int initialRefsChangedEvents = refsChangedEvents;
  854. List<ReceiveCommand> cmds = Arrays
  855. .asList(new ReceiveCommand(A, B, "refs/heads/master", UPDATE));
  856. LockFile myLock = refdir.lockPackedRefs();
  857. try {
  858. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
  859. assertEquals(initialRefsChangedEvents + 1, refsChangedEvents);
  860. } finally {
  861. myLock.unlock();
  862. }
  863. }
  864. @Test
  865. public void atomicUpdateRespectsInProcessLock() throws Exception {
  866. assumeTrue(atomic);
  867. assumeFalse(useReftable);
  868. writeLooseRef("refs/heads/master", A);
  869. List<ReceiveCommand> cmds = Arrays.asList(
  870. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  871. new ReceiveCommand(zeroId(), B, "refs/heads/branch", CREATE));
  872. Thread t = new Thread(() -> {
  873. try {
  874. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
  875. } catch (Exception e) {
  876. throw new RuntimeException(e);
  877. }
  878. });
  879. ReentrantLock l = refdir.inProcessPackedRefsLock;
  880. l.lock();
  881. try {
  882. t.start();
  883. long timeoutSecs = 10;
  884. long startNanos = System.nanoTime();
  885. // Hold onto the lock until we observe the worker thread has
  886. // attempted to
  887. // acquire it.
  888. while (l.getQueueLength() == 0) {
  889. long elapsedNanos = System.nanoTime() - startNanos;
  890. assertTrue(
  891. "timed out waiting for work thread to attempt to acquire lock",
  892. NANOSECONDS.toSeconds(elapsedNanos) < timeoutSecs);
  893. Thread.sleep(3);
  894. }
  895. // Once we unlock, the worker thread should finish the update
  896. // promptly.
  897. l.unlock();
  898. t.join(SECONDS.toMillis(timeoutSecs));
  899. assertFalse(t.isAlive());
  900. } finally {
  901. if (l.isHeldByCurrentThread()) {
  902. l.unlock();
  903. }
  904. }
  905. assertResults(cmds, OK, OK);
  906. assertRefs("refs/heads/master", B, "refs/heads/branch", B);
  907. }
  908. @Test
  909. public void atomicUpdateRespectsInProcessLockRefsChangedEvents()
  910. throws Exception {
  911. assumeTrue(atomic);
  912. assumeFalse(useReftable);
  913. writeLooseRef("refs/heads/master", A);
  914. int initialRefsChangedEvents = refsChangedEvents;
  915. List<ReceiveCommand> cmds = Arrays.asList(
  916. new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
  917. new ReceiveCommand(zeroId(), B, "refs/heads/branch", CREATE));
  918. Thread t = new Thread(() -> {
  919. try {
  920. execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
  921. } catch (Exception e) {
  922. throw new RuntimeException(e);
  923. }
  924. });
  925. ReentrantLock l = refdir.inProcessPackedRefsLock;
  926. l.lock();
  927. try {
  928. t.start();
  929. long timeoutSecs = 10;
  930. // Hold onto the lock until we observe the worker thread has
  931. // attempted to
  932. // acquire it.
  933. while (l.getQueueLength() == 0) {
  934. Thread.sleep(3);
  935. }
  936. // Once we unlock, the worker thread should finish the update
  937. // promptly.
  938. l.unlock();
  939. t.join(SECONDS.toMillis(timeoutSecs));
  940. } finally {
  941. if (l.isHeldByCurrentThread()) {
  942. l.unlock();
  943. }
  944. }
  945. assertEquals(initialRefsChangedEvents + 1, refsChangedEvents);
  946. }
  947. private void setLogAllRefUpdates(boolean enable) throws Exception {
  948. StoredConfig cfg = diskRepo.getConfig();
  949. cfg.load();
  950. cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  951. ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES, enable);
  952. cfg.save();
  953. }
  954. private void writeLooseRef(String name, AnyObjectId id) throws IOException {
  955. if (useReftable) {
  956. writeRef(name, id);
  957. } else {
  958. write(new File(diskRepo.getDirectory(), name), id.name() + "\n");
  959. // force the refs-changed event to be fired for the loose ref that
  960. // was created. We do this to get the events fired during the test
  961. // 'setup' out of the way and this allows us to now accurately
  962. // assert only for the new events fired during the BatchRefUpdate.
  963. refdir.exactRef(name);
  964. }
  965. }
  966. private void writeLooseRefs(String name1, AnyObjectId id1, String name2,
  967. AnyObjectId id2) throws IOException {
  968. if (useReftable) {
  969. BatchRefUpdate bru = diskRepo.getRefDatabase().newBatchUpdate();
  970. Ref r1 = diskRepo.exactRef(name1);
  971. ReceiveCommand c1 = new ReceiveCommand(
  972. r1 != null ? r1.getObjectId() : ObjectId.zeroId(),
  973. id1.toObjectId(), name1, r1 == null ? CREATE : UPDATE);
  974. Ref r2 = diskRepo.exactRef(name2);
  975. ReceiveCommand c2 = new ReceiveCommand(
  976. r2 != null ? r2.getObjectId() : ObjectId.zeroId(),
  977. id2.toObjectId(), name2, r2 == null ? CREATE : UPDATE);
  978. bru.addCommand(c1, c2);
  979. try (RevWalk rw = new RevWalk(diskRepo)) {
  980. bru.execute(rw, NullProgressMonitor.INSTANCE);
  981. }
  982. assertEquals(c2.getResult(), ReceiveCommand.Result.OK);
  983. assertEquals(c1.getResult(), ReceiveCommand.Result.OK);
  984. } else {
  985. writeLooseRef(name1, id1);
  986. writeLooseRef(name2, id2);
  987. }
  988. }
  989. private void writeRef(String name, AnyObjectId id) throws IOException {
  990. RefUpdate u = diskRepo.updateRef(name);
  991. u.setRefLogMessage(getClass().getSimpleName(), false);
  992. u.setForceUpdate(true);
  993. u.setNewObjectId(id);
  994. RefUpdate.Result r = u.update();
  995. switch (r) {
  996. case NEW:
  997. case FORCED:
  998. return;
  999. default:
  1000. throw new IOException("Got " + r + " while updating " + name);
  1001. }
  1002. }
  1003. private BatchRefUpdate newBatchUpdate(List<ReceiveCommand> cmds) {
  1004. BatchRefUpdate u = diskRepo.getRefDatabase().newBatchUpdate();
  1005. if (atomic) {
  1006. assertTrue(u.isAtomic());
  1007. } else {
  1008. u.setAtomic(false);
  1009. }
  1010. u.addCommand(cmds);
  1011. return u;
  1012. }
  1013. private void execute(BatchRefUpdate u) throws IOException {
  1014. execute(u, false);
  1015. }
  1016. private void execute(BatchRefUpdate u, boolean strictWork)
  1017. throws IOException {
  1018. try (RevWalk rw = new RevWalk(diskRepo)) {
  1019. u.execute(rw, strictWork ? new StrictWorkMonitor()
  1020. : NullProgressMonitor.INSTANCE);
  1021. }
  1022. }
  1023. private void assertRefs(Object... args) throws IOException {
  1024. if (args.length % 2 != 0) {
  1025. throw new IllegalArgumentException(
  1026. "expected even number of args: " + Arrays.toString(args));
  1027. }
  1028. Map<String, AnyObjectId> expected = new LinkedHashMap<>();
  1029. for (int i = 0; i < args.length; i += 2) {
  1030. expected.put((String) args[i], (AnyObjectId) args[i + 1]);
  1031. }
  1032. Map<String, Ref> refs = diskRepo.getRefDatabase()
  1033. .getRefs(RefDatabase.ALL);
  1034. Ref actualHead = refs.remove(Constants.HEAD);
  1035. if (actualHead != null) {
  1036. String actualLeafName = actualHead.getLeaf().getName();
  1037. assertEquals(
  1038. "expected HEAD to point to refs/heads/master, got: "
  1039. + actualLeafName,
  1040. "refs/heads/master", actualLeafName);
  1041. AnyObjectId expectedMaster = expected.get("refs/heads/master");
  1042. assertNotNull("expected master ref since HEAD exists",
  1043. expectedMaster);
  1044. assertEquals(expectedMaster, actualHead.getObjectId());
  1045. }
  1046. Map<String, AnyObjectId> actual = new LinkedHashMap<>();
  1047. refs.forEach((n, r) -> actual.put(n, r.getObjectId()));
  1048. assertEquals(expected.keySet(), actual.keySet());
  1049. actual.forEach((n, a) -> assertEquals(n, expected.get(n), a));
  1050. }
  1051. enum Result {
  1052. OK(ReceiveCommand.Result.OK),
  1053. LOCK_FAILURE(ReceiveCommand.Result.LOCK_FAILURE),
  1054. REJECTED_NONFASTFORWARD(ReceiveCommand.Result.REJECTED_NONFASTFORWARD),
  1055. REJECTED_MISSING_OBJECT(ReceiveCommand.Result.REJECTED_MISSING_OBJECT),
  1056. TRANSACTION_ABORTED(ReceiveCommand::isTransactionAborted);
  1057. @SuppressWarnings("ImmutableEnumChecker")
  1058. final Predicate<? super ReceiveCommand> p;
  1059. private Result(Predicate<? super ReceiveCommand> p) {
  1060. this.p = p;
  1061. }
  1062. private Result(ReceiveCommand.Result result) {
  1063. this(c -> c.getResult() == result);
  1064. }
  1065. }
  1066. private void assertResults(List<ReceiveCommand> cmds, Result... expected) {
  1067. if (expected.length != cmds.size()) {
  1068. throw new IllegalArgumentException(
  1069. "expected " + cmds.size() + " result args");
  1070. }
  1071. for (int i = 0; i < cmds.size(); i++) {
  1072. ReceiveCommand c = cmds.get(i);
  1073. Result r = expected[i];
  1074. assertTrue(String.format(
  1075. "result of command (%d) should be %s, got %s %s%s",
  1076. Integer.valueOf(i), r, c, c.getResult(),
  1077. c.getMessage() != null ? " (" + c.getMessage() + ")" : ""),
  1078. r.p.test(c));
  1079. }
  1080. }
  1081. private Map<String, ReflogEntry> getLastReflogs(String... names)
  1082. throws IOException {
  1083. Map<String, ReflogEntry> result = new LinkedHashMap<>();
  1084. for (String name : names) {
  1085. ReflogEntry e = getLastReflog(name);
  1086. if (e != null) {
  1087. result.put(name, e);
  1088. }
  1089. }
  1090. return result;
  1091. }
  1092. private ReflogEntry getLastReflog(String name) throws IOException {
  1093. ReflogReader r = diskRepo.getReflogReader(name);
  1094. if (r == null) {
  1095. return null;
  1096. }
  1097. return r.getLastEntry();
  1098. }
  1099. private File getLockFile(String refName) {
  1100. return LockFile.getLockFile(refdir.fileFor(refName));
  1101. }
  1102. private void assertReflogUnchanged(Map<String, ReflogEntry> old,
  1103. String name) throws IOException {
  1104. assertReflogEquals(old.get(name), getLastReflog(name), true);
  1105. }
  1106. private static void assertReflogEquals(ReflogEntry expected,
  1107. ReflogEntry actual) {
  1108. assertReflogEquals(expected, actual, false);
  1109. }
  1110. private static void assertReflogEquals(ReflogEntry expected,
  1111. ReflogEntry actual, boolean strictTime) {
  1112. if (expected == null) {
  1113. assertNull(actual);
  1114. return;
  1115. }
  1116. assertNotNull(actual);
  1117. assertEquals(expected.getOldId(), actual.getOldId());
  1118. assertEquals(expected.getNewId(), actual.getNewId());
  1119. if (strictTime) {
  1120. assertEquals(expected.getWho(), actual.getWho());
  1121. } else {
  1122. assertEquals(expected.getWho().getName(),
  1123. actual.getWho().getName());
  1124. assertEquals(expected.getWho().getEmailAddress(),
  1125. actual.getWho().getEmailAddress());
  1126. }
  1127. assertEquals(expected.getComment(), actual.getComment());
  1128. }
  1129. private static ReflogEntry reflog(ObjectId oldId, ObjectId newId,
  1130. PersonIdent who, String comment) {
  1131. return new ReflogEntry() {
  1132. @Override
  1133. public ObjectId getOldId() {
  1134. return oldId;
  1135. }
  1136. @Override
  1137. public ObjectId getNewId() {
  1138. return newId;
  1139. }
  1140. @Override
  1141. public PersonIdent getWho() {
  1142. return who;
  1143. }
  1144. @Override
  1145. public String getComment() {
  1146. return comment;
  1147. }
  1148. @Override
  1149. public CheckoutEntry parseCheckout() {
  1150. throw new UnsupportedOperationException();
  1151. }
  1152. };
  1153. }
  1154. private boolean batchesRefUpdates() {
  1155. return atomic || useReftable;
  1156. }
  1157. }