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; }
/** * Format a patch script from a list of difference entries. Requires * {@link #scan(AbstractTreeIterator, AbstractTreeIterator)} to have been * called first. * * @param entries * entries describing the affected files. * @throws java.io.IOException * a file's content cannot be read, or the output stream cannot * be written to. */ public void format(List<? extends DiffEntry> entries) throws IOException { for (DiffEntry ent : entries) format(ent); }
/** * 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)); }
/** * 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(AbstractTreeIterator a, AbstractTreeIterator b) throws IOException { format(scan(a, 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(AnyObjectId a, AnyObjectId b) throws IOException { format(scan(a, b)); }
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); } }
/** * Format index line * * @param o * the stream the formatter will write line data to * @param ent * the DiffEntry to create the FileHeader for * @throws java.io.IOException * writing to the supplied stream failed. */ 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'); }
/** * 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); }
out.write(head.getBuffer(), start, end - start); if (head.getPatchType() == PatchType.UNIFIED) format(head.toEditList(), a, b);
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); } } } } }
@Override public void format(final EditList edits, final RawText a, final RawText b) throws IOException { // Flush header before formatting of edits begin stream.flushLine(); super.format(edits, a, b); } }
/** * Format a patch script for one file entry. * * @param ent * the entry to be formatted. * @throws 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); }
if (sourcePrefix != null) diffFmt.setOldPrefix(sourcePrefix); diffFmt.format(result); diffFmt.flush(); return result;
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); }
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 ); }
private void showDiff(RevCommit c) throws IOException { final RevTree a = c.getParent(0).getTree(); final RevTree b = c.getTree(); if (showNameAndStatusOnly) Diff.nameStatus(outw, diffFmt.scan(a, b)); else { outw.flush(); diffFmt.format(a, b); diffFmt.flush(); } outw.println(); } }