use of org.eclipse.jgit.diff.DiffEntry in project zeppelin by apache.
the class GitNotebookRepoTest method initNonemptyNotebookDir.
@Test
public void initNonemptyNotebookDir() throws IOException, GitAPIException {
// given - .git does not exit
File dotGit = new File(String.join(File.separator, notebooksDir, ".git"));
assertFalse(dotGit.exists());
// when
notebookRepo = new GitNotebookRepo(conf);
// then
Git git = notebookRepo.getGit();
assertNotNull(git);
assertTrue(dotGit.exists());
assertFalse(notebookRepo.list(null).isEmpty());
List<DiffEntry> diff = git.diff().call();
// no commit, diff isn't empty
assertFalse(diff.isEmpty());
}
use of org.eclipse.jgit.diff.DiffEntry in project gerrit by GerritCodeReview.
the class DiffOperationsImpl method loadModifiedFilesWithoutCache.
/**
* Loads the modified file paths between two commits without inspecting the diff cache.
*/
private static Map<String, ModifiedFile> loadModifiedFilesWithoutCache(Project.NameKey project, DiffParameters diffParams, RevWalk revWalk, Config repoConfig) throws DiffNotAvailableException {
ObjectId newCommit = diffParams.newCommit();
ObjectId oldCommit = diffParams.baseCommit();
try {
ObjectReader reader = revWalk.getObjectReader();
List<DiffEntry> diffEntries;
try (DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE)) {
df.setReader(reader, repoConfig);
df.setDetectRenames(false);
diffEntries = df.scan(oldCommit.equals(ObjectId.zeroId()) ? null : oldCommit, newCommit);
}
List<ModifiedFile> modifiedFiles = diffEntries.stream().map(entry -> ModifiedFile.builder().changeType(toChangeType(entry.getChangeType())).oldPath(getGitPath(entry.getOldPath())).newPath(getGitPath(entry.getNewPath())).build()).collect(Collectors.toList());
return DiffUtil.mergeRewrittenModifiedFiles(modifiedFiles).stream().collect(ImmutableMap.toImmutableMap(ModifiedFile::getDefaultPath, Function.identity()));
} catch (IOException e) {
throw new DiffNotAvailableException(String.format("Failed to compute the modified files for project '%s'," + " old commit '%s', new commit '%s'.", project, oldCommit.name(), newCommit.name()), e);
}
}
use of org.eclipse.jgit.diff.DiffEntry in project gerrit by GerritCodeReview.
the class PRED_commit_delta_4 method exec.
@Override
public Operation exec(Prolog engine) throws PrologException {
engine.cont = cont;
engine.setB0();
Term a1 = arg1.dereference();
if (a1 instanceof VariableTerm) {
throw new PInstantiationException(this, 1);
}
if (!(a1 instanceof SymbolTerm)) {
throw new IllegalTypeException(this, 1, "symbol", a1);
}
Pattern regex = Pattern.compile(a1.name());
engine.r1 = new JavaObjectTerm(regex);
engine.r2 = arg2;
engine.r3 = arg3;
engine.r4 = arg4;
Repository repository = StoredValues.REPOSITORY.get(engine);
try (DiffFormatter diffFormatter = new DiffFormatter(DisabledOutputStream.INSTANCE)) {
diffFormatter.setRepository(repository);
// Do not detect renames; that would require reading file contents, which is slow for large
// files.
RevCommit commit = StoredValues.COMMIT.get(engine);
List<DiffEntry> diffEntries = diffFormatter.scan(// parent #0 is always the right choice, if it exists.
commit.getParentCount() > 0 ? commit.getParent(0) : null, commit);
engine.r5 = new JavaObjectTerm(diffEntries.iterator());
} catch (IOException e) {
throw new JavaException(e);
}
return engine.jtry5(commit_delta_check, commit_delta_next);
}
use of org.eclipse.jgit.diff.DiffEntry in project chuidiang-ejemplos by chuidiang.
the class GitAnalyzer method analyze.
public void analyze(HtmlFormat format, String issuePattern, String issueName) throws IOException {
try (RevWalk walk = new RevWalk(repository)) {
RevFilter filter = MessageRevFilter.create(issuePattern);
walk.setRevFilter(filter);
walk.markStart(walk.parseCommit(repository.resolve("HEAD")));
Iterator<RevCommit> iterator = walk.iterator();
if (!iterator.hasNext()) {
return;
}
format.addIssue(issueName);
while (iterator.hasNext()) {
RevCommit commit = iterator.next();
String commitNumber = commit.getId().getName();
Date date = commit.getAuthorIdent().getWhen();
String author = commit.getAuthorIdent().getName();
String comment = commit.getFullMessage();
ObjectId objectId = commit.getTree().getId();
RevCommit parent = commit.getParent(0);
PatchIdDiffFormatter formatter = new PatchIdDiffFormatter();
formatter.setRepository(repository);
ArrayList<String> files = new ArrayList<>();
try {
List<DiffEntry> entries = formatter.scan(parent, objectId);
entries.forEach((entry) -> {
try {
FileHeader header = formatter.toFileHeader(entry);
files.add(header.getChangeType() + " " + getPath(header));
} catch (IOException e) {
e.printStackTrace();
}
});
} catch (Exception e) {
e.printStackTrace();
}
format.addChange(commitNumber, date, author, comment, files.toArray(new String[] {}));
}
format.endIssue();
} catch (Exception e) {
format.endIssue();
}
}
use of org.eclipse.jgit.diff.DiffEntry in project sonarqube by SonarSource.
the class GitScmProvider method collectChangedLines.
private void collectChangedLines(Repository repo, RevCommit mergeBaseCommit, Map<Path, Set<Integer>> changedLines, Path changedFile) {
ChangedLinesComputer computer = new ChangedLinesComputer();
try (DiffFormatter diffFmt = new DiffFormatter(new BufferedOutputStream(computer.receiver()))) {
// copied from DiffCommand so that we can use a custom DiffFormatter which ignores white spaces.
diffFmt.setRepository(repo);
diffFmt.setProgressMonitor(NullProgressMonitor.INSTANCE);
diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_ALL);
Path workTree = repo.getWorkTree().toPath();
String relativePath = workTree.relativize(changedFile).toString();
PathFilter pathFilter = PathFilter.create(toGitPath(relativePath));
diffFmt.setPathFilter(pathFilter);
AbstractTreeIterator mergeBaseTree = prepareTreeParser(repo, mergeBaseCommit);
List<DiffEntry> diffEntries = diffFmt.scan(mergeBaseTree, new FileTreeIterator(repo));
diffFmt.format(diffEntries);
diffFmt.flush();
diffEntries.stream().filter(diffEntry -> diffEntry.getChangeType() == DiffEntry.ChangeType.ADD || diffEntry.getChangeType() == DiffEntry.ChangeType.MODIFY).findAny().ifPresent(diffEntry -> changedLines.put(changedFile, computer.changedLines()));
} catch (Exception e) {
LOG.warn("Failed to get changed lines from git for file " + changedFile, e);
}
}
Aggregations