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

[JGIT PATCH 10/13] Add compare-and-swap semantics to RefUpdate

From
Shawn O. Pearce <spearce@spearce.org>
Date
Dec 23, 2008, 00:27 UTC
Message-ID
<1229992043-1053-11-git-send-email-spearce@spearce.org>
In-Reply-To
<1229992043-1053-10-git-send-email-spearce@spearce.org>

This permits the caller to set the value it expects to find in the Ref upon obtaining the update lock. If the ref value doesn't match then the update is aborted with Result.LOCK_FAILURE, so the caller can recover gracefully without making unexpected changes.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../src/org/spearce/jgit/lib/RefUpdate.java        |   30 ++++++++++++++++++++
 1 files changed, 30 insertions(+), 0 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 235c2fd..0c9ce91 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/RefUpdate.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RefUpdate.java
@@ -149,6 +149,9 @@
 	/** Old value of the ref, obtained after we lock it. */
 	private ObjectId oldValue;
 
+	/** If non-null, the value {@link #oldValue} must have to continue. */
+	private ObjectId expValue;
+
 	/** Result of the update operation. */
 	private Result result = Result.NOT_ATTEMPTED;
 
@@ -190,6 +193,27 @@ public void setNewObjectId(final AnyObjectId id) {
 	}
 
 	/**
+	 * @return the expected value of the ref after the lock is taken, but before
+	 *         update occurs. Null to avoid the compare and swap test. Use
+	 *         {@link ObjectId#zeroId()} to indicate expectation of a
+	 *         non-existant ref.
+	 */
+	public ObjectId getExpectedOldObjectId() {
+		return expValue;
+	}
+
+	/**
+	 * @param id
+	 *            the expected value of the ref after the lock is taken, but
+	 *            before update occurs. Null to avoid the compare and swap test.
+	 *            Use {@link ObjectId#zeroId()} to indicate expectation of a
+	 *            non-existant ref.
+	 */
+	public void setExpectedOldObjectId(final AnyObjectId id) {
+		expValue = id != null ? id.toObjectId() : null;
+	}
+
+	/**
 	 * Check if this update wants to forcefully change the ref.
 	 * 
 	 * @return true if this update should ignore merge tests.
@@ -370,6 +394,12 @@ private Result updateImpl(final RevWalk walk, final Store store)
 			return Result.LOCK_FAILURE;
 		try {
 			oldValue = db.idOf(getName());
+			if (expValue != null) {
+				final ObjectId o;
+				o = oldValue != null ? oldValue : ObjectId.zeroId();
+				if (!expValue.equals(o))
+					return Result.LOCK_FAILURE;
+			}
 			if (oldValue == null)
 				return store.store(lock, Result.NEW);
 
-- 
1.6.1.rc4.301.g5497a
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 11 of 17 in “Add receive-pack server side support”
  1. 00/13 Add receive-pack server side supportShawn O. Pearce, Dec 23, 2008
  2. 01/13 Fix invalid "double checked locking" in InflaterCacheShawn O. Pearce, Dec 23, 2008
  3. 02/13 Cleanup stupid release of the cached Inflater in IndexPackShawn O. Pearce, Dec 23, 2008
  4. 03/13 Cache an Inflater inside a WindowCursor and reuse it as much as possibleShawn O. Pearce, Dec 23, 2008
  5. 04/13 Make RefDatabase thread-safeShawn O. Pearce, Dec 23, 2008
  6. 05/13 Make PackFile thread-safeShawn O. Pearce, Dec 23, 2008
  7. 06/13 Make Repository thread-safeShawn O. Pearce, Dec 23, 2008
  8. 07/13 Don't open a PackFile multiple times on scanForPacksShawn O. Pearce, Dec 23, 2008
  9. 08/13 Expose RepositoryConfig.getBoolean so applications can use itShawn O. Pearce, Dec 23, 2008
  10. 09/13 Add AnyObjectId.copyTo(StringBuilder)Shawn O. Pearce, Dec 23, 2008
  11. 10/13 Add compare-and-swap semantics to RefUpdateShawn O. Pearce, Dec 23, 2008
  12. 11/13 Allow null new ObjectId during RefUpdate.deleteShawn O. Pearce, Dec 23, 2008
  13. 12/13 Implement the git-receive-pack process in JavaShawn O. Pearce, Dec 23, 2008
  14. 13/13 Add basic git daemon support to publish receive-packShawn O. Pearce, Dec 23, 2008
  15. Robin RosenbergJan 3, 2009
  16. Permit a wider range of repository names in jgit daemon requestsShawn O. Pearce, Jan 5, 2009
  17. Robin RosenbergJan 5, 2009

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.