Search in sources :

Example 6 with ObjectReader

use of org.eclipse.jgit.lib.ObjectReader in project gerrit by GerritCodeReview.

the class AbstractQueryChangesTest method newPatchSet.

protected Change newPatchSet(TestRepository<Repo> repo, Change c) throws Exception {
    // Add a new file so the patch set is not a trivial rebase, to avoid default
    // Code-Review label copying.
    int n = c.currentPatchSetId().get() + 1;
    RevCommit commit = repo.parseBody(repo.commit().message("message").add("file" + n, "contents " + n).create());
    ChangeControl ctl = changeControlFactory.controlFor(db, c, user);
    PatchSetInserter inserter = patchSetFactory.create(ctl, new PatchSet.Id(c.getId(), n), commit).setNotify(NotifyHandling.NONE).setFireRevisionCreated(false).setValidate(false);
    try (BatchUpdate bu = updateFactory.create(db, c.getProject(), user, TimeUtil.nowTs());
        ObjectInserter oi = repo.getRepository().newObjectInserter();
        ObjectReader reader = oi.newReader();
        RevWalk rw = new RevWalk(reader)) {
        bu.setRepository(repo.getRepository(), rw, oi);
        bu.addOp(c.getId(), inserter);
        bu.execute();
    }
    return inserter.getChange();
}
Also used : ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) PatchSetInserter(com.google.gerrit.server.change.PatchSetInserter) ChangeControl(com.google.gerrit.server.project.ChangeControl) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ObjectId(org.eclipse.jgit.lib.ObjectId) RevWalk(org.eclipse.jgit.revwalk.RevWalk) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 7 with ObjectReader

use of org.eclipse.jgit.lib.ObjectReader in project gitiles by GerritCodeReview.

the class DocServlet method doGetHtml.

@Override
protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException {
    MarkdownConfig cfg = MarkdownConfig.get(getAccess(req).getConfig());
    if (!cfg.render) {
        res.setStatus(SC_NOT_FOUND);
        return;
    }
    GitilesView view = ViewFilter.getView(req);
    Repository repo = ServletUtils.getRepository(req);
    try (RevWalk rw = new RevWalk(repo)) {
        ObjectReader reader = rw.getObjectReader();
        String path = view.getPathPart();
        RevTree root;
        try {
            root = rw.parseTree(view.getRevision().getId());
        } catch (IncorrectObjectTypeException e) {
            res.setStatus(SC_NOT_FOUND);
            return;
        }
        MarkdownFile srcmd = findFile(rw, root, path);
        if (srcmd == null) {
            res.setStatus(SC_NOT_FOUND);
            return;
        }
        MarkdownFile navmd = findFile(rw, root, NAVBAR_MD);
        String curEtag = etag(srcmd, navmd);
        if (etagMatch(req, curEtag)) {
            res.setStatus(SC_NOT_MODIFIED);
            return;
        }
        view = view.toBuilder().setPathPart(srcmd.path).build();
        try {
            srcmd.read(reader, cfg);
            if (navmd != null) {
                navmd.read(reader, cfg);
            }
        } catch (LargeObjectException.ExceedsLimit errBig) {
            fileTooBig(res, view, errBig);
            return;
        } catch (IOException err) {
            readError(res, view, err);
            return;
        }
        MarkdownToHtml.Builder fmt = MarkdownToHtml.builder().setConfig(cfg).setGitilesView(view).setRequestUri(req.getRequestURI()).setReader(reader).setRootTree(root);
        res.setHeader(HttpHeaders.ETAG, curEtag);
        showDoc(req, res, view, cfg, fmt, navmd, srcmd);
    }
}
Also used : GitilesView(com.google.gitiles.GitilesView) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) LargeObjectException(org.eclipse.jgit.errors.LargeObjectException) Repository(org.eclipse.jgit.lib.Repository) ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevTree(org.eclipse.jgit.revwalk.RevTree)

Example 8 with ObjectReader

use of org.eclipse.jgit.lib.ObjectReader in project gerrit by GerritCodeReview.

the class TreeCreator method readBaseTree.

