Re: [JGIT PATCH 1/4] Ensured that RefUpdate cannot delete current branch
From: Shawn O. Pearce <hidden>
Date: 2016-06-15 22:45:12
Subsystem:
the rest · Maintainer:
Linus Torvalds
Charles O'Farrell [off-list ref] wrote:
If attempted it will return a REJECTED_CURRENT_BRANCH Result.
quoted hunk ↗ jump to hunk
@@ -323,6 +330,9 @@ public Result update(final RevWalk walk) throws IOException { * @throws IOException */ public Result delete() throws IOException { + if (name.substring(Constants.R_HEADS.length()).equals( + db.getRepository().getBranch())) + return Result.REJECTED_CURRENT_BRANCH; try { return updateImpl(new RevWalk(db.getRepository()), new DeleteStore());
I'm squashing this into the patch, as I think its a safer (and faster) way to evaluate what the current branch is. We have a cache in RefDatabase showing the current value of HEAD. We also don't look at .git/head-name, which happens during a bisection. But I also don't think we want to mess around with this test if we are dealing with refs/remotes or refs/tags. Really it is only refs/heads/ that should typically appear in HEAD, so we only need to guard against that case. If the user knows enough to make HEAD point at something else, maybe they will also know enough to not delete the damn thing out from under themselves. If we did fix it to test for all refs, its just a matter of removing the first if.
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 aa2cecb..77dada0 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/RefUpdate.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RefUpdate.java@@ -330,9 +330,12 @@ public Result update(final RevWalk walk) throws IOException { * @throws IOException */ public Result delete() throws IOException { - if (name.substring(Constants.R_HEADS.length()).equals( - db.getRepository().getBranch())) - return Result.REJECTED_CURRENT_BRANCH; + if (name.startsWith(Constants.R_HEADS)) { + final Ref head = db.readRef(Constants.HEAD); + if (head != null && name.equals(head.getName())) + return Result.REJECTED_CURRENT_BRANCH; + } + try { return updateImpl(new RevWalk(db.getRepository()), new DeleteStore());
--
Shawn.