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

[JGIT PATCH 05/11] Don't advertise HEAD from ReceivePack

From
Shawn O. Pearce <spearce@spearce.org>
Date
Jul 11, 2009, 20:19 UTC
Message-ID
<1247343566-19025-6-git-send-email-spearce@spearce.org>
In-Reply-To
<1247343566-19025-5-git-send-email-spearce@spearce.org>

The HEAD ref cannot be pushed to by a client, if it is a detached HEAD the client shouldn't be permitted to change it, if it is a symref to another ref then the client should update the destination ref and not the symref. Instead offer the HEAD ref as a ".have" line, which is an invalid ref that the client can't update but still lets the client know we have its target object reachable.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../org/spearce/jgit/transport/ReceivePack.java    |    5 ++++-
 .../org/spearce/jgit/transport/RefAdvertiser.java  |   15 +++++++++++++++
 2 files changed, 19 insertions(+), 1 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/ReceivePack.java b/org.spearce.jgit/src/org/spearce/jgit/transport/ReceivePack.java
index fd8aa86..1c490af 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/ReceivePack.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/ReceivePack.java
@@ -509,8 +509,11 @@ private void sendAdvertisedRefs() throws IOException {
 		adv.advertiseCapability(CAPABILITY_REPORT_STATUS);
 		if (allowOfsDelta)
 			adv.advertiseCapability(CAPABILITY_OFS_DELTA);
-		refs = db.getAllRefs();
+		refs = new HashMap<String, Ref>(db.getAllRefs());
+		final Ref head = refs.remove(Constants.HEAD);
 		adv.send(refs.values());
+		if (head != null && head.getName() == head.getOrigName())
+			adv.advertiseHave(head.getObjectId());
 		if (adv.isEmpty())
 			adv.advertiseId(ObjectId.zeroId(), "capabilities^{}");
 		pckOut.end();
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/RefAdvertiser.java b/org.spearce.jgit/src/org/spearce/jgit/transport/RefAdvertiser.java
index 245891d..91700da 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/RefAdvertiser.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/RefAdvertiser.java
@@ -95,6 +95,15 @@ void send(final Collection<Ref> refs) throws IOException {
 		}
 	}
 
+	void advertiseHave(AnyObjectId id) throws IOException {
+		RevObject obj = parseAnyOrNull(id);
+		if (obj != null) {
+			advertiseAnyOnce(obj, ".have");
+			if (obj instanceof RevTag)
+				advertiseAnyOnce(((RevTag) obj).getObject(), ".have");
+		}
+	}
+
 	boolean isEmpty() {
 		return first;
 	}
@@ -109,6 +118,12 @@ private RevObject parseAnyOrNull(final AnyObjectId id) {
 		}
 	}
 
+	private void advertiseAnyOnce(final RevObject obj, final String refName)
+			throws IOException {
+		if (!obj.has(ADVERTISED))
+			advertiseAny(obj, refName);
+	}
+
 	private void advertiseAny(final RevObject obj, final String refName)
 			throws IOException {
 		obj.add(ADVERTISED);
-- 
1.6.4.rc0.117.g28cb
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 6 of 14 in “Repository instance caching”
  1. 00/11 Repository instance cachingShawn O. Pearce, Jul 11, 2009
  2. 01/11 Change Daemon to use concurrent collections for exported repositoriesShawn O. Pearce, Jul 11, 2009
  3. 02/11 Make Daemon's exportAll check not require synchronizationShawn O. Pearce, Jul 11, 2009
  4. 03/11 Don't retry ".git" suffix in daemon if already triedShawn O. Pearce, Jul 11, 2009
  5. 04/11 Refactor ref advertisement code from server implementationsShawn O. Pearce, Jul 11, 2009
  6. 05/11 Don't advertise HEAD from ReceivePackShawn O. Pearce, Jul 11, 2009
  7. 06/11 Add a use reference counter to RepositoryShawn O. Pearce, Jul 11, 2009
  8. 07/11 Introduce RepositoryCache to cache handles of Repository objectsShawn O. Pearce, Jul 11, 2009
  9. 08/11 Change Daemon to use RepositoryCacheShawn O. Pearce, Jul 11, 2009
  10. 09/11 Expose the Repository's ObjectDatabase objectShawn O. Pearce, Jul 11, 2009
  11. 10/11 Use cached Repository instances when resolving alternatesShawn O. Pearce, Jul 11, 2009
  12. 11/11 Send .have lines in ReceivePack for alternate repositoriesShawn O. Pearce, Jul 11, 2009
  13. Robin RosenbergJul 21, 2009
  14. Shawn O. PearceJul 21, 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.