Search in sources :

Example 6 with JGitInternalException

use of org.eclipse.jgit.api.errors.JGitInternalException in project bndtools by bndtools.

the class GitCloneTemplate method generateOutputs.

@Override
public ResourceMap generateOutputs(Map<String, List<Object>> parameters, IProgressMonitor monitor) throws Exception {
    File workingDir = null;
    File gitDir = null;
    // Get existing checkout if available
    synchronized (this) {
        if (checkedOut != null) {
            workingDir = checkedOut.getWorkTree();
            gitDir = new File(workingDir, ".git");
        }
    }
    if (workingDir == null) {
        // Need to do a new checkout
        workingDir = Files.createTempDirectory("checkout").toFile();
        gitDir = new File(workingDir, ".git");
        try {
            CloneCommand cloneCmd = Git.cloneRepository().setURI(params.cloneUrl).setDirectory(workingDir).setNoCheckout(true);
            cloneCmd.setProgressMonitor(new EclipseGitProgressTransformer(monitor));
            Git git = cloneCmd.call();
            CheckoutCommand checkout = git.checkout().setCreateBranch(true).setName("_tmp");
            if (params.branch == null) {
                checkout.setStartPoint(GitCloneTemplateParams.DEFAULT_BRANCH);
            } else {
                String startPoint = null;
                if (params.branch.startsWith(Constants.DEFAULT_REMOTE_NAME + "/")) {
                    startPoint = params.branch;
                } else {
                    // Check for a matching tag
                    for (Ref ref : git.tagList().call()) {
                        if (ref.getName().endsWith("/" + params.branch)) {
                            startPoint = params.branch;
                            break;
                        }
                    }
                    if (startPoint == null) {
                        // Check remote branches
                        for (Ref ref : git.branchList().setListMode(ListMode.REMOTE).call()) {
                            if (ref.getName().endsWith("/" + params.branch)) {
                                startPoint = Constants.DEFAULT_REMOTE_NAME + "/" + params.branch;
                                break;
                            }
                        }
                        if (startPoint == null) {
                            if (SHA1_PATTERN.matcher(params.branch).matches()) {
                                startPoint = params.branch;
                                if (startPoint == null) {
                                    throw new Exception("Unable to find requested ref \"" + params.branch + "\"");
                                }
                            }
                        }
                    }
                }
                checkout.setStartPoint(startPoint);
            }
            checkout.call();
            checkedOut = git.getRepository();
        } catch (JGitInternalException e) {
            Throwable cause = e.getCause();
            if (cause instanceof Exception)
                throw (Exception) cause;
            throw e;
        }
    }
    final File exclude = gitDir;
    FileFilter filter = new FileFilter() {

        @Override
        public boolean accept(File path) {
            return !path.equals(exclude);
        }
    };
    return toResourceMap(workingDir, filter);
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) FileFilter(java.io.FileFilter) File(java.io.File) IOException(java.io.IOException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException)

Aggregations

JGitInternalException (org.eclipse.jgit.api.errors.JGitInternalException)6 IOException (java.io.IOException)3 Git (org.eclipse.jgit.api.Git)3 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)3 File (java.io.File)2 CheckoutCommand (org.eclipse.jgit.api.CheckoutCommand)2 ConcurrentRefUpdateException (org.eclipse.jgit.api.errors.ConcurrentRefUpdateException)2 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)2 ObjectId (org.eclipse.jgit.lib.ObjectId)2 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)2 PersonIdent (org.eclipse.jgit.lib.PersonIdent)2 RefUpdate (org.eclipse.jgit.lib.RefUpdate)2 Result (org.eclipse.jgit.lib.RefUpdate.Result)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 RevWalk (org.eclipse.jgit.revwalk.RevWalk)2 RefModel (com.gitblit.models.RefModel)1 ProxyHelper (com.google.devtools.build.lib.bazel.repository.downloader.ProxyHelper)1 RepositoryFunctionException (com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)1 WorkspaceAttributeMapper (com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper)1 EvalException (com.google.devtools.build.lib.syntax.EvalException)1