See the discussion [1] in the Gerrit mailing list.
[1] https://groups.google.com/forum/#!topic/repo-discuss/RRQT_xCqz4o
Change-Id: I2c67384309c5c2e8511a7d0d4e088b4e95f819ff
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
try {
rp.sendAdvertisedRefs(pck);
} finally {
- rp.getRevWalk().release();
+ rp.getRevWalk().close();
}
}
}
up.setBiDirectionalPipe(false);
up.sendAdvertisedRefs(pck);
} finally {
- up.getRevWalk().release();
+ up.getRevWalk().close();
}
}
}
*/
protected void resetIndex(FileTreeIterator treeItr)
throws FileNotFoundException, IOException {
- ObjectInserter inserter = db.newObjectInserter();
- DirCacheBuilder builder = db.lockDirCache().builder();
- DirCacheEntry dce;
-
- while (!treeItr.eof()) {
- long len = treeItr.getEntryLength();
-
- dce = new DirCacheEntry(treeItr.getEntryPathString());
- dce.setFileMode(treeItr.getEntryFileMode());
- dce.setLastModified(treeItr.getEntryLastModified());
- dce.setLength((int) len);
- FileInputStream in = new FileInputStream(treeItr.getEntryFile());
- dce.setObjectId(inserter.insert(Constants.OBJ_BLOB, len, in));
- in.close();
- builder.add(dce);
- treeItr.next(1);
+ try (ObjectInserter inserter = db.newObjectInserter()) {
+ DirCacheBuilder builder = db.lockDirCache().builder();
+ DirCacheEntry dce;
+
+ while (!treeItr.eof()) {
+ long len = treeItr.getEntryLength();
+
+ dce = new DirCacheEntry(treeItr.getEntryPathString());
+ dce.setFileMode(treeItr.getEntryFileMode());
+ dce.setLastModified(treeItr.getEntryLastModified());
+ dce.setLength((int) len);
+ FileInputStream in = new FileInputStream(
+ treeItr.getEntryFile());
+ dce.setObjectId(inserter.insert(Constants.OBJ_BLOB, len, in));
+ in.close();
+ builder.add(dce);
+ treeItr.next(1);
+ }
+ builder.commit();
+ inserter.flush();
}
- builder.commit();
- inserter.flush();
- inserter.release();
}
/**
protected void checkoutBranch(String branchName)
throws IllegalStateException, IOException {
- RevWalk walk = new RevWalk(db);
- RevCommit head = walk.parseCommit(db.resolve(Constants.HEAD));
- RevCommit branch = walk.parseCommit(db.resolve(branchName));
- DirCacheCheckout dco = new DirCacheCheckout(db, head.getTree().getId(),
- db.lockDirCache(), branch.getTree().getId());
- dco.setFailOnConflict(true);
- dco.checkout();
- walk.release();
+ try (RevWalk walk = new RevWalk(db)) {
+ RevCommit head = walk.parseCommit(db.resolve(Constants.HEAD));
+ RevCommit branch = walk.parseCommit(db.resolve(branchName));
+ DirCacheCheckout dco = new DirCacheCheckout(db,
+ head.getTree().getId(), db.lockDirCache(),
+ branch.getTree().getId());
+ dco.setFailOnConflict(true);
+ dco.checkout();
+ }
// update the HEAD
RefUpdate refUpdate = db.updateRef(Constants.HEAD);
refUpdate.setRefLogMessage("checkout: moving to " + branchName, false);
private boolean verbose;
protected void showFetchResult(final FetchResult r) throws IOException {
- ObjectReader reader = db.newObjectReader();
- try {
+ try (ObjectReader reader = db.newObjectReader()) {
boolean shownURI = false;
for (final TrackingRefUpdate u : r.getTrackingRefUpdates()) {
if (!verbose && u.getResult() == RefUpdate.Result.NO_CHANGE)
src, dst);
outw.println();
}
- } finally {
- reader.release();
}
showRemoteMessages(errw, r.getMessages());
}
else
dateFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ZZZZ"); //$NON-NLS-1$
- BlameGenerator generator = new BlameGenerator(db, file);
- RevFlag scanned = generator.newFlag("SCANNED"); //$NON-NLS-1$
reader = db.newObjectReader();
- try {
+ try (BlameGenerator generator = new BlameGenerator(db, file)) {
+ RevFlag scanned = generator.newFlag("SCANNED"); //$NON-NLS-1$
generator.setTextComparator(comparator);
if (!reverseRange.isEmpty()) {
} while (++line < end && blame.getSourceCommit(line) == c);
}
} finally {
- generator.release();
- reader.release();
+ reader.close();
}
}
addRefs(refs, Constants.R_HEADS);
addRefs(refs, Constants.R_REMOTES);
- ObjectReader reader = db.newObjectReader();
- try {
+ try (ObjectReader reader = db.newObjectReader()) {
for (final Entry<String, Ref> e : printRefs.entrySet()) {
final Ref ref = e.getValue();
printHead(reader, e.getKey(),
current.equals(ref.getName()), ref);
}
- } finally {
- reader.release();
}
}
}
if (head == null)
die(MessageFormat.format(CLIText.get().notATree, HEAD));
CanonicalTreeParser p = new CanonicalTreeParser();
- ObjectReader reader = db.newObjectReader();
- try {
+ try (ObjectReader reader = db.newObjectReader()) {
p.reset(reader, head);
- } finally {
- reader.release();
}
oldTree = p;
}
diffFmt.flush();
}
} finally {
- diffFmt.release();
+ diffFmt.close();
}
}
@Override
protected void run() throws Exception {
BufferedInputStream in = new BufferedInputStream(ins);
- ObjectInserter inserter = db.newObjectInserter();
- try {
+ try (ObjectInserter inserter = db.newObjectInserter()) {
PackParser p = inserter.newPackParser(in);
p.setAllowThin(fixThin);
if (indexVersion != -1 && p instanceof ObjectDirectoryPackParser) {
}
p.parse(new TextProgressMonitor(errw));
inserter.flush();
- } finally {
- inserter.release();
}
}
}
super.run();
} finally {
- diffFmt.release();
+ diffFmt.close();
}
}
push.setTimeout(timeout);
Iterable<PushResult> results = push.call();
for (PushResult result : results) {
- ObjectReader reader = db.newObjectReader();
- try {
+ try (ObjectReader reader = db.newObjectReader()) {
printPushResult(reader, result.getURI(), result);
- } finally {
- reader.release();
}
}
}
else
objectId = db.resolve(objectName);
- RevWalk rw = new RevWalk(db);
- try {
+ try (RevWalk rw = new RevWalk(db)) {
RevObject obj = rw.parseAny(objectId);
while (obj instanceof RevTag) {
show((RevTag) obj);
CLIText.get().cannotReadBecause, obj.name(),
obj.getType()));
}
- } finally {
- rw.release();
}
} finally {
- diffFmt.release();
+ diffFmt.close();
}
}
int maxN = 0;
AbbreviatedObjectId startId;
- ObjectReader or = db.newObjectReader();
- try {
+ try (ObjectReader or = db.newObjectReader()) {
final MutableObjectId id = new MutableObjectId();
RevWalk rw = new RevWalk(or);
TreeWalk tw = new TreeWalk(or);
if (count > 0 && files > count)
break;
}
- } finally {
- or.release();
}
Collections.sort(all, new Comparator<Test>() {
}
private void recreateCommitGraph() throws IOException {
- final RevWalk rw = new RevWalk(db);
final Map<ObjectId, ToRewrite> toRewrite = new HashMap<ObjectId, ToRewrite>();
List<ToRewrite> queue = new ArrayList<ToRewrite>();
- final BufferedReader br = new BufferedReader(new InputStreamReader(
- new FileInputStream(graph), Constants.CHARSET));
- try {
+ try (RevWalk rw = new RevWalk(db);
+ final BufferedReader br = new BufferedReader(
+ new InputStreamReader(new FileInputStream(graph),
+ Constants.CHARSET))) {
String line;
while ((line = br.readLine()) != null) {
final String[] parts = line.split("[ \t]{1,}"); //$NON-NLS-1$
toRewrite.put(oldId, t);
queue.add(t);
}
- } finally {
- br.close();
}
pm.beginTask("Rewriting commits", queue.size());
- final ObjectInserter oi = db.newObjectInserter();
- final ObjectId emptyTree = oi.insert(Constants.OBJ_TREE, new byte[] {});
- final PersonIdent me = new PersonIdent("jgit rebuild-commitgraph", //$NON-NLS-1$
- "rebuild-commitgraph@localhost"); //$NON-NLS-1$
- while (!queue.isEmpty()) {
- final ListIterator<ToRewrite> itr = queue
- .listIterator(queue.size());
- queue = new ArrayList<ToRewrite>();
- REWRITE: while (itr.hasPrevious()) {
- final ToRewrite t = itr.previous();
- final ObjectId[] newParents = new ObjectId[t.oldParents.length];
- for (int k = 0; k < t.oldParents.length; k++) {
- final ToRewrite p = toRewrite.get(t.oldParents[k]);
- if (p != null) {
- if (p.newId == null) {
- // Must defer until after the parent is rewritten.
- queue.add(t);
- continue REWRITE;
+ try (ObjectInserter oi = db.newObjectInserter()) {
+ final ObjectId emptyTree = oi.insert(Constants.OBJ_TREE,
+ new byte[] {});
+ final PersonIdent me = new PersonIdent("jgit rebuild-commitgraph", //$NON-NLS-1$
+ "rebuild-commitgraph@localhost"); //$NON-NLS-1$
+ while (!queue.isEmpty()) {
+ final ListIterator<ToRewrite> itr = queue
+ .listIterator(queue.size());
+ queue = new ArrayList<ToRewrite>();
+ REWRITE: while (itr.hasPrevious()) {
+ final ToRewrite t = itr.previous();
+ final ObjectId[] newParents = new ObjectId[t.oldParents.length];
+ for (int k = 0; k < t.oldParents.length; k++) {
+ final ToRewrite p = toRewrite.get(t.oldParents[k]);
+ if (p != null) {
+ if (p.newId == null) {
+ // Must defer until after the parent is
+ // rewritten.
+ queue.add(t);
+ continue REWRITE;
+ } else {
+ newParents[k] = p.newId;
+ }
} else {
- newParents[k] = p.newId;
+ // We have the old parent object. Use it.
+ //
+ newParents[k] = t.oldParents[k];
}
- } else {
- // We have the old parent object. Use it.
- //
- newParents[k] = t.oldParents[k];
}
- }
- final CommitBuilder newc = new CommitBuilder();
- newc.setTreeId(emptyTree);
- newc.setAuthor(new PersonIdent(me, new Date(t.commitTime)));
- newc.setCommitter(newc.getAuthor());
- newc.setParentIds(newParents);
- newc.setMessage("ORIGINAL " + t.oldId.name() + "\n"); //$NON-NLS-2$
- t.newId = oi.insert(newc);
- rewrites.put(t.oldId, t.newId);
- pm.update(1);
+ final CommitBuilder newc = new CommitBuilder();
+ newc.setTreeId(emptyTree);
+ newc.setAuthor(new PersonIdent(me, new Date(t.commitTime)));
+ newc.setCommitter(newc.getAuthor());
+ newc.setParentIds(newParents);
+ newc.setMessage("ORIGINAL " + t.oldId.name() + "\n"); //$NON-NLS-2$
+ t.newId = oi.insert(newc);
+ rewrites.put(t.oldId, t.newId);
+ pm.update(1);
+ }
}
+ oi.flush();
}
- oi.flush();
- oi.release();
pm.endTask();
}
}
private Map<String, Ref> computeNewRefs() throws IOException {
- final RevWalk rw = new RevWalk(db);
final Map<String, Ref> refs = new HashMap<String, Ref>();
- final BufferedReader br = new BufferedReader(new InputStreamReader(
- new FileInputStream(refList), Constants.CHARSET));
- try {
+ try (RevWalk rw = new RevWalk(db);
+ BufferedReader br = new BufferedReader(
+ new InputStreamReader(new FileInputStream(refList),
+ Constants.CHARSET))) {
String line;
while ((line = br.readLine()) != null) {
final String[] parts = line.split("[ \t]{1,}"); //$NON-NLS-1$
refs.put(name, new ObjectIdRef.Unpeeled(Ref.Storage.PACKED,
name, id));
}
- } finally {
- rw.release();
- br.close();
}
return refs;
}
long fileCnt = 0;
long lineCnt = 0;
- ObjectReader or = db.newObjectReader();
- try {
+ try (ObjectReader or = db.newObjectReader()) {
final MutableObjectId id = new MutableObjectId();
RevWalk rw = new RevWalk(or);
TreeWalk tw = new TreeWalk(or);
for (Function fun : all)
testOne(fun, txt, lines, cnt);
}
- } finally {
- or.release();
}
if (db.getDirectory() != null) {
throw new CmdLineException(MessageFormat.format(CLIText.get().notATree, name));
final CanonicalTreeParser p = new CanonicalTreeParser();
- final ObjectReader curs = clp.getRepository().newObjectReader();
- try {
+ try (ObjectReader curs = clp.getRepository().newObjectReader()) {
p.reset(curs, clp.getRevWalk().parseTree(id));
} catch (MissingObjectException e) {
throw new CmdLineException(MessageFormat.format(CLIText.get().notATree, name));
throw new CmdLineException(MessageFormat.format(CLIText.get().notATree, name));
} catch (IOException e) {
throw new CmdLineException(MessageFormat.format(CLIText.get().cannotReadBecause, name, e.getMessage()));
- } finally {
- curs.release();
}
setter.addValue(p);
assertEquals(commit, pathStatus.getHeadId());
assertEquals(commit, pathStatus.getIndexId());
- SubmoduleWalk walk = SubmoduleWalk.forIndex(git2.getRepository());
- assertTrue(walk.next());
- Repository clonedSub1 = walk.getRepository();
- addRepoToClose(clonedSub1);
- assertNotNull(clonedSub1);
- assertEquals(
- new File(git2.getRepository().getWorkTree(), walk.getPath()),
- clonedSub1.getWorkTree());
- assertEquals(new File(new File(git2.getRepository().getDirectory(),
- "modules"), walk.getPath()), clonedSub1.getDirectory());
- walk.release();
+ try (SubmoduleWalk walk = SubmoduleWalk
+ .forIndex(git2.getRepository())) {
+ assertTrue(walk.next());
+ Repository clonedSub1 = walk.getRepository();
+ addRepoToClose(clonedSub1);
+ assertNotNull(clonedSub1);
+ assertEquals(new File(git2.getRepository().getWorkTree(),
+ walk.getPath()), clonedSub1.getWorkTree());
+ assertEquals(
+ new File(new File(git2.getRepository().getDirectory(),
+ "modules"), walk.getPath()),
+ clonedSub1.getDirectory());
+ }
}
@Test
+ "[unmerged2, mode:100644, stage:3]",
indexState(0));
- TreeWalk walk = TreeWalk.forPath(db, "unmerged1", commit.getTree());
- assertEquals(FileMode.REGULAR_FILE, walk.getFileMode(0));
- walk.release();
+ try (TreeWalk walk = TreeWalk.forPath(db, "unmerged1", commit.getTree())) {
+ assertEquals(FileMode.REGULAR_FILE, walk.getFileMode(0));
+ }
}
private static void addUnmergedEntry(String file, DirCacheBuilder builder) {
if (id == null)
throw new IllegalArgumentException(name);
final CanonicalTreeParser p = new CanonicalTreeParser();
- final ObjectReader or = db.newObjectReader();
- try {
+ try (ObjectReader or = db.newObjectReader()) {
p.reset(or, new RevWalk(db).parseTree(id));
return p;
- } finally {
- or.release();
}
}
}
public static void validateIndex(Git git) throws NoWorkTreeException,
IOException {
DirCache dc = git.getRepository().lockDirCache();
- ObjectReader r = git.getRepository().getObjectDatabase().newReader();
- try {
+ try (ObjectReader r = git.getRepository().getObjectDatabase()
+ .newReader()) {
for (int i = 0; i < dc.getEntryCount(); ++i) {
DirCacheEntry entry = dc.getEntry(i);
if (entry.getLength() > 0)
}
} finally {
dc.unlock();
- r.release();
}
}
}
assertFileContentsEqual(sourceFile, "content");
- RevWalk rw = new RevWalk(dbTarget);
- rw.sort(RevSort.TOPO);
- rw.markStart(rw.parseCommit(dbTarget.resolve("refs/heads/master")));
+ try (RevWalk rw = new RevWalk(dbTarget)) {
+ rw.sort(RevSort.TOPO);
+ rw.markStart(rw.parseCommit(dbTarget.resolve("refs/heads/master")));
- RevCommit next;
- if (expectedPullMode == TestPullMode.MERGE) {
- next = rw.next();
- assertEquals(2, next.getParentCount());
- assertEquals(merge, next.getParent(0));
- assertEquals(sourceCommit, next.getParent(1));
- // since both parents are known do no further checks here
- } else {
- if (expectedPullMode == TestPullMode.REBASE_PREASERVE) {
+ RevCommit next;
+ if (expectedPullMode == TestPullMode.MERGE) {
next = rw.next();
assertEquals(2, next.getParentCount());
+ assertEquals(merge, next.getParent(0));
+ assertEquals(sourceCommit, next.getParent(1));
+ // since both parents are known do no further checks here
+ } else {
+ if (expectedPullMode == TestPullMode.REBASE_PREASERVE) {
+ next = rw.next();
+ assertEquals(2, next.getParentCount());
+ }
+ next = rw.next();
+ assertEquals(t2.getShortMessage(), next.getShortMessage());
+ next = rw.next();
+ assertEquals(t1.getShortMessage(), next.getShortMessage());
+ next = rw.next();
+ assertEquals(sourceCommit, next);
+ next = rw.next();
+ assertEquals("Initial commit for source",
+ next.getShortMessage());
+ next = rw.next();
+ assertNull(next);
}
- next = rw.next();
- assertEquals(t2.getShortMessage(), next.getShortMessage());
- next = rw.next();
- assertEquals(t1.getShortMessage(), next.getShortMessage());
- next = rw.next();
- assertEquals(sourceCommit, next);
- next = rw.next();
- assertEquals("Initial commit for source", next.getShortMessage());
- next = rw.next();
- assertNull(next);
}
- rw.release();
}
@Override
private void checkoutCommit(RevCommit commit) throws IllegalStateException,
IOException {
- RevWalk walk = new RevWalk(db);
- RevCommit head = walk.parseCommit(db.resolve(Constants.HEAD));
- DirCacheCheckout dco = new DirCacheCheckout(db, head.getTree(), db
- .lockDirCache(), commit.getTree());
- dco.setFailOnConflict(true);
- dco.checkout();
- walk.release();
+ RevCommit head;
+ try (RevWalk walk = new RevWalk(db)) {
+ head = walk.parseCommit(db.resolve(Constants.HEAD));
+ DirCacheCheckout dco = new DirCacheCheckout(db, head.getTree(),
+ db.lockDirCache(), commit.getTree());
+ dco.setFailOnConflict(true);
+ dco.checkout();
+ }
// update the HEAD
RefUpdate refUpdate = db.updateRef(Constants.HEAD, true);
refUpdate.setNewObjectId(commit);
}
private String readFile(String path, RevCommit commit) throws IOException {
- TreeWalk walk = TreeWalk.forPath(db, path, commit.getTree());
- ObjectLoader loader = db.open(walk.getObjectId(0), Constants.OBJ_BLOB);
- String result = RawParseUtils.decode(loader.getCachedBytes());
- walk.release();
- return result;
+ try (TreeWalk walk = TreeWalk.forPath(db, path, commit.getTree())) {
+ ObjectLoader loader = db.open(walk.getObjectId(0),
+ Constants.OBJ_BLOB);
+ String result = RawParseUtils.decode(loader.getCachedBytes());
+ return result;
+ }
}
@Test
private List<DiffEntry> diffWorkingAgainstHead(final RevCommit commit,
RevWalk revWalk)
throws IOException {
- TreeWalk walk = createTreeWalk();
RevCommit parentCommit = revWalk.parseCommit(commit.getParent(0));
- try {
+ try (TreeWalk walk = createTreeWalk()) {
walk.addTree(parentCommit.getTree());
walk.addTree(commit.getTree());
return DiffEntry.scan(walk);
- } finally {
- walk.release();
}
}
*/
private boolean inHead(String path) throws IOException {
ObjectId headId = db.resolve(Constants.HEAD);
- RevWalk rw = new RevWalk(db);
- TreeWalk tw = null;
- try {
- tw = TreeWalk.forPath(db, path, rw.parseTree(headId));
+ try (RevWalk rw = new RevWalk(db);
+ TreeWalk tw = TreeWalk.forPath(db, path,
+ rw.parseTree(headId))) {
return tw != null;
- } finally {
- rw.release();
- rw.dispose();
- if (tw != null)
- tw.release();
}
}
assertEquals(parentCount, commit.getParentCount());
// Load parents
- RevWalk walk = new RevWalk(db);
- try {
+ try (RevWalk walk = new RevWalk(db)) {
for (RevCommit parent : commit.getParents())
walk.parseBody(parent);
- } finally {
- walk.release();
}
assertEquals(1, commit.getParent(1).getParentCount());
private List<DiffEntry> diffWorkingAgainstHead(final RevCommit commit)
throws IOException {
- TreeWalk walk = createTreeWalk();
- try {
+ try (TreeWalk walk = createTreeWalk()) {
walk.addTree(commit.getParent(0).getTree());
walk.addTree(commit.getTree());
return DiffEntry.scan(walk);
- } finally {
- walk.release();
}
}
private List<DiffEntry> diffIndexAgainstHead(final RevCommit commit)
throws IOException {
- TreeWalk walk = createTreeWalk();
- try {
+ try (TreeWalk walk = createTreeWalk()) {
walk.addTree(commit.getParent(0).getTree());
walk.addTree(commit.getParent(1).getTree());
return DiffEntry.scan(walk);
- } finally {
- walk.release();
}
}
private List<DiffEntry> diffIndexAgainstWorking(final RevCommit commit)
throws IOException {
- TreeWalk walk = createTreeWalk();
- try {
+ try (TreeWalk walk = createTreeWalk()) {
walk.addTree(commit.getParent(1).getTree());
walk.addTree(commit.getTree());
return DiffEntry.scan(walk);
- } finally {
- walk.release();
}
}
writeTrashFile("file", "content2");
RevCommit stashedWorkTree = Git.wrap(db).stashCreate().call();
validateStashedCommit(stashedWorkTree);
- RevWalk walk = new RevWalk(db);
- RevCommit stashedIndex = stashedWorkTree.getParent(1);
- walk.parseBody(stashedIndex);
- walk.parseBody(stashedIndex.getTree());
- walk.parseBody(stashedIndex.getParent(0));
+ try (RevWalk walk = new RevWalk(db)) {
+ RevCommit stashedIndex = stashedWorkTree.getParent(1);
+ walk.parseBody(stashedIndex);
+ walk.parseBody(stashedIndex.getTree());
+ walk.parseBody(stashedIndex.getParent(0));
+ }
List<DiffEntry> workTreeStashAgainstWorkTree = diffWorkingAgainstHead(stashedWorkTree);
assertEquals(1, workTreeStashAgainstWorkTree.size());
List<DiffEntry> workIndexAgainstWorkTree = diffIndexAgainstHead(stashedWorkTree);
git.add().addFilepattern("file.txt").call();
RevCommit c2 = git.commit().setMessage("create file").call();
- BlameGenerator generator = new BlameGenerator(db, "file.txt");
- try {
+ try (BlameGenerator generator = new BlameGenerator(db, "file.txt")) {
generator.push(null, db.resolve(Constants.HEAD));
assertEquals(3, generator.getResultContents().size());
assertEquals("file.txt", generator.getSourcePath());
assertFalse(generator.next());
- } finally {
- generator.release();
}
}
git.add().addFilepattern(FILENAME_2).call();
RevCommit c2 = git.commit().setMessage("change file2").call();
- BlameGenerator generator = new BlameGenerator(db, FILENAME_2);
- try {
+ try (BlameGenerator generator = new BlameGenerator(db, FILENAME_2)) {
generator.push(null, db.resolve(Constants.HEAD));
assertEquals(3, generator.getResultContents().size());
assertEquals(FILENAME_1, generator.getSourcePath());
assertFalse(generator.next());
- } finally {
- generator.release();
}
// and test again with other BlameGenerator API:
- generator = new BlameGenerator(db, FILENAME_2);
- try {
+ try (BlameGenerator generator = new BlameGenerator(db, FILENAME_2)) {
generator.push(null, db.resolve(Constants.HEAD));
BlameResult result = generator.computeBlameResult();
assertEquals(c1, result.getSourceCommit(2));
assertEquals(FILENAME_1, result.getSourcePath(2));
-
- } finally {
- generator.release();
}
}
git.add().addFilepattern("file.txt").call();
RevCommit c3 = git.commit().setMessage("create file").call();
- BlameGenerator generator = new BlameGenerator(db, "file.txt");
- try {
+ try (BlameGenerator generator = new BlameGenerator(db, "file.txt")) {
generator.push(null, db.resolve(Constants.HEAD));
assertEquals(3, generator.getResultContents().size());
assertEquals(3, generator.getResultEnd());
assertFalse(generator.next());
- } finally {
- generator.release();
}
}
@Override
@After
public void tearDown() throws Exception {
- if (df != null)
- df.release();
+ if (df != null) {
+ df.close();
+ }
super.tearDown();
}
@Test
public void testInserterDiscardsPack() throws IOException {
- ObjectInserter ins = db.newObjectInserter();
- ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
- ins.insert(Constants.OBJ_BLOB, Constants.encode("bar"));
- assertEquals(0, db.getObjectDatabase().listPacks().size());
-
- ins.release();
+ try (ObjectInserter ins = db.newObjectInserter()) {
+ ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
+ ins.insert(Constants.OBJ_BLOB, Constants.encode("bar"));
+ assertEquals(0, db.getObjectDatabase().listPacks().size());
+ }
assertEquals(0, db.getObjectDatabase().listPacks().size());
}
@After
public void tearDown() throws Exception {
- if (reader != null)
- reader.release();
+ if (reader != null) {
+ reader.close();
+ }
}
@Test
// within the pack has been modified.
//
final RevObject o2 = writeBlob(eden, "o2");
- final PackWriter pw = new PackWriter(eden);
- pw.addObject(o2);
- pw.addObject(o1);
- write(out1, pw);
- pw.release();
+ try (PackWriter pw = new PackWriter(eden)) {
+ pw.addObject(o2);
+ pw.addObject(o1);
+ write(out1, pw);
+ }
// Try the old name, then the new name. The old name should cause the
// pack to reload when it opens and the index and pack mismatch.
private File[] pack(final Repository src, final RevObject... list)
throws IOException {
- final PackWriter pw = new PackWriter(src);
- for (final RevObject o : list) {
- pw.addObject(o);
- }
+ try (PackWriter pw = new PackWriter(src)) {
+ for (final RevObject o : list) {
+ pw.addObject(o);
+ }
- final ObjectId name = pw.computeName();
- final File packFile = fullPackFileName(name, ".pack");
- final File idxFile = fullPackFileName(name, ".idx");
- final File[] files = new File[] { packFile, idxFile };
- write(files, pw);
- pw.release();
- return files;
+ final ObjectId name = pw.computeName();
+ final File packFile = fullPackFileName(name, ".pack");
+ final File idxFile = fullPackFileName(name, ".idx");
+ final File[] files = new File[] { packFile, idxFile };
+ write(files, pw);
+ return files;
+ }
}
private static void write(final File[] files, final PackWriter pw)
throws IOException {
final RevWalk revWalk = new RevWalk(repo);
final byte[] bytes = Constants.encode(data);
- final ObjectInserter inserter = repo.newObjectInserter();
final ObjectId id;
- try {
+ try (ObjectInserter inserter = repo.newObjectInserter()) {
id = inserter.insert(Constants.OBJ_BLOB, bytes);
inserter.flush();
- } finally {
- inserter.release();
}
try {
parse(id);
@After
public void release() {
- if (inserter != null)
- inserter.release();
+ if (inserter != null) {
+ inserter.close();
+ }
}
private PackParser index(byte[] raw) throws IOException {
@After
public void tearDown() throws Exception {
if (writer != null) {
- writer.release();
+ writer.close();
writer = null;
}
if (inserter != null) {
- inserter.release();
+ inserter.close();
inserter = null;
}
super.tearDown();
private static PackIndex writePack(FileRepository repo,
Set<? extends ObjectId> want, Set<ObjectIdSet> excludeObjects)
throws IOException {
- PackWriter pw = new PackWriter(repo);
- pw.setDeltaBaseAsOffset(true);
- pw.setReuseDeltaCommits(false);
- for (ObjectIdSet idx : excludeObjects)
- pw.excludeObjects(idx);
- pw.preparePack(NullProgressMonitor.INSTANCE, want,
- Collections.<ObjectId> emptySet());
- String id = pw.computeName().getName();
- File packdir = new File(repo.getObjectsDirectory(), "pack");
- File packFile = new File(packdir, "pack-" + id + ".pack");
- FileOutputStream packOS = new FileOutputStream(packFile);
- pw.writePack(NullProgressMonitor.INSTANCE,
- NullProgressMonitor.INSTANCE, packOS);
- packOS.close();
- File idxFile = new File(packdir, "pack-" + id + ".idx");
- FileOutputStream idxOS = new FileOutputStream(idxFile);
- pw.writeIndex(idxOS);
- idxOS.close();
- pw.release();
- return PackIndex.open(idxFile);
+ try (PackWriter pw = new PackWriter(repo)) {
+ pw.setDeltaBaseAsOffset(true);
+ pw.setReuseDeltaCommits(false);
+ for (ObjectIdSet idx : excludeObjects)
+ pw.excludeObjects(idx);
+ pw.preparePack(NullProgressMonitor.INSTANCE, want,
+ Collections.<ObjectId> emptySet());
+ String id = pw.computeName().getName();
+ File packdir = new File(repo.getObjectsDirectory(), "pack");
+ File packFile = new File(packdir, "pack-" + id + ".pack");
+ FileOutputStream packOS = new FileOutputStream(packFile);
+ pw.writePack(NullProgressMonitor.INSTANCE,
+ NullProgressMonitor.INSTANCE, packOS);
+ packOS.close();
+ File idxFile = new File(packdir, "pack-" + id + ".idx");
+ FileOutputStream idxOS = new FileOutputStream(idxFile);
+ pw.writeIndex(idxOS);
+ idxOS.close();
+ return PackIndex.open(idxFile);
+ }
}
// TODO: testWritePackDeltasCycle()
writer.setIgnoreMissingUninteresting(ignoreMissingUninteresting);
writer.preparePack(m, interestings, uninterestings);
writer.writePack(m, m, os);
- writer.release();
+ writer.close();
verifyOpenPack(thin);
}
writer.preparePack(objectSource.iterator());
assertEquals(objectSource.size(), writer.getObjectCount());
writer.writePack(m, m, os);
- writer.release();
+ writer.close();
verifyOpenPack(false);
}
// object (as it already exists in the pack).
//
final Repository newdb = createBareRepository();
- final ObjectInserter oi = newdb.newObjectInserter();
- final ObjectId treeId = oi.insert(new TreeFormatter());
- oi.release();
+ try (final ObjectInserter oi = newdb.newObjectInserter()) {
+ final ObjectId treeId = oi.insert(new TreeFormatter());
+ assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904",
+ treeId.name());
+ }
- assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904", treeId.name());
final File o = new File(new File(new File(newdb.getDirectory(),
"objects"), "4b"), "825dc642cb6eb9a060e54bf8d69288fbee4904");
assertTrue("Exists " + o, o.isFile());
@Test
public void test026_CreateCommitMultipleparents() throws IOException {
final ObjectId treeId;
- final ObjectInserter oi = db.newObjectInserter();
- try {
+ try (final ObjectInserter oi = db.newObjectInserter()) {
final ObjectId blobId = oi.insert(Constants.OBJ_BLOB,
"and this is the data in me\n".getBytes(Constants.CHARSET
.name()));
fmt.append("i-am-a-file", FileMode.REGULAR_FILE, blobId);
treeId = oi.insert(fmt);
oi.flush();
- } finally {
- oi.release();
}
assertEquals(ObjectId
.fromString("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"), treeId);
private ObjectId insertEmptyBlob() throws IOException {
final ObjectId emptyId;
- ObjectInserter oi = db.newObjectInserter();
- try {
+ try (ObjectInserter oi = db.newObjectInserter()) {
emptyId = oi.insert(Constants.OBJ_BLOB, new byte[] {});
oi.flush();
- } finally {
- oi.release();
}
return emptyId;
}
private ObjectId insertTree(Tree tree) throws IOException {
- ObjectInserter oi = db.newObjectInserter();
- try {
+ try (ObjectInserter oi = db.newObjectInserter()) {
ObjectId id = oi.insert(Constants.OBJ_TREE, tree.format());
oi.flush();
return id;
- } finally {
- oi.release();
}
}
private ObjectId insertTree(TreeFormatter tree) throws IOException {
- ObjectInserter oi = db.newObjectInserter();
- try {
+ try (ObjectInserter oi = db.newObjectInserter()) {
ObjectId id = oi.insert(tree);
oi.flush();
return id;
- } finally {
- oi.release();
}
}
private ObjectId insertCommit(final CommitBuilder builder)
throws IOException, UnsupportedEncodingException {
- ObjectInserter oi = db.newObjectInserter();
- try {
+ try (ObjectInserter oi = db.newObjectInserter()) {
ObjectId id = oi.insert(builder);
oi.flush();
return id;
- } finally {
- oi.release();
}
}
private RevCommit parseCommit(AnyObjectId id)
throws MissingObjectException, IncorrectObjectTypeException,
IOException {
- RevWalk rw = new RevWalk(db);
- try {
+ try (RevWalk rw = new RevWalk(db)) {
return rw.parseCommit(id);
- } finally {
- rw.release();
}
}
private ObjectId insertTag(final TagBuilder tag) throws IOException,
UnsupportedEncodingException {
- ObjectInserter oi = db.newObjectInserter();
- try {
+ try (ObjectInserter oi = db.newObjectInserter()) {
ObjectId id = oi.insert(tag);
oi.flush();
return id;
- } finally {
- oi.release();
}
}
private RevTag parseTag(AnyObjectId id) throws MissingObjectException,
IncorrectObjectTypeException, IOException {
- RevWalk rw = new RevWalk(db);
- try {
+ try (RevWalk rw = new RevWalk(db)) {
return rw.parseTag(id);
- } finally {
- rw.release();
}
}
}
ObjectId genSha1(String data) {
- ObjectInserter w = db.newObjectInserter();
- try {
+ try (ObjectInserter w = db.newObjectInserter()) {
ObjectId id = w.insert(Constants.OBJ_BLOB, data.getBytes());
w.flush();
return id;
} catch (IOException e) {
fail(e.toString());
- } finally {
- w.release();
}
return null;
}
}
private ObjectId insertTree(Tree tree) throws IOException {
- ObjectInserter oi = db.newObjectInserter();
- try {
+ try (ObjectInserter oi = db.newObjectInserter()) {
ObjectId id = oi.insert(Constants.OBJ_TREE, tree.format());
oi.flush();
return id;
- } finally {
- oi.release();
}
}
commit.setAuthor(author);
commit.setCommitter(committer);
commit.setMessage(commitMsg);
- ObjectInserter inserter = db.newObjectInserter();
ObjectId id;
- try {
+ try (ObjectInserter inserter = db.newObjectInserter()) {
commit.setTreeId(inserter.insert(new TreeFormatter()));
id = inserter.insert(commit);
inserter.flush();
- } finally {
- inserter.release();
}
int nl = commitMsg.indexOf('\n');
@Override
@After
public void tearDown() throws Exception {
- reader.release();
- inserter.release();
+ reader.close();
+ inserter.close();
super.tearDown();
}
@Override
@After
public void tearDown() throws Exception {
- reader.release();
- inserter.release();
+ reader.close();
+ inserter.close();
super.tearDown();
}
@Override
@After
public void tearDown() throws Exception {
- reader.release();
- inserter.release();
+ reader.close();
+ inserter.close();
super.tearDown();
}
Tree A_A = A.addTree("A");
Tree A_B = A.addTree("B");
- final ObjectInserter inserter = db.newObjectInserter();
- try {
+ try (final ObjectInserter inserter = db.newObjectInserter()) {
A_A.setId(inserter.insert(Constants.OBJ_TREE, A_A.format()));
A_B.setId(inserter.insert(Constants.OBJ_TREE, A_B.format()));
A.setId(inserter.insert(Constants.OBJ_TREE, A.format()));
root.setId(inserter.insert(Constants.OBJ_TREE, root.format()));
inserter.flush();
- } finally {
- inserter.release();
}
tree_root = rw.parseTree(root.getId());
RevCommit a = commit();
Ref branchA = branch("a", a);
- RevWalk walk = new RevWalk(db);
- try {
+ try (RevWalk walk = new RevWalk(db)) {
RevCommit parsedCommit = walk.parseCommit(a.getId());
assertContains(parsedCommit, asList(branchA));
- } finally {
- walk.release();
}
}
@After
public void release() {
- if (inserter != null)
- inserter.release();
+ if (inserter != null) {
+ inserter.close();
+ }
}
private PackParser index(InputStream in) throws IOException {
@After
public void release() {
- if (inserter != null)
- inserter.release();
+ if (inserter != null) {
+ inserter.close();
+ }
}
private void openPack(TemporaryBuffer.Heap buf) throws IOException {
DirCacheEditor dce = dc.editor();
final String UNNORMALIZED = "target/";
final byte[] UNNORMALIZED_BYTES = Constants.encode(UNNORMALIZED);
- ObjectInserter oi = db.newObjectInserter();
- final ObjectId linkid = oi.insert(Constants.OBJ_BLOB,
- UNNORMALIZED_BYTES, 0,
- UNNORMALIZED_BYTES.length);
- oi.release();
- dce.add(new DirCacheEditor.PathEdit("link") {
- @Override
- public void apply(DirCacheEntry ent) {
- ent.setFileMode(FileMode.SYMLINK);
- ent.setObjectId(linkid);
- ent.setLength(UNNORMALIZED_BYTES.length);
- }
- });
- assertTrue(dce.commit());
+ try (ObjectInserter oi = db.newObjectInserter()) {
+ final ObjectId linkid = oi.insert(Constants.OBJ_BLOB,
+ UNNORMALIZED_BYTES, 0, UNNORMALIZED_BYTES.length);
+ dce.add(new DirCacheEditor.PathEdit("link") {
+ @Override
+ public void apply(DirCacheEntry ent) {
+ ent.setFileMode(FileMode.SYMLINK);
+ ent.setObjectId(linkid);
+ ent.setLength(UNNORMALIZED_BYTES.length);
+ }
+ });
+ assertTrue(dce.commit());
+ }
new Git(db).commit().setMessage("Adding link").call();
new Git(db).reset().setMode(ResetType.HARD).call();
DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
DirCacheEditor dce = dc.editor();
final String NORMALIZED = "target";
final byte[] NORMALIZED_BYTES = Constants.encode(NORMALIZED);
- ObjectInserter oi = db.newObjectInserter();
- final ObjectId linkid = oi.insert(Constants.OBJ_BLOB, NORMALIZED_BYTES,
- 0, NORMALIZED_BYTES.length);
- oi.release();
- dce.add(new DirCacheEditor.PathEdit("link") {
- @Override
- public void apply(DirCacheEntry ent) {
- ent.setFileMode(FileMode.SYMLINK);
- ent.setObjectId(linkid);
- ent.setLength(NORMALIZED_BYTES.length);
- }
- });
- assertTrue(dce.commit());
+ try (ObjectInserter oi = db.newObjectInserter()) {
+ final ObjectId linkid = oi.insert(Constants.OBJ_BLOB,
+ NORMALIZED_BYTES, 0, NORMALIZED_BYTES.length);
+ dce.add(new DirCacheEditor.PathEdit("link") {
+ @Override
+ public void apply(DirCacheEntry ent) {
+ ent.setFileMode(FileMode.SYMLINK);
+ ent.setObjectId(linkid);
+ ent.setLength(NORMALIZED_BYTES.length);
+ }
+ });
+ assertTrue(dce.commit());
+ }
new Git(db).commit().setMessage("Adding link").call();
new Git(db).reset().setMode(ResetType.HARD).call();
DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
public void testSymlinkActuallyModified() throws Exception {
final String NORMALIZED = "target";
final byte[] NORMALIZED_BYTES = Constants.encode(NORMALIZED);
- ObjectInserter oi = db.newObjectInserter();
- final ObjectId linkid = oi.insert(Constants.OBJ_BLOB, NORMALIZED_BYTES,
- 0, NORMALIZED_BYTES.length);
- oi.release();
- DirCache dc = db.lockDirCache();
- DirCacheEditor dce = dc.editor();
- dce.add(new DirCacheEditor.PathEdit("link") {
- @Override
- public void apply(DirCacheEntry ent) {
- ent.setFileMode(FileMode.SYMLINK);
- ent.setObjectId(linkid);
- ent.setLength(NORMALIZED_BYTES.length);
- }
- });
- assertTrue(dce.commit());
+ try (ObjectInserter oi = db.newObjectInserter()) {
+ final ObjectId linkid = oi.insert(Constants.OBJ_BLOB,
+ NORMALIZED_BYTES, 0, NORMALIZED_BYTES.length);
+ DirCache dc = db.lockDirCache();
+ DirCacheEditor dce = dc.editor();
+ dce.add(new DirCacheEditor.PathEdit("link") {
+ @Override
+ public void apply(DirCacheEntry ent) {
+ ent.setFileMode(FileMode.SYMLINK);
+ ent.setObjectId(linkid);
+ ent.setLength(NORMALIZED_BYTES.length);
+ }
+ });
+ assertTrue(dce.commit());
+ }
new Git(db).commit().setMessage("Adding link").call();
new Git(db).reset().setMode(ResetType.HARD).call();
ObjectId fromRaw = ObjectId.fromRaw(fti.idBuffer(), fti.idOffset());
assertEquals("6b584e8ece562ebffc15d38808cd6b98fc3d97ea",
fromRaw.getName());
- ObjectReader objectReader = db.newObjectReader();
- assertFalse(fti.isModified(dce, false, objectReader));
- objectReader.release();
+ try (ObjectReader objectReader = db.newObjectReader()) {
+ assertFalse(fti.isModified(dce, false, objectReader));
+ }
}
@Test
// Modify previously committed DirCacheEntry and write it back to disk
DirCacheEntry dce = db.readDirCache().getEntry("symlink");
dce.setFileMode(FileMode.SYMLINK);
- ObjectReader objectReader = db.newObjectReader();
- DirCacheCheckout.checkoutEntry(db, dce, objectReader);
-
- FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db
- .getConfig().get(WorkingTreeOptions.KEY));
- while (!fti.getEntryPathString().equals("symlink"))
- fti.next(1);
- assertFalse(fti.isModified(dce, false, objectReader));
- objectReader.release();
+ try (ObjectReader objectReader = db.newObjectReader()) {
+ DirCacheCheckout.checkoutEntry(db, dce, objectReader);
+
+ FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(),
+ db.getConfig().get(WorkingTreeOptions.KEY));
+ while (!fti.getEntryPathString().equals("symlink"))
+ fti.next(1);
+ assertFalse(fti.isModified(dce, false, objectReader));
+ }
}
@Test
// If the rounding trick does not work we could skip the compareMetaData
// test and hope that we are usually testing the intended code path.
assertEquals(MetadataDiff.SMUDGED, fti.compareMetadata(dce));
- ObjectReader objectReader = db.newObjectReader();
- assertTrue(fti.isModified(dce, false, objectReader));
- objectReader.release();
+ try (ObjectReader objectReader = db.newObjectReader()) {
+ assertTrue(fti.isModified(dce, false, objectReader));
+ }
}
@Test
public void testFindObjects() throws Exception {
final DirCache tree0 = DirCache.newInCore();
final DirCacheBuilder b0 = tree0.builder();
- ObjectReader or = db.newObjectReader();
- ObjectInserter oi = db.newObjectInserter();
-
- DirCacheEntry aDotB = createEntry("a.b", EXECUTABLE_FILE);
- b0.add(aDotB);
- DirCacheEntry aSlashB = createEntry("a/b", REGULAR_FILE);
- b0.add(aSlashB);
- DirCacheEntry aSlashCSlashD = createEntry("a/c/d", REGULAR_FILE);
- b0.add(aSlashCSlashD);
- DirCacheEntry aZeroB = createEntry("a0b", SYMLINK);
- b0.add(aZeroB);
- b0.finish();
- assertEquals(4, tree0.getEntryCount());
- ObjectId tree = tree0.writeTree(oi);
-
- // Find the directories that were implicitly created above.
- TreeWalk tw = new TreeWalk(or);
- tw.addTree(tree);
- ObjectId a = null;
- ObjectId aSlashC = null;
- while (tw.next()) {
- if (tw.getPathString().equals("a")) {
- a = tw.getObjectId(0);
- tw.enterSubtree();
- while (tw.next()) {
- if (tw.getPathString().equals("a/c")) {
- aSlashC = tw.getObjectId(0);
- break;
+ try (ObjectReader or = db.newObjectReader();
+ ObjectInserter oi = db.newObjectInserter()) {
+
+ DirCacheEntry aDotB = createEntry("a.b", EXECUTABLE_FILE);
+ b0.add(aDotB);
+ DirCacheEntry aSlashB = createEntry("a/b", REGULAR_FILE);
+ b0.add(aSlashB);
+ DirCacheEntry aSlashCSlashD = createEntry("a/c/d", REGULAR_FILE);
+ b0.add(aSlashCSlashD);
+ DirCacheEntry aZeroB = createEntry("a0b", SYMLINK);
+ b0.add(aZeroB);
+ b0.finish();
+ assertEquals(4, tree0.getEntryCount());
+ ObjectId tree = tree0.writeTree(oi);
+
+ // Find the directories that were implicitly created above.
+ TreeWalk tw = new TreeWalk(or);
+ tw.addTree(tree);
+ ObjectId a = null;
+ ObjectId aSlashC = null;
+ while (tw.next()) {
+ if (tw.getPathString().equals("a")) {
+ a = tw.getObjectId(0);
+ tw.enterSubtree();
+ while (tw.next()) {
+ if (tw.getPathString().equals("a/c")) {
+ aSlashC = tw.getObjectId(0);
+ break;
+ }
}
+ break;
}
- break;
}
- }
-
- assertEquals(a, TreeWalk.forPath(or, "a", tree).getObjectId(0));
- assertEquals(a, TreeWalk.forPath(or, "a/", tree).getObjectId(0));
- assertEquals(null, TreeWalk.forPath(or, "/a", tree));
- assertEquals(null, TreeWalk.forPath(or, "/a/", tree));
-
- assertEquals(aDotB.getObjectId(), TreeWalk.forPath(or, "a.b", tree)
- .getObjectId(0));
- assertEquals(null, TreeWalk.forPath(or, "/a.b", tree));
- assertEquals(null, TreeWalk.forPath(or, "/a.b/", tree));
- assertEquals(aDotB.getObjectId(), TreeWalk.forPath(or, "a.b/", tree)
- .getObjectId(0));
-
- assertEquals(aZeroB.getObjectId(), TreeWalk.forPath(or, "a0b", tree)
- .getObjectId(0));
- assertEquals(aSlashB.getObjectId(), TreeWalk.forPath(or, "a/b", tree)
- .getObjectId(0));
- assertEquals(aSlashB.getObjectId(), TreeWalk.forPath(or, "b", a)
- .getObjectId(0));
-
- assertEquals(aSlashC, TreeWalk.forPath(or, "a/c", tree).getObjectId(0));
- assertEquals(aSlashC, TreeWalk.forPath(or, "c", a).getObjectId(0));
-
- assertEquals(aSlashCSlashD.getObjectId(),
- TreeWalk.forPath(or, "a/c/d", tree).getObjectId(0));
- assertEquals(aSlashCSlashD.getObjectId(), TreeWalk
- .forPath(or, "c/d", a).getObjectId(0));
-
- or.release();
- oi.release();
+ assertEquals(a, TreeWalk.forPath(or, "a", tree).getObjectId(0));
+ assertEquals(a, TreeWalk.forPath(or, "a/", tree).getObjectId(0));
+ assertEquals(null, TreeWalk.forPath(or, "/a", tree));
+ assertEquals(null, TreeWalk.forPath(or, "/a/", tree));
+
+ assertEquals(aDotB.getObjectId(),
+ TreeWalk.forPath(or, "a.b", tree).getObjectId(0));
+ assertEquals(null, TreeWalk.forPath(or, "/a.b", tree));
+ assertEquals(null, TreeWalk.forPath(or, "/a.b/", tree));
+ assertEquals(aDotB.getObjectId(),
+ TreeWalk.forPath(or, "a.b/", tree).getObjectId(0));
+
+ assertEquals(aZeroB.getObjectId(),
+ TreeWalk.forPath(or, "a0b", tree).getObjectId(0));
+
+ assertEquals(aSlashB.getObjectId(),
+ TreeWalk.forPath(or, "a/b", tree).getObjectId(0));
+ assertEquals(aSlashB.getObjectId(),
+ TreeWalk.forPath(or, "b", a).getObjectId(0));
+
+ assertEquals(aSlashC,
+ TreeWalk.forPath(or, "a/c", tree).getObjectId(0));
+ assertEquals(aSlashC, TreeWalk.forPath(or, "c", a).getObjectId(0));
+
+ assertEquals(aSlashCSlashD.getObjectId(),
+ TreeWalk.forPath(or, "a/c/d", tree).getObjectId(0));
+ assertEquals(aSlashCSlashD.getObjectId(),
+ TreeWalk.forPath(or, "c/d", a).getObjectId(0));
+ }
}
}
@Test
public void testMissingSubtree_DetectFileAdded_FileModified()
throws Exception {
- final ObjectInserter inserter = db.newObjectInserter();
- final ObjectId aFileId = inserter.insert(OBJ_BLOB, encode("a"));
- final ObjectId bFileId = inserter.insert(OBJ_BLOB, encode("b"));
- final ObjectId cFileId1 = inserter.insert(OBJ_BLOB, encode("c-1"));
- final ObjectId cFileId2 = inserter.insert(OBJ_BLOB, encode("c-2"));
+ final ObjectId oldTree, newTree, bFileId, cFileId1, cFileId2;
+ try (ObjectInserter inserter = db.newObjectInserter()) {
+ final ObjectId aFileId = inserter.insert(OBJ_BLOB, encode("a"));
+ bFileId = inserter.insert(OBJ_BLOB, encode("b"));
+ cFileId1 = inserter.insert(OBJ_BLOB, encode("c-1"));
+ cFileId2 = inserter.insert(OBJ_BLOB, encode("c-2"));
- // Create sub-a/empty, sub-c/empty = hello.
- final ObjectId oldTree;
- {
- final Tree root = new Tree(db);
+ // Create sub-a/empty, sub-c/empty = hello.
{
- final Tree subA = root.addTree("sub-a");
- subA.addFile("empty").setId(aFileId);
- subA.setId(inserter.insert(OBJ_TREE, subA.format()));
+ final Tree root = new Tree(db);
+ {
+ final Tree subA = root.addTree("sub-a");
+ subA.addFile("empty").setId(aFileId);
+ subA.setId(inserter.insert(OBJ_TREE, subA.format()));
+ }
+ {
+ final Tree subC = root.addTree("sub-c");
+ subC.addFile("empty").setId(cFileId1);
+ subC.setId(inserter.insert(OBJ_TREE, subC.format()));
+ }
+ oldTree = inserter.insert(OBJ_TREE, root.format());
}
- {
- final Tree subC = root.addTree("sub-c");
- subC.addFile("empty").setId(cFileId1);
- subC.setId(inserter.insert(OBJ_TREE, subC.format()));
- }
- oldTree = inserter.insert(OBJ_TREE, root.format());
- }
- // Create sub-a/empty, sub-b/empty, sub-c/empty.
- final ObjectId newTree;
- {
- final Tree root = new Tree(db);
- {
- final Tree subA = root.addTree("sub-a");
- subA.addFile("empty").setId(aFileId);
- subA.setId(inserter.insert(OBJ_TREE, subA.format()));
- }
- {
- final Tree subB = root.addTree("sub-b");
- subB.addFile("empty").setId(bFileId);
- subB.setId(inserter.insert(OBJ_TREE, subB.format()));
- }
+ // Create sub-a/empty, sub-b/empty, sub-c/empty.
{
- final Tree subC = root.addTree("sub-c");
- subC.addFile("empty").setId(cFileId2);
- subC.setId(inserter.insert(OBJ_TREE, subC.format()));
+ final Tree root = new Tree(db);
+ {
+ final Tree subA = root.addTree("sub-a");
+ subA.addFile("empty").setId(aFileId);
+ subA.setId(inserter.insert(OBJ_TREE, subA.format()));
+ }
+ {
+ final Tree subB = root.addTree("sub-b");
+ subB.addFile("empty").setId(bFileId);
+ subB.setId(inserter.insert(OBJ_TREE, subB.format()));
+ }
+ {
+ final Tree subC = root.addTree("sub-c");
+ subC.addFile("empty").setId(cFileId2);
+ subC.setId(inserter.insert(OBJ_TREE, subC.format()));
+ }
+ newTree = inserter.insert(OBJ_TREE, root.format());
}
- newTree = inserter.insert(OBJ_TREE, root.format());
+ inserter.flush();
}
- inserter.flush();
- inserter.release();
final TreeWalk tw = new TreeWalk(db);
tw.reset(oldTree, newTree);
return queue != null ? queue.sourceText : null;
}
- /**
- * Release the current blame session. Use {@link #close()} instead.
- */
- @Deprecated
- public void release() {
- close();
- }
-
/**
* Release the current blame session.
*
out.flush();
}
- /**
- * Release the internal ObjectReader state. Use {@link #close()} instead.
- */
- @Deprecated
- public void release() {
- close();
- }
-
/**
* Release the internal ObjectReader state.
*
}
@Override
- public void release() {
+ public void close() {
if (packOut != null) {
try {
packOut.close();
}
@Override
- public void release() {
+ public void close() {
ctx.close();
}
}
/** Release the current window cursor. */
@Override
- public void release() {
+ public void close() {
last = null;
block = null;
baseCache = null;
}
@Override
- public void release() {
+ public void close() {
if (deflate != null) {
try {
deflate.end();
return state.snapshot();
}
- /**
- * Release all resources used by this writer. Use {@link #close()} instead.
- */
- @Deprecated
- public void release() {
- close();
- }
-
/**
* Release all resources used by this writer.
*
* <p>
* Objects written by an inserter may not be immediately visible for reading
* after the insert method completes. Callers must invoke either
- * {@link #release()} or {@link #flush()} prior to updating references or
+ * {@link #close()} or {@link #flush()} prior to updating references or
* otherwise making the returned ObjectIds visible to other code.
*/
public abstract class ObjectInserter implements AutoCloseable {
}
@Override
- public void release() {
+ public void close() {
// Do nothing.
}
}
delegate().flush();
}
- public void release() {
- delegate().release();
+ public void close() {
+ delegate().close();
}
}
*/
public abstract void flush() throws IOException;
- /**
- * Release any resources used by this inserter.
- * <p>
- * An inserter that has been released can be used again, but may need to be
- * released after the subsequent usage. Use {@link #close()} instead
- */
- @Deprecated
- public abstract void release();
-
/**
* Release any resources used by this inserter.
* <p>
* @since 4.0
*/
@Override
- public void close() {
- release();
- }
+ public abstract void close();
}
return null;
}
- /**
- * Release any resources used by this reader.
- * <p>
- * A reader that has been released can be used again, but may need to be
- * released after the subsequent usage. Use {@link #close()} instead.
- */
- @Deprecated
- public void release() {
- // Do nothing.
- }
-
/**
* Release any resources used by this reader.
* <p>
*/
@Override
public void close() {
- release();
+ // Do nothing.
}
}
return reader;
}
- /**
- * Release any resources used by this walker's reader.
- * <p>
- * A walker that has been released can be used again, but may need to be
- * released after the subsequent usage. Use {@link #close()} instead.
- */
- @Deprecated
- public void release() {
- close();
- }
-
/**
* Release any resources used by this walker's reader.
* <p>
return url != null ? getSubmoduleRemoteUrl(repository, url) : null;
}
- /**
- * Release any resources used by this walker's reader. Use {@link #close()}
- * instead.
- */
- @Deprecated
- public void release() {
- close();
- }
-
/**
* Release any resources used by this walker's reader.
*
return reader;
}
- /**
- * Release any resources used by this walker's reader.
- * <p>
- * A walker that has been released can be used again, but may need to be
- * released after the subsequent usage. Use {@link #close()} instead.
- */
- @Deprecated
- public void release() {
- close();
- }
-
/**
* Release any resources used by this walker's reader.
* <p>