Example usage for org.eclipse.jgit.lib ObjectId fromString

List of usage examples for org.eclipse.jgit.lib ObjectId fromString

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib ObjectId fromString.

Prototype

public static ObjectId fromString(String str) 

Source Link

Document

Convert an ObjectId from hex characters.

Usage

From source file:com.google.gerrit.server.change.FileInfoJson.java

License:Apache License

Map<String, FileInfo> toFileInfoMap(Change change, RevId revision, @Nullable PatchSet base)
        throws PatchListNotAvailableException {
    ObjectId a = (base == null) ? null : ObjectId.fromString(base.getRevision().get());
    ObjectId b = ObjectId.fromString(revision.get());
    PatchList list = patchListCache.get(new PatchListKey(a, b, Whitespace.IGNORE_NONE), change.getProject());

    Map<String, FileInfo> files = Maps.newTreeMap();
    for (PatchListEntry e : list.getPatches()) {
        FileInfo d = new FileInfo();
        d.status = e.getChangeType() != Patch.ChangeType.MODIFIED ? e.getChangeType().getCode() : null;
        d.oldPath = e.getOldName();/*  w w w . j a v  a2 s.co m*/
        if (e.getPatchType() == Patch.PatchType.BINARY) {
            d.binary = true;
        } else {
            d.linesInserted = e.getInsertions() > 0 ? e.getInsertions() : null;
            d.linesDeleted = e.getDeletions() > 0 ? e.getDeletions() : null;
        }

        FileInfo o = files.put(e.getNewName(), d);
        if (o != null) {
            // This should only happen on a delete-add break created by JGit
            // when the file was rewritten and too little content survived. Write
            // a single record with data from both sides.
            d.status = Patch.ChangeType.REWRITE.getCode();
            if (o.binary != null && o.binary) {
                d.binary = true;
            }
            if (o.linesInserted != null) {
                d.linesInserted = o.linesInserted;
            }
            if (o.linesDeleted != null) {
                d.linesDeleted = o.linesDeleted;
            }
        }
    }
    return files;
}

From source file:com.google.gerrit.server.change.GetArchive.java

License:Apache License

@Override
public BinaryResult apply(RevisionResource rsrc)
        throws BadRequestException, IOException, MethodNotAllowedException {
    if (Strings.isNullOrEmpty(format)) {
        throw new BadRequestException("format is not specified");
    }//from w w w  .j  a  v  a 2s . c  o m
    final ArchiveFormat f = allowedFormats.extensions.get("." + format);
    if (f == null) {
        throw new BadRequestException("unknown archive format");
    }
    if (f == ArchiveFormat.ZIP) {
        throw new MethodNotAllowedException("zip format is disabled");
    }
    boolean close = true;
    final Repository repo = repoManager.openRepository(rsrc.getControl().getProject().getNameKey());
    try {
        final RevCommit commit;
        String name;
        try (RevWalk rw = new RevWalk(repo)) {
            commit = rw.parseCommit(ObjectId.fromString(rsrc.getPatchSet().getRevision().get()));
            name = name(f, rw, commit);
        }

        BinaryResult bin = new BinaryResult() {
            @Override
            public void writeTo(OutputStream out) throws IOException {
                try {
                    new ArchiveCommand(repo).setFormat(f.name()).setTree(commit.getTree()).setOutputStream(out)
                            .call();
                } catch (GitAPIException e) {
                    throw new IOException(e);
                }
            }

            @Override
            public void close() throws IOException {
                repo.close();
            }
        };

        bin.disableGzip().setContentType(f.getMimeType()).setAttachmentName(name);

        close = false;
        return bin;
    } finally {
        if (close) {
            repo.close();
        }
    }
}

From source file:com.google.gerrit.server.change.GetCommit.java

License:Apache License

