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

[PATCH v2 1/4] sha1_name: add get_sha1_with_context()

From
CPClément Poulain <clement.poulain@ensimag.imag.fr>
Date
Jun 8, 2010, 13:49 UTC
Message-ID
<1276004958-13540-2-git-send-email-clement.poulain@ensimag.imag.fr>
In-Reply-To
<1276004958-13540-1-git-send-email-clement.poulain@ensimag.imag.fr>

Textconv is defined by the diff driver, which is associated with a pathname, not a blob. This fonction permits to know the context for the sha1 you're looking for, especially his pathname

Signed-off-by: Clément Poulain <clement.poulain@ensimag.imag.fr>
Signed-off-by: Diane Gasselin <diane.gasselin@ensimag.imag.fr> 
Signed-off-by: Axel Bonnet <axel.bonnet@ensimag.imag.fr> 
---
 cache.h     |   11 +++++++++++
 sha1_name.c |   30 +++++++++++++++++++++++++-----
 2 files changed, 36 insertions(+), 5 deletions(-)
diff --git a/cache.h b/cache.h
index 0f4263c..43a8c10 100644
--- a/cache.h
+++ b/cache.h
@@ -730,12 +730,23 @@ static inline unsigned int hexval(unsigned char c)
 #define MINIMUM_ABBREV 4
 #define DEFAULT_ABBREV 7
 
+struct object_context {
+	unsigned char tree[20];
+	char path[PATH_MAX];
+	unsigned mode;
+};
+#define OBJECT_CONTEXT_INIT  { 0, 0, 0 }
+
 extern int get_sha1(const char *str, unsigned char *sha1);
 extern int get_sha1_with_mode_1(const char *str, unsigned char *sha1, unsigned *mode, int gently, const char *prefix);
 static inline int get_sha1_with_mode(const char *str, unsigned char *sha1, unsigned *mode)
 {
 	return get_sha1_with_mode_1(str, sha1, mode, 1, NULL);
 }
+static inline int get_sha1_with_context(const char *str, unsigned char *sha1, struct object_context *orc)
+{
+	return get_sha1_with_context_1(str, sha1, orc, 1, NULL);
+}
 extern int get_sha1_hex(const char *hex, unsigned char *sha1);
 extern char *sha1_to_hex(const unsigned char *sha1);	/* static buffer result! */
 extern int read_ref(const char *filename, unsigned char *sha1);
diff --git a/sha1_name.c b/sha1_name.c
index bf92417..02358f9 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -933,8 +933,8 @@ int interpret_branch_name(const char *name, struct strbuf *buf)
  */
 int get_sha1(const char *name, unsigned char *sha1)
 {
-	unsigned unused;
-	return get_sha1_with_mode(name, sha1, &unused);
+	struct object_context unused;
+	return get_sha1_with_context(name, sha1, &unused);
 }
 
 /* Must be called only when object_name:filename doesn't exist. */
@@ -1032,11 +1032,22 @@ static void diagnose_invalid_index_path(int stage,
 
 int get_sha1_with_mode_1(const char *name, unsigned char *sha1, unsigned *mode, int gently, const char *prefix)
 {
+	struct object_context orc;
+	int ret;
+	ret = get_sha1_with_context_1(name, sha1, &orc, gently, prefix, NULL);
+	*mode = orc.mode;
+	return ret;
+}
+
+int get_sha1_with_context_1(const char *name, unsigned char *sha1,
+			    struct object_context *orc,
+			    int gently, const char *prefix)
+{
 	int ret, bracket_depth;
 	int namelen = strlen(name);
 	const char *cp;
 
-	*mode = S_IFINVALID;
+	orc->mode = S_IFINVALID;
 	ret = get_sha1_1(name, namelen, sha1);
 	if (!ret)
 		return ret;
@@ -1059,6 +1070,11 @@ int get_sha1_with_mode_1(const char *name, unsigned char *sha1, unsigned *mode,
 			cp = name + 3;
 		}
 		namelen = namelen - (cp - name);
+
+		strncpy(orc->path, cp,
+			sizeof(orc->path));
+		orc->path[sizeof(orc->path)] = '\0';
+
 		if (!active_cache)
 			read_cache();
 		pos = cache_name_pos(cp, namelen);
@@ -1071,7 +1087,6 @@ int get_sha1_with_mode_1(const char *name, unsigned char *sha1, unsigned *mode,
 				break;
 			if (ce_stage(ce) == stage) {
 				hashcpy(sha1, ce->sha1);
-				*mode = ce->ce_mode;
 				return 0;
 			}
 			pos++;
@@ -1098,12 +1113,17 @@ int get_sha1_with_mode_1(const char *name, unsigned char *sha1, unsigned *mode,
 		}
 		if (!get_sha1_1(name, cp-name, tree_sha1)) {
 			const char *filename = cp+1;
-			ret = get_tree_entry(tree_sha1, filename, sha1, mode);
+			ret = get_tree_entry(tree_sha1, filename, sha1, &orc->mode);
 			if (!gently) {
 				diagnose_invalid_sha1_path(prefix, filename,
 							   tree_sha1, object_name);
 				free(object_name);
 			}
+			hashcpy(orc->tree, tree_sha1);
+			strncpy(orc->path, filename,
+				sizeof(orc->path));
+			orc->path[sizeof(orc->path)] = '\0';
+
 			return ret;
 		} else {
 			if (!gently)
-- 
1.7.1.202.g79415.dirty
Previous: Clément PoulainNext: Clément Poulain
Message 2 of 10 in “git-gui blame: use textconv”
  1. 0/4 git-gui blame: use textconvClément Poulain, Jun 8, 2010
  2. 1/4 sha1_name: add get_sha1_with_context()Clément Poulain, Jun 8, 2010
  3. 2/4 textconv: support for cat_fileClément Poulain, Jun 8, 2010
  4. 3/4 git gui: use textconv filter for diff and blameClément Poulain, Jun 8, 2010
  5. 4/4 t/t8007: test textconv support for cat-fileClément Poulain, Jun 8, 2010
  6. Matthieu MoyJun 8, 2010
  7. Matthieu MoyJun 8, 2010
  8. Clément PoulainJun 8, 2010
  9. Jeff KingJun 9, 2010
  10. Matthieu MoyJun 9, 2010

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.