Search in sources :

Example 1 with ArchiveCommand

use of org.eclipse.jgit.api.ArchiveCommand in project gerrit by GerritCodeReview.

the class GetArchive method apply.

@Override
public BinaryResult apply(RevisionResource rsrc) throws BadRequestException, IOException, MethodNotAllowedException {
    if (Strings.isNullOrEmpty(format)) {
        throw new BadRequestException("format is not specified");
    }
    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();
        }
    }
}
Also used : MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) OutputStream(java.io.OutputStream) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ArchiveCommand(org.eclipse.jgit.api.ArchiveCommand) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Repository(org.eclipse.jgit.lib.Repository) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult)

Example 2 with ArchiveCommand

use of org.eclipse.jgit.api.ArchiveCommand in project gerrit by GerritCodeReview.

the class UploadArchive method runImpl.

@Override
protected void runImpl() throws IOException, PermissionBackendException, Failure {
    PacketLineOut packetOut = new PacketLineOut(out);
    packetOut.setFlushOnEnd(true);
    packetOut.writeString("ACK");
    packetOut.end();
    try {
        // Parse Git arguments
        readArguments();
        ArchiveFormat f = allowedFormats.getExtensions().get("." + options.format);
        if (f == null) {
            throw new Failure(3, "fatal: upload-archive not permitted");
        }
        // Find out the object to get from the specified reference and paths
        ObjectId treeId = repo.resolve(options.treeIsh);
        if (treeId == null) {
            throw new Failure(4, "fatal: reference not found");
        }
        // Verify the user has permissions to read the specified tree.
        if (!canRead(treeId)) {
            throw new Failure(5, "fatal: cannot perform upload-archive operation");
        }
        // The archive is sent in DATA sideband channel
        try (SideBandOutputStream sidebandOut = new SideBandOutputStream(SideBandOutputStream.CH_DATA, SideBandOutputStream.MAX_BUF, out)) {
            new ArchiveCommand(repo).setFormat(f.name()).setFormatOptions(getFormatOptions(f)).setTree(treeId).setPaths(options.path.toArray(new String[0])).setPrefix(options.prefix).setOutputStream(sidebandOut).call();
            sidebandOut.flush();
        } catch (GitAPIException e) {
            throw new Failure(7, "fatal: git api exception, " + e);
        }
    } catch (Failure f) {
        // Report the error in ERROR sideband channel
        try (SideBandOutputStream sidebandError = new SideBandOutputStream(SideBandOutputStream.CH_ERROR, SideBandOutputStream.MAX_BUF, out)) {
            sidebandError.write(f.getMessage().getBytes(UTF_8));
            sidebandError.flush();
        }
        throw f;
    } finally {
        // In any case, cleanly close the packetOut channel
        packetOut.end();
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) PacketLineOut(org.eclipse.jgit.transport.PacketLineOut) ArchiveFormat(com.google.gerrit.server.change.ArchiveFormat) ObjectId(org.eclipse.jgit.lib.ObjectId) SideBandOutputStream(org.eclipse.jgit.transport.SideBandOutputStream) ArchiveCommand(org.eclipse.jgit.api.ArchiveCommand)

Example 3 with ArchiveCommand

use of org.eclipse.jgit.api.ArchiveCommand in project gitiles by GerritCodeReview.

the class ArchiveServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    GitilesView view = ViewFilter.getView(req);
    Revision rev = view.getRevision();
    Repository repo = ServletUtils.getRepository(req);
    ObjectId treeId = getTree(view, repo, rev);
    if (treeId.equals(ObjectId.zeroId())) {
        res.sendError(SC_NOT_FOUND);
        return;
    }
    Optional<ArchiveFormat> format = ArchiveFormat.byExtension(view.getExtension(), getAccess(req).getConfig());
    if (!format.isPresent()) {
        res.setStatus(SC_NOT_FOUND);
        return;
    }
    String filename = getFilename(view, rev, view.getExtension());
    setDownloadHeaders(req, res, filename, format.get().getMimeType());
    res.setStatus(SC_OK);
    try {
        new ArchiveCommand(repo).setFormat(format.get().getRegisteredName()).setTree(treeId).setOutputStream(res.getOutputStream()).call();
    } catch (GitAPIException e) {
        throw new IOException(e);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) ArchiveCommand(org.eclipse.jgit.api.ArchiveCommand)

Aggregations

ArchiveCommand (org.eclipse.jgit.api.ArchiveCommand)3 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)3 IOException (java.io.IOException)2 ObjectId (org.eclipse.jgit.lib.ObjectId)2 Repository (org.eclipse.jgit.lib.Repository)2 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 BinaryResult (com.google.gerrit.extensions.restapi.BinaryResult)1 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)1 ArchiveFormat (com.google.gerrit.server.change.ArchiveFormat)1 OutputStream (java.io.OutputStream)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1 PacketLineOut (org.eclipse.jgit.transport.PacketLineOut)1 SideBandOutputStream (org.eclipse.jgit.transport.SideBandOutputStream)1