Merge changes Ifdb33501,Idc7b7bbd,Ia3db8696,I4ef82311,I3ad58d4c, ... into stable-4.2

* changes:
  ConcurrentRepackTest: Don't use deprecated WindowCache.reconfigure
  ConcurrentRepackTest: Open RevWalk in try-with-resource
  CommitOnlyTest: Open RevWalk in try-with-resource
  UnpackedObjectTest: Create ObjectInserter.Formatter in try-with-resource
  FileRepositoryBuilderTest: Use try-with-resource for auto-closeables
  RepositorySetupWorkDirTest: Fix "resource leak" warnings
This commit is contained in:
Matthias Sohn 2016-01-23 20:11:04 -05:00 committed by Gerrit Code Review @ Eclipse.org
commit 281dcf8956
5 changed files with 78 additions and 69 deletions

View File

@ -1294,10 +1294,12 @@ static private String getHead(final Git git, final String path)
try { try {
final Repository repo = git.getRepository(); final Repository repo = git.getRepository();
final ObjectId headId = repo.resolve(Constants.HEAD + "^{commit}"); final ObjectId headId = repo.resolve(Constants.HEAD + "^{commit}");
try (RevWalk rw = new RevWalk(repo)) {
final TreeWalk tw = TreeWalk.forPath(repo, path, final TreeWalk tw = TreeWalk.forPath(repo, path,
new RevWalk(repo).parseTree(headId)); rw.parseTree(headId));
return new String(tw.getObjectReader().open(tw.getObjectId(0)) return new String(tw.getObjectReader().open(tw.getObjectId(0))
.getBytes()); .getBytes());
}
} catch (Exception e) { } catch (Exception e) {
return ""; return "";
} }

View File

@ -81,15 +81,14 @@ public class ConcurrentRepackTest extends RepositoryTestCase {
public void setUp() throws Exception { public void setUp() throws Exception {
WindowCacheConfig windowCacheConfig = new WindowCacheConfig(); WindowCacheConfig windowCacheConfig = new WindowCacheConfig();
windowCacheConfig.setPackedGitOpenFiles(1); windowCacheConfig.setPackedGitOpenFiles(1);
WindowCache.reconfigure(windowCacheConfig); windowCacheConfig.install();
super.setUp(); super.setUp();
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
super.tearDown(); super.tearDown();
WindowCacheConfig windowCacheConfig = new WindowCacheConfig(); new WindowCacheConfig().install();
WindowCache.reconfigure(windowCacheConfig);
} }
@Test @Test
@ -206,12 +205,14 @@ public void testObjectMovedToNewPack2()
private static void whackCache() { private static void whackCache() {
final WindowCacheConfig config = new WindowCacheConfig(); final WindowCacheConfig config = new WindowCacheConfig();
config.setPackedGitOpenFiles(1); config.setPackedGitOpenFiles(1);
WindowCache.reconfigure(config); config.install();
} }
private RevObject parse(final AnyObjectId id) private RevObject parse(final AnyObjectId id)
throws MissingObjectException, IOException { throws MissingObjectException, IOException {
return new RevWalk(db).parseAny(id); try (RevWalk rw = new RevWalk(db)) {
return rw.parseAny(id);
}
} }
private File[] pack(final Repository src, final RevObject... list) private File[] pack(final Repository src, final RevObject... list)
@ -280,7 +281,6 @@ private File fullPackFileName(final ObjectId name, final String suffix) {
private RevObject writeBlob(final Repository repo, final String data) private RevObject writeBlob(final Repository repo, final String data)
throws IOException { throws IOException {
final RevWalk revWalk = new RevWalk(repo);
final byte[] bytes = Constants.encode(data); final byte[] bytes = Constants.encode(data);
final ObjectId id; final ObjectId id;
try (ObjectInserter inserter = repo.newObjectInserter()) { try (ObjectInserter inserter = repo.newObjectInserter()) {
@ -293,6 +293,8 @@ private RevObject writeBlob(final Repository repo, final String data)
} catch (MissingObjectException e) { } catch (MissingObjectException e) {
// Ok // Ok
} }
try (RevWalk revWalk = new RevWalk(repo)) {
return revWalk.lookupBlob(id); return revWalk.lookupBlob(id);
} }
}
} }

View File

