Java Code Examples for org.eclipse.jgit.api.errors.GitAPIException#getMessage()

The following examples show how to use org.eclipse.jgit.api.errors.GitAPIException#getMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JGitController.java    From spring-boot-demo with MIT License 6 votes vote down vote up
/**
 * 克隆
 *
 * @return
 */
@RequestMapping("/clone")
public String clone() {
    String result;
    try {
        Git.cloneRepository()
                .setURI("https://github.com/smltq/blog.git")
                .setDirectory(new File("/blog"))
                .call();
        result = "克隆成功了!";
    } catch (GitAPIException e) {
        result = e.getMessage();
        e.printStackTrace();
    }
    return result;
}
 
Example 2
Source File: GitGCSSyncApp.java    From policyscanner with Apache License 2.0 6 votes vote down vote up
/**
 * Handler for the GET request to this app.
 * @param req The request object.
 * @param resp The response object.
 * @throws IOException Thrown if there's an error reading from one of the APIs.
 */
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws IOException {
  PrintWriter out = resp.getWriter();
  this.bucket = System.getenv("POLICY_SCANNER_GIT_SYNC_DEST_BUCKET");
  String repository = System.getenv("POLICY_SCANNER_GIT_REPOSITORY_URL");
  String repoName = System.getenv("POLICY_SCANNER_GIT_REPOSITORY_NAME");
  File gitDir = new File(repoName);
  if (gitDir.exists()) {
    delete(gitDir);
  }
  try {
    Git.cloneRepository()
        .setURI(repository)
        .call();
  } catch (GitAPIException e) {
    throw new IOException(e.getMessage());
  }
  syncToGCS(repoName + "/.git/");
  out.write("Copied files to GCS!");
}
 
Example 3
Source File: CloneRepositoryAdapter.java    From coderadar with MIT License 6 votes vote down vote up
@Override
public void cloneRepository(CloneRepositoryCommand cloneRepositoryCommand)
    throws UnableToCloneRepositoryException {
  try {
    // TODO: support progress monitoring
    CloneCommand cloneCommand =
        Git.cloneRepository()
            .setURI(cloneRepositoryCommand.getRemoteUrl())
            .setDirectory(new File(cloneRepositoryCommand.getLocalDir()))
            .setBare(true);
    if (cloneRepositoryCommand.getUsername() != null
        && cloneRepositoryCommand.getPassword() != null) {
      cloneCommand.setCredentialsProvider(
          new UsernamePasswordCredentialsProvider(
              cloneRepositoryCommand.getUsername(), cloneRepositoryCommand.getPassword()));
    }
    cloneCommand.call().close();
  } catch (GitAPIException e) {
    throw new UnableToCloneRepositoryException(e.getMessage());
  }
}
 
Example 4
Source File: GitMapSourceFactory.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Iterator<? extends MapSource> loadNewSources() throws MapMissingException {
  try {
    git.pull()
        .setCredentialsProvider(credentials)
        .setFastForward(MergeCommand.FastForwardMode.FF)
        .call();
  } catch (GitAPIException e) {
    throw new MapMissingException(
        git.getRepository().getDirectory().getPath(), e.getMessage(), e.getCause());
  }

  return Iterators.emptyIterator();
}
 
Example 5
Source File: GitUtils.java    From gitPic with MIT License 5 votes vote down vote up
/**
 * git push
 *
 * @param repository
 */
public static void push(Repository repository) {
    Git git = new Git(repository);
    try {
        Iterable<PushResult> results = git.push().call();
        PushResult result = results.iterator().next();
        validPushResult(result);
    } catch (GitAPIException e) {
        logger.error(e.getMessage(), e);
        throw new TipException("git push 异常, message:" + e.getMessage());
    }
}
 
Example 6
Source File: GitUtils.java    From blueocean-plugin with MIT License 5 votes vote down vote up
/**
 *  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(true) // we only want to test
            .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);
    }
}
 
Example 7
Source File: GitUtils.java    From blueocean-plugin with MIT License 5 votes vote down vote up
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);
    }
}
 
Example 8
Source File: GitContentRepository.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public byte[] removeContentFromExploded(byte[] deploymentHash, List<String> paths) throws ExplodedContentException {
    byte[] result = super.removeContentFromExploded(deploymentHash, paths);
    if (!Arrays.equals(deploymentHash, result)) {
        final Path realFile = getDeploymentContentFile(result, true);
        try (Git git = gitRepository.getGit()) {
            git.add().addFilepattern(gitRepository.getPattern(realFile)).call();
        } catch (GitAPIException ex) {
            throw new ExplodedContentException(ex.getMessage(), ex);
        }
    }
    return result;
}
 
Example 9
Source File: GitContentRepository.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public byte[] addContentToExploded(byte[] deploymentHash, List<ExplodedContent> addFiles, boolean overwrite) throws ExplodedContentException {
    byte[] result = super.addContentToExploded(deploymentHash, addFiles, overwrite);
    if (!Arrays.equals(deploymentHash, result)) {
        final Path realFile = getDeploymentContentFile(result, true);
        try (Git git = gitRepository.getGit()) {
            git.add().addFilepattern(gitRepository.getPattern(realFile)).call();
        } catch (GitAPIException ex) {
            throw new ExplodedContentException(ex.getMessage(), ex);
        }
    }
    return result;
}
 
Example 10
Source File: GitContentRepository.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public byte[] explodeSubContent(byte[] deploymentHash, String relativePath) throws ExplodedContentException {
    byte[] result = super.explodeSubContent(deploymentHash, relativePath);
    if (!Arrays.equals(deploymentHash, result)) {
        final Path realFile = getDeploymentContentFile(result, true);
        try (Git git = gitRepository.getGit()) {
            git.add().addFilepattern(gitRepository.getPattern(realFile)).call();
        } catch (GitAPIException ex) {
            throw new ExplodedContentException(ex.getMessage(), ex);
        }
    }
    return result;
}
 
Example 11
Source File: GitContentRepository.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public byte[] explodeContent(byte[] deploymentHash) throws ExplodedContentException {
    byte[] result = super.explodeContent(deploymentHash);
    if (!Arrays.equals(deploymentHash, result)) {
        final Path realFile = getDeploymentContentFile(result, true);
        try (Git git = gitRepository.getGit()) {
            git.add().addFilepattern(gitRepository.getPattern(realFile)).call();
        } catch (GitAPIException ex) {
            throw new ExplodedContentException(ex.getMessage(), ex);
        }
    }
    return result;
}