* 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" warningstags/v4.3.0.201603230630-rc1
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}"); | ||||
final TreeWalk tw = TreeWalk.forPath(repo, path, | |||||
new RevWalk(repo).parseTree(headId)); | |||||
return new String(tw.getObjectReader().open(tw.getObjectId(0)) | |||||
.getBytes()); | |||||
try (RevWalk rw = new RevWalk(repo)) { | |||||
final TreeWalk tw = TreeWalk.forPath(repo, path, | |||||
rw.parseTree(headId)); | |||||
return new String(tw.getObjectReader().open(tw.getObjectId(0)) | |||||
.getBytes()); | |||||
} | |||||
} catch (Exception e) { | } catch (Exception e) { | ||||
return ""; | return ""; | ||||
} | } |
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(); | |||||
WindowCache.reconfigure(windowCacheConfig); | |||||
new WindowCacheConfig().install(); | |||||
} | } | ||||
@Test | @Test | ||||
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) | ||||
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()) { | ||||
} catch (MissingObjectException e) { | } catch (MissingObjectException e) { | ||||
// Ok | // Ok | ||||
} | } | ||||
return revWalk.lookupBlob(id); | |||||
try (RevWalk revWalk = new RevWalk(repo)) { | |||||
return revWalk.lookupBlob(id); | |||||
} | |||||
} | } | ||||
} | } |
.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(); | ||||
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(); | ||||
ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, "notanumber"); | ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, "notanumber"); | ||||
config.save(); | config.save(); | ||||
try { | |||||
new FileRepository(r.getDirectory()); | |||||
try (FileRepository repo = 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(); | ||||
ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, 999999); | ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, 999999); | ||||
config.save(); | config.save(); | ||||
try { | |||||
new FileRepository(r.getDirectory()); | |||||
try (FileRepository repo = 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( | |||||
"gitdir: " + repo1.getDirectory().getAbsolutePath()).close(); | |||||
FileRepositoryBuilder builder = new FileRepositoryBuilder(); | |||||
try (FileWriter writer = new FileWriter(dotGit)) { | |||||
writer.append("gitdir: " + repo1.getDirectory().getAbsolutePath()).close(); | |||||
FileRepositoryBuilder builder = new FileRepositoryBuilder(); | |||||
builder.setWorkTree(dir); | |||||
builder.setMustExist(true); | |||||
Repository repo2 = builder.build(); | |||||
builder.setWorkTree(dir); | |||||
builder.setMustExist(true); | |||||
Repository repo2 = builder.build(); | |||||
assertEquals(repo1.getDirectory().getAbsolutePath(), repo2 | |||||
.getDirectory().getAbsolutePath()); | |||||
assertEquals(dir, repo2.getWorkTree()); | |||||
assertEquals(repo1.getDirectory().getAbsolutePath(), repo2 | |||||
.getDirectory().getAbsolutePath()); | |||||
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) | |||||
.close(); | |||||
FileRepositoryBuilder builder = new FileRepositoryBuilder(); | |||||
builder.setWorkTree(dir); | |||||
builder.setMustExist(true); | |||||
Repository repo2 = builder.build(); | |||||
// The tmp directory may be a symlink so the actual path | |||||
// may not | |||||
assertEquals(repo1.getDirectory().getCanonicalPath(), repo2 | |||||
.getDirectory().getCanonicalPath()); | |||||
assertEquals(dir, repo2.getWorkTree()); | |||||
try (FileWriter writer = new FileWriter(dotGit)) { | |||||
writer.append("gitdir: ../" + Constants.DOT_GIT).close(); | |||||
FileRepositoryBuilder builder = new FileRepositoryBuilder(); | |||||
builder.setWorkTree(dir); | |||||
builder.setMustExist(true); | |||||
Repository repo2 = builder.build(); | |||||
// The tmp directory may be a symlink so the actual path | |||||
// may not | |||||
assertEquals(repo1.getDirectory().getCanonicalPath(), repo2 | |||||
.getDirectory().getCanonicalPath()); | |||||
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( | |||||
"gitdir: " + repo1.getDirectory().getAbsolutePath()).close(); | |||||
FileRepositoryBuilder builder = new FileRepositoryBuilder(); | |||||
builder.setWorkTree(dir); | |||||
builder.findGitDir(dir); | |||||
assertEquals(repo1.getDirectory().getAbsolutePath(), builder | |||||
.getGitDir().getAbsolutePath()); | |||||
builder.setMustExist(true); | |||||
Repository repo2 = builder.build(); | |||||
// The tmp directory may be a symlink | |||||
assertEquals(repo1.getDirectory().getCanonicalPath(), repo2 | |||||
.getDirectory().getCanonicalPath()); | |||||
assertEquals(dir, repo2.getWorkTree()); | |||||
try (FileWriter writer = new FileWriter(dotGit)) { | |||||
writer.append( | |||||
"gitdir: " + repo1.getDirectory().getAbsolutePath()).close(); | |||||
FileRepositoryBuilder builder = new FileRepositoryBuilder(); | |||||
builder.setWorkTree(dir); | |||||
builder.findGitDir(dir); | |||||
assertEquals(repo1.getDirectory().getAbsolutePath(), builder | |||||
.getGitDir().getAbsolutePath()); | |||||
builder.setMustExist(true); | |||||
Repository repo2 = builder.build(); | |||||
// The tmp directory may be a symlink | |||||
assertEquals(repo1.getDirectory().getCanonicalPath(), repo2 | |||||
.getDirectory().getCanonicalPath()); | |||||
assertEquals(dir, repo2.getWorkTree()); | |||||
} | |||||
} | } | ||||
} | } |
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); | ||||
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"); | ||||
} | } | ||||
@Test | @Test | ||||
public void testExceptionThrown_BareRepoGetWorkDir() throws Exception { | public void testExceptionThrown_BareRepoGetWorkDir() throws Exception { | ||||
File gitDir = getFile("workdir"); | File gitDir = getFile("workdir"); | ||||
try { | |||||
new FileRepository(gitDir).getWorkTree(); | |||||
try (Repository repo = new FileRepository(gitDir)) { | |||||
repo.getWorkTree(); | |||||
fail("Expected NoWorkTreeException missing"); | fail("Expected NoWorkTreeException missing"); | ||||
} catch (NoWorkTreeException e) { | } catch (NoWorkTreeException e) { | ||||
// expected | // expected | ||||
@Test | @Test | ||||
public void testExceptionThrown_BareRepoGetIndex() throws Exception { | public void testExceptionThrown_BareRepoGetIndex() throws Exception { | ||||
File gitDir = getFile("workdir"); | File gitDir = getFile("workdir"); | ||||
try { | |||||
new FileRepository(gitDir).readDirCache(); | |||||
try (Repository repo = new FileRepository(gitDir)) { | |||||
repo.readDirCache(); | |||||
fail("Expected NoWorkTreeException missing"); | fail("Expected NoWorkTreeException missing"); | ||||
} catch (NoWorkTreeException e) { | } catch (NoWorkTreeException e) { | ||||
// expected | // expected | ||||
@Test | @Test | ||||
public void testExceptionThrown_BareRepoGetIndexFile() throws Exception { | public void testExceptionThrown_BareRepoGetIndexFile() throws Exception { | ||||
File gitDir = getFile("workdir"); | File gitDir = getFile("workdir"); | ||||
try { | |||||
new FileRepository(gitDir).getIndexFile(); | |||||
try (Repository repo = new FileRepository(gitDir)) { | |||||
repo.getIndexFile(); | |||||
fail("Expected NoWorkTreeException missing"); | fail("Expected NoWorkTreeException missing"); | ||||
} catch (NoWorkTreeException e) { | } catch (NoWorkTreeException e) { | ||||
// expected | // expected |
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; | ||||
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; | ||||
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); | ||||
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); | ||||
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; | ||||
out.close(); | out.close(); | ||||
} | } | ||||
} | } | ||||
private ObjectId getId(int type, byte[] data) { | |||||
try (ObjectInserter.Formatter formatter = new ObjectInserter.Formatter()) { | |||||
return formatter.idFor(type, data); | |||||
} | |||||
} | |||||
} | } |