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

Re: Detached checkout will clobber branch head when using symlink HEAD

From
Jeff King <peff@peff.net>
Date
Oct 16, 2008, 19:17 UTC
Message-ID
<20081016191751.GB14707@coredump.intra.peff.net>
In-Reply-To
<1224095087.5366.19.camel@localhost>
On Wed, Oct 15, 2008 at 02:24:47PM -0400, Matt Draisey wrote:
> If HEAD is a symlink rather than a "ref:" style link this is really
> bad.

Hmm. I don't think we have shipped with a symlink HEAD for quite a long time. Using one obviously doesn't work with detached HEAD, but also would fail with packed refs. I don't know if we ever deprecated it.

Still, this is a pretty nasty effect, in that it kills whatever was in the existing ref. There is a fix below which refuses to lock any ref if it is a symlink and we have requested not to dereference (actually, it is overly cautious -- the symlink flag is triggered when we see a symlink anywhere in the chain, though we need only prevent the situation when the _first_ symref is a symlink. I don't know if it is worth differentiating, since they are both "should never happen anymore" situations, I think).

---
diff --git a/refs.c b/refs.c
index b680750..43568e7 100644
--- a/refs.c
+++ b/refs.c
@@ -447,7 +447,7 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *
 				strcpy(ref_buffer, buffer);
 				ref = ref_buffer;
 				if (flag)
-					*flag |= REF_ISSYMREF;
+					*flag |= REF_ISSYMREF | REF_ISSYMLINK;
 				continue;
 			}
 		}
@@ -817,6 +817,10 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
 		}
 		ref = resolve_ref(orig_ref, lock->old_sha1, mustexist, &type);
 	}
+	if (type & REF_ISSYMLINK && flags & REF_NODEREF) {
+		error("unable to directly lock symbolic link '%s'", orig_ref);
+		goto error_return;
+	}
 	if (type_p)
 	    *type_p = type;
 	if (!ref) {
diff --git a/refs.h b/refs.h
index 06ad260..6356a6a 100644
--- a/refs.h
+++ b/refs.h
@@ -12,6 +12,7 @@ struct ref_lock {
 
 #define REF_ISSYMREF 01
 #define REF_ISPACKED 02
+#define REF_ISSYMLINK 04
 
 /*
  * Calls the specified function for each ref file until it returns nonzero,
Previous: Matt DraiseyNext: Matt Draisey
Message 2 of 15 in “Detached checkout will clobber branch head when using symlink HEAD”
  1. Matt DraiseyOct 15, 2008
  2. Jeff KingOct 16, 2008
  3. Matt DraiseyOct 16, 2008
  4. Nicolas PitreOct 16, 2008
  5. Jeff KingOct 16, 2008
  6. Nicolas PitreOct 16, 2008
  7. Matt DraiseyOct 16, 2008
  8. Nicolas PitreOct 16, 2008
  9. Jeff KingOct 16, 2008
  10. Junio C HamanoOct 17, 2008
  11. 1/3 demonstrate breakage of detached checkout with symbolic link HEADJunio C Hamano, Oct 17, 2008
  12. 2/3 Enhance hold_lock_file_for_{update,append}() APIJunio C Hamano, Oct 17, 2008
  13. 3/3 Fix checkout not to clobber the branch when using symlinked HEAD upon detachingJunio C Hamano, Oct 17, 2008
  14. Jeff KingOct 19, 2008
  15. Junio C HamanoOct 19, 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.