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