Merge branch 'stable-4.1'

* 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>
This commit is contained in:
Matthias Sohn 2015-10-02 01:34:45 +02:00
commit d652a6bfd7
24 changed files with 335 additions and 308 deletions

View File

@ -117,10 +117,10 @@ public void execute() throws BuildException {
} }
AddCommand gitAdd; AddCommand gitAdd;
try { try (Repository repo = new FileRepositoryBuilder().readEnvironment()
Repository repo = new FileRepositoryBuilder().readEnvironment() .findGitDir(src).build();
.findGitDir(src).build(); Git git = new Git(repo);) {
gitAdd = new Git(repo).add(); gitAdd = git.add();
} catch (IOException e) { } catch (IOException e) {
throw new BuildException("Could not access repository " + src, e); throw new BuildException("Could not access repository " + src, e);
} }

View File

@ -105,10 +105,10 @@ public void setForce(boolean force) {
@Override @Override
public void execute() throws BuildException { public void execute() throws BuildException {
CheckoutCommand checkout; CheckoutCommand checkout;
try { try (Repository repo = new FileRepositoryBuilder().readEnvironment()
Repository repo = new FileRepositoryBuilder().readEnvironment() .findGitDir(src).build();
.findGitDir(src).build(); Git git = new Git(repo)) {
checkout = new Git(repo).checkout(); checkout = git.checkout();
} catch (IOException e) { } catch (IOException e) {
throw new BuildException("Could not access repository " + src, e); throw new BuildException("Could not access repository " + src, e);
} }

View File

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

View File

@ -87,8 +87,8 @@ protected void run() throws Exception {
else else
stream = outs; stream = outs;
try { try (Git git = new Git(db)) {
ArchiveCommand cmd = new Git(db).archive() ArchiveCommand cmd = git.archive()
.setTree(tree) .setTree(tree)
.setFormat(format) .setFormat(format)
.setPrefix(prefix) .setPrefix(prefix)

View File

@ -186,29 +186,31 @@ private void list() throws Exception {
// This can happen if HEAD is stillborn // This can happen if HEAD is stillborn
if (head != null) { if (head != null) {
String current = head.getLeaf().getName(); String current = head.getLeaf().getName();
ListBranchCommand command = new Git(db).branchList(); try (Git git = new Git(db)) {
if (all) ListBranchCommand command = git.branchList();
command.setListMode(ListMode.ALL); if (all)
else if (remote) command.setListMode(ListMode.ALL);
command.setListMode(ListMode.REMOTE); else if (remote)
command.setListMode(ListMode.REMOTE);
if (containsCommitish != null) if (containsCommitish != null)
command.setContains(containsCommitish); command.setContains(containsCommitish);
List<Ref> refs = command.call(); List<Ref> refs = command.call();
for (Ref ref : refs) { for (Ref ref : refs) {
if (ref.getName().equals(Constants.HEAD)) if (ref.getName().equals(Constants.HEAD))
addRef("(no branch)", head); //$NON-NLS-1$ addRef("(no branch)", head); //$NON-NLS-1$
} }
addRefs(refs, Constants.R_HEADS); addRefs(refs, Constants.R_HEADS);
addRefs(refs, Constants.R_REMOTES); addRefs(refs, Constants.R_REMOTES);
try (ObjectReader reader = db.newObjectReader()) { try (ObjectReader reader = db.newObjectReader()) {
for (final Entry<String, Ref> e : printRefs.entrySet()) { for (final Entry<String, Ref> e : printRefs.entrySet()) {
final Ref ref = e.getValue(); final Ref ref = e.getValue();
printHead(reader, e.getKey(), printHead(reader, e.getKey(),
current.equals(ref.getName()), ref); current.equals(ref.getName()), ref);
}
} }
} }
} }

View File

@ -89,47 +89,49 @@ protected void run() throws Exception {
throw die(CLIText.get().onBranchToBeBorn); throw die(CLIText.get().onBranchToBeBorn);
} }
CheckoutCommand command = new Git(db).checkout(); try (Git git = new Git(db)) {
if (paths.size() > 0) { CheckoutCommand command = git.checkout();
command.setStartPoint(name); if (paths.size() > 0) {
for (String path : paths) command.setStartPoint(name);
command.addPath(path); for (String path : paths)
} else { command.addPath(path);
command.setCreateBranch(createBranch); } else {
command.setName(name); command.setCreateBranch(createBranch);
command.setForce(force); command.setName(name);
command.setOrphan(orphan); 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) 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( outw.println(MessageFormat.format(
CLIText.get().switchedToNewBranch, name)); CLIText.get().pathspecDidNotMatch,
else name));
outw.println(MessageFormat.format( } catch (RefAlreadyExistsException e) {
CLIText.get().switchedToBranch, throw die(MessageFormat.format(CLIText.get().branchAlreadyExists,
Repository.shortenRefName(ref.getName()))); name));
} catch (RefNotFoundException e) { } catch (CheckoutConflictException e) {
outw.println(MessageFormat.format( outw.println(CLIText.get().checkoutConflict);
CLIText.get().pathspecDidNotMatch, for (String path : e.getConflictingPaths())
name)); outw.println(MessageFormat.format(
} catch (RefAlreadyExistsException e) { CLIText.get().checkoutConflictPathLine, path));
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));
} }
} }
} }

