Skip to content

Commit cf4403a

Browse files
committed
Merge branch 'cp/textconv-cat-file'
* cp/textconv-cat-file: git-cat-file.txt: Document --textconv t/t8007: test textconv support for cat-file textconv: support for cat_file sha1_name: add get_sha1_with_context()
2 parents 6aa2064 + 9f77fe0 commit cf4403a

File tree

7 files changed

+155
-15
lines changed

7 files changed

+155
-15
lines changed

Documentation/git-cat-file.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ git-cat-file - Provide content or type and size information for repository objec
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git cat-file' (-t | -s | -e | -p | <type>) <object>
12+
'git cat-file' (-t | -s | -e | -p | <type> | --textconv ) <object>
1313
'git cat-file' (--batch | --batch-check) < <list-of-objects>
1414

1515
DESCRIPTION
1616
-----------
1717
In its first form, the command provides the content or the type of an object in
1818
the repository. The type is required unless '-t' or '-p' is used to find the
19-
object type, or '-s' is used to find the object size.
19+
object type, or '-s' is used to find the object size, or '--textconv' is used
20+
(which implies type "blob").
2021

2122
In the second form, a list of objects (separated by linefeeds) is provided on
2223
stdin, and the SHA1, type, and size of each object is printed on stdout.
@@ -51,6 +52,11 @@ OPTIONS
5152
or to ask for a "blob" with <object> being a tag object that
5253
points at it.
5354

55+
--textconv::
56+
Show the content as transformed by a textconv filter. In this case,
57+
<object> has be of the form <treeish>:<path>, or :<path> in order
58+
to apply the filter to the content recorded in the index at <path>.
59+
5460
--batch::
5561
Print the SHA1, type, size, and contents of each object provided on
5662
stdin. May not be combined with any other options or arguments.

builtin.h

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

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

40+
extern int textconv_object(const char *path, const unsigned char *sha1, char **buf, unsigned long *buf_size);
41+
4042
extern int cmd_add(int argc, const char **argv, const char *prefix);
4143
extern int cmd_annotate(int argc, const char **argv, const char *prefix);
4244
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;
@@ -134,6 +137,17 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
134137

135138
/* otherwise just spit out the data */
136139
break;
140+
141+
case 'c':
142+
if (!obj_context.path[0])
143+
die("git cat-file --textconv %s: <object> must be <sha1:path>",
144+
obj_name);
145+
146+
if (!textconv_object(obj_context.path, sha1, &buf, &size))
147+
die("git cat-file --textconv: unable to run textconv on %s",
148+
obj_name);
149+
break;
150+
137151
case 0:
138152
buf = read_object_with_reference(sha1, exp_type, &size, NULL);
139153
break;
@@ -203,11 +217,25 @@ static int batch_objects(int print_contents)
203217
}
204218

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

225+
static int git_cat_file_config(const char *var, const char *value, void *cb)
226+
{
227+
switch (userdiff_config(var, value)) {
228+
case 0:
229+
break;
230+
case -1:
231+
return -1;
232+
default:
233+
return 0;
234+
}
235+
236+
return git_default_config(var, value, cb);
237+
}
238+
211239
int cmd_cat_file(int argc, const char **argv, const char *prefix)
212240
{
213241
int opt = 0, batch = 0;
@@ -220,6 +248,8 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
220248
OPT_SET_INT('e', NULL, &opt,
221249
"exit with zero when there's no error", 'e'),
222250
OPT_SET_INT('p', NULL, &opt, "pretty-print object's content", 'p'),
251+
OPT_SET_INT(0, "textconv", &opt,
252+
"for blob objects, run textconv on object's content", 'c'),
223253
OPT_SET_INT(0, "batch", &batch,
224254
"show info and content of objects fed from the standard input",
225255
BATCH),
@@ -229,7 +259,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
229259
OPT_END()
230260
};
231261

232-
git_config(git_default_config, NULL);
262+
git_config(git_cat_file_config, NULL);
233263

234264
if (argc != 3 && argc != 2)
235265
usage_with_options(cat_file_usage, options);

cache.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,12 +750,23 @@ static inline unsigned int hexval(unsigned char c)
750750
#define MINIMUM_ABBREV 4
751751
#define DEFAULT_ABBREV 7
752752

753+
struct object_context {
754+
unsigned char tree[20];
755+
char path[PATH_MAX];
756+
unsigned mode;
757+
};
758+
753759
extern int get_sha1(const char *str, unsigned char *sha1);
754760
extern int get_sha1_with_mode_1(const char *str, unsigned char *sha1, unsigned *mode, int gently, const char *prefix);
755761
static inline int get_sha1_with_mode(const char *str, unsigned char *sha1, unsigned *mode)
756762
{
757763
return get_sha1_with_mode_1(str, sha1, mode, 1, NULL);
758764
}
765+
extern int get_sha1_with_context_1(const char *name, unsigned char *sha1, struct object_context *orc, int gently, const char *prefix);
766+
static inline int get_sha1_with_context(const char *str, unsigned char *sha1, struct object_context *orc)
767+
{
768+
return get_sha1_with_context_1(str, sha1, orc, 1, NULL);
769+
}
759770
extern int get_sha1_hex(const char *hex, unsigned char *sha1);
760771
extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
761772
extern int read_ref(const char *filename, unsigned char *sha1);