@Override
public Response<CommitInfo> apply(RevisionResource rsrc) throws IOException {
    Project.NameKey p = rsrc.getChange().getProject();
    try (Repository repo = repoManager.openRepository(p); RevWalk rw = new RevWalk(repo)) {
        String rev = rsrc.getPatchSet().getRevision().get();
        RevCommit commit = rw.parseCommit(ObjectId.fromString(rev));
        rw.parseBody(commit);/*w ww  .j a v  a2  s . co  m*/
        CommitInfo info = json.create(ChangeJson.NO_OPTIONS).toCommit(rsrc.getControl(), rw, commit, addLinks);
        info.commit = commit.name();
        Response<CommitInfo> r = Response.ok(info);
        if (rsrc.isCacheable()) {
            r.caching(CacheControl.PRIVATE(7, TimeUnit.DAYS));
        }
        return r;
    }
}

From source file:com.google.gerrit.server.change.GetContent.java

License:Apache License

@Override
public BinaryResult apply(FileResource rsrc)
        throws ResourceNotFoundException, IOException, NoSuchChangeException, OrmException {
    String path = rsrc.getPatchKey().get();
    if (Patch.COMMIT_MSG.equals(path)) {
        String msg = changeUtil.getMessage(rsrc.getRevision().getChange());
        return BinaryResult.create(msg).setContentType(FileContentUtil.TEXT_X_GERRIT_COMMIT_MESSAGE).base64();
    }// ww  w . ja va2  s  . com
    return fileContentUtil.getContent(rsrc.getRevision().getControl().getProjectControl().getProjectState(),
            ObjectId.fromString(rsrc.getRevision().getPatchSet().getRevision().get()), path);
}

From source file:com.google.gerrit.server.change.GetMergeList.java

License:Apache License

@Override
public Response<List<CommitInfo>> apply(RevisionResource rsrc) throws BadRequestException, IOException {
    Project.NameKey p = rsrc.getChange().getProject();
    try (Repository repo = repoManager.openRepository(p); RevWalk rw = new RevWalk(repo)) {
        String rev = rsrc.getPatchSet().getRevision().get();
        RevCommit commit = rw.parseCommit(ObjectId.fromString(rev));
        rw.parseBody(commit);/*w  ww .j  ava  2  s . c  o  m*/

        if (uninterestingParent < 1 || uninterestingParent > commit.getParentCount()) {
            throw new BadRequestException("No such parent: " + uninterestingParent);
        }

        if (commit.getParentCount() < 2) {
            return createResponse(rsrc, ImmutableList.<CommitInfo>of());
        }

        List<RevCommit> commits = MergeListBuilder.build(rw, commit, uninterestingParent);
        List<CommitInfo> result = new ArrayList<>(commits.size());
        ChangeJson changeJson = json.noOptions();
        for (RevCommit c : commits) {
            result.add(changeJson.toCommit(rsrc.getControl(), rw, c, addLinks, true));
        }
        return createResponse(rsrc, result);
    }
}

From source file:com.google.gerrit.server.change.GetPatch.java

License:Apache License

