{"thread":{"id":"15028","subject":"[EGIT PATCH] Silently permit invalid ObjectIds during RefUpdate","startedAt":"2008-08-15T17:44:46Z","lastAt":"2008-08-15T17:44:46Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"87330","messageId":"1218822286-4069-1-git-send-email-spearce@spearce.org","threadId":"15028","inReplyTo":null,"subject":"[EGIT PATCH] Silently permit invalid ObjectIds during RefUpdate","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2008-08-15T17:44:46Z","receivedAt":"2008-08-15T17:44:46Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"If we try to parse objects which don't exist it may be because\nwe are trying to force deletion of a ref exactly because the\nobject the ref currently refers to has been pruned from the\nobject database already.  Rather than fail with an exception\nwe should still permit the removal.\n\nThis also fixes the breakage in the unit tests introduced by\nCharles' 5a318367 \"Refactor of RefUpdate force ...\".  One of\nthe unit tests for RefUpdate is using a bad ObjectId as the\nnew value for the ref, and uses forceUpdate to hammer it into\nthe ref database anyway, even though it is not valid as the\nobject does not exist.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n .../src/org/spearce/jgit/lib/RefUpdate.java        |   19 +++++++++++++++++--\n 1 files changed, 17 insertions(+), 2 deletions(-)\n\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/RefUpdate.java b/org.spearce.jgit/src/org/spearce/jgit/lib/RefUpdate.java\nindex 858ba46..ca77b75 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/lib/RefUpdate.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RefUpdate.java\n@@ -40,6 +40,7 @@\n import java.io.File;\n import java.io.IOException;\n \n+import org.spearce.jgit.errors.MissingObjectException;\n import org.spearce.jgit.lib.Ref.Storage;\n import org.spearce.jgit.revwalk.RevCommit;\n import org.spearce.jgit.revwalk.RevObject;\n@@ -345,8 +346,8 @@ private Result updateImpl(final RevWalk walk, final Store store)\n \t\t\tif (oldValue == null)\n \t\t\t\treturn store.store(lock, Result.NEW);\n \n-\t\t\tnewObj = walk.parseAny(newValue);\n-\t\t\toldObj = walk.parseAny(oldValue);\n+\t\t\tnewObj = safeParse(walk, newValue);\n+\t\t\toldObj = safeParse(walk, oldValue);\n \t\t\tif (newObj == oldObj)\n \t\t\t\treturn Result.NO_CHANGE;\n \n@@ -363,6 +364,20 @@ private Result updateImpl(final RevWalk walk, final Store store)\n \t\t}\n \t}\n \n+\tprivate static RevObject safeParse(final RevWalk rw, final AnyObjectId id)\n+\t\t\tthrows IOException {\n+\t\ttry {\n+\t\t\treturn rw.parseAny(id);\n+\t\t} catch (MissingObjectException e) {\n+\t\t\t// We can expect some objects to be missing, like if we are\n+\t\t\t// trying to force a deletion of a branch and the object it\n+\t\t\t// points to has been pruned from the database due to freak\n+\t\t\t// corruption accidents (it happens with 'git new-work-dir').\n+\t\t\t//\n+\t\t\treturn null;\n+\t\t}\n+\t}\n+\n \tprivate Result updateStore(final LockFile lock, final Result status)\n \t\t\tthrows IOException {\n \t\tlock.setNeedStatInformation(true);\n-- \n1.6.0.rc3.250.g8dd0\n"}]}