Run Eclipse formatter on BatchRefUpdateTest

Change-Id: I583491f621e54401c60150efb285fcc7243a9355
Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
This commit is contained in:
Han-Wen Nienhuys 2019-11-14 13:48:28 -08:00
parent 74bfec4112
commit 63fba337b2
1 changed files with 145 additions and 178 deletions

View File

@ -114,13 +114,18 @@ public class BatchRefUpdateTest extends LocalDiskRepositoryTestCase {
@Parameters(name = "atomic={0}") @Parameters(name = "atomic={0}")
public static Collection<Object[]> data() { public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{ {Boolean.FALSE}, {Boolean.TRUE} }); return Arrays
.asList(new Object[][] { { Boolean.FALSE }, { Boolean.TRUE } });
} }
private Repository diskRepo; private Repository diskRepo;
private TestRepository<Repository> repo; private TestRepository<Repository> repo;
private RefDirectory refdir; private RefDirectory refdir;
private RevCommit A; private RevCommit A;
private RevCommit B; private RevCommit B;
/** /**
@ -169,8 +174,8 @@ public void packedRefsFileIsSorted() throws IOException {
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
BatchRefUpdate bu = diskRepo.getRefDatabase().newBatchUpdate(); BatchRefUpdate bu = diskRepo.getRefDatabase().newBatchUpdate();
String b1 = String.format("refs/heads/a%d",i); String b1 = String.format("refs/heads/a%d", i);
String b2 = String.format("refs/heads/b%d",i); String b2 = String.format("refs/heads/b%d", i);
bu.setAtomic(atomic); bu.setAtomic(atomic);
ReceiveCommand c1 = new ReceiveCommand(ObjectId.zeroId(), A, b1); ReceiveCommand c1 = new ReceiveCommand(ObjectId.zeroId(), A, b1);
ReceiveCommand c2 = new ReceiveCommand(ObjectId.zeroId(), B, b2); ReceiveCommand c2 = new ReceiveCommand(ObjectId.zeroId(), B, b2);
@ -183,11 +188,12 @@ public void packedRefsFileIsSorted() throws IOException {
} }
File packed = new File(diskRepo.getDirectory(), "packed-refs"); File packed = new File(diskRepo.getDirectory(), "packed-refs");
String packedStr = new String(Files.readAllBytes(packed.toPath()), UTF_8); String packedStr = new String(Files.readAllBytes(packed.toPath()),
UTF_8);
int a2 = packedStr.indexOf("refs/heads/a1"); int a2 = packedStr.indexOf("refs/heads/a1");
int b1 = packedStr.indexOf("refs/heads/b0"); int b1 = packedStr.indexOf("refs/heads/b0");
assertTrue(a2 < b1); assertTrue(a2 < b1);
} }
@Test @Test
@ -197,20 +203,17 @@ public void simpleNoForce() throws IOException {
List<ReceiveCommand> cmds = Arrays.asList( List<ReceiveCommand> cmds = Arrays.asList(
new ReceiveCommand(A, B, "refs/heads/master", UPDATE), new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
new ReceiveCommand(B, A, "refs/heads/masters", UPDATE_NONFASTFORWARD)); new ReceiveCommand(B, A, "refs/heads/masters",
UPDATE_NONFASTFORWARD));
execute(newBatchUpdate(cmds)); execute(newBatchUpdate(cmds));
if (atomic) { if (atomic) {
assertResults(cmds, TRANSACTION_ABORTED, REJECTED_NONFASTFORWARD); assertResults(cmds, TRANSACTION_ABORTED, REJECTED_NONFASTFORWARD);
assertRefs( assertRefs("refs/heads/master", A, "refs/heads/masters", B);
"refs/heads/master", A,
"refs/heads/masters", B);
assertEquals(1, refsChangedEvents); assertEquals(1, refsChangedEvents);
} else { } else {
assertResults(cmds, OK, REJECTED_NONFASTFORWARD); assertResults(cmds, OK, REJECTED_NONFASTFORWARD);
assertRefs( assertRefs("refs/heads/master", B, "refs/heads/masters", B);
"refs/heads/master", B,
"refs/heads/masters", B);
assertEquals(2, refsChangedEvents); assertEquals(2, refsChangedEvents);
} }
} }
@ -222,31 +225,30 @@ public void simpleForce() throws IOException {
List<ReceiveCommand> cmds = Arrays.asList( List<ReceiveCommand> cmds = Arrays.asList(
new ReceiveCommand(A, B, "refs/heads/master", UPDATE), new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
new ReceiveCommand(B, A, "refs/heads/masters", UPDATE_NONFASTFORWARD)); new ReceiveCommand(B, A, "refs/heads/masters",
UPDATE_NONFASTFORWARD));
execute(newBatchUpdate(cmds).setAllowNonFastForwards(true)); execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
assertResults(cmds, OK, OK); assertResults(cmds, OK, OK);
assertRefs( assertRefs("refs/heads/master", B, "refs/heads/masters", A);
"refs/heads/master", B,
"refs/heads/masters", A);
assertEquals(atomic ? 2 : 3, refsChangedEvents); assertEquals(atomic ? 2 : 3, refsChangedEvents);
} }
@Test @Test
public void nonFastForwardDoesNotDoExpensiveMergeCheck() throws IOException { public void nonFastForwardDoesNotDoExpensiveMergeCheck()
throws IOException {
writeLooseRef("refs/heads/master", B); writeLooseRef("refs/heads/master", B);
List<ReceiveCommand> cmds = Arrays.asList( List<ReceiveCommand> cmds = Arrays.asList(new ReceiveCommand(B, A,
new ReceiveCommand(B, A, "refs/heads/master", UPDATE_NONFASTFORWARD)); "refs/heads/master", UPDATE_NONFASTFORWARD));
try (RevWalk rw = new RevWalk(diskRepo) { try (RevWalk rw = new RevWalk(diskRepo) {
@Override @Override
public boolean isMergedInto(RevCommit base, RevCommit tip) { public boolean isMergedInto(RevCommit base, RevCommit tip) {
throw new AssertionError("isMergedInto() should not be called"); throw new AssertionError("isMergedInto() should not be called");
} }
}) { }) {
newBatchUpdate(cmds) newBatchUpdate(cmds).setAllowNonFastForwards(true).execute(rw,
.setAllowNonFastForwards(true) new StrictWorkMonitor());
.execute(rw, new StrictWorkMonitor());
} }
assertResults(cmds, OK); assertResults(cmds, OK);
@ -266,21 +268,19 @@ public void fileDirectoryConflict() throws IOException {
execute(newBatchUpdate(cmds).setAllowNonFastForwards(true), false); execute(newBatchUpdate(cmds).setAllowNonFastForwards(true), false);
if (atomic) { if (atomic) {
// Atomic update sees that master and master/x are conflicting, then marks // Atomic update sees that master and master/x are conflicting, then
// marks
// the first one in the list as LOCK_FAILURE and aborts the rest. // the first one in the list as LOCK_FAILURE and aborts the rest.
assertResults(cmds, assertResults(cmds, LOCK_FAILURE, TRANSACTION_ABORTED,
LOCK_FAILURE, TRANSACTION_ABORTED, TRANSACTION_ABORTED); TRANSACTION_ABORTED);
assertRefs( assertRefs("refs/heads/master", A, "refs/heads/masters", B);
"refs/heads/master", A,
"refs/heads/masters", B);
assertEquals(1, refsChangedEvents); assertEquals(1, refsChangedEvents);
} else { } else {
// Non-atomic updates are applied in order: master succeeds, then master/x // Non-atomic updates are applied in order: master succeeds, then
// master/x
// fails due to conflict. // fails due to conflict.
assertResults(cmds, OK, LOCK_FAILURE, LOCK_FAILURE); assertResults(cmds, OK, LOCK_FAILURE, LOCK_FAILURE);
assertRefs( assertRefs("refs/heads/master", B, "refs/heads/masters", B);
"refs/heads/master", B,
"refs/heads/masters", B);
assertEquals(2, refsChangedEvents); assertEquals(2, refsChangedEvents);
} }
} }
@ -297,9 +297,7 @@ public void conflictThanksToDelete() throws IOException {
execute(newBatchUpdate(cmds).setAllowNonFastForwards(true)); execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
assertResults(cmds, OK, OK, OK); assertResults(cmds, OK, OK, OK);
assertRefs( assertRefs("refs/heads/master", B, "refs/heads/masters/x", A);
"refs/heads/master", B,
"refs/heads/masters/x", A);
if (atomic) { if (atomic) {
assertEquals(2, refsChangedEvents); assertEquals(2, refsChangedEvents);
} else { } else {
@ -315,8 +313,8 @@ public void conflictThanksToDelete() throws IOException {
public void updateToMissingObject() throws IOException { public void updateToMissingObject() throws IOException {
writeLooseRef("refs/heads/master", A); writeLooseRef("refs/heads/master", A);
ObjectId bad = ObjectId bad = ObjectId
ObjectId.fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"); .fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
List<ReceiveCommand> cmds = Arrays.asList( List<ReceiveCommand> cmds = Arrays.asList(
new ReceiveCommand(A, bad, "refs/heads/master", UPDATE), new ReceiveCommand(A, bad, "refs/heads/master", UPDATE),
new ReceiveCommand(zeroId(), B, "refs/heads/foo2", CREATE)); new ReceiveCommand(zeroId(), B, "refs/heads/foo2", CREATE));
@ -328,9 +326,7 @@ public void updateToMissingObject() throws IOException {
assertEquals(1, refsChangedEvents); assertEquals(1, refsChangedEvents);
} else { } else {
assertResults(cmds, REJECTED_MISSING_OBJECT, OK); assertResults(cmds, REJECTED_MISSING_OBJECT, OK);
assertRefs( assertRefs("refs/heads/master", A, "refs/heads/foo2", B);
"refs/heads/master", A,
"refs/heads/foo2", B);
assertEquals(2, refsChangedEvents); assertEquals(2, refsChangedEvents);
} }
} }
@ -339,8 +335,8 @@ public void updateToMissingObject() throws IOException {
public void addMissingObject() throws IOException { public void addMissingObject() throws IOException {
writeLooseRef("refs/heads/master", A); writeLooseRef("refs/heads/master", A);
ObjectId bad = ObjectId bad = ObjectId
ObjectId.fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"); .fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
List<ReceiveCommand> cmds = Arrays.asList( List<ReceiveCommand> cmds = Arrays.asList(
new ReceiveCommand(A, B, "refs/heads/master", UPDATE), new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
new ReceiveCommand(zeroId(), bad, "refs/heads/foo2", CREATE)); new ReceiveCommand(zeroId(), bad, "refs/heads/foo2", CREATE));
@ -390,9 +386,7 @@ public void oneRefWrongOldValue() throws IOException {
assertEquals(1, refsChangedEvents); assertEquals(1, refsChangedEvents);
} else { } else {
assertResults(cmds, LOCK_FAILURE, OK); assertResults(cmds, LOCK_FAILURE, OK);
assertRefs( assertRefs("refs/heads/master", A, "refs/heads/foo2", B);
"refs/heads/master", A,
"refs/heads/foo2", B);
assertEquals(2, refsChangedEvents); assertEquals(2, refsChangedEvents);
} }
} }
@ -421,9 +415,10 @@ public void nonExistentRef() throws IOException {
public void noRefLog() throws IOException { public void noRefLog() throws IOException {
writeRef("refs/heads/master", A); writeRef("refs/heads/master", A);
Map<String, ReflogEntry> oldLogs = Map<String, ReflogEntry> oldLogs = getLastReflogs("refs/heads/master",
getLastReflogs("refs/heads/master", "refs/heads/branch"); "refs/heads/branch");
assertEquals(Collections.singleton("refs/heads/master"), oldLogs.keySet()); assertEquals(Collections.singleton("refs/heads/master"),
oldLogs.keySet());
List<ReceiveCommand> cmds = Arrays.asList( List<ReceiveCommand> cmds = Arrays.asList(
new ReceiveCommand(A, B, "refs/heads/master", UPDATE), new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
@ -431,9 +426,7 @@ public void noRefLog() throws IOException {
execute(newBatchUpdate(cmds).setAllowNonFastForwards(true)); execute(newBatchUpdate(cmds).setAllowNonFastForwards(true));
assertResults(cmds, OK, OK); assertResults(cmds, OK, OK);
assertRefs( assertRefs("refs/heads/master", B, "refs/heads/branch", B);
"refs/heads/master", B,
"refs/heads/branch", B);
assertEquals(atomic ? 2 : 3, refsChangedEvents); assertEquals(atomic ? 2 : 3, refsChangedEvents);
assertReflogUnchanged(oldLogs, "refs/heads/master"); assertReflogUnchanged(oldLogs, "refs/heads/master");
assertReflogUnchanged(oldLogs, "refs/heads/branch"); assertReflogUnchanged(oldLogs, "refs/heads/branch");
@ -444,24 +437,19 @@ public void reflogDefaultIdent() throws IOException {
writeRef("refs/heads/master", A); writeRef("refs/heads/master", A);
writeRef("refs/heads/branch2", A); writeRef("refs/heads/branch2", A);
Map<String, ReflogEntry> oldLogs = getLastReflogs( Map<String, ReflogEntry> oldLogs = getLastReflogs("refs/heads/master",
"refs/heads/master", "refs/heads/branch1", "refs/heads/branch2"); "refs/heads/branch1", "refs/heads/branch2");
List<ReceiveCommand> cmds = Arrays.asList( List<ReceiveCommand> cmds = Arrays.asList(
new ReceiveCommand(A, B, "refs/heads/master", UPDATE), new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
new ReceiveCommand(zeroId(), B, "refs/heads/branch1", CREATE)); new ReceiveCommand(zeroId(), B, "refs/heads/branch1", CREATE));
execute( execute(newBatchUpdate(cmds).setAllowNonFastForwards(true)
newBatchUpdate(cmds) .setRefLogMessage("a reflog", false));
.setAllowNonFastForwards(true)
.setRefLogMessage("a reflog", false));
assertResults(cmds, OK, OK); assertResults(cmds, OK, OK);
assertRefs( assertRefs("refs/heads/master", B, "refs/heads/branch1", B,
"refs/heads/master", B,
"refs/heads/branch1", B,
"refs/heads/branch2", A); "refs/heads/branch2", A);
assertEquals(atomic ? 3 : 4, refsChangedEvents); assertEquals(atomic ? 3 : 4, refsChangedEvents);
assertReflogEquals( assertReflogEquals(reflog(A, B, new PersonIdent(diskRepo), "a reflog"),
reflog(A, B, new PersonIdent(diskRepo), "a reflog"),
getLastReflog("refs/heads/master")); getLastReflog("refs/heads/master"));
assertReflogEquals( assertReflogEquals(
reflog(zeroId(), B, new PersonIdent(diskRepo), "a reflog"), reflog(zeroId(), B, new PersonIdent(diskRepo), "a reflog"),
@ -476,21 +464,19 @@ public void reflogAppendStatusNoMessage() throws IOException {
List<ReceiveCommand> cmds = Arrays.asList( List<ReceiveCommand> cmds = Arrays.asList(
new ReceiveCommand(A, B, "refs/heads/master", UPDATE), new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
new ReceiveCommand(B, A, "refs/heads/branch1", UPDATE_NONFASTFORWARD), new ReceiveCommand(B, A, "refs/heads/branch1",
UPDATE_NONFASTFORWARD),
new ReceiveCommand(zeroId(), A, "refs/heads/branch2", CREATE)); new ReceiveCommand(zeroId(), A, "refs/heads/branch2", CREATE));
execute( execute(newBatchUpdate(cmds).setAllowNonFastForwards(true)
newBatchUpdate(cmds) .setRefLogMessage(null, true));
.setAllowNonFastForwards(true)
.setRefLogMessage(null, true));
assertResults(cmds, OK, OK, OK); assertResults(cmds, OK, OK, OK);
assertRefs( assertRefs("refs/heads/master", B, "refs/heads/branch1", A,
"refs/heads/master", B,
"refs/heads/branch1", A,
"refs/heads/branch2", A); "refs/heads/branch2", A);
assertEquals(atomic ? 3 : 5, refsChangedEvents); assertEquals(atomic ? 3 : 5, refsChangedEvents);
assertReflogEquals( assertReflogEquals(
// Always forced; setAllowNonFastForwards(true) bypasses the check. // Always forced; setAllowNonFastForwards(true) bypasses the
// check.
reflog(A, B, new PersonIdent(diskRepo), "forced-update"), reflog(A, B, new PersonIdent(diskRepo), "forced-update"),
getLastReflog("refs/heads/master")); getLastReflog("refs/heads/master"));
assertReflogEquals( assertReflogEquals(
@ -505,8 +491,8 @@ public void reflogAppendStatusNoMessage() throws IOException {
public void reflogAppendStatusFastForward() throws IOException { public void reflogAppendStatusFastForward() throws IOException {
writeRef("refs/heads/master", A); writeRef("refs/heads/master", A);
List<ReceiveCommand> cmds = Arrays.asList( List<ReceiveCommand> cmds = Arrays
new ReceiveCommand(A, B, "refs/heads/master", UPDATE)); .asList(new ReceiveCommand(A, B, "refs/heads/master", UPDATE));
execute(newBatchUpdate(cmds).setRefLogMessage(null, true)); execute(newBatchUpdate(cmds).setRefLogMessage(null, true));
assertResults(cmds, OK); assertResults(cmds, OK);
@ -527,15 +513,15 @@ public void reflogAppendStatusWithMessage() throws IOException {
execute(newBatchUpdate(cmds).setRefLogMessage("a reflog", true)); execute(newBatchUpdate(cmds).setRefLogMessage("a reflog", true));
assertResults(cmds, OK, OK); assertResults(cmds, OK, OK);
assertRefs( assertRefs("refs/heads/master", B, "refs/heads/branch", A);
"refs/heads/master", B,
"refs/heads/branch", A);
assertEquals(atomic ? 2 : 3, refsChangedEvents); assertEquals(atomic ? 2 : 3, refsChangedEvents);
assertReflogEquals( assertReflogEquals(
reflog(A, B, new PersonIdent(diskRepo), "a reflog: fast-forward"), reflog(A, B, new PersonIdent(diskRepo),
"a reflog: fast-forward"),
getLastReflog("refs/heads/master")); getLastReflog("refs/heads/master"));
assertReflogEquals( assertReflogEquals(
reflog(zeroId(), A, new PersonIdent(diskRepo), "a reflog: created"), reflog(zeroId(), A, new PersonIdent(diskRepo),
"a reflog: created"),
getLastReflog("refs/heads/branch")); getLastReflog("refs/heads/branch"));
} }
@ -546,33 +532,26 @@ public void reflogCustomIdent() throws IOException {
List<ReceiveCommand> cmds = Arrays.asList( List<ReceiveCommand> cmds = Arrays.asList(
new ReceiveCommand(A, B, "refs/heads/master", UPDATE), new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
new ReceiveCommand(zeroId(), B, "refs/heads/branch", CREATE)); new ReceiveCommand(zeroId(), B, "refs/heads/branch", CREATE));
PersonIdent ident = new PersonIdent("A Reflog User", "reflog@example.com"); PersonIdent ident = new PersonIdent("A Reflog User",
execute( "reflog@example.com");
newBatchUpdate(cmds) execute(newBatchUpdate(cmds).setRefLogMessage("a reflog", false)
.setRefLogMessage("a reflog", false) .setRefLogIdent(ident));
.setRefLogIdent(ident));
assertResults(cmds, OK, OK); assertResults(cmds, OK, OK);
assertEquals(atomic ? 2 : 3, refsChangedEvents); assertEquals(atomic ? 2 : 3, refsChangedEvents);
assertRefs( assertRefs("refs/heads/master", B, "refs/heads/branch", B);
"refs/heads/master", B, assertReflogEquals(reflog(A, B, ident, "a reflog"),
"refs/heads/branch", B); getLastReflog("refs/heads/master"), true);
assertReflogEquals( assertReflogEquals(reflog(zeroId(), B, ident, "a reflog"),
reflog(A, B, ident, "a reflog"), getLastReflog("refs/heads/branch"), true);
getLastReflog("refs/heads/master"),
true);
assertReflogEquals(
reflog(zeroId(), B, ident, "a reflog"),
getLastReflog("refs/heads/branch"),
true);
} }
@Test @Test
public void reflogDelete() throws IOException { public void reflogDelete() throws IOException {
writeRef("refs/heads/master", A); writeRef("refs/heads/master", A);
writeRef("refs/heads/branch", A); writeRef("refs/heads/branch", A);
assertEquals( assertEquals(2, getLastReflogs("refs/heads/master", "refs/heads/branch")
2, getLastReflogs("refs/heads/master", "refs/heads/branch").size()); .size());
List<ReceiveCommand> cmds = Arrays.asList( List<ReceiveCommand> cmds = Arrays.asList(
new ReceiveCommand(A, zeroId(), "refs/heads/master", DELETE), new ReceiveCommand(A, zeroId(), "refs/heads/master", DELETE),
@ -583,8 +562,7 @@ public void reflogDelete() throws IOException {
assertRefs("refs/heads/branch", B); assertRefs("refs/heads/branch", B);
assertEquals(atomic ? 3 : 4, refsChangedEvents); assertEquals(atomic ? 3 : 4, refsChangedEvents);
assertNull(getLastReflog("refs/heads/master")); assertNull(getLastReflog("refs/heads/master"));
assertReflogEquals( assertReflogEquals(reflog(A, B, new PersonIdent(diskRepo), "a reflog"),
reflog(A, B, new PersonIdent(diskRepo), "a reflog"),
getLastReflog("refs/heads/branch")); getLastReflog("refs/heads/branch"));
} }
@ -610,8 +588,8 @@ public void reflogFileDirectoryConflict() throws IOException {
public void reflogOnLockFailure() throws IOException { public void reflogOnLockFailure() throws IOException {
writeRef("refs/heads/master", A); writeRef("refs/heads/master", A);
Map<String, ReflogEntry> oldLogs = Map<String, ReflogEntry> oldLogs = getLastReflogs("refs/heads/master",
getLastReflogs("refs/heads/master", "refs/heads/branch"); "refs/heads/branch");
List<ReceiveCommand> cmds = Arrays.asList( List<ReceiveCommand> cmds = Arrays.asList(
new ReceiveCommand(A, B, "refs/heads/master", UPDATE), new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
@ -642,29 +620,23 @@ public void overrideRefLogMessage() throws Exception {
new ReceiveCommand(zeroId(), B, "refs/heads/branch", CREATE)); new ReceiveCommand(zeroId(), B, "refs/heads/branch", CREATE));
cmds.get(0).setRefLogMessage("custom log", false); cmds.get(0).setRefLogMessage("custom log", false);
PersonIdent ident = new PersonIdent(diskRepo); PersonIdent ident = new PersonIdent(diskRepo);
execute( execute(newBatchUpdate(cmds).setRefLogIdent(ident)
newBatchUpdate(cmds) .setRefLogMessage("a reflog", true));
.setRefLogIdent(ident)
.setRefLogMessage("a reflog", true));
assertResults(cmds, OK, OK); assertResults(cmds, OK, OK);
assertEquals(atomic ? 2 : 3, refsChangedEvents); assertEquals(atomic ? 2 : 3, refsChangedEvents);
assertReflogEquals( assertReflogEquals(reflog(A, B, ident, "custom log"),
reflog(A, B, ident, "custom log"), getLastReflog("refs/heads/master"), true);
getLastReflog("refs/heads/master"), assertReflogEquals(reflog(zeroId(), B, ident, "a reflog: created"),
true); getLastReflog("refs/heads/branch"), true);
assertReflogEquals(
reflog(zeroId(), B, ident, "a reflog: created"),
getLastReflog("refs/heads/branch"),
true);
} }
@Test @Test
public void overrideDisableRefLog() throws Exception { public void overrideDisableRefLog() throws Exception {
writeRef("refs/heads/master", A); writeRef("refs/heads/master", A);
Map<String, ReflogEntry> oldLogs = Map<String, ReflogEntry> oldLogs = getLastReflogs("refs/heads/master",
getLastReflogs("refs/heads/master", "refs/heads/branch"); "refs/heads/branch");
List<ReceiveCommand> cmds = Arrays.asList( List<ReceiveCommand> cmds = Arrays.asList(
new ReceiveCommand(A, B, "refs/heads/master", UPDATE), new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
@ -676,7 +648,8 @@ public void overrideDisableRefLog() throws Exception {
assertEquals(atomic ? 2 : 3, refsChangedEvents); assertEquals(atomic ? 2 : 3, refsChangedEvents);
assertReflogUnchanged(oldLogs, "refs/heads/master"); assertReflogUnchanged(oldLogs, "refs/heads/master");
assertReflogEquals( assertReflogEquals(
reflog(zeroId(), B, new PersonIdent(diskRepo), "a reflog: created"), reflog(zeroId(), B, new PersonIdent(diskRepo),
"a reflog: created"),
getLastReflog("refs/heads/branch")); getLastReflog("refs/heads/branch"));
} }
@ -685,8 +658,8 @@ public void refLogNotWrittenWithoutConfigOption() throws Exception {
setLogAllRefUpdates(false); setLogAllRefUpdates(false);
writeRef("refs/heads/master", A); writeRef("refs/heads/master", A);
Map<String, ReflogEntry> oldLogs = Map<String, ReflogEntry> oldLogs = getLastReflogs("refs/heads/master",
getLastReflogs("refs/heads/master", "refs/heads/branch"); "refs/heads/branch");
assertTrue(oldLogs.isEmpty()); assertTrue(oldLogs.isEmpty());
List<ReceiveCommand> cmds = Arrays.asList( List<ReceiveCommand> cmds = Arrays.asList(
@ -703,20 +676,17 @@ public void refLogNotWrittenWithoutConfigOption() throws Exception {
public void forceRefLogInUpdate() throws Exception { public void forceRefLogInUpdate() throws Exception {
setLogAllRefUpdates(false); setLogAllRefUpdates(false);
writeRef("refs/heads/master", A); writeRef("refs/heads/master", A);
assertTrue( assertTrue(getLastReflogs("refs/heads/master", "refs/heads/branch")
getLastReflogs("refs/heads/master", "refs/heads/branch").isEmpty()); .isEmpty());
List<ReceiveCommand> cmds = Arrays.asList( List<ReceiveCommand> cmds = Arrays.asList(
new ReceiveCommand(A, B, "refs/heads/master", UPDATE), new ReceiveCommand(A, B, "refs/heads/master", UPDATE),
new ReceiveCommand(zeroId(), B, "refs/heads/branch", CREATE)); new ReceiveCommand(zeroId(), B, "refs/heads/branch", CREATE));
execute( execute(newBatchUpdate(cmds).setRefLogMessage("a reflog", false)
newBatchUpdate(cmds) .setForceRefLog(true));
.setRefLogMessage("a reflog", false)
.setForceRefLog(true));
assertResults(cmds, OK, OK); assertResults(cmds, OK, OK);
assertReflogEquals( assertReflogEquals(reflog(A, B, new PersonIdent(diskRepo), "a reflog"),
reflog(A, B, new PersonIdent(diskRepo), "a reflog"),
getLastReflog("refs/heads/master")); getLastReflog("refs/heads/master"));
assertReflogEquals( assertReflogEquals(
reflog(zeroId(), B, new PersonIdent(diskRepo), "a reflog"), reflog(zeroId(), B, new PersonIdent(diskRepo), "a reflog"),
@ -728,8 +698,8 @@ public void forceRefLogInCommand() throws Exception {
setLogAllRefUpdates(false); setLogAllRefUpdates(false);
writeRef("refs/heads/master", A); writeRef("refs/heads/master", A);
Map<String, ReflogEntry> oldLogs = Map<String, ReflogEntry> oldLogs = getLastReflogs("refs/heads/master",
getLastReflogs("refs/heads/master", "refs/heads/branch"); "refs/heads/branch");
assertTrue(oldLogs.isEmpty()); assertTrue(oldLogs.isEmpty());
List<ReceiveCommand> cmds = Arrays.asList( List<ReceiveCommand> cmds = Arrays.asList(
@ -765,11 +735,10 @@ public void packedRefsLockFailure() throws Exception {
assertRefs("refs/heads/master", A); assertRefs("refs/heads/master", A);
assertEquals(1, refsChangedEvents); assertEquals(1, refsChangedEvents);
} else { } else {
// Only operates on loose refs, doesn't care that packed-refs is locked. // Only operates on loose refs, doesn't care that packed-refs is
// locked.
assertResults(cmds, OK, OK); assertResults(cmds, OK, OK);
assertRefs( assertRefs("refs/heads/master", B, "refs/heads/branch", B);
"refs/heads/master", B,
"refs/heads/branch", B);
assertEquals(3, refsChangedEvents); assertEquals(3, refsChangedEvents);
} }
} finally { } finally {
@ -799,9 +768,7 @@ public void oneRefLockFailure() throws Exception {
assertEquals(1, refsChangedEvents); assertEquals(1, refsChangedEvents);
} else { } else {
assertResults(cmds, OK, LOCK_FAILURE); assertResults(cmds, OK, LOCK_FAILURE);
assertRefs( assertRefs("refs/heads/branch", B, "refs/heads/master", A);
"refs/heads/branch", B,
"refs/heads/master", A);
assertEquals(2, refsChangedEvents); assertEquals(2, refsChangedEvents);
} }
} finally { } finally {
@ -813,8 +780,8 @@ public void oneRefLockFailure() throws Exception {
public void singleRefUpdateDoesNotRequirePackedRefsLock() throws Exception { public void singleRefUpdateDoesNotRequirePackedRefsLock() throws Exception {
writeLooseRef("refs/heads/master", A); writeLooseRef("refs/heads/master", A);
List<ReceiveCommand> cmds = Arrays.asList( List<ReceiveCommand> cmds = Arrays
new ReceiveCommand(A, B, "refs/heads/master", UPDATE)); .asList(new ReceiveCommand(A, B, "refs/heads/master", UPDATE));
LockFile myLock = refdir.lockPackedRefs(); LockFile myLock = refdir.lockPackedRefs();
try { try {
@ -854,7 +821,8 @@ public void atomicUpdateRespectsInProcessLock() throws Exception {
long timeoutSecs = 10; long timeoutSecs = 10;
long startNanos = System.nanoTime(); long startNanos = System.nanoTime();
// Hold onto the lock until we observe the worker thread has attempted to // Hold onto the lock until we observe the worker thread has
// attempted to
// acquire it. // acquire it.
while (l.getQueueLength() == 0) { while (l.getQueueLength() == 0) {
long elapsedNanos = System.nanoTime() - startNanos; long elapsedNanos = System.nanoTime() - startNanos;
@ -864,7 +832,8 @@ public void atomicUpdateRespectsInProcessLock() throws Exception {
Thread.sleep(3); Thread.sleep(3);
} }
// Once we unlock, the worker thread should finish the update promptly. // Once we unlock, the worker thread should finish the update
// promptly.
l.unlock(); l.unlock();
t.join(SECONDS.toMillis(timeoutSecs)); t.join(SECONDS.toMillis(timeoutSecs));
assertFalse(t.isAlive()); assertFalse(t.isAlive());
@ -876,9 +845,7 @@ public void atomicUpdateRespectsInProcessLock() throws Exception {
assertResults(cmds, OK, OK); assertResults(cmds, OK, OK);
assertEquals(2, refsChangedEvents); assertEquals(2, refsChangedEvents);
assertRefs( assertRefs("refs/heads/master", B, "refs/heads/branch", B);
"refs/heads/master", B,
"refs/heads/branch", B);
} }
private void setLogAllRefUpdates(boolean enable) throws Exception { private void setLogAllRefUpdates(boolean enable) throws Exception {
@ -900,11 +867,11 @@ private void writeRef(String name, AnyObjectId id) throws IOException {
u.setNewObjectId(id); u.setNewObjectId(id);
RefUpdate.Result r = u.update(); RefUpdate.Result r = u.update();
switch (r) { switch (r) {
case NEW: case NEW:
case FORCED: case FORCED:
return; return;
default: default:
throw new IOException("Got " + r + " while updating " + name); throw new IOException("Got " + r + " while updating " + name);
} }
} }
@ -923,10 +890,11 @@ private void execute(BatchRefUpdate u) throws IOException {
execute(u, false); execute(u, false);
} }
private void execute(BatchRefUpdate u, boolean strictWork) throws IOException { private void execute(BatchRefUpdate u, boolean strictWork)
throws IOException {
try (RevWalk rw = new RevWalk(diskRepo)) { try (RevWalk rw = new RevWalk(diskRepo)) {
u.execute(rw, u.execute(rw, strictWork ? new StrictWorkMonitor()
strictWork ? new StrictWorkMonitor() : NullProgressMonitor.INSTANCE); : NullProgressMonitor.INSTANCE);
} }
} }
@ -946,10 +914,12 @@ private void assertRefs(Object... args) throws IOException {
if (actualHead != null) { if (actualHead != null) {
String actualLeafName = actualHead.getLeaf().getName(); String actualLeafName = actualHead.getLeaf().getName();
assertEquals( assertEquals(
"expected HEAD to point to refs/heads/master, got: " + actualLeafName, "expected HEAD to point to refs/heads/master, got: "
+ actualLeafName,
"refs/heads/master", actualLeafName); "refs/heads/master", actualLeafName);
AnyObjectId expectedMaster = expected.get("refs/heads/master"); AnyObjectId expectedMaster = expected.get("refs/heads/master");
assertNotNull("expected master ref since HEAD exists", expectedMaster); assertNotNull("expected master ref since HEAD exists",
expectedMaster);
assertEquals(expectedMaster, actualHead.getObjectId()); assertEquals(expectedMaster, actualHead.getObjectId());
} }
@ -961,11 +931,11 @@ private void assertRefs(Object... args) throws IOException {
} }
enum Result { enum Result {
OK(ReceiveCommand.Result.OK), OK(ReceiveCommand.Result.OK), LOCK_FAILURE(
LOCK_FAILURE(ReceiveCommand.Result.LOCK_FAILURE), ReceiveCommand.Result.LOCK_FAILURE), REJECTED_NONFASTFORWARD(
REJECTED_NONFASTFORWARD(ReceiveCommand.Result.REJECTED_NONFASTFORWARD), ReceiveCommand.Result.REJECTED_NONFASTFORWARD), REJECTED_MISSING_OBJECT(
REJECTED_MISSING_OBJECT(ReceiveCommand.Result.REJECTED_MISSING_OBJECT), ReceiveCommand.Result.REJECTED_MISSING_OBJECT), TRANSACTION_ABORTED(
TRANSACTION_ABORTED(ReceiveCommand::isTransactionAborted); ReceiveCommand::isTransactionAborted);
@SuppressWarnings("ImmutableEnumChecker") @SuppressWarnings("ImmutableEnumChecker")
final Predicate<? super ReceiveCommand> p; final Predicate<? super ReceiveCommand> p;
@ -979,8 +949,7 @@ private Result(ReceiveCommand.Result result) {
} }
} }
private void assertResults( private void assertResults(List<ReceiveCommand> cmds, Result... expected) {
List<ReceiveCommand> cmds, Result... expected) {
if (expected.length != cmds.size()) { if (expected.length != cmds.size()) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"expected " + cmds.size() + " result args"); "expected " + cmds.size() + " result args");
@ -988,12 +957,10 @@ private void assertResults(
for (int i = 0; i < cmds.size(); i++) { for (int i = 0; i < cmds.size(); i++) {
ReceiveCommand c = cmds.get(i); ReceiveCommand c = cmds.get(i);
Result r = expected[i]; Result r = expected[i];
assertTrue( assertTrue(String.format(
String.format( "result of command (%d) should be %s: %s %s%s",
"result of command (%d) should be %s: %s %s%s", Integer.valueOf(i), r, c, c.getResult(),
Integer.valueOf(i), r, c, c.getMessage() != null ? " (" + c.getMessage() + ")" : ""),
c.getResult(),
c.getMessage() != null ? " (" + c.getMessage() + ")" : ""),
r.p.test(c)); r.p.test(c));
} }
} }
@ -1022,18 +989,18 @@ private File getLockFile(String refName) {
return LockFile.getLockFile(refdir.fileFor(refName)); return LockFile.getLockFile(refdir.fileFor(refName));
} }
private void assertReflogUnchanged( private void assertReflogUnchanged(Map<String, ReflogEntry> old,
Map<String, ReflogEntry> old, String name) throws IOException { String name) throws IOException {
assertReflogEquals(old.get(name), getLastReflog(name), true); assertReflogEquals(old.get(name), getLastReflog(name), true);
} }
private static void assertReflogEquals( private static void assertReflogEquals(ReflogEntry expected,
ReflogEntry expected, ReflogEntry actual) { ReflogEntry actual) {
assertReflogEquals(expected, actual, false); assertReflogEquals(expected, actual, false);
} }
private static void assertReflogEquals( private static void assertReflogEquals(ReflogEntry expected,
ReflogEntry expected, ReflogEntry actual, boolean strictTime) { ReflogEntry actual, boolean strictTime) {
if (expected == null) { if (expected == null) {
assertNull(actual); assertNull(actual);
return; return;
@ -1044,9 +1011,9 @@ private static void assertReflogEquals(
if (strictTime) { if (strictTime) {
assertEquals(expected.getWho(), actual.getWho()); assertEquals(expected.getWho(), actual.getWho());
} else { } else {
assertEquals(expected.getWho().getName(), actual.getWho().getName()); assertEquals(expected.getWho().getName(),
assertEquals( actual.getWho().getName());
expected.getWho().getEmailAddress(), assertEquals(expected.getWho().getEmailAddress(),
actual.getWho().getEmailAddress()); actual.getWho().getEmailAddress());
} }
assertEquals(expected.getComment(), actual.getComment()); assertEquals(expected.getComment(), actual.getComment());