use of org.eclipse.jgit.revwalk.RevTree in project OpenGrok by OpenGrok.
the class GitRepository method prepareTreeParser.
private static AbstractTreeIterator prepareTreeParser(org.eclipse.jgit.lib.Repository repository, RevCommit commit) throws IOException {
// from the commit we can build the tree which allows us to construct the TreeParser
try (RevWalk walk = new RevWalk(repository)) {
RevTree tree = walk.parseTree(commit.getTree().getId());
CanonicalTreeParser treeParser = new CanonicalTreeParser();
try (ObjectReader reader = repository.newObjectReader()) {
treeParser.reset(reader, tree.getId());
}
walk.dispose();
return treeParser;
}
}
use of org.eclipse.jgit.revwalk.RevTree in project OpenGrok by OpenGrok.
the class GitRepository method getHistoryRev.
/**
* Try to get file contents for given revision.
*
* @param out a required OutputStream
* @param fullpath full pathname of the file
* @param rev revision string
* @return a defined instance with {@code success} == {@code true} if no
* error occurred and with non-zero {@code iterations} if some data was transferred
*/
private HistoryRevResult getHistoryRev(OutputStream out, String fullpath, String rev) {
HistoryRevResult result = new HistoryRevResult();
File directory = new File(getDirectoryName());
String filename;
result.success = false;
try {
filename = getGitFilePath(Paths.get(getCanonicalDirectoryName()).relativize(Paths.get(fullpath)).toString());
} catch (IOException e) {
LOGGER.log(Level.WARNING, String.format("Failed to relativize '%s' in for repository '%s'", fullpath, directory), e);
return result;
}
try (org.eclipse.jgit.lib.Repository repository = getJGitRepository(directory.getAbsolutePath())) {
ObjectId commitId = repository.resolve(rev);
// A RevWalk allows walking over commits based on some filtering that is defined.
try (RevWalk revWalk = new RevWalk(repository)) {
RevCommit commit = revWalk.parseCommit(commitId);
// and using commit's tree find the path
RevTree tree = commit.getTree();
// Now try to find a specific file.
try (TreeWalk treeWalk = new TreeWalk(repository)) {
treeWalk.addTree(tree);
treeWalk.setRecursive(true);
treeWalk.setFilter(PathFilter.create(filename));
if (!treeWalk.next()) {
LOGGER.log(Level.FINEST, "Did not find expected file ''{0}'' in revision {1} " + "for repository ''{2}''", new Object[] { filename, rev, directory });
return result;
}
ObjectId objectId = treeWalk.getObjectId(0);
ObjectLoader loader = repository.open(objectId);
CountingOutputStream countingOutputStream = new CountingOutputStream(out);
loader.copyTo(countingOutputStream);
result.iterations = countingOutputStream.getCount();
result.success = true;
}
revWalk.dispose();
}
} catch (IOException e) {
LOGGER.log(Level.WARNING, String.format("Failed to get file '%s' in revision %s for repository '%s'", filename, rev, directory), e);
}
return result;
}
use of org.eclipse.jgit.revwalk.RevTree in project egit by eclipse.
the class ExistingOrNewPage method fillTreeItemWithGitDirectory.
private void fillTreeItemWithGitDirectory(RepositoryMapping m, TreeItem treeItem, IPath gitDir, boolean isAlternative) {
if (m.getGitDir() == null)
treeItem.setText(2, UIText.ExistingOrNewPage_SymbolicValueEmptyMapping);
else {
IPath relativePath = new Path(m.getGitDir());
if (isAlternative) {
IPath withoutLastSegment = relativePath.removeLastSegments(1);
IPath path;
if (withoutLastSegment.isEmpty())
// $NON-NLS-1$
path = Path.fromPortableString(".");
else
path = withoutLastSegment;
treeItem.setText(0, path.toString());
}
treeItem.setText(2, relativePath.toOSString());
try {
IProject project = m.getContainer().getProject();
Repository repo = new RepositoryBuilder().setGitDir(gitDir.toFile()).build();
File workTree = repo.getWorkTree();
IPath workTreePath = Path.fromOSString(workTree.getAbsolutePath());
if (workTreePath.isPrefixOf(project.getLocation())) {
IPath makeRelativeTo = project.getLocation().makeRelativeTo(workTreePath);
String repoRelativePath = makeRelativeTo.append("/.project").toPortableString();
ObjectId headCommitId = repo.resolve(Constants.HEAD);
if (headCommitId != null) {
// Not an empty repo
try (RevWalk revWalk = new RevWalk(repo)) {
RevCommit headCommit = revWalk.parseCommit(headCommitId);
RevTree headTree = headCommit.getTree();
TreeWalk projectInRepo = TreeWalk.forPath(repo, repoRelativePath, headTree);
if (projectInRepo != null) {
// the .project file is tracked by this repo
treeItem.setChecked(true);
}
}
}
}
repo.close();
} catch (IOException e1) {
Activator.logError(UIText.ExistingOrNewPage_FailedToDetectRepositoryMessage, e1);
}
}
}
use of org.eclipse.jgit.revwalk.RevTree in project egit by eclipse.
the class RemoveCommand method isTracked.
private boolean isTracked(File file, Repository repo) throws IOException {
ObjectId objectId = repo.resolve(Constants.HEAD);
RevTree tree;
try (RevWalk rw = new RevWalk(repo);
TreeWalk treeWalk = new TreeWalk(repo)) {
if (objectId != null)
tree = rw.parseTree(objectId);
else
tree = null;
treeWalk.setRecursive(true);
if (tree != null)
treeWalk.addTree(tree);
else
treeWalk.addTree(new EmptyTreeIterator());
treeWalk.addTree(new DirCacheIterator(repo.readDirCache()));
treeWalk.setFilter(PathFilterGroup.createFromStrings(Collections.singleton(Repository.stripWorkDir(repo.getWorkTree(), file))));
return treeWalk.next();
}
}
use of org.eclipse.jgit.revwalk.RevTree in project gerrit by GerritCodeReview.
the class VersionedMetaDataOnInit method save.
protected void save(PersonIdent ident, String msg) throws IOException, ConfigInvalidException {
File path = getPath();
if (path == null) {
throw new IOException(project + " does not exist.");
}
try (Repository repo = new FileRepository(path);
ObjectInserter i = repo.newObjectInserter();
ObjectReader r = repo.newObjectReader();
RevWalk rw = new RevWalk(r)) {
inserter = i;
reader = r;
RevTree srcTree = revision != null ? rw.parseTree(revision) : null;
newTree = readTree(srcTree);
CommitBuilder commit = new CommitBuilder();
commit.setAuthor(ident);
commit.setCommitter(ident);
commit.setMessage(msg);
onSave(commit);
ObjectId res = newTree.writeTree(inserter);
if (res.equals(srcTree)) {
return;
}
commit.setTreeId(res);
if (revision != null) {
commit.addParentId(revision);
}
ObjectId newRevision = inserter.insert(commit);
updateRef(repo, ident, newRevision, "commit: " + msg);
revision = rw.parseCommit(newRevision);
} finally {
inserter = null;
reader = null;
}
}
Aggregations