Search in sources :

Example 6 with TreeWalk

use of org.eclipse.jgit.treewalk.TreeWalk in project gitblit by gitblit.

the class JGitUtils method getPathModel.

/**
	 * Returns a path model by path string
	 *
	 * @param repo
	 * @param path
	 * @param filter
	 * @param commit
	 * @return a path model of the specified object
	 */
private static PathModel getPathModel(Repository repo, String path, String filter, RevCommit commit) throws IOException {
    long size = 0;
    FilestoreModel filestoreItem = null;
    TreeWalk tw = TreeWalk.forPath(repo, path, commit.getTree());
    String pathString = path;
    if (!tw.isSubtree() && (tw.getFileMode(0) != FileMode.GITLINK)) {
        pathString = PathUtils.getLastPathComponent(pathString);
        size = tw.getObjectReader().getObjectSize(tw.getObjectId(0), Constants.OBJ_BLOB);
        if (isPossibleFilestoreItem(size)) {
            filestoreItem = getFilestoreItem(tw.getObjectReader().open(tw.getObjectId(0)));
        }
    } else if (tw.isSubtree()) {
        // do not display dirs that are behind in the path
        if (!Strings.isNullOrEmpty(filter)) {
            pathString = path.replaceFirst(filter + "/", "");
        }
        // remove the last slash from path in displayed link
        if (pathString != null && pathString.charAt(pathString.length() - 1) == '/') {
            pathString = pathString.substring(0, pathString.length() - 1);
        }
    }
    return new PathModel(pathString, tw.getPathString(), filestoreItem, size, tw.getFileMode(0).getBits(), tw.getObjectId(0).getName(), commit.getName());
}
Also used : PathModel(com.gitblit.models.PathModel) FilestoreModel(com.gitblit.models.FilestoreModel) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Example 7 with TreeWalk

use of org.eclipse.jgit.treewalk.TreeWalk in project gitblit by gitblit.

the class LuceneService method reindex.

/**
	 * This completely indexes the repository and will destroy any existing
	 * index.
	 *
	 * @param repositoryName
	 * @param repository
	 * @return IndexResult
	 */
