From ef9d791711147217aa3f99813b20b876a3a1986f Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Fri, 15 Aug 2008 10:43:41 -0700 Subject: [PATCH] Silently permit invalid ObjectIds during RefUpdate If we try to parse objects which don't exist it may be because we are trying to force deletion of a ref exactly because the object the ref currently refers to has been pruned from the object database already. Rather than fail with an exception we should still permit the removal. This also fixes the breakage in the unit tests introduced by Charles' 5a318367 "Refactor of RefUpdate force ...". One of the unit tests for RefUpdate is using a bad ObjectId as the new value for the ref, and uses forceUpdate to hammer it into the ref database anyway, even though it is not valid as the object does not exist. Signed-off-by: Shawn O. Pearce --- .../src/org/spearce/jgit/lib/RefUpdate.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/RefUpdate.java b/org.spearce.jgit/src/org/spearce/jgit/lib/RefUpdate.java index 858ba462..ca77b757 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/lib/RefUpdate.java +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RefUpdate.java @@ -40,6 +40,7 @@ package org.spearce.jgit.lib; import java.io.File; import java.io.IOException; +import org.spearce.jgit.errors.MissingObjectException; import org.spearce.jgit.lib.Ref.Storage; import org.spearce.jgit.revwalk.RevCommit; import org.spearce.jgit.revwalk.RevObject; @@ -345,8 +346,8 @@ public class RefUpdate { if (oldValue == null) return store.store(lock, Result.NEW); - newObj = walk.parseAny(newValue); - oldObj = walk.parseAny(oldValue); + newObj = safeParse(walk, newValue); + oldObj = safeParse(walk, oldValue); if (newObj == oldObj) return Result.NO_CHANGE; @@ -363,6 +364,20 @@ public class RefUpdate { } } + private static RevObject safeParse(final RevWalk rw, final AnyObjectId id) + throws IOException { + try { + return rw.parseAny(id); + } catch (MissingObjectException e) { + // We can expect some objects to be missing, like if we are + // trying to force a deletion of a branch and the object it + // points to has been pruned from the database due to freak + // corruption accidents (it happens with 'git new-work-dir'). + // + return null; + } + } + private Result updateStore(final LockFile lock, final Result status) throws IOException { lock.setNeedStatInformation(true); -- 2.11.4.GIT