git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 6/7] Added ref deletion to RefUpdate

From
Charles O'Farrell <charleso@charleso.org>
Date
Aug 14, 2008, 10:13 UTC
Message-ID
<1218708829-8175-7-git-send-email-charleso@charleso.org>
In-Reply-To
<1218708829-8175-6-git-send-email-charleso@charleso.org>

This is mixed in with the update code as they as they are common in their general behaviour, differing only in the storing of the changes.

Signed-off-by: Charles O'Farrell <charleso@charleso.org>
---
 .../src/org/spearce/jgit/lib/RefUpdate.java        |   70 ++++++++++++++++++--
 1 files changed, 64 insertions(+), 6 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 1b38fce..e7560e9 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.lib.Ref.Storage;
 import org.spearce.jgit.revwalk.RevCommit;
 import org.spearce.jgit.revwalk.RevObject;
 import org.spearce.jgit.revwalk.RevWalk;
@@ -146,8 +147,11 @@ public class RefUpdate {
 	/** Result of the update operation. */
 	private Result result = Result.NOT_ATTEMPTED;
 
+	private final Ref ref;
+
 	RefUpdate(final RefDatabase r, final Ref ref, final File f) {
 		db = r;
+		this.ref = ref;
 		name = ref.getName();
 		oldValue = ref.getObjectId();
 		looseFile = f;
@@ -304,14 +308,31 @@ public class RefUpdate {
 	public Result update(final RevWalk walk) throws IOException {
 		requireCanDoUpdate();
 		try {
-			return result = updateImpl(walk);
+			return result = updateImpl(walk, new UpdateStore());
+		} catch (IOException x) {
+			result = Result.IO_FAILURE;
+			throw x;
+		}
+	}
+
+	/**
+	 * Delete the ref.
+	 * 
+	 * @return the result status of the delete.
+	 * @throws IOException
+	 */
+	public Result delete() throws IOException {
+		try {
+			return updateImpl(new RevWalk(db.getRepository()),
+					new DeleteStore());
 		} catch (IOException x) {
 			result = Result.IO_FAILURE;
 			throw x;
 		}
 	}
 
-	private Result updateImpl(final RevWalk walk) throws IOException {
+	private Result updateImpl(final RevWalk walk, final Store store)
+			throws IOException {
 		final LockFile lock;
 		RevObject newObj;
 		RevObject oldObj;
@@ -322,12 +343,12 @@ public class RefUpdate {
 		try {
 			oldValue = db.idOf(name);
 			if (oldValue == null)
-				return store(lock, Result.NEW);
+				return store.store(lock, Result.NEW);
 
 			if (isForceUpdate()) {
 				if (oldValue.equals(newValue))
 					return Result.NO_CHANGE;
-				return store(lock, Result.FORCED);
+				return store.store(lock, Result.FORCED);
 			}
 
 			newObj = walk.parseAny(newValue);
@@ -337,7 +358,7 @@ public class RefUpdate {
 
 			if (newObj instanceof RevCommit && oldObj instanceof RevCommit) {
 				if (walk.isMergedInto((RevCommit) oldObj, (RevCommit) newObj))
-					return store(lock, Result.FAST_FORWARD);
+					return store.store(lock, Result.FAST_FORWARD);
 			}
 
 			return Result.REJECTED;
@@ -346,7 +367,7 @@ public class RefUpdate {
 		}
 	}
 
-	private Result store(final LockFile lock, final Result status)
+	private Result updateStore(final LockFile lock, final Result status)
 			throws IOException {
 		lock.setNeedStatInformation(true);
 		lock.write(newValue);
@@ -366,4 +387,41 @@ public class RefUpdate {
 		db.stored(name, newValue, lock.getCommitLastModified());
 		return status;
 	}
+
+	/**
+	 * Handle the abstraction of storing a ref update. This is because both
+	 * updating and deleting of a ref have merge testing in common.
+	 */
+	private abstract class Store {
+		abstract Result store(final LockFile lock, final Result status)
+				throws IOException;
+	}
+
+	private class UpdateStore extends Store {
+
+		@Override
+		Result store(final LockFile lock, final Result status)
+				throws IOException {
+			return updateStore(lock, status);
+		}
+	}
+
+	private class DeleteStore extends Store {
+
+		@Override
+		Result store(LockFile lock, Result status) throws IOException {
+			Storage storage = ref.getStorage();
+			if (storage == Storage.NEW)
+				return status;
+			if (storage.isPacked())
+				db.removePackedRef(ref.getName());
+			if (storage.isLoose())
+				if (!looseFile.delete())
+					throw new IOException("File cannot be deleted: "
+							+ looseFile);
+			new File(db.getRepository().getDirectory(), Constants.LOGS + "/"
+					+ ref.getName()).delete();
+			return status;
+		}
+	}
 }
-- 
1.6.0.rc2.35.g04c6e
Previous: Charles O'FarrellNext: Charles O'Farrell
Message 7 of 11 in “jgit: Branch command now supports deletion”
  1. 0/7 jgit: Branch command now supports deletionCharles O'Farrell, Aug 14, 2008
  2. 1/7 Refactor of WalkRemoteObjectDatabase ref writing into common classCharles O'Farrell, Aug 14, 2008
  3. 2/7 Refactor of RefUpdate force to call common updateImpl instead of duplicationCharles O'Farrell, Aug 14, 2008
  4. 3/7 Minor refactor of constants, including log and ROOT_DIRCharles O'Farrell, Aug 14, 2008
  5. 4/7 Extract lockAndWriteFile method in RefDatabase for reuseCharles O'Farrell, Aug 14, 2008
  6. 5/7 Added removePackedRef method to RefDatabase for packed branch deletionCharles O'Farrell, Aug 14, 2008
  7. 6/7 Added ref deletion to RefUpdateCharles O'Farrell, Aug 14, 2008
  8. 7/7 jgit: Added branch deletion to jgit commandCharles O'Farrell, Aug 14, 2008
  9. Shawn O. PearceAug 14, 2008
  10. 2/7 Refactor of RefUpdate force to call common updateImpl instead of duplicationCharles O'Farrell, Aug 15, 2008
  11. 6/7 Added ref deletion to RefUpdateCharles O'Farrell, Aug 15, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.