|
|
@ -45,8 +45,14 @@ package org.eclipse.jgit.pgm; |
|
|
|
|
|
|
|
|
|
|
|
import java.lang.String; |
|
|
|
import java.lang.String; |
|
|
|
import java.lang.System; |
|
|
|
import java.lang.System; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
import java.io.OutputStream; |
|
|
|
|
|
|
|
import java.util.EnumMap; |
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
import java.text.MessageFormat; |
|
|
|
import java.text.MessageFormat; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.compress.archivers.ArchiveEntry; |
|
|
|
|
|
|
|
import org.apache.commons.compress.archivers.ArchiveOutputStream; |
|
|
|
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; |
|
|
|
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; |
|
|
|
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; |
|
|
|
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; |
|
|
|
import org.eclipse.jgit.lib.FileMode; |
|
|
|
import org.eclipse.jgit.lib.FileMode; |
|
|
@ -58,18 +64,23 @@ import org.eclipse.jgit.pgm.TextBuiltin; |
|
|
|
import org.eclipse.jgit.treewalk.AbstractTreeIterator; |
|
|
|
import org.eclipse.jgit.treewalk.AbstractTreeIterator; |
|
|
|
import org.eclipse.jgit.treewalk.TreeWalk; |
|
|
|
import org.eclipse.jgit.treewalk.TreeWalk; |
|
|
|
import org.kohsuke.args4j.Argument; |
|
|
|
import org.kohsuke.args4j.Argument; |
|
|
|
|
|
|
|
import org.kohsuke.args4j.Option; |
|
|
|
|
|
|
|
|
|
|
|
@Command(common = true, usage = "usage_archive") |
|
|
|
@Command(common = true, usage = "usage_archive") |
|
|
|
class Archive extends TextBuiltin { |
|
|
|
class Archive extends TextBuiltin { |
|
|
|
@Argument(index = 0, metaVar = "metaVar_treeish") |
|
|
|
@Argument(index = 0, metaVar = "metaVar_treeish") |
|
|
|
private AbstractTreeIterator tree; |
|
|
|
private AbstractTreeIterator tree; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Option(name = "--format", metaVar = "metaVar_archiveFormat", usage = "usage_archiveFormat") |
|
|
|
|
|
|
|
private Format format = Format.ZIP; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void run() throws Exception { |
|
|
|
protected void run() throws Exception { |
|
|
|
final TreeWalk walk = new TreeWalk(db); |
|
|
|
final TreeWalk walk = new TreeWalk(db); |
|
|
|
final ObjectReader reader = walk.getObjectReader(); |
|
|
|
final ObjectReader reader = walk.getObjectReader(); |
|
|
|
final MutableObjectId idBuf = new MutableObjectId(); |
|
|
|
final MutableObjectId idBuf = new MutableObjectId(); |
|
|
|
final ZipArchiveOutputStream out = new ZipArchiveOutputStream(outs); |
|
|
|
final Archiver fmt = formats.get(format); |
|
|
|
|
|
|
|
final ArchiveOutputStream out = fmt.createArchiveOutputStream(outs); |
|
|
|
|
|
|
|
|
|
|
|
if (tree == null) |
|
|
|
if (tree == null) |
|
|
|
throw die(CLIText.get().treeIsRequired); |
|
|
|
throw die(CLIText.get().treeIsRequired); |
|
|
@ -87,9 +98,43 @@ class Archive extends TextBuiltin { |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
|
|
walk.getObjectId(idBuf, 0); |
|
|
|
walk.getObjectId(idBuf, 0); |
|
|
|
final ZipArchiveEntry entry = new ZipArchiveEntry(name); |
|
|
|
fmt.putEntry(name, mode, reader.open(idBuf), out); |
|
|
|
final ObjectLoader loader = reader.open(idBuf); |
|
|
|
} |
|
|
|
entry.setSize(loader.getSize()); |
|
|
|
|
|
|
|
|
|
|
|
out.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static private void warnArchiveEntryModeIgnored(String name) { |
|
|
|
|
|
|
|
System.err.println(MessageFormat.format( //
|
|
|
|
|
|
|
|
CLIText.get().archiveEntryModeIgnored, //
|
|
|
|
|
|
|
|
name)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public enum Format { |
|
|
|
|
|
|
|
ZIP |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static interface Archiver { |
|
|
|
|
|
|
|
ArchiveOutputStream createArchiveOutputStream(OutputStream s); |
|
|
|
|
|
|
|
void putEntry(String path, FileMode mode, //
|
|
|
|
|
|
|
|
ObjectLoader loader, ArchiveOutputStream out) //
|
|
|
|
|
|
|
|
throws IOException; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final Map<Format, Archiver> formats; |
|
|
|
|
|
|
|
static { |
|
|
|
|
|
|
|
Map<Format, Archiver> fmts = new EnumMap<Format, Archiver>(Format.class); |
|
|
|
|
|
|
|
fmts.put(Format.ZIP, new Archiver() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public ArchiveOutputStream createArchiveOutputStream(OutputStream s) { |
|
|
|
|
|
|
|
return new ZipArchiveOutputStream(s); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void putEntry(String path, FileMode mode, //
|
|
|
|
|
|
|
|
ObjectLoader loader, ArchiveOutputStream out) //
|
|
|
|
|
|
|
|
throws IOException { |
|
|
|
|
|
|
|
final ZipArchiveEntry entry = new ZipArchiveEntry(path); |
|
|
|
|
|
|
|
|
|
|
|
if (mode == FileMode.REGULAR_FILE) |
|
|
|
if (mode == FileMode.REGULAR_FILE) |
|
|
|
; // ok
|
|
|
|
; // ok
|
|
|
@ -97,15 +142,13 @@ class Archive extends TextBuiltin { |
|
|
|
mode == FileMode.SYMLINK) |
|
|
|
mode == FileMode.SYMLINK) |
|
|
|
entry.setUnixMode(mode.getBits()); |
|
|
|
entry.setUnixMode(mode.getBits()); |
|
|
|
else |
|
|
|
else |
|
|
|
System.err.println(MessageFormat.format( //
|
|
|
|
warnArchiveEntryModeIgnored(path); |
|
|
|
CLIText.get().archiveEntryModeIgnored, //
|
|
|
|
entry.setSize(loader.getSize()); |
|
|
|
name)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out.putArchiveEntry(entry); |
|
|
|
out.putArchiveEntry(entry); |
|
|
|
loader.copyTo(out); |
|
|
|
loader.copyTo(out); |
|
|
|
out.closeArchiveEntry(); |
|
|
|
out.closeArchiveEntry(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
out.close(); |
|
|
|
formats = fmts; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|