List of usage examples for org.eclipse.jgit.api Git close
@Override public void close()
Free resources associated with this instance.
From source file:com.github.checkstyle.regression.git.DiffParser.java
License:Open Source License
/** * Parses the diff between a given branch and the master in the give repository path. * @param repositoryPath the path of checkstyle repository * @param branchName the name of the branch to be compared with master * @return a list of {@link GitChange} to represent the changes * @throws IOException JGit library exception * @throws GitAPIException JGit library exception */// www.j a v a 2 s.co m public static List<GitChange> parse(String repositoryPath, String branchName) throws IOException, GitAPIException { final List<GitChange> returnValue = new LinkedList<>(); final File gitDir = new File(repositoryPath, ".git"); final Repository repository = new FileRepositoryBuilder().setGitDir(gitDir).readEnvironment().findGitDir() .build(); try { final TreeParserPair pair = getTreeParserPair(repository, branchName); final Git git = new Git(repository); final DiffFormatter formatter = new DiffFormatter(DisabledOutputStream.INSTANCE); formatter.setRepository(repository); try { final List<DiffEntry> diffs = git.diff().setOldTree(pair.commonAncestorTreeParser) .setNewTree(pair.prTreeParser).call().stream() .filter(entry -> entry.getChangeType() != DiffEntry.ChangeType.DELETE) .collect(Collectors.toList()); for (DiffEntry diff : diffs) { returnValue.add(convertDiffEntryToGitChange(diff, formatter)); } } finally { git.close(); } } finally { repository.close(); } return returnValue; }
From source file:com.github.rwhogg.git_vcr.App.java
License:Open Source License
/** * Clean up the Git repo/*w ww.ja v a 2s. c o m*/ * @param git Git to use * @param oldCommit Hash of the commit to return to */ private static void cleanupGit(Git git, String oldCommit) { try { git.reset().setMode(ResetType.HARD).call(); git.checkout().setName(oldCommit).call(); } catch (org.eclipse.jgit.api.errors.CheckoutConflictException e) { Util.error("there was an checkout conflict!"); } catch (GitAPIException e) { Util.error("there was an unspecified Git API error!"); } git.close(); }
From source file:com.google.appraise.eclipse.core.client.git.AppraiseGitReviewClient.java
License:Open Source License
/** * Retrieves all the reviews in the current project's repository by commit hash. *///from w w w . j a v a 2 s . c o m public Map<String, Review> listReviews() throws GitClientException { // Get the most up-to-date list of reviews. syncCommentsAndReviews(); Map<String, Review> reviews = new LinkedHashMap<>(); Git git = new Git(repo); try { ListNotesCommand cmd = git.notesList(); cmd.setNotesRef(REVIEWS_REF); List<Note> notes = cmd.call(); for (Note note : notes) { String rawNoteDataStr = noteToString(repo, note); Review latest = extractLatestReviewFromNotes(rawNoteDataStr); if (latest != null) { reviews.put(note.getName(), latest); } } } catch (Exception e) { throw new GitClientException(e); } finally { git.close(); } return reviews; }
From source file:com.google.devtools.build.lib.bazel.repository.GitCloneFunction.java
License:Open Source License
@Nullable @Override// ww w. j a v a2 s .co m public SkyValue compute(SkyKey skyKey, Environment env) throws RepositoryFunctionException { GitRepositoryDescriptor descriptor = (GitRepositoryDescriptor) skyKey.argument(); Git git = null; try { if (descriptor.directory.exists()) { if (isUpToDate(descriptor)) { return new HttpDownloadValue(descriptor.directory); } try { FileSystemUtils.deleteTree(descriptor.directory); } catch (IOException e) { throw new RepositoryFunctionException(e, Transience.TRANSIENT); } } git = Git.cloneRepository().setURI(descriptor.remote) .setCredentialsProvider(new NetRCCredentialsProvider()) .setDirectory(descriptor.directory.getPathFile()).setCloneSubmodules(false).setNoCheckout(true) .setProgressMonitor(new GitProgressMonitor("Cloning " + descriptor.remote, reporter)).call(); git.checkout().setCreateBranch(true).setName("bazel-checkout").setStartPoint(descriptor.checkout) .call(); // Using CloneCommand.setCloneSubmodules() results in SubmoduleInitCommand and // SubmoduleUpdateCommand to be called recursively for all submodules. This is not // desirable for repositories, such as github.com/rust-lang/rust-installer, which // recursively includes itself as a submodule, which would result in an infinite // loop if submodules are cloned recursively. For now, limit submodules to only // the first level. if (descriptor.initSubmodules && !git.submoduleInit().call().isEmpty()) { git.submoduleUpdate() .setProgressMonitor( new GitProgressMonitor("Cloning submodules for " + descriptor.remote, reporter)) .call(); } } catch (InvalidRemoteException e) { throw new RepositoryFunctionException(new IOException("Invalid Git repository URI: " + e.getMessage()), Transience.PERSISTENT); } catch (RefNotFoundException | InvalidRefNameException e) { throw new RepositoryFunctionException( new IOException("Invalid branch, tag, or commit: " + e.getMessage()), Transience.PERSISTENT); } catch (GitAPIException e) { throw new RepositoryFunctionException(new IOException(e.getMessage()), Transience.TRANSIENT); } finally { if (git != null) { git.close(); } } return new HttpDownloadValue(descriptor.directory); }
From source file:com.google.devtools.build.lib.bazel.repository.GitCloner.java
License:Open Source License
public static SkyValue clone(Rule rule, Path outputDirectory, EventHandler eventHandler, Map<String, String> clientEnvironment) throws RepositoryFunctionException { WorkspaceAttributeMapper mapper = WorkspaceAttributeMapper.of(rule); if (mapper.isAttributeValueExplicitlySpecified("commit") == mapper .isAttributeValueExplicitlySpecified("tag")) { throw new RepositoryFunctionException( new EvalException(rule.getLocation(), "One of either commit or tag must be defined"), Transience.PERSISTENT);//from ww w . java 2s . c om } GitRepositoryDescriptor descriptor; String startingPoint; try { if (mapper.isAttributeValueExplicitlySpecified("commit")) { startingPoint = mapper.get("commit", Type.STRING); } else { startingPoint = "tags/" + mapper.get("tag", Type.STRING); } descriptor = new GitRepositoryDescriptor(mapper.get("remote", Type.STRING), startingPoint, mapper.get("init_submodules", Type.BOOLEAN), outputDirectory); } catch (EvalException e) { throw new RepositoryFunctionException(e, Transience.PERSISTENT); } // Setup proxy if remote is http or https if (descriptor.remote != null && Ascii.toLowerCase(descriptor.remote).startsWith("http")) { try { new ProxyHelper(clientEnvironment).createProxyIfNeeded(new URL(descriptor.remote)); } catch (IOException ie) { throw new RepositoryFunctionException(ie, Transience.TRANSIENT); } } Git git = null; try { if (descriptor.directory.exists()) { if (isUpToDate(descriptor)) { return new HttpDownloadValue(descriptor.directory); } try { FileSystemUtils.deleteTree(descriptor.directory); } catch (IOException e) { throw new RepositoryFunctionException(e, Transience.TRANSIENT); } } git = Git.cloneRepository().setURI(descriptor.remote) .setCredentialsProvider(new NetRCCredentialsProvider()) .setDirectory(descriptor.directory.getPathFile()).setCloneSubmodules(false).setNoCheckout(true) .setProgressMonitor(new GitProgressMonitor("Cloning " + descriptor.remote, eventHandler)) .call(); git.checkout().setCreateBranch(true).setName("bazel-checkout").setStartPoint(descriptor.checkout) .call(); // Using CloneCommand.setCloneSubmodules() results in SubmoduleInitCommand and // SubmoduleUpdateCommand to be called recursively for all submodules. This is not // desirable for repositories, such as github.com/rust-lang/rust-installer, which // recursively includes itself as a submodule, which would result in an infinite // loop if submodules are cloned recursively. For now, limit submodules to only // the first level. if (descriptor.initSubmodules && !git.submoduleInit().call().isEmpty()) { git.submoduleUpdate() .setProgressMonitor( new GitProgressMonitor("Cloning submodules for " + descriptor.remote, eventHandler)) .call(); } } catch (InvalidRemoteException e) { throw new RepositoryFunctionException(new IOException("Invalid Git repository URI: " + e.getMessage()), Transience.PERSISTENT); } catch (RefNotFoundException | InvalidRefNameException e) { throw new RepositoryFunctionException( new IOException("Invalid branch, tag, or commit: " + e.getMessage()), Transience.PERSISTENT); } catch (GitAPIException e) { // This is a sad attempt to actually get a useful error message out of jGit, which will bury // the actual (useful) cause of the exception under several throws. StringBuilder errmsg = new StringBuilder(); errmsg.append(e.getMessage()); Throwable throwable = e; while (throwable.getCause() != null) { throwable = throwable.getCause(); errmsg.append(" caused by " + throwable.getMessage()); } throw new RepositoryFunctionException(new IOException("Error cloning repository: " + errmsg), Transience.PERSISTENT); } catch (JGitInternalException e) { // This is a lame catch-all for jgit throwing RuntimeExceptions all over the place because, // as the docs put it, "a lot of exceptions are so low-level that is is unlikely that the // caller of the command can handle them effectively." Thanks, jgit. throw new RepositoryFunctionException(new IOException(e.getMessage()), Transience.PERSISTENT); } finally { if (git != null) { git.close(); } } return new HttpDownloadValue(descriptor.directory); }
From source file:com.hazelcast.simulator.utils.jars.GitSupport.java
License:Open Source License
private String fetchSources(File path, String revision) { Git git = null; String fullSha1 = null;/*from www. j av a2 s . c o m*/ try { git = cloneIfNecessary(path); syncRemoteRepositories(git); fetchAllRepositories(git); fullSha1 = checkoutRevision(git, revision); } catch (GitAPIException e) { throw new CommandLineExitException("Error while fetching sources from Git", e); } catch (IOException e) { throw new CommandLineExitException("Error while fetching sources from Git", e); } finally { if (git != null) { git.close(); } } return fullSha1; }
From source file:com.hazelcast.simulator.utils.jars.GitSupport.java
License:Open Source License
private boolean isValidLocalRepository(File repository) { boolean result = false; Git git = null; try {/*from ww w . ja v a 2s. co m*/ git = Git.open(repository); result = true; } catch (RepositoryNotFoundException e) { result = false; } catch (IOException e) { result = false; } finally { if (git != null) { git.close(); } } return result; }
From source file:com.maiereni.synchronizer.git.utils.GitDownloaderImpl.java
License:Apache License
/** * Downloads artifacts from the Git Repository defined by the argument * @param properties//from w ww . j a v a2s. com * @return the result * @throws Exception */ @Override public GitResults download(@Nonnull final GitProperties properties) throws Exception { Git git = null; if (StringUtils.isBlank(properties.getLocalRepo())) { throw new Exception("The local repository cannot be null"); } File fRepo = new File(properties.getLocalRepo()); if (fRepo.isFile()) { throw new Exception("The local repo setting points to a file. It needs to be a directory"); } boolean isCreated = false; if (isRepository(fRepo)) { git = Git.open(fRepo); } else { git = createRepository(fRepo, properties); isCreated = true; } GitResults res = downloadProject(git, properties); res.setContentPath(new File(fRepo, properties.getContentPath()).toString()); res.setCreated(isCreated); LayoutRules layoutRules = getLayoutRules(res); res.setLayoutRules(layoutRules); git.close(); return res; }
From source file:com.searchcode.app.util.Helpers.java
License:Open Source License
public void closeQuietly(Git git) { try { git.close(); } catch (Exception ex) { } }
From source file:com.sebastian_daschner.asciiblog.business.source.control.GitExtractorTest.java
License:Apache License
private void changeAndRenameFile() throws IOException, GitAPIException { final Git git = Git.open(gitCloneDirectory); git.pull().setRemote("origin").setRemoteBranchName("master").call(); try (FileWriter writer = new FileWriter(file2, true)) { writer.append("\nchanged"); }//from ww w . jav a 2s. c om if (!file2.renameTo(file2.toPath().resolveSibling("file3.adoc").toFile())) throw new IOException("Could not rename file2 to file3"); git.add().setUpdate(true).addFilepattern(".").call(); git.add().addFilepattern(".").call(); git.commit().setMessage("changed file2 and renamed to file3").call(); git.push().setRemote("origin").add("master").call(); git.close(); }