private DirCache readBaseTree(Repository repository) throws IOException {
    try (ObjectReader objectReader = repository.newObjectReader()) {
        DirCache dirCache = DirCache.newInCore();
        DirCacheBuilder dirCacheBuilder = dirCache.builder();
        dirCacheBuilder.addTree(new byte[0], DirCacheEntry.STAGE_0, objectReader, baseCommit.getTree());
        dirCacheBuilder.finish();
        return dirCache;
    }
}
Also used : DirCache(org.eclipse.jgit.dircache.DirCache) DirCacheBuilder(org.eclipse.jgit.dircache.DirCacheBuilder) ObjectReader(org.eclipse.jgit.lib.ObjectReader)

Example 9 with ObjectReader

use of org.eclipse.jgit.lib.ObjectReader in project gerrit by GerritCodeReview.

the class AutoMerger method commit.

private RevCommit commit(Repository repo, RevWalk rw, @Nullable InMemoryInserter tmpIns, ObjectInserter ins, String refName, ObjectId tree, RevCommit merge) throws IOException {
    rw.parseHeaders(merge);
    // For maximum stability, choose a single ident using the committer time of
    // the input commit, using the server name and timezone.
    PersonIdent ident = new PersonIdent(gerritIdent, merge.getCommitterIdent().getWhen(), gerritIdent.getTimeZone());
    CommitBuilder cb = new CommitBuilder();
    cb.setAuthor(ident);
    cb.setCommitter(ident);
    cb.setTreeId(tree);
    cb.setMessage("Auto-merge of " + merge.name() + '\n');
    for (RevCommit p : merge.getParents()) {
        cb.addParentId(p);
    }
    if (!save) {
        checkArgument(tmpIns != null);
        try (ObjectReader tmpReader = tmpIns.newReader();
            RevWalk tmpRw = new RevWalk(tmpReader)) {
            return tmpRw.parseCommit(tmpIns.insert(cb));
        }
    }
    checkArgument(tmpIns == null);
    checkArgument(!(ins instanceof InMemoryInserter));
    ObjectId commitId = ins.insert(cb);
    ins.flush();
    RefUpdate ru = repo.updateRef(refName);
    ru.setNewObjectId(commitId);
    ru.disableRefLog();
    ru.forceUpdate();
    return rw.parseCommit(commitId);
}
Also used : PersonIdent(org.eclipse.jgit.lib.PersonIdent) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) ObjectReader(org.eclipse.jgit.lib.ObjectReader) InMemoryInserter(com.google.gerrit.server.git.InMemoryInserter) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Example 10 with ObjectReader

use of org.eclipse.jgit.lib.ObjectReader in project gerrit by GerritCodeReview.

the class NoteDbUpdateManager method openRepo.

private OpenRepo openRepo(Project.NameKey p) throws IOException {
    // Closed by OpenRepo#close.
    Repository repo = repoManager.openRepository(p);
    // Closed by OpenRepo#close.
    ObjectInserter ins = repo.newObjectInserter();
    // Not closed by OpenRepo#close.
    ObjectReader reader = ins.newReader();
    try (RevWalk rw = new RevWalk(reader)) {
        // Doesn't escape OpenRepo constructor.
        return new OpenRepo(repo, rw, ins, new ChainedReceiveCommands(repo), true) {

            @Override
            public void close() {
                reader.close();
                super.close();
            }
        };
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ChainedReceiveCommands(com.google.gerrit.server.update.ChainedReceiveCommands) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevWalk(org.eclipse.jgit.revwalk.RevWalk)

Aggregations

ObjectReader (org.eclipse.jgit.lib.ObjectReader)41 RevWalk (org.eclipse.jgit.revwalk.RevWalk)29 ObjectId (org.eclipse.jgit.lib.ObjectId)21 RevCommit (org.eclipse.jgit.revwalk.RevCommit)21 Repository (org.eclipse.jgit.lib.Repository)17 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)16 IOException (java.io.IOException)11 Change (com.google.gerrit.reviewdb.client.Change)10 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)10 RevTree (org.eclipse.jgit.revwalk.RevTree)10 DiffEntry (org.eclipse.jgit.diff.DiffEntry)7 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)6 ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)6 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)6 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)6 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)5 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)5 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)4 Project (com.google.gerrit.reviewdb.client.Project)4 RevId (com.google.gerrit.reviewdb.client.RevId)4