use of org.eclipse.jgit.diff.DiffConfig in project egit by eclipse.
the class CommitSelectionDialog method createTreeFilter.
private TreeFilter createTreeFilter() {
if (filterResources == null)
return TreeFilter.ALL;
List<TreeFilter> filters = new ArrayList<>();
for (IResource resource : filterResources) {
RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
if (mapping != null) {
DiffConfig diffConfig = mapping.getRepository().getConfig().get(DiffConfig.KEY);
String path = mapping.getRepoRelativePath(resource);
if (path != null && !"".equals(path)) {
// $NON-NLS-1$
if (resource.getType() == IResource.FILE)
filters.add(FollowFilter.create(path, diffConfig));
else
filters.add(AndTreeFilter.create(PathFilter.create(path), TreeFilter.ANY_DIFF));
}
}
}
if (filters.isEmpty())
return TreeFilter.ALL;
else if (filters.size() == 1)
return filters.get(0);
else
return OrTreeFilter.create(filters);
}
use of org.eclipse.jgit.diff.DiffConfig in project egit by eclipse.
the class GitHistoryPage method createFollowFilterFor.
/**
* Creates a filter for the given files, will make sure that renames/copies
* of all files will be followed.
* @param paths the list of files to follow, must not be <code>null</code> or empty
* @return the ORed list of {@link FollowFilter follow filters}
*/
private TreeFilter createFollowFilterFor(List<String> paths) {
if (paths == null || paths.isEmpty())
// $NON-NLS-1$
throw new IllegalArgumentException("paths must not be null nor empty");
DiffConfig diffConfig = currentRepo.getConfig().get(DiffConfig.KEY);
List<TreeFilter> followFilters = new ArrayList<>(paths.size());
for (String path : paths) followFilters.add(createFollowFilter(path, diffConfig));
if (followFilters.size() == 1) {
FollowFilter followFilter = (FollowFilter) followFilters.get(0);
renameTracker.reset(followFilter.getPath());
return followFilters.get(0);
}
// FollowFilter for rename detection.
return OrTreeFilter.create(followFilters);
}
use of org.eclipse.jgit.diff.DiffConfig in project egit by eclipse.
the class GitDocument method populate.
void populate() throws IOException {
if (GitTraceLocation.QUICKDIFF.isActive())
GitTraceLocation.getTrace().traceEntry(GitTraceLocation.QUICKDIFF.getLocation(), resource);
// Do not populate if already disposed
if (disposed)
return;
RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
if (mapping == null) {
// $NON-NLS-1$
setResolved(null, null, null, "");
return;
}
final String gitPath = mapping.getRepoRelativePath(resource);
if (gitPath == null) {
// $NON-NLS-1$
setResolved(null, null, null, "");
return;
}
final Repository repository = mapping.getRepository();
String baseline = GitQuickDiffProvider.baseline.get(repository);
if (baseline == null)
baseline = Constants.HEAD;
ObjectId commitId = repository.resolve(baseline);
if (commitId != null) {
if (commitId.equals(lastCommit)) {
if (GitTraceLocation.QUICKDIFF.isActive())
GitTraceLocation.getTrace().trace(GitTraceLocation.QUICKDIFF.getLocation(), // $NON-NLS-1$
"(GitDocument) already resolved");
return;
}
} else {
if (repository.exactRef(Constants.HEAD) == null) {
// Complain only if not an unborn branch
String msg = NLS.bind(UIText.GitDocument_errorResolveQuickdiff, new Object[] { baseline, resource, repository });
Activator.logError(msg, new Throwable());
}
// $NON-NLS-1$
setResolved(null, null, null, "");
return;
}
RevCommit baselineCommit;
String oldPath = gitPath;
try (RevWalk rw = new RevWalk(repository);
ObjectReader reader = repository.newObjectReader()) {
baselineCommit = rw.parseCommit(commitId);
DiffConfig diffConfig = repository.getConfig().get(DiffConfig.KEY);
if (diffConfig.getRenameDetectionType() != RenameDetectionType.FALSE) {
TreeWalk walk = new TreeWalk(repository);
CanonicalTreeParser baseLineIterator = new CanonicalTreeParser();
baseLineIterator.reset(reader, baselineCommit.getTree());
walk.addTree(baseLineIterator);
walk.addTree(new DirCacheIterator(repository.readDirCache()));
List<DiffEntry> diffs = DiffEntry.scan(walk, true);
RenameDetector renameDetector = new RenameDetector(repository);
renameDetector.addAll(diffs);
List<DiffEntry> renames = renameDetector.compute();
for (DiffEntry e : renames) {
if (e.getNewPath().equals(gitPath)) {
oldPath = e.getOldPath();
break;
}
}
}
} catch (IOException err) {
String msg = NLS.bind(UIText.GitDocument_errorLoadCommit, new Object[] { commitId, baseline, resource, repository });
Activator.logError(msg, err);
// $NON-NLS-1$
setResolved(null, null, null, "");
return;
}
RevTree treeId = baselineCommit.getTree();
if (treeId.equals(lastTree)) {
if (GitTraceLocation.QUICKDIFF.isActive())
GitTraceLocation.getTrace().trace(GitTraceLocation.QUICKDIFF.getLocation(), // $NON-NLS-1$
"(GitDocument) already resolved");
return;
}
try (TreeWalk tw = TreeWalk.forPath(repository, oldPath, treeId)) {
if (tw == null) {
if (GitTraceLocation.QUICKDIFF.isActive())
GitTraceLocation.getTrace().trace(GitTraceLocation.QUICKDIFF.getLocation(), // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
"(GitDocument) resource " + resource + " not found in " + treeId + " in " + repository + ", baseline=" + baseline);
// $NON-NLS-1$
setResolved(null, null, null, "");
return;
}
ObjectId id = tw.getObjectId(0);
if (id.equals(ObjectId.zeroId())) {
// $NON-NLS-1$
setResolved(null, null, null, "");
String msg = NLS.bind(UIText.GitDocument_errorLoadTree, new Object[] { treeId.getName(), baseline, resource, repository });
Activator.logError(msg, new Throwable());
// $NON-NLS-1$
setResolved(null, null, null, "");
return;
}
if (!id.equals(lastBlob)) {
if (GitTraceLocation.QUICKDIFF.isActive())
GitTraceLocation.getTrace().trace(GitTraceLocation.QUICKDIFF.getLocation(), // $NON-NLS-1$
"(GitDocument) compareTo: " + baseline);
ObjectLoader loader = repository.open(id, Constants.OBJ_BLOB);
byte[] bytes = loader.getBytes();
String charset;
charset = CompareCoreUtils.getResourceEncoding(resource);
// Finally we could consider validating the content with respect
// to the content. We don't do that here.
String s = new String(bytes, charset);
setResolved(commitId, treeId, id, s);
if (GitTraceLocation.QUICKDIFF.isActive())
GitTraceLocation.getTrace().trace(GitTraceLocation.QUICKDIFF.getLocation(), // $NON-NLS-1$ //$NON-NLS-2$
"(GitDocument) has reference doc, size=" + s.length() + " bytes");
} else {
if (GitTraceLocation.QUICKDIFF.isActive())
GitTraceLocation.getTrace().trace(GitTraceLocation.QUICKDIFF.getLocation(), // $NON-NLS-1$
"(GitDocument) already resolved");
}
} finally {
if (GitTraceLocation.QUICKDIFF.isActive()) {
GitTraceLocation.getTrace().traceExit(GitTraceLocation.QUICKDIFF.getLocation());
}
}
}
use of org.eclipse.jgit.diff.DiffConfig in project egit by eclipse.
the class RepositoryActionHandler method getHeadCommit.
protected RevCommit getHeadCommit(IResource resource) throws IOException {
Repository repository = getRepository();
if (resource == null) {
return null;
}
RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
if (mapping == null) {
return null;
}
String path = mapping.getRepoRelativePath(resource);
if (path == null) {
return null;
}
try (RevWalk rw = new RevWalk(repository)) {
rw.sort(RevSort.COMMIT_TIME_DESC, true);
rw.sort(RevSort.BOUNDARY, true);
if (path.length() > 0) {
DiffConfig diffConfig = repository.getConfig().get(DiffConfig.KEY);
FollowFilter filter = FollowFilter.create(path, diffConfig);
rw.setTreeFilter(filter);
}
Ref head = repository.findRef(Constants.HEAD);
if (head == null) {
return null;
}
RevCommit headCommit = rw.parseCommit(head.getObjectId());
rw.close();
return headCommit;
}
}
use of org.eclipse.jgit.diff.DiffConfig in project egit by eclipse.
the class RepositoryActionHandler method findPreviousCommits.
/**
* Returns the previous commit of the given resources.
*
* @param resources
* The {@link IResource} for which the previous commit shall be
* determined.
* @return The second to last commit which touched any of the given
* resources.
* @throws IOException
* When the commit can not be parsed.
*/
protected List<RevCommit> findPreviousCommits(Collection<IResource> resources) throws IOException {
List<RevCommit> result = new ArrayList<>();
Repository repository = getRepository();
RepositoryMapping mapping = RepositoryMapping.getMapping(resources.iterator().next().getProject());
if (mapping == null) {
return result;
}
try (RevWalk rw = new RevWalk(repository)) {
rw.sort(RevSort.COMMIT_TIME_DESC, true);
rw.sort(RevSort.BOUNDARY, true);
List<TreeFilter> filters = new ArrayList<>();
DiffConfig diffConfig = repository.getConfig().get(DiffConfig.KEY);
for (IResource resource : resources) {
String path = mapping.getRepoRelativePath(resource);
if (path != null && path.length() > 0) {
filters.add(FollowFilter.create(path, diffConfig));
}
}
if (filters.size() >= 2) {
TreeFilter filter = OrTreeFilter.create(filters);
rw.setTreeFilter(filter);
} else if (filters.size() == 1) {
rw.setTreeFilter(filters.get(0));
}
Ref head = repository.findRef(Constants.HEAD);
if (head == null) {
return result;
}
RevCommit headCommit = rw.parseCommit(head.getObjectId());
rw.markStart(headCommit);
headCommit = rw.next();
if (headCommit == null)
return result;
List<RevCommit> directParents = Arrays.asList(headCommit.getParents());
RevCommit previousCommit = rw.next();
while (previousCommit != null && result.size() < directParents.size()) {
if (directParents.contains(previousCommit)) {
result.add(previousCommit);
}
previousCommit = rw.next();
}
rw.dispose();
}
return result;
}
Aggregations