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;
}
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");
}
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();
}
}
}
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;
}
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;
}
Aggregations