Search in sources :

Example 1 with RemoteAddCommand

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);
}
Also used : URIish(org.eclipse.jgit.transport.URIish) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) IOException(java.io.IOException)

Example 2 with RemoteAddCommand

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);
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) User(hudson.model.User) RemoteRemoveCommand(org.eclipse.jgit.api.RemoteRemoveCommand) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) URISyntaxException(java.net.URISyntaxException) BasicSSHUserPrivateKey(com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) ServiceException(io.jenkins.blueocean.commons.ServiceException) FetchCommand(org.eclipse.jgit.api.FetchCommand) File(java.io.File) Nonnull(javax.annotation.Nonnull)

Example 3 with RemoteAddCommand

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;
}
Also used : URIish(org.eclipse.jgit.transport.URIish) PersonIdent(org.eclipse.jgit.lib.PersonIdent) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) MergeResult(org.eclipse.jgit.api.MergeResult) RevWalk(org.eclipse.jgit.revwalk.RevWalk) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) PatchApplyException(org.eclipse.jgit.api.errors.PatchApplyException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) PatchFormatException(org.eclipse.jgit.api.errors.PatchFormatException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 4 with RemoteAddCommand

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);
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) URISyntaxException(java.net.URISyntaxException)

Example 5 with RemoteAddCommand

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();
}
Also used : URIish(org.eclipse.jgit.transport.URIish) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) List(java.util.List) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) File(java.io.File) Test(org.junit.Test)

Aggregations

RemoteAddCommand (org.eclipse.jgit.api.RemoteAddCommand)7 URIish (org.eclipse.jgit.transport.URIish)7 IOException (java.io.IOException)4 Git (org.eclipse.jgit.api.Git)4 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)4 File (java.io.File)3 URISyntaxException (java.net.URISyntaxException)3 RemoteConfig (org.eclipse.jgit.transport.RemoteConfig)2 BasicSSHUserPrivateKey (com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey)1 User (hudson.model.User)1 ServiceException (io.jenkins.blueocean.commons.ServiceException)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 List (java.util.List)1 Nonnull (javax.annotation.Nonnull)1 CheckoutCommand (org.eclipse.jgit.api.CheckoutCommand)1 FetchCommand (org.eclipse.jgit.api.FetchCommand)1 InitCommand (org.eclipse.jgit.api.InitCommand)1 MergeResult (org.eclipse.jgit.api.MergeResult)1 RemoteRemoveCommand (org.eclipse.jgit.api.RemoteRemoveCommand)1