String findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit) { if (laterCommit == null || earlierCommit == null) { return null; } String output = null; try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { DiffFormatter diffFormatter = new DiffFormatter(out); diffFormatter.setRepository(gitRepo); diffFormatter.format(earlierCommit.getId(), laterCommit.getId()); output = out.toString(); output = StringUtil.stripTillLastOccurrenceOf(output, "+++ b/cruise-config.xml"); } catch (IOException e) { throw new RuntimeException("Error occurred during diff computation. Message: " + e.getMessage()); } return output; }
public List<DiffEntry> call() throws GitAPIException { try (DiffFormatter diffFmt = getDiffFormatter()) { diffFmt.setRepository(repo); diffFmt.setProgressMonitor(monitor); if (cached) { if (oldTree == null) { diffFmt.setPathFilter(pathFilter); List<DiffEntry> result = diffFmt.scan(oldTree, newTree); if (showNameAndStatusOnly) return result; else { if (contextLines >= 0) diffFmt.setContext(contextLines); if (destinationPrefix != null) diffFmt.setNewPrefix(destinationPrefix); if (sourcePrefix != null) diffFmt.setOldPrefix(sourcePrefix); diffFmt.format(result); diffFmt.flush(); return result;
private void setReader(ObjectReader reader, Config cfg, boolean closeReader) { close(); this.closeReader = closeReader; this.reader = reader; this.diffCfg = cfg.get(DiffConfig.KEY); ContentSource cs = ContentSource.create(reader); source = new ContentSource.Pair(cs, cs); if (diffCfg.isNoPrefix()) { setOldPrefix(""); //$NON-NLS-1$ setNewPrefix(""); //$NON-NLS-1$ } setDetectRenames(diffCfg.isRenameDetectionEnabled()); diffAlgorithm = DiffAlgorithm.getAlgorithm(cfg.getEnum( ConfigConstants.CONFIG_DIFF_SECTION, null, ConfigConstants.CONFIG_KEY_ALGORITHM, SupportedAlgorithm.HISTOGRAM)); }
/** * Format a patch script for one file entry. * * @param ent * the entry to be formatted. * @throws java.io.IOException * a file's content cannot be read, or the output stream cannot * be written to. */ public void format(DiffEntry ent) throws IOException { FormatResult res = createFormatResult(ent); format(res.header, res.a, res.b); }
/** * Format the differences between two trees. * * The patch is expressed as instructions to modify {@code a} to make it * {@code b}. * * <p> * Either side may be null to indicate that the tree has beed added or * removed. The diff will be computed against nothing. * * @param a * the old (or previous) side or null * @param b * the new (or updated) side or null * @throws java.io.IOException * trees cannot be read, file contents cannot be read, or the * patch cannot be output. */ public void format(RevTree a, RevTree b) throws IOException { format(scan(a, b)); }
protected static DiffFormatter createDiffFormatter(Repository r, OutputStream buffer) { DiffFormatter formatter = new DiffFormatter(buffer); formatter.setRepository(r); formatter.setDiffComparator(RawTextComparator.DEFAULT); formatter.setDetectRenames(true); return formatter; }
/** * Compares the RawText of two files and creates a formateted diff to return. * * @param oldText - RawText. * @param newText - RawText. * @return - Formatted diff in a byte[]. * @throws IOException -failed. */ public byte[] getFormatter(final RawText oldText, final RawText newText) throws IOException { if (!isUndefined()) { val diffAlgorithm = DiffAlgorithm.getAlgorithm( git.getRepository().getConfig().getEnum("diff", null, "algorithm", DiffAlgorithm.SupportedAlgorithm.HISTOGRAM)); val editList = diffAlgorithm.diff(RawTextComparator.DEFAULT, oldText, newText); val bytes = new ByteArrayOutputStream(); try (val df = new DiffFormatter(bytes)) { df.setRepository(git.getRepository()); df.format(editList, oldText, newText); df.flush(); return bytes.toByteArray(); } } return StringUtils.EMPTY.getBytes(StandardCharsets.UTF_8); }
/** * Compares the old tree and the new tree to get the list of the affected files. */ private List<DiffEntry> compareTrees( @Nullable ObjectId prevTreeId, ObjectId nextTreeId, TreeFilter filter) { try (DiffFormatter diffFormatter = new DiffFormatter(NullOutputStream.INSTANCE)) { diffFormatter.setRepository(jGitRepository); diffFormatter.setPathFilter(filter); return diffFormatter.scan(prevTreeId, nextTreeId); } catch (IOException e) { throw new StorageException("failed to compare two trees: " + prevTreeId + " vs. " + nextTreeId, e); } }
DiffFormatter formatter = new DiffFormatter(buffer); formatter.setRepository(r); formatter.setDiffComparator(cmp); formatter.setDetectRenames(true); List<DiffEntry> diffEntries = formatter.scan(baseTree, commitTree); if (blobPath != null && blobPath.length() > 0) { for (DiffEntry diffEntry : diffEntries) { if (diffEntry.getNewPath().equalsIgnoreCase(blobPath)) { formatter.format(diffEntry); break; formatter.format(diffEntries); formatter.flush(); return buffer.toString(); } catch (IOException e) {
// Create two commits to be compared File file = new File( git.getRepository().getWorkTree(), "file.txt" ); writeFile( file, "line1\n" ); RevCommit oldCommit = commitChanges(); writeFile( file, "line1\nline2\n" ); RevCommit newCommit = commitChanges(); // Obtain tree iterators to traverse the tree of the old/new commit ObjectReader reader = git.getRepository().newObjectReader(); CanonicalTreeParser oldTreeIter = new CanonicalTreeParser(); oldTreeIter.reset( reader, oldCommit.getTree() ); CanonicalTreeParser newTreeIter = new CanonicalTreeParser(); newTreeIter.reset( reader, newCommit.getTree() ); // Use a DiffFormatter to compare new and old tree and return a list of changes DiffFormatter diffFormatter = new DiffFormatter( DisabledOutputStream.INSTANCE ); diffFormatter.setRepository( git.getRepository() ); diffFormatter.setContext( 0 ); List<DiffEntry> entries = diffFormatter.scan( newTreeIter, oldTreeIter ); // Print the contents of the DiffEntries for( DiffEntry entry : entries ) { System.out.println( entry ); FileHeader fileHeader = diffFormatter.toFileHeader( entry ); List<? extends HunkHeader> hunks = fileHeader.getHunks(); for( HunkHeader hunk : hunks ) { System.out.println( hunk ); } }
@Override protected void run() throws Exception { diffFmt.setRepository(db); try { if (cached) { diffFmt.setProgressMonitor(pm); diffFmt.setPathFilter(pathFilter); if (detectRenames != null) diffFmt.setDetectRenames(detectRenames.booleanValue()); if (renameLimit != null && diffFmt.isDetectRenames()) { RenameDetector rd = diffFmt.getRenameDetector(); rd.setRenameLimit(renameLimit.intValue()); nameStatus(outw, diffFmt.scan(oldTree, newTree)); outw.flush(); diffFmt.format(oldTree, newTree); diffFmt.flush(); diffFmt.release();
DiffFormatter formatter = new DiffFormatter( System.out ); formatter.setRepository( git.getRepository() ); AbstractTreeIterator commitTreeIterator = prepareTreeParser( git.getRepository(), Constants.HEAD ); FileTreeIterator workTreeIterator = new FileTreeIterator( git.getRepository() ); List<DiffEntry> diffEntries = formatter.scan( commitTreeIterator, workTreeIterator ); for( DiffEntry entry : diffEntries ) { System.out.println( "Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId() ); formatter.format( entry ); }
walk.markStart(commit); DiffFormatter diffFormatter = new DiffFormatter(DisabledOutputStream.INSTANCE); //we don't print out diffFormatter.setRepository(repo); diffFormatter.setContext(0); //we only want to index the changes diffFormatter.setDetectRenames(true); List<DiffEntry> diffs = diffFormatter.scan(rev.getParent(0), rev); //first parent = current branch for (int diffIdx = 0; diffIdx < diffs.size(); diffIdx++) { DiffEntry diffEntry = diffs.get(diffIdx);
@Override public Map<String, List<Revision>> getAllHistories() throws StoreException { final Repository repository = git.getRepository(); try { final ObjectId head = repository.resolve(Constants.HEAD); final RevWalk revWalk = new RevWalk(repository); final DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE); df.setRepository(git.getRepository()); df.setDiffComparator(RawTextComparator.DEFAULT); final HistoryParser historyParser = new HistoryParser(revWalk, df, getTestDefinitionsDirectory()); return historyParser.parseFromHead(head); } catch (final IOException e) { throw new StoreException("Could not get history " + getGitCore().getRefName(), e); } }
/** * Scans the working dir for active changes and returns a list of differences. * * @return - List of DiffEntry. */ public List<DiffEntry> scanWorkingDiffs() { if (isUndefined()) { return new ArrayList<>(); } val workTreeIterator = new FileTreeIterator(git.getRepository()); val oldTreeIter = new CanonicalTreeParser(); try (ObjectReader reader = git.getRepository().newObjectReader()) { oldTreeIter.reset(reader, git.getRepository().resolve("HEAD^{tree}")); val formatter = new DiffFormatter(new ByteArrayOutputStream()); formatter.setRepository(git.getRepository()); return formatter.scan(oldTreeIter, workTreeIterator); } catch (final Exception e) { LOGGER.error(e.getMessage(), e); } return new ArrayList<>(); }
int linesAdded = 0; int linesDeleted = 0; int filesChanged = 0; try { repo = new FileRepository(new File("repo/.git")); RevWalk rw = new RevWalk(repo); RevCommit commit = rw.parseCommit(repo.resolve("486817d67b")); // Any ref will work here (HEAD, a sha1, tag, branch) RevCommit parent = rw.parseCommit(commit.getParent(0).getId()); DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE); df.setRepository(repo); df.setDiffComparator(RawTextComparator.DEFAULT); df.setDetectRenames(true); List<DiffEntry> diffs; diffs = df.scan(parent.getTree(), commit.getTree()); filesChanged = diffs.size(); for (DiffEntry diff : diffs) { for (Edit edit : df.toFileHeader(diff).toEditList()) { linesDeleted += edit.getEndA() - edit.getBeginA(); linesAdded += edit.getEndB() - edit.getBeginB(); } } } catch (IOException e1) { throw new RuntimeException(e1); }
DiffFormatter diffFormatter = new DiffFormatter(DisabledOutputStream.INSTANCE); diffFormatter.setRepository(repository); diffFormatter.setContext(0); FileHeader header = diffFormatter.toFileHeader(entry); List<? extends HunkHeader> hunks = header.getHunks(); for (HunkHeader hunkHeader : hunks) { diffFormatter.close(); return new Churn(addedLines, deletedLines);
@Override protected void run() throws Exception { diffFmt.setRepository(db); try { diffFmt.setPathFilter(pathFilter); if (detectRenames != null) diffFmt.setDetectRenames(detectRenames.booleanValue()); if (renameLimit != null && diffFmt.isDetectRenames()) { RenameDetector rd = diffFmt.getRenameDetector(); rd.setRenameLimit(renameLimit.intValue()); diffFmt.release();
private DiffFormatter createFormatter() { OutputStream outputStream = new ByteArrayOutputStream(); DiffFormatter formatter = new DiffFormatter(outputStream); formatter.setRepository(git.getRepository()); return formatter; }