Skip to content

Commit e5fba60

Browse files
Clément Poulaingitster
authored andcommitted
textconv: support for cat_file
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 <[email protected]> Signed-off-by: Diane Gasselin <[email protected]> Signed-off-by: Axel Bonnet <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 573285e commit e5fba60

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

builtin.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c);
3636

3737
extern int check_pager_config(const char *cmd);
3838

39+
extern int textconv_object(const char *path, const unsigned char *sha1, char **buf, unsigned long *buf_size);
40+
3941
extern int cmd_add(int argc, const char **argv, const char *prefix);
4042
extern int cmd_annotate(int argc, const char **argv, const char *prefix);
4143
extern int cmd_apply(int argc, const char **argv, const char *prefix);

builtin/blame.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ struct origin {
9191
* if the textconv driver exists.
9292
* Return 1 if the conversion succeeds, 0 otherwise.
9393
*/
94-
static int textconv_object(const char *path,
95-
const unsigned char *sha1,
96-
char **buf,
97-
unsigned long *buf_size)
94+
int textconv_object(const char *path,
95+
const unsigned char *sha1,
96+
char **buf,
97+
unsigned long *buf_size)
9898
{
9999
struct diff_filespec *df;
100100
struct userdiff_driver *textconv;

builtin/cat-file.c

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "tree.h"
1010
#include "builtin.h"
1111
#include "parse-options.h"
12+
#include "diff.h"
13+
#include "userdiff.h"
1214

1315
#define BATCH 1
1416
#define BATCH_CHECK 2
@@ -84,10 +86,11 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
8486
{
8587
unsigned char sha1[20];
8688
enum object_type type;
87-
void *buf;
89+
char *buf;
8890
unsigned long size;
91+
struct object_context obj_context;
8992

90-
if (get_sha1(obj_name, sha1))
93+
if (get_sha1_with_context(obj_name, sha1, &obj_context))
9194
die("Not a valid object name %s", obj_name);
9295

9396
buf = NULL;
@@ -132,6 +135,17 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
132135

133136
/* otherwise just spit out the data */
134137
break;
138+
139+
case 'c':
140+
if (!obj_context.path[0])
141+
die("git cat-file --textconv %s: <object> must be <sha1:path>",
142+
obj_name);
143+
144+
if (!textconv_object(obj_context.path, sha1, &buf, &size))
145+
die("git cat-file --textconv: unable to run textconv on %s",
146+
obj_name);
147+
break;
148+
135149
case 0:
136150
buf = read_object_with_reference(sha1, exp_type, &size, NULL);
137151
break;
@@ -201,11 +215,25 @@ static int batch_objects(int print_contents)
201215
}
202216

203217
static const char * const cat_file_usage[] = {
204-
"git cat-file (-t|-s|-e|-p|<type>) <object>",
218+
"git cat-file (-t|-s|-e|-p|<type>|--textconv) <object>",
205219
"git cat-file (--batch|--batch-check) < <list_of_objects>",
206220
NULL
207221
};
208222

223+
static int git_cat_file_config(const char *var, const char *value, void *cb)
224+
{
225+
switch (userdiff_config(var, value)) {
226+
case 0:
227+
break;
228+
case -1:
229+
return -1;
230+
default:
231+
return 0;
232+
}
233+
234+
return git_default_config(var, value, cb);
235+
}
236+
209237
int cmd_cat_file(int argc, const char **argv, const char *prefix)
210238
{
211239
int opt = 0, batch = 0;
@@ -218,6 +246,8 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
218246
OPT_SET_INT('e', NULL, &opt,
219247
"exit with zero when there's no error", 'e'),
220248
OPT_SET_INT('p', NULL, &opt, "pretty-print object's content", 'p'),
249+
OPT_SET_INT(0, "textconv", &opt,
250+
"for blob objects, run textconv on object's content", 'c'),
221251
OPT_SET_INT(0, "batch", &batch,
222252
"show info and content of objects fed from the standard input",
223253
BATCH),
@@ -227,7 +257,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
227257
OPT_END()
228258
};
229259

230-
git_config(git_default_config, NULL);
260+
git_config(git_cat_file_config, NULL);
231261

232262
if (argc != 3 && argc != 2)
233263
usage_with_options(cat_file_usage, options);

0 commit comments

Comments
 (0)