public IndexResult reindex(RepositoryModel model, Repository repository) {
    IndexResult result = new IndexResult();
    if (!deleteIndex(model.name)) {
        return result;
    }
    try {
        String[] encodings = storedSettings.getStrings(Keys.web.blobEncodings).toArray(new String[0]);
        FileBasedConfig config = getConfig(repository);
        Set<String> indexedCommits = new TreeSet<String>();
        IndexWriter writer = getIndexWriter(model.name);
        // build a quick lookup of tags
        Map<String, List<String>> tags = new HashMap<String, List<String>>();
        for (RefModel tag : JGitUtils.getTags(repository, false, -1)) {
            if (!tag.isAnnotatedTag()) {
                // skip non-annotated tags
                continue;
            }
            if (!tags.containsKey(tag.getReferencedObjectId().getName())) {
                tags.put(tag.getReferencedObjectId().getName(), new ArrayList<String>());
            }
            tags.get(tag.getReferencedObjectId().getName()).add(tag.displayName);
        }
        ObjectReader reader = repository.newObjectReader();
        // get the local branches
        List<RefModel> branches = JGitUtils.getLocalBranches(repository, true, -1);
        // sort them by most recently updated
        Collections.sort(branches, new Comparator<RefModel>() {

            @Override
            public int compare(RefModel ref1, RefModel ref2) {
                return ref2.getDate().compareTo(ref1.getDate());
            }
        });
        // reorder default branch to first position
        RefModel defaultBranch = null;
        ObjectId defaultBranchId = JGitUtils.getDefaultBranch(repository);
        for (RefModel branch : branches) {
            if (branch.getObjectId().equals(defaultBranchId)) {
                defaultBranch = branch;
                break;
            }
        }
        branches.remove(defaultBranch);
        branches.add(0, defaultBranch);
        // walk through each branch
        for (RefModel branch : branches) {
            boolean indexBranch = false;
            if (model.indexedBranches.contains(com.gitblit.Constants.DEFAULT_BRANCH) && branch.equals(defaultBranch)) {
                // indexing "default" branch
                indexBranch = true;
            } else if (branch.getName().startsWith(com.gitblit.Constants.R_META)) {
                // skip internal meta branches
                indexBranch = false;
            } else {
                // normal explicit branch check
                indexBranch = model.indexedBranches.contains(branch.getName());
            }
            // if this branch is not specifically indexed then skip
            if (!indexBranch) {
                continue;
            }
            String branchName = branch.getName();
            RevWalk revWalk = new RevWalk(reader);
            RevCommit tip = revWalk.parseCommit(branch.getObjectId());
            String tipId = tip.getId().getName();
            String keyName = getBranchKey(branchName);
            config.setString(CONF_ALIAS, null, keyName, branchName);
            config.setString(CONF_BRANCH, null, keyName, tipId);
            // index the blob contents of the tree
            TreeWalk treeWalk = new TreeWalk(repository);
            treeWalk.addTree(tip.getTree());
            treeWalk.setRecursive(true);
            Map<String, ObjectId> paths = new TreeMap<String, ObjectId>();
            while (treeWalk.next()) {
                // ensure path is not in a submodule
                if (treeWalk.getFileMode(0) != FileMode.GITLINK) {
                    paths.put(treeWalk.getPathString(), treeWalk.getObjectId(0));
                }
            }
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            byte[] tmp = new byte[32767];
            RevWalk commitWalk = new RevWalk(reader);
            commitWalk.markStart(tip);
            RevCommit commit;
            while ((paths.size() > 0) && (commit = commitWalk.next()) != null) {
                TreeWalk diffWalk = new TreeWalk(reader);
                int parentCount = commit.getParentCount();
                switch(parentCount) {
                    case 0:
                        diffWalk.addTree(new EmptyTreeIterator());
                        break;
                    case 1:
                        diffWalk.addTree(getTree(commitWalk, commit.getParent(0)));
                        break;
                    default:
                        // skip merge commits
                        continue;
                }
                diffWalk.addTree(getTree(commitWalk, commit));
                diffWalk.setFilter(ANY_DIFF);
                diffWalk.setRecursive(true);
                while ((paths.size() > 0) && diffWalk.next()) {
                    String path = diffWalk.getPathString();
                    if (!paths.containsKey(path)) {
                        continue;
                    }
                    // remove path from set
                    ObjectId blobId = paths.remove(path);
                    result.blobCount++;
                    // index the blob metadata
                    String blobAuthor = getAuthor(commit);
                    String blobCommitter = getCommitter(commit);
                    String blobDate = DateTools.timeToString(commit.getCommitTime() * 1000L, Resolution.MINUTE);
                    Document doc = new Document();
                    doc.add(new Field(FIELD_OBJECT_TYPE, SearchObjectType.blob.name(), StringField.TYPE_STORED));
                    doc.add(new Field(FIELD_BRANCH, branchName, TextField.TYPE_STORED));
                    doc.add(new Field(FIELD_COMMIT, commit.getName(), TextField.TYPE_STORED));
                    doc.add(new Field(FIELD_PATH, path, TextField.TYPE_STORED));
                    doc.add(new Field(FIELD_DATE, blobDate, StringField.TYPE_STORED));
                    doc.add(new Field(FIELD_AUTHOR, blobAuthor, TextField.TYPE_STORED));
                    doc.add(new Field(FIELD_COMMITTER, blobCommitter, TextField.TYPE_STORED));
                    // determine extension to compare to the extension
                    // blacklist
                    String ext = null;
                    String name = path.toLowerCase();
                    if (name.indexOf('.') > -1) {
                        ext = name.substring(name.lastIndexOf('.') + 1);
                    }
                    // index the blob content
                    if (StringUtils.isEmpty(ext) || !excludedExtensions.contains(ext)) {
                        ObjectLoader ldr = repository.open(blobId, Constants.OBJ_BLOB);
                        InputStream in = ldr.openStream();
                        int n;
                        while ((n = in.read(tmp)) > 0) {
                            os.write(tmp, 0, n);
                        }
                        in.close();
                        byte[] content = os.toByteArray();
                        String str = StringUtils.decodeString(content, encodings);
                        doc.add(new Field(FIELD_CONTENT, str, TextField.TYPE_STORED));
                        os.reset();
                    }
                    // add the blob to the index
                    writer.addDocument(doc);
                }
            }
            os.close();
            // index the tip commit object
            if (indexedCommits.add(tipId)) {
                Document doc = createDocument(tip, tags.get(tipId));
                doc.add(new Field(FIELD_BRANCH, branchName, TextField.TYPE_STORED));
                writer.addDocument(doc);
                result.commitCount += 1;
                result.branchCount += 1;
            }
            // traverse the log and index the previous commit objects
            RevWalk historyWalk = new RevWalk(reader);
            historyWalk.markStart(historyWalk.parseCommit(tip.getId()));
            RevCommit rev;
            while ((rev = historyWalk.next()) != null) {
                String hash = rev.getId().getName();
                if (indexedCommits.add(hash)) {
                    Document doc = createDocument(rev, tags.get(hash));
                    doc.add(new Field(FIELD_BRANCH, branchName, TextField.TYPE_STORED));
                    writer.addDocument(doc);
                    result.commitCount += 1;
                }
            }
        }
        // finished
        reader.close();
        // commit all changes and reset the searcher
        config.save();
        writer.commit();
        resetIndexSearcher(model.name);
        result.success();
    } catch (Exception e) {
        logger.error("Exception while reindexing " + model.name, e);
    }
    return result;
}
Also used : RefModel(com.gitblit.models.RefModel) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Document(org.apache.lucene.document.Document) StringField(org.apache.lucene.document.StringField) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) TreeSet(java.util.TreeSet) List(java.util.List) ArrayList(java.util.ArrayList) ObjectReader(org.eclipse.jgit.lib.ObjectReader) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit) ObjectId(org.eclipse.jgit.lib.ObjectId) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeMap(java.util.TreeMap) ParseException(java.text.ParseException) InvalidTokenOffsetsException(org.apache.lucene.search.highlight.InvalidTokenOffsetsException) IOException(java.io.IOException) IndexWriter(org.apache.lucene.index.IndexWriter) EmptyTreeIterator(org.eclipse.jgit.treewalk.EmptyTreeIterator) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader)

