use of org.eclipse.jgit.lib.Constants.R_REMOTES in project n4js by eclipse.
the class GitUtils method clone.
/**
* Clones a branch of a remote Git repository to the local file system.
*
* @param remoteUri
* the remote Git repository URI as String.
* @param localClonePath
* the local file system path.
* @param branch
* the name of the branch to be cloned.
*/
private static void clone(final String remoteUri, final Path localClonePath, final String branch) {
checkNotNull(remoteUri, "remoteUri");
checkNotNull(localClonePath, "clonePath");
checkNotNull(branch, "branch");
final File destinationFolder = localClonePath.toFile();
if (!destinationFolder.exists()) {
try {
createDirectories(localClonePath);
} catch (final IOException e) {
final String message = "Error while creating directory for local repository under " + localClonePath + ".";
LOGGER.error(message, e);
throw new RuntimeException(message, e);
}
LOGGER.info("Creating folder for repository at '" + localClonePath + "'.");
}
checkState(destinationFolder.exists(), "Repository folder does not exist folder does not exist: " + destinationFolder.getAbsolutePath());
final File[] existingFiles = destinationFolder.listFiles();
if (!isEmpty(existingFiles)) {
try (final Git git = open(localClonePath.toFile())) {
LOGGER.info("Repository already exists. Aborting clone phase. Files in " + destinationFolder + " are: " + Joiner.on(',').join(existingFiles));
final List<URIish> originUris = getOriginUris(git);
if (!hasRemote(originUris, remoteUri)) {
LOGGER.info("Desired remote URI differs from the current one. Desired: '" + remoteUri + "' origin URIs: '" + Joiner.on(',').join(originUris) + "'.");
LOGGER.info("Cleaning up current git clone and running clone phase from scratch.");
deleteRecursively(destinationFolder);
clone(remoteUri, localClonePath, branch);
} else {
final String currentBranch = git.getRepository().getBranch();
if (!currentBranch.equals(branch)) {
LOGGER.info("Desired branch differs from the current one. Desired: '" + branch + "' current: '" + currentBranch + "'.");
git.pull().setProgressMonitor(createMonitor()).call();
// check if the remote desired branch exists or not.
final Ref remoteBranchRef = from(git.branchList().setListMode(REMOTE).call()).firstMatch(ref -> ref.getName().equals(R_REMOTES + ORIGIN + "/" + branch)).orNull();
// we have to delete the repository content and run a full clone from scratch.
if (null == remoteBranchRef) {
LOGGER.info("Cleaning up current git clone and running clone phase from scratch.");
deleteRecursively(destinationFolder);
clone(remoteUri, localClonePath, currentBranch);
} else {
git.pull().setProgressMonitor(createMonitor()).call();
LOGGER.info("Pulled from upstream.");
}
}
}
return;
} catch (final Exception e) {
final String msg = "Error when performing git pull in " + localClonePath + " from " + remoteUri + ".";
LOGGER.error(msg, e);
throw new RuntimeException("Error when performing git pull in " + localClonePath + " from " + remoteUri + ".", e);
}
}
LOGGER.info("Cloning repository from '" + remoteUri + "'...");
final CloneCommand cloneCommand = cloneRepository().setURI(remoteUri).setDirectory(destinationFolder).setBranch(branch).setProgressMonitor(createMonitor()).setTransportConfigCallback(TRANSPORT_CALLBACK);
try (final Git git = cloneCommand.call()) {
LOGGER.info("Repository content has been successfully cloned to '" + git.getRepository().getDirectory() + "'.");
} catch (final GitAPIException e) {
final String message = "Error while cloning repository.";
LOGGER.error(message, e);
LOGGER.info("Trying to clean up local repository content: " + destinationFolder + ".");
deleteRecursively(destinationFolder);
LOGGER.info("Inconsistent checkout directory was successfully cleaned up.");
throw new RuntimeException(message, e);
}
}
Aggregations