diff options
author | David Pursehouse <david.pursehouse@gmail.com> | 2018-03-06 02:55:50 -0500 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org <gerrit@eclipse.org> | 2018-03-06 02:55:50 -0500 |
commit | e6c456eeae1972783974675b3242801f0d8c855c (patch) | |
tree | 7583d697cfeef88b27b6b7c4db82e899234aa58b | |
parent | 6754fd0d95c42b119141929db5230c4a6996f679 (diff) | |
parent | e2d875ee30a14a1094c322b89b183f6021e46bdd (diff) | |
download | jgit-e6c456eeae1972783974675b3242801f0d8c855c.tar.gz jgit-e6c456eeae1972783974675b3242801f0d8c855c.zip |
Merge changes from topic 'try-with-resource'
* changes:
DiffCommand: Open DiffFormatter in try-with-resource
DiffAlgorithms: Open Repository in try-with-resource
DescribeCommandTest: Open FileWriter in try-with-resource
CommitCommand: Open InputStream in try-with-resource
DefaultNoteMerger: Open UnionInputStream in try-with-resource
5 files changed, 17 insertions, 27 deletions
diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/DiffAlgorithms.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/DiffAlgorithms.java index 5f8ebdb1a9..0e1b398a73 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/DiffAlgorithms.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/DiffAlgorithms.java @@ -160,11 +160,8 @@ class DiffAlgorithms extends TextBuiltin { else rb.findGitDir(dir); - Repository repo = rb.build(); - try { + try (Repository repo = rb.build()) { run(repo); - } finally { - repo.close(); } } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java index d78a328402..79da2da7ea 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java @@ -297,9 +297,9 @@ public class DescribeCommandTest extends RepositoryTestCase { } private static void touch(File f, String contents) throws Exception { - FileWriter w = new FileWriter(f); - w.write(contents); - w.close(); + try (FileWriter w = new FileWriter(f)) { + w.write(contents); + } } private String describe(ObjectId c1, boolean longDesc) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java index 8a89ba1611..f2572835fc 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java @@ -409,14 +409,11 @@ public class CommitCommand extends GitCommand<RevCommit> { inserter = repo.newObjectInserter(); long contentLength = fTree .getEntryContentLength(); - InputStream inputStream = fTree - .openEntryStream(); - try { + try (InputStream inputStream = fTree + .openEntryStream()) { dcEntry.setObjectId(inserter.insert( Constants.OBJ_BLOB, contentLength, inputStream)); - } finally { - inputStream.close(); } } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/DiffCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/DiffCommand.java index 4c6f351142..f65b5735de 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/DiffCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/DiffCommand.java @@ -104,6 +104,12 @@ public class DiffCommand extends GitCommand<List<DiffEntry>> { super(repo); } + private DiffFormatter getDiffFormatter() { + return out != null && !showNameAndStatusOnly + ? new DiffFormatter(new BufferedOutputStream(out)) + : new DiffFormatter(NullOutputStream.INSTANCE); + } + /** * {@inheritDoc} * <p> @@ -114,14 +120,9 @@ public class DiffCommand extends GitCommand<List<DiffEntry>> { */ @Override public List<DiffEntry> call() throws GitAPIException { - final DiffFormatter diffFmt; - if (out != null && !showNameAndStatusOnly) - diffFmt = new DiffFormatter(new BufferedOutputStream(out)); - else - diffFmt = new DiffFormatter(NullOutputStream.INSTANCE); - diffFmt.setRepository(repo); - diffFmt.setProgressMonitor(monitor); - try { + try (DiffFormatter diffFmt = getDiffFormatter()) { + diffFmt.setRepository(repo); + diffFmt.setProgressMonitor(monitor); if (cached) { if (oldTree == null) { ObjectId head = repo.resolve(HEAD + "^{tree}"); //$NON-NLS-1$ @@ -159,8 +160,6 @@ public class DiffCommand extends GitCommand<List<DiffEntry>> { } } catch (IOException e) { throw new JGitInternalException(e.getMessage(), e); - } finally { - diffFmt.close(); } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/notes/DefaultNoteMerger.java b/org.eclipse.jgit/src/org/eclipse/jgit/notes/DefaultNoteMerger.java index 87bd4b55b1..54a2d8996a 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/notes/DefaultNoteMerger.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/notes/DefaultNoteMerger.java @@ -82,14 +82,11 @@ public class DefaultNoteMerger implements NoteMerger { ObjectLoader lo = reader.open(ours.getData()); ObjectLoader lt = reader.open(theirs.getData()); - UnionInputStream union = new UnionInputStream(lo.openStream(), - lt.openStream()); - try { + try (UnionInputStream union = new UnionInputStream(lo.openStream(), + lt.openStream())) { ObjectId noteData = inserter.insert(Constants.OBJ_BLOB, lo.getSize() + lt.getSize(), union); return new Note(ours, noteData); - } finally { - union.close(); } } } |