List of usage examples for org.eclipse.jgit.diff DiffEntry getNewId
public AbbreviatedObjectId getNewId()
index. From source file:ShowFileDiff.java
License:Apache License
public static void main(String[] args) throws IOException, GitAPIException { Repository repository = null; //CookbookHelper.openJGitCookbookRepository(); // the diff works on TreeIterators, we prepare two for the two branches AbstractTreeIterator oldTreeParser = prepareTreeParser(repository, "09c65401f3730eb3e619c33bf31e2376fb393727"); AbstractTreeIterator newTreeParser = prepareTreeParser(repository, "aa31703b65774e4a06010824601e56375a70078c"); // then the procelain diff-command returns a list of diff entries List<DiffEntry> diff = new Git(repository).diff().setOldTree(oldTreeParser).setNewTree(newTreeParser) .setPathFilter(PathFilter.create("README.md")).call(); for (DiffEntry entry : diff) { System.out.println("Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId()); DiffFormatter formatter = new DiffFormatter(System.out); formatter.setRepository(repository); formatter.format(entry);// w ww . j a v a 2 s. c o m } repository.close(); }
From source file:MyDiffFormatter.java
License:Eclipse Distribution License
private static void writeGitLinkDiffText(OutputStream o, DiffEntry ent) throws IOException { if (ent.getOldMode() == GITLINK) { o.write(encodeASCII("-Subproject commit " + ent.getOldId().name() //$NON-NLS-1$ + "\n")); //$NON-NLS-1$ }/* w ww.j a v a 2 s . c o m*/ if (ent.getNewMode() == GITLINK) { o.write(encodeASCII("+Subproject commit " + ent.getNewId().name() //$NON-NLS-1$ + "\n")); //$NON-NLS-1$ } }
From source file:MyDiffFormatter.java
License:Eclipse Distribution License
public FormatResult getFormatResult(DiffEntry ent) throws IOException { final FormatResult res = new FormatResult(); ByteArrayOutputStream buf = new ByteArrayOutputStream(); final EditList editList; final FileHeader.PatchType type; formatHeader(buf, ent);//from w w w . j a v a 2 s . c om if (ent.getOldMode() == GITLINK || ent.getNewMode() == GITLINK) { formatOldNewPaths(buf, ent); writeGitLinkDiffText(buf, ent); editList = new EditList(); type = PatchType.UNIFIED; } else if (ent.getOldId() == null || ent.getNewId() == null) { // Content not changed (e.g. only mode, pure rename) editList = new EditList(); type = PatchType.UNIFIED; } else { assertHaveRepository(); byte[] aRaw = open(OLD, ent); byte[] bRaw = open(NEW, ent); if (aRaw == BINARY || bRaw == BINARY // || RawText.isBinary(aRaw) || RawText.isBinary(bRaw)) { formatOldNewPaths(buf, ent); buf.write(encodeASCII("Binary files differ\n")); //$NON-NLS-1$ editList = new EditList(); type = PatchType.BINARY; } else { res.a = new RawText(aRaw); res.b = new RawText(bRaw); editList = diff(res.a, res.b); type = PatchType.UNIFIED; switch (ent.getChangeType()) { case RENAME: case COPY: if (!editList.isEmpty()) formatOldNewPaths(buf, ent); break; default: formatOldNewPaths(buf, ent); break; } } } res.header = new FileHeader(buf.toByteArray(), editList, type); return res; }
From source file:MyDiffFormatter.java
License:Eclipse Distribution License
private void formatHeader(ByteArrayOutputStream o, DiffEntry ent) throws IOException { final ChangeType type = ent.getChangeType(); final String oldp = ent.getOldPath(); final String newp = ent.getNewPath(); final FileMode oldMode = ent.getOldMode(); final FileMode newMode = ent.getNewMode(); formatGitDiffFirstHeaderLine(o, type, oldp, newp); if ((type == MODIFY || type == COPY || type == RENAME) && !oldMode.equals(newMode)) { o.write(encodeASCII("old mode ")); //$NON-NLS-1$ oldMode.copyTo(o);/*from w ww. j a v a 2 s.c o m*/ o.write('\n'); o.write(encodeASCII("new mode ")); //$NON-NLS-1$ newMode.copyTo(o); o.write('\n'); } switch (type) { case ADD: o.write(encodeASCII("new file mode ")); //$NON-NLS-1$ newMode.copyTo(o); o.write('\n'); break; case DELETE: o.write(encodeASCII("deleted file mode ")); //$NON-NLS-1$ oldMode.copyTo(o); o.write('\n'); break; case RENAME: o.write(encodeASCII("similarity index " + ent.getScore() + "%")); //$NON-NLS-1$ //$NON-NLS-2$ o.write('\n'); o.write(encode("rename from " + quotePath(oldp))); //$NON-NLS-1$ o.write('\n'); o.write(encode("rename to " + quotePath(newp))); //$NON-NLS-1$ o.write('\n'); break; case COPY: o.write(encodeASCII("similarity index " + ent.getScore() + "%")); //$NON-NLS-1$ //$NON-NLS-2$ o.write('\n'); o.write(encode("copy from " + quotePath(oldp))); //$NON-NLS-1$ o.write('\n'); o.write(encode("copy to " + quotePath(newp))); //$NON-NLS-1$ o.write('\n'); break; case MODIFY: if (0 < ent.getScore()) { o.write(encodeASCII("dissimilarity index " //$NON-NLS-1$ + (100 - ent.getScore()) + "%")); //$NON-NLS-1$ o.write('\n'); } break; } if (ent.getOldId() != null && !ent.getOldId().equals(ent.getNewId())) { formatIndexLine(o, ent); } }
From source file:MyDiffFormatter.java
License:Eclipse Distribution License
/** * @param o the stream the formatter will write line data to * @param ent the DiffEntry to create the FileHeader for * @throws IOException writing to the supplied stream failed. *///ww w . j a v a 2s.c om protected void formatIndexLine(OutputStream o, DiffEntry ent) throws IOException { o.write(encodeASCII("index " // //$NON-NLS-1$ + format(ent.getOldId()) // + ".." // //$NON-NLS-1$ + format(ent.getNewId()))); if (ent.getOldMode().equals(ent.getNewMode())) { o.write(' '); ent.getNewMode().copyTo(o); } o.write('\n'); }
From source file:MyDiffFormatter.java
License:Eclipse Distribution License
private void formatOldNewPaths(ByteArrayOutputStream o, DiffEntry ent) throws IOException { if (ent.getOldId().equals(ent.getNewId())) return;/*from w w w .j a v a 2s.c o m*/ final String oldp; final String newp; switch (ent.getChangeType()) { case ADD: oldp = DiffEntry.DEV_NULL; newp = quotePath(newPrefix + ent.getNewPath()); break; case DELETE: oldp = quotePath(oldPrefix + ent.getOldPath()); newp = DiffEntry.DEV_NULL; break; default: oldp = quotePath(oldPrefix + ent.getOldPath()); newp = quotePath(newPrefix + ent.getNewPath()); break; } o.write(encode("--- " + oldp + "\n")); //$NON-NLS-1$ //$NON-NLS-2$ o.write(encode("+++ " + newp + "\n")); //$NON-NLS-1$ //$NON-NLS-2$ }
From source file:at.bitandart.zoubek.mervin.gerrit.GerritReviewRepositoryService.java
License:Open Source License
/** * loads all patches of from the given list of {@link DiffEntry}s. * //w w w.j av a 2 s. co m * @param patchSet * the patchSet to add the patches to. * @param ref * the ref to the commit of the patch set. * @param repository * the git repository instance * @throws RepositoryIOException */ private void loadPatches(PatchSet patchSet, Ref ref, Git git) throws RepositoryIOException { EList<Patch> patches = patchSet.getPatches(); Repository repository = git.getRepository(); try { RevWalk revWalk = new RevWalk(repository); RevCommit newCommit = revWalk.parseCommit(ref.getObjectId()); RevCommit oldCommit = newCommit.getParent(0); revWalk.parseHeaders(oldCommit); ObjectReader objectReader = repository.newObjectReader(); revWalk.close(); CanonicalTreeParser newTreeIterator = new CanonicalTreeParser(); newTreeIterator.reset(objectReader, newCommit.getTree().getId()); CanonicalTreeParser oldTreeIterator = new CanonicalTreeParser(); oldTreeIterator.reset(objectReader, oldCommit.getTree().getId()); List<DiffEntry> diffs = git.diff().setOldTree(oldTreeIterator).setNewTree(newTreeIterator).call(); for (DiffEntry diff : diffs) { String newPath = diff.getNewPath(); String oldPath = diff.getOldPath(); Patch patch = null; /* * only papyrus diagrams are supported for now, so models are in * .uml files, diagrams in .notation files */ if (diff.getChangeType() != ChangeType.DELETE) { if (newPath.endsWith(".uml")) { patch = modelReviewFactory.createModelPatch(); } else if (newPath.endsWith(".notation")) { patch = modelReviewFactory.createDiagramPatch(); } else { patch = modelReviewFactory.createPatch(); } } else { if (oldPath.endsWith(".uml")) { patch = modelReviewFactory.createModelPatch(); } else if (oldPath.endsWith(".notation")) { patch = modelReviewFactory.createDiagramPatch(); } else { patch = modelReviewFactory.createPatch(); } } switch (diff.getChangeType()) { case ADD: patch.setChangeType(PatchChangeType.ADD); break; case COPY: patch.setChangeType(PatchChangeType.COPY); break; case DELETE: patch.setChangeType(PatchChangeType.DELETE); break; case MODIFY: patch.setChangeType(PatchChangeType.MODIFY); break; case RENAME: patch.setChangeType(PatchChangeType.RENAME); break; } patch.setNewPath(newPath); patch.setOldPath(oldPath); if (diff.getChangeType() != ChangeType.DELETE) { ObjectLoader objectLoader = repository.open(diff.getNewId().toObjectId()); patch.setNewContent(objectLoader.getBytes()); } if (diff.getChangeType() != ChangeType.ADD) { ObjectLoader objectLoader = repository.open(diff.getOldId().toObjectId()); patch.setOldContent(objectLoader.getBytes()); } patches.add(patch); } } catch (IOException e) { throw new RepositoryIOException(MessageFormat.format( "An IO error occured during loading the patches for patch set #{0}", patchSet.getId()), e); } catch (GitAPIException e) { throw new RepositoryIOException( MessageFormat.format("An JGit API error occured during loading the patches for patch set #{0}", patchSet.getId()), e); } }
From source file:boa.datagen.scm.GitCommit.java
License:Apache License
private void getChangeFiles(final RevCommit parent, final RevCommit rc, final HashMap<String, String> rChangedPaths, final HashMap<String, String> rRemovedPaths, final HashMap<String, String> rAddedPaths) { final DiffFormatter df = new DiffFormatter(NullOutputStream.INSTANCE); df.setRepository(repository);//from w w w .ja v a 2 s. c o m df.setDiffComparator(RawTextComparator.DEFAULT); df.setDetectRenames(true); try { final AbstractTreeIterator parentIter; if (parent == null) parentIter = new EmptyTreeIterator(); else parentIter = new CanonicalTreeParser(null, repository.newObjectReader(), parent.getTree()); for (final DiffEntry diff : df.scan(parentIter, new CanonicalTreeParser(null, repository.newObjectReader(), rc.getTree()))) { if (diff.getChangeType() == ChangeType.MODIFY || diff.getChangeType() == ChangeType.COPY || diff.getChangeType() == ChangeType.RENAME) { if (diff.getOldMode().getObjectType() == Constants.OBJ_BLOB && diff.getNewMode().getObjectType() == Constants.OBJ_BLOB) { String path = diff.getNewPath(); rChangedPaths.put(path, diff.getOldPath()); filePathGitObjectIds.put(path, diff.getNewId().toObjectId()); } } else if (diff.getChangeType() == ChangeType.ADD) { if (diff.getNewMode().getObjectType() == Constants.OBJ_BLOB) { String path = diff.getNewPath(); rAddedPaths.put(path, null); filePathGitObjectIds.put(path, diff.getNewId().toObjectId()); } } else if (diff.getChangeType() == ChangeType.DELETE) { if (diff.getOldMode().getObjectType() == Constants.OBJ_BLOB) { rRemovedPaths.put(diff.getOldPath(), diff.getOldPath()); } } } } catch (final IOException e) { if (debug) System.err.println("Git Error getting commit diffs: " + e.getMessage()); } df.close(); }
From source file:br.com.metricminer2.scm.GitRepository.java
License:Apache License
private String getSourceCode(Repository repo, DiffEntry diff) throws MissingObjectException, IOException, UnsupportedEncodingException { try {//from w ww .j a v a 2s . com ObjectReader reader = repo.newObjectReader(); byte[] bytes = reader.open(diff.getNewId().toObjectId()).getBytes(); return new String(bytes, "utf-8"); } catch (Throwable e) { return ""; } }
From source file:com.buildautomation.jgit.api.ShowFileDiff.java
License:Apache License
public static void showFileDiff() throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { // the diff works on TreeIterators, we prepare two for the two branches AbstractTreeIterator oldTreeParser = prepareTreeParser(repository, "09c65401f3730eb3e619c33bf31e2376fb393727"); AbstractTreeIterator newTreeParser = prepareTreeParser(repository, "aa31703b65774e4a06010824601e56375a70078c"); // then the procelain diff-command returns a list of diff entries try (Git git = new Git(repository)) { List<DiffEntry> diff = git.diff().setOldTree(oldTreeParser).setNewTree(newTreeParser) .setPathFilter(PathFilter.create("README.md")).call(); for (DiffEntry entry : diff) { System.out.println( "Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId()); try (DiffFormatter formatter = new DiffFormatter(System.out)) { formatter.setRepository(repository); formatter.format(entry); }/*from ww w . java 2 s . c om*/ } } } }