Optional<IRemoteRepo> remoteRepo = getRemoteRepo(remote, repository()
.deduplicationService());
Preconditions.checkState(remoteRepo.isPresent(), "Failed to connect to the remote.");
IRemoteRepo remoteRepoInstance = remoteRepo.get();
try {
remoteRepoInstance.open();
} catch (IOException e) {
Throwables.propagate(e);
}
try {
int refCount = 0;
for (ChangedRef ref : needUpdate) {
if (ref.getType() != ChangeTypes.REMOVED_REF) {
refCount++;
Optional<Integer> newFetchLimit = depth;
// If we haven't specified a depth, but this is a shallow repository, set
// the
// fetch limit to the current repository depth.
if (!newFetchLimit.isPresent() && repoDepth.isPresent()
&& ref.getType() == ChangeTypes.ADDED_REF) {
newFetchLimit = repoDepth;
}
// Fetch updated data from this ref
Ref newRef = ref.getNewRef();
remoteRepoInstance.fetchNewData(newRef, newFetchLimit, progressListener);
if (repoDepth.isPresent() && !fullDepth) {
// Update the repository depth if it is deeper than before.
int newDepth;
try {
newDepth = repository().graphDatabase().getDepth(
newRef.getObjectId());
} catch (IllegalStateException e) {
throw new RuntimeException(ref.toString(), e);
}
if (newDepth > repoDepth.get()) {
command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET)
.setScope(ConfigScope.LOCAL)
.setName(Repository.DEPTH_CONFIG_KEY)
.setValue(Integer.toString(newDepth)).call();
repoDepth = Optional.of(newDepth);
}
}
// Update the ref
Ref updatedRef = updateLocalRef(newRef, remote, localRemoteRefs);
ref.setNewRef(updatedRef);
}
}
if (needUpdate.size() > 0) {
result.addAll(remote.getFetchURL(), needUpdate);
}
// Update HEAD ref
if (!remote.getMapped()) {
Ref remoteHead = remoteRepoInstance.headRef();
if (remoteHead != null) {
updateLocalRef(remoteHead, remote, localRemoteRefs);
}
}
} finally {
try {
remoteRepoInstance.close();
} catch (IOException e) {
Throwables.propagate(e);
}
}
}