Browse Source

Use try-with-resource to close resources in CommitCommand

Change-Id: Ibbbc74acfd050f28e68f318970660b5959caf7e3
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
stable-4.1
Matthias Sohn 10 years ago
parent
commit
d726f0c1e0
  1. 53
      org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java

53
org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java

@ -165,9 +165,7 @@ public class CommitCommand extends GitCommand<RevCommit> {
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 class CommitCommand extends GitCommand<RevCommit> {
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 class CommitCommand extends GitCommand<RevCommit> {
// 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 class CommitCommand extends GitCommand<RevCommit> {
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 class CommitCommand extends GitCommand<RevCommit> {
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 class CommitCommand extends GitCommand<RevCommit> {
} catch (IOException e) {
throw new JGitInternalException(
JGitText.get().exceptionCaughtDuringExecutionOfCommitCommand, e);
} finally {
rw.dispose();
}
}
@ -338,8 +327,9 @@ public class CommitCommand extends GitCommand<RevCommit> {
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 @@ public class CommitCommand extends GitCommand<RevCommit> {
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 @@ public class CommitCommand extends GitCommand<RevCommit> {
// 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 @@ public class CommitCommand extends GitCommand<RevCommit> {
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 @@ public class CommitCommand extends GitCommand<RevCommit> {
} 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 @@ public class CommitCommand extends GitCommand<RevCommit> {
existingBuilder.add(dcTree.getDirCacheEntry());
}
}
}
// there must be no unprocessed paths left at this point; otherwise an
// untracked or unknown path has been specified

Loading…
Cancel
Save