List of usage examples for org.eclipse.jgit.diff DiffAlgorithm getAlgorithm
public static DiffAlgorithm getAlgorithm(SupportedAlgorithm alg)
From source file:MyDiffFormatter.java
License:Eclipse Distribution License
/** * Set the repository the formatter can load object contents from. * <p/>/*ww w . j av a 2 s . com*/ * Once a repository has been set, the formatter must be released to ensure * the internal ObjectReader is able to release its resources. * * @param repository source repository holding referenced objects. */ public void setRepository(Repository repository) { if (reader != null) reader.release(); db = repository; reader = db.newObjectReader(); diffCfg = db.getConfig().get(DiffConfig.KEY); ContentSource cs = ContentSource.create(reader); source = new ContentSource.Pair(cs, cs); DiffConfig dc = db.getConfig().get(DiffConfig.KEY); if (dc.isNoPrefix()) { setOldPrefix(""); //$NON-NLS-1$ setNewPrefix(""); //$NON-NLS-1$ } setDetectRenames(dc.isRenameDetectionEnabled()); diffAlgorithm = DiffAlgorithm.getAlgorithm(db.getConfig().getEnum(ConfigConstants.CONFIG_DIFF_SECTION, null, ConfigConstants.CONFIG_KEY_ALGORITHM, SupportedAlgorithm.HISTOGRAM)); }
From source file:com.github.checkstyle.parser.JgitUtils.java
License:Open Source License
/** * Generates the differences between the contents of the 2 files. * * @param baseFile/*from w ww. j av a2 s.c o m*/ * The base file to examine. * @param patchFile * The patch file to examine. * @return The iterator containing the differences. * @throws IOException * if Exceptions occur while reading the file. */ public static Iterator<JgitDifference> getDifferences(File baseFile, File patchFile) throws IOException { if (diffAlgorithm == null) { diffAlgorithm = DiffAlgorithm.getAlgorithm(SupportedAlgorithm.HISTOGRAM); } final RawText baseFileRaw = new RawText(baseFile); final RawText patchFileRaw = new RawText(patchFile); return new JgitDifferenceIterator(diffAlgorithm.diff(RawTextComparator.DEFAULT, baseFileRaw, patchFileRaw), baseFileRaw, patchFileRaw); }
From source file:com.itemis.maven.plugins.unleash.scm.providers.merge.UnleashGitMerger.java
License:Eclipse Distribution License
/** * @param local/*from w w w . ja va2 s. c o m*/ * @param inCore */ protected UnleashGitMerger(Repository local, boolean inCore, MergeClient mergeClient) { super(local); this.mergeClient = mergeClient; SupportedAlgorithm diffAlg = local.getConfig().getEnum(ConfigConstants.CONFIG_DIFF_SECTION, null, ConfigConstants.CONFIG_KEY_ALGORITHM, SupportedAlgorithm.HISTOGRAM); this.mergeAlgorithm = new MergeAlgorithm(DiffAlgorithm.getAlgorithm(diffAlg)); this.commitNames = new String[] { "BASE", "OURS", "THEIRS" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ this.inCore = inCore; if (inCore) { this.implicitDirCache = false; this.dircache = DirCache.newInCore(); } else { this.implicitDirCache = true; } }
From source file:jbenchmarker.trace.git.GitWalker.java
License:Open Source License
public GitWalker(String... args) throws IOException { try {/*from w w w .j a va2s .c o m*/ this.parser = new CmdLineParser(this); parser.parseArgument(args); if (help) { help(0); } } catch (CmdLineException ex) { System.err.println("Error in argument " + ex); help(-1); } Logger.getLogger("").setLevel(levels[logLevel.ordinal()]); builder = new FileRepositoryBuilder(); repository = builder.setGitDir(new File(gitDir + "/.git")).readEnvironment().findGitDir().build(); this.reader = repository.newObjectReader(); this.git = new Git(repository); this.source = ContentSource.create(reader); this.diffAlgorithm = DiffAlgorithm.getAlgorithm(DiffAlgorithm.SupportedAlgorithm.MYERS); this.fromCommit = repository.resolve(fromCommitStr); }
From source file:models.FileDiffTest.java
License:Apache License
@Test public void getHunks() throws IOException { // given/*from ww w.j av a 2 s.c om*/ FileDiff fileDiff = new FileDiff(); fileDiff.a = new RawText("apple\nbanana\ncat\n".getBytes()); fileDiff.b = new RawText("apple\nbanana\ncorn\n".getBytes()); DiffAlgorithm diffAlgorithm = DiffAlgorithm.getAlgorithm(DiffAlgorithm.SupportedAlgorithm.HISTOGRAM); fileDiff.editList = diffAlgorithm.diff(RawTextComparator.DEFAULT, fileDiff.a, fileDiff.b); // when List<Hunk> hunks = fileDiff.getHunks(); // then Hunk expectedHunk = new Hunk(); expectedHunk.beginA = 0; expectedHunk.endA = 3; expectedHunk.beginB = 0; expectedHunk.endB = 3; expectedHunk.lines = new ArrayList<>(); expectedHunk.lines.add(new DiffLine(fileDiff, DiffLineType.CONTEXT, 0, 0, "apple")); expectedHunk.lines.add(new DiffLine(fileDiff, DiffLineType.CONTEXT, 1, 1, "banana")); expectedHunk.lines.add(new DiffLine(fileDiff, DiffLineType.REMOVE, 2, null, "cat")); expectedHunk.lines.add(new DiffLine(fileDiff, DiffLineType.ADD, null, 2, "corn")); ArrayList<Hunk> expectedHunks = new ArrayList<>(); expectedHunks.add(expectedHunk); assertThat(hunks).describedAs("Test FileDiff.hunks").isEqualTo(expectedHunks); }
From source file:models.PullRequestTest.java
License:Apache License
@Test public void getDiff1() throws IOException { // given/*from w w w . ja v a 2 s . c o m*/ List<FileDiff> expected = new ArrayList<>(); expected.add(new FileDiff()); expected.get(0).commitA = baseCommit.getName(); expected.get(0).commitB = firstCommit.getName(); expected.get(0).a = new RawText("apple\nbanana\ncat\n".getBytes()); expected.get(0).b = new RawText("apple\nbanana\ncorn\n".getBytes()); expected.get(0).pathA = "test.txt"; expected.get(0).pathB = "test.txt"; expected.get(0).editList = DiffAlgorithm.getAlgorithm(DiffAlgorithm.SupportedAlgorithm.HISTOGRAM) .diff(RawTextComparator.DEFAULT, expected.get(0).a, expected.get(0).b); expected.get(0).changeType = DiffEntry.ChangeType.MODIFY; // when List<FileDiff> diff = pullRequest.getDiff(firstCommit.getName()); // then assertThat(diff).isEqualTo(expected); }
From source file:org.kuali.student.git.tools.GitExtractor.java
License:Educational Community License
public void extractDifference(String targetTagName, String copyFromTagName) throws MissingObjectException, IncorrectObjectTypeException, IOException { RevTree target = extractTag(targetTagName); RevTree copyFrom = extractTag(copyFromTagName); DiffFormatter formatter = new DiffFormatter(System.out); formatter.setRepository(repository); formatter.setDetectRenames(true);//from w w w . j a v a2s. co m formatter.setDiffAlgorithm(DiffAlgorithm.getAlgorithm(SupportedAlgorithm.MYERS)); formatter.setDiffComparator(RawTextComparator.WS_IGNORE_ALL); List<DiffEntry> results = formatter.scan(target, copyFrom); for (DiffEntry entry : results) { if (entry.getChangeType().equals(ChangeType.COPY)) { AbbreviatedObjectId copyFromBlobId = entry.getOldId(); // Cscore:md5:target-path:copy-from-path log.info(String.format("C%d:%s:%s:%s", entry.getScore(), "md5", entry.getNewPath(), entry.getOldPath())); } else if (entry.getChangeType().equals(ChangeType.MODIFY)) { // Cscore:md5:target-path:copy-from-path log.info(String.format("M%d:%s:%s:%s", entry.getScore(), "md5", entry.getNewPath(), entry.getOldPath())); } } }
From source file:org.sjanisch.skillview.git.GitContentDiffServices.java
License:Open Source License
private static ContentDiffService createService(SupportedAlgorithm diffAlgorithm) { return (previous, current) -> { DiffAlgorithm algorithm = DiffAlgorithm.getAlgorithm(diffAlgorithm); return new GitAlgorithmContentDiff(previous, current, algorithm); };//from ww w . jav a 2 s . c o m }
From source file:playRepository.GitRepository.java
License:Apache License
private static List<FileDiff> getFileDiffs(final Repository repositoryA, Repository repositoryB, ObjectId commitA, ObjectId commitB) throws IOException { class MultipleRepositoryObjectReader extends ObjectReader { Collection<ObjectReader> readers = new HashSet<>(); @Override/* ww w .j a va2 s. c om*/ public ObjectReader newReader() { return new MultipleRepositoryObjectReader(readers); } public MultipleRepositoryObjectReader(Collection<ObjectReader> readers) { this.readers = readers; } public MultipleRepositoryObjectReader() { this.readers = new HashSet<>(); } public void addObjectReader(ObjectReader reader) { this.readers.add(reader); } @Override public Collection<ObjectId> resolve(AbbreviatedObjectId id) throws IOException { Set<ObjectId> result = new HashSet<>(); for (ObjectReader reader : readers) { result.addAll(reader.resolve(id)); } return result; } @Override public ObjectLoader open(AnyObjectId objectId, int typeHint) throws IOException { for (ObjectReader reader : readers) { if (reader.has(objectId, typeHint)) { return reader.open(objectId, typeHint); } } return null; } @Override public Set<ObjectId> getShallowCommits() throws IOException { Set<ObjectId> union = new HashSet<>(); for (ObjectReader reader : readers) { union.addAll(reader.getShallowCommits()); } return union; } } final MultipleRepositoryObjectReader reader = new MultipleRepositoryObjectReader(); reader.addObjectReader(repositoryA.newObjectReader()); reader.addObjectReader(repositoryB.newObjectReader()); @SuppressWarnings("rawtypes") Repository fakeRepo = new Repository(new BaseRepositoryBuilder()) { @Override public void create(boolean bare) throws IOException { throw new UnsupportedOperationException(); } @Override public ObjectDatabase getObjectDatabase() { throw new UnsupportedOperationException(); } @Override public RefDatabase getRefDatabase() { throw new UnsupportedOperationException(); } @Override public StoredConfig getConfig() { return repositoryA.getConfig(); } @Override public void scanForRepoChanges() throws IOException { throw new UnsupportedOperationException(); } @Override public void notifyIndexChanged() { throw new UnsupportedOperationException(); } @Override public ReflogReader getReflogReader(String refName) throws IOException { throw new UnsupportedOperationException(); } public ObjectReader newObjectReader() { return reader; } }; DiffFormatter formatter = new DiffFormatter(NullOutputStream.INSTANCE); formatter.setRepository(fakeRepo); formatter.setDetectRenames(true); AbstractTreeIterator treeParserA, treeParserB; RevTree treeA = null, treeB = null; if (commitA != null) { treeA = new RevWalk(repositoryA).parseTree(commitA); treeParserA = new CanonicalTreeParser(); ((CanonicalTreeParser) treeParserA).reset(reader, treeA); } else { treeParserA = new EmptyTreeIterator(); } if (commitB != null) { treeB = new RevWalk(repositoryB).parseTree(commitB); treeParserB = new CanonicalTreeParser(); ((CanonicalTreeParser) treeParserB).reset(reader, treeB); } else { treeParserB = new EmptyTreeIterator(); } List<FileDiff> result = new ArrayList<>(); int size = 0; int lines = 0; for (DiffEntry diff : formatter.scan(treeParserA, treeParserB)) { FileDiff fileDiff = new FileDiff(); fileDiff.commitA = commitA != null ? commitA.getName() : null; fileDiff.commitB = commitB != null ? commitB.getName() : null; fileDiff.changeType = diff.getChangeType(); fileDiff.oldMode = diff.getOldMode(); fileDiff.newMode = diff.getNewMode(); String pathA = diff.getPath(DiffEntry.Side.OLD); String pathB = diff.getPath(DiffEntry.Side.NEW); byte[] rawA = null; if (treeA != null && Arrays.asList(DELETE, MODIFY, RENAME, COPY).contains(diff.getChangeType())) { TreeWalk t1 = TreeWalk.forPath(repositoryA, pathA, treeA); ObjectId blobA = t1.getObjectId(0); fileDiff.pathA = pathA; try { rawA = repositoryA.open(blobA).getBytes(); fileDiff.isBinaryA = RawText.isBinary(rawA); fileDiff.a = fileDiff.isBinaryA ? null : new RawText(rawA); } catch (org.eclipse.jgit.errors.LargeObjectException e) { fileDiff.addError(FileDiff.Error.A_SIZE_EXCEEDED); } } byte[] rawB = null; if (treeB != null && Arrays.asList(ADD, MODIFY, RENAME, COPY).contains(diff.getChangeType())) { TreeWalk t2 = TreeWalk.forPath(repositoryB, pathB, treeB); ObjectId blobB = t2.getObjectId(0); fileDiff.pathB = pathB; try { rawB = repositoryB.open(blobB).getBytes(); fileDiff.isBinaryB = RawText.isBinary(rawB); fileDiff.b = fileDiff.isBinaryB ? null : new RawText(rawB); } catch (org.eclipse.jgit.errors.LargeObjectException e) { fileDiff.addError(FileDiff.Error.B_SIZE_EXCEEDED); } } if (size > DIFF_SIZE_LIMIT || lines > DIFF_LINE_LIMIT) { fileDiff.addError(FileDiff.Error.OTHERS_SIZE_EXCEEDED); result.add(fileDiff); continue; } // Get diff if necessary if (fileDiff.a != null && fileDiff.b != null && !(fileDiff.isBinaryA || fileDiff.isBinaryB) && Arrays.asList(MODIFY, RENAME).contains(diff.getChangeType())) { DiffAlgorithm diffAlgorithm = DiffAlgorithm .getAlgorithm(repositoryB.getConfig().getEnum(ConfigConstants.CONFIG_DIFF_SECTION, null, ConfigConstants.CONFIG_KEY_ALGORITHM, DiffAlgorithm.SupportedAlgorithm.HISTOGRAM)); fileDiff.editList = diffAlgorithm.diff(RawTextComparator.DEFAULT, fileDiff.a, fileDiff.b); size += fileDiff.getHunks().size; lines += fileDiff.getHunks().lines; } // update lines and sizes if (fileDiff.b != null && !fileDiff.isBinaryB && diff.getChangeType().equals(ADD)) { lines += fileDiff.b.size(); size += rawB.length; } // update lines and sizes if (fileDiff.a != null && !fileDiff.isBinaryA && diff.getChangeType().equals(DELETE)) { lines += fileDiff.a.size(); size += rawA.length; } // Stop if exceeds the limit for total number of files if (result.size() > DIFF_FILE_LIMIT) { break; } result.add(fileDiff); } return result; }