@ -72,7 +72,6 @@ public void testShouldAutomagicallyDetectGitDirectory() throws Exception {
.findGitDir(d).getGitDir()); .findGitDir(d).getGitDir());
} }
@SuppressWarnings("unused")
@Test @Test
public void emptyRepositoryFormatVersion() throws Exception { public void emptyRepositoryFormatVersion() throws Exception {
Repository r = createWorkRepository(); Repository r = createWorkRepository();
@ -81,10 +80,11 @@ public void emptyRepositoryFormatVersion() throws Exception {
ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, ""); ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, "");
config.save(); config.save();
new FileRepository(r.getDirectory()); try (FileRepository repo = new FileRepository(r.getDirectory())) {
// Unused
}
} }
@SuppressWarnings("unused")
@Test @Test
public void invalidRepositoryFormatVersion() throws Exception { public void invalidRepositoryFormatVersion() throws Exception {
Repository r = createWorkRepository(); Repository r = createWorkRepository();
@ -93,15 +93,13 @@ public void invalidRepositoryFormatVersion() throws Exception {
ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, "notanumber"); ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, "notanumber");
config.save(); config.save();
try { try (FileRepository repo = new FileRepository(r.getDirectory())) {
new FileRepository(r.getDirectory());
fail("IllegalArgumentException not thrown"); fail("IllegalArgumentException not thrown");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
assertNotNull(e.getMessage()); assertNotNull(e.getMessage());
} }
} }
@SuppressWarnings("unused")
@Test @Test
public void unknownRepositoryFormatVersion() throws Exception { public void unknownRepositoryFormatVersion() throws Exception {
Repository r = createWorkRepository(); Repository r = createWorkRepository();
@ -110,22 +108,20 @@ public void unknownRepositoryFormatVersion() throws Exception {
ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, 999999); ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, 999999);
config.save(); config.save();
try { try (FileRepository repo = new FileRepository(r.getDirectory())) {
new FileRepository(r.getDirectory());
fail("IOException not thrown"); fail("IOException not thrown");
} catch (IOException e) { } catch (IOException e) {
assertNotNull(e.getMessage()); assertNotNull(e.getMessage());
} }
} }
@SuppressWarnings("resource" /* java 7 */)
@Test @Test
public void absoluteGitDirRef() throws Exception { public void absoluteGitDirRef() throws Exception {
Repository repo1 = createWorkRepository(); Repository repo1 = createWorkRepository();
File dir = createTempDirectory("dir"); File dir = createTempDirectory("dir");
File dotGit = new File(dir, Constants.DOT_GIT); File dotGit = new File(dir, Constants.DOT_GIT);
new FileWriter(dotGit).append( try (FileWriter writer = new FileWriter(dotGit)) {
"gitdir: " + repo1.getDirectory().getAbsolutePath()).close(); writer.append("gitdir: " + repo1.getDirectory().getAbsolutePath()).close();
FileRepositoryBuilder builder = new FileRepositoryBuilder(); FileRepositoryBuilder builder = new FileRepositoryBuilder();
builder.setWorkTree(dir); builder.setWorkTree(dir);
@ -136,16 +132,16 @@ public void absoluteGitDirRef() throws Exception {
.getDirectory().getAbsolutePath()); .getDirectory().getAbsolutePath());
assertEquals(dir, repo2.getWorkTree()); assertEquals(dir, repo2.getWorkTree());
} }
}
@SuppressWarnings("resource" /* java 7 */)
@Test @Test
public void relativeGitDirRef() throws Exception { public void relativeGitDirRef() throws Exception {
Repository repo1 = createWorkRepository(); Repository repo1 = createWorkRepository();
File dir = new File(repo1.getWorkTree(), "dir"); File dir = new File(repo1.getWorkTree(), "dir");
assertTrue(dir.mkdir()); assertTrue(dir.mkdir());
File dotGit = new File(dir, Constants.DOT_GIT); File dotGit = new File(dir, Constants.DOT_GIT);
new FileWriter(dotGit).append("gitdir: ../" + Constants.DOT_GIT) try (FileWriter writer = new FileWriter(dotGit)) {
.close(); writer.append("gitdir: ../" + Constants.DOT_GIT).close();
FileRepositoryBuilder builder = new FileRepositoryBuilder(); FileRepositoryBuilder builder = new FileRepositoryBuilder();
builder.setWorkTree(dir); builder.setWorkTree(dir);
@ -158,14 +154,15 @@ public void relativeGitDirRef() throws Exception {
.getDirectory().getCanonicalPath()); .getDirectory().getCanonicalPath());
assertEquals(dir, repo2.getWorkTree()); assertEquals(dir, repo2.getWorkTree());
} }
}
@SuppressWarnings("resource" /* java 7 */)
@Test @Test
public void scanWithGitDirRef() throws Exception { public void scanWithGitDirRef() throws Exception {
Repository repo1 = createWorkRepository(); Repository repo1 = createWorkRepository();
File dir = createTempDirectory("dir"); File dir = createTempDirectory("dir");
File dotGit = new File(dir, Constants.DOT_GIT); File dotGit = new File(dir, Constants.DOT_GIT);
new FileWriter(dotGit).append( try (FileWriter writer = new FileWriter(dotGit)) {
writer.append(
"gitdir: " + repo1.getDirectory().getAbsolutePath()).close(); "gitdir: " + repo1.getDirectory().getAbsolutePath()).close();
FileRepositoryBuilder builder = new FileRepositoryBuilder(); FileRepositoryBuilder builder = new FileRepositoryBuilder();
@ -181,4 +178,5 @@ public void scanWithGitDirRef() throws Exception {
.getDirectory().getCanonicalPath()); .getDirectory().getCanonicalPath());
assertEquals(dir, repo2.getWorkTree()); assertEquals(dir, repo2.getWorkTree());
} }
}
} }

