From 7a22cf013fa57e44ecc4050c8560f92843ed631a Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Wed, 3 Sep 2008 01:51:42 +0200 Subject: [PATCH] Improve performance and remove race conditions during commit Remove calls to reset() and dispose() after walking revisions or trees. If they are no longer needed the calls will mainly cause more memory to be allocated. Create the RefUpdate object before making the commit so that there will not be a later race condition when the branch reference is updated. Thanks to Shawn O. Spearce for the advices. --- src/org/gitscm/nbjgit/util/GitCommand.java | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/org/gitscm/nbjgit/util/GitCommand.java b/src/org/gitscm/nbjgit/util/GitCommand.java index cfcbc79..3eef7c4 100644 --- a/src/org/gitscm/nbjgit/util/GitCommand.java +++ b/src/org/gitscm/nbjgit/util/GitCommand.java @@ -55,7 +55,6 @@ import org.gitscm.nbjgit.Git; import org.gitscm.nbjgit.ui.log.RepositoryRevision; import org.netbeans.api.queries.SharabilityQuery; import org.openide.util.Exceptions; -import org.spearce.jgit.lib.AnyObjectId; import org.spearce.jgit.lib.Commit; import org.spearce.jgit.lib.Constants; import org.spearce.jgit.lib.GitIndex; @@ -135,7 +134,7 @@ public class GitCommand { String relative = toGitPath(getRelative(root, base)); - ObjectId[] ids = {commit.getTree().getId()}; + ObjectId[] ids = { commit.getTree().getId() }; TreeWalk walk = TreeWalk.forPath(repo, relative, ids); ObjectId blobId = walk.getObjectId(0); @@ -147,10 +146,9 @@ public class GitCommand { } FileOutputStream out = new FileOutputStream(tempFile); - out.write(blob.getBytes()); + out.write(blob.getCachedBytes()); out.close(); - walk.reset(); } catch (Exception ex) { // FIXME: Do unload, delete, ... here @@ -218,7 +216,6 @@ public class GitCommand { walk.setRevFilter(RevFilter.NO_MERGES); } catch (IOException ioe) { - walk.dispose(); return null; } @@ -319,10 +316,10 @@ public class GitCommand { writeTreeWithSubTrees(tree); - ObjectId currentHeadId = repo.resolve("HEAD"); + final RefUpdate ru = repo.updateRef(Constants.HEAD); ObjectId[] parentIds; - if (currentHeadId != null) - parentIds = new ObjectId[]{currentHeadId}; + if (ru.getOldObjectId() != null) + parentIds = new ObjectId[] { ru.getOldObjectId() }; else parentIds = new ObjectId[0]; Commit commit = new Commit(repo, parentIds); @@ -336,10 +333,16 @@ public class GitCommand { ObjectWriter writer = new ObjectWriter(repo); commit.setCommitId(writer.writeCommit(commit)); - final RefUpdate ru = repo.updateRef("HEAD"); ru.setNewObjectId(commit.getCommitId()); ru.setRefLogMessage(buildReflogMessage(message), false); - if (ru.forceUpdate() == RefUpdate.Result.LOCK_FAILURE) + ru.update(); + boolean ok; + if (ru.getOldObjectId() != null) + ok = ru.getResult() == RefUpdate.Result.FAST_FORWARD; + else + ok = ru.getResult() == RefUpdate.Result.NEW; + + if (!ok) logger.output("Failed to update " + ru.getName() + " to commit " + commit.getCommitId() + "."); } } @@ -414,7 +417,7 @@ public class GitCommand { } for (RevCommit rev : walk) { - revs.add(new String[]{rev.getShortMessage(), rev.getId().toString()}); + revs.add(new String[] { rev.getShortMessage(), rev.getId().toString() }); if (--limit <= 0) break; } @@ -422,8 +425,6 @@ public class GitCommand { } catch (IOException ioe) { } - walk.dispose(); - return revs; } -- 2.11.4.GIT