Parcourir la source

archive: Drop unnecessary empty comments and 'final' qualifiers on locals

Early JGit code used comments to inform the Eclipse formatter about
where to break lines and used final in the hope of making code faster.
The ArchiveCommand command implementation imitated that style.

Nowadays the project relies less on the Eclipse formatter and relies
more on Java having sane performance with local variables that are not
explicitly marked 'final'.  Removing the unnecessary empty comments and
'final' qualifiers makes this code more readable and more consistent
with recent JGit code.

Change-Id: I7a181432eda7e18bd32cf110d89c0efbe490c4f1
Signed-off-by: Jonathan Nieder <jrn@google.com>
tags/v4.0.0.201506020755-rc3
Jonathan Nieder il y a 9 ans
Parent
révision
a5778b6f41

+ 75
- 75
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java Voir le fichier

@Ignore("Some versions of java.util.zip refuse to write an empty ZIP") @Ignore("Some versions of java.util.zip refuse to write an empty ZIP")
@Test @Test
public void testEmptyArchive() throws Exception { public void testEmptyArchive() throws Exception {
final byte[] result = CLIGitCommand.rawExecute( //
byte[] result = CLIGitCommand.rawExecute(
"git archive --format=zip " + emptyTree, db); "git archive --format=zip " + emptyTree, db);
assertArrayEquals(new String[0], listZipEntries(result)); assertArrayEquals(new String[0], listZipEntries(result));
} }


@Test @Test
public void testEmptyTar() throws Exception { public void testEmptyTar() throws Exception {
final byte[] result = CLIGitCommand.rawExecute( //
byte[] result = CLIGitCommand.rawExecute(
"git archive --format=tar " + emptyTree, db); "git archive --format=tar " + emptyTree, db);
assertArrayEquals(new String[0], listTarEntries(result)); assertArrayEquals(new String[0], listTarEntries(result));
} }


@Test @Test
public void testUnrecognizedFormat() throws Exception { 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); assertArrayEquals(expect, actual);
} }


git.add().addFilepattern("c").call(); git.add().addFilepattern("c").call();
git.commit().setMessage("populate toplevel").call(); git.commit().setMessage("populate toplevel").call();


final byte[] result = CLIGitCommand.rawExecute( //
byte[] result = CLIGitCommand.rawExecute(
"git archive --format=zip HEAD", db); "git archive --format=zip HEAD", db);
assertArrayEquals(new String[] { "a", "c" }, //
assertArrayEquals(new String[] { "a", "c" },
listZipEntries(result)); listZipEntries(result));
} }


@Test @Test
public void testDefaultFormatIsTar() throws Exception { public void testDefaultFormatIsTar() throws Exception {
commitGreeting(); commitGreeting();
final byte[] result = CLIGitCommand.rawExecute( //
byte[] result = CLIGitCommand.rawExecute(
"git archive HEAD", db); "git archive HEAD", db);
assertArrayEquals(new String[] { "greeting" }, //
assertArrayEquals(new String[] { "greeting" },
listTarEntries(result)); listTarEntries(result));
} }




