use of org.eclipse.jgit.api.RemoteRemoveCommand in project blueocean-plugin by jenkinsci.
the class GitCacheCloneReadSaveRequest method getActiveRepository.
@Nonnull
private Git getActiveRepository(Repository repository) throws IOException {
try {
// Clone the bare repository
File cloneDir = File.createTempFile("clone", "");
if (cloneDir.exists()) {
if (cloneDir.isDirectory()) {
FileUtils.deleteDirectory(cloneDir);
} else {
if (!cloneDir.delete()) {
throw new ServiceException.UnexpectedErrorException("Unable to delete repository clone");
}
}
}
if (!cloneDir.mkdirs()) {
throw new ServiceException.UnexpectedErrorException("Unable to create repository clone directory");
}
String url = repository.getConfig().getString("remote", "origin", "url");
Git gitClient = Git.cloneRepository().setCloneAllBranches(false).setProgressMonitor(new CloneProgressMonitor(url)).setURI(repository.getDirectory().getCanonicalPath()).setDirectory(cloneDir).call();
RemoteRemoveCommand remove = gitClient.remoteRemove();
remove.setName("origin");
remove.call();
RemoteAddCommand add = gitClient.remoteAdd();
add.setName("origin");
add.setUri(new URIish(gitSource.getRemote()));
add.call();
if (GitUtils.isSshUrl(gitSource.getRemote())) {
// Get committer info and credentials
User user = User.current();
if (user == null) {
throw new ServiceException.UnauthorizedException("Not authenticated");
}
BasicSSHUserPrivateKey privateKey = UserSSHKeyManager.getOrCreate(user);
// Make sure up-to-date and credentials work
GitUtils.fetch(repository, privateKey);
} else {
FetchCommand fetch = gitClient.fetch();
fetch.call();
}
if (!StringUtils.isEmpty(sourceBranch) && !sourceBranch.equals(branch)) {
CheckoutCommand checkout = gitClient.checkout();
checkout.setStartPoint("origin/" + sourceBranch);
checkout.setName(sourceBranch);
// to create a new local branch
checkout.setCreateBranch(true);
checkout.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.NOTRACK);
checkout.call();
checkout = gitClient.checkout();
checkout.setName(branch);
// this *should* be a new branch
checkout.setCreateBranch(true);
checkout.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.NOTRACK);
checkout.call();
} else {
CheckoutCommand checkout = gitClient.checkout();
checkout.setStartPoint("origin/" + branch);
checkout.setName(branch);
// to create a new local branch
checkout.setCreateBranch(true);
checkout.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.NOTRACK);
checkout.call();
}
return gitClient;
} catch (GitAPIException | URISyntaxException ex) {
throw new ServiceException.UnexpectedErrorException("Unable to get working repository directory", ex);
}
}
Aggregations