Skip to content

Commit 4657919

Browse files
committed
cat-file: introduce the --filters option
The --filters option applies the convert_to_working_tree() filter for the path when showing the contents of a regular file blob object. This feature comes in handy when a 3rd-party tool wants to work with the contents of files from past revisions as if they had been checked out, but without detouring via temporary files. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent ac98787 commit 4657919

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

Documentation/git-cat-file.txt

Lines changed: 9 additions & 3 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 | --filters ) <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+
`--filters` 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.
@@ -58,6 +58,12 @@ OPTIONS
5858
order to apply the filter to the content recorded in the index at
5959
<path>.
6060

61+
--filters::
62+
Show the content as converted by the filters configured in
63+
the current working tree for the given <path> (i.e. smudge filters,
64+
end-of-line conversion, etc). In this case, <object> has to be of
65+
the form <tree-ish>:<path>, or :<path>.
66+
6167
--batch::
6268
--batch=<format>::
6369
Print object information and contents for each object provided

builtin/cat-file.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,33 @@ struct batch_options {
2020
const char *format;
2121
};
2222

23+
static int filter_object(const char *path, unsigned mode,
24+
const unsigned char *sha1,
25+
char **buf, unsigned long *size)
26+
{
27+
enum object_type type;
28+
29+
*buf = read_sha1_file(sha1, &type, size);
30+
if (!*buf)
31+
return error(_("cannot read object %s '%s'"),
32+
sha1_to_hex(sha1), path);
33+
if (type != OBJ_BLOB) {
34+
free(*buf);
35+
return error(_("blob expected for %s '%s'"),
36+
sha1_to_hex(sha1), path);
37+
}
38+
if (S_ISREG(mode)) {
39+
struct strbuf strbuf = STRBUF_INIT;
40+
if (convert_to_working_tree(path, *buf, *size, &strbuf)) {
41+
free(*buf);
42+
*size = strbuf.len;
43+
*buf = strbuf_detach(&strbuf, NULL);
44+
}
45+
}
46+
47+
return 0;
48+
}
49+
2350
static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
2451
int unknown_type)
2552
{
@@ -61,6 +88,16 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
6188
case 'e':
6289
return !has_sha1_file(sha1);
6390

91+
case 'w':
92+
if (!obj_context.path[0])
93+
die("git cat-file --filters %s: <object> must be "
94+
"<sha1:path>", obj_name);
95+
96+
if (filter_object(obj_context.path, obj_context.mode,
97+
sha1, &buf, &size))
98+
return -1;
99+
break;
100+
64101
case 'c':
65102
if (!obj_context.path[0])
66103
die("git cat-file --textconv %s: <object> must be <sha1:path>",
@@ -440,7 +477,7 @@ static int batch_objects(struct batch_options *opt)
440477
}
441478

442479
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>"),
480+
N_("git cat-file (-t [--allow-unknown-type]|-s [--allow-unknown-type]|-e|-p|<type>|--textconv|--filters) <object>"),
444481
N_("git cat-file (--batch | --batch-check) [--follow-symlinks]"),
445482
NULL
446483
};
@@ -486,6 +523,8 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
486523
OPT_CMDMODE('p', NULL, &opt, N_("pretty-print object's content"), 'p'),
487524
OPT_CMDMODE(0, "textconv", &opt,
488525
N_("for blob objects, run textconv on object's content"), 'c'),
526+
OPT_CMDMODE(0, "filters", &opt,
527+
N_("for blob objects, run filters on object's content"), 'w'),
489528
OPT_BOOL(0, "allow-unknown-type", &unknown_type,
490529
N_("allow -s and -t to work with broken/corrupt objects")),
491530
OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")),

t/t8010-cat-file-filters.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 --filters converts to worktree version' '
30+
git cat-file --filters HEAD:world.txt >actual &&
31+
has_cr actual
32+
'
33+
34+
test_done

0 commit comments

Comments
 (0)