@Override
public BinaryResult apply(RevisionResource rsrc) throws ResourceConflictException, IOException {
    Project.NameKey project = rsrc.getControl().getProject().getNameKey();
    final Repository repo = repoManager.openRepository(project);
    boolean close = true;
    try {//  w w  w .j  a  v  a  2 s . c  o m
        final RevWalk rw = new RevWalk(repo);
        try {
            final RevCommit commit = rw
                    .parseCommit(ObjectId.fromString(rsrc.getPatchSet().getRevision().get()));
            RevCommit[] parents = commit.getParents();
            if (parents.length > 1) {
                throw new ResourceConflictException("Revision has more than 1 parent.");
            } else if (parents.length == 0) {
                throw new ResourceConflictException("Revision has no parent.");
            }
            final RevCommit base = parents[0];
            rw.parseBody(base);

            BinaryResult bin = new BinaryResult() {
                @Override
                public void writeTo(OutputStream out) throws IOException {
                    if (zip) {
                        ZipOutputStream zos = new ZipOutputStream(out);
                        ZipEntry e = new ZipEntry(fileName(rw, commit));
                        e.setTime(commit.getCommitTime() * 1000L);
                        zos.putNextEntry(e);
                        format(zos);
                        zos.closeEntry();
                        zos.finish();
                    } else {
                        format(out);
                    }
                }

                private void format(OutputStream out) throws IOException {
                    out.write(formatEmailHeader(commit).getBytes(UTF_8));
                    try (DiffFormatter fmt = new DiffFormatter(out)) {
                        fmt.setRepository(repo);
                        fmt.format(base.getTree(), commit.getTree());
                        fmt.flush();
                    }
                }

                @Override
                public void close() throws IOException {
                    rw.close();
                    repo.close();
                }
            };

            if (zip) {
                bin.disableGzip().setContentType("application/zip")
                        .setAttachmentName(fileName(rw, commit) + ".zip");
            } else {
                bin.base64().setContentType("application/mbox")
                        .setAttachmentName(download ? fileName(rw, commit) + ".base64" : null);
            }

            close = false;
            return bin;
        } finally {
            if (close) {
                rw.close();
            }
        }
    } finally {
        if (close) {
            repo.close();
        }
    }
}

From source file:com.google.gerrit.server.change.GetRelatedByAncestors.java

License:Apache License

private List<ChangeAndCommit> walk(RevisionResource rsrc, RevWalk rw, Ref ref)
        throws OrmException, IOException {
    Map<Change.Id, ChangeData> changes = allOpenChanges(rsrc);
    Map<PatchSet.Id, PatchSet> patchSets = allPatchSets(rsrc, changes.values());

    Map<String, PatchSet> commits = Maps.newHashMap();
    for (PatchSet p : patchSets.values()) {
        commits.put(p.getRevision().get(), p);
    }/* ww  w .  ja  va 2s .  c  o  m*/

    RevCommit rev = rw.parseCommit(ObjectId.fromString(rsrc.getPatchSet().getRevision().get()));
    rw.sort(RevSort.TOPO);
    rw.markStart(rev);

    if (ref != null && ref.getObjectId() != null) {
        try {
            rw.markUninteresting(rw.parseCommit(ref.getObjectId()));
        } catch (IncorrectObjectTypeException notCommit) {
            // Ignore and treat as new branch.
        }
    }

    Set<Change.Id> added = Sets.newHashSet();
    List<ChangeAndCommit> parents = Lists.newArrayList();
    for (RevCommit c; (c = rw.next()) != null;) {
        PatchSet p = commits.get(c.name());
        Change g = null;
        if (p != null) {
            g = changes.get(p.getId().getParentKey()).change();
            added.add(p.getId().getParentKey());
        }
        parents.add(new ChangeAndCommit(g, p, c));
    }
    List<ChangeAndCommit> list = children(rsrc, rw, changes, patchSets, added);
    list.addAll(parents);

    if (list.size() == 1) {
        ChangeAndCommit r = list.get(0);
        if (r.commit != null && r.commit.commit.equals(rsrc.getPatchSet().getRevision().get())) {
            return Collections.emptyList();
        }
    }
    return list;
}

From source file:com.google.gerrit.server.change.GetRelatedByAncestors.java

License:Apache License

