use of org.eclipse.jgit.treewalk.CanonicalTreeParser in project che by eclipse.
the class JGitDiffPage method commitToWorkingTree.
/**
* Show changes between specified revision and working tree.
*
* @param commitId
* id of commit
* @param formatter
* diff formatter
* @return list of diff entries
* @throws IOException
* if any i/o errors occurs
*/
private List<DiffEntry> commitToWorkingTree(String commitId, DiffFormatter formatter) throws IOException {
ObjectId commitA = repository.resolve(commitId);
if (commitA == null) {
File heads = new File(repository.getWorkTree().getPath() + "/.git/refs/heads");
if (heads.exists() && heads.list().length == 0) {
return Collections.emptyList();
}
throw new IllegalArgumentException("Invalid commit id " + commitId);
}
RevTree treeA;
try (RevWalk revWalkA = new RevWalk(repository)) {
treeA = revWalkA.parseTree(commitA);
}
List<DiffEntry> diff;
try (ObjectReader reader = repository.newObjectReader()) {
CanonicalTreeParser iterA = new CanonicalTreeParser();
iterA.reset(reader, treeA);
FileTreeIterator iterB = new FileTreeIterator(repository);
// Seems bug in DiffFormatter when work with working. Disable detect
// renames by formatter and do it later.
formatter.setDetectRenames(false);
diff = formatter.scan(iterA, iterB);
if (!params.isNoRenames()) {
// Detect renames.
RenameDetector renameDetector = createRenameDetector();
ContentSource.Pair sourcePairReader = new ContentSource.Pair(ContentSource.create(reader), ContentSource.create(iterB));
renameDetector.addAll(diff);
diff = renameDetector.compute(sourcePairReader, NullProgressMonitor.INSTANCE);
}
}
return diff;
}
use of org.eclipse.jgit.treewalk.CanonicalTreeParser in project che by eclipse.
the class JGitDiffPage method emptyToCommit.
/**
* Show changes between specified revision and empty tree.
*
* @param commitId
* id of commit
* @param formatter
* diff formatter
* @return list of diff entries
* @throws IOException
* if any i/o errors occurs
*/
private List<DiffEntry> emptyToCommit(String commitId, DiffFormatter formatter) throws IOException {
ObjectId commit = repository.resolve(commitId);
checkArgument(commit != null, "Invalid commit id " + commitId);
RevTree tree;
try (RevWalk revWalkA = new RevWalk(repository)) {
tree = revWalkA.parseTree(commit);
}
List<DiffEntry> diff;
try (ObjectReader reader = repository.newObjectReader()) {
CanonicalTreeParser iterator = new CanonicalTreeParser();
iterator.reset(reader, tree);
diff = formatter.scan(new EmptyTreeIterator(), iterator);
}
return diff;
}
use of org.eclipse.jgit.treewalk.CanonicalTreeParser in project gitblit by gitblit.
the class JGitUtils method getTreeEntries.
/**
* Returns all tree entries that do not match the ignore paths.
*
* @param db
* @param ignorePaths
* @param dcBuilder
* @throws IOException
*/
public static List<DirCacheEntry> getTreeEntries(Repository db, String branch, Collection<String> ignorePaths) throws IOException {
List<DirCacheEntry> list = new ArrayList<DirCacheEntry>();
TreeWalk tw = null;
try {
ObjectId treeId = db.resolve(branch + "^{tree}");
if (treeId == null) {
// branch does not exist yet
return list;
}
tw = new TreeWalk(db);
int hIdx = tw.addTree(treeId);
tw.setRecursive(true);
while (tw.next()) {
String path = tw.getPathString();
CanonicalTreeParser hTree = null;
if (hIdx != -1) {
hTree = tw.getTree(hIdx, CanonicalTreeParser.class);
}
if (!ignorePaths.contains(path)) {
// add all other tree entries
if (hTree != null) {
final DirCacheEntry entry = new DirCacheEntry(path);
entry.setObjectId(hTree.getEntryObjectId());
entry.setFileMode(hTree.getEntryFileMode());
list.add(entry);
}
}
}
} finally {
if (tw != null) {
tw.close();
}
}
return list;
}
use of org.eclipse.jgit.treewalk.CanonicalTreeParser in project gocd by gocd.
the class ConfigRepository method contentFromTree.
private String contentFromTree(RevTree tree) {
try {
final ObjectReader reader = gitRepo.newObjectReader();
CanonicalTreeParser parser = new CanonicalTreeParser();
parser.reset(reader, tree);
String lastPath = null;
while (true) {
final String path = parser.getEntryPathString();
parser = parser.next();
if (path.equals(lastPath)) {
break;
}
lastPath = path;
if (path.equals(CRUISE_CONFIG_XML)) {
final ObjectId id = parser.getEntryObjectId();
final ObjectLoader loader = reader.open(id);
return new String(loader.getBytes());
}
}
return null;
} catch (IOException e) {
LOGGER.error("Could not fetch content from the config repository found at path '{}'", workingDir.getAbsolutePath(), e);
throw new RuntimeException("Error while fetching content from the config repository.", e);
}
}
use of org.eclipse.jgit.treewalk.CanonicalTreeParser in project gitiles by GerritCodeReview.
the class PathServlet method getOnlyChildSubtree.
private CanonicalTreeParser getOnlyChildSubtree(ObjectReader reader, ObjectId id, byte[] prefix) throws IOException {
CanonicalTreeParser p = new CanonicalTreeParser(prefix, reader, id);
if (p.eof() || p.getEntryFileMode() != FileMode.TREE) {
return null;
}
p.next(1);
return p.eof() ? p : null;
}
Aggregations