Example usage for org.eclipse.jgit.treewalk TreeWalk addTree

List of usage examples for org.eclipse.jgit.treewalk TreeWalk addTree

Introduction

In this page you can find the example usage for org.eclipse.jgit.treewalk TreeWalk addTree.

Prototype

public int addTree(AbstractTreeIterator p) 

Source Link

Document

Add an already created tree iterator for walking.

Usage

From source file:MyDiffFormatter.java

License:Eclipse Distribution License

/**
* Determine the differences between two trees.
*
* No output is created, instead only the file paths that are different are
* returned. Callers may choose to format these paths themselves, or convert
* them into {@link FileHeader} instances with a complete edit list by
*
* @param a/*from  w ww.  j a v a2  s. co  m*/
*            the old (or previous) side.
* @param b
*            the new (or updated) side.
* @return the paths that are different.
* @throws IOException
*             trees cannot be read or file contents cannot be read.
*/
public List<DiffEntry> scan(AbstractTreeIterator a, AbstractTreeIterator b) throws IOException {
    assertHaveRepository();

    TreeWalk walk = new TreeWalk(reader);
    walk.addTree(a);
    walk.addTree(b);
    walk.setRecursive(true);

    TreeFilter filter = getDiffTreeFilterFor(a, b);
    if (pathFilter instanceof FollowFilter) {
        walk.setFilter(AndTreeFilter.create(PathFilter.create(((FollowFilter) pathFilter).getPath()), filter));
    } else {
        walk.setFilter(AndTreeFilter.create(pathFilter, filter));
    }

    source = new ContentSource.Pair(source(a), source(b));

    List<DiffEntry> files = DiffEntry.scan(walk);
    if (pathFilter instanceof FollowFilter && isAdd(files)) {
        // The file we are following was added here, find where it
        // came from so we can properly show the rename or copy,
        // then continue digging backwards.
        //
        a.reset();
        b.reset();
        walk.reset();
        walk.addTree(a);
        walk.addTree(b);
        walk.setFilter(filter);

        if (renameDetector == null)
            setDetectRenames(true);
        files = updateFollowFilter(detectRenames(DiffEntry.scan(walk)));

    } else if (renameDetector != null)
        files = detectRenames(files);

    return files;
}

From source file:at.bitandart.zoubek.mervin.gerrit.GerritReviewRepositoryService.java

License:Open Source License

/**
 * builds a dir cache by copying entries from an existing tree, skipping
 * files if specified./*from www. jav a2s . com*/
 *
 * @param tree
 *            the tree to copy from.
 * @param repository
 *            the repository to work upon.
 * @param dirCacheBuilder
 *            the dir builder instance used to add entries to the dir cache.
 * @param ignoredFilePaths
 *            a list of file paths to ignore during copying.
 * @throws MissingObjectException
 * @throws IncorrectObjectTypeException
 * @throws CorruptObjectException
 * @throws IOException
 */
private void buildDirCacheFromTree(RevTree tree, Repository repository, DirCacheBuilder dirCacheBuilder,
        List<String> ignoredFilePaths)
        throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException {
    // TODO improve exception handling

    TreeWalk treeWalk = new TreeWalk(repository);
    int treeId = treeWalk.addTree(tree);
    treeWalk.setRecursive(true);

    while (treeWalk.next()) {
        String path = treeWalk.getPathString();

        CanonicalTreeParser prevTreeParser = treeWalk.getTree(treeId, CanonicalTreeParser.class);
        if (prevTreeParser != null && !ignoredFilePaths.contains(path)) {
            // create a new DirCacheEntry with data from the previous commit

            final DirCacheEntry dcEntry = new DirCacheEntry(path);
            dcEntry.setObjectId(prevTreeParser.getEntryObjectId());
            dcEntry.setFileMode(prevTreeParser.getEntryFileMode());
            dirCacheBuilder.add(dcEntry);
        }
    }
    treeWalk.close();
}

From source file:au.id.soundadvice.systemdesign.versioning.jgit.GitVersionControl.java

License:Open Source License

/**
 * Find the tree that contains the required identity.
 *
 * @return The ObjectId of the tree (directory) that contains the matching
 * identity within the supplied hierarchy.
 *//*  w  ww  .j a  v a2 s.  co  m*/
private ObjectId findMatchingIdentity(IdentityValidator identityValidator, ObjectId tree) throws IOException {
    TreeWalk treeWalk = new TreeWalk(repo.getRepository());
    try {
        treeWalk.setRecursive(false);
        treeWalk.addTree(tree);

        while (treeWalk.next()) {
            if (treeWalk.isSubtree()) {
                ObjectId candidateId = findMatchingIdentity(identityValidator, treeWalk.getObjectId(0));
                if (ObjectId.zeroId().equals(candidateId)) {
                    // Keep searching
                } else {
                    return candidateId;
                }
            } else if (identityValidator.getIdentityFileName().equals(treeWalk.getNameString())) {
                // Read the identity file
                ObjectLoader loader = repo.getRepository().open(treeWalk.getObjectId(0));
                ObjectStream stream = loader.openStream();
                InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8);
                if (identityValidator.isIdentityMatched(new BufferedReader(reader))) {
                    // We found it
                    return tree;
                }
            }
        }
        return ObjectId.zeroId();
    } finally {
        treeWalk.release();
    }
}

