diff options
author | David Pursehouse <david.pursehouse@sonymobile.com> | 2015-10-01 17:38:53 +0900 |
---|---|---|
committer | David Pursehouse <david.pursehouse@sonymobile.com> | 2015-10-01 17:44:15 +0900 |
commit | ee66914e410a258d8d8acd877a97f9152e2c1a41 (patch) | |
tree | db08677c8333f822a2b3264c67d62fedd907ba11 /org.eclipse.jgit.pgm | |
parent | 58e290864ef599592e70285637abf6a538400e5d (diff) | |
download | jgit-ee66914e410a258d8d8acd877a97f9152e2c1a41.tar.gz jgit-ee66914e410a258d8d8acd877a97f9152e2c1a41.zip |
pgm: Open RevWalk and TreeWalk in try-with-resource
To prevent potential resource leaks.
Change-Id: I2039af04d9fb75405f8e13abf508623b7d4ef324
Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
Diffstat (limited to 'org.eclipse.jgit.pgm')
8 files changed, 87 insertions, 79 deletions
diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/DiffTree.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/DiffTree.java index d89053c4cb..32adf6df0c 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/DiffTree.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/DiffTree.java @@ -74,46 +74,47 @@ class DiffTree extends TextBuiltin { @Override protected void run() throws Exception { - final TreeWalk walk = new TreeWalk(db); - walk.setRecursive(recursive); - for (final AbstractTreeIterator i : trees) - walk.addTree(i); - walk.setFilter(AndTreeFilter.create(TreeFilter.ANY_DIFF, pathFilter)); + try (final TreeWalk walk = new TreeWalk(db)) { + walk.setRecursive(recursive); + for (final AbstractTreeIterator i : trees) + walk.addTree(i); + walk.setFilter(AndTreeFilter.create(TreeFilter.ANY_DIFF, pathFilter)); - final int nTree = walk.getTreeCount(); - while (walk.next()) { - for (int i = 1; i < nTree; i++) - outw.print(':'); - for (int i = 0; i < nTree; i++) { - final FileMode m = walk.getFileMode(i); - final String s = m.toString(); - for (int pad = 6 - s.length(); pad > 0; pad--) - outw.print('0'); - outw.print(s); - outw.print(' '); - } + final int nTree = walk.getTreeCount(); + while (walk.next()) { + for (int i = 1; i < nTree; i++) + outw.print(':'); + for (int i = 0; i < nTree; i++) { + final FileMode m = walk.getFileMode(i); + final String s = m.toString(); + for (int pad = 6 - s.length(); pad > 0; pad--) + outw.print('0'); + outw.print(s); + outw.print(' '); + } - for (int i = 0; i < nTree; i++) { - outw.print(walk.getObjectId(i).name()); - outw.print(' '); - } + for (int i = 0; i < nTree; i++) { + outw.print(walk.getObjectId(i).name()); + outw.print(' '); + } - char chg = 'M'; - if (nTree == 2) { - final int m0 = walk.getRawMode(0); - final int m1 = walk.getRawMode(1); - if (m0 == 0 && m1 != 0) - chg = 'A'; - else if (m0 != 0 && m1 == 0) - chg = 'D'; - else if (m0 != m1 && walk.idEqual(0, 1)) - chg = 'T'; - } - outw.print(chg); + char chg = 'M'; + if (nTree == 2) { + final int m0 = walk.getRawMode(0); + final int m1 = walk.getRawMode(1); + if (m0 == 0 && m1 != 0) + chg = 'A'; + else if (m0 != 0 && m1 == 0) + chg = 'D'; + else if (m0 != m1 && walk.idEqual(0, 1)) + chg = 'T'; + } + outw.print(chg); - outw.print('\t'); - outw.print(walk.getPathString()); - outw.println(); + outw.print('\t'); + outw.print(walk.getPathString()); + outw.println(); + } } } } diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/LsTree.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/LsTree.java index 4b16ed8800..872ea67774 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/LsTree.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/LsTree.java @@ -72,27 +72,28 @@ class LsTree extends TextBuiltin { @Override protected void run() throws Exception { - final TreeWalk walk = new TreeWalk(db); - walk.reset(); // drop the first empty tree, which we do not need here - if (paths.size() > 0) - walk.setFilter(PathFilterGroup.createFromStrings(paths)); - walk.setRecursive(recursive); - walk.addTree(tree); + try (final TreeWalk walk = new TreeWalk(db)) { + walk.reset(); // drop the first empty tree, which we do not need here + if (paths.size() > 0) + walk.setFilter(PathFilterGroup.createFromStrings(paths)); + walk.setRecursive(recursive); + walk.addTree(tree); - while (walk.next()) { - final FileMode mode = walk.getFileMode(0); - if (mode == FileMode.TREE) - outw.print('0'); - outw.print(mode); - outw.print(' '); - outw.print(Constants.typeString(mode.getObjectType())); + while (walk.next()) { + final FileMode mode = walk.getFileMode(0); + if (mode == FileMode.TREE) + outw.print('0'); + outw.print(mode); + outw.print(' '); + outw.print(Constants.typeString(mode.getObjectType())); - outw.print(' '); - outw.print(walk.getObjectId(0).name()); + outw.print(' '); + outw.print(walk.getObjectId(0).name()); - outw.print('\t'); - outw.print(QuotedString.GIT_PATH.quote(walk.getPathString())); - outw.println(); + outw.print('\t'); + outw.print(QuotedString.GIT_PATH.quote(walk.getPathString())); + outw.println(); + } } } } diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Merge.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Merge.java index fc32d4fcfe..e0ff0583cb 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Merge.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Merge.java @@ -207,12 +207,13 @@ class Merge extends TextBuiltin { private boolean isMergedInto(Ref oldHead, AnyObjectId src) throws IOException { - RevWalk revWalk = new RevWalk(db); - ObjectId oldHeadObjectId = oldHead.getPeeledObjectId(); - if (oldHeadObjectId == null) - oldHeadObjectId = oldHead.getObjectId(); - RevCommit oldHeadCommit = revWalk.lookupCommit(oldHeadObjectId); - RevCommit srcCommit = revWalk.lookupCommit(src); - return revWalk.isMergedInto(oldHeadCommit, srcCommit); + try (RevWalk revWalk = new RevWalk(db)) { + ObjectId oldHeadObjectId = oldHead.getPeeledObjectId(); + if (oldHeadObjectId == null) + oldHeadObjectId = oldHead.getObjectId(); + RevCommit oldHeadCommit = revWalk.lookupCommit(oldHeadObjectId); + RevCommit srcCommit = revWalk.lookupCommit(src); + return revWalk.isMergedInto(oldHeadCommit, srcCommit); + } } } diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Show.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Show.java index b668139b5e..c5986b01aa 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Show.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Show.java @@ -251,16 +251,17 @@ class Show extends TextBuiltin { private void show(RevTree obj) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException { - final TreeWalk walk = new TreeWalk(db); - walk.reset(); - walk.addTree(obj); - - while (walk.next()) { - outw.print(walk.getPathString()); - final FileMode mode = walk.getFileMode(0); - if (mode == FileMode.TREE) - outw.print("/"); //$NON-NLS-1$ - outw.println(); + try (final TreeWalk walk = new TreeWalk(db)) { + walk.reset(); + walk.addTree(obj); + + while (walk.next()) { + outw.print(walk.getPathString()); + final FileMode mode = walk.getFileMode(0); + if (mode == FileMode.TREE) + outw.print("/"); //$NON-NLS-1$ + outw.println(); + } } } diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Tag.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Tag.java index 582094d158..45fceb570f 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Tag.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Tag.java @@ -85,8 +85,9 @@ class Tag extends TextBuiltin { .setMessage(message).setName(tagName); if (object != null) { - RevWalk walk = new RevWalk(db); - command.setObjectId(walk.parseAny(object)); + try (RevWalk walk = new RevWalk(db)) { + command.setObjectId(walk.parseAny(object)); + } } try { command.call(); 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 24d717dfd8..df7ebb78b8 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 @@ -173,9 +173,9 @@ class DiffAlgorithms extends TextBuiltin { int maxN = 0; AbbreviatedObjectId startId; - try (ObjectReader or = db.newObjectReader()) { + try (ObjectReader or = db.newObjectReader(); + RevWalk rw = new RevWalk(or)) { final MutableObjectId id = new MutableObjectId(); - RevWalk rw = new RevWalk(or); TreeWalk tw = new TreeWalk(or); tw.setFilter(TreeFilter.ANY_DIFF); tw.setRecursive(true); diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/ShowPackDelta.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/ShowPackDelta.java index 7b5cdbf8f7..d3eb245cd9 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/ShowPackDelta.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/ShowPackDelta.java @@ -75,7 +75,10 @@ class ShowPackDelta extends TextBuiltin { @Override protected void run() throws Exception { ObjectReader reader = db.newObjectReader(); - RevObject obj = new RevWalk(reader).parseAny(objectId); + RevObject obj; + try (RevWalk rw = new RevWalk(reader)) { + obj = rw.parseAny(objectId); + } byte[] delta = getDelta(reader, obj); // We're crossing our fingers that this will be a delta. Double diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/TextHashFunctions.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/TextHashFunctions.java index dcfa8cf00a..dcbc37bed6 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/TextHashFunctions.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/TextHashFunctions.java @@ -300,10 +300,10 @@ class TextHashFunctions extends TextBuiltin { long fileCnt = 0; long lineCnt = 0; - try (ObjectReader or = db.newObjectReader()) { - final MutableObjectId id = new MutableObjectId(); + try (ObjectReader or = db.newObjectReader(); RevWalk rw = new RevWalk(or); - TreeWalk tw = new TreeWalk(or); + TreeWalk tw = new TreeWalk(or)) { + final MutableObjectId id = new MutableObjectId(); tw.reset(rw.parseTree(db.resolve(Constants.HEAD))); tw.setRecursive(true); |