rev-parse --verify: do not output anything on error
Before this patch, when "git rev-parse --verify" was passed at least one good rev and then anything, it would output something for the good rev even if it would latter exit on error. With this patch, we only output something if everything is ok. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Christian Couder committed
May 11, 2008 at 18:28 UTC
dfd1b749befcb581d84722caa6a3af56ce6526a5
2 files changed
+26
-24
builtin-rev-parse.c
+23
-15
@@ -28,8 +28,6 @@ static int symbolic;
28
static int abbrev;
29
static int output_sq;
30
31
-static int revs_count;
32
-
31
/*
32
* Some arguments are relevant "revision" arguments,
33
* others are about output format or other details.
@@ -102,7 +100,6 @@ static void show_rev(int type, const unsigned char *sha1, const char *name)
100
if (!(filter & DO_REVS))
101
return;
102
def = NULL;
105
- revs_count++;
103
104
if (type != show_type)
105
putchar('^');
@@ -150,7 +147,7 @@ static int show_flag(const char *arg)
147
return 0;
148
}
149
153
-static void show_default(void)
150
+static int show_default(void)
151
{
152
const char *s = def;
153
@@ -160,9 +157,10 @@ static void show_default(void)
157
def = NULL;
158
if (!get_sha1(s, sha1)) {
159
show_rev(NORMAL, sha1, s);
163
- return;
160
+ return 1;
161
}
162
}
163
+ return 0;
164
}
165
166
static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
@@ -375,8 +373,9 @@ static void die_no_single_rev(int quiet)
373
374
int cmd_rev_parse(int argc, const char **argv, const char *prefix)
375
{
378
- int i, as_is = 0, verify = 0, quiet = 0;
376
+ int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
377
unsigned char sha1[20];
378
+ const char *name = NULL;
379
380
if (argc > 1 && !strcmp("--parseopt", argv[1]))
381
return cmd_parseopt(argc - 1, argv + 1, prefix);
@@ -568,12 +567,17 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
567
/* Not a flag argument */
568
if (try_difference(arg))
569
continue;
571
- if (!get_sha1(arg, sha1)) {
572
- show_rev(NORMAL, sha1, arg);
573
- continue;
570
+ name = arg;
571
+ type = NORMAL;
572
+ if (*arg == '^') {
573
+ name++;
574
+ type = REVERSED;
575
}
575
- if (*arg == '^' && !get_sha1(arg+1, sha1)) {
576
- show_rev(REVERSED, sha1, arg+1);
576
+ if (!get_sha1(name, sha1)) {
577
+ if (verify)
578
+ revs_count++;
579
+ else
580
+ show_rev(type, sha1, name);
581
continue;
582
}
583
if (verify)
@@ -583,10 +587,14 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
587
continue;
588
verify_filename(prefix, arg);
589
}
586
- if (verify && revs_count == 1)
587
- return 0;
588
- show_default();
589
- if (verify && revs_count != 1)
590
+ if (verify) {
591
+ if (revs_count == 1) {
592
+ show_rev(type, sha1, name);
593
+ return 0;
594
+ } else if (revs_count == 0 && show_default())
595
+ return 0;
596
die_no_single_rev(quiet);
597
+ } else
598
+ show_default();
599
return 0;
600
}
t/t1503-rev-parse-verify.sh
+3
-9
@@ -83,17 +83,11 @@ test_expect_success 'fails silently when using -q' '
83
test -z "$(cat error)"
84
'
85
86
-test_expect_success '1 no stdout output on error' '
86
+test_expect_success 'no stdout output on error' '
87
test -z "$(git rev-parse --verify)" &&
88
test -z "$(git rev-parse --verify foo)" &&
89
- test -z "$(git rev-parse --verify baz HEAD)"
90
-'
91
-
92
-test_expect_failure '2 no stdout output on error' '
93
- test -z "$(git rev-parse --verify HEAD bar)"
94
-'
95
-
96
-test_expect_failure '3 no stdout output on error' '
89
+ test -z "$(git rev-parse --verify baz HEAD)" &&
90
+ test -z "$(git rev-parse --verify HEAD bar)" &&
91
test -z "$(git rev-parse --verify $HASH2 HEAD)"
92
'
93