View File

@ -80,37 +80,39 @@ class Commit extends TextBuiltin {
@Override @Override
protected void run() throws NoHeadException, NoMessageException, protected void run() throws NoHeadException, NoMessageException,
ConcurrentRefUpdateException, JGitInternalException, Exception { ConcurrentRefUpdateException, JGitInternalException, Exception {
CommitCommand commitCmd = new Git(db).commit(); try (Git git = new Git(db)) {
if (author != null) CommitCommand commitCmd = git.commit();
commitCmd.setAuthor(RawParseUtils.parsePersonIdent(author)); if (author != null)
if (message != null) commitCmd.setAuthor(RawParseUtils.parsePersonIdent(author));
commitCmd.setMessage(message); if (message != null)
if (only && paths.isEmpty()) commitCmd.setMessage(message);
throw die(CLIText.get().pathsRequired); if (only && paths.isEmpty())
if (only && all) throw die(CLIText.get().pathsRequired);
throw die(CLIText.get().onlyOneOfIncludeOnlyAllInteractiveCanBeUsed); if (only && all)
if (!paths.isEmpty()) throw die(CLIText.get().onlyOneOfIncludeOnlyAllInteractiveCanBeUsed);
for (String p : paths) if (!paths.isEmpty())
commitCmd.setOnly(p); for (String p : paths)
commitCmd.setAmend(amend); commitCmd.setOnly(p);
commitCmd.setAll(all); commitCmd.setAmend(amend);
Ref head = db.getRef(Constants.HEAD); commitCmd.setAll(all);
RevCommit commit; Ref head = db.getRef(Constants.HEAD);
try { RevCommit commit;
commit = commitCmd.call(); try {
} catch (JGitInternalException e) { commit = commitCmd.call();
throw die(e.getMessage()); } catch (JGitInternalException e) {
} throw die(e.getMessage());
}
String branchName; String branchName;
if (!head.isSymbolic()) if (!head.isSymbolic())
branchName = CLIText.get().branchDetachedHEAD; branchName = CLIText.get().branchDetachedHEAD;
else { else {
branchName = head.getTarget().getName(); branchName = head.getTarget().getName();
if (branchName.startsWith(Constants.R_HEADS)) if (branchName.startsWith(Constants.R_HEADS))
branchName = branchName.substring(Constants.R_HEADS.length()); 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());
} }
} }

View File

@ -61,20 +61,21 @@ class Describe extends TextBuiltin {
@Override @Override
protected void run() throws Exception { protected void run() throws Exception {
DescribeCommand cmd = new Git(db).describe(); try (Git git = new Git(db)) {
if (tree != null) DescribeCommand cmd = git.describe();
cmd.setTarget(tree); if (tree != null)
cmd.setLong(longDesc); cmd.setTarget(tree);
String result = null; cmd.setLong(longDesc);
try { String result = null;
result = cmd.call(); try {
} catch (RefNotFoundException e) { result = cmd.call();
throw die(CLIText.get().noNamesFound, e); } catch (RefNotFoundException e) {
throw die(CLIText.get().noNamesFound, e);
}
if (result == null)
throw die(CLIText.get().noNamesFound);
outw.println(result);
} }
if (result == null)
throw die(CLIText.get().noNamesFound);
outw.println(result);
} }
} }