Example 8 with TreeWalk

use of org.eclipse.jgit.treewalk.TreeWalk in project gitblit by gitblit.

the class RawServlet method streamFromRepo.

protected boolean streamFromRepo(HttpServletRequest request, HttpServletResponse response, Repository repository, RevCommit commit, String requestedPath) throws IOException {
    boolean served = false;
    RevWalk rw = new RevWalk(repository);
    TreeWalk tw = new TreeWalk(repository);
    try {
        tw.reset();
        tw.addTree(commit.getTree());
        PathFilter f = PathFilter.create(requestedPath);
        tw.setFilter(f);
        tw.setRecursive(true);
        MutableObjectId id = new MutableObjectId();
        ObjectReader reader = tw.getObjectReader();
        while (tw.next()) {
            FileMode mode = tw.getFileMode(0);
            if (mode == FileMode.GITLINK || mode == FileMode.TREE) {
                continue;
            }
            tw.getObjectId(id, 0);
            String filename = StringUtils.getLastPathElement(requestedPath);
            try {
                String userAgent = request.getHeader("User-Agent");
                if (userAgent != null && userAgent.indexOf("MSIE 5.5") > -1) {
                    response.setHeader("Content-Disposition", "filename=\"" + URLEncoder.encode(filename, Constants.ENCODING) + "\"");
                } else if (userAgent != null && userAgent.indexOf("MSIE") > -1) {
                    response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(filename, Constants.ENCODING) + "\"");
                } else {
                    response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(filename.getBytes(Constants.ENCODING), "latin1") + "\"");
                }
            } catch (UnsupportedEncodingException e) {
                response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
            }
            long len = reader.getObjectSize(id, org.eclipse.jgit.lib.Constants.OBJ_BLOB);
            setContentType(response, "application/octet-stream");
            response.setIntHeader("Content-Length", (int) len);
            ObjectLoader ldr = repository.open(id);
            ldr.copyTo(response.getOutputStream());
            served = true;
        }
    } finally {
        tw.close();
        rw.dispose();
    }
    response.flushBuffer();
    return served;
}
Also used : FileMode(org.eclipse.jgit.lib.FileMode) MutableObjectId(org.eclipse.jgit.lib.MutableObjectId) PathFilter(org.eclipse.jgit.treewalk.filter.PathFilter) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Example 9 with TreeWalk

