Search in sources :

Example 56 with GitAPIException

use of org.eclipse.jgit.api.errors.GitAPIException in project gradle by gradle.

the class GitVersionControlSystem method cloneRepo.

private static void cloneRepo(File workingDir, GitVersionControlSpec gitSpec, VersionRef ref) {
    CloneCommand clone = Git.cloneRepository().setURI(gitSpec.getUrl().toString()).setDirectory(workingDir).setCloneSubmodules(true);
    Git git = null;
    try {
        git = clone.call();
        git.reset().setMode(ResetCommand.ResetType.HARD).setRef(ref.getCanonicalId()).call();
    } catch (GitAPIException e) {
        throw wrapGitCommandException("clone", gitSpec.getUrl(), workingDir, e);
    } catch (JGitInternalException e) {
        throw wrapGitCommandException("clone", gitSpec.getUrl(), workingDir, e);
    } finally {
        if (git != null) {
            git.close();
        }
    }
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException)

Example 57 with GitAPIException

use of org.eclipse.jgit.api.errors.GitAPIException in project gocd by gocd.

the class ConfigRepository method findDiffBetweenTwoRevisions.

String findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit) throws GitAPIException {
    if (laterCommit == null || earlierCommit == null) {
        return null;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    String output = null;
    try {
        DiffFormatter diffFormatter = new DiffFormatter(out);
        diffFormatter.setRepository(gitRepo);
        diffFormatter.format(earlierCommit.getId(), laterCommit.getId());
        output = out.toString();
        output = StringUtil.stripTillLastOccurrenceOf(output, "+++ b/cruise-config.xml");
    } catch (IOException e) {
        throw new RuntimeException("Error occurred during diff computation. Message: " + e.getMessage());
    } finally {
        try {
            out.close();
        } catch (Exception e) {
        }
    }
    return output;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) ConfigFileHasChangedException(com.thoughtworks.go.config.exceptions.ConfigFileHasChangedException) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) NoHeadException(org.eclipse.jgit.api.errors.NoHeadException) NullArgumentException(org.apache.commons.lang.NullArgumentException) ConfigMergeException(com.thoughtworks.go.config.exceptions.ConfigMergeException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException)

Example 58 with GitAPIException

use of org.eclipse.jgit.api.errors.GitAPIException in project gocd by gocd.

the class CachedGoConfigIntegrationTest method shouldErrorOutOnUpdateConfigWithValidPartials_WithMainConfigBreakingPartials.

@Test
public void shouldErrorOutOnUpdateConfigWithValidPartials_WithMainConfigBreakingPartials() throws GitAPIException, IOException {
    setupExternalConfigRepoWithDependencyMaterialOnPipelineInMainXml("upstream", "downstream");
    String gitShaBeforeSave = configRepository.getCurrentRevCommit().getName();
    CruiseConfig originalConfig = cachedGoConfig.loadForEditing();
    CruiseConfig editedConfig = new Cloner().deepClone(originalConfig);
    editedConfig.getGroups().remove(editedConfig.findGroup("default"));
    try {
        cachedGoConfig.writeFullConfigWithLock(new FullConfigUpdateCommand(editedConfig, goConfigService.configFileMd5()));
        fail("Expected the test to fail");
    } catch (Exception e) {
        String gitShaAfterSave = configRepository.getCurrentRevCommit().getName();
        String configXmlFromConfigFolder = FileUtils.readFileToString(new File(goConfigDao.fileLocation()), UTF_8);
        assertThat(cachedGoConfig.loadForEditing(), is(originalConfig));
        assertEquals(gitShaBeforeSave, gitShaAfterSave);
        assertThat(cachedGoConfig.loadForEditing().getMd5(), is(configRepository.getCurrentRevision().getMd5()));
        assertThat(cachedGoConfig.currentConfig().getMd5(), is(configRepository.getCurrentRevision().getMd5()));
        assertThat(configXmlFromConfigFolder, is(configRepository.getCurrentRevision().getContent()));
        RepoConfigOrigin origin = (RepoConfigOrigin) cachedGoPartials.lastValidPartials().get(0).getOrigin();
        assertThat(origin.getRevision(), is("r1"));
    }
}
Also used : FullConfigUpdateCommand(com.thoughtworks.go.config.update.FullConfigUpdateCommand) RepoConfigOrigin(com.thoughtworks.go.config.remote.RepoConfigOrigin) StringContains.containsString(org.hamcrest.core.StringContains.containsString) File(java.io.File) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) Cloner(com.rits.cloning.Cloner)

Example 59 with GitAPIException

use of org.eclipse.jgit.api.errors.GitAPIException in project blueocean-plugin by jenkinsci.

the class GitUtils method validatePushAccess.

/**
 *  Attempts to push to a non-existent branch to validate the user actually has push access
 *
 * @param repo local repository
 * @param remoteUrl git repo url
 * @param credential credential to use when accessing git
 */