From source file:au.id.soundadvice.systemdesign.versioning.jgit.GitVersionControl.java

License:Open Source License

@Override
public Optional<BufferedReader> getBufferedReader(IdentityValidator identityValidator, String filename,
        Optional<VersionInfo> version) throws IOException {
    if (version.isPresent()) {
        Pair<String, ObjectId> diff = diffCache.get();
        if (!version.get().getId().equals(diff.getKey())) {
            // Grab the id of the commit we are trying to diff against
            ObjectId id = ObjectId.fromString(version.get().getId());
            RevWalk revWalk = new RevWalk(repo.getRepository());
            try {
                // Read the commit
                RevCommit commit = revWalk.parseCommit(id);
                ObjectId matchedDirectory = findMatchingIdentity(identityValidator, commit.getTree());
                diff = new Pair<>(version.get().getId(), matchedDirectory);
                diffCache.set(diff);/*  ww w .j a va  2s.c o m*/
            } finally {
                revWalk.release();
            }
        }

        if (ObjectId.zeroId().equals(diff.getValue())) {
            // No such tree
            return Optional.empty();
        } else {
            // Find the file in this tree
            TreeWalk treeWalk = new TreeWalk(repo.getRepository());
            try {
                treeWalk.setRecursive(false);
                treeWalk.addTree(diff.getValue());

                while (treeWalk.next()) {
                    if (filename.equals(treeWalk.getNameString())) {
                        // Read the file
                        ObjectLoader loader = repo.getRepository().open(treeWalk.getObjectId(0));
                        ObjectStream stream = loader.openStream();
                        InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8);
                        return Optional.of(new BufferedReader(reader));
                    }
                }
                // No such file
                return Optional.empty();
            } finally {
                treeWalk.release();
            }
        }
    } else {
        Path path = identityValidator.getDirectoryPath().resolve(filename);
        if (Files.exists(path)) {
            return Optional.of(Files.newBufferedReader(path));
        } else {
            return Optional.empty();
        }
    }
}

From source file:co.bledo.gitmin.servlet.Review.java

License:Apache License

private String getFileContent(Repository repo, RevCommit commit, String path)
        throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException {
    // and using commit's tree find the path
    RevTree tree = commit.getTree();// w  ww .  java 2 s  . co m
    TreeWalk treeWalk = new TreeWalk(repo);
    treeWalk.addTree(tree);
    treeWalk.setRecursive(true);
    treeWalk.setFilter(PathFilter.create(path));
    if (!treeWalk.next()) {
        return "";
    }
    ObjectId objectId = treeWalk.getObjectId(0);
    ObjectLoader loader = repo.open(objectId);

    // and then one can use either
    //InputStream in = loader.openStream();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    loader.copyTo(out);
    return out.toString();
}

From source file:com.beck.ep.team.git.GitListBuilder.java

License:BSD License

private int addTree(int mode, Shell shell, Repository rep, RevWalk rw, TreeWalk tw) throws Exception {
    int index = -1;
    switch (mode) {
    case GitModeMenuMgr.MODE_STAGING:
        tw.addTree(new DirCacheIterator(rep.readDirCache()));
        break;/*from   w  ww. ja  v a2  s .c o  m*/
    case GitModeMenuMgr.MODE_OTHERS://choose a GIT ref
        CompareTargetSelectionDialog dialog = new CompareTargetSelectionDialog(shell, rep, null);
        if (dialog.open() != CompareTargetSelectionDialog.OK) {
            return -2;
        }
        ObjectId obId = rep.getRef(dialog.getRefName()).getObjectId();//"master"
        tw.addTree(rw.parseCommit(obId).getTree());
        break;
    case GitModeMenuMgr.MODE_BRANCH_HEAD:
        Ref head = rep.getRef(Constants.HEAD);
        RevCommit rc = rw.parseCommit(head.getObjectId());
        tw.addTree(rc.getTree());
        break;
    default://compare to working area
        index = tw.addTree(new FileTreeIterator(rep));
        break;
    }
    return index;
}

From source file:com.gitblit.build.BuildGhPages.java

License:Apache License

/**
 * Creates an in-memory index of the issue change.
 * /*from  w  w w  . j  a  va2  s  . c  o m*/
 * @param repo
 * @param headId
 * @param sourceFolder
 * @param obliterate
 *            if true the source folder tree is used as the new tree for
 *            gh-pages and non-existent files are considered deleted
 * @return an in-memory index
 * @throws IOException
 */
