List of usage examples for org.eclipse.jgit.lib ObjectLoader getBytes
public final byte[] getBytes(int sizeLimit) throws LargeObjectException, MissingObjectException, IOException
From source file:MyDiffFormatter.java
License:Eclipse Distribution License
private byte[] open(DiffEntry.Side side, DiffEntry entry) throws IOException { if (entry.getMode(side) == FileMode.MISSING) return EMPTY; if (entry.getMode(side).getObjectType() != Constants.OBJ_BLOB) return EMPTY; AbbreviatedObjectId id = entry.getId(side); if (!id.isComplete()) { Collection<ObjectId> ids = reader.resolve(id); if (ids.size() == 1) { throw new IllegalStateException(); // id = AbbreviatedObjectId.fromObjectId(ids.iterator().next()); // switch (side) { // case OLD: // entry.oldId = id; // break; // case NEW: // entry.newId = id; // break; // } } else if (ids.size() == 0) throw new MissingObjectException(id, Constants.OBJ_BLOB); else//from ww w . j a v a 2 s . c om throw new AmbiguousObjectException(id, ids); } try { ObjectLoader ldr = source.open(side, entry); int binaryFileThreshold = DEFAULT_BINARY_FILE_THRESHOLD; return ldr.getBytes(binaryFileThreshold); } catch (LargeObjectException.ExceedsLimit overLimit) { return BINARY; } catch (LargeObjectException.ExceedsByteArrayLimit overLimit) { return BINARY; } catch (LargeObjectException.OutOfMemory tooBig) { return BINARY; } catch (LargeObjectException tooBig) { tooBig.setObjectId(id.toObjectId()); throw tooBig; } }
From source file:com.googlesource.gerrit.plugins.xdocs.formatter.FormatterUtil.java
License:Apache License
/** * Returns the content of the specified file from the "<plugin-name>/" folder * of the ref/meta/config branch.// w w w .j av a 2 s. co m * * @param projectName the name of the project * @param fileName the name of the file in the "<plugin-name>/" folder * @return the file content, <code>null</code> if the file doesn't exist */ public String getMetaConfigFile(String projectName, String fileName) { try (Repository repo = repoManager.openRepository(new Project.NameKey(projectName))) { try (RevWalk rw = new RevWalk(repo)) { ObjectId id = repo.resolve(RefNames.REFS_CONFIG); if (id == null) { return null; } RevCommit commit = rw.parseCommit(id); RevTree tree = commit.getTree(); try (TreeWalk tw = new TreeWalk(repo)) { tw.addTree(tree); tw.setRecursive(true); tw.setFilter(PathFilter.create(pluginName + "/" + fileName)); if (!tw.next()) { return null; } ObjectId objectId = tw.getObjectId(0); ObjectLoader loader = repo.open(objectId); byte[] raw = loader.getBytes(Integer.MAX_VALUE); return new String(raw, UTF_8); } } } catch (IOException e) { return null; } }
From source file:com.googlesource.gerrit.plugins.xdocs.XDocLoader.java
License:Apache License
private String getHtml(String formatterName, StringFormatter f, Repository repo, ObjectLoader loader, Project.NameKey project, String path, ObjectId revId) throws MethodNotAllowedException, IOException, GitAPIException { byte[] bytes = loader.getBytes(Integer.MAX_VALUE); boolean isBinary = RawText.isBinary(bytes); if (formatterName.equals(Formatters.RAW_FORMATTER) && isBinary) { throw new MethodNotAllowedException(); }//from www. j a va2 s. c o m String raw = new String(bytes, UTF_8); String abbrRevId = getAbbrRevId(repo, revId); if (!isBinary) { raw = replaceMacros(repo, project, revId, abbrRevId, raw); } return f.format(project.get(), path, revId.getName(), abbrRevId, getFormatterConfig(formatterName), raw); }
From source file:com.googlesource.gerrit.plugins.xdocs.XDocServlet.java
License:Apache License
private Resource getImageResource(Repository repo, DiffMode diffMode, ObjectId revId, ObjectId revIdB, String file) {//from w w w . ja v a 2s .c o m ObjectId id = diffMode == DiffMode.NO_DIFF || diffMode == DiffMode.SIDEBYSIDE_A ? revId : revIdB; try (RevWalk rw = new RevWalk(repo)) { RevCommit commit = rw.parseCommit(id); RevTree tree = commit.getTree(); try (TreeWalk tw = new TreeWalk(repo)) { tw.addTree(tree); tw.setRecursive(true); tw.setFilter(PathFilter.create(file)); if (!tw.next()) { return Resource.NOT_FOUND; } ObjectId objectId = tw.getObjectId(0); ObjectLoader loader = repo.open(objectId); byte[] content = loader.getBytes(Integer.MAX_VALUE); MimeType mimeType = fileTypeRegistry.getMimeType(file, content); if (!isSafeImage(mimeType)) { return Resource.NOT_FOUND; } return new SmallResource(content).setContentType(mimeType.toString()) .setCharacterEncoding(UTF_8.name()).setLastModified(commit.getCommitTime()); } } catch (IOException e) { return Resource.NOT_FOUND; } }
From source file:jbenchmarker.trace.git.GitExtraction.java
License:Open Source License
private byte[] getBytes(ObjectLoader ldr, ObjectId id) throws IOException { try {//from w w w.j a v a 2 s . c o m return ldr.getBytes(binaryFileThreshold); } catch (LargeObjectException.ExceedsLimit overLimit) { return BINARY; } catch (LargeObjectException.ExceedsByteArrayLimit overLimit) { return BINARY; } catch (LargeObjectException.OutOfMemory tooBig) { return BINARY; } catch (LargeObjectException tooBig) { tooBig.setObjectId(id); throw tooBig; } }
From source file:jbenchmarker.trace.git.GitWalker.java
License:Open Source License
/** * For all registred parent combinaison place git repository in merged state * it finds file from id in next commit and it compares with file in * repository In phase 3//from ww w .j av a 2s. c o m * * @throws Exception */ public void mesuresDiff() throws Exception { // int commit = 0; ProgressBar progress = new ProgressBar(idCommitParentsToFiles.keySet().size()); /* double commitPercent = idCommitParentsToFiles.keySet().size() / 100.0; double nextStep = commitPercent;*/ for (String idcommitParent : idCommitParentsToFiles.keySet()) { logger.log(Level.INFO, "Treat Commit {0}", idcommitParent); /* * place git repository in merged state with parents */ gitPositionMerge(commitParent.getAll(idcommitParent)); /* * Foreach file must be compared in this state */ for (Couple c : idCommitParentsToFiles.getAll(idcommitParent)) { RawText stateAfterMerge = EmptyFile; RawText stateAfterCommit = EmptyFile; /* * get file in this repository */ File f = new File(gitDir + "/" + c.getFileName()); if (f.exists()) { // if file is existing after merge launchAndWait("sed /^[<=>][<=>][<=>][<=>]/d -i " + f.getPath()); stateAfterMerge = new RawText(f); } else { logger.log(Level.WARNING, "File{0} not found !", f.getPath()); } /* * get file from git with id. */ try { ObjectLoader ldr = source.open(c.fileName, c.getId()); ldr.getType(); stateAfterCommit = new RawText(ldr.getBytes(PackConfig.DEFAULT_BIG_FILE_THRESHOLD)); } catch (LargeObjectException.ExceedsLimit overLimit) {// File is overlimits => binary logger.log(Level.WARNING, "File {0} overlimits !", c.getFileName()); continue; } catch (LargeObjectException.ExceedsByteArrayLimit overLimit) {// File is overlimits => binary logger.log(Level.WARNING, "File {0} exceed limits!", c.getFileName()); continue; } catch (Exception ex) { // if another exception like inexitant considere 0 file. logger.log(Level.WARNING, "File {0} not found in after commit !", c.getFileName()); } /* * make a diff with this file */ EditList editList = diffAlgorithm.diff(RawTextComparator.DEFAULT, stateAfterMerge, stateAfterCommit); BlockLine editCount = files.get(c.fileName); editCount.incrementPass(); for (Edit ed : editList) {// Count line replace is two time counted editCount.addLine(ed.getEndA() - ed.getBeginA() + ed.getEndB() - ed.getBeginB()); } //Count the block size editCount.addBlock(editList.size()); } logger.info("Next"); // commit++; if (progressBar) { progress.progress(1); } } }