Search in sources :

Example 21 with RevObject

use of org.eclipse.jgit.revwalk.RevObject in project gerrit by GerritCodeReview.

the class ReceiveCommits method parseCreate.

private void parseCreate(ReceiveCommand cmd) {
    RevObject obj;
    try {
        obj = rp.getRevWalk().parseAny(cmd.getNewId());
    } catch (IOException err) {
        logError("Invalid object " + cmd.getNewId().name() + " for " + cmd.getRefName() + " creation", err);
        reject(cmd, "invalid object");
        return;
    }
    logDebug("Creating {}", cmd);
    if (isHead(cmd) && !isCommit(cmd)) {
        return;
    }
    RefControl ctl = projectControl.controlForRef(cmd.getRefName());
    if (ctl.canCreate(db, rp.getRepository(), obj)) {
        if (!validRefOperation(cmd)) {
            return;
        }
        validateNewCommits(ctl, cmd);
        actualCommands.add(cmd);
    } else {
        reject(cmd, "prohibited by Gerrit: create access denied for " + cmd.getRefName());
    }
}
Also used : RevObject(org.eclipse.jgit.revwalk.RevObject) RefControl(com.google.gerrit.server.project.RefControl) IOException(java.io.IOException)

Example 22 with RevObject

use of org.eclipse.jgit.revwalk.RevObject in project gerrit by GerritCodeReview.

the class ProjectConfigTest method text.

private String text(RevCommit rev, String path) throws Exception {
    RevObject blob = util.get(rev.getTree(), path);
    byte[] data = db.open(blob).getCachedBytes(Integer.MAX_VALUE);
    return RawParseUtils.decode(data);
}
Also used : RevObject(org.eclipse.jgit.revwalk.RevObject)

Example 23 with RevObject

use of org.eclipse.jgit.revwalk.RevObject in project gerrit by GerritCodeReview.

the class PatchListLoader method readPatchList.

public PatchList readPatchList(Repository repo, RevWalk rw, ObjectInserter ins) throws IOException, PatchListNotAvailableException {
    ObjectReader reader = rw.getObjectReader();
    checkArgument(reader.getCreatedFromInserter() == ins);
    RawTextComparator cmp = comparatorFor(key.getWhitespace());
    try (DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE)) {
        RevCommit b = rw.parseCommit(key.getNewId());
        RevObject a = aFor(key, repo, rw, ins, b);
        if (a == null) {
            // TODO(sop) Remove this case.
            // This is an octopus merge commit which should be compared against the
            // auto-merge. However since we don't support computing the auto-merge
            // for octopus merge commits, we fall back to diffing against the first
            // parent, even though this wasn't what was requested.
            //
            ComparisonType comparisonType = ComparisonType.againstParent(1);
            PatchListEntry[] entries = new PatchListEntry[2];
            entries[0] = newCommitMessage(cmp, reader, null, b);
            entries[1] = newMergeList(cmp, reader, null, b, comparisonType);
            return new PatchList(a, b, true, comparisonType, entries);
        }
        ComparisonType comparisonType = getComparisonType(a, b);
        RevCommit aCommit = a instanceof RevCommit ? (RevCommit) a : null;
        RevTree aTree = rw.parseTree(a);
        RevTree bTree = b.getTree();
        df.setReader(reader, repo.getConfig());
        df.setDiffComparator(cmp);
        df.setDetectRenames(true);
        List<DiffEntry> diffEntries = df.scan(aTree, bTree);
        Set<String> paths = null;
        if (key.getOldId() != null && b.getParentCount() == 1) {
            PatchListKey newKey = PatchListKey.againstDefaultBase(key.getNewId(), key.getWhitespace());
            PatchListKey oldKey = PatchListKey.againstDefaultBase(key.getOldId(), key.getWhitespace());
            paths = Stream.concat(patchListCache.get(newKey, project).getPatches().stream(), patchListCache.get(oldKey, project).getPatches().stream()).map(PatchListEntry::getNewName).collect(toSet());
        }
        int cnt = diffEntries.size();
        List<PatchListEntry> entries = new ArrayList<>();
        entries.add(newCommitMessage(cmp, reader, comparisonType.isAgainstParentOrAutoMerge() ? null : aCommit, b));
        boolean isMerge = b.getParentCount() > 1;
        if (isMerge) {
            entries.add(newMergeList(cmp, reader, comparisonType.isAgainstParentOrAutoMerge() ? null : aCommit, b, comparisonType));
        }
        for (int i = 0; i < cnt; i++) {
            DiffEntry e = diffEntries.get(i);
            if (paths == null || paths.contains(e.getNewPath()) || paths.contains(e.getOldPath())) {
                FileHeader fh = toFileHeader(key, df, e);
                long oldSize = getFileSize(reader, e.getOldMode(), e.getOldPath(), aTree);
                long newSize = getFileSize(reader, e.getNewMode(), e.getNewPath(), bTree);
                entries.add(newEntry(aTree, fh, newSize, newSize - oldSize));
            }
        }
        return new PatchList(a, b, isMerge, comparisonType, entries.toArray(new PatchListEntry[entries.size()]));
    }
}
Also used : RevObject(org.eclipse.jgit.revwalk.RevObject) RawTextComparator(org.eclipse.jgit.diff.RawTextComparator) ArrayList(java.util.ArrayList) ObjectReader(org.eclipse.jgit.lib.ObjectReader) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) FileHeader(org.eclipse.jgit.patch.FileHeader) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 24 with RevObject