View File

@ -74,46 +74,47 @@ void tree_0(final AbstractTreeIterator c) {
@Override @Override
protected void run() throws Exception { protected void run() throws Exception {
final TreeWalk walk = new TreeWalk(db); try (final TreeWalk walk = new TreeWalk(db)) {
walk.setRecursive(recursive); walk.setRecursive(recursive);
for (final AbstractTreeIterator i : trees) for (final AbstractTreeIterator i : trees)
walk.addTree(i); walk.addTree(i);
walk.setFilter(AndTreeFilter.create(TreeFilter.ANY_DIFF, pathFilter)); walk.setFilter(AndTreeFilter.create(TreeFilter.ANY_DIFF, pathFilter));
final int nTree = walk.getTreeCount(); final int nTree = walk.getTreeCount();
while (walk.next()) { while (walk.next()) {
for (int i = 1; i < nTree; i++) for (int i = 1; i < nTree; i++)
outw.print(':'); outw.print(':');
for (int i = 0; i < nTree; i++) { for (int i = 0; i < nTree; i++) {
final FileMode m = walk.getFileMode(i); final FileMode m = walk.getFileMode(i);
final String s = m.toString(); final String s = m.toString();
for (int pad = 6 - s.length(); pad > 0; pad--) for (int pad = 6 - s.length(); pad > 0; pad--)
outw.print('0'); outw.print('0');
outw.print(s); outw.print(s);
outw.print(' '); 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);
outw.print('\t');
outw.print(walk.getPathString());
outw.println();
} }
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);
outw.print('\t');
outw.print(walk.getPathString());
outw.println();
} }
} }
} }

View File

@ -104,31 +104,32 @@ void notags(@SuppressWarnings("unused")
@Override @Override
protected void run() throws Exception { protected void run() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
FetchCommand fetch = git.fetch(); FetchCommand fetch = git.fetch();
if (fsck != null) if (fsck != null)
fetch.setCheckFetchedObjects(fsck.booleanValue()); fetch.setCheckFetchedObjects(fsck.booleanValue());
if (prune != null) if (prune != null)
fetch.setRemoveDeletedRefs(prune.booleanValue()); fetch.setRemoveDeletedRefs(prune.booleanValue());
if (toget != null) if (toget != null)
fetch.setRefSpecs(toget); fetch.setRefSpecs(toget);
if (tags != null) { if (tags != null) {
fetch.setTagOpt(tags.booleanValue() ? TagOpt.FETCH_TAGS fetch.setTagOpt(tags.booleanValue() ? TagOpt.FETCH_TAGS
: TagOpt.NO_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);
} }
} }

View File

@ -72,27 +72,28 @@ class LsTree extends TextBuiltin {
@Override @Override
protected void run() throws Exception { protected void run() throws Exception {
final TreeWalk walk = new TreeWalk(db); try (final TreeWalk walk = new TreeWalk(db)) {
walk.reset(); // drop the first empty tree, which we do not need here walk.reset(); // drop the first empty tree, which we do not need here
if (paths.size() > 0) if (paths.size() > 0)
walk.setFilter(PathFilterGroup.createFromStrings(paths)); walk.setFilter(PathFilterGroup.createFromStrings(paths));
walk.setRecursive(recursive); walk.setRecursive(recursive);
walk.addTree(tree); walk.addTree(tree);
while (walk.next()) { while (walk.next()) {
final FileMode mode = walk.getFileMode(0); final FileMode mode = walk.getFileMode(0);
if (mode == FileMode.TREE) if (mode == FileMode.TREE)
outw.print('0'); outw.print('0');
outw.print(mode); outw.print(mode);
outw.print(' '); outw.print(' ');
outw.print(Constants.typeString(mode.getObjectType())); outw.print(Constants.typeString(mode.getObjectType()));
outw.print(' '); outw.print(' ');
outw.print(walk.getObjectId(0).name()); outw.print(walk.getObjectId(0).name());
outw.print('\t'); outw.print('\t');
outw.print(QuotedString.GIT_PATH.quote(walk.getPathString())); outw.print(QuotedString.GIT_PATH.quote(walk.getPathString()));
outw.println(); outw.println();
}
} }
} }
} }

