@Signature public Memory diff(ArrayMemory settings) throws GitAPIException { DiffCommand command = getWrappedObject().diff(); if (settings != null) { command.setCached(settings.valueOfIndex("cached").toBoolean()); Memory contextLines = settings.valueOfIndex("contextLines"); if (contextLines.isNotNull()) { command.setContextLines(contextLines.toInteger()); } Memory destPrefix = settings.valueOfIndex("destPrefix"); if (destPrefix.isNotNull()) { command.setDestinationPrefix(destPrefix.toString()); } Memory sourcePrefix = settings.valueOfIndex("sourcePrefix"); if (sourcePrefix.isNotNull()) { command.setSourcePrefix(sourcePrefix.toString()); } command.setShowNameAndStatusOnly(settings.valueOfIndex("showNameAndStatusOnly").toBoolean()); Memory pathFilter = settings.valueOfIndex("pathFilter"); if (pathFilter.isNotNull()) { command.setPathFilter(PathFilter.create(pathFilter.toString())); } } List<DiffEntry> call = command.call(); return GitUtils.valueOfDiffEntries(call); }
private static @NonNull DiffEntry diffFile(Repository repo, String oldCommit, String newCommit, String path) throws IOException, GitAPIException { Config config = new Config(); config.setBoolean("diff", null, "renames", true); DiffConfig diffConfig = config.get(DiffConfig.KEY); try (Git git = new Git(repo)) { List<DiffEntry> diffList = git.diff(). setOldTree(prepareTreeParser(repo, oldCommit)). setNewTree(prepareTreeParser(repo, newCommit)). setPathFilter(FollowFilter.create(path, diffConfig)). call(); if (diffList.size() == 0) return null; if (diffList.size() > 1) throw new RuntimeException("invalid diff"); return diffList.get(0); } } }
private static @NonNull DiffEntry diffFile(Repository repo, String oldCommit, String newCommit, String path) throws IOException, GitAPIException { Config config = new Config(); config.setBoolean("diff", null, "renames", true); DiffConfig diffConfig = config.get(DiffConfig.KEY); try (Git git = new Git(repo)) { List<DiffEntry> diffList = git.diff(). setOldTree(prepareTreeParser(repo, oldCommit)). setNewTree(prepareTreeParser(repo, newCommit)). setPathFilter(FollowFilter.create(path, diffConfig)). call(); if (diffList.size() == 0) return null; if (diffList.size() > 1) throw new RuntimeException("invalid diff"); return diffList.get(0); } } }
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); } } } } }
/** * Check path vs repository */ public boolean isDiff(String path) throws Exception { return !git.diff().setPathFilter(PathFilter.create(path)).call().isEmpty(); }
.setOutputStream(computer.receiver()) .setOldTree(prepareTreeParser(repo, targetRef)) .setPathFilter(PathFilter.create(toGitPath(repoRootDir.relativize(path).toString()))) .call();
/** * Get the diff of a file against a particular commit * * @param jobId The job config to diff * @param commitId If specified, the commit to compare against; otherwise, compare against the latest revision * @return A string description of the diff, comparable to the output of "git diff filename" * @throws GitAPIException If there is a problem fetching the diff from git * @throws IOException If there is a problem reading from the file */ public String getDiff(String jobId, String commitId) throws GitAPIException, IOException { OutputStream out = new ByteArrayOutputStream(); DiffCommand diff = git.diff() .setPathFilter(PathFilter.create(getPathForJobId(jobId))) .setOutputStream(out) .setSourcePrefix("old:") .setDestinationPrefix("new:"); if (commitId != null) { diff.setOldTree(getTreeIterator(commitId)); diff.setNewTree(getTreeIterator("HEAD")); } diff.call(); return out.toString(); }
DiffCommand dc = git.diff().setNewTree(newTreeIter); if (path != null) dc.setPathFilter(PathFilter.create(path)); dc.setShowNameAndStatusOnly(true); for (DiffEntry diff : dc.call()) {