Search in sources :

Example 1 with ArchiveFormatInternal

use of com.google.gerrit.server.change.ArchiveFormatInternal 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();
        ArchiveFormatInternal f = allowedFormats.getExtensions().get("." + options.format);
        if (f == null) {
            throw new Failure(3, "fatal: upload-archive not permitted for format " + options.format);
        }
        // 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: " + options.treeIsh);
        }
        // Verify the user has permissions to read the specified tree.
        if (!canRead(treeId)) {
            throw new Failure(5, "fatal: no permission to read tree" + options.treeIsh);
        }
        // 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 (Exception e) {
        // NoClassDefFound.
        try (SideBandOutputStream sidebandError = new SideBandOutputStream(SideBandOutputStream.CH_ERROR, SideBandOutputStream.MAX_BUF, out)) {
            sidebandError.write(e.getMessage().getBytes(UTF_8));
            sidebandError.flush();
        }
        throw e;
    } 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) ObjectId(org.eclipse.jgit.lib.ObjectId) SideBandOutputStream(org.eclipse.jgit.transport.SideBandOutputStream) ArchiveCommand(org.eclipse.jgit.api.ArchiveCommand) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) AuthException(com.google.gerrit.extensions.restapi.AuthException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) CmdLineException(org.kohsuke.args4j.CmdLineException) ArchiveFormatInternal(com.google.gerrit.server.change.ArchiveFormatInternal)

Example 2 with ArchiveFormatInternal

use of com.google.gerrit.server.change.ArchiveFormatInternal in project gerrit by GerritCodeReview.

the class GetServerInfo method getDownloadInfo.

private DownloadInfo getDownloadInfo() {
    DownloadInfo info = new DownloadInfo();
    info.schemes = new HashMap<>();
    downloadSchemes.runEach(extension -> {
        DownloadScheme scheme = extension.get();
        if (scheme.isEnabled() && scheme.getUrl("${project}") != null) {
            info.schemes.put(extension.getExportName(), getDownloadSchemeInfo(scheme));
        }
    });
    info.archives = archiveFormats.getAllowed().stream().map(ArchiveFormatInternal::getShortName).collect(toList());
    return info;
}
Also used : DownloadInfo(com.google.gerrit.extensions.common.DownloadInfo) DownloadScheme(com.google.gerrit.extensions.config.DownloadScheme) ArchiveFormatInternal(com.google.gerrit.server.change.ArchiveFormatInternal)

Example 3 with ArchiveFormatInternal

use of com.google.gerrit.server.change.ArchiveFormatInternal in project gerrit by GerritCodeReview.

the class GetArchive method apply.

@Override
public Response<BinaryResult> apply(RevisionResource rsrc) throws BadRequestException, IOException, MethodNotAllowedException {
    if (Strings.isNullOrEmpty(format)) {
        throw new BadRequestException("format is not specified");
    }
    ArchiveFormatInternal f = allowedFormats.extensions.get("." + format);
    if (f == null) {
        throw new BadRequestException("unknown archive format");
    }
    if (f == ArchiveFormatInternal.ZIP) {
        throw new MethodNotAllowedException("zip format is disabled");
    }
    boolean close = true;
    Repository repo = repoManager.openRepository(rsrc.getProject());
    try {
        RevCommit commit;
        String name;
        try (RevWalk rw = new RevWalk(repo)) {
            commit = rw.parseCommit(rsrc.getPatchSet().commitId());
            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 Response.ok(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) ArchiveFormatInternal(com.google.gerrit.server.change.ArchiveFormatInternal) RevCommit(org.eclipse.jgit.revwalk.RevCommit) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult)

Aggregations

ArchiveFormatInternal (com.google.gerrit.server.change.ArchiveFormatInternal)3 IOException (java.io.IOException)2 ArchiveCommand (org.eclipse.jgit.api.ArchiveCommand)2 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)2 DownloadInfo (com.google.gerrit.extensions.common.DownloadInfo)1 DownloadScheme (com.google.gerrit.extensions.config.DownloadScheme)1 AuthException (com.google.gerrit.extensions.restapi.AuthException)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 BinaryResult (com.google.gerrit.extensions.restapi.BinaryResult)1 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)1 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)1 OutputStream (java.io.OutputStream)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 Repository (org.eclipse.jgit.lib.Repository)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 CmdLineException (org.kohsuke.args4j.CmdLineException)1