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. 349
      org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java

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

@ -165,9 +165,7 @@ public class CommitCommand extends GitCommand<RevCommit> {
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 class CommitCommand extends GitCommand<RevCommit> {
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,80 +218,74 @@ public class CommitCommand extends GitCommand<RevCommit> {
// 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(); // Write the index as tree to the object database. This may
try { // fail for example when the index contains unmerged paths
// Write the index as tree to the object database. This may // (unresolved conflicts)
// fail for example when the index contains unmerged paths ObjectId indexTreeId = index.writeTree(odi);
// (unresolved conflicts)
ObjectId indexTreeId = index.writeTree(odi); if (insertChangeId)
insertChangeId(indexTreeId);
if (insertChangeId)
insertChangeId(indexTreeId); // Create a Commit object, populate it and write it
CommitBuilder commit = new CommitBuilder();
// Create a Commit object, populate it and write it commit.setCommitter(committer);
CommitBuilder commit = new CommitBuilder(); commit.setAuthor(author);
commit.setCommitter(committer); commit.setMessage(message);
commit.setAuthor(author);
commit.setMessage(message); commit.setParentIds(parents);
commit.setTreeId(indexTreeId);
commit.setParentIds(parents); ObjectId commitId = odi.insert(commit);
commit.setTreeId(indexTreeId); odi.flush();
ObjectId commitId = odi.insert(commit);
odi.flush(); RevCommit revCommit = rw.parseCommit(commitId);
RefUpdate ru = repo.updateRef(Constants.HEAD);
RevCommit revCommit = rw.parseCommit(commitId); ru.setNewObjectId(commitId);
RefUpdate ru = repo.updateRef(Constants.HEAD); if (reflogComment != null) {
ru.setNewObjectId(commitId); ru.setRefLogMessage(reflogComment, false);
if (reflogComment != null) { } else {
ru.setRefLogMessage(reflogComment, false); String prefix = amend ? "commit (amend): " //$NON-NLS-1$
} else { : parents.size() == 0 ? "commit (initial): " //$NON-NLS-1$
String prefix = amend ? "commit (amend): " //$NON-NLS-1$ : "commit: "; //$NON-NLS-1$
: parents.size() == 0 ? "commit (initial): " //$NON-NLS-1$ ru.setRefLogMessage(prefix + revCommit.getShortMessage(),
: "commit: "; //$NON-NLS-1$ false);
ru.setRefLogMessage( }
prefix + revCommit.getShortMessage(), false); if (headId != null)
} ru.setExpectedOldObjectId(headId);
if (headId != null) else
ru.setExpectedOldObjectId(headId); ru.setExpectedOldObjectId(ObjectId.zeroId());
else Result rc = ru.forceUpdate();
ru.setExpectedOldObjectId(ObjectId.zeroId()); switch (rc) {
Result rc = ru.forceUpdate(); case NEW:
switch (rc) { case FORCED:
case NEW: case FAST_FORWARD: {
case FORCED: setCallable(false);
case FAST_FORWARD: { if (state == RepositoryState.MERGING_RESOLVED
setCallable(false); || isMergeDuringRebase(state)) {
if (state == RepositoryState.MERGING_RESOLVED // Commit was successful. Now delete the files
|| isMergeDuringRebase(state)) { // used for merge commits
// Commit was successful. Now delete the files repo.writeMergeCommitMsg(null);
// used for merge commits repo.writeMergeHeads(null);
repo.writeMergeCommitMsg(null); } else if (state == RepositoryState.CHERRY_PICKING_RESOLVED) {
repo.writeMergeHeads(null); repo.writeMergeCommitMsg(null);
} else if (state == RepositoryState.CHERRY_PICKING_RESOLVED) { repo.writeCherryPickHead(null);
repo.writeMergeCommitMsg(null); } else if (state == RepositoryState.REVERTING_RESOLVED) {
repo.writeCherryPickHead(null); repo.writeMergeCommitMsg(null);
} else if (state == RepositoryState.REVERTING_RESOLVED) { repo.writeRevertHead(null);
repo.writeMergeCommitMsg(null);
repo.writeRevertHead(null);
}
return revCommit;
}
case REJECTED:
case LOCK_FAILURE:
throw new ConcurrentRefUpdateException(
JGitText.get().couldNotLockHEAD, ru.getRef(),
rc);
default:
throw new JGitInternalException(MessageFormat.format(
JGitText.get().updatingRefFailed,
Constants.HEAD, commitId.toString(), rc));
} }
} finally { return revCommit;
odi.release(); }
case REJECTED:
case LOCK_FAILURE:
throw new ConcurrentRefUpdateException(
JGitText.get().couldNotLockHEAD, ru.getRef(), rc);
default:
throw new JGitInternalException(MessageFormat.format(
JGitText.get().updatingRefFailed, Constants.HEAD,
commitId.toString(), rc));
} }
} finally { } finally {
index.unlock(); index.unlock();
@ -304,8 +295,6 @@ public class CommitCommand extends GitCommand<RevCommit> {
} catch (IOException e) { } catch (IOException e) {
throw new JGitInternalException( throw new JGitInternalException(
JGitText.get().exceptionCaughtDuringExecutionOfCommitCommand, e); JGitText.get().exceptionCaughtDuringExecutionOfCommitCommand, e);
} finally {
rw.dispose();
} }
} }
@ -338,114 +327,120 @@ public class CommitCommand extends GitCommand<RevCommit> {
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
int fIdx = treeWalk.addTree(new FileTreeIterator(repo)); .addTree(new DirCacheBuildIterator(existingBuilder));
int hIdx = -1; int fIdx = treeWalk.addTree(new FileTreeIterator(repo));
if (headId != null) int hIdx = -1;
hIdx = treeWalk.addTree(rw.parseTree(headId)); if (headId != null)
treeWalk.setRecursive(true); hIdx = treeWalk.addTree(rw.parseTree(headId));
treeWalk.setRecursive(true);
String lastAddedFile = null;
while (treeWalk.next()) { String lastAddedFile = null;
String path = treeWalk.getPathString(); while (treeWalk.next()) {
// check if current entry's path matches a specified path String path = treeWalk.getPathString();
int pos = lookupOnly(path); // check if current entry's path matches a specified path
int pos = lookupOnly(path);
CanonicalTreeParser hTree = null;
if (hIdx != -1) CanonicalTreeParser hTree = null;
hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class); if (hIdx != -1)
hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class);
DirCacheIterator dcTree = treeWalk.getTree(dcIdx,
DirCacheIterator.class); DirCacheIterator dcTree = treeWalk.getTree(dcIdx,
DirCacheIterator.class);
if (pos >= 0) {
// include entry in commit if (pos >= 0) {
// include entry in commit
FileTreeIterator fTree = treeWalk.getTree(fIdx,
FileTreeIterator.class); FileTreeIterator fTree = treeWalk.getTree(fIdx,
FileTreeIterator.class);
// check if entry refers to a tracked file
boolean tracked = dcTree != null || hTree != null; // check if entry refers to a tracked file
if (!tracked) boolean tracked = dcTree != null || hTree != null;
break; if (!tracked)
break;
// for an unmerged path, DirCacheBuildIterator will yield 3
// entries, we only want to add one // for an unmerged path, DirCacheBuildIterator will yield 3
if (path.equals(lastAddedFile)) // entries, we only want to add one
continue; if (path.equals(lastAddedFile))
continue;
lastAddedFile = path;
lastAddedFile = path;
if (fTree != null) {
// create a new DirCacheEntry with data retrieved from disk if (fTree != null) {
final DirCacheEntry dcEntry = new DirCacheEntry(path); // create a new DirCacheEntry with data retrieved from
long entryLength = fTree.getEntryLength(); // disk
dcEntry.setLength(entryLength); final DirCacheEntry dcEntry = new DirCacheEntry(path);
dcEntry.setLastModified(fTree.getEntryLastModified()); long entryLength = fTree.getEntryLength();
dcEntry.setFileMode(fTree.getIndexFileMode(dcTree)); dcEntry.setLength(entryLength);
dcEntry.setLastModified(fTree.getEntryLastModified());
boolean objectExists = (dcTree != null && fTree dcEntry.setFileMode(fTree.getIndexFileMode(dcTree));
.idEqual(dcTree))
|| (hTree != null && fTree.idEqual(hTree)); boolean objectExists = (dcTree != null
if (objectExists) { && fTree.idEqual(dcTree))
dcEntry.setObjectId(fTree.getEntryObjectId()); || (hTree != null && fTree.idEqual(hTree));
} else { if (objectExists) {
if (FileMode.GITLINK.equals(dcEntry.getFileMode()))
dcEntry.setObjectId(fTree.getEntryObjectId()); dcEntry.setObjectId(fTree.getEntryObjectId());
else { } else {
// insert object if (FileMode.GITLINK.equals(dcEntry.getFileMode()))
if (inserter == null) dcEntry.setObjectId(fTree.getEntryObjectId());
inserter = repo.newObjectInserter(); else {
long contentLength = fTree.getEntryContentLength(); // insert object
InputStream inputStream = fTree.openEntryStream(); if (inserter == null)
try { inserter = repo.newObjectInserter();
dcEntry.setObjectId(inserter.insert( long contentLength = fTree
Constants.OBJ_BLOB, contentLength, .getEntryContentLength();
inputStream)); InputStream inputStream = fTree
} finally { .openEntryStream();
inputStream.close(); try {
dcEntry.setObjectId(inserter.insert(
Constants.OBJ_BLOB, contentLength,
inputStream));
} finally {
inputStream.close();
}
} }
} }
// add to existing index
existingBuilder.add(dcEntry);
// add to temporary in-core index
tempBuilder.add(dcEntry);
if (emptyCommit
&& (hTree == null || !hTree.idEqual(fTree)
|| hTree.getEntryRawMode() != fTree
.getEntryRawMode()))
// this is a change
emptyCommit = false;
} else {
// if no file exists on disk, neither add it to
// index nor to temporary in-core index
if (emptyCommit && hTree != null)
// this is a change
emptyCommit = false;
} }
// add to existing index // keep track of processed path
existingBuilder.add(dcEntry); onlyProcessed[pos] = true;
// add to temporary in-core index
tempBuilder.add(dcEntry);
if (emptyCommit
&& (hTree == null || !hTree.idEqual(fTree) || hTree
.getEntryRawMode() != fTree
.getEntryRawMode()))
// this is a change
emptyCommit = false;
} else { } else {
// if no file exists on disk, neither add it to // add entries from HEAD for all other paths
// index nor to temporary in-core index if (hTree != null) {
// create a new DirCacheEntry with data retrieved from
if (emptyCommit && hTree != null) // HEAD
// this is a change final DirCacheEntry dcEntry = new DirCacheEntry(path);
emptyCommit = false; dcEntry.setObjectId(hTree.getEntryObjectId());
} dcEntry.setFileMode(hTree.getEntryFileMode());
// add to temporary in-core index
tempBuilder.add(dcEntry);
}
// keep track of processed path // preserve existing entry in index
onlyProcessed[pos] = true; if (dcTree != null)
} else { existingBuilder.add(dcTree.getDirCacheEntry());
// add entries from HEAD for all other paths
if (hTree != null) {
// create a new DirCacheEntry with data retrieved from HEAD
final DirCacheEntry dcEntry = new DirCacheEntry(path);
dcEntry.setObjectId(hTree.getEntryObjectId());
dcEntry.setFileMode(hTree.getEntryFileMode());
// add to temporary in-core index
tempBuilder.add(dcEntry);
} }
// preserve existing entry in index
if (dcTree != null)
existingBuilder.add(dcTree.getDirCacheEntry());
} }
} }

Loading…
Cancel
Save