Use try-with-resource to close resources in CommitCommand

Change-Id: Ibbbc74acfd050f28e68f318970660b5959caf7e3
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
This commit is contained in:
Matthias Sohn 2015-04-03 00:52:41 +02:00
parent bb2ca576ff
commit d726f0c1e0
1 changed files with 158 additions and 163 deletions

View File

@ -165,9 +165,7 @@ public RevCommit call() throws GitAPIException, NoHeadException,
checkCallable(); checkCallable();
Collections.sort(only); Collections.sort(only);
RevWalk rw = new RevWalk(repo); try (RevWalk rw = new RevWalk(repo)) {
try {
RepositoryState state = repo.getRepositoryState(); RepositoryState state = repo.getRepositoryState();
if (!state.canCommit()) if (!state.canCommit())
throw new WrongRepositoryStateException(MessageFormat.format( throw new WrongRepositoryStateException(MessageFormat.format(
@ -181,8 +179,7 @@ public RevCommit call() throws GitAPIException, NoHeadException,
processOptions(state, rw); processOptions(state, rw);
if (all && !repo.isBare() && repo.getWorkTree() != null) { if (all && !repo.isBare() && repo.getWorkTree() != null) {
Git git = new Git(repo); try (Git git = new Git(repo)) {
try {
git.add() git.add()
.addFilepattern(".") //$NON-NLS-1$ .addFilepattern(".") //$NON-NLS-1$
.setUpdate(true).call(); .setUpdate(true).call();
@ -221,12 +218,10 @@ public RevCommit call() throws GitAPIException, NoHeadException,
// lock the index // lock the index
DirCache index = repo.lockDirCache(); DirCache index = repo.lockDirCache();
try { try (ObjectInserter odi = repo.newObjectInserter()) {
if (!only.isEmpty()) if (!only.isEmpty())
index = createTemporaryIndex(headId, index, rw); index = createTemporaryIndex(headId, index, rw);
ObjectInserter odi = repo.newObjectInserter();
try {
// Write the index as tree to the object database. This may // Write the index as tree to the object database. This may
// fail for example when the index contains unmerged paths // fail for example when the index contains unmerged paths
// (unresolved conflicts) // (unresolved conflicts)
@ -255,8 +250,8 @@ public RevCommit call() throws GitAPIException, NoHeadException,
String prefix = amend ? "commit (amend): " //$NON-NLS-1$ String prefix = amend ? "commit (amend): " //$NON-NLS-1$
: parents.size() == 0 ? "commit (initial): " //$NON-NLS-1$ : parents.size() == 0 ? "commit (initial): " //$NON-NLS-1$
: "commit: "; //$NON-NLS-1$ : "commit: "; //$NON-NLS-1$
ru.setRefLogMessage( ru.setRefLogMessage(prefix + revCommit.getShortMessage(),
prefix + revCommit.getShortMessage(), false); false);
} }
if (headId != null) if (headId != null)
ru.setExpectedOldObjectId(headId); ru.setExpectedOldObjectId(headId);
@ -286,15 +281,11 @@ public RevCommit call() throws GitAPIException, NoHeadException,
case REJECTED: case REJECTED:
case LOCK_FAILURE: case LOCK_FAILURE:
throw new ConcurrentRefUpdateException( throw new ConcurrentRefUpdateException(
JGitText.get().couldNotLockHEAD, ru.getRef(), JGitText.get().couldNotLockHEAD, ru.getRef(), rc);
rc);
default: default:
throw new JGitInternalException(MessageFormat.format( throw new JGitInternalException(MessageFormat.format(
JGitText.get().updatingRefFailed, JGitText.get().updatingRefFailed, Constants.HEAD,
Constants.HEAD, commitId.toString(), rc)); commitId.toString(), rc));
}
} finally {
odi.release();
} }
} finally { } finally {
index.unlock(); index.unlock();
@ -304,8 +295,6 @@ public RevCommit call() throws GitAPIException, NoHeadException,
} catch (IOException e) { } catch (IOException e) {
throw new JGitInternalException( throw new JGitInternalException(
JGitText.get().exceptionCaughtDuringExecutionOfCommitCommand, e); JGitText.get().exceptionCaughtDuringExecutionOfCommitCommand, e);
} finally {
rw.dispose();
} }
} }
@ -338,8 +327,9 @@ private DirCache createTemporaryIndex(ObjectId headId, DirCache index,
onlyProcessed = new boolean[only.size()]; onlyProcessed = new boolean[only.size()];
boolean emptyCommit = true; boolean emptyCommit = true;
TreeWalk treeWalk = new TreeWalk(repo); try (TreeWalk treeWalk = new TreeWalk(repo)) {
int dcIdx = treeWalk.addTree(new DirCacheBuildIterator(existingBuilder)); int dcIdx = treeWalk
.addTree(new DirCacheBuildIterator(existingBuilder));
int fIdx = treeWalk.addTree(new FileTreeIterator(repo)); int fIdx = treeWalk.addTree(new FileTreeIterator(repo));
int hIdx = -1; int hIdx = -1;
if (headId != null) if (headId != null)
@ -378,15 +368,16 @@ private DirCache createTemporaryIndex(ObjectId headId, DirCache index,
lastAddedFile = path; lastAddedFile = path;
if (fTree != null) { if (fTree != null) {
// create a new DirCacheEntry with data retrieved from disk // create a new DirCacheEntry with data retrieved from
// disk
final DirCacheEntry dcEntry = new DirCacheEntry(path); final DirCacheEntry dcEntry = new DirCacheEntry(path);
long entryLength = fTree.getEntryLength(); long entryLength = fTree.getEntryLength();
dcEntry.setLength(entryLength); dcEntry.setLength(entryLength);
dcEntry.setLastModified(fTree.getEntryLastModified()); dcEntry.setLastModified(fTree.getEntryLastModified());
dcEntry.setFileMode(fTree.getIndexFileMode(dcTree)); dcEntry.setFileMode(fTree.getIndexFileMode(dcTree));
boolean objectExists = (dcTree != null && fTree boolean objectExists = (dcTree != null
.idEqual(dcTree)) && fTree.idEqual(dcTree))
|| (hTree != null && fTree.idEqual(hTree)); || (hTree != null && fTree.idEqual(hTree));
if (objectExists) { if (objectExists) {
dcEntry.setObjectId(fTree.getEntryObjectId()); dcEntry.setObjectId(fTree.getEntryObjectId());
@ -397,8 +388,10 @@ private DirCache createTemporaryIndex(ObjectId headId, DirCache index,
// insert object // insert object
if (inserter == null) if (inserter == null)
inserter = repo.newObjectInserter(); inserter = repo.newObjectInserter();
long contentLength = fTree.getEntryContentLength(); long contentLength = fTree
InputStream inputStream = fTree.openEntryStream(); .getEntryContentLength();
InputStream inputStream = fTree
.openEntryStream();
try { try {
dcEntry.setObjectId(inserter.insert( dcEntry.setObjectId(inserter.insert(
Constants.OBJ_BLOB, contentLength, Constants.OBJ_BLOB, contentLength,
@ -415,8 +408,8 @@ private DirCache createTemporaryIndex(ObjectId headId, DirCache index,
tempBuilder.add(dcEntry); tempBuilder.add(dcEntry);
if (emptyCommit if (emptyCommit
&& (hTree == null || !hTree.idEqual(fTree) || hTree && (hTree == null || !hTree.idEqual(fTree)
.getEntryRawMode() != fTree || hTree.getEntryRawMode() != fTree
.getEntryRawMode())) .getEntryRawMode()))
// this is a change // this is a change
emptyCommit = false; emptyCommit = false;
@ -434,7 +427,8 @@ private DirCache createTemporaryIndex(ObjectId headId, DirCache index,
} else { } else {
// add entries from HEAD for all other paths // add entries from HEAD for all other paths
if (hTree != null) { if (hTree != null) {
// create a new DirCacheEntry with data retrieved from HEAD // create a new DirCacheEntry with data retrieved from
// HEAD
final DirCacheEntry dcEntry = new DirCacheEntry(path); final DirCacheEntry dcEntry = new DirCacheEntry(path);
dcEntry.setObjectId(hTree.getEntryObjectId()); dcEntry.setObjectId(hTree.getEntryObjectId());
dcEntry.setFileMode(hTree.getEntryFileMode()); dcEntry.setFileMode(hTree.getEntryFileMode());
@ -448,6 +442,7 @@ private DirCache createTemporaryIndex(ObjectId headId, DirCache index,
existingBuilder.add(dcTree.getDirCacheEntry()); existingBuilder.add(dcTree.getDirCacheEntry());
} }
} }
}
// there must be no unprocessed paths left at this point; otherwise an // there must be no unprocessed paths left at this point; otherwise an
// untracked or unknown path has been specified // untracked or unknown path has been specified