@@ -20,6 +20,34 @@ struct batch_options {
2020 const char * format ;
2121};
2222
23+ static const char * force_path ;
24+
25+ static int smudge_object (const char * path , unsigned mode , unsigned char * sha1 ,
26+ char * * buf , unsigned long * size )
27+ {
28+ enum object_type type ;
29+
30+ * buf = read_sha1_file (sha1 , & type , size );
31+ if (!* buf )
32+ return error (_ ("cannot read object %s '%s'" ),
33+ sha1_to_hex (sha1 ), path );
34+ if (type != OBJ_BLOB ) {
35+ free (* buf );
36+ return error (_ ("blob expected for %s '%s'" ),
37+ sha1_to_hex (sha1 ), path );
38+ }
39+ if (S_ISREG (mode )) {
40+ struct strbuf strbuf = STRBUF_INIT ;
41+ if (convert_to_working_tree (path , * buf , * size , & strbuf )) {
42+ free (* buf );
43+ * size = strbuf .len ;
44+ * buf = strbuf_detach (& strbuf , NULL );
45+ }
46+ }
47+
48+ return 0 ;
49+ }
50+
2351static int cat_one_file (int opt , const char * exp_type , const char * obj_name ,
2452 int unknown_type )
2553{
@@ -61,12 +89,25 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
6189 case 'e' :
6290 return !has_sha1_file (sha1 );
6391
92+ case 'w' :
93+ if (!force_path && !obj_context .path [0 ])
94+ die ("git cat-file --smudge %s: <object> must be <sha1:path>" ,
95+ obj_name );
96+
97+ if (smudge_object (force_path ? force_path : obj_context .path ,
98+ force_path ? 0100644 : obj_context .mode ,
99+ sha1 , & buf , & size ))
100+ return -1 ;
101+ break ;
102+
64103 case 'c' :
65- if (!obj_context .path [0 ])
104+ if (!force_path && ! obj_context .path [0 ])
66105 die ("git cat-file --textconv %s: <object> must be <sha1:path>" ,
67106 obj_name );
68107
69- if (textconv_object (obj_context .path , obj_context .mode , sha1 , 1 , & buf , & size ))
108+ if (textconv_object (force_path ? force_path : obj_context .path ,
109+ force_path ? 0100644 : obj_context .mode ,
110+ sha1 , 1 , & buf , & size ))
70111 break ;
71112
72113 case 'p' :
@@ -440,7 +481,7 @@ static int batch_objects(struct batch_options *opt)
440481}
441482
442483static const char * const cat_file_usage [] = {
443- N_ ("git cat-file (-t [--allow-unknown-type]|-s [--allow-unknown-type]|-e|-p|<type>|--textconv) <object>" ),
484+ N_ ("git cat-file (-t [--allow-unknown-type]|-s [--allow-unknown-type]|-e|-p|<type>|--textconv|--smudge) [--use-path=<path>] <object>" ),
444485 N_ ("git cat-file (--batch | --batch-check) [--follow-symlinks]" ),
445486 NULL
446487};
@@ -486,6 +527,10 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
486527 OPT_CMDMODE ('p' , NULL , & opt , N_ ("pretty-print object's content" ), 'p' ),
487528 OPT_CMDMODE (0 , "textconv" , & opt ,
488529 N_ ("for blob objects, run textconv on object's content" ), 'c' ),
530+ OPT_CMDMODE (0 , "smudge" , & opt ,
531+ N_ ("for blob objects, run smudge filters on object's content" ), 'w' ),
532+ OPT_STRING (0 , "use-path" , & force_path , N_ ("blob" ),
533+ N_ ("use a specific path for --textconv/--smudge" )),
489534 OPT_BOOL (0 , "allow-unknown-type" , & unknown_type ,
490535 N_ ("allow -s and -t to work with broken/corrupt objects" )),
491536 OPT_BOOL (0 , "buffer" , & batch .buffer_output , N_ ("buffer --batch output" )),
@@ -528,6 +573,11 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
528573 usage_with_options (cat_file_usage , options );
529574 }
530575
576+ if (force_path && opt != 'c' && opt != 'w' ) {
577+ error ("--use-path=<path> needs --textconv or --smudge" );
578+ usage_with_options (cat_file_usage , options );
579+ }
580+
531581 if (batch .buffer_output < 0 )
532582 batch .buffer_output = batch .all_objects ;
533583
0 commit comments