use of org.eclipse.jgit.revwalk.RevObject in project gitiles by GerritCodeReview.

the class RevisionServlet method doGetJson.

@Override
protected void doGetJson(HttpServletRequest req, HttpServletResponse res) throws IOException {
    GitilesView view = ViewFilter.getView(req);
    Repository repo = ServletUtils.getRepository(req);
    try (RevWalk walk = new RevWalk(repo)) {
        DateFormatter df = new DateFormatter(getAccess(req), Format.DEFAULT);
        RevObject obj = walk.parseAny(view.getRevision().getId());
        switch(obj.getType()) {
            case OBJ_COMMIT:
                renderJson(req, res, new CommitJsonData().setRevWalk(walk).toJsonData(req, (RevCommit) obj, COMMIT_JSON_FIELDS, df), Commit.class);
                break;
            default:
                // TODO(dborowitz): Support showing other types.
                res.setStatus(SC_NOT_FOUND);
                break;
        }
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) RevObject(org.eclipse.jgit.revwalk.RevObject) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 25 with RevObject

use of org.eclipse.jgit.revwalk.RevObject in project gitiles by GerritCodeReview.

the class RepositoryIndexServlet method renderReadme.

private static Map<String, Object> renderReadme(HttpServletRequest req, RevWalk walk, GitilesView view, Config cfg, RevObject head) throws IOException {
    RevTree rootTree;
    try {
        rootTree = walk.parseTree(head);
    } catch (IncorrectObjectTypeException notTreeish) {
        return null;
    }
    ReadmeHelper readme = new ReadmeHelper(walk.getObjectReader(), GitilesView.path().copyFrom(view).setRevision(Revision.HEAD).setPathPart("/").build(), MarkdownConfig.get(cfg), rootTree, req.getRequestURI());
    readme.scanTree(rootTree);
    if (readme.isPresent()) {
        SanitizedContent html = readme.render();
        if (html != null) {
            return ImmutableMap.<String, Object>of("readmeHtml", html);
        }
    }
    return null;
}
Also used : SanitizedContent(com.google.template.soy.data.SanitizedContent) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) RevObject(org.eclipse.jgit.revwalk.RevObject) RevTree(org.eclipse.jgit.revwalk.RevTree)

Aggregations

RevObject (org.eclipse.jgit.revwalk.RevObject)25 RevWalk (org.eclipse.jgit.revwalk.RevWalk)14 IOException (java.io.IOException)10 ObjectId (org.eclipse.jgit.lib.ObjectId)10 RevCommit (org.eclipse.jgit.revwalk.RevCommit)10 Repository (org.eclipse.jgit.lib.Repository)8 Ref (org.eclipse.jgit.lib.Ref)7 RevTag (org.eclipse.jgit.revwalk.RevTag)6 RevTree (org.eclipse.jgit.revwalk.RevTree)6 Map (java.util.Map)4 IncorrectObjectTypeException (org.eclipse.jgit.errors.IncorrectObjectTypeException)4 TagCommand (org.eclipse.jgit.api.TagCommand)3 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)3 PersonIdent (org.eclipse.jgit.lib.PersonIdent)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 AuthException (com.google.gerrit.extensions.restapi.AuthException)2 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)2 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)2 OutputStream (java.io.OutputStream)2 Writer (java.io.Writer)2