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; the contents are written out as-is for other types of objects. 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 <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Aug 24, 2016 at 14:23 UTC
b9e62f60115c75c5be5de593862925c8b8d7e683
3 files changed
+78
-4
Documentation/git-cat-file.txt
+9
-3
@@ -9,15 +9,15 @@ git-cat-file - Provide content or type and size information for repository objec
9
SYNOPSIS
10
--------
11
[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>
13
'git cat-file' (--batch | --batch-check) [--follow-symlinks]
14
15
DESCRIPTION
16
-----------
17
In its first form, the command provides the content or the type of an object in
18
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").
21
22
In the second form, a list of objects (separated by linefeeds) is provided on
23
stdin, and the SHA-1, type, and size of each object is printed on stdout.
@@ -58,6 +58,12 @@ OPTIONS
58
order to apply the filter to the content recorded in the index at
59
<path>.
60
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
+
67
--batch::
68
--batch=<format>::
69
Print object information and contents for each object provided
builtin/cat-file.c
+35
-1
@@ -20,6 +20,28 @@ struct batch_options {
20
const char *format;
21
};
22
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) && S_ISREG(mode)) {
34
+ struct strbuf strbuf = STRBUF_INIT;
35
+ if (convert_to_working_tree(path, *buf, *size, &strbuf)) {
36
+ free(*buf);
37
+ *size = strbuf.len;
38
+ *buf = strbuf_detach(&strbuf, NULL);
39
+ }
40
+ }
41
+
42
+ return 0;
43
+}
44
+
45
static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
46
int unknown_type)
47
{
@@ -61,6 +83,16 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
83
case 'e':
84
return !has_sha1_file(sha1);
85
86
+ case 'w':
87
+ if (!obj_context.path[0])
88
+ die("git cat-file --filters %s: <object> must be "
89
+ "<sha1:path>", obj_name);
90
+
91
+ if (filter_object(obj_context.path, obj_context.mode,
92
+ sha1, &buf, &size))
93
+ return -1;
94
+ break;
95
+
96
case 'c':
97
if (!obj_context.path[0])
98
die("git cat-file --textconv %s: <object> must be <sha1:path>",
@@ -440,7 +472,7 @@ static int batch_objects(struct batch_options *opt)
472
}
473
474
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>"),
475
+ N_("git cat-file (-t [--allow-unknown-type]|-s [--allow-unknown-type]|-e|-p|<type>|--textconv|--filters) <object>"),
476
N_("git cat-file (--batch | --batch-check) [--follow-symlinks]"),
477
NULL
478
};
@@ -486,6 +518,8 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
518
OPT_CMDMODE('p', NULL, &opt, N_("pretty-print object's content"), 'p'),
519
OPT_CMDMODE(0, "textconv", &opt,
520
N_("for blob objects, run textconv on object's content"), 'c'),
521
+ OPT_CMDMODE(0, "filters", &opt,
522
+ N_("for blob objects, run filters on object's content"), 'w'),
523
OPT_BOOL(0, "allow-unknown-type", &unknown_type,
524
N_("allow -s and -t to work with broken/corrupt objects")),
525
OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")),
t/t8010-cat-file-filters.sh
new
+34
@@ -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