Skip to content

Commit 7576a30

Browse files
committed
Merge 'cat-file-smudge'
This topic branch adds support to cat-file to apply the smudge filter (if any) when showing blobs. Signed-off-by: Johannes Schindelin <[email protected]>
2 parents 6de9d8a + a918477 commit 7576a30

File tree

3 files changed

+116
-8
lines changed

3 files changed

+116
-8
lines changed

Documentation/git-cat-file.txt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +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 [--allow-unknown-type]| -s [--allow-unknown-type]| -e | -p | <type> | --textconv ) <object>
12+
'git cat-file' (-t [--allow-unknown-type]| -s [--allow-unknown-type]| -e | -p | <type> | --textconv | --smudge ) [--use-path=<path>] <object>
1313
'git cat-file' (--batch | --batch-check) [--follow-symlinks]
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, or `--textconv` is used
20-
(which implies type "blob").
19+
object type, or `-s` is used to find the object size, or `--textconv` or
20+
`--smudge` is used (which imply type "blob").
2121

2222
In the second form, a list of objects (separated by linefeeds) is provided on
2323
stdin, and the SHA-1, type, and size of each object is printed on stdout.
@@ -54,8 +54,19 @@ OPTIONS
5454

5555
--textconv::
5656
Show the content as transformed by a textconv filter. In this case,
57-
<object> has be of the form <tree-ish>:<path>, or :<path> in order
58-
to apply the filter to the content recorded in the index at <path>.
57+
<object> has to be of the form <tree-ish>:<path>, or :<path> in
58+
order to apply the filter to the content recorded in the index at
59+
<path>.
60+
61+
--smudge::
62+
Show the content as transformed by the smudge filter configured in
63+
the current working tree for the given <path>. In this case,
64+
<object> has to be of the form <tree-ish>:<path>, or :<path>.
65+
66+
--use-path=<path>::
67+
For use with --textconv or --smudge, to allow specifying an object
68+
name and a path separately, e.g. when it is difficult to figure out
69+
the revision from which the blob came.
5970

6071
--batch::
6172
--batch=<format>::

builtin/cat-file.c

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
2351
static 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

442483
static 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

t/t8010-cat-file-filters.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/sh
2+
3+
test_description='git cat-file filters support'
4+
. ./test-lib.sh
5+
6+
test_expect_success 'setup ' '
7+
echo "*.txt eol=crlf diff=txt" >.gitattributes &&
8+
echo "hello" | append_cr >world.txt &&
9+
git add .gitattributes world.txt &&
10+
test_tick &&
11+
git commit -m "Initial commit"
12+
'
13+
14+
has_cr() {
15+
tr '\015' Q <"$1" | grep Q >/dev/null
16+
}
17+
18+
test_expect_success 'no filters with `git show`' '
19+
git show HEAD:world.txt >actual &&
20+
! has_cr actual
21+
22+
'
23+
24+
test_expect_success 'no filters with cat-file' '
25+
git cat-file blob HEAD:world.txt >actual &&
26+
! has_cr actual
27+
'
28+
29+
test_expect_success 'cat-file --smudge converts to worktree version' '
30+
git cat-file --smudge HEAD:world.txt >actual &&
31+
has_cr actual
32+
'
33+
34+
test_expect_success 'cat-file --smudge --use-path=<path> works' '
35+
sha1=$(git rev-parse -q --verify HEAD:world.txt) &&
36+
git cat-file --smudge --use-path=world.txt $sha1 >actual &&
37+
has_cr actual
38+
'
39+
40+
test_expect_success 'cat-file --textconv --use-path=<path> works' '
41+
sha1=$(git rev-parse -q --verify HEAD:world.txt) &&
42+
test_config diff.txt.textconv "tr A-Za-z N-ZA-Mn-za-m <" &&
43+
git cat-file --textconv --use-path=hello.txt $sha1 >rot13 &&
44+
test uryyb = "$(cat rot13)"
45+
'
46+
47+
test_done

0 commit comments

Comments
 (0)