View File

@ -73,13 +73,14 @@ public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase {
public void testIsBare_CreateRepositoryFromArbitraryGitDir() public void testIsBare_CreateRepositoryFromArbitraryGitDir()
throws Exception { throws Exception {
File gitDir = getFile("workdir"); File gitDir = getFile("workdir");
assertTrue(new FileRepository(gitDir).isBare()); Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
assertTrue(repo.isBare());
} }
@Test @Test
public void testNotBare_CreateRepositoryFromDotGitGitDir() throws Exception { public void testNotBare_CreateRepositoryFromDotGitGitDir() throws Exception {
File gitDir = getFile("workdir", Constants.DOT_GIT); File gitDir = getFile("workdir", Constants.DOT_GIT);
Repository repo = new FileRepository(gitDir); Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
assertFalse(repo.isBare()); assertFalse(repo.isBare());
assertWorkdirPath(repo, "workdir"); assertWorkdirPath(repo, "workdir");
assertGitdirPath(repo, "workdir", Constants.DOT_GIT); assertGitdirPath(repo, "workdir", Constants.DOT_GIT);
@ -89,7 +90,7 @@ public void testNotBare_CreateRepositoryFromDotGitGitDir() throws Exception {
public void testWorkdirIsParentDir_CreateRepositoryFromDotGitGitDir() public void testWorkdirIsParentDir_CreateRepositoryFromDotGitGitDir()
throws Exception { throws Exception {
File gitDir = getFile("workdir", Constants.DOT_GIT); File gitDir = getFile("workdir", Constants.DOT_GIT);
Repository repo = new FileRepository(gitDir); Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
String workdir = repo.getWorkTree().getName(); String workdir = repo.getWorkTree().getName();
assertEquals(workdir, "workdir"); assertEquals(workdir, "workdir");
} }
@ -157,8 +158,8 @@ public void testNotBare_CreateRepositoryFromGitDirOnlyWithBareConfigFalse()
@Test @Test
public void testExceptionThrown_BareRepoGetWorkDir() throws Exception { public void testExceptionThrown_BareRepoGetWorkDir() throws Exception {
File gitDir = getFile("workdir"); File gitDir = getFile("workdir");
try { try (Repository repo = new FileRepository(gitDir)) {
new FileRepository(gitDir).getWorkTree(); repo.getWorkTree();
fail("Expected NoWorkTreeException missing"); fail("Expected NoWorkTreeException missing");
} catch (NoWorkTreeException e) { } catch (NoWorkTreeException e) {
// expected // expected
@ -168,8 +169,8 @@ public void testExceptionThrown_BareRepoGetWorkDir() throws Exception {
@Test @Test
public void testExceptionThrown_BareRepoGetIndex() throws Exception { public void testExceptionThrown_BareRepoGetIndex() throws Exception {
File gitDir = getFile("workdir"); File gitDir = getFile("workdir");
try { try (Repository repo = new FileRepository(gitDir)) {
new FileRepository(gitDir).readDirCache(); repo.readDirCache();
fail("Expected NoWorkTreeException missing"); fail("Expected NoWorkTreeException missing");
} catch (NoWorkTreeException e) { } catch (NoWorkTreeException e) {
// expected // expected
@ -179,8 +180,8 @@ public void testExceptionThrown_BareRepoGetIndex() throws Exception {
@Test @Test
public void testExceptionThrown_BareRepoGetIndexFile() throws Exception { public void testExceptionThrown_BareRepoGetIndexFile() throws Exception {
File gitDir = getFile("workdir"); File gitDir = getFile("workdir");
try { try (Repository repo = new FileRepository(gitDir)) {
new FileRepository(gitDir).getIndexFile(); repo.getIndexFile();
fail("Expected NoWorkTreeException missing"); fail("Expected NoWorkTreeException missing");
} catch (NoWorkTreeException e) { } catch (NoWorkTreeException e) {
// expected // expected

View File

@ -143,7 +143,7 @@ public void testStandardFormat_SmallObject() throws Exception {
public void testStandardFormat_LargeObject() throws Exception { public void testStandardFormat_LargeObject() throws Exception {
final int type = Constants.OBJ_BLOB; final int type = Constants.OBJ_BLOB;
byte[] data = getRng().nextBytes(streamThreshold + 5); byte[] data = getRng().nextBytes(streamThreshold + 5);
ObjectId id = new ObjectInserter.Formatter().idFor(type, data); ObjectId id = getId(type, data);
write(id, compressStandardFormat(type, data)); write(id, compressStandardFormat(type, data));
ObjectLoader ol; ObjectLoader ol;
@ -306,7 +306,7 @@ public void testStandardFormat_LargeObject_CorruptZLibStream()
throws Exception { throws Exception {
final int type = Constants.OBJ_BLOB; final int type = Constants.OBJ_BLOB;
byte[] data = getRng().nextBytes(streamThreshold + 5); byte[] data = getRng().nextBytes(streamThreshold + 5);
ObjectId id = new ObjectInserter.Formatter().idFor(type, data); ObjectId id = getId(type, data);
byte[] gz = compressStandardFormat(type, data); byte[] gz = compressStandardFormat(type, data);
gz[gz.length - 1] = 0; gz[gz.length - 1] = 0;
gz[gz.length - 2] = 0; gz[gz.length - 2] = 0;
@ -344,7 +344,7 @@ public void testStandardFormat_LargeObject_TruncatedZLibStream()
throws Exception { throws Exception {
final int type = Constants.OBJ_BLOB; final int type = Constants.OBJ_BLOB;
byte[] data = getRng().nextBytes(streamThreshold + 5); byte[] data = getRng().nextBytes(streamThreshold + 5);
ObjectId id = new ObjectInserter.Formatter().idFor(type, data); ObjectId id = getId(type, data);
byte[] gz = compressStandardFormat(type, data); byte[] gz = compressStandardFormat(type, data);
byte[] tr = new byte[gz.length - 1]; byte[] tr = new byte[gz.length - 1];
System.arraycopy(gz, 0, tr, 0, tr.length); System.arraycopy(gz, 0, tr, 0, tr.length);
@ -379,7 +379,7 @@ public void testStandardFormat_LargeObject_TrailingGarbage()
throws Exception { throws Exception {
final int type = Constants.OBJ_BLOB; final int type = Constants.OBJ_BLOB;
byte[] data = getRng().nextBytes(streamThreshold + 5); byte[] data = getRng().nextBytes(streamThreshold + 5);
ObjectId id = new ObjectInserter.Formatter().idFor(type, data); ObjectId id = getId(type, data);
byte[] gz = compressStandardFormat(type, data); byte[] gz = compressStandardFormat(type, data);
byte[] tr = new byte[gz.length + 1]; byte[] tr = new byte[gz.length + 1];
System.arraycopy(gz, 0, tr, 0, gz.length); System.arraycopy(gz, 0, tr, 0, gz.length);
@ -438,7 +438,7 @@ public void testPackFormat_SmallObject() throws Exception {
public void testPackFormat_LargeObject() throws Exception { public void testPackFormat_LargeObject() throws Exception {
final int type = Constants.OBJ_BLOB; final int type = Constants.OBJ_BLOB;
byte[] data = getRng().nextBytes(streamThreshold + 5); byte[] data = getRng().nextBytes(streamThreshold + 5);
ObjectId id = new ObjectInserter.Formatter().idFor(type, data); ObjectId id = getId(type, data);
write(id, compressPackFormat(type, data)); write(id, compressPackFormat(type, data));
ObjectLoader ol; ObjectLoader ol;
@ -578,4 +578,10 @@ private void write(ObjectId id, byte[] data) throws IOException {
out.close(); out.close();
} }
} }
private ObjectId getId(int type, byte[] data) {
try (ObjectInserter.Formatter formatter = new ObjectInserter.Formatter()) {
return formatter.idFor(type, data);
}
}
} }