Browse 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 9 years ago
parent
commit
a5778b6f41

+ 75
- 75
org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java View File

@@ -92,22 +92,22 @@ public class ArchiveTest extends CLIRepositoryTestCase {
@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);
}

@@ -120,9 +120,9 @@ public class ArchiveTest extends CLIRepositoryTestCase {
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));
}

@@ -135,9 +135,9 @@ public class ArchiveTest extends CLIRepositoryTestCase {
@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));
}

@@ -147,8 +147,8 @@ public class ArchiveTest extends CLIRepositoryTestCase {

@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[] { "" },
@@ -162,8 +162,8 @@ public class ArchiveTest extends CLIRepositoryTestCase {

@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[] { "" },
@@ -176,8 +176,8 @@ public class ArchiveTest extends CLIRepositoryTestCase {

@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[] { "" },
@@ -189,8 +189,8 @@ public class ArchiveTest extends CLIRepositoryTestCase {

@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[] { "" },
@@ -202,8 +202,8 @@ public class ArchiveTest extends CLIRepositoryTestCase {

@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 " +
@@ -218,8 +218,8 @@ public class ArchiveTest extends CLIRepositoryTestCase {

@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[] { "" },
@@ -234,8 +234,8 @@ public class ArchiveTest extends CLIRepositoryTestCase {
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()) + " " +
@@ -253,8 +253,8 @@ public class ArchiveTest extends CLIRepositoryTestCase {
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()) + " " +
@@ -272,8 +272,8 @@ public class ArchiveTest extends CLIRepositoryTestCase {
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()) + " " +
@@ -302,7 +302,7 @@ public class ArchiveTest extends CLIRepositoryTestCase {
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);
@@ -328,7 +328,7 @@ public class ArchiveTest extends CLIRepositoryTestCase {
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);
@@ -390,7 +390,7 @@ public class ArchiveTest extends CLIRepositoryTestCase {
@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));
@@ -421,7 +421,7 @@ public class ArchiveTest extends CLIRepositoryTestCase {
@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);
@@ -441,7 +441,7 @@ public class ArchiveTest extends CLIRepositoryTestCase {
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);
@@ -461,7 +461,7 @@ public class ArchiveTest extends CLIRepositoryTestCase {
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);
@@ -491,7 +491,7 @@ public class ArchiveTest extends CLIRepositoryTestCase {

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");
@@ -520,7 +520,7 @@ public class ArchiveTest extends CLIRepositoryTestCase {

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");
@@ -532,7 +532,7 @@ public class ArchiveTest extends CLIRepositoryTestCase {
@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);
@@ -543,7 +543,7 @@ public class ArchiveTest extends CLIRepositoryTestCase {
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));
@@ -552,7 +552,7 @@ public class ArchiveTest extends CLIRepositoryTestCase {
@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);
@@ -563,7 +563,7 @@ public class ArchiveTest extends CLIRepositoryTestCase {
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));
@@ -571,34 +571,34 @@ public class ArchiveTest extends CLIRepositoryTestCase {

@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 {
@@ -612,15 +612,15 @@ public class ArchiveTest extends CLIRepositoryTestCase {
}

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)
@@ -672,20 +672,20 @@ public class ArchiveTest extends CLIRepositoryTestCase {
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 {
@@ -694,8 +694,8 @@ public class ArchiveTest extends CLIRepositoryTestCase {
}

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;
@@ -706,9 +706,9 @@ public class ArchiveTest extends CLIRepositoryTestCase {
}

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);
@@ -721,13 +721,13 @@ public class ArchiveTest extends CLIRepositoryTestCase {
}

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;
@@ -742,9 +742,9 @@ public class ArchiveTest extends CLIRepositoryTestCase {
}
}

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) {
@@ -752,8 +752,8 @@ public class ArchiveTest extends CLIRepositoryTestCase {
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)
@@ -765,13 +765,13 @@ public class ArchiveTest extends CLIRepositoryTestCase {
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;

+ 5
- 5
org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java View File

@@ -367,10 +367,10 @@ public class ArchiveCommand extends GitCommand<OutputStream> {
try {
try (TreeWalk walk = new TreeWalk(repo);
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));
if (!paths.isEmpty())
@@ -414,7 +414,7 @@ public class ArchiveCommand extends GitCommand<OutputStream> {
public OutputStream call() throws GitAPIException {
checkCallable();

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

Loading…
Cancel
Save