View File

@ -121,22 +121,23 @@ protected void run() throws Exception {
CLIText.get().refDoesNotExistOrNoCommit, ref)); CLIText.get().refDoesNotExistOrNoCommit, ref));
Ref oldHead = db.getRef(Constants.HEAD); 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);
if (message != null)
mergeCmd.setMessage(message);
MergeResult result; MergeResult result;
try { try (Git git = new Git(db)) {
result = mergeCmd.call(); MergeCommand mergeCmd = git.merge().setStrategy(mergeStrategy)
} catch (CheckoutConflictException e) { .setSquash(squash).setFastForward(ff).setCommit(!noCommit);
result = new MergeResult(e.getConflictingPaths()); // CHECKOUT_CONFLICT if (srcRef != null)
mergeCmd.include(srcRef);
else
mergeCmd.include(src);
if (message != null)
mergeCmd.setMessage(message);
try {
result = mergeCmd.call();
} catch (CheckoutConflictException e) {
result = new MergeResult(e.getConflictingPaths()); // CHECKOUT_CONFLICT
}
} }
switch (result.getMergeStatus()) { switch (result.getMergeStatus()) {
@ -206,12 +207,13 @@ protected void run() throws Exception {
private boolean isMergedInto(Ref oldHead, AnyObjectId src) private boolean isMergedInto(Ref oldHead, AnyObjectId src)
throws IOException { throws IOException {
RevWalk revWalk = new RevWalk(db); try (RevWalk revWalk = new RevWalk(db)) {
ObjectId oldHeadObjectId = oldHead.getPeeledObjectId(); ObjectId oldHeadObjectId = oldHead.getPeeledObjectId();
if (oldHeadObjectId == null) if (oldHeadObjectId == null)
oldHeadObjectId = oldHead.getObjectId(); oldHeadObjectId = oldHead.getObjectId();
RevCommit oldHeadCommit = revWalk.lookupCommit(oldHeadObjectId); RevCommit oldHeadCommit = revWalk.lookupCommit(oldHeadObjectId);
RevCommit srcCommit = revWalk.lookupCommit(src); RevCommit srcCommit = revWalk.lookupCommit(src);
return revWalk.isMergedInto(oldHeadCommit, srcCommit); return revWalk.isMergedInto(oldHeadCommit, srcCommit);
}
} }
} }

View File

@ -109,24 +109,25 @@ void nothin(@SuppressWarnings("unused") final boolean ignored) {
@Override @Override
protected void run() throws Exception { protected void run() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
PushCommand push = git.push(); PushCommand push = git.push();
push.setDryRun(dryRun); push.setDryRun(dryRun);
push.setForce(force); push.setForce(force);
push.setProgressMonitor(new TextProgressMonitor(errw)); push.setProgressMonitor(new TextProgressMonitor(errw));
push.setReceivePack(receivePack); push.setReceivePack(receivePack);
push.setRefSpecs(refSpecs); push.setRefSpecs(refSpecs);
if (all) if (all)
push.setPushAll(); push.setPushAll();
if (tags) if (tags)
push.setPushTags(); push.setPushTags();
push.setRemote(remote); push.setRemote(remote);
push.setThin(thin); push.setThin(thin);
push.setTimeout(timeout); push.setTimeout(timeout);
Iterable<PushResult> results = push.call(); Iterable<PushResult> results = push.call();
for (PushResult result : results) { for (PushResult result : results) {
try (ObjectReader reader = db.newObjectReader()) { try (ObjectReader reader = db.newObjectReader()) {
printPushResult(reader, result.getURI(), result); printPushResult(reader, result.getURI(), result);
}
} }
} }
} }

