/** 커밋을 입력받으면 당시 파일들을 압축하여 사용자에게 보내줌. * @param commitName * @param format * @param response */ public void getRepositoryZip(String commitName,String format, HttpServletResponse response) { try { ArchiveCommand.registerFormat("zip", new ZipFormat()); ArchiveCommand.registerFormat("tar", new TarFormat()); ObjectId revId = this.localRepo.resolve(commitName); git.archive().setOutputStream(response.getOutputStream()) .setFormat(format) .setTree(revId) .call(); ArchiveCommand.unregisterFormat("zip"); ArchiveCommand.unregisterFormat("tar"); response.flushBuffer(); } catch (Exception e) { System.err.println(e.getMessage()); } }
/** 커밋을 입력받으면 당시 파일들을 압축하여 사용자에게 보내줌. * @param commitName * @param format * @param response */ public void getProjectZip(String commitName,String format, HttpServletResponse response) { try { ArchiveCommand.registerFormat("zip", new ZipFormat()); ArchiveCommand.registerFormat("tar", new TarFormat()); ObjectId revId = this.localRepo.resolve(commitName); git.archive().setOutputStream(response.getOutputStream()) .setFormat(format) .setTree(revId) .call(); ArchiveCommand.unregisterFormat("zip"); ArchiveCommand.unregisterFormat("tar"); response.flushBuffer(); } catch (Exception e) { System.err.println(e.getMessage()); } }
/** * Export the git tag identified by the entry id * * @param config * @param e * @param exportDirectory * @throws Exception */ public void getConfiguration(Configuration config, Entry e, File exportDirectory) throws Exception { Preconditions.checkNotNull(config, "Configuration must be supplied."); Preconditions.checkNotNull(e, "Entry must be supplied."); Preconditions.checkNotNull(exportDirectory, "Directory must be supplied."); File zip = new File(exportDirectory, e.getUUID().toString()+".zip"); ArchiveCommand.registerFormat("zip", new ZipFormat()); try (FileOutputStream out = new FileOutputStream(zip);){ LOG.debug("Exporting tag {} to {}", e.getUUID(), zip); git.archive().setTree(git.getRepository().resolve(e.getUUID().toString())) .setFormat("zip") .setOutputStream(out) .call(); unzip(zip); } catch (IOException | RevisionSyntaxException | GitAPIException ioe) { zip.delete(); LOG.error("Error creating archive from tag: " + e.getUUID().toString(), ioe); throw ioe; } finally { ArchiveCommand.unregisterFormat("zip"); } }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { File file = File.createTempFile("test", ".mzip"); // make the archive format known ArchiveCommand.registerFormat("myzip", new ZipArchiveFormat()); try { // this is the file that we write the archive to try (OutputStream out = new FileOutputStream(file)) { // finally call the ArchiveCommand to write out using the given format try (Git git = new Git(repository)) { git.archive() .setTree(repository.resolve("master")) .setFormat("myzip") .setOutputStream(out) .call(); } } } finally { ArchiveCommand.unregisterFormat("myzip"); } System.out.println("Wrote " + file.length() + " bytes to " + file); } }
@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.getProject()); 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(); } } }