Example usage for org.eclipse.jgit.storage.file FileBasedConfig getNames

List of usage examples for org.eclipse.jgit.storage.file FileBasedConfig getNames

Introduction

In this page you can find the example usage for org.eclipse.jgit.storage.file FileBasedConfig getNames.

Prototype

public Set<String> getNames(String section) 

Source Link

Document

Get the list of names defined for this section

Usage

From source file:com.gitblit.LuceneExecutor.java

License:Apache License

/**
 * Updates a repository index incrementally from the last indexed commits.
 * //  w ww  . j a  v  a2s.co  m
 * @param model
 * @param repository
 * @return IndexResult
 */
private IndexResult updateIndex(RepositoryModel model, Repository repository) {
    IndexResult result = new IndexResult();
    try {
        FileBasedConfig config = getConfig(repository);
        config.load();

        // build a quick lookup of annotated 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);
        }

        // detect branch deletion
        // first assume all branches are deleted and then remove each
        // existing branch from deletedBranches during indexing
        Set<String> deletedBranches = new TreeSet<String>();
        for (String alias : config.getNames(CONF_ALIAS)) {
            String branch = config.getString(CONF_ALIAS, null, alias);
            deletedBranches.add(branch);
        }

        // 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 branches
        for (RefModel branch : branches) {
            String branchName = branch.getName();

            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)) {
                // update issues modified on the GB_ISSUES branch
                // note: this is different than reindex
                indexBranch = true;
            } else {
                // normal explicit branch check
                indexBranch = model.indexedBranches.contains(branch.getName());
            }

            // if this branch is not specifically indexed then skip
            if (!indexBranch) {
                continue;
            }

            // remove this branch from the deletedBranches set
            deletedBranches.remove(branchName);

            // determine last commit
            String keyName = getBranchKey(branchName);
            String lastCommit = config.getString(CONF_BRANCH, null, keyName);

            List<RevCommit> revs;
            if (StringUtils.isEmpty(lastCommit)) {
                // new branch/unindexed branch, get all commits on branch
                revs = JGitUtils.getRevLog(repository, branchName, 0, -1);
            } else {
                // pre-existing branch, get changes since last commit
                revs = JGitUtils.getRevLog(repository, lastCommit, branchName);
            }

            if (revs.size() > 0) {
                result.branchCount += 1;
            }

            // track the issue ids that we have already indexed
            Set<String> indexedIssues = new TreeSet<String>();

            // reverse the list of commits so we start with the first commit            
            Collections.reverse(revs);
            for (RevCommit commit : revs) {
                if (IssueUtils.GB_ISSUES.equals(branch)) {
                    // only index an issue once during updateIndex
                    String issueId = commit.getShortMessage().substring(2).trim();
                    if (indexedIssues.contains(issueId)) {
                        continue;
                    }
                    indexedIssues.add(issueId);

                    IssueModel issue = IssueUtils.getIssue(repository, issueId);
                    if (issue == null) {
                        // issue was deleted, remove from index
                        if (!deleteIssue(model.name, issueId)) {
                            logger.error(MessageFormat.format("Failed to delete issue {0} from Lucene index!",
                                    issueId));
                        }
                    } else {
                        // issue was updated
                        index(model.name, issue);
                        result.issueCount++;
                    }
                } else {
                    // index a commit
                    result.add(index(model.name, repository, branchName, commit));
                }
            }

            // update the config
            config.setInt(CONF_INDEX, null, CONF_VERSION, INDEX_VERSION);
            config.setString(CONF_ALIAS, null, keyName, branchName);
            config.setString(CONF_BRANCH, null, keyName, branch.getObjectId().getName());
            config.save();
        }

        // the deletedBranches set will normally be empty by this point
        // unless a branch really was deleted and no longer exists
        if (deletedBranches.size() > 0) {
            for (String branch : deletedBranches) {
                IndexWriter writer = getIndexWriter(model.name);
                writer.deleteDocuments(new Term(FIELD_BRANCH, branch));
                writer.commit();
            }
        }
        result.success = true;
    } catch (Throwable t) {
        logger.error(MessageFormat.format("Exception while updating {0} Lucene index", model.name), t);
    }
    return result;
}

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

License:Apache License

/**
 * Updates a repository index incrementally from the last indexed commits.
 *
 * @param model/*from ww w. j av  a 2 s . co  m*/
 * @param repository
 * @return IndexResult
 */
private IndexResult updateIndex(RepositoryModel model, Repository repository) {
    IndexResult result = new IndexResult();
    try {
        FileBasedConfig config = getConfig(repository);
        config.load();

        // build a quick lookup of annotated 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().getName())) {
                tags.put(tag.getReferencedObjectId().getName(), new ArrayList<String>());
            }
            tags.get(tag.getReferencedObjectId().getName()).add(tag.displayName);
        }

        // detect branch deletion
        // first assume all branches are deleted and then remove each
        // existing branch from deletedBranches during indexing
        Set<String> deletedBranches = new TreeSet<String>();
        for (String alias : config.getNames(CONF_ALIAS)) {
            String branch = config.getString(CONF_ALIAS, null, alias);
            deletedBranches.add(branch);
        }

        // 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 branches
        for (RefModel branch : branches) {
            String branchName = branch.getName();

            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)) {
                // ignore 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;
            }

            // remove this branch from the deletedBranches set
            deletedBranches.remove(branchName);

            // determine last commit
            String keyName = getBranchKey(branchName);
            String lastCommit = config.getString(CONF_BRANCH, null, keyName);

            List<RevCommit> revs;
            if (StringUtils.isEmpty(lastCommit)) {
                // new branch/unindexed branch, get all commits on branch
                revs = JGitUtils.getRevLog(repository, branchName, 0, -1);
            } else {
                // pre-existing branch, get changes since last commit
                revs = JGitUtils.getRevLog(repository, lastCommit, branchName);
            }

            if (revs.size() > 0) {
                result.branchCount += 1;
            }

            // reverse the list of commits so we start with the first commit
            Collections.reverse(revs);
            for (RevCommit commit : revs) {
                // index a commit
                result.add(index(model.name, repository, branchName, commit));
            }

            // update the config
            config.setString(CONF_ALIAS, null, keyName, branchName);
            config.setString(CONF_BRANCH, null, keyName, branch.getObjectId().getName());
            config.save();
        }

        // the deletedBranches set will normally be empty by this point
        // unless a branch really was deleted and no longer exists
        if (deletedBranches.size() > 0) {
            for (String branch : deletedBranches) {
                IndexWriter writer = getIndexWriter(model.name);
                writer.deleteDocuments(new Term(FIELD_BRANCH, branch));
                writer.commit();
            }
        }
        result.success = true;
    } catch (Throwable t) {
        logger.error(MessageFormat.format("Exception while updating {0} Lucene index", model.name), t);
    }
    return result;
}