use of org.eclipse.jgit.api.RemoteAddCommand in project camel by apache.
the class GitProducer method doRemoteAdd.
protected void doRemoteAdd(Exchange exchange, String operation) throws Exception {
if (ObjectHelper.isEmpty(endpoint.getRemoteName())) {
throw new IllegalArgumentException("Remote Name must be specified to execute " + operation);
}
if (ObjectHelper.isEmpty(endpoint.getRemotePath())) {
throw new IllegalArgumentException("Remote Path must be specified to execute " + operation);
}
RemoteConfig result = null;
try {
RemoteAddCommand remoteAddCommand = git.remoteAdd();
remoteAddCommand.setUri(new URIish(endpoint.getRemotePath()));
remoteAddCommand.setName(endpoint.getRemoteName());
result = remoteAddCommand.call();
} catch (Exception e) {
LOG.error("There was an error in Git " + operation + " operation");
throw e;
}
updateExchange(exchange, result);
}
use of org.eclipse.jgit.api.RemoteAddCommand 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);
}
}
use of org.eclipse.jgit.api.RemoteAddCommand in project repairnator by Spirals-Team.
the class GitHelper method mergeTwoCommitsForPR.
public boolean mergeTwoCommitsForPR(Git git, Build build, PRInformation prInformation, String repository, AbstractStep step, List<String> pathes) {
try {
String remoteBranchPath = CloneRepository.GITHUB_ROOT_REPO + prInformation.getOtherRepo().getSlug() + ".git";
RemoteAddCommand remoteBranchCommand = git.remoteAdd();
remoteBranchCommand.setName("PR");
remoteBranchCommand.setUri(new URIish(remoteBranchPath));
remoteBranchCommand.call();
git.fetch().setRemote("PR").call();
String commitHeadSha = this.testCommitExistence(git, prInformation.getHead().getSha(), step, build);
String commitBaseSha = this.testCommitExistence(git, prInformation.getBase().getSha(), step, build);
if (commitHeadSha == null) {
step.addStepError("Commit head ref cannot be retrieved in the repository: " + prInformation.getHead().getSha() + ". Operation aborted.");
this.getLogger().debug("Step " + step.getName() + " - " + prInformation.getHead().toString());
return false;
}
if (commitBaseSha == null) {
step.addStepError("Commit base ref cannot be retrieved in the repository: " + prInformation.getBase().getSha() + ". Operation aborted.");
this.getLogger().debug("Step " + step.getName() + " - " + prInformation.getBase().toString());
return false;
}
this.getLogger().debug("Step " + step.getName() + " - Get the commit " + commitHeadSha + " for repo " + repository);
if (pathes != null) {
git.checkout().setName(commitHeadSha).addPaths(pathes).call();
PersonIdent personIdent = new PersonIdent("Luc Esape", "luc.esape@gmail.com");
git.commit().setMessage("Undo changes on source code").setAuthor(personIdent).setCommitter(personIdent).call();
} else {
git.checkout().setName(commitHeadSha).call();
}
RevWalk revwalk = new RevWalk(git.getRepository());
RevCommit revCommitBase = revwalk.lookupCommit(git.getRepository().resolve(commitBaseSha));
this.getLogger().debug("Step " + step.getName() + " - Do the merge with the PR commit for repo " + repository);
MergeResult result = git.merge().include(revCommitBase).setFastForward(MergeCommand.FastForwardMode.NO_FF).call();
this.nbCommits++;
} catch (Exception e) {
step.addStepError(e.getMessage());
this.getLogger().error("Step " + step.getName() + " - Repository " + repository + " cannot be cloned.", e);
return false;
}
return true;
}
use of org.eclipse.jgit.api.RemoteAddCommand in project zeppelin by apache.
the class GitHubNotebookRepo method configureRemoteStream.
private void configureRemoteStream() {
try {
LOG.debug("Setting up remote stream");
RemoteAddCommand remoteAddCommand = git.remoteAdd();
remoteAddCommand.setName(zeppelinConfiguration.getZeppelinNotebookGitRemoteOrigin());
remoteAddCommand.setUri(new URIish(zeppelinConfiguration.getZeppelinNotebookGitURL()));
remoteAddCommand.call();
} catch (GitAPIException e) {
LOG.error("Error configuring GitHub", e);
} catch (URISyntaxException e) {
LOG.error("Error in GitHub URL provided", e);
}
}
use of org.eclipse.jgit.api.RemoteAddCommand in project camel by apache.
the class GitProducerTest method remoteListTest.
@Test
public void remoteListTest() throws Exception {
Repository repository = getTestRepository();
File gitDir = new File(gitLocalRepo, ".git");
assertEquals(gitDir.exists(), true);
Git git = new Git(repository);
RemoteAddCommand remoteAddCommand = git.remoteAdd();
remoteAddCommand.setName("origin");
remoteAddCommand.setUri(new URIish(remoteUriTest));
remoteAddCommand.call();
List<RemoteConfig> gitRemoteConfigs = git.remoteList().call();
Object result = template.requestBody("direct:remoteList", "");
assertTrue(result instanceof List);
List<RemoteConfig> remoteConfigs = (List<RemoteConfig>) result;
assertEquals(gitRemoteConfigs.size(), remoteConfigs.size());
assertEquals(gitRemoteConfigs.get(0).getName(), remoteConfigs.get(0).getName());
assertEquals(gitRemoteConfigs.get(0).getURIs(), remoteConfigs.get(0).getURIs());
git.close();
}
Aggregations