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; }
private DiffFormatter getDiffFormatter() { return out != null && !showNameAndStatusOnly ? new DiffFormatter(new BufferedOutputStream(out)) : new DiffFormatter(NullOutputStream.INSTANCE); }
private static void runDiff(Repository repo, String oldCommit, String newCommit, String path) throws IOException, GitAPIException { // Diff README.md between two commits. The file is named README.md in // the new commit (5a10bd6e), but was named "jgit-cookbook README.md" in // the old commit (2e1d65e4). DiffEntry diff = diffFile(repo, oldCommit, newCommit, path); // Display the diff System.out.println("Showing diff of " + path); try (DiffFormatter formatter = new DiffFormatter(System.out)) { formatter.setRepository(repo); //noinspection ConstantConditions formatter.format(diff); } }
private static void runDiff(Repository repo, String oldCommit, String newCommit, String path) throws IOException, GitAPIException { // Diff README.md between two commits. The file is named README.md in // the new commit (5a10bd6e), but was named "jgit-cookbook README.md" in // the old commit (2e1d65e4). DiffEntry diff = diffFile(repo, oldCommit, newCommit, path); // Display the diff System.out.println("Showing diff of " + path); try (DiffFormatter formatter = new DiffFormatter(System.out)) { formatter.setRepository(repo); //noinspection ConstantConditions formatter.format(diff); } }
/** * Returns a git-style diff between the two unix strings. * * Output has no trailing newlines. * * Boolean args determine whether whitespace or line endings will be visible. */ private static String diffWhitespaceLineEndings(String dirty, String clean, boolean whitespace, boolean lineEndings) throws IOException { dirty = visibleWhitespaceLineEndings(dirty, whitespace, lineEndings); clean = visibleWhitespaceLineEndings(clean, whitespace, lineEndings); RawText a = new RawText(dirty.getBytes(StandardCharsets.UTF_8)); RawText b = new RawText(clean.getBytes(StandardCharsets.UTF_8)); EditList edits = new EditList(); edits.addAll(MyersDiff.INSTANCE.diff(RawTextComparator.DEFAULT, a, b)); ByteArrayOutputStream out = new ByteArrayOutputStream(); try (DiffFormatter formatter = new DiffFormatter(out)) { formatter.format(edits, a, b); } String formatted = out.toString(StandardCharsets.UTF_8.name()); // we don't need the diff to show this, since we display newlines ourselves formatted = formatted.replace("\\ No newline at end of file\n", ""); return NEWLINE_MATCHER.trimTrailingFrom(formatted); }
private RebaseResult stop(RevCommit commitToPick, RebaseResult.Status status) throws IOException { PersonIdent author = commitToPick.getAuthorIdent(); String authorScript = toAuthorScript(author); rebaseState.createFile(AUTHOR_SCRIPT, authorScript); rebaseState.createFile(MESSAGE, commitToPick.getFullMessage()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (DiffFormatter df = new DiffFormatter(bos)) { df.setRepository(repo); df.format(commitToPick.getParent(0), commitToPick); } rebaseState.createFile(PATCH, new String(bos.toByteArray(), UTF_8)); rebaseState.createFile(STOPPED_SHA, repo.newObjectReader() .abbreviate( commitToPick).name()); // Remove cherry pick state file created by CherryPickCommand, it's not // needed for rebase repo.writeCherryPickHead(null); return RebaseResult.result(status, commitToPick); }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { // the diff works on TreeIterators, we prepare two for the two branches AbstractTreeIterator oldTreeParser = prepareTreeParser(repository, "b97b184b0ce11c0b6a4dcc2b57768ff155cb696b"); AbstractTreeIterator newTreeParser = prepareTreeParser(repository, "9e0719d7d773b41b49ebf04e6fd7b5c637e96063"); // then the porcelain 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")). // to filter on Suffix use the following instead //setPathFilter(PathSuffixFilter.create(".java")). 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); } } } } }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { // the diff works on TreeIterators, we prepare two for the two branches AbstractTreeIterator oldTreeParser = prepareTreeParser(repository, "b97b184b0ce11c0b6a4dcc2b57768ff155cb696b"); AbstractTreeIterator newTreeParser = prepareTreeParser(repository, "9e0719d7d773b41b49ebf04e6fd7b5c637e96063"); // then the porcelain 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")). // to filter on Suffix use the following instead //setPathFilter(PathSuffixFilter.create(".java")). 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); } } } } }
private DiffFormatter getDiffFormatter() { return out != null && !showNameAndStatusOnly ? new DiffFormatter(new BufferedOutputStream(out)) : new DiffFormatter(NullOutputStream.INSTANCE); }
@Override protected void init(final Repository repository, final String gitDir) { super.init(repository, gitDir); diffFmt = new DiffFormatter(new BufferedOutputStream(outs)); }
@Override protected void init(final Repository repository, final String gitDir) { super.init(repository, gitDir); diffFmt = new DiffFormatter(new BufferedOutputStream(outs)); }
protected String computePatch( Iterable<DiffEntry> entries, Repository repository ) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); DiffFormatter formatter = new DiffFormatter(output); formatter.setRepository(repository); for (DiffEntry entry : entries) { formatter.format(entry); } return output.toString(DIFF_CHARSET_NAME); }
private DiffFormatter createFormatter() { OutputStream outputStream = new ByteArrayOutputStream(); DiffFormatter formatter = new DiffFormatter(outputStream); formatter.setRepository(git.getRepository()); return formatter; }
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 ); }
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; }
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 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); } }
/** * 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); } }
public DiffFormatter getDiffFormatter(OutputStream out) { DiffFormatter formatter = new DiffFormatter(out); formatter.setRepository(git.getRepository()); formatter.setDiffComparator(RawTextComparator.DEFAULT); formatter.setDetectRenames(true); return formatter; }
private List<DiffEntry> getDiffs(RevCommit commit, RevCommit parent) throws IOException { DiffFormatter formatter = new DiffFormatter(DisabledOutputStream.INSTANCE); formatter.setRepository(repository); formatter.setDiffComparator(RawTextComparator.DEFAULT); formatter.setDetectRenames(true); return formatter.scan(parent.getTree(), commit.getTree()); } }