View File

@ -59,13 +59,15 @@ class Reflog extends TextBuiltin {
@Override @Override
protected void run() throws Exception { protected void run() throws Exception {
ReflogCommand cmd = new Git(db).reflog(); try (Git git = new Git(db)) {
if (ref != null) ReflogCommand cmd = git.reflog();
cmd.setRef(ref); if (ref != null)
Collection<ReflogEntry> entries = cmd.call(); cmd.setRef(ref);
int i = 0; Collection<ReflogEntry> entries = cmd.call();
for (ReflogEntry entry : entries) { int i = 0;
outw.println(toString(entry, i++)); for (ReflogEntry entry : entries) {
outw.println(toString(entry, i++));
}
} }
} }

View File

@ -66,19 +66,21 @@ class Reset extends TextBuiltin {
@Override @Override
protected void run() throws Exception { protected void run() throws Exception {
ResetCommand command = new Git(db).reset(); try (Git git = new Git(db)) {
command.setRef(commit); ResetCommand command = git.reset();
ResetType mode = null; command.setRef(commit);
if (soft) ResetType mode = null;
mode = selectMode(mode, ResetType.SOFT); if (soft)
if (mixed) mode = selectMode(mode, ResetType.SOFT);
mode = selectMode(mode, ResetType.MIXED); if (mixed)
if (hard) mode = selectMode(mode, ResetType.MIXED);
mode = selectMode(mode, ResetType.HARD); if (hard)
if (mode == null) mode = selectMode(mode, ResetType.HARD);
throw die("no reset mode set"); if (mode == null)
command.setMode(mode); throw die("no reset mode set");
command.call(); command.setMode(mode);
command.call();
}
} }
private static ResetType selectMode(ResetType mode, ResetType want) { private static ResetType selectMode(ResetType mode, ResetType want) {

View File

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

View File

@ -251,16 +251,17 @@ private void show(RevTag tag) throws IOException {
private void show(RevTree obj) throws MissingObjectException, private void show(RevTree obj) throws MissingObjectException,
IncorrectObjectTypeException, CorruptObjectException, IOException { IncorrectObjectTypeException, CorruptObjectException, IOException {
final TreeWalk walk = new TreeWalk(db); try (final TreeWalk walk = new TreeWalk(db)) {
walk.reset(); walk.reset();
walk.addTree(obj); walk.addTree(obj);
while (walk.next()) { while (walk.next()) {
outw.print(walk.getPathString()); outw.print(walk.getPathString());
final FileMode mode = walk.getFileMode(0); final FileMode mode = walk.getFileMode(0);
if (mode == FileMode.TREE) if (mode == FileMode.TREE)
outw.print("/"); //$NON-NLS-1$ outw.print("/"); //$NON-NLS-1$
outw.println(); outw.println();
}
} }
} }

View File

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

View File

@ -79,26 +79,28 @@ class Tag extends TextBuiltin {
@Override @Override
protected void run() throws Exception { protected void run() throws Exception {
Git git = new Git(db); try (Git git = new Git(db)) {
if (tagName != null) { if (tagName != null) {
TagCommand command = git.tag().setForceUpdate(force) TagCommand command = git.tag().setForceUpdate(force)
.setMessage(message).setName(tagName); .setMessage(message).setName(tagName);
if (object != null) { if (object != null) {
RevWalk walk = new RevWalk(db); try (RevWalk walk = new RevWalk(db)) {
command.setObjectId(walk.parseAny(object)); command.setObjectId(walk.parseAny(object));
} }
try { }
command.call(); try {
} catch (RefAlreadyExistsException e) { command.call();
throw die(MessageFormat.format(CLIText.get().tagAlreadyExists, } catch (RefAlreadyExistsException e) {
tagName)); throw die(MessageFormat.format(CLIText.get().tagAlreadyExists,
} tagName));
} else { }
ListTagCommand command = git.tagList(); } else {
List<Ref> list = command.call(); ListTagCommand command = git.tagList();
for (Ref ref : list) { List<Ref> list = command.call();
outw.println(Repository.shortenRefName(ref.getName())); for (Ref ref : list) {
outw.println(Repository.shortenRefName(ref.getName()));
}
} }
} }
} }

View File

@ -173,9 +173,9 @@ private void run(Repository db) throws Exception {
int maxN = 0; int maxN = 0;
AbbreviatedObjectId startId; AbbreviatedObjectId startId;
try (ObjectReader or = db.newObjectReader()) { try (ObjectReader or = db.newObjectReader();
RevWalk rw = new RevWalk(or)) {
final MutableObjectId id = new MutableObjectId(); final MutableObjectId id = new MutableObjectId();
RevWalk rw = new RevWalk(or);
TreeWalk tw = new TreeWalk(or); TreeWalk tw = new TreeWalk(or);
tw.setFilter(TreeFilter.ANY_DIFF); tw.setFilter(TreeFilter.ANY_DIFF);
tw.setRecursive(true); tw.setRecursive(true);

View File

@ -75,7 +75,10 @@ class ShowPackDelta extends TextBuiltin {
@Override @Override
protected void run() throws Exception { protected void run() throws Exception {
ObjectReader reader = db.newObjectReader(); 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); byte[] delta = getDelta(reader, obj);
// We're crossing our fingers that this will be a delta. Double // We're crossing our fingers that this will be a delta. Double

View File

@ -300,10 +300,10 @@ private void run(Repository db) throws Exception {
long fileCnt = 0; long fileCnt = 0;
long lineCnt = 0; long lineCnt = 0;
try (ObjectReader or = db.newObjectReader()) { try (ObjectReader or = db.newObjectReader();
final MutableObjectId id = new MutableObjectId();
RevWalk rw = new RevWalk(or); 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.reset(rw.parseTree(db.resolve(Constants.HEAD)));
tw.setRecursive(true); tw.setRecursive(true);

View File

@ -665,10 +665,9 @@ public void format(DiffEntry ent) throws IOException {
format(res.header, res.a, res.b); format(res.header, res.a, res.b);
} }
private static byte[] writeGitLinkText(AbbreviatedObjectId id) private static byte[] writeGitLinkText(AbbreviatedObjectId id) {
throws IOException {
return encodeASCII("Subproject commit " + id.name() //$NON-NLS-1$ return encodeASCII("Subproject commit " + id.name() //$NON-NLS-1$
+ "\n"); + "\n"); //$NON-NLS-1$
} }
private String format(AbbreviatedObjectId id) { private String format(AbbreviatedObjectId id) {

View File

@ -260,8 +260,8 @@ ObjectId writeTree(ObjectInserter inserter) throws IOException {
} }
ObjectId getTreeId() { ObjectId getTreeId() {
try { try (ObjectInserter.Formatter f = new ObjectInserter.Formatter()) {
return new ObjectInserter.Formatter().idFor(build(false, null)); return f.idFor(build(false, null));
} catch (IOException e) { } catch (IOException e) {
// should never happen as we are not inserting // should never happen as we are not inserting
throw new RuntimeException(e); throw new RuntimeException(e);