private List<ChangeAndCommit> children(RevisionResource rsrc, RevWalk rw, Map<Change.Id, ChangeData> changes,
        Map<PatchSet.Id, PatchSet> patchSets, Set<Change.Id> added) throws OrmException, IOException {
    // children is a map of parent commit name to PatchSet built on it.
    Multimap<String, PatchSet.Id> children = allChildren(changes.keySet());

    RevFlag seenCommit = rw.newFlag("seenCommit");
    LinkedList<String> q = Lists.newLinkedList();
    seedQueue(rsrc, rw, seenCommit, patchSets, q);

    ProjectControl projectCtl = rsrc.getControl().getProjectControl();
    Set<Change.Id> seenChange = Sets.newHashSet();
    List<ChangeAndCommit> graph = Lists.newArrayList();
    while (!q.isEmpty()) {
        String id = q.remove();/*from   ww w  . j av  a 2  s .c om*/

        // For every matching change find the most recent patch set.
        Map<Change.Id, PatchSet.Id> matches = Maps.newHashMap();
        for (PatchSet.Id psId : children.get(id)) {
            PatchSet.Id e = matches.get(psId.getParentKey());
            if ((e == null || e.get() < psId.get()) && isVisible(projectCtl, changes, patchSets, psId)) {
                matches.put(psId.getParentKey(), psId);
            }
        }

        for (Map.Entry<Change.Id, PatchSet.Id> e : matches.entrySet()) {
            ChangeData cd = changes.get(e.getKey());
            PatchSet ps = patchSets.get(e.getValue());
            if (cd == null || ps == null || !seenChange.add(e.getKey())) {
                continue;
            }

            RevCommit c = rw.parseCommit(ObjectId.fromString(ps.getRevision().get()));
            if (!c.has(seenCommit)) {
                c.add(seenCommit);
                q.addFirst(ps.getRevision().get());
                if (added.add(ps.getId().getParentKey())) {
                    rw.parseBody(c);
                    graph.add(new ChangeAndCommit(cd.change(), ps, c));
                }
            }
        }
    }
    Collections.reverse(graph);
    return graph;
}

From source file:com.google.gerrit.server.change.GetRelatedByAncestors.java

License:Apache License

private void seedQueue(RevisionResource rsrc, RevWalk rw, RevFlag seenCommit,
        Map<PatchSet.Id, PatchSet> patchSets, LinkedList<String> q) throws IOException {
    RevCommit tip = rw.parseCommit(ObjectId.fromString(rsrc.getPatchSet().getRevision().get()));
    tip.add(seenCommit);//ww w . ja v a2 s .c  o  m
    q.add(tip.name());

    Change.Id cId = rsrc.getChange().getId();
    for (PatchSet p : patchSets.values()) {
        if (cId.equals(p.getId().getParentKey())) {
            try {
                RevCommit c = rw.parseCommit(ObjectId.fromString(p.getRevision().get()));
                if (!c.has(seenCommit)) {
                    c.add(seenCommit);
                    q.add(c.name());
                }
            } catch (IOException e) {
                log.warn(String.format("Cannot read patch set %d of %d", p.getPatchSetId(), cId.get()), e);
            }
        }
    }
}

From source file:com.google.gerrit.server.change.IncludedIn.java

License:Apache License

@Override
public IncludedInInfo apply(ChangeResource rsrc)
        throws BadRequestException, ResourceConflictException, OrmException, IOException {
    ChangeControl ctl = rsrc.getControl();
    PatchSet ps = db.get().patchSets().get(ctl.getChange().currentPatchSetId());
    Project.NameKey project = ctl.getProject().getNameKey();
    try (Repository r = repoManager.openRepository(project); RevWalk rw = new RevWalk(r)) {
        rw.setRetainBody(false);/*from www .  ja  v  a  2s  . co  m*/
        RevCommit rev;
        try {
            rev = rw.parseCommit(ObjectId.fromString(ps.getRevision().get()));
        } catch (IncorrectObjectTypeException err) {
            throw new BadRequestException(err.getMessage());
        } catch (MissingObjectException err) {
            throw new ResourceConflictException(err.getMessage());
        }

        IncludedInDetail d = IncludedInResolver.resolve(r, rw, rev);
        Map<String, Collection<String>> external = new HashMap<>();
        for (DynamicMap.Entry<ExternalIncludedIn> i : includedIn) {
            external.put(i.getExportName(), i.getProvider().get().getIncludedIn(project.get(), rev.name(),
                    d.getTags(), d.getBranches()));
        }
        return new IncludedInInfo(d, (!external.isEmpty() ? external : null));
    }
}