@Ignore("Some versions of java.util.zip refuse to write an empty ZIP")
@Test
public void testEmptyArchive() throws Exception {
- final byte[] result = CLIGitCommand.rawExecute( //
+ byte[] result = CLIGitCommand.rawExecute(
"git archive --format=zip " + emptyTree, db);
assertArrayEquals(new String[0], listZipEntries(result));
}
@Test
public void testEmptyTar() throws Exception {
- final byte[] result = CLIGitCommand.rawExecute( //
+ byte[] result = CLIGitCommand.rawExecute(
"git archive --format=tar " + emptyTree, db);
assertArrayEquals(new String[0], listTarEntries(result));
}
@Test
public void testUnrecognizedFormat() throws Exception {
- final String[] expect = new String[] { "fatal: Unknown archive format 'nonsense'" };
- final String[] actual = execute("git archive --format=nonsense " + emptyTree);
+ String[] expect = new String[] { "fatal: Unknown archive format 'nonsense'" };
+ String[] actual = execute("git archive --format=nonsense " + emptyTree);
assertArrayEquals(expect, actual);
}
git.add().addFilepattern("c").call();
git.commit().setMessage("populate toplevel").call();
- final byte[] result = CLIGitCommand.rawExecute( //
+ byte[] result = CLIGitCommand.rawExecute(
"git archive --format=zip HEAD", db);
- assertArrayEquals(new String[] { "a", "c" }, //
+ assertArrayEquals(new String[] { "a", "c" },
listZipEntries(result));
}
@Test
public void testDefaultFormatIsTar() throws Exception {
commitGreeting();
- final byte[] result = CLIGitCommand.rawExecute( //
+ byte[] result = CLIGitCommand.rawExecute(
"git archive HEAD", db);
- assertArrayEquals(new String[] { "greeting" }, //
+ assertArrayEquals(new String[] { "greeting" },
listTarEntries(result));
}
@Test
public void testFormatOverridesFilename() throws Exception {
- final File archive = new File(db.getWorkTree(), "format-overrides-name.tar");
- final String path = archive.getAbsolutePath();
+ File archive = new File(db.getWorkTree(), "format-overrides-name.tar");
+ String path = archive.getAbsolutePath();
commitGreeting();
assertArrayEquals(new String[] { "" },
@Test
public void testUnrecognizedExtensionMeansTar() throws Exception {
- final File archive = new File(db.getWorkTree(), "example.txt");
- final String path = archive.getAbsolutePath();
+ File archive = new File(db.getWorkTree(), "example.txt");
+ String path = archive.getAbsolutePath();
commitGreeting();
assertArrayEquals(new String[] { "" },
@Test
public void testNoExtensionMeansTar() throws Exception {
- final File archive = new File(db.getWorkTree(), "example");
- final String path = archive.getAbsolutePath();
+ File archive = new File(db.getWorkTree(), "example");
+ String path = archive.getAbsolutePath();
commitGreeting();
assertArrayEquals(new String[] { "" },
@Test
public void testExtensionMatchIsAnchored() throws Exception {
- final File archive = new File(db.getWorkTree(), "two-extensions.zip.bak");
- final String path = archive.getAbsolutePath();
+ File archive = new File(db.getWorkTree(), "two-extensions.zip.bak");
+ String path = archive.getAbsolutePath();
commitGreeting();
assertArrayEquals(new String[] { "" },
@Test
public void testZipExtension() throws Exception {
- final File archiveWithDot = new File(db.getWorkTree(), "greeting.zip");
- final File archiveNoDot = new File(db.getWorkTree(), "greetingzip");
+ File archiveWithDot = new File(db.getWorkTree(), "greeting.zip");
+ File archiveNoDot = new File(db.getWorkTree(), "greetingzip");
commitGreeting();
execute("git archive " +
@Test
public void testTarExtension() throws Exception {
- final File archive = new File(db.getWorkTree(), "tarball.tar");
- final String path = archive.getAbsolutePath();
+ File archive = new File(db.getWorkTree(), "tarball.tar");
+ String path = archive.getAbsolutePath();
commitGreeting();
assertArrayEquals(new String[] { "" },
commitGreeting();
for (String ext : Arrays.asList("tar.gz", "tgz")) {
- final File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext);
- final File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext);
+ File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext);
+ File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext);
execute("git archive " +
shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " +
commitGreeting();
for (String ext : Arrays.asList("tar.bz2", "tbz", "tbz2")) {
- final File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext);
- final File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext);
+ File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext);
+ File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext);
execute("git archive " +
shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " +
commitGreeting();
for (String ext : Arrays.asList("tar.xz", "txz")) {
- final File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext);
- final File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext);
+ File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext);
+ File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext);
execute("git archive " +
shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " +
git.add().addFilepattern("b").call();
git.commit().setMessage("add subdir").call();
- final byte[] result = CLIGitCommand.rawExecute( //
+ byte[] result = CLIGitCommand.rawExecute(
"git archive --format=zip master", db);
String[] expect = { "a", "b.c", "b0c", "b/", "b/a", "b/b", "c" };
String[] actual = listZipEntries(result);
git.add().addFilepattern("b").call();
git.commit().setMessage("add subdir").call();
- final byte[] result = CLIGitCommand.rawExecute( //
+ byte[] result = CLIGitCommand.rawExecute(
"git archive --format=tar master", db);
String[] expect = { "a", "b.c", "b0c", "b/", "b/a", "b/b", "c" };
String[] actual = listTarEntries(result);
@Test
public void testPrefixDoesNotNormalizeDoubleSlashInTar() throws Exception {
commitFoo();
- final byte[] result = CLIGitCommand.rawExecute( //
+ byte[] result = CLIGitCommand.rawExecute(
"git archive --prefix=x// --format=tar master", db);
String[] expect = { "x//foo" };
assertArrayEquals(expect, listTarEntries(result));
@Test
public void testTarPrefixWithoutTrailingSlash() throws Exception {
commitBazAndFooSlashBar();
- final byte[] result = CLIGitCommand.rawExecute( //
+ byte[] result = CLIGitCommand.rawExecute(
"git archive --prefix=my- --format=tar master", db);
String[] expect = { "my-baz", "my-foo/", "my-foo/bar" };
String[] actual = listTarEntries(result);
git.submoduleAdd().setURI("./.").setPath("b").call().close();
git.commit().setMessage("add submodule").call();
- final byte[] result = CLIGitCommand.rawExecute( //
+ byte[] result = CLIGitCommand.rawExecute(
"git archive --format=zip master", db);
String[] expect = { ".gitmodules", "a", "b/", "c" };
String[] actual = listZipEntries(result);
git.submoduleAdd().setURI("./.").setPath("b").call().close();
git.commit().setMessage("add submodule").call();
- final byte[] result = CLIGitCommand.rawExecute( //
+ byte[] result = CLIGitCommand.rawExecute(
"git archive --format=tar master", db);
String[] expect = { ".gitmodules", "a", "b/", "c" };
String[] actual = listTarEntries(result);
git.commit().setMessage("three files with different modes").call();
- final byte[] zipData = CLIGitCommand.rawExecute( //
+ byte[] zipData = CLIGitCommand.rawExecute(
"git archive --format=zip master", db);
writeRaw("zip-with-modes.zip", zipData);
assertContainsEntryWithMode("zip-with-modes.zip", "-rw-", "plain");
git.commit().setMessage("three files with different modes").call();
- final byte[] archive = CLIGitCommand.rawExecute( //
+ byte[] archive = CLIGitCommand.rawExecute(
"git archive --format=tar master", db);
writeRaw("with-modes.tar", archive);
assertTarContainsEntry("with-modes.tar", "-rw-r--r--", "plain");
@Test
public void testArchiveWithLongFilename() throws Exception {
String filename = "";
- final List<String> l = new ArrayList<String>();
+ List<String> l = new ArrayList<String>();
for (int i = 0; i < 20; i++) {
filename = filename + "1234567890/";
l.add(filename);
git.add().addFilepattern("1234567890").call();
git.commit().setMessage("file with long name").call();
- final byte[] result = CLIGitCommand.rawExecute( //
+ byte[] result = CLIGitCommand.rawExecute(
"git archive --format=zip HEAD", db);
assertArrayEquals(l.toArray(new String[l.size()]),
listZipEntries(result));
@Test
public void testTarWithLongFilename() throws Exception {
String filename = "";
- final List<String> l = new ArrayList<String>();
+ List<String> l = new ArrayList<String>();
for (int i = 0; i < 20; i++) {
filename = filename + "1234567890/";
l.add(filename);
git.add().addFilepattern("1234567890").call();
git.commit().setMessage("file with long name").call();
- final byte[] result = CLIGitCommand.rawExecute( //
+ byte[] result = CLIGitCommand.rawExecute(
"git archive --format=tar HEAD", db);
assertArrayEquals(l.toArray(new String[l.size()]),
listTarEntries(result));
@Test
public void testArchivePreservesContent() throws Exception {
- final String payload = "“The quick brown fox jumps over the lazy dog!”";
+ String payload = "“The quick brown fox jumps over the lazy dog!”";
writeTrashFile("xyzzy", payload);
git.add().addFilepattern("xyzzy").call();
git.commit().setMessage("add file with content").call();
- final byte[] result = CLIGitCommand.rawExecute( //
+ byte[] result = CLIGitCommand.rawExecute(
"git archive --format=zip HEAD", db);
- assertArrayEquals(new String[] { payload }, //
+ assertArrayEquals(new String[] { payload },
zipEntryContent(result, "xyzzy"));
}
@Test
public void testTarPreservesContent() throws Exception {
- final String payload = "“The quick brown fox jumps over the lazy dog!”";
+ String payload = "“The quick brown fox jumps over the lazy dog!”";
writeTrashFile("xyzzy", payload);
git.add().addFilepattern("xyzzy").call();
git.commit().setMessage("add file with content").call();
- final byte[] result = CLIGitCommand.rawExecute( //
+ byte[] result = CLIGitCommand.rawExecute(
"git archive --format=tar HEAD", db);
- assertArrayEquals(new String[] { payload }, //
+ assertArrayEquals(new String[] { payload },
tarEntryContent(result, "xyzzy"));
}
private Process spawnAssumingCommandPresent(String... cmdline) {
- final File cwd = db.getWorkTree();
- final ProcessBuilder procBuilder = new ProcessBuilder(cmdline) //
- .directory(cwd) //
+ File cwd = db.getWorkTree();
+ ProcessBuilder procBuilder = new ProcessBuilder(cmdline)
+ .directory(cwd)
.redirectErrorStream(true);
Process proc = null;
try {
}
private BufferedReader readFromProcess(Process proc) throws Exception {
- return new BufferedReader( //
+ return new BufferedReader(
new InputStreamReader(proc.getInputStream(), "UTF-8"));
}
- private void grepForEntry(String name, String mode, String... cmdline) //
+ private void grepForEntry(String name, String mode, String... cmdline)
throws Exception {
- final Process proc = spawnAssumingCommandPresent(cmdline);
+ Process proc = spawnAssumingCommandPresent(cmdline);
proc.getOutputStream().close();
- final BufferedReader reader = readFromProcess(proc);
+ BufferedReader reader = readFromProcess(proc);
try {
String line;
while ((line = reader.readLine()) != null)
assertMagic(new byte[] { (byte) 0xfd, '7', 'z', 'X', 'Z', 0 }, file);
}
- private void assertContainsEntryWithMode(String zipFilename, String mode, String name) //
+ private void assertContainsEntryWithMode(String zipFilename, String mode, String name)
throws Exception {
grepForEntry(name, mode, "zipinfo", zipFilename);
}
- private void assertTarContainsEntry(String tarfile, String mode, String name) //
+ private void assertTarContainsEntry(String tarfile, String mode, String name)
throws Exception {
grepForEntry(name, mode, "tar", "tvf", tarfile);
}
- private void writeRaw(String filename, byte[] data) //
+ private void writeRaw(String filename, byte[] data)
throws IOException {
- final File path = new File(db.getWorkTree(), filename);
- final OutputStream out = new FileOutputStream(path);
+ File path = new File(db.getWorkTree(), filename);
+ OutputStream out = new FileOutputStream(path);
try {
out.write(data);
} finally {
}
private static String[] listZipEntries(byte[] zipData) throws IOException {
- final List<String> l = new ArrayList<String>();
- final ZipInputStream in = new ZipInputStream( //
+ List<String> l = new ArrayList<String>();
+ ZipInputStream in = new ZipInputStream(
new ByteArrayInputStream(zipData));
ZipEntry e;
}
private static Future<Object> writeAsync(final OutputStream stream, final byte[] data) {
- final ExecutorService executor = Executors.newSingleThreadExecutor();
+ ExecutorService executor = Executors.newSingleThreadExecutor();
- return executor.submit(new Callable<Object>() { //
+ return executor.submit(new Callable<Object>() {
public Object call() throws IOException {
try {
stream.write(data);
}
private String[] listTarEntries(byte[] tarData) throws Exception {
- final List<String> l = new ArrayList<String>();
- final Process proc = spawnAssumingCommandPresent("tar", "tf", "-");
- final BufferedReader reader = readFromProcess(proc);
- final OutputStream out = proc.getOutputStream();
+ List<String> l = new ArrayList<String>();
+ Process proc = spawnAssumingCommandPresent("tar", "tf", "-");
+ BufferedReader reader = readFromProcess(proc);
+ OutputStream out = proc.getOutputStream();
// Dump tarball to tar stdin in background
- final Future<?> writing = writeAsync(out, tarData);
+ Future<?> writing = writeAsync(out, tarData);
try {
String line;
}
}
- private static String[] zipEntryContent(byte[] zipData, String path) //
+ private static String[] zipEntryContent(byte[] zipData, String path)
throws IOException {
- final ZipInputStream in = new ZipInputStream( //
+ ZipInputStream in = new ZipInputStream(
new ByteArrayInputStream(zipData));
ZipEntry e;
while ((e = in.getNextEntry()) != null) {
continue;
// found!
- final List<String> l = new ArrayList<String>();
- final BufferedReader reader = new BufferedReader( //
+ List<String> l = new ArrayList<String>();
+ BufferedReader reader = new BufferedReader(
new InputStreamReader(in, "UTF-8"));
String line;
while ((line = reader.readLine()) != null)
return null;
}
- private String[] tarEntryContent(byte[] tarData, String path) //
+ private String[] tarEntryContent(byte[] tarData, String path)
throws Exception {
- final List<String> l = new ArrayList<String>();
- final Process proc = spawnAssumingCommandPresent("tar", "Oxf", "-", path);
- final BufferedReader reader = readFromProcess(proc);
- final OutputStream out = proc.getOutputStream();
- final Future<?> writing = writeAsync(out, tarData);
+ List<String> l = new ArrayList<String>();
+ Process proc = spawnAssumingCommandPresent("tar", "Oxf", "-", path);
+ BufferedReader reader = readFromProcess(proc);
+ OutputStream out = proc.getOutputStream();
+ Future<?> writing = writeAsync(out, tarData);
try {
String line;