Преглед изворни кода

Drop dependency by ArchiveCommand.Format interface on commons-compress

Otherwise, anyone trying to implement a new format would have to
depend on commons-compress, even if using a different underlying
library to write the archive.

Change-Id: I301a1997e3b48aa7e32d693fd8f4b2d436c9b3a7
tags/v3.1.0.201309270735-rc1
Jonathan Nieder пре 11 година
родитељ
комит
cfc15dd9dc

+ 25
- 16
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/archive/ArchiveCommand.java Прегледај датотеку

*/ */
package org.eclipse.jgit.pgm.archive; package org.eclipse.jgit.pgm.archive;


import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;


import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.GitCommand; import org.eclipse.jgit.api.GitCommand;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
* *
* Usage: * Usage:
* Repository repo = git.getRepository(); * Repository repo = git.getRepository();
* ArchiveOutputStream out = format.createArchiveOutputStream(System.out);
* T out = format.createArchiveOutputStream(System.out);
* try { * try {
* for (...) { * for (...) {
* format.putEntry(out, path, mode, repo.open(objectId)); * format.putEntry(out, path, mode, repo.open(objectId));
* out.close(); * out.close();
* } * }
*/ */
public static interface Format {
ArchiveOutputStream createArchiveOutputStream(OutputStream s);
void putEntry(ArchiveOutputStream out, String path, FileMode mode,
public static interface Format<T extends Closeable> {
T createArchiveOutputStream(OutputStream s);
void putEntry(T out, String path, FileMode mode,
ObjectLoader loader) throws IOException; ObjectLoader loader) throws IOException;
} }


} }
} }


private static final ConcurrentMap<String, Format> formats =
new ConcurrentHashMap<String, Format>();
/**
* Available archival formats (corresponding to values for
* the --format= option)
*/
private static final ConcurrentMap<String, Format<?>> formats =
new ConcurrentHashMap<String, Format<?>>();


static { static {
formats.put("zip", new ZipFormat()); formats.put("zip", new ZipFormat());
formats.put("tar", new TarFormat()); formats.put("tar", new TarFormat());
} }


private static Format lookupFormat(String formatName) throws UnsupportedFormatException {
Format fmt = formats.get(formatName);
private static Format<?> lookupFormat(String formatName) throws UnsupportedFormatException {
Format<?> fmt = formats.get(formatName);
if (fmt == null) if (fmt == null)
throw new UnsupportedFormatException(formatName); throw new UnsupportedFormatException(formatName);
return fmt; return fmt;
walk.release(); walk.release();
} }


/**
* @return the stream to which the archive has been written
*/
@Override
public OutputStream call() throws GitAPIException {
private <T extends Closeable>
OutputStream writeArchive(Format<T> fmt) throws GitAPIException {
final MutableObjectId idBuf = new MutableObjectId(); final MutableObjectId idBuf = new MutableObjectId();
final Format fmt = lookupFormat(format);
final ArchiveOutputStream outa = fmt.createArchiveOutputStream(out);
final T outa = fmt.createArchiveOutputStream(out);
final ObjectReader reader = walk.getObjectReader(); final ObjectReader reader = walk.getObjectReader();


try { try {
return out; return out;
} }


/**
* @return the stream to which the archive has been written
*/
@Override
public OutputStream call() throws GitAPIException {
final Format<?> fmt = lookupFormat(format);
return writeArchive(fmt);
}

/** /**
* @param tree * @param tree
* the tag, commit, or tree object to produce an archive for * the tag, commit, or tree object to produce an archive for

+ 1
- 1
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/archive/TarFormat.java Прегледај датотеку

import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.ObjectLoader; import org.eclipse.jgit.lib.ObjectLoader;


class TarFormat implements ArchiveCommand.Format {
class TarFormat implements ArchiveCommand.Format<ArchiveOutputStream> {
public ArchiveOutputStream createArchiveOutputStream(OutputStream s) { public ArchiveOutputStream createArchiveOutputStream(OutputStream s) {
return new TarArchiveOutputStream(s); return new TarArchiveOutputStream(s);
} }

+ 1
- 1
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/archive/ZipFormat.java Прегледај датотеку

import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.ObjectLoader; import org.eclipse.jgit.lib.ObjectLoader;


class ZipFormat implements ArchiveCommand.Format {
class ZipFormat implements ArchiveCommand.Format<ArchiveOutputStream> {
public ArchiveOutputStream createArchiveOutputStream(OutputStream s) { public ArchiveOutputStream createArchiveOutputStream(OutputStream s) {
return new ZipArchiveOutputStream(s); return new ZipArchiveOutputStream(s);
} }

Loading…
Откажи
Сачувај