Search in sources :

Example 51 with GitAPIException

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

the class GitUtils method getRepository.

public static synchronized Repository getRepository(File gitRoot, String branch, String gitUrl, String gitPushUrl) throws IOException, ConfigInvalidException, JGitInternalException, GitAPIException {
    File dotGit;
    if (gitRoot.getName().equals(Constants.DOT_GIT)) {
        dotGit = gitRoot;
    } else {
        dotGit = new File(gitRoot, Constants.DOT_GIT);
    }
    Repository repository = localRepos.get(dotGit);
    if (repository != null && dotGit.exists()) {
        return repository;
    }
    if (!dotGit.exists()) {
        Git.cloneRepository().setDirectory(gitRoot).setCloneAllBranches(true).setURI(gitUrl).call();
        FileBasedConfig config = new FileBasedConfig(new File(dotGit, "config"), FS.DETECTED);
        config.load();
        if (gitPushUrl != null) {
            config.setString(ConfigConstants.CONFIG_REMOTE_SECTION, "origin", "pushurl", gitPushUrl);
        }
        config.save();
    }
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    repository = builder.setGitDir(dotGit).readEnvironment().findGitDir().build();
    localRepos.put(dotGit, repository);
    try {
        repository.incrementOpen();
        Git git = Git.wrap(repository);
        // Check branch
        boolean pull = true;
        String currentBranch = repository.getBranch();
        if (branch != null && !branch.equals(currentBranch)) {
            CheckoutCommand checkout = git.checkout();
            if (!branchExists(git, branch)) {
                checkout.setCreateBranch(true);
                pull = false;
            }
            checkout.setName(branch);
            checkout.call();
        }
        if (pull) {
            git.pull().call();
        } else {
            git.fetch().call();
        }
    } catch (Exception e) {
        if (!(e.getCause() instanceof TransportException)) {
            throw new RuntimeException(e);
        }
    } finally {
        if (repository != null) {
            repository.close();
        }
    }
    return repository;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) Git(org.eclipse.jgit.api.Git) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) File(java.io.File) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) TransportException(org.eclipse.jgit.errors.TransportException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) TransportException(org.eclipse.jgit.errors.TransportException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException)

Example 52 with GitAPIException

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

the class GitVersionControlSystem method getDefaultBranch.

@Override
public VersionRef getDefaultBranch(VersionControlSpec spec) {
    GitVersionControlSpec gitSpec = cast(spec);
    Collection<Ref> refs;
    try {
        refs = Git.lsRemoteRepository().setRemote(normalizeUri(gitSpec.getUrl())).setTags(false).call();
    } catch (URISyntaxException e) {
        throw wrapGitCommandException("ls-remote", gitSpec.getUrl(), null, e);
    } catch (GitAPIException e) {
        throw wrapGitCommandException("ls-remote", gitSpec.getUrl(), null, e);
    }
    for (Ref ref : refs) {
        if (ref.getName().equals("refs/heads/master")) {
            return GitVersionRef.from(ref);
        }
    }
    throw new UnsupportedOperationException("Git repository has no master branch");
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) VersionRef(org.gradle.vcs.internal.VersionRef) Ref(org.eclipse.jgit.lib.Ref) GitVersionControlSpec(org.gradle.vcs.git.GitVersionControlSpec) URISyntaxException(java.net.URISyntaxException)

Example 53 with GitAPIException

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

the class GitVersionControlSystem method updateRepo.

private static void updateRepo(File workingDir, GitVersionControlSpec gitSpec, VersionRef ref) {
    Git git = null;
    try {
        git = Git.open(workingDir);
        git.fetch().setRemote(getRemoteForUrl(git.getRepository(), gitSpec.getUrl())).call();
        git.reset().setMode(ResetCommand.ResetType.HARD).setRef(ref.getCanonicalId()).call();
        updateSubModules(git);
    } catch (IOException e) {
        throw wrapGitCommandException("update", gitSpec.getUrl(), workingDir, e);
    } catch (URISyntaxException e) {
        throw wrapGitCommandException("update", gitSpec.getUrl(), workingDir, e);
    } catch (GitAPIException e) {
        throw wrapGitCommandException("update", gitSpec.getUrl(), workingDir, e);
    } catch (JGitInternalException e) {
        throw wrapGitCommandException("update", gitSpec.getUrl(), workingDir, e);
    } finally {
        if (git != null) {
            git.close();
        }
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException)

Example 54 with GitAPIException

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

the class GitVersionControlSystem method getAvailableVersions.

@Override
public Set<VersionRef> getAvailableVersions(VersionControlSpec spec) {
    GitVersionControlSpec gitSpec = cast(spec);
    Collection<Ref> refs;
    try {
        refs = Git.lsRemoteRepository().setRemote(normalizeUri(gitSpec.getUrl())).setTags(true).setHeads(false).call();
    } catch (URISyntaxException e) {
        throw wrapGitCommandException("ls-remote", gitSpec.getUrl(), null, e);
    } catch (GitAPIException e) {
        throw wrapGitCommandException("ls-remote", gitSpec.getUrl(), null, e);
    }
    Set<VersionRef> versions = Sets.newHashSet();
    for (Ref ref : refs) {
        GitVersionRef gitRef = GitVersionRef.from(ref);
        versions.add(gitRef);
    }
    return versions;
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) VersionRef(org.gradle.vcs.internal.VersionRef) VersionRef(org.gradle.vcs.internal.VersionRef) Ref(org.eclipse.jgit.lib.Ref) GitVersionControlSpec(org.gradle.vcs.git.GitVersionControlSpec) URISyntaxException(java.net.URISyntaxException)

Example 55 with GitAPIException

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

the class GitVersionControlSystem method getBranch.

@Nullable
@Override
public VersionRef getBranch(VersionControlSpec spec, String branch) {
    GitVersionControlSpec gitSpec = cast(spec);
    Collection<Ref> refs;
    try {
        refs = Git.lsRemoteRepository().setRemote(normalizeUri(gitSpec.getUrl())).setHeads(true).setTags(false).call();
    } catch (URISyntaxException e) {
        throw wrapGitCommandException("ls-remote", gitSpec.getUrl(), null, e);
    } catch (GitAPIException e) {
        throw wrapGitCommandException("ls-remote", gitSpec.getUrl(), null, e);
    }
    String refName = "refs/heads/" + branch;
    for (Ref ref : refs) {
        if (ref.getName().equals(refName)) {
            return GitVersionRef.from(ref);
        }
    }
    return null;
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) VersionRef(org.gradle.vcs.internal.VersionRef) Ref(org.eclipse.jgit.lib.Ref) GitVersionControlSpec(org.gradle.vcs.git.GitVersionControlSpec) URISyntaxException(java.net.URISyntaxException) Nullable(javax.annotation.Nullable)

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