ProgressListener progressListener = getProgressListener();
progressListener.started();
// Set up origin
Remote remote = command(RemoteAddOp.class).setName("origin").setURL(repositoryURL)
.setMapped(repository.isSparse()).setUserName(username).setPassword(password)
.setBranch(repository.isSparse() ? branch.get() : null).call();
if (!depth.isPresent()) {
// See if we are cloning a shallow clone. If so, a depth must be specified.
Optional<IRemoteRepo> remoteRepo = RemoteUtils.newRemote(
GlobalContextBuilder.builder.build(Hints.readOnly()), remote, repository,
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 {
depth = remoteRepoInstance.getDepth();
} finally {
try {
remoteRepoInstance.close();
} catch (IOException e) {
Throwables.propagate(e);
}
}
}
if (depth.isPresent()) {
command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL)
.setName(Repository.DEPTH_CONFIG_KEY).setValue(depth.get().toString()).call();
}
// Fetch remote data
command(FetchOp.class).setDepth(depth.or(0)).setProgressListener(progressListener).call();
// Set up remote tracking branches
final ImmutableSet<Ref> remoteRefs = command(LsRemote.class).retrieveTags(false)
.setRemote(Suppliers.ofInstance(Optional.of(remote))).call();
boolean emptyRepo = true;
for (Ref remoteRef : remoteRefs) {
if (emptyRepo && !remoteRef.getObjectId().isNull()) {
emptyRepo = false;
}
String branchName = remoteRef.localName();
if (remoteRef instanceof SymRef) {
continue;
}
if (!command(RefParse.class).setName(Ref.HEADS_PREFIX + branchName).call().isPresent()) {
command(BranchCreateOp.class).setName(branchName)
.setSource(remoteRef.getObjectId().toString()).call();
} else {
command(UpdateRef.class).setName(Ref.HEADS_PREFIX + branchName)
.setNewValue(remoteRef.getObjectId()).call();
}
command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL)
.setName("branches." + branchName + ".remote").setValue(remote.getName())
.call();
command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET).setScope(ConfigScope.LOCAL)
.setName("branches." + branchName + ".merge")
.setValue(Ref.HEADS_PREFIX + remoteRef.localName()).call();
}
if (!emptyRepo) {
// checkout branch
if (branch.isPresent()) {
command(CheckoutOp.class).setForce(true).setSource(branch.get()).call();
} else {
// checkout the head
final Optional<Ref> currRemoteHead = command(RefParse.class).setName(
Ref.REMOTES_PREFIX + remote.getName() + "/" + Ref.HEAD).call();
if (currRemoteHead.isPresent() && currRemoteHead.get() instanceof SymRef) {
final SymRef remoteHeadRef = (SymRef) currRemoteHead.get();
final String currentBranch = Ref.localName(remoteHeadRef.getTarget());
command(CheckoutOp.class).setForce(true).setSource(currentBranch).call();
} else {