Search in sources :

Example 16 with ObjectId

use of org.eclipse.jgit.lib.ObjectId in project gitblit by gitblit.

the class JGitUtils method getRevLog.

/**
	 * Returns a list of commits for the repository within the range specified
	 * by startRangeId and endRangeId. If the repository does not exist or is
	 * empty, an empty list is returned.
	 *
	 * @param repository
	 * @param startRangeId
	 *            the first commit (not included in results)
	 * @param endRangeId
	 *            the end commit (included in results)
	 * @return a list of commits
	 */
public static List<RevCommit> getRevLog(Repository repository, String startRangeId, String endRangeId) {
    List<RevCommit> list = new ArrayList<RevCommit>();
    if (!hasCommits(repository)) {
        return list;
    }
    try {
        ObjectId endRange = repository.resolve(endRangeId);
        ObjectId startRange = repository.resolve(startRangeId);
        RevWalk rw = new RevWalk(repository);
        rw.markStart(rw.parseCommit(endRange));
        if (startRange.equals(ObjectId.zeroId())) {
            // maybe this is a tag or an orphan branch
            list.add(rw.parseCommit(endRange));
            rw.dispose();
            return list;
        } else {
            rw.markUninteresting(rw.parseCommit(startRange));
        }
        Iterable<RevCommit> revlog = rw;
        for (RevCommit rev : revlog) {
            list.add(rev);
        }
        rw.dispose();
    } catch (Throwable t) {
        error(t, repository, "{0} failed to get revlog for {1}..{2}", startRangeId, endRangeId);
    }
    return list;
}
Also used : AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 17 with ObjectId

use of org.eclipse.jgit.lib.ObjectId 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;
}
Also used : DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser)

Example 18 with ObjectId

use of org.eclipse.jgit.lib.ObjectId in project gitblit by gitblit.

the class LuceneService method updateIndex.

/**
	 * Updates a repository index incrementally from the last indexed commits.
	 *
	 * @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().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();
        }
        // 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;
}
Also used : RefModel(com.gitblit.models.RefModel) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ObjectId(org.eclipse.jgit.lib.ObjectId) Term(org.apache.lucene.index.Term) IndexWriter(org.apache.lucene.index.IndexWriter) TreeSet(java.util.TreeSet) List(java.util.List) ArrayList(java.util.ArrayList) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 19 with ObjectId

use of org.eclipse.jgit.lib.ObjectId 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 20 with ObjectId

use of org.eclipse.jgit.lib.ObjectId in project gitblit by gitblit.

the class BranchGraphServlet method doGet.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    InputStream is = null;
    Repository r = null;
    PlotWalk rw = null;
    try {
        String repository = request.getParameter("r");
        if (StringUtils.isEmpty(repository)) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().append("Bad request");
            return;
        }
        String objectId = request.getParameter("h");
        String length = request.getParameter("l");
        r = repositoryManager.getRepository(repository);
        if (r == null) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().append("Bad request");
            return;
        }
        rw = new PlotWalk(r);
        if (StringUtils.isEmpty(objectId)) {
            objectId = JGitUtils.getHEADRef(r);
        }
        ObjectId id = r.resolve(objectId);
        if (id == null) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().append("Bad request");
            return;
        }
        rw.markStart(rw.lookupCommit(id));
        // default to the items-per-page setting, unless specified
        int maxCommits = settings.getInteger(Keys.web.itemsPerPage, 50);
        int requestedCommits = maxCommits;
        if (!StringUtils.isEmpty(length)) {
            int l = Integer.parseInt(length);
            if (l > 0) {
                requestedCommits = l;
            }
        }
        // fetch the requested commits plus some extra so that the last
        // commit displayed *likely* has correct lane assignments
        CommitList commitList = new CommitList();
        commitList.source(rw);
        commitList.fillTo(2 * Math.max(requestedCommits, maxCommits));
        // determine the appropriate width for the image
        int numLanes = 1;
        int numCommits = Math.min(requestedCommits, commitList.size());
        if (numCommits > 1) {
            // determine graph width
            Set<String> parents = new TreeSet<String>();
            for (int i = 0; i < commitList.size(); i++) {
                PlotCommit<Lane> commit = commitList.get(i);
                boolean checkLane = false;
                if (i < numCommits) {
                    // commit in visible list
                    checkLane = true;
                    // remember parents
                    for (RevCommit p : commit.getParents()) {
                        parents.add(p.getName());
                    }
                } else if (parents.contains(commit.getName())) {
                    // commit outside visible list, but it is a parent of a
                    // commit in the visible list so we need to know it's lane
                    // assignment
                    checkLane = true;
                }
                if (checkLane) {
                    int pos = commit.getLane().getPosition();
                    numLanes = Math.max(numLanes, pos + 1);
                }
            }
        }
        int graphWidth = numLanes * LANE_WIDTH + RIGHT_PAD;
        int rowHeight = ROW_HEIGHT;
        // create an image buffer and render the lanes
        BufferedImage image = new BufferedImage(graphWidth, rowHeight * numCommits, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = null;
        try {
            g = image.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            LanesRenderer renderer = new LanesRenderer();
            for (int i = 0; i < commitList.size(); i++) {
                PlotCommit<Lane> commit = commitList.get(i);
                Graphics row = g.create(0, i * rowHeight, graphWidth, rowHeight);
                try {
                    renderer.paint(row, commit, rowHeight, graphWidth);
                } finally {
                    row.dispose();
                    row = null;
                }
            }
        } finally {
            if (g != null) {
                g.dispose();
                g = null;
            }
        }
        // write the image buffer to the client
        response.setContentType("image/png");
        if (numCommits > 1) {
            response.setHeader("Cache-Control", "public, max-age=60, must-revalidate");
            response.setDateHeader("Last-Modified", JGitUtils.getCommitDate(commitList.get(0)).getTime());
        }
        OutputStream os = response.getOutputStream();
        ImageIO.write(image, "png", os);
        os.flush();
        image.flush();
        image = null;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (is != null) {
            is.close();
            is = null;
        }
        if (rw != null) {
            rw.dispose();
            rw = null;
        }
        if (r != null) {
            r.close();
            r = null;
        }
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) PlotLane(org.eclipse.jgit.revplot.PlotLane) PlotCommitList(org.eclipse.jgit.revplot.PlotCommitList) BufferedImage(java.awt.image.BufferedImage) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Graphics2D(java.awt.Graphics2D) Graphics(java.awt.Graphics) Repository(org.eclipse.jgit.lib.Repository) TreeSet(java.util.TreeSet) PlotWalk(org.eclipse.jgit.revplot.PlotWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

ObjectId (org.eclipse.jgit.lib.ObjectId)355 Test (org.junit.Test)128 RevCommit (org.eclipse.jgit.revwalk.RevCommit)123 RevWalk (org.eclipse.jgit.revwalk.RevWalk)84 Repository (org.eclipse.jgit.lib.Repository)63 IOException (java.io.IOException)61 Change (com.google.gerrit.reviewdb.client.Change)41 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)39 ArrayList (java.util.ArrayList)36 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)34 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)34 Ref (org.eclipse.jgit.lib.Ref)34 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)33 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)25 AnyObjectId (org.eclipse.jgit.lib.AnyObjectId)24 RefUpdate (org.eclipse.jgit.lib.RefUpdate)22 NoteMap (org.eclipse.jgit.notes.NoteMap)22 Map (java.util.Map)21 OrmException (com.google.gwtorm.server.OrmException)20 ObjectReader (org.eclipse.jgit.lib.ObjectReader)19