@matheusbsbs / matheus-bagliano / commits / 7950571ad7
Fork 0

A few more options for git-cat-file

This adds '-e' option to git-cat-file, to test for the existence of the object. This also cleans up the option-parsing in git-cat-file slightly. [jc: HPA version had -n option which did rev-parse --verify; the real value of this patch is the option parsing cleanup.] Signed-off-by: H. Peter Anvin <hpa@zytor.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

H. Peter Anvin committed Dec 3, 2005 at 17:57 UTC 7950571ad75c1c97e5e53626d8342b01b167c790
2 files changed +42 -18
Documentation/git-cat-file.txt
+10 -3
@@ -8,7 +8,7 @@ git-cat-file - Provide content or type information for repository objects
8
9 SYNOPSIS
10 --------
11 -'git-cat-file' (-t | -s | <type>) <object>
11 +'git-cat-file' (-t | -s | -e | <type>) <object>
12
13 DESCRIPTION
14 -----------
@@ -29,6 +29,10 @@ OPTIONS
29 Instead of the content, show the object size identified by
30 <object>.
31
32 +-e::
33 + Suppress all output; instead exit with zero status if <object>
34 + exists and is a valid object.
35 +
36 <type>::
37 Typically this matches the real type of <object> but asking
38 for a type that can trivially be dereferenced from the given
@@ -39,8 +43,11 @@ OPTIONS
43
44 OUTPUT
45 ------
42 -If '-t' is specified, one of the <type>. If '-s' is specified,
43 -the size of the <object> in bytes.
46 +If '-t' is specified, one of the <type>.
47 +
48 +If '-s' is specified, the size of the <object> in bytes.
49 +
50 +If '-e' is specified, no output.
51
52 Otherwise the raw (though uncompressed) contents of the <object> will
53 be returned.
cat-file.c
+32 -15
@@ -11,27 +11,44 @@ int main(int argc, char **argv)
11 char type[20];
12 void *buf;
13 unsigned long size;
14 + int opt;
15
16 setup_git_directory();
17 if (argc != 3 || get_sha1(argv[2], sha1))
17 - usage("git-cat-file [-t | -s | <type>] <sha1>");
18 -
19 - if (!strcmp("-t", argv[1]) || !strcmp("-s", argv[1])) {
20 - if (!sha1_object_info(sha1, type,
21 - argv[1][1] == 's' ? &size : NULL)) {
22 - switch (argv[1][1]) {
23 - case 't':
24 - printf("%s\n", type);
25 - break;
26 - case 's':
27 - printf("%lu\n", size);
28 - break;
29 - }
18 + usage("git-cat-file [-t|-s|-e|<type>] <sha1>");
19 +
20 + opt = 0;
21 + if ( argv[1][0] == '-' ) {
22 + opt = argv[1][1];
23 + if ( !opt || argv[1][2] )
24 + opt = -1; /* Not a single character option */
25 + }
26 +
27 + buf = NULL;
28 + switch (opt) {
29 + case 't':
30 + if (!sha1_object_info(sha1, type, NULL)) {
31 + printf("%s\n", type);
32 return 0;
33 }
32 - buf = NULL;
33 - } else {
34 + break;
35 +
36 + case 's':
37 + if (!sha1_object_info(sha1, type, &size)) {
38 + printf("%lu\n", size);
39 + return 0;
40 + }
41 + break;
42 +
43 + case 'e':
44 + return !has_sha1_file(sha1);
45 +
46 + case 0:
47 buf = read_object_with_reference(sha1, argv[1], &size, NULL);
48 + break;
49 +
50 + default:
51 + die("git-cat-file: unknown option: %s\n", argv[1]);
52 }
53
54 if (!buf)