private static DirCache createIndex(Repository repo, ObjectId headId, File sourceFolder, boolean obliterate)
        throws IOException {

    DirCache inCoreIndex = DirCache.newInCore();
    DirCacheBuilder dcBuilder = inCoreIndex.builder();
    ObjectInserter inserter = repo.newObjectInserter();

    try {
        // Add all files to the temporary index
        Set<String> ignorePaths = new TreeSet<String>();
        List<File> files = listFiles(sourceFolder);
        for (File file : files) {
            // create an index entry for the file
            final DirCacheEntry dcEntry = new DirCacheEntry(
                    StringUtils.getRelativePath(sourceFolder.getPath(), file.getPath()));
            dcEntry.setLength(file.length());
            dcEntry.setLastModified(file.lastModified());
            dcEntry.setFileMode(FileMode.REGULAR_FILE);

            // add this entry to the ignore paths set
            ignorePaths.add(dcEntry.getPathString());

            // insert object
            InputStream inputStream = new FileInputStream(file);
            try {
                dcEntry.setObjectId(inserter.insert(Constants.OBJ_BLOB, file.length(), inputStream));
            } finally {
                inputStream.close();
            }

            // add to temporary in-core index
            dcBuilder.add(dcEntry);
        }

        if (!obliterate) {
            // Traverse HEAD to add all other paths
            TreeWalk treeWalk = new TreeWalk(repo);
            int hIdx = -1;
            if (headId != null)
                hIdx = treeWalk.addTree(new RevWalk(repo).parseTree(headId));
            treeWalk.setRecursive(true);

            while (treeWalk.next()) {
                String path = treeWalk.getPathString();
                CanonicalTreeParser hTree = null;
                if (hIdx != -1)
                    hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class);
                if (!ignorePaths.contains(path)) {
                    // add entries from HEAD for all other paths
                    if (hTree != null) {
                        // create a new DirCacheEntry with data retrieved
                        // from
                        // HEAD
                        final DirCacheEntry dcEntry = new DirCacheEntry(path);
                        dcEntry.setObjectId(hTree.getEntryObjectId());
                        dcEntry.setFileMode(hTree.getEntryFileMode());

                        // add to temporary in-core index
                        dcBuilder.add(dcEntry);
                    }
                }
            }

            // release the treewalk
            treeWalk.release();
        }

        // finish temporary in-core index used for this commit
        dcBuilder.finish();
    } finally {
        inserter.release();
    }
    return inCoreIndex;
}

From source file:com.gitblit.LuceneExecutor.java

License:Apache License

/**
 * This completely indexes the repository and will destroy any existing
 * index./*from  ww  w.j a  va 2 s.  co  m*/
 * 
 * @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.getObjectId())) {
                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 (IssueUtils.GB_ISSUES.equals(branch)) {
                // skip the GB_ISSUES branch because it is indexed later
                // note: this is different than updateIndex
                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(), Store.YES,
                            Index.NOT_ANALYZED_NO_NORMS));
                    doc.add(new Field(FIELD_BRANCH, branchName, Store.YES, Index.ANALYZED));
                    doc.add(new Field(FIELD_COMMIT, commit.getName(), Store.YES, Index.ANALYZED));
                    doc.add(new Field(FIELD_PATH, path, Store.YES, Index.ANALYZED));
                    doc.add(new Field(FIELD_DATE, blobDate, Store.YES, Index.NO));
                    doc.add(new Field(FIELD_AUTHOR, blobAuthor, Store.YES, Index.ANALYZED));
                    doc.add(new Field(FIELD_COMMITTER, blobCommitter, Store.YES, Index.ANALYZED));

                    // 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, Store.YES, Index.ANALYZED));
                        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, Store.YES, Index.ANALYZED));
                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, Store.YES, Index.ANALYZED));
                    writer.addDocument(doc);
                    result.commitCount += 1;
                }
            }
        }

        // finished
        reader.release();

        // this repository has a gb-issues branch, index all issues
        if (IssueUtils.getIssuesBranch(repository) != null) {
            List<IssueModel> issues = IssueUtils.getIssues(repository, null);
            if (issues.size() > 0) {
                result.branchCount += 1;
            }
            for (IssueModel issue : issues) {
                result.issueCount++;
                Document doc = createDocument(issue);
                writer.addDocument(doc);
            }
        }

        // commit all changes and reset the searcher
        config.setInt(CONF_INDEX, null, CONF_VERSION, INDEX_VERSION);
        config.save();
        writer.commit();
        resetIndexSearcher(model.name);
        result.success();
    } catch (Exception e) {
        logger.error("Exception while reindexing " + model.name, e);
    }
    return result;
}

From source file:com.gitblit.service.LuceneService.java

License:Apache License

/**
 * This completely indexes the repository and will destroy any existing
 * index.//w  w  w.  ja  va2  s  .  co  m
 *
 * @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;
}

From source file:com.gitblit.servlet.RawServlet.java

License:Apache License

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 {//  w  ww  .j  a v a 2  s.c o  m
        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;
}