use of org.eclipse.jgit.treewalk.TreeWalk in project gerrit by GerritCodeReview.

the class ConflictsPredicate method listFiles.

public static List<String> listFiles(Change c, Arguments args, ChangeDataCache changeDataCache) throws OrmException {
    try (Repository repo = args.repoManager.openRepository(c.getProject());
        RevWalk rw = new RevWalk(repo)) {
        RevCommit ps = rw.parseCommit(changeDataCache.getTestAgainst());
        if (ps.getParentCount() > 1) {
            String dest = c.getDest().get();
            Ref destBranch = repo.getRefDatabase().getRef(dest);
            destBranch.getObjectId();
            rw.setRevFilter(RevFilter.MERGE_BASE);
            rw.markStart(rw.parseCommit(destBranch.getObjectId()));
            rw.markStart(ps);
            RevCommit base = rw.next();
            // TODO(zivkov): handle the case with multiple merge bases
            List<String> files = new ArrayList<>();
            try (TreeWalk tw = new TreeWalk(repo)) {
                if (base != null) {
                    tw.setFilter(TreeFilter.ANY_DIFF);
                    tw.addTree(base.getTree());
                }
                tw.addTree(ps.getTree());
                tw.setRecursive(true);
                while (tw.next()) {
                    files.add(tw.getPathString());
                }
            }
            return files;
        }
        return args.changeDataFactory.create(args.db.get(), c).currentFilePaths();
    } catch (IOException e) {
        throw new OrmException(e);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) OrmException(com.google.gwtorm.server.OrmException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CodeReviewRevWalk(com.google.gerrit.server.git.CodeReviewCommit.CodeReviewRevWalk) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 10 with TreeWalk

use of org.eclipse.jgit.treewalk.TreeWalk in project gitiles by GerritCodeReview.

the class BlameServlet method resolveBlob.

private static ObjectId resolveBlob(GitilesView view, RevWalk rw, ObjectId commitId) throws IOException {
    try {
        if (commitId == null || Strings.isNullOrEmpty(view.getPathPart())) {
            return null;
        }
        RevTree tree = rw.parseTree(commitId);
        TreeWalk tw = TreeWalk.forPath(rw.getObjectReader(), view.getPathPart(), tree);
        if (tw == null || (tw.getRawMode(0) & FileMode.TYPE_MASK) != FileMode.TYPE_FILE) {
            return null;
        }
        return tw.getObjectId(0);
    } catch (IncorrectObjectTypeException e) {
        return null;
    }
}
Also used : IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevTree(org.eclipse.jgit.revwalk.RevTree)

Aggregations

TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)31 RevWalk (org.eclipse.jgit.revwalk.RevWalk)19 RevCommit (org.eclipse.jgit.revwalk.RevCommit)14 ObjectId (org.eclipse.jgit.lib.ObjectId)11 ArrayList (java.util.ArrayList)10 IOException (java.io.IOException)9 ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)9 ObjectReader (org.eclipse.jgit.lib.ObjectReader)6 Repository (org.eclipse.jgit.lib.Repository)6 FileMode (org.eclipse.jgit.lib.FileMode)5 RevTree (org.eclipse.jgit.revwalk.RevTree)5 PathFilter (org.eclipse.jgit.treewalk.filter.PathFilter)5 FilestoreModel (com.gitblit.models.FilestoreModel)4 PathModel (com.gitblit.models.PathModel)4 IncorrectObjectTypeException (org.eclipse.jgit.errors.IncorrectObjectTypeException)3 LargeObjectException (org.eclipse.jgit.errors.LargeObjectException)3 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)3 RefModel (com.gitblit.models.RefModel)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2