Browse Source

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
stable-4.11
David Pursehouse 7 years ago committed by Gerrit Code Review @ Eclipse.org
parent
commit
e6c456eeae
  1. 5
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/DiffAlgorithms.java
  2. 6
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java
  3. 7
      org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java
  4. 19
      org.eclipse.jgit/src/org/eclipse/jgit/api/DiffCommand.java
  5. 7
      org.eclipse.jgit/src/org/eclipse/jgit/notes/DefaultNoteMerger.java

5
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();
}
}
}

6
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)

7
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();
}
}
}

19
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();
}
}

7
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();
}
}
}

Loading…
Cancel
Save