Skip to content

Commit 507fe83

Browse files
committedSep 29, 2014
Merge branch 'da/rev-parse-verify-quiet'
"rev-parse --verify --quiet $name" is meant to quietly exit with a non-zero status when $name is not a valid object name, but still gave error messages in some cases. * da/rev-parse-verify-quiet: stash: prefer --quiet over shell redirection of the standard error stream refs: make rev-parse --quiet actually quiet t1503: use test_must_be_empty Documentation: a note about stdout for git rev-parse --verify --quiet
2 parents b8e533f + 1956dfa commit 507fe83

8 files changed

+71
-27
lines changed
 

‎Documentation/git-rev-parse.txt

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ can be used.
114114
Only meaningful in `--verify` mode. Do not output an error
115115
message if the first argument is not a valid object name;
116116
instead exit with non-zero status silently.
117+
SHA-1s for valid object names are printed to stdout on success.
117118

118119
--sq::
119120
Usually the output is made one line per flag and

‎builtin/rev-parse.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
508508
int has_dashdash = 0;
509509
int output_prefix = 0;
510510
unsigned char sha1[20];
511+
unsigned int flags = 0;
511512
const char *name = NULL;
513+
struct object_context unused;
512514

513515
if (argc > 1 && !strcmp("--parseopt", argv[1]))
514516
return cmd_parseopt(argc - 1, argv + 1, prefix);
@@ -596,6 +598,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
596598
}
597599
if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
598600
quiet = 1;
601+
flags |= GET_SHA1_QUIETLY;
599602
continue;
600603
}
601604
if (!strcmp(arg, "--short") ||
@@ -818,7 +821,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
818821
name++;
819822
type = REVERSED;
820823
}
821-
if (!get_sha1(name, sha1)) {
824+
if (!get_sha1_with_context(name, flags, sha1, &unused)) {
822825
if (verify)
823826
revs_count++;
824827
else

‎builtin/show-branch.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
723723
char nth_desc[256];
724724
char *ref;
725725
int base = 0;
726+
unsigned int flags = 0;
726727

727728
if (ac == 0) {
728729
static const char *fake_av[2];
@@ -749,7 +750,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
749750
/* Ah, that is a date spec... */
750751
unsigned long at;
751752
at = approxidate(reflog_base);
752-
read_ref_at(ref, at, -1, sha1, NULL,
753+
read_ref_at(ref, flags, at, -1, sha1, NULL,
753754
NULL, NULL, &base);
754755
}
755756
}
@@ -760,7 +761,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
760761
unsigned long timestamp;
761762
int tz;
762763

763-
if (read_ref_at(ref, 0, base+i, sha1, &logmsg,
764+
if (read_ref_at(ref, flags, 0, base+i, sha1, &logmsg,
764765
&timestamp, &tz, NULL)) {
765766
reflog = i;
766767
break;

‎git-stash.sh

+7-6
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ clear_stash () {
5050
then
5151
die "$(gettext "git stash clear with parameters is unimplemented")"
5252
fi
53-
if current=$(git rev-parse --verify $ref_stash 2>/dev/null)
53+
if current=$(git rev-parse --verify --quiet $ref_stash)
5454
then
5555
git update-ref -d $ref_stash $current
5656
fi
@@ -292,7 +292,7 @@ save_stash () {
292292
}
293293

294294
have_stash () {
295-
git rev-parse --verify $ref_stash >/dev/null 2>&1
295+
git rev-parse --verify --quiet $ref_stash >/dev/null
296296
}
297297

298298
list_stash () {
@@ -392,12 +392,12 @@ parse_flags_and_rev()
392392
;;
393393
esac
394394

395-
REV=$(git rev-parse --quiet --symbolic --verify "$1" 2>/dev/null) || {
395+
REV=$(git rev-parse --symbolic --verify --quiet "$1") || {
396396
reference="$1"
397397
die "$(eval_gettext "\$reference is not a valid reference")"
398398
}
399399

400-
i_commit=$(git rev-parse --quiet --verify "$REV^2" 2>/dev/null) &&
400+
i_commit=$(git rev-parse --verify --quiet "$REV^2") &&
401401
set -- $(git rev-parse "$REV" "$REV^1" "$REV:" "$REV^1:" "$REV^2:" 2>/dev/null) &&
402402
s=$1 &&
403403
w_commit=$1 &&
@@ -409,7 +409,7 @@ parse_flags_and_rev()
409409
test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
410410
IS_STASH_REF=t
411411

412-
u_commit=$(git rev-parse --quiet --verify "$REV^3" 2>/dev/null) &&
412+
u_commit=$(git rev-parse --verify --quiet "$REV^3") &&
413413
u_tree=$(git rev-parse "$REV^3:" 2>/dev/null)
414414
}
415415

@@ -531,7 +531,8 @@ drop_stash () {
531531
die "$(eval_gettext "\${REV}: Could not drop stash entry")"
532532

533533
# clear_stash if we just dropped the last stash entry
534-
git rev-parse --verify "$ref_stash@{0}" >/dev/null 2>&1 || clear_stash
534+
git rev-parse --verify --quiet "$ref_stash@{0}" >/dev/null ||
535+
clear_stash
535536
}
536537

537538
apply_to_branch () {

‎refs.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -3159,7 +3159,7 @@ static int read_ref_at_ent_oldest(unsigned char *osha1, unsigned char *nsha1,
31593159
return 1;
31603160
}
31613161

3162-
int read_ref_at(const char *refname, unsigned long at_time, int cnt,
3162+
int read_ref_at(const char *refname, unsigned int flags, unsigned long at_time, int cnt,
31633163
unsigned char *sha1, char **msg,
31643164
unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt)
31653165
{
@@ -3177,8 +3177,12 @@ int read_ref_at(const char *refname, unsigned long at_time, int cnt,
31773177

31783178
for_each_reflog_ent_reverse(refname, read_ref_at_ent, &cb);
31793179

3180-
if (!cb.reccnt)
3181-
die("Log for %s is empty.", refname);
3180+
if (!cb.reccnt) {
3181+
if (flags & GET_SHA1_QUIETLY)
3182+
exit(128);
3183+
else
3184+
die("Log for %s is empty.", refname);
3185+
}
31823186
if (cb.found_it)
31833187
return 0;
31843188

‎refs.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, cons
206206
int log_ref_setup(const char *refname, char *logfile, int bufsize);
207207

208208
/** Reads log for the value of ref during at_time. **/
209-
extern int read_ref_at(const char *refname, unsigned long at_time, int cnt,
209+
extern int read_ref_at(const char *refname, unsigned int flags,
210+
unsigned long at_time, int cnt,
210211
unsigned char *sha1, char **msg,
211212
unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt);
212213

‎sha1_name.c

+15-9
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,8 @@ static inline int upstream_mark(const char *string, int len)
432432
static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags);
433433
static int interpret_nth_prior_checkout(const char *name, int namelen, struct strbuf *buf);
434434

435-
static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
435+
static int get_sha1_basic(const char *str, int len, unsigned char *sha1,
436+
unsigned int flags)
436437
{
437438
static const char *warn_msg = "refname '%.*s' is ambiguous.";
438439
static const char *object_name_msg = N_(
@@ -511,7 +512,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
511512
if (!refs_found)
512513
return -1;
513514

514-
if (warn_ambiguous_refs &&
515+
if (warn_ambiguous_refs && !(flags & GET_SHA1_QUIETLY) &&
515516
(refs_found > 1 ||
516517
!get_short_sha1(str, len, tmp_sha1, GET_SHA1_QUIETLY)))
517518
warning(warn_msg, len, str);
@@ -545,7 +546,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
545546
return -1;
546547
}
547548
}
548-
if (read_ref_at(real_ref, at_time, nth, sha1, NULL,
549+
if (read_ref_at(real_ref, flags, at_time, nth, sha1, NULL,
549550
&co_time, &co_tz, &co_cnt)) {
550551
if (!len) {
551552
if (starts_with(real_ref, "refs/heads/")) {
@@ -557,11 +558,16 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
557558
len = 4;
558559
}
559560
}
560-
if (at_time)
561-
warning("Log for '%.*s' only goes "
562-
"back to %s.", len, str,
563-
show_date(co_time, co_tz, DATE_RFC2822));
564-
else {
561+
if (at_time) {
562+
if (!(flags & GET_SHA1_QUIETLY)) {
563+
warning("Log for '%.*s' only goes "
564+
"back to %s.", len, str,
565+
show_date(co_time, co_tz, DATE_RFC2822));
566+
}
567+
} else {
568+
if (flags & GET_SHA1_QUIETLY) {
569+
exit(128);
570+
}
565571
die("Log for '%.*s' only has %d entries.",
566572
len, str, co_cnt);
567573
}
@@ -801,7 +807,7 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned l
801807
if (!ret)
802808
return 0;
803809

804-
ret = get_sha1_basic(name, len, sha1);
810+
ret = get_sha1_basic(name, len, sha1, lookup_flags);
805811
if (!ret)
806812
return 0;
807813

‎t/t1503-rev-parse-verify.sh

+32-5
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,42 @@ test_expect_success 'fails with any bad rev or many good revs' '
7272

7373
test_expect_success 'fails silently when using -q' '
7474
test_must_fail git rev-parse --verify --quiet 2>error &&
75-
test -z "$(cat error)" &&
75+
test_must_be_empty error &&
7676
test_must_fail git rev-parse -q --verify foo 2>error &&
77-
test -z "$(cat error)" &&
77+
test_must_be_empty error &&
7878
test_must_fail git rev-parse --verify -q HEAD bar 2>error &&
79-
test -z "$(cat error)" &&
79+
test_must_be_empty error &&
8080
test_must_fail git rev-parse --quiet --verify baz HEAD 2>error &&
81-
test -z "$(cat error)" &&
81+
test_must_be_empty error &&
8282
test_must_fail git rev-parse -q --verify $HASH2 HEAD 2>error &&
83-
test -z "$(cat error)"
83+
test_must_be_empty error
84+
'
85+
86+
test_expect_success 'fails silently when using -q with deleted reflogs' '
87+
ref=$(git rev-parse HEAD) &&
88+
: >.git/logs/refs/test &&
89+
git update-ref -m "message for refs/test" refs/test "$ref" &&
90+
git reflog delete --updateref --rewrite refs/test@{0} &&
91+
test_must_fail git rev-parse -q --verify refs/test@{0} >error 2>&1 &&
92+
test_must_be_empty error
93+
'
94+
95+
test_expect_success 'fails silently when using -q with not enough reflogs' '
96+
ref=$(git rev-parse HEAD) &&
97+
: >.git/logs/refs/test2 &&
98+
git update-ref -m "message for refs/test2" refs/test2 "$ref" &&
99+
test_must_fail git rev-parse -q --verify refs/test2@{999} >error 2>&1 &&
100+
test_must_be_empty error
101+
'
102+
103+
test_expect_success 'succeeds silently with -q and reflogs that do not go far back enough in time' '
104+
ref=$(git rev-parse HEAD) &&
105+
: >.git/logs/refs/test3 &&
106+
git update-ref -m "message for refs/test3" refs/test3 "$ref" &&
107+
git rev-parse -q --verify refs/test3@{1.year.ago} >actual 2>error &&
108+
test_must_be_empty error &&
109+
echo "$ref" >expect &&
110+
test_cmp expect actual
84111
'
85112

86113
test_expect_success 'no stdout output on error' '

0 commit comments

Comments
 (0)