Move addition of refs/users/self symlink out of filtering logic

Gerrit moved to a model where it wraps around RefDatabase to tell JGit
which refs are visible to a user. This was done in I0bb85de2b. This
meant moving filtering code from a AdvertiseRefsHook into a wrapper of
RefDatabase.

There is one case in which the logic that got moved would not only
filter refs but add a ref in addition that is not present in the input:
when authenticated users interact with All-Users. In this case, Gerrit
adds a refs/users/self symbolic reference to make it easier to interact
with one's account data.

This led to a problem with the new inferface and smelled as a whole
since concerns weren't clearly separated. The problem with the new
interface would occur when using RefDatabase#exactRef - a method that
expects {0,1} refs but got two for All-Users since refs/users/self was
added.

This change fixes the issue by separating the addition of the symref
into an AdvertiseRefsHook and leaving ref filtering by just that.

RefAdvertisementIT has different tests for testing the advertisement of
refs/users/self and PushAccountIT covers push cases. The tests succeeded
before, because the problem could only be encountered when the in-memory
ref cache on the push path is not used. We add a config to run push tests
without in-memory ref cache to make sure the code path that failed
previosuly gets executed now.

Bug: Issue 12027
Change-Id: I07777b698ddb86ad5cec1407ac5671874866fee5
This commit is contained in:
Patrick Hiesel
2020-01-10 14:14:51 +01:00
parent 4c0f07cb04
commit 2e505b7ac0
11 changed files with 204 additions and 42 deletions

View File

@@ -68,7 +68,6 @@ import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.SymbolicRef;
class DefaultRefFilter {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
@@ -206,10 +205,6 @@ class DefaultRefFilter {
throws PermissionBackendException {
logger.atFinest().log("Filter refs (refs = %s)", refs);
if (projectState.isAllUsers()) {
refs = addUsersSelfSymref(repo, refs);
}
// TODO(hiesel): Remove when optimization is done.
boolean hasReadOnRefsStar =
checkProjectPermission(permissionBackendForProject, ProjectPermission.READ);
@@ -400,28 +395,6 @@ class DefaultRefFilter {
return refs;
}
private List<Ref> addUsersSelfSymref(Repository repo, List<Ref> refs)
throws PermissionBackendException {
if (user.isIdentifiedUser()) {
String refName = RefNames.refsUsers(user.getAccountId());
try {
Ref r = repo.exactRef(refName);
if (r == null) {
logger.atWarning().log("User ref %s not found", refName);
return refs;
}
SymbolicRef s = new SymbolicRef(REFS_USERS_SELF, r);
refs = new ArrayList<>(refs);
refs.add(s);
logger.atFinest().log("Added %s as alias for user ref %s", REFS_USERS_SELF, refName);
} catch (IOException e) {
throw new PermissionBackendException(e);
}
}
return refs;
}
private boolean visible(Repository repo, Change.Id changeId) throws PermissionBackendException {
if (visibleChanges == null) {
if (changeCache == null) {