* stable-4.1: pgm: Open RevWalk and TreeWalk in try-with-resource ant: Open Repository and Git in try-with-resource pgm: Create instances of Git in try-with-resource FanoutBucket: Create ObjectInserter.Formatter in try-with-resource Fix compiler warnings in DiffFormatter.writeGitLinkText Change-Id: I448ecc9a1334977d9f304dd61ea20c7a8e692b10 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>tags/v4.2.0.201511101648-m1
@@ -117,10 +117,10 @@ public class GitAddTask extends Task { | |||
} | |||
AddCommand gitAdd; | |||
try { | |||
Repository repo = new FileRepositoryBuilder().readEnvironment() | |||
.findGitDir(src).build(); | |||
gitAdd = new Git(repo).add(); | |||
try (Repository repo = new FileRepositoryBuilder().readEnvironment() | |||
.findGitDir(src).build(); | |||
Git git = new Git(repo);) { | |||
gitAdd = git.add(); | |||
} catch (IOException e) { | |||
throw new BuildException("Could not access repository " + src, e); | |||
} |
@@ -105,10 +105,10 @@ public class GitCheckoutTask extends Task { | |||
@Override | |||
public void execute() throws BuildException { | |||
CheckoutCommand checkout; | |||
try { | |||
Repository repo = new FileRepositoryBuilder().readEnvironment() | |||
.findGitDir(src).build(); | |||
checkout = new Git(repo).checkout(); | |||
try (Repository repo = new FileRepositoryBuilder().readEnvironment() | |||
.findGitDir(src).build(); | |||
Git git = new Git(repo)) { | |||
checkout = git.checkout(); | |||
} catch (IOException e) { | |||
throw new BuildException("Could not access repository " + src, e); | |||
} |
@@ -62,10 +62,12 @@ class Add extends TextBuiltin { | |||
@Override | |||
protected void run() throws Exception { | |||
AddCommand addCmd = new Git(db).add(); | |||
addCmd.setUpdate(update); | |||
for (String p : filepatterns) | |||
addCmd.addFilepattern(p); | |||
addCmd.call(); | |||
try (Git git = new Git(db)) { | |||
AddCommand addCmd = git.add(); | |||
addCmd.setUpdate(update); | |||
for (String p : filepatterns) | |||
addCmd.addFilepattern(p); | |||
addCmd.call(); | |||
} | |||
} | |||
} |
@@ -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) |
@@ -186,29 +186,31 @@ 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(); | |||
if (all) | |||
command.setListMode(ListMode.ALL); | |||
else if (remote) | |||
command.setListMode(ListMode.REMOTE); | |||
try (Git git = new Git(db)) { | |||
ListBranchCommand command = git.branchList(); | |||
if (all) | |||
command.setListMode(ListMode.ALL); | |||
else if (remote) | |||
command.setListMode(ListMode.REMOTE); | |||
if (containsCommitish != null) | |||
command.setContains(containsCommitish); | |||
if (containsCommitish != null) | |||
command.setContains(containsCommitish); | |||
List<Ref> refs = command.call(); | |||
for (Ref ref : refs) { | |||
if (ref.getName().equals(Constants.HEAD)) | |||
addRef("(no branch)", head); //$NON-NLS-1$ | |||
} | |||
List<Ref> refs = command.call(); | |||
for (Ref ref : refs) { | |||
if (ref.getName().equals(Constants.HEAD)) | |||
addRef("(no branch)", head); //$NON-NLS-1$ | |||
} | |||
addRefs(refs, Constants.R_HEADS); | |||
addRefs(refs, Constants.R_REMOTES); | |||
addRefs(refs, Constants.R_HEADS); | |||
addRefs(refs, Constants.R_REMOTES); | |||
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); | |||
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); | |||
} | |||
} | |||
} | |||
} |
@@ -89,47 +89,49 @@ class Checkout extends TextBuiltin { | |||
throw die(CLIText.get().onBranchToBeBorn); | |||
} | |||
CheckoutCommand command = new Git(db).checkout(); | |||
if (paths.size() > 0) { | |||
command.setStartPoint(name); | |||
for (String path : paths) | |||
command.addPath(path); | |||
} else { | |||
command.setCreateBranch(createBranch); | |||
command.setName(name); | |||
command.setForce(force); | |||
command.setOrphan(orphan); | |||
} | |||
try { | |||
String oldBranch = db.getBranch(); | |||
Ref ref = command.call(); | |||
if (ref == null) | |||
return; | |||
if (Repository.shortenRefName(ref.getName()).equals(oldBranch)) { | |||
try (Git git = new Git(db)) { | |||
CheckoutCommand command = git.checkout(); | |||
if (paths.size() > 0) { | |||
command.setStartPoint(name); | |||
for (String path : paths) | |||
command.addPath(path); | |||
} else { | |||
command.setCreateBranch(createBranch); | |||
command.setName(name); | |||
command.setForce(force); | |||
command.setOrphan(orphan); | |||
} | |||
try { | |||
String oldBranch = db.getBranch(); | |||
Ref ref = command.call(); | |||
if (ref == null) | |||
return; | |||
if (Repository.shortenRefName(ref.getName()).equals(oldBranch)) { | |||
outw.println(MessageFormat.format( | |||
CLIText.get().alreadyOnBranch, | |||
name)); | |||
return; | |||
} | |||
if (createBranch || orphan) | |||
outw.println(MessageFormat.format( | |||
CLIText.get().switchedToNewBranch, name)); | |||
else | |||
outw.println(MessageFormat.format( | |||
CLIText.get().switchedToBranch, | |||
Repository.shortenRefName(ref.getName()))); | |||
} catch (RefNotFoundException e) { | |||
outw.println(MessageFormat.format( | |||
CLIText.get().alreadyOnBranch, | |||
CLIText.get().pathspecDidNotMatch, | |||
name)); | |||
} catch (RefAlreadyExistsException e) { | |||
throw die(MessageFormat.format(CLIText.get().branchAlreadyExists, | |||
name)); | |||
return; | |||
} catch (CheckoutConflictException e) { | |||
outw.println(CLIText.get().checkoutConflict); | |||
for (String path : e.getConflictingPaths()) | |||
outw.println(MessageFormat.format( | |||
CLIText.get().checkoutConflictPathLine, path)); | |||
} | |||
if (createBranch || orphan) | |||
outw.println(MessageFormat.format( | |||
CLIText.get().switchedToNewBranch, name)); | |||
else | |||
outw.println(MessageFormat.format( | |||
CLIText.get().switchedToBranch, | |||
Repository.shortenRefName(ref.getName()))); | |||
} catch (RefNotFoundException e) { | |||
outw.println(MessageFormat.format( | |||
CLIText.get().pathspecDidNotMatch, | |||
name)); | |||
} catch (RefAlreadyExistsException e) { | |||
throw die(MessageFormat.format(CLIText.get().branchAlreadyExists, | |||
name)); | |||
} catch (CheckoutConflictException e) { | |||
outw.println(CLIText.get().checkoutConflict); | |||
for (String path : e.getConflictingPaths()) | |||
outw.println(MessageFormat.format( | |||
CLIText.get().checkoutConflictPathLine, path)); | |||
} | |||
} | |||
} |
@@ -80,37 +80,39 @@ class Commit extends TextBuiltin { | |||
@Override | |||
protected void run() throws NoHeadException, NoMessageException, | |||
ConcurrentRefUpdateException, JGitInternalException, Exception { | |||
CommitCommand commitCmd = new Git(db).commit(); | |||
if (author != null) | |||
commitCmd.setAuthor(RawParseUtils.parsePersonIdent(author)); | |||
if (message != null) | |||
commitCmd.setMessage(message); | |||
if (only && paths.isEmpty()) | |||
throw die(CLIText.get().pathsRequired); | |||
if (only && all) | |||
throw die(CLIText.get().onlyOneOfIncludeOnlyAllInteractiveCanBeUsed); | |||
if (!paths.isEmpty()) | |||
for (String p : paths) | |||
commitCmd.setOnly(p); | |||
commitCmd.setAmend(amend); | |||
commitCmd.setAll(all); | |||
Ref head = db.getRef(Constants.HEAD); | |||
RevCommit commit; | |||
try { | |||
commit = commitCmd.call(); | |||
} catch (JGitInternalException e) { | |||
throw die(e.getMessage()); | |||
} | |||
try (Git git = new Git(db)) { | |||
CommitCommand commitCmd = git.commit(); | |||
if (author != null) | |||
commitCmd.setAuthor(RawParseUtils.parsePersonIdent(author)); | |||
if (message != null) | |||
commitCmd.setMessage(message); | |||
if (only && paths.isEmpty()) | |||
throw die(CLIText.get().pathsRequired); | |||
if (only && all) | |||
throw die(CLIText.get().onlyOneOfIncludeOnlyAllInteractiveCanBeUsed); | |||
if (!paths.isEmpty()) | |||
for (String p : paths) | |||
commitCmd.setOnly(p); | |||
commitCmd.setAmend(amend); | |||
commitCmd.setAll(all); | |||
Ref head = db.getRef(Constants.HEAD); | |||
RevCommit commit; | |||
try { | |||
commit = commitCmd.call(); | |||
} catch (JGitInternalException e) { | |||
throw die(e.getMessage()); | |||
} | |||
String branchName; | |||
if (!head.isSymbolic()) | |||
branchName = CLIText.get().branchDetachedHEAD; | |||
else { | |||
branchName = head.getTarget().getName(); | |||
if (branchName.startsWith(Constants.R_HEADS)) | |||
branchName = branchName.substring(Constants.R_HEADS.length()); | |||
String branchName; | |||
if (!head.isSymbolic()) | |||
branchName = CLIText.get().branchDetachedHEAD; | |||
else { | |||
branchName = head.getTarget().getName(); | |||
if (branchName.startsWith(Constants.R_HEADS)) | |||
branchName = branchName.substring(Constants.R_HEADS.length()); | |||
} | |||
outw.println("[" + branchName + " " + commit.name() + "] " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |||
+ commit.getShortMessage()); | |||
} | |||
outw.println("[" + branchName + " " + commit.name() + "] " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |||
+ commit.getShortMessage()); | |||
} | |||
} |
@@ -61,20 +61,21 @@ class Describe extends TextBuiltin { | |||
@Override | |||
protected void run() throws Exception { | |||
DescribeCommand cmd = new Git(db).describe(); | |||
if (tree != null) | |||
cmd.setTarget(tree); | |||
cmd.setLong(longDesc); | |||
String result = null; | |||
try { | |||
result = cmd.call(); | |||
} catch (RefNotFoundException e) { | |||
throw die(CLIText.get().noNamesFound, e); | |||
} | |||
if (result == null) | |||
throw die(CLIText.get().noNamesFound); | |||
try (Git git = new Git(db)) { | |||
DescribeCommand cmd = git.describe(); | |||
if (tree != null) | |||
cmd.setTarget(tree); | |||
cmd.setLong(longDesc); | |||
String result = null; | |||
try { | |||
result = cmd.call(); | |||
} catch (RefNotFoundException e) { | |||
throw die(CLIText.get().noNamesFound, e); | |||
} | |||
if (result == null) | |||
throw die(CLIText.get().noNamesFound); | |||
outw.println(result); | |||
outw.println(result); | |||
} | |||
} | |||
} |
@@ -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(); | |||
} | |||
} | |||
} | |||
} |
@@ -104,31 +104,32 @@ class Fetch extends AbstractFetchCommand { | |||
@Override | |||
protected void run() throws Exception { | |||
Git git = new Git(db); | |||
FetchCommand fetch = git.fetch(); | |||
if (fsck != null) | |||
fetch.setCheckFetchedObjects(fsck.booleanValue()); | |||
if (prune != null) | |||
fetch.setRemoveDeletedRefs(prune.booleanValue()); | |||
if (toget != null) | |||
fetch.setRefSpecs(toget); | |||
if (tags != null) { | |||
fetch.setTagOpt(tags.booleanValue() ? TagOpt.FETCH_TAGS | |||
: TagOpt.NO_TAGS); | |||
try (Git git = new Git(db)) { | |||
FetchCommand fetch = git.fetch(); | |||
if (fsck != null) | |||
fetch.setCheckFetchedObjects(fsck.booleanValue()); | |||
if (prune != null) | |||
fetch.setRemoveDeletedRefs(prune.booleanValue()); | |||
if (toget != null) | |||
fetch.setRefSpecs(toget); | |||
if (tags != null) { | |||
fetch.setTagOpt(tags.booleanValue() ? TagOpt.FETCH_TAGS | |||
: TagOpt.NO_TAGS); | |||
} | |||
if (0 <= timeout) | |||
fetch.setTimeout(timeout); | |||
fetch.setDryRun(dryRun); | |||
fetch.setRemote(remote); | |||
if (thin != null) | |||
fetch.setThin(thin.booleanValue()); | |||
if (quiet == null || !quiet.booleanValue()) | |||
fetch.setProgressMonitor(new TextProgressMonitor(errw)); | |||
FetchResult result = fetch.call(); | |||
if (result.getTrackingRefUpdates().isEmpty()) | |||
return; | |||
showFetchResult(result); | |||
} | |||
if (0 <= timeout) | |||
fetch.setTimeout(timeout); | |||
fetch.setDryRun(dryRun); | |||
fetch.setRemote(remote); | |||
if (thin != null) | |||
fetch.setThin(thin.booleanValue()); | |||
if (quiet == null || !quiet.booleanValue()) | |||
fetch.setProgressMonitor(new TextProgressMonitor(errw)); | |||
FetchResult result = fetch.call(); | |||
if (result.getTrackingRefUpdates().isEmpty()) | |||
return; | |||
showFetchResult(result); | |||
} | |||
} |
@@ -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(); | |||
} | |||
} | |||
} | |||
} |
@@ -121,22 +121,23 @@ class Merge extends TextBuiltin { | |||
CLIText.get().refDoesNotExistOrNoCommit, ref)); | |||
Ref oldHead = db.getRef(Constants.HEAD); | |||
Git git = new Git(db); | |||
MergeCommand mergeCmd = git.merge().setStrategy(mergeStrategy) | |||
.setSquash(squash).setFastForward(ff).setCommit(!noCommit); | |||
if (srcRef != null) | |||
mergeCmd.include(srcRef); | |||
else | |||
mergeCmd.include(src); | |||
MergeResult result; | |||
try (Git git = new Git(db)) { | |||
MergeCommand mergeCmd = git.merge().setStrategy(mergeStrategy) | |||
.setSquash(squash).setFastForward(ff).setCommit(!noCommit); | |||
if (srcRef != null) | |||
mergeCmd.include(srcRef); | |||
else | |||
mergeCmd.include(src); | |||
if (message != null) | |||
mergeCmd.setMessage(message); | |||
if (message != null) | |||
mergeCmd.setMessage(message); | |||
MergeResult result; | |||
try { | |||
result = mergeCmd.call(); | |||
} catch (CheckoutConflictException e) { | |||
result = new MergeResult(e.getConflictingPaths()); // CHECKOUT_CONFLICT | |||
try { | |||
result = mergeCmd.call(); | |||
} catch (CheckoutConflictException e) { | |||
result = new MergeResult(e.getConflictingPaths()); // CHECKOUT_CONFLICT | |||
} | |||
} | |||
switch (result.getMergeStatus()) { | |||
@@ -206,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); | |||
} | |||
} | |||
} |
@@ -109,24 +109,25 @@ class Push extends TextBuiltin { | |||
@Override | |||
protected void run() throws Exception { | |||
Git git = new Git(db); | |||
PushCommand push = git.push(); | |||
push.setDryRun(dryRun); | |||
push.setForce(force); | |||
push.setProgressMonitor(new TextProgressMonitor(errw)); | |||
push.setReceivePack(receivePack); | |||
push.setRefSpecs(refSpecs); | |||
if (all) | |||
push.setPushAll(); | |||
if (tags) | |||
push.setPushTags(); | |||
push.setRemote(remote); | |||
push.setThin(thin); | |||
push.setTimeout(timeout); | |||
Iterable<PushResult> results = push.call(); | |||
for (PushResult result : results) { | |||
try (ObjectReader reader = db.newObjectReader()) { | |||
printPushResult(reader, result.getURI(), result); | |||
try (Git git = new Git(db)) { | |||
PushCommand push = git.push(); | |||
push.setDryRun(dryRun); | |||
push.setForce(force); | |||
push.setProgressMonitor(new TextProgressMonitor(errw)); | |||
push.setReceivePack(receivePack); | |||
push.setRefSpecs(refSpecs); | |||
if (all) | |||
push.setPushAll(); | |||
if (tags) | |||
push.setPushTags(); | |||
push.setRemote(remote); | |||
push.setThin(thin); | |||
push.setTimeout(timeout); | |||
Iterable<PushResult> results = push.call(); | |||
for (PushResult result : results) { | |||
try (ObjectReader reader = db.newObjectReader()) { | |||
printPushResult(reader, result.getURI(), result); | |||
} | |||
} | |||
} | |||
} |
@@ -59,13 +59,15 @@ class Reflog extends TextBuiltin { | |||
@Override | |||
protected void run() throws Exception { | |||
ReflogCommand cmd = new Git(db).reflog(); | |||
if (ref != null) | |||
cmd.setRef(ref); | |||
Collection<ReflogEntry> entries = cmd.call(); | |||
int i = 0; | |||
for (ReflogEntry entry : entries) { | |||
outw.println(toString(entry, i++)); | |||
try (Git git = new Git(db)) { | |||
ReflogCommand cmd = git.reflog(); | |||
if (ref != null) | |||
cmd.setRef(ref); | |||
Collection<ReflogEntry> entries = cmd.call(); | |||
int i = 0; | |||
for (ReflogEntry entry : entries) { | |||
outw.println(toString(entry, i++)); | |||
} | |||
} | |||
} | |||
@@ -66,19 +66,21 @@ class Reset extends TextBuiltin { | |||
@Override | |||
protected void run() throws Exception { | |||
ResetCommand command = new Git(db).reset(); | |||
command.setRef(commit); | |||
ResetType mode = null; | |||
if (soft) | |||
mode = selectMode(mode, ResetType.SOFT); | |||
if (mixed) | |||
mode = selectMode(mode, ResetType.MIXED); | |||
if (hard) | |||
mode = selectMode(mode, ResetType.HARD); | |||
if (mode == null) | |||
throw die("no reset mode set"); | |||
command.setMode(mode); | |||
command.call(); | |||
try (Git git = new Git(db)) { | |||
ResetCommand command = git.reset(); | |||
command.setRef(commit); | |||
ResetType mode = null; | |||
if (soft) | |||
mode = selectMode(mode, ResetType.SOFT); | |||
if (mixed) | |||
mode = selectMode(mode, ResetType.MIXED); | |||
if (hard) | |||
mode = selectMode(mode, ResetType.HARD); | |||
if (mode == null) | |||
throw die("no reset mode set"); | |||
command.setMode(mode); | |||
command.call(); | |||
} | |||
} | |||
private static ResetType selectMode(ResetType mode, ResetType want) { |
@@ -63,10 +63,11 @@ class Rm extends TextBuiltin { | |||
@Override | |||
protected void run() throws Exception { | |||
RmCommand command = new Git(db).rm(); | |||
for (String p : paths) | |||
command.addFilepattern(p); | |||
command.call(); | |||
try (Git git = new Git(db)) { | |||
RmCommand command = git.rm(); | |||
for (String p : paths) | |||
command.addFilepattern(p); | |||
command.call(); | |||
} | |||
} | |||
} |
@@ -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(); | |||
} | |||
} | |||
} | |||
@@ -88,12 +88,14 @@ class Status extends TextBuiltin { | |||
@Override | |||
protected void run() throws Exception { | |||
StatusCommand statusCommand = new Git(db).status(); | |||
if (filterPaths != null && filterPaths.size() > 0) | |||
for (String path : filterPaths) | |||
statusCommand.addPath(path); | |||
org.eclipse.jgit.api.Status status = statusCommand.call(); | |||
printStatus(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) |
@@ -79,26 +79,28 @@ class Tag extends TextBuiltin { | |||
@Override | |||
protected void run() throws Exception { | |||
Git git = new Git(db); | |||
if (tagName != null) { | |||
TagCommand command = git.tag().setForceUpdate(force) | |||
.setMessage(message).setName(tagName); | |||
try (Git git = new Git(db)) { | |||
if (tagName != null) { | |||
TagCommand command = git.tag().setForceUpdate(force) | |||
.setMessage(message).setName(tagName); | |||
if (object != null) { | |||
RevWalk walk = new RevWalk(db); | |||
command.setObjectId(walk.parseAny(object)); | |||
} | |||
try { | |||
command.call(); | |||
} catch (RefAlreadyExistsException e) { | |||
throw die(MessageFormat.format(CLIText.get().tagAlreadyExists, | |||
tagName)); | |||
} | |||
} else { | |||
ListTagCommand command = git.tagList(); | |||
List<Ref> list = command.call(); | |||
for (Ref ref : list) { | |||
outw.println(Repository.shortenRefName(ref.getName())); | |||
if (object != null) { | |||
try (RevWalk walk = new RevWalk(db)) { | |||
command.setObjectId(walk.parseAny(object)); | |||
} | |||
} | |||
try { | |||
command.call(); | |||
} catch (RefAlreadyExistsException e) { | |||
throw die(MessageFormat.format(CLIText.get().tagAlreadyExists, | |||
tagName)); | |||
} | |||
} else { | |||
ListTagCommand command = git.tagList(); | |||
List<Ref> list = command.call(); | |||
for (Ref ref : list) { | |||
outw.println(Repository.shortenRefName(ref.getName())); | |||
} | |||
} | |||
} | |||
} |
@@ -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); |
@@ -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 |
@@ -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); | |||
@@ -665,10 +665,9 @@ public class DiffFormatter implements AutoCloseable { | |||
format(res.header, res.a, res.b); | |||
} | |||
private static byte[] writeGitLinkText(AbbreviatedObjectId id) | |||
throws IOException { | |||
private static byte[] writeGitLinkText(AbbreviatedObjectId id) { | |||
return encodeASCII("Subproject commit " + id.name() //$NON-NLS-1$ | |||
+ "\n"); | |||
+ "\n"); //$NON-NLS-1$ | |||
} | |||
private String format(AbbreviatedObjectId id) { |
@@ -260,8 +260,8 @@ class FanoutBucket extends InMemoryNoteBucket { | |||
} | |||
ObjectId getTreeId() { | |||
try { | |||
return new ObjectInserter.Formatter().idFor(build(false, null)); | |||
try (ObjectInserter.Formatter f = new ObjectInserter.Formatter()) { | |||
return f.idFor(build(false, null)); | |||
} catch (IOException e) { | |||
// should never happen as we are not inserting | |||
throw new RuntimeException(e); |