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 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); } }
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); } } } } }
public List<DiffEntry> call() throws GitAPIException { try (DiffFormatter diffFmt = getDiffFormatter()) { diffFmt.setRepository(repo); diffFmt.setProgressMonitor(monitor); if (cached) {
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; }
try( DiffFormatter diffFormatter = new DiffFormatter( DisabledOutputStream.INSTANCE ) ) { diffFormatter.setRepository( git.getRepository() ); List<DiffEntry> diffEntries = diffFormatter.scan( oldTreeIterator, newTreeIterator ); FileHeader fileHeader = diffFormatter.toFileHeader( diffEntries.get( 0 ) ); return fileHeader.toEditList(); }
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); } }
/** * 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); } }
ObjectReader reader = git.getRepository().newObjectReader(); CanonicalTreeParser oldTreeIter = new CanonicalTreeParser(); ObjectId oldTree = git.getRepository().resolve( "HEAD^{tree}" ); oldTreeIter.reset( reader, oldTree ); CanonicalTreeParser newTreeIter = new CanonicalTreeParser(); ObjectId newTree = git.getRepository().resolve( "HEAD~1^{tree}" ); newTreeIter.reset( reader, newTree ); DiffFormatter diffFormatter = new DiffFormatter( DisabledOutputStream.INSTANCE ); diffFormatter.setRepository( git.getRepository() ); List<DiffEntry> entries = diffFormatter.scan( oldTreeIter, newTreeIter ); for( DiffEntry entry : entries ) { System.out.println( entry.getChangeType() ); }
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()); } }
@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); } }