sha1_name.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -939,8 +939,8 @@ int interpret_branch_name(const char *name, struct strbuf *buf)
939939
*/
940940
int get_sha1(const char *name, unsigned char *sha1)
941941
{
942-
unsigned unused;
943-
return get_sha1_with_mode(name, sha1, &unused);
942+
struct object_context unused;
943+
return get_sha1_with_context(name, sha1, &unused);
944944
}
945945

946946
/* Must be called only when object_name:filename doesn't exist. */
@@ -1037,12 +1037,24 @@ static void diagnose_invalid_index_path(int stage,
10371037

10381038

10391039
int get_sha1_with_mode_1(const char *name, unsigned char *sha1, unsigned *mode, int gently, const char *prefix)
1040+
{
1041+
struct object_context oc;
1042+
int ret;
1043+
ret = get_sha1_with_context_1(name, sha1, &oc, gently, prefix);
1044+
*mode = oc.mode;
1045+
return ret;
1046+
}
1047+
1048+
int get_sha1_with_context_1(const char *name, unsigned char *sha1,
1049+
struct object_context *oc,
1050+
int gently, const char *prefix)
10401051
{
10411052
int ret, bracket_depth;
10421053
int namelen = strlen(name);
10431054
const char *cp;
10441055

1045-
*mode = S_IFINVALID;
1056+
memset(oc, 0, sizeof(*oc));
1057+
oc->mode = S_IFINVALID;
10461058
ret = get_sha1_1(name, namelen, sha1);
10471059
if (!ret)
10481060
return ret;
@@ -1065,6 +1077,11 @@ int get_sha1_with_mode_1(const char *name, unsigned char *sha1, unsigned *mode,
10651077
cp = name + 3;
10661078
}
10671079
namelen = namelen - (cp - name);
1080+
1081+
strncpy(oc->path, cp,
1082+
sizeof(oc->path));
1083+
oc->path[sizeof(oc->path)-1] = '\0';
1084+
10681085
if (!active_cache)
10691086
read_cache();
10701087
pos = cache_name_pos(cp, namelen);
@@ -1077,7 +1094,6 @@ int get_sha1_with_mode_1(const char *name, unsigned char *sha1, unsigned *mode,
10771094
break;
10781095
if (ce_stage(ce) == stage) {
10791096
hashcpy(sha1, ce->sha1);
1080-
*mode = ce->ce_mode;
10811097
return 0;
10821098
}
10831099
pos++;
@@ -1104,12 +1120,17 @@ int get_sha1_with_mode_1(const char *name, unsigned char *sha1, unsigned *mode,
11041120
}
11051121
if (!get_sha1_1(name, cp-name, tree_sha1)) {
11061122
const char *filename = cp+1;
1107-
ret = get_tree_entry(tree_sha1, filename, sha1, mode);
1123+
ret = get_tree_entry(tree_sha1, filename, sha1, &oc->mode);
11081124
if (!gently) {
11091125
diagnose_invalid_sha1_path(prefix, filename,
11101126
tree_sha1, object_name);
11111127
free(object_name);
11121128
}
1129+
hashcpy(oc->tree, tree_sha1);
1130+
strncpy(oc->path, filename,
1131+
sizeof(oc->path));
1132+
oc->path[sizeof(oc->path)-1] = '\0';
1133+
11131134
return ret;
11141135
} else {
11151136
if (!gently)

t/t8007-cat-file-textconv.sh

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/sh
2+
3+
test_description='git cat-file textconv support'
4+
. ./test-lib.sh
5+
6+
cat >helper <<'EOF'
7+
#!/bin/sh
8+
sed 's/^/converted: /' "$@"
9+
EOF
10+
chmod +x helper
11+
12+
test_expect_success 'setup ' '
13+
echo test >one.bin &&
14+
git add . &&
15+
GIT_AUTHOR_NAME=Number1 git commit -a -m First --date="2010-01-01 18:00:00" &&
16+
echo test version 2 >one.bin &&
17+
GIT_AUTHOR_NAME=Number2 git commit -a -m Second --date="2010-01-01 20:00:00"
18+
'
19+
20+
cat >expected <<EOF
21+
fatal: git cat-file --textconv: unable to run textconv on :one.bin
22+
EOF
23+
24+
test_expect_success 'no filter specified' '
25+
git cat-file --textconv :one.bin 2>result
26+
test_cmp expected result
27+
'
28+
29+
test_expect_success 'setup textconv filters' '
30+
echo "*.bin diff=test" >.gitattributes &&
31+
git config diff.test.textconv ./helper &&
32+
git config diff.test.cachetextconv false
33+
'
34+
35+
cat >expected <<EOF
36+
test version 2
37+
EOF
38+
39+
test_expect_success 'cat-file without --textconv' '
40+
git cat-file blob :one.bin >result &&
41+
test_cmp expected result
42+
'
43+
44+
cat >expected <<EOF
45+
test
46+
EOF
47+
48+
test_expect_success 'cat-file without --textconv on previous commit' '
49+
git cat-file -p HEAD^:one.bin >result &&
50+
test_cmp expected result
51+
'
52+
53+
cat >expected <<EOF
54+
converted: test version 2
55+
EOF
56+
57+
test_expect_success 'cat-file --textconv on last commit' '
58+
git cat-file --textconv :one.bin >result &&
59+
test_cmp expected result
60+
'
61+
62+
cat >expected <<EOF
63+
converted: test
64+
EOF
65+
66+
test_expect_success 'cat-file --textconv on previous commit' '
67+
git cat-file --textconv HEAD^:one.bin >result &&
68+
test_cmp expected result
69+
'
70+
test_done

0 commit comments

Comments
 (0)