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

[PATCH v2 2/4] textconv: support for cat_file

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

Make the textconv_object function public, and add --textconv option to cat-file to perform conversion on blob objects. Using --textconv implies that we are working on a blob. As files drivers need to be initialized, a new config is required in addition to git_default_config. Therefore git_cat_file_config() is introduced

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> 
---
 builtin/blame.c    |    8 ++++----
 builtin/cat-file.c |   32 +++++++++++++++++++++++++++++---
 2 files changed, 33 insertions(+), 7 deletions(-)
diff --git a/builtin/blame.c b/builtin/blame.c
index f831e3a..64605f5 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -91,10 +91,10 @@ struct origin {
  * if the textconv driver exists.
  * Return 1 if the conversion succeeds, 0 otherwise.
  */
-static int textconv_object(const char *path,
-			   const unsigned char *sha1,
-			   char **buf,
-			   size_t *buf_size)
+int textconv_object(const char *path,
+		    const unsigned char *sha1,
+		    char **buf,
+		    size_t *buf_size)
 {
 	struct diff_filespec *df;
 	struct userdiff_driver *textconv;
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index a933eaa..1457340 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -9,6 +9,7 @@
 #include "tree.h"
 #include "builtin.h"
 #include "parse-options.h"
+#include "diff.h"
 
 #define BATCH 1
 #define BATCH_CHECK 2
@@ -86,8 +87,9 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
 	enum object_type type;
 	void *buf;
 	unsigned long size;
+	struct object_context obj_context = OBJECT_CONTEXT_INIT;
 
-	if (get_sha1(obj_name, sha1))
+	if (get_sha1_with_context(obj_name, sha1, &obj_context))
 		die("Not a valid object name %s", obj_name);
 
 	buf = NULL;
@@ -132,6 +134,17 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
 
 		/* otherwise just spit out the data */
 		break;
+
+	case 'c':
+		if (!obj_context.path)
+			die("git cat-file --textconv %s: <object> must be <sha1:path>",
+			    obj_name);
+
+		if(!textconv_object(obj_context.path, sha1, &buf, (size_t *) &size))
+			die("git cat-file --textconv: unable to run textconv on %s",
+			    obj_name);
+		break;
+
 	case 0:
 		buf = read_object_with_reference(sha1, exp_type, &size, NULL);
 		break;
@@ -201,11 +214,22 @@ static int batch_objects(int print_contents)
 }
 
 static const char * const cat_file_usage[] = {
-	"git cat-file (-t|-s|-e|-p|<type>) <object>",
+	"git cat-file (-t|-s|-e|-p|<type>|--textconv) <object>",
 	"git cat-file (--batch|--batch-check) < <list_of_objects>",
 	NULL
 };
 
+static int git_cat_file_config(const char *var, const char *value, void *cb)
+{
+	switch (userdiff_config(var, value)) {
+		case 0: break;
+		case -1: return -1;
+		default: return 0;
+	}
+
+	return git_default_config(var, value, cb);
+}
+
 int cmd_cat_file(int argc, const char **argv, const char *prefix)
 {
 	int opt = 0, batch = 0;
@@ -218,6 +242,8 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
 		OPT_SET_INT('e', NULL, &opt,
 			    "exit with zero when there's no error", 'e'),
 		OPT_SET_INT('p', NULL, &opt, "pretty-print object's content", 'p'),
+		OPT_SET_INT(0, "textconv", &opt,
+				"for blob objects, run textconv on object's content", 'c'),
 		OPT_SET_INT(0, "batch", &batch,
 			    "show info and content of objects fed from the standard input",
 			    BATCH),
@@ -227,7 +253,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
 		OPT_END()
 	};
 
-	git_config(git_default_config, NULL);
+	git_config(git_cat_file_config, NULL);
 
 	if (argc != 3 && argc != 2)
 		usage_with_options(cat_file_usage, options);
-- 
1.7.1.202.g79415.dirty
Previous: Clément PoulainNext: Clément Poulain
Message 3 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.