use of org.eclipse.jgit.diff.DiffEntry in project winery by eclipse.
the class GitBasedRepository method hasChangesInFile.
@Override
public boolean hasChangesInFile(DefinitionsChildId id) {
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(id);
try (Git git = getGit()) {
if (!git.status().call().isClean()) {
List<DiffEntry> diffEntries = git.diff().call();
List<DiffEntry> entries = diffEntries.stream().filter(item -> item.getNewPath().startsWith(BackendUtils.getPathInsideRepo(ref.getParent()))).collect(Collectors.toList());
git.getRepository().close();
return entries.size() > 0;
}
} catch (GitAPIException e) {
LOGGER.trace(e.getMessage(), e);
} catch (IOException e) {
LOGGER.error("Error initializing Git.", e);
}
return false;
}
use of org.eclipse.jgit.diff.DiffEntry in project winery by eclipse.
the class GitBasedRepository method postEventMap.
public Map<DiffEntry, String> postEventMap() throws GitAPIException {
Map<DiffEntry, String> diffMap = new HashMap<>();
try (OutputStream stream = new ByteArrayOutputStream();
Git git = getGit()) {
List<DiffEntry> list = git.diff().setOutputStream(stream).call();
BufferedReader reader = new BufferedReader(new StringReader(stream.toString()));
String line = reader.readLine();
for (DiffEntry entry : list) {
StringWriter diff = new StringWriter();
if (line != null) {
do {
diff.append(line);
diff.write('\n');
line = reader.readLine();
} while (line != null && !line.startsWith("diff"));
}
diffMap.put(entry, diff.toString());
}
git.getRepository().close();
} catch (IOException exc) {
LOGGER.trace("Reading of git information failed!", exc);
} catch (JGitInternalException gitException) {
LOGGER.trace("Could not create Diff!", gitException);
}
this.eventBus.post(diffMap);
return diffMap;
}
use of org.eclipse.jgit.diff.DiffEntry in project sonarqube by SonarSource.
the class GitScmProvider method branchChangedFiles.
@CheckForNull
@Override
public Set<Path> branchChangedFiles(String targetBranchName, Path rootBaseDir) {
try (Repository repo = buildRepo(rootBaseDir)) {
Ref targetRef = resolveTargetRef(targetBranchName, repo);
if (targetRef == null) {
addWarningTargetNotFound(targetBranchName);
return null;
}
if (isDiffAlgoInvalid(repo.getConfig())) {
LOG.warn("The diff algorithm configured in git is not supported. " + "No information regarding changes in the branch will be collected, which can lead to unexpected results.");
return null;
}
Optional<RevCommit> mergeBaseCommit = findMergeBase(repo, targetRef);
if (!mergeBaseCommit.isPresent()) {
LOG.warn("No merge base found between HEAD and " + targetRef.getName());
return null;
}
AbstractTreeIterator mergeBaseTree = prepareTreeParser(repo, mergeBaseCommit.get());
// we compare a commit with HEAD, so no point ignoring line endings (it will be whatever is committed)
try (Git git = newGit(repo)) {
List<DiffEntry> diffEntries = git.diff().setShowNameAndStatusOnly(true).setOldTree(mergeBaseTree).setNewTree(prepareNewTree(repo)).call();
return diffEntries.stream().filter(diffEntry -> diffEntry.getChangeType() == DiffEntry.ChangeType.ADD || diffEntry.getChangeType() == DiffEntry.ChangeType.MODIFY).map(diffEntry -> repo.getWorkTree().toPath().resolve(diffEntry.getNewPath())).collect(Collectors.toSet());
}
} catch (IOException | GitAPIException e) {
LOG.warn(e.getMessage(), e);
}
return null;
}
use of org.eclipse.jgit.diff.DiffEntry in project OpenGrok by OpenGrok.
the class GitRepository method findOriginalName.
/**
* Get the name of file in given revision. The returned file name is relative to the repository root.
* Assumes renamed file hanndling is on.
*
* @param fullpath full file path
* @param changeset revision ID (could be short)
* @return original filename relative to the repository root
* @throws java.io.IOException if I/O exception occurred
* @see #getPathRelativeToCanonicalRepositoryRoot(String)
*/
String findOriginalName(String fullpath, String changeset) throws IOException {
if (fullpath == null || fullpath.isEmpty()) {
throw new IOException(String.format("Invalid file path string: %s", fullpath));
}
if (changeset == null || changeset.isEmpty()) {
throw new IOException(String.format("Invalid changeset string for path %s: %s", fullpath, changeset));
}
String fileInRepo = getGitFilePath(getPathRelativeToCanonicalRepositoryRoot(fullpath));
String originalFile = fileInRepo;
try (org.eclipse.jgit.lib.Repository repository = getJGitRepository(getDirectoryName());
RevWalk walk = new RevWalk(repository)) {
walk.markStart(walk.parseCommit(repository.resolve(Constants.HEAD)));
walk.markUninteresting(walk.lookupCommit(repository.resolve(changeset)));
Config config = repository.getConfig();
config.setBoolean("diff", null, "renames", true);
org.eclipse.jgit.diff.DiffConfig dc = config.get(org.eclipse.jgit.diff.DiffConfig.KEY);
FollowFilter followFilter = FollowFilter.create(getGitFilePath(fileInRepo), dc);
walk.setTreeFilter(followFilter);
for (RevCommit commit : walk) {
if (commit.getParentCount() > 1 && !isMergeCommitsEnabled()) {
continue;
}
if (commit.getId().getName().startsWith(changeset)) {
break;
}
if (commit.getParentCount() >= 1) {
OutputStream outputStream = NullOutputStream.INSTANCE;
try (DiffFormatter formatter = new DiffFormatter(outputStream)) {
formatter.setRepository(repository);
formatter.setDetectRenames(true);
List<DiffEntry> diffs = formatter.scan(prepareTreeParser(repository, commit.getParent(0)), prepareTreeParser(repository, commit));
for (DiffEntry diff : diffs) {
if (diff.getChangeType() == DiffEntry.ChangeType.RENAME && originalFile.equals(diff.getNewPath())) {
originalFile = diff.getOldPath();
}
}
}
}
}
}
if (originalFile == null) {
LOGGER.log(Level.WARNING, "Failed to get original name in revision {0} for: \"{1}\"", new Object[] { changeset, fullpath });
return null;
}
return getNativePath(originalFile);
}
use of org.eclipse.jgit.diff.DiffEntry in project omegat by omegat-org.
the class GITRemoteRepository2 method indexIsEmpty.
private boolean indexIsEmpty(DirCache dc) throws Exception {
DirCacheIterator dci = new DirCacheIterator(dc);
AbstractTreeIterator old = prepareTreeParser(repository, repository.resolve(Constants.HEAD));
try (Git git = new Git(repository)) {
List<DiffEntry> diffs = git.diff().setOldTree(old).setNewTree(dci).call();
return diffs.isEmpty();
}
}
Aggregations