public static void validatePushAccess(@Nonnull Repository repo, @Nonnull String remoteUrl, @Nullable StandardCredentials credential) throws GitException {
    try (org.eclipse.jgit.api.Git git = new org.eclipse.jgit.api.Git(repo)) {
        // we need to perform an actual push, so we try a deletion of a very-unlikely-to-exist branch
        // which needs to have push permissions in order to get a 'branch not found' message
        String pushSpec = ":this-branch-is-only-to-test-if-jenkins-has-push-access";
        PushCommand pushCommand = git.push();
        addCredential(repo, pushCommand, credential);
        Iterable<PushResult> resultIterable = pushCommand.setRefSpecs(new RefSpec(pushSpec)).setRemote(remoteUrl).setDryRun(// we only want to test
        true).call();
        PushResult result = resultIterable.iterator().next();
        if (result.getRemoteUpdates().isEmpty()) {
            System.out.println("No remote updates occurred");
        } else {
            for (RemoteRefUpdate update : result.getRemoteUpdates()) {
                if (!RemoteRefUpdate.Status.NON_EXISTING.equals(update.getStatus()) && !RemoteRefUpdate.Status.OK.equals(update.getStatus())) {
                    throw new ServiceException.UnexpectedErrorException("Expected non-existent ref but got: " + update.getStatus().name() + ": " + update.getMessage());
                }
            }
        }
    } catch (GitAPIException e) {
        if (e.getMessage().toLowerCase().contains("auth")) {
            throw new ServiceException.UnauthorizedException(e.getMessage(), e);
        }
        throw new ServiceException.UnexpectedErrorException("Unable to access and push to: " + remoteUrl + " - " + e.getMessage(), e);
    }
}
Also used : RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) PushResult(org.eclipse.jgit.transport.PushResult) PushCommand(org.eclipse.jgit.api.PushCommand) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.jenkinsci.plugins.gitclient.Git) RefSpec(org.eclipse.jgit.transport.RefSpec) ServiceException(io.jenkins.blueocean.commons.ServiceException)

Example 60 with GitAPIException

use of org.eclipse.jgit.api.errors.GitAPIException in project blueocean-plugin by jenkinsci.

the class GitUtils method push.

public static void push(String remoteUrl, Repository repo, StandardCredentials credential, String localBranchRef, String remoteBranchRef) {
    try (org.eclipse.jgit.api.Git git = new org.eclipse.jgit.api.Git(repo)) {
        String pushSpec = "+" + localBranchRef + ":" + remoteBranchRef;
        PushCommand pushCommand = git.push();
        addCredential(repo, pushCommand, credential);
        Iterable<PushResult> resultIterable = pushCommand.setRefSpecs(new RefSpec(pushSpec)).setRemote(remoteUrl).call();
        PushResult result = resultIterable.iterator().next();
        if (result.getRemoteUpdates().isEmpty()) {
            throw new RuntimeException("No remote updates occurred");
        } else {
            for (RemoteRefUpdate update : result.getRemoteUpdates()) {
                if (!RemoteRefUpdate.Status.OK.equals(update.getStatus())) {
                    throw new ServiceException.UnexpectedErrorException("Remote update failed: " + update.getStatus().name() + ": " + update.getMessage());
                }
            }
        }
    } catch (GitAPIException e) {
        if (e.getMessage().toLowerCase().contains("auth")) {
            throw new ServiceException.UnauthorizedException(e.getMessage(), e);
        }
        throw new ServiceException.UnexpectedErrorException("Unable to save and push to: " + remoteUrl + " - " + e.getMessage(), e);
    }
}
Also used : RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) PushResult(org.eclipse.jgit.transport.PushResult) PushCommand(org.eclipse.jgit.api.PushCommand) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.jenkinsci.plugins.gitclient.Git) RefSpec(org.eclipse.jgit.transport.RefSpec) ServiceException(io.jenkins.blueocean.commons.ServiceException)

Aggregations

GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)60 IOException (java.io.IOException)25 Git (org.eclipse.jgit.api.Git)22 GitException (org.eclipse.che.api.git.exception.GitException)18 File (java.io.File)16 Ref (org.eclipse.jgit.lib.Ref)13 RevCommit (org.eclipse.jgit.revwalk.RevCommit)12 ArrayList (java.util.ArrayList)11 ObjectId (org.eclipse.jgit.lib.ObjectId)11 RefSpec (org.eclipse.jgit.transport.RefSpec)11 Repository (org.eclipse.jgit.lib.Repository)9 URISyntaxException (java.net.URISyntaxException)8 PushResult (org.eclipse.jgit.transport.PushResult)8 RemoteRefUpdate (org.eclipse.jgit.transport.RemoteRefUpdate)8 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)8 PushCommand (org.eclipse.jgit.api.PushCommand)7 CheckoutCommand (org.eclipse.jgit.api.CheckoutCommand)6 FetchCommand (org.eclipse.jgit.api.FetchCommand)6 HashMap (java.util.HashMap)5 DiffCommitFile (org.eclipse.che.api.git.shared.DiffCommitFile)5