Browse Source

pgm: Create instances of Git in try-with-resource

To prevent potential resource leak.

Change-Id: I8ac4ae61193324849bafb46501a55f93c5029a4e
Signed-off-by: David Pursehouse <david.pursehouse@sonymobile.com>
stable-4.1
David Pursehouse 9 years ago
parent
commit
3096ab6502
  1. 4
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Add.java
  2. 4
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Archive.java
  3. 4
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Branch.java
  4. 4
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Checkout.java
  5. 4
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Commit.java
  6. 5
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Describe.java
  7. 3
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Fetch.java
  8. 5
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Merge.java
  9. 3
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Push.java
  10. 4
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Reflog.java
  11. 4
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Reset.java
  12. 5
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Rm.java
  13. 4
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Status.java
  14. 3
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Tag.java

4
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Add.java

@ -62,10 +62,12 @@ class Add extends TextBuiltin {
@Override
protected void run() throws Exception {
AddCommand addCmd = new Git(db).add();
try (Git git = new Git(db)) {
AddCommand addCmd = git.add();
addCmd.setUpdate(update);
for (String p : filepatterns)
addCmd.addFilepattern(p);
addCmd.call();
}
}
}

4
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Archive.java

@ -87,8 +87,8 @@ class Archive extends TextBuiltin {
else
stream = outs;
try {
ArchiveCommand cmd = new Git(db).archive()
try (Git git = new Git(db)) {
ArchiveCommand cmd = git.archive()
.setTree(tree)
.setFormat(format)
.setPrefix(prefix)

4
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Branch.java

@ -186,7 +186,8 @@ class Branch extends TextBuiltin {
// This can happen if HEAD is stillborn
if (head != null) {
String current = head.getLeaf().getName();
ListBranchCommand command = new Git(db).branchList();
try (Git git = new Git(db)) {
ListBranchCommand command = git.branchList();
if (all)
command.setListMode(ListMode.ALL);
else if (remote)
@ -213,6 +214,7 @@ class Branch extends TextBuiltin {
}
}
}
}
private void addRefs(final Collection<Ref> refs, final String prefix) {
for (final Ref ref : RefComparator.sort(refs)) {

4
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Checkout.java

@ -89,7 +89,8 @@ class Checkout extends TextBuiltin {
throw die(CLIText.get().onBranchToBeBorn);
}
CheckoutCommand command = new Git(db).checkout();
try (Git git = new Git(db)) {
CheckoutCommand command = git.checkout();
if (paths.size() > 0) {
command.setStartPoint(name);
for (String path : paths)
@ -133,3 +134,4 @@ class Checkout extends TextBuiltin {
}
}
}
}

4
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Commit.java

@ -80,7 +80,8 @@ class Commit extends TextBuiltin {
@Override
protected void run() throws NoHeadException, NoMessageException,
ConcurrentRefUpdateException, JGitInternalException, Exception {
CommitCommand commitCmd = new Git(db).commit();
try (Git git = new Git(db)) {
CommitCommand commitCmd = git.commit();
if (author != null)
commitCmd.setAuthor(RawParseUtils.parsePersonIdent(author));
if (message != null)
@ -114,3 +115,4 @@ class Commit extends TextBuiltin {
+ commit.getShortMessage());
}
}
}

5
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Describe.java

@ -61,7 +61,8 @@ class Describe extends TextBuiltin {
@Override
protected void run() throws Exception {
DescribeCommand cmd = new Git(db).describe();
try (Git git = new Git(db)) {
DescribeCommand cmd = git.describe();
if (tree != null)
cmd.setTarget(tree);
cmd.setLong(longDesc);
@ -76,5 +77,5 @@ class Describe extends TextBuiltin {
outw.println(result);
}
}
}

3
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Fetch.java

@ -104,7 +104,7 @@ class Fetch extends AbstractFetchCommand {
@Override
protected void run() throws Exception {
Git git = new Git(db);
try (Git git = new Git(db)) {
FetchCommand fetch = git.fetch();
if (fsck != null)
fetch.setCheckFetchedObjects(fsck.booleanValue());
@ -132,3 +132,4 @@ class Fetch extends AbstractFetchCommand {
showFetchResult(result);
}
}
}

5
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Merge.java

@ -121,7 +121,8 @@ class Merge extends TextBuiltin {
CLIText.get().refDoesNotExistOrNoCommit, ref));
Ref oldHead = db.getRef(Constants.HEAD);
Git git = new Git(db);
MergeResult result;
try (Git git = new Git(db)) {
MergeCommand mergeCmd = git.merge().setStrategy(mergeStrategy)
.setSquash(squash).setFastForward(ff).setCommit(!noCommit);
if (srcRef != null)
@ -132,12 +133,12 @@ class Merge extends TextBuiltin {
if (message != null)
mergeCmd.setMessage(message);
MergeResult result;
try {
result = mergeCmd.call();
} catch (CheckoutConflictException e) {
result = new MergeResult(e.getConflictingPaths()); // CHECKOUT_CONFLICT
}
}
switch (result.getMergeStatus()) {
case ALREADY_UP_TO_DATE:

3
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Push.java

@ -109,7 +109,7 @@ class Push extends TextBuiltin {
@Override
protected void run() throws Exception {
Git git = new Git(db);
try (Git git = new Git(db)) {
PushCommand push = git.push();
push.setDryRun(dryRun);
push.setForce(force);
@ -130,6 +130,7 @@ class Push extends TextBuiltin {
}
}
}
}
private void printPushResult(final ObjectReader reader, final URIish uri,
final PushResult result) throws IOException {

4
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Reflog.java

@ -59,7 +59,8 @@ class Reflog extends TextBuiltin {
@Override
protected void run() throws Exception {
ReflogCommand cmd = new Git(db).reflog();
try (Git git = new Git(db)) {
ReflogCommand cmd = git.reflog();
if (ref != null)
cmd.setRef(ref);
Collection<ReflogEntry> entries = cmd.call();
@ -68,6 +69,7 @@ class Reflog extends TextBuiltin {
outw.println(toString(entry, i++));
}
}
}
private String toString(ReflogEntry entry, int i) {
final StringBuilder s = new StringBuilder();

4
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Reset.java

@ -66,7 +66,8 @@ class Reset extends TextBuiltin {
@Override
protected void run() throws Exception {
ResetCommand command = new Git(db).reset();
try (Git git = new Git(db)) {
ResetCommand command = git.reset();
command.setRef(commit);
ResetType mode = null;
if (soft)
@ -80,6 +81,7 @@ class Reset extends TextBuiltin {
command.setMode(mode);
command.call();
}
}
private static ResetType selectMode(ResetType mode, ResetType want) {
if (mode != null)

5
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Rm.java

@ -63,10 +63,11 @@ class Rm extends TextBuiltin {
@Override
protected void run() throws Exception {
RmCommand command = new Git(db).rm();
try (Git git = new Git(db)) {
RmCommand command = git.rm();
for (String p : paths)
command.addFilepattern(p);
command.call();
}
}
}

4
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Status.java

@ -88,13 +88,15 @@ class Status extends TextBuiltin {
@Override
protected void run() throws Exception {
StatusCommand statusCommand = new Git(db).status();
try (Git git = new Git(db)) {
StatusCommand statusCommand = git.status();
if (filterPaths != null && filterPaths.size() > 0)
for (String path : filterPaths)
statusCommand.addPath(path);
org.eclipse.jgit.api.Status status = statusCommand.call();
printStatus(status);
}
}
private void printStatus(org.eclipse.jgit.api.Status status)
throws IOException {

3
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Tag.java

@ -79,7 +79,7 @@ class Tag extends TextBuiltin {
@Override
protected void run() throws Exception {
Git git = new Git(db);
try (Git git = new Git(db)) {
if (tagName != null) {
TagCommand command = git.tag().setForceUpdate(force)
.setMessage(message).setName(tagName);
@ -103,3 +103,4 @@ class Tag extends TextBuiltin {
}
}
}
}

Loading…
Cancel
Save