Skip to content

Commit c41a87d

Browse files
davvidgitster
authored andcommitted
refs: make rev-parse --quiet actually quiet
When a reflog is deleted, e.g. when "git stash" clears its stashes, "git rev-parse --verify --quiet" dies: fatal: Log for refs/stash is empty. The reason is that the get_sha1() code path does not allow us to suppress this message. Pass the flags bitfield through get_sha1_with_context() so that read_ref_at() can suppress the message. Use get_sha1_with_context1() instead of get_sha1() in rev-parse so that the --quiet flag is honored. Signed-off-by: David Aguilar <davvid@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 2892dfe commit c41a87d

6 files changed

Lines changed: 58 additions & 16 deletions

File tree

builtin/rev-parse.c

Lines changed: 4 additions & 1 deletion
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

Lines changed: 3 additions & 2 deletions
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;

refs.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3108,7 +3108,7 @@ static int read_ref_at_ent_oldest(unsigned char *osha1, unsigned char *nsha1,
31083108
return 1;
31093109
}
31103110

3111-
int read_ref_at(const char *refname, unsigned long at_time, int cnt,
3111+
int read_ref_at(const char *refname, unsigned int flags, unsigned long at_time, int cnt,
31123112
unsigned char *sha1, char **msg,
31133113
unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt)
31143114
{
@@ -3126,8 +3126,12 @@ int read_ref_at(const char *refname, unsigned long at_time, int cnt,
31263126

31273127
for_each_reflog_ent_reverse(refname, read_ref_at_ent, &cb);
31283128

3129-
if (!cb.reccnt)
3130-
die("Log for %s is empty.", refname);
3129+
if (!cb.reccnt) {
3130+
if (flags & GET_SHA1_QUIETLY)
3131+
exit(128);
3132+
else
3133+
die("Log for %s is empty.", refname);
3134+
}
31313135
if (cb.found_it)
31323136
return 0;
31333137

refs.h

Lines changed: 2 additions & 1 deletion
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

Lines changed: 15 additions & 9 deletions
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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,33 @@ test_expect_success 'fails silently when using -q' '
8383
test_must_be_empty error
8484
'
8585

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
111+
'
112+
86113
test_expect_success 'no stdout output on error' '
87114
test -z "$(git rev-parse --verify)" &&
88115
test -z "$(git rev-parse --verify foo)" &&

0 commit comments

Comments
 (0)