@Test @Test
public void testFormatOverridesFilename() throws Exception { 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(); commitGreeting();
assertArrayEquals(new String[] { "" }, assertArrayEquals(new String[] { "" },


@Test @Test
public void testUnrecognizedExtensionMeansTar() throws Exception { 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(); commitGreeting();
assertArrayEquals(new String[] { "" }, assertArrayEquals(new String[] { "" },


@Test @Test
public void testNoExtensionMeansTar() throws Exception { 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(); commitGreeting();
assertArrayEquals(new String[] { "" }, assertArrayEquals(new String[] { "" },


@Test @Test
public void testExtensionMatchIsAnchored() throws Exception { 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(); commitGreeting();
assertArrayEquals(new String[] { "" }, assertArrayEquals(new String[] { "" },


@Test @Test
public void testZipExtension() throws Exception { 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(); commitGreeting();
execute("git archive " + execute("git archive " +


@Test @Test
public void testTarExtension() throws Exception { 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(); commitGreeting();
assertArrayEquals(new String[] { "" }, assertArrayEquals(new String[] { "" },
commitGreeting(); commitGreeting();


for (String ext : Arrays.asList("tar.gz", "tgz")) { 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 " + execute("git archive " +
shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " + shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " +
commitGreeting(); commitGreeting();


for (String ext : Arrays.asList("tar.bz2", "tbz", "tbz2")) { 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 " + execute("git archive " +
shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " + shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " +
commitGreeting(); commitGreeting();


for (String ext : Arrays.asList("tar.xz", "txz")) { 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 " + execute("git archive " +
shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " + shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " +
git.add().addFilepattern("b").call(); git.add().addFilepattern("b").call();
git.commit().setMessage("add subdir").call(); git.commit().setMessage("add subdir").call();


final byte[] result = CLIGitCommand.rawExecute( //
byte[] result = CLIGitCommand.rawExecute(
"git archive --format=zip master", db); "git archive --format=zip master", db);
String[] expect = { "a", "b.c", "b0c", "b/", "b/a", "b/b", "c" }; String[] expect = { "a", "b.c", "b0c", "b/", "b/a", "b/b", "c" };
String[] actual = listZipEntries(result); String[] actual = listZipEntries(result);
git.add().addFilepattern("b").call(); git.add().addFilepattern("b").call();
git.commit().setMessage("add subdir").call(); git.commit().setMessage("add subdir").call();


final byte[] result = CLIGitCommand.rawExecute( //
byte[] result = CLIGitCommand.rawExecute(
"git archive --format=tar master", db); "git archive --format=tar master", db);
String[] expect = { "a", "b.c", "b0c", "b/", "b/a", "b/b", "c" }; String[] expect = { "a", "b.c", "b0c", "b/", "b/a", "b/b", "c" };
String[] actual = listTarEntries(result); String[] actual = listTarEntries(result);
@Test @Test
public void testPrefixDoesNotNormalizeDoubleSlashInTar() throws Exception { public void testPrefixDoesNotNormalizeDoubleSlashInTar() throws Exception {
commitFoo(); commitFoo();
final byte[] result = CLIGitCommand.rawExecute( //
byte[] result = CLIGitCommand.rawExecute(
"git archive --prefix=x// --format=tar master", db); "git archive --prefix=x// --format=tar master", db);
String[] expect = { "x//foo" }; String[] expect = { "x//foo" };
assertArrayEquals(expect, listTarEntries(result)); assertArrayEquals(expect, listTarEntries(result));
@Test @Test
public void testTarPrefixWithoutTrailingSlash() throws Exception { public void testTarPrefixWithoutTrailingSlash() throws Exception {
commitBazAndFooSlashBar(); commitBazAndFooSlashBar();
final byte[] result = CLIGitCommand.rawExecute( //
byte[] result = CLIGitCommand.rawExecute(
"git archive --prefix=my- --format=tar master", db); "git archive --prefix=my- --format=tar master", db);
String[] expect = { "my-baz", "my-foo/", "my-foo/bar" }; String[] expect = { "my-baz", "my-foo/", "my-foo/bar" };
String[] actual = listTarEntries(result); String[] actual = listTarEntries(result);
git.submoduleAdd().setURI("./.").setPath("b").call().close(); git.submoduleAdd().setURI("./.").setPath("b").call().close();
git.commit().setMessage("add submodule").call(); git.commit().setMessage("add submodule").call();


final byte[] result = CLIGitCommand.rawExecute( //
byte[] result = CLIGitCommand.rawExecute(
"git archive --format=zip master", db); "git archive --format=zip master", db);
String[] expect = { ".gitmodules", "a", "b/", "c" }; String[] expect = { ".gitmodules", "a", "b/", "c" };
String[] actual = listZipEntries(result); String[] actual = listZipEntries(result);
git.submoduleAdd().setURI("./.").setPath("b").call().close(); git.submoduleAdd().setURI("./.").setPath("b").call().close();
git.commit().setMessage("add submodule").call(); git.commit().setMessage("add submodule").call();


final byte[] result = CLIGitCommand.rawExecute( //
byte[] result = CLIGitCommand.rawExecute(
"git archive --format=tar master", db); "git archive --format=tar master", db);
String[] expect = { ".gitmodules", "a", "b/", "c" }; String[] expect = { ".gitmodules", "a", "b/", "c" };
String[] actual = listTarEntries(result); String[] actual = listTarEntries(result);


git.commit().setMessage("three files with different modes").call(); git.commit().setMessage("three files with different modes").call();


final byte[] zipData = CLIGitCommand.rawExecute( //
byte[] zipData = CLIGitCommand.rawExecute(
"git archive --format=zip master", db); "git archive --format=zip master", db);
writeRaw("zip-with-modes.zip", zipData); writeRaw("zip-with-modes.zip", zipData);
assertContainsEntryWithMode("zip-with-modes.zip", "-rw-", "plain"); assertContainsEntryWithMode("zip-with-modes.zip", "-rw-", "plain");


git.commit().setMessage("three files with different modes").call(); git.commit().setMessage("three files with different modes").call();


final byte[] archive = CLIGitCommand.rawExecute( //
byte[] archive = CLIGitCommand.rawExecute(
"git archive --format=tar master", db); "git archive --format=tar master", db);
writeRaw("with-modes.tar", archive); writeRaw("with-modes.tar", archive);
assertTarContainsEntry("with-modes.tar", "-rw-r--r--", "plain"); assertTarContainsEntry("with-modes.tar", "-rw-r--r--", "plain");
@Test @Test
public void testArchiveWithLongFilename() throws Exception { public void testArchiveWithLongFilename() throws Exception {
String filename = ""; String filename = "";
final List<String> l = new ArrayList<String>();
List<String> l = new ArrayList<String>();
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
filename = filename + "1234567890/"; filename = filename + "1234567890/";
l.add(filename); l.add(filename);
git.add().addFilepattern("1234567890").call(); git.add().addFilepattern("1234567890").call();
git.commit().setMessage("file with long name").call(); git.commit().setMessage("file with long name").call();


final byte[] result = CLIGitCommand.rawExecute( //
byte[] result = CLIGitCommand.rawExecute(
"git archive --format=zip HEAD", db); "git archive --format=zip HEAD", db);
assertArrayEquals(l.toArray(new String[l.size()]), assertArrayEquals(l.toArray(new String[l.size()]),
listZipEntries(result)); listZipEntries(result));
@Test @Test
public void testTarWithLongFilename() throws Exception { public void testTarWithLongFilename() throws Exception {
String filename = ""; String filename = "";
final List<String> l = new ArrayList<String>();
List<String> l = new ArrayList<String>();
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
filename = filename + "1234567890/"; filename = filename + "1234567890/";
l.add(filename); l.add(filename);
git.add().addFilepattern("1234567890").call(); git.add().addFilepattern("1234567890").call();
git.commit().setMessage("file with long name").call(); git.commit().setMessage("file with long name").call();


final byte[] result = CLIGitCommand.rawExecute( //
byte[] result = CLIGitCommand.rawExecute(
"git archive --format=tar HEAD", db); "git archive --format=tar HEAD", db);
assertArrayEquals(l.toArray(new String[l.size()]), assertArrayEquals(l.toArray(new String[l.size()]),
listTarEntries(result)); listTarEntries(result));


@Test @Test
public void testArchivePreservesContent() throws Exception { 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); writeTrashFile("xyzzy", payload);
git.add().addFilepattern("xyzzy").call(); git.add().addFilepattern("xyzzy").call();
git.commit().setMessage("add file with content").call(); git.commit().setMessage("add file with content").call();


final byte[] result = CLIGitCommand.rawExecute( //
byte[] result = CLIGitCommand.rawExecute(
"git archive --format=zip HEAD", db); "git archive --format=zip HEAD", db);
assertArrayEquals(new String[] { payload }, //
assertArrayEquals(new String[] { payload },
zipEntryContent(result, "xyzzy")); zipEntryContent(result, "xyzzy"));
} }


@Test @Test
public void testTarPreservesContent() throws Exception { 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); writeTrashFile("xyzzy", payload);
git.add().addFilepattern("xyzzy").call(); git.add().addFilepattern("xyzzy").call();
git.commit().setMessage("add file with content").call(); git.commit().setMessage("add file with content").call();


final byte[] result = CLIGitCommand.rawExecute( //
byte[] result = CLIGitCommand.rawExecute(
"git archive --format=tar HEAD", db); "git archive --format=tar HEAD", db);
assertArrayEquals(new String[] { payload }, //
assertArrayEquals(new String[] { payload },
tarEntryContent(result, "xyzzy")); tarEntryContent(result, "xyzzy"));
} }


private Process spawnAssumingCommandPresent(String... cmdline) { 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); .redirectErrorStream(true);
Process proc = null; Process proc = null;
try { try {
} }


private BufferedReader readFromProcess(Process proc) throws Exception { private BufferedReader readFromProcess(Process proc) throws Exception {
return new BufferedReader( //
return new BufferedReader(
new InputStreamReader(proc.getInputStream(), "UTF-8")); 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 { throws Exception {
final Process proc = spawnAssumingCommandPresent(cmdline);
Process proc = spawnAssumingCommandPresent(cmdline);
proc.getOutputStream().close(); proc.getOutputStream().close();
final BufferedReader reader = readFromProcess(proc);
BufferedReader reader = readFromProcess(proc);
try { try {
String line; String line;
while ((line = reader.readLine()) != null) while ((line = reader.readLine()) != null)
assertMagic(new byte[] { (byte) 0xfd, '7', 'z', 'X', 'Z', 0 }, file); 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 { throws Exception {
grepForEntry(name, mode, "zipinfo", zipFilename); 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 { throws Exception {
grepForEntry(name, mode, "tar", "tvf", tarfile); grepForEntry(name, mode, "tar", "tvf", tarfile);
} }


private void writeRaw(String filename, byte[] data) //
private void writeRaw(String filename, byte[] data)
throws IOException { 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 { try {
out.write(data); out.write(data);
} finally { } finally {
} }


private static String[] listZipEntries(byte[] zipData) throws IOException { 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)); new ByteArrayInputStream(zipData));


ZipEntry e; ZipEntry e;
} }


private static Future<Object> writeAsync(final OutputStream stream, final byte[] data) { 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 { public Object call() throws IOException {
try { try {
stream.write(data); stream.write(data);
} }


private String[] listTarEntries(byte[] tarData) throws Exception { 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 // Dump tarball to tar stdin in background
final Future<?> writing = writeAsync(out, tarData);
Future<?> writing = writeAsync(out, tarData);


try { try {
String line; String line;
} }
} }


private static String[] zipEntryContent(byte[] zipData, String path) //
private static String[] zipEntryContent(byte[] zipData, String path)
throws IOException { throws IOException {
final ZipInputStream in = new ZipInputStream( //
ZipInputStream in = new ZipInputStream(
new ByteArrayInputStream(zipData)); new ByteArrayInputStream(zipData));
ZipEntry e; ZipEntry e;
while ((e = in.getNextEntry()) != null) { while ((e = in.getNextEntry()) != null) {
continue; continue;


// found! // 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")); new InputStreamReader(in, "UTF-8"));
String line; String line;
while ((line = reader.readLine()) != null) while ((line = reader.readLine()) != null)
return null; return null;
} }


private String[] tarEntryContent(byte[] tarData, String path) //
private String[] tarEntryContent(byte[] tarData, String path)
throws Exception { 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 { try {
String line; String line;

+ 5
- 5
org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java Voir le fichier

try { try {
try (TreeWalk walk = new TreeWalk(repo); try (TreeWalk walk = new TreeWalk(repo);
RevWalk rw = new RevWalk(walk.getObjectReader())) { RevWalk rw = new RevWalk(walk.getObjectReader())) {
final String pfx = prefix == null ? "" : prefix; //$NON-NLS-1$
final T outa = fmt.createArchiveOutputStream(out, formatOptions);
final MutableObjectId idBuf = new MutableObjectId();
final ObjectReader reader = walk.getObjectReader();
String pfx = prefix == null ? "" : prefix; //$NON-NLS-1$
T outa = fmt.createArchiveOutputStream(out, formatOptions);
MutableObjectId idBuf = new MutableObjectId();
ObjectReader reader = walk.getObjectReader();


walk.reset(rw.parseTree(tree)); walk.reset(rw.parseTree(tree));
if (!paths.isEmpty()) if (!paths.isEmpty())
public OutputStream call() throws GitAPIException { public OutputStream call() throws GitAPIException {
checkCallable(); checkCallable();


final Format<?> fmt;
Format<?> fmt;
if (format == null) if (format == null)
fmt = formatBySuffix(suffix); fmt = formatBySuffix(suffix);
else else

Chargement…
Annuler
Enregistrer