|
|
|
|
|
|
|
|
* |
|
|
* |
|
|
* <pre> |
|
|
* <pre> |
|
|
* ArchiveCommand.registerFormat("tar", new TarFormat()); |
|
|
* ArchiveCommand.registerFormat("tar", new TarFormat()); |
|
|
* cmd = git.archive(); |
|
|
|
|
|
* try { |
|
|
* try { |
|
|
* cmd.setTree(db.resolve("HEAD")) |
|
|
|
|
|
* .setOutputStream(out).call(); |
|
|
|
|
|
|
|
|
* git.archive() |
|
|
|
|
|
* .setTree(db.resolve("HEAD")) |
|
|
|
|
|
* .setOutputStream(out) |
|
|
|
|
|
* .call(); |
|
|
* } finally { |
|
|
* } finally { |
|
|
* cmd.release(); |
|
|
|
|
|
* ArchiveCommand.unregisterFormat("tar"); |
|
|
* ArchiveCommand.unregisterFormat("tar"); |
|
|
* } |
|
|
* } |
|
|
* </pre> |
|
|
* </pre> |
|
|
|
|
|
|
|
|
* <pre> |
|
|
* <pre> |
|
|
* ArchiveCommand.registerFormat("zip", new ZipFormat()); |
|
|
* ArchiveCommand.registerFormat("zip", new ZipFormat()); |
|
|
* try { |
|
|
* try { |
|
|
* cmd.setTree(db.resolve("master")) |
|
|
|
|
|
|
|
|
* git.archive(). |
|
|
|
|
|
* .setTree(db.resolve("master")) |
|
|
* .setFormat("zip") |
|
|
* .setFormat("zip") |
|
|
* .setOutputStream(out).call(); |
|
|
|
|
|
|
|
|
* .setOutputStream(out) |
|
|
|
|
|
* .call(); |
|
|
* } finally { |
|
|
* } finally { |
|
|
* cmd.release(); |
|
|
|
|
|
* ArchiveCommand.unregisterFormat("zip"); |
|
|
* ArchiveCommand.unregisterFormat("zip"); |
|
|
* } |
|
|
* } |
|
|
* </pre> |
|
|
* </pre> |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private OutputStream out; |
|
|
private OutputStream out; |
|
|
private TreeWalk walk; |
|
|
|
|
|
|
|
|
private ObjectId tree; |
|
|
private String format = "tar"; |
|
|
private String format = "tar"; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
|
|
|
|
|
|
*/ |
|
|
*/ |
|
|
public ArchiveCommand(Repository repo) { |
|
|
public ArchiveCommand(Repository repo) { |
|
|
super(repo); |
|
|
super(repo); |
|
|
walk = new TreeWalk(repo); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Release any resources used by the internal ObjectReader. |
|
|
|
|
|
* <p> |
|
|
|
|
|
* This does not close the output stream set with setOutputStream, which |
|
|
|
|
|
* belongs to the caller. |
|
|
|
|
|
*/ |
|
|
|
|
|
public void release() { |
|
|
|
|
|
walk.release(); |
|
|
|
|
|
|
|
|
setCallable(false); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private <T extends Closeable> |
|
|
private <T extends Closeable> |
|
|
OutputStream writeArchive(Format<T> fmt) throws GitAPIException { |
|
|
OutputStream writeArchive(Format<T> fmt) throws GitAPIException { |
|
|
final MutableObjectId idBuf = new MutableObjectId(); |
|
|
|
|
|
final T outa = fmt.createArchiveOutputStream(out); |
|
|
|
|
|
final ObjectReader reader = walk.getObjectReader(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final TreeWalk walk = new TreeWalk(repo); |
|
|
try { |
|
|
try { |
|
|
|
|
|
final T outa = fmt.createArchiveOutputStream(out); |
|
|
try { |
|
|
try { |
|
|
|
|
|
final MutableObjectId idBuf = new MutableObjectId(); |
|
|
|
|
|
final ObjectReader reader = walk.getObjectReader(); |
|
|
|
|
|
final RevWalk rw = new RevWalk(walk.getObjectReader()); |
|
|
|
|
|
|
|
|
|
|
|
walk.reset(rw.parseTree(tree)); |
|
|
walk.setRecursive(true); |
|
|
walk.setRecursive(true); |
|
|
while (walk.next()) { |
|
|
while (walk.next()) { |
|
|
final String name = walk.getPathString(); |
|
|
final String name = walk.getPathString(); |
|
|
|
|
|
|
|
|
} finally { |
|
|
} finally { |
|
|
outa.close(); |
|
|
outa.close(); |
|
|
} |
|
|
} |
|
|
|
|
|
return out; |
|
|
} catch (IOException e) { |
|
|
} catch (IOException e) { |
|
|
// TODO(jrn): Throw finer-grained errors. |
|
|
// TODO(jrn): Throw finer-grained errors. |
|
|
throw new JGitInternalException( |
|
|
throw new JGitInternalException( |
|
|
JGitText.get().exceptionCaughtDuringExecutionOfArchiveCommand, e); |
|
|
JGitText.get().exceptionCaughtDuringExecutionOfArchiveCommand, e); |
|
|
|
|
|
} finally { |
|
|
|
|
|
walk.release(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return out; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
|
|
|
|
|
|
*/ |
|
|
*/ |
|
|
@Override |
|
|
@Override |
|
|
public OutputStream call() throws GitAPIException { |
|
|
public OutputStream call() throws GitAPIException { |
|
|
|
|
|
checkCallable(); |
|
|
|
|
|
|
|
|
final Format<?> fmt = lookupFormat(format); |
|
|
final Format<?> fmt = lookupFormat(format); |
|
|
return writeArchive(fmt); |
|
|
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 |
|
|
* @return this |
|
|
* @return this |
|
|
* @throws IOException |
|
|
|
|
|
*/ |
|
|
*/ |
|
|
public ArchiveCommand setTree(ObjectId tree) throws IOException { |
|
|
|
|
|
final RevWalk rw = new RevWalk(walk.getObjectReader()); |
|
|
|
|
|
walk.reset(rw.parseTree(tree)); |
|
|
|
|
|
|
|
|
public ArchiveCommand setTree(ObjectId tree) { |
|
|
|
|
|
if (tree == null) |
|
|
|
|
|
throw new IllegalArgumentException(); |
|
|
|
|
|
|
|
|
|
|
|
this.tree = tree; |
|
|
|
|
|
setCallable(true); |
|
|
return this; |
|
|
return this; |
|
|
} |
|
|
} |
|
|
|
|
|
|