Browse Source
Move all connectivity check to separate classes. Set default to be FullConnectivityChecker i.e. checker which will check connectivity from all advertised refs. Add ability to set other connectivity checker which can use different approach. Signed-off-by: Demetr Starshov <dstarshov@google.com> Change-Id: Ibb107dbfbdde8ad109be25b5ecf42be7660ef736stable-5.7
Demetr Starshov
5 years ago
3 changed files with 362 additions and 85 deletions
@ -0,0 +1,138 @@
|
||||
/* |
||||
* Copyright (c) 2019, Google LLC and others |
||||
* |
||||
* This program and the accompanying materials are made available under the |
||||
* terms of the Eclipse Distribution License v. 1.0 which is available at |
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
* |
||||
* SPDX-License-Identifier: BSD-3-Clause |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.transport.internal; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
|
||||
import org.eclipse.jgit.lib.ObjectId; |
||||
import org.eclipse.jgit.lib.ProgressMonitor; |
||||
import org.eclipse.jgit.lib.Repository; |
||||
import org.eclipse.jgit.revwalk.RevWalk; |
||||
import org.eclipse.jgit.transport.PackParser; |
||||
import org.eclipse.jgit.transport.ReceiveCommand; |
||||
|
||||
/** |
||||
* Checks that a received pack only depends on objects which are reachable from |
||||
* a defined set of references. |
||||
*/ |
||||
public interface ConnectivityChecker { |
||||
|
||||
/** |
||||
* Checks connectivity of the commit graph after pack uploading. |
||||
* |
||||
* @param connectivityCheckInfo |
||||
* Input for the connectivity check. |
||||
* @param haves |
||||
* Set of references known for client. |
||||
* @param pm |
||||
* Monitor to publish progress to. |
||||
* @throws IOException |
||||
* an error occurred during connectivity checking. |
||||
* |
||||
*/ |
||||
void checkConnectivity(ConnectivityCheckInfo connectivityCheckInfo, |
||||
Set<ObjectId> haves, ProgressMonitor pm) |
||||
throws IOException; |
||||
|
||||
/** |
||||
* POJO which is used to pass all information which is needed to perform |
||||
* connectivity check. |
||||
*/ |
||||
public static class ConnectivityCheckInfo { |
||||
private Repository repository; |
||||
|
||||
private PackParser parser; |
||||
|
||||
private boolean checkObjects; |
||||
|
||||
private List<ReceiveCommand> commands; |
||||
|
||||
private RevWalk walk; |
||||
|
||||
/** |
||||
* @return database we write the stored objects into. |
||||
*/ |
||||
public Repository getRepository() { |
||||
return repository; |
||||
} |
||||
|
||||
/** |
||||
* @param repository |
||||
* set database we write the stored objects into. |
||||
*/ |
||||
public void setRepository(Repository repository) { |
||||
this.repository = repository; |
||||
} |
||||
|
||||
/** |
||||
* @return the parser used to parse pack. |
||||
*/ |
||||
public PackParser getParser() { |
||||
return parser; |
||||
} |
||||
|
||||
/** |
||||
* @param parser |
||||
* the parser to set |
||||
*/ |
||||
public void setParser(PackParser parser) { |
||||
this.parser = parser; |
||||
} |
||||
|
||||
/** |
||||
* @return if checker should check objects. |
||||
*/ |
||||
public boolean isCheckObjects() { |
||||
return checkObjects; |
||||
} |
||||
|
||||
/** |
||||
* @param checkObjects |
||||
* set if checker should check referenced objects outside of |
||||
* the received pack are reachable. |
||||
*/ |
||||
public void setCheckObjects(boolean checkObjects) { |
||||
this.checkObjects = checkObjects; |
||||
} |
||||
|
||||
/** |
||||
* @return command received by the current request. |
||||
*/ |
||||
public List<ReceiveCommand> getCommands() { |
||||
return commands; |
||||
} |
||||
|
||||
/** |
||||
* @param commands |
||||
* set command received by the current request. |
||||
*/ |
||||
public void setCommands(List<ReceiveCommand> commands) { |
||||
this.commands = commands; |
||||
} |
||||
|
||||
/** |
||||
* @param walk |
||||
* the walk to parse commits |
||||
*/ |
||||
public void setWalk(RevWalk walk) { |
||||
this.walk = walk; |
||||
} |
||||
|
||||
/** |
||||
* @return the walk to parse commits |
||||
*/ |
||||
public RevWalk getWalk() { |
||||
return walk; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,200 @@
|
||||
/* |
||||
* Copyright (c) 2019, Google LLC and others |
||||
* |
||||
* This program and the accompanying materials are made available under the |
||||
* terms of the Eclipse Distribution License v. 1.0 which is available at |
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
* |
||||
* SPDX-License-Identifier: BSD-3-Clause |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.transport.internal; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.Set; |
||||
|
||||
import org.eclipse.jgit.errors.MissingObjectException; |
||||
import org.eclipse.jgit.internal.JGitText; |
||||
import org.eclipse.jgit.lib.Constants; |
||||
import org.eclipse.jgit.lib.ObjectId; |
||||
import org.eclipse.jgit.lib.ObjectIdSubclassMap; |
||||
import org.eclipse.jgit.lib.ProgressMonitor; |
||||
import org.eclipse.jgit.revwalk.ObjectWalk; |
||||
import org.eclipse.jgit.revwalk.RevBlob; |
||||
import org.eclipse.jgit.revwalk.RevCommit; |
||||
import org.eclipse.jgit.revwalk.RevFlag; |
||||
import org.eclipse.jgit.revwalk.RevObject; |
||||
import org.eclipse.jgit.revwalk.RevSort; |
||||
import org.eclipse.jgit.revwalk.RevTree; |
||||
import org.eclipse.jgit.transport.ReceiveCommand; |
||||
import org.eclipse.jgit.transport.ReceiveCommand.Result; |
||||
|
||||
/** |
||||
* A connectivity checker that uses the entire reference database to perform |
||||
* reachability checks when checking the connectivity of objects. If |
||||
* info.isCheckObjects() is set it will also check that objects referenced by |
||||
* deltas are either provided or reachable as well. |
||||
*/ |
||||
public final class FullConnectivityChecker implements ConnectivityChecker { |
||||
@Override |
||||
public void checkConnectivity(ConnectivityCheckInfo connectivityCheckInfo, |
||||
Set<ObjectId> haves, ProgressMonitor pm) |
||||
throws MissingObjectException, IOException { |
||||
pm.beginTask(JGitText.get().countingObjects, |
||||
ProgressMonitor.UNKNOWN); |
||||
try (ObjectWalk ow = new ObjectWalk(connectivityCheckInfo.getRepository())) { |
||||
if (!markStartAndKnownNodes(connectivityCheckInfo, ow, haves, |
||||
pm)) { |
||||
return; |
||||
} |
||||
checkCommitTree(connectivityCheckInfo, ow, pm); |
||||
checkObjects(connectivityCheckInfo, ow, pm); |
||||
} finally { |
||||
pm.endTask(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @param connectivityCheckInfo |
||||
* Source for connectivity check. |
||||
* @param ow |
||||
* Walk which can also check blobs. |
||||
* @param haves |
||||
* Set of references known for client. |
||||
* @param pm |
||||
* Monitor to publish progress to. |
||||
* @return true if at least one new node was marked. |
||||
* @throws IOException |
||||
* an error occurred during connectivity checking. |
||||
*/ |
||||
private boolean markStartAndKnownNodes( |
||||
ConnectivityCheckInfo connectivityCheckInfo, |
||||
ObjectWalk ow, |
||||
Set<ObjectId> haves, ProgressMonitor pm) |
||||
throws IOException { |
||||
boolean markTrees = connectivityCheckInfo |
||||
.isCheckObjects() |
||||
&& !connectivityCheckInfo.getParser().getBaseObjectIds() |
||||
.isEmpty(); |
||||
if (connectivityCheckInfo.isCheckObjects()) { |
||||
ow.sort(RevSort.TOPO); |
||||
if (!connectivityCheckInfo.getParser().getBaseObjectIds() |
||||
.isEmpty()) { |
||||
ow.sort(RevSort.BOUNDARY, true); |
||||
} |
||||
} |
||||
boolean hasInteresting = false; |
||||
|
||||
for (ReceiveCommand cmd : connectivityCheckInfo.getCommands()) { |
||||
if (cmd.getResult() != Result.NOT_ATTEMPTED) { |
||||
continue; |
||||
} |
||||
if (cmd.getType() == ReceiveCommand.Type.DELETE) { |
||||
continue; |
||||
} |
||||
if (haves.contains(cmd.getNewId())) { |
||||
continue; |
||||
} |
||||
ow.markStart(ow.parseAny(cmd.getNewId())); |
||||
pm.update(1); |
||||
hasInteresting = true; |
||||
} |
||||
if (!hasInteresting) { |
||||
return false; |
||||
} |
||||
for (ObjectId have : haves) { |
||||
RevObject o = ow.parseAny(have); |
||||
ow.markUninteresting(o); |
||||
pm.update(1); |
||||
|
||||
if (markTrees) { |
||||
o = ow.peel(o); |
||||
if (o instanceof RevCommit) { |
||||
o = ((RevCommit) o).getTree(); |
||||
} |
||||
if (o instanceof RevTree) { |
||||
ow.markUninteresting(o); |
||||
} |
||||
} |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
/** |
||||
* @param connectivityCheckInfo |
||||
* Source for connectivity check. |
||||
* @param ow |
||||
* Walk which can also check blobs. |
||||
* @param pm |
||||
* Monitor to publish progress to. |
||||
* @throws IOException |
||||
* an error occurred during connectivity checking. |
||||
*/ |
||||
private void checkCommitTree(ConnectivityCheckInfo connectivityCheckInfo, |
||||
ObjectWalk ow, |
||||
ProgressMonitor pm) throws IOException { |
||||
RevCommit c; |
||||
ObjectIdSubclassMap<ObjectId> newObjectIds = connectivityCheckInfo |
||||
.getParser() |
||||
.getNewObjectIds(); |
||||
while ((c = ow.next()) != null) { |
||||
pm.update(1); |
||||
if (connectivityCheckInfo.isCheckObjects() |
||||
&& !c.has(RevFlag.UNINTERESTING) |
||||
&& !newObjectIds.contains(c)) { |
||||
throw new MissingObjectException(c, Constants.TYPE_COMMIT); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @param connectivityCheckInfo |
||||
* Source for connectivity check. |
||||
* @param ow |
||||
* Walk which can also check blobs. |
||||
* @param pm |
||||
* Monitor to publish progress to. |
||||
* @throws IOException |
||||
* an error occurred during connectivity checking. |
||||
* |
||||
*/ |
||||
private void checkObjects(ConnectivityCheckInfo connectivityCheckInfo, |
||||
ObjectWalk ow, |
||||
ProgressMonitor pm) throws IOException { |
||||
RevObject o; |
||||
ObjectIdSubclassMap<ObjectId> newObjectIds = connectivityCheckInfo |
||||
.getParser() |
||||
.getNewObjectIds(); |
||||
|
||||
while ((o = ow.nextObject()) != null) { |
||||
pm.update(1); |
||||
if (o.has(RevFlag.UNINTERESTING)) { |
||||
continue; |
||||
} |
||||
|
||||
if (connectivityCheckInfo.isCheckObjects()) { |
||||
if (newObjectIds.contains(o)) { |
||||
continue; |
||||
} |
||||
throw new MissingObjectException(o, o.getType()); |
||||
|
||||
} |
||||
|
||||
if (o instanceof RevBlob |
||||
&& !connectivityCheckInfo.getRepository().getObjectDatabase() |
||||
.has(o)) { |
||||
throw new MissingObjectException(o, Constants.TYPE_BLOB); |
||||
} |
||||
} |
||||
|
||||
if (connectivityCheckInfo.isCheckObjects()) { |
||||
for (ObjectId id : connectivityCheckInfo.getParser() |
||||
.getBaseObjectIds()) { |
||||
o = ow.parseAny(id); |
||||
if (!o.has(RevFlag.UNINTERESTING)) { |
||||
throw new MissingObjectException(o, o.getType()); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue