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

[RFC] get_sha1(): :path and :[0-3]:path to extract from index.

From
Junio C Hamano <junkio@cox.net>
Date
Apr 22, 2006, 00:49 UTC
Message-ID
<7v7j5iph7f.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<Pine.LNX.4.64.0604181627101.3701@g5.osdl.org>
[ NOTE! The reason I put "RFC" in the subject rather than "PATCH" is that 
  I'm not 100% sure this isn't just a "shiny object" of mine rather than a 
  really useful thing to do. What do people think? Have you ever wanted to 
  access individual files in some random revision? Do you think this is 
  useful? I think it's cool and _may_ be useful, but I'm not going to 
  really push this patch. Consider it a throw-away patch unless somebody 
  else finds it intriguing enough.. ]

This is a fairly straightforward patch to allow "get_sha1()" to also have shorthands for blob objects in the current index.

The syntax is very simple and intuitive: you can specify a blob from the current index by simply specifying :<stage>:<path> or :<path>, and get_sha1() will do the SHA1 lookup from the index for you.

You can currently do it with "git ls-files -s <path>" and parsing the output, but that's actually pretty awkward.

With this, you can do something like
	git cat-file blob :3:Makefile

to get the contents of "Makefile" from their version during a conflicted merge.

This does not do trees like <ent>:<path> version does. We could do that by writing out a temporary tree object if we wanted to, but that would smudge the object database, so I refrained from doing that as part of this patch. If we did that,

	git diff-tree :2: :3:
would be an inefficient equivalent to "git diff-stages 2 3".
---
 sha1_name.c |   54 ++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 46 insertions(+), 8 deletions(-)
diff --git a/sha1_name.c b/sha1_name.c
index 345935b..ec5cd2c 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -458,17 +458,55 @@ int get_sha1(const char *name, unsigned 
 {
 	int ret;
 	unsigned unused;
+	int namelen = strlen(name);
+	const char *cp;
 
 	prepare_alt_odb();
-	ret = get_sha1_1(name, strlen(name), sha1);
-	if (ret < 0) {
-		const char *cp = strchr(name, ':');
-		if (cp) {
-			unsigned char tree_sha1[20];
-			if (!get_sha1_1(name, cp-name, tree_sha1))
-				return get_tree_entry(tree_sha1, cp+1, sha1,
-						      &unused);
+	ret = get_sha1_1(name, namelen, sha1);
+	if (!ret)
+		return ret;
+	/* sha1:path --> object name of path in ent sha1
+	 * :path -> object name of path in index
+	 * :[0-3]:path -> object name of path in index at stage
+	 */
+	if (name[0] == ':') {
+		int stage = 0;
+		struct cache_entry *ce;
+		int pos;
+		if (namelen < 3 ||
+		    name[2] != ':' ||
+		    name[1] < '0' || '3' < name[1])
+			cp = name + 1;
+		else {
+			stage = name[1] - '0';
+			cp = name + 3;
 		}
+		namelen = namelen - (cp - name);
+		if (!active_cache)
+			read_cache();
+		if (active_nr < 0)
+			return -1;
+		pos = cache_name_pos(cp, namelen);
+		if (pos < 0)
+			pos = -pos - 1;
+		while (pos < active_nr) {
+			ce = active_cache[pos];
+			if (ce_namelen(ce) != namelen ||
+			    memcmp(ce->name, cp, namelen))
+				break;
+			if (ce_stage(ce) == stage) {
+				memcpy(sha1, ce->sha1, 20);
+				return 0;
+			}
+		}
+		return -1;
+	}
+	cp = strchr(name, ':');
+	if (cp) {
+		unsigned char tree_sha1[20];
+		if (!get_sha1_1(name, cp-name, tree_sha1))
+			return get_tree_entry(tree_sha1, cp+1, sha1,
+					      &unused);
 	}
 	return ret;
 }
-- 
1.3.0.g6082
Previous: Linus TorvaldsNext: Uwe Zeisberger
Message 22 of 24 in “[RFC] get_sha1() shorthands for blob/tree objects”
  1. Linus TorvaldsApr 18, 2006
  2. Martin LanghoffApr 19, 2006
  3. Shawn PearceApr 19, 2006
  4. Ray LehtiniemiApr 19, 2006
  5. Junio C HamanoApr 19, 2006
  6. Linus TorvaldsApr 19, 2006
  7. Linus TorvaldsApr 19, 2006
  8. Andreas EricssonApr 19, 2006
  9. Linus TorvaldsApr 19, 2006
  10. Junio C HamanoApr 19, 2006
  11. Linus TorvaldsApr 19, 2006
  12. Linus TorvaldsApr 19, 2006
  13. Junio C HamanoApr 19, 2006
  14. Linus TorvaldsApr 19, 2006
  15. Junio C HamanoApr 19, 2006
  16. Linus TorvaldsApr 19, 2006
  17. Junio C HamanoApr 19, 2006
  18. Linus TorvaldsApr 19, 2006
  19. Martin LanghoffApr 19, 2006
  20. Linus TorvaldsApr 19, 2006
  21. Linus TorvaldsApr 19, 2006
  22. [RFC] get_sha1(): :path and :[0-3]:path to extract from index.Junio C Hamano, Apr 22, 2006
  23. Uwe ZeisbergerApr 25, 2006
  24. Junio C HamanoApr 25, 2006

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.