From e6eedc31d5c3d448d000eecb678ce891a4800b54 Mon Sep 17 00:00:00 2001 From: Jon Seymour Date: Tue, 24 Aug 2010 14:52:42 +1000 Subject: [PATCH 1/4] rev-parse: exit with non-zero status if ref@{n} is not valid. "The current behaviour of ref@{...} syntax parser is suboptimal: $ git rev-parse --verify jch@{99999} && echo true warning: Log for 'jch' only has 1368 entries. cfb88e9a8d4926b0011ae2dd67e1f57a98f4b768 true It even knows that it is running off the cut-off point; it should just cause the caller to notice that fact. I don't think changing it to error out should cause any harm to existing callers." With this change: $ git rev-parse --verify jch@{99999} || echo false fatal: Log for 'jch' only has 1368 entries. false $ git rev-parse jch@{99999} || echo false fatal: Log for 'jch' only has 1368 entries. false Signed-off-by: Jon Seymour Signed-off-by: Junio C Hamano --- sha1_name.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/sha1_name.c b/sha1_name.c index 4f2af8da93..90ad430a86 100644 --- a/sha1_name.c +++ b/sha1_name.c @@ -430,10 +430,11 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1) "warning: Log for '%.*s' only goes " "back to %s.\n", len, str, show_date(co_time, co_tz, DATE_RFC2822)); - else - fprintf(stderr, - "warning: Log for '%.*s' only has " - "%d entries.\n", len, str, co_cnt); + else { + free(real_ref); + die("Log for '%.*s' only has %d entries.", + len, str, co_cnt); + } } } From eedce784a7492faa370fd96e372c948cba087965 Mon Sep 17 00:00:00 2001 From: Jon Seymour Date: Tue, 24 Aug 2010 14:52:43 +1000 Subject: [PATCH 2/4] sha1_name.c: use warning in preference to fprintf(stderr This commit changes sha1_name.c to use warning instead of fprintf(stderr). Trailing newlines from message formats have been removed since warning adds one itself. Signed-off-by: Jon Seymour Signed-off-by: Junio C Hamano --- sha1_name.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/sha1_name.c b/sha1_name.c index 90ad430a86..13209dc5b8 100644 --- a/sha1_name.c +++ b/sha1_name.c @@ -342,7 +342,7 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1); static int get_sha1_basic(const char *str, int len, unsigned char *sha1) { - static const char *warning = "warning: refname '%.*s' is ambiguous.\n"; + static const char *warn_msg = "refname '%.*s' is ambiguous."; char *real_ref = NULL; int refs_found = 0; int at, reflog_len; @@ -390,7 +390,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1) return -1; if (warn_ambiguous_refs && refs_found > 1) - fprintf(stderr, warning, len, str); + warning(warn_msg, len, str); if (reflog_len) { int nth, i; @@ -426,9 +426,8 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1) if (read_ref_at(real_ref, at_time, nth, sha1, NULL, &co_time, &co_tz, &co_cnt)) { if (at_time) - fprintf(stderr, - "warning: Log for '%.*s' only goes " - "back to %s.\n", len, str, + warning("Log for '%.*s' only goes " + "back to %s.", len, str, show_date(co_time, co_tz, DATE_RFC2822)); else { free(real_ref); From 9c46c054ae426cf1d064f651615ce699d038d276 Mon Sep 17 00:00:00 2001 From: Jon Seymour Date: Tue, 24 Aug 2010 14:52:44 +1000 Subject: [PATCH 3/4] rev-parse: tests git rev-parse --verify master@{n}, for various n This commit introduces tests that verify that rev-parse parses master@{n} correctly for various values of n less than, equal to and greater than the number of revisions in the reference log. In particular, these tests check that rev-parse exits with a non-zero status code and prints a message of the following form to stderr. fatal: Log for [^ ]* only has [0-9][0-9]* entries. Signed-off-by: Jon Seymour Signed-off-by: Junio C Hamano --- t/t1503-rev-parse-verify.sh | 11 +++++++++++ t/t1506-rev-parse-diagnosis.sh | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/t/t1503-rev-parse-verify.sh b/t/t1503-rev-parse-verify.sh index cc65394947..61092f70f1 100755 --- a/t/t1503-rev-parse-verify.sh +++ b/t/t1503-rev-parse-verify.sh @@ -104,4 +104,15 @@ test_expect_success 'use --default' ' test_must_fail git rev-parse --verify --default bar ' +test_expect_success 'master@{n} for various n' ' + N=$(git reflog | wc -l) && + Nm1=$((N-1)) && + Np1=$((N+1)) && + git rev-parse --verify master@{0} && + git rev-parse --verify master@{1} && + git rev-parse --verify master@{$Nm1} && + test_must_fail "git rev-parse --verify master@{$N}" && + test_must_fail "git rev-parse --verify master@{$Np1}" +' + test_done diff --git a/t/t1506-rev-parse-diagnosis.sh b/t/t1506-rev-parse-diagnosis.sh index af721f9719..0eeeb0e450 100755 --- a/t/t1506-rev-parse-diagnosis.sh +++ b/t/t1506-rev-parse-diagnosis.sh @@ -66,4 +66,13 @@ test_expect_success 'incorrect file in :path and :N:path' ' grep "fatal: Path '"'"'disk-only.txt'"'"' exists on disk, but not in the index." error ' +test_expect_success 'invalid @{n} reference' ' + test_must_fail git rev-parse master@{99999} >output 2>error && + test -z "$(cat output)" && + grep "fatal: Log for [^ ]* only has [0-9][0-9]* entries." error && + test_must_fail git rev-parse --verify master@{99999} >output 2>error && + test -z "$(cat output)" && + grep "fatal: Log for [^ ]* only has [0-9][0-9]* entries." error +' + test_done From ba9eab7bfdcb83ad67f4714507599f378a91b17a Mon Sep 17 00:00:00 2001 From: Jon Seymour Date: Wed, 1 Sep 2010 00:49:19 +1000 Subject: [PATCH 4/4] t1503: fix broken test_must_fail calls Some tests in maint-reflog-beyond-horizon are calling test_must_fail in such a way that the arguments to test_must_fail do, indeed, fail but not in the manner expected by the test. This patch removes the unnecessary and unhelpful double quotes. Signed-off-by: Jon Seymour Signed-off-by: Junio C Hamano --- t/t1503-rev-parse-verify.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/t/t1503-rev-parse-verify.sh b/t/t1503-rev-parse-verify.sh index 61092f70f1..100f857b16 100755 --- a/t/t1503-rev-parse-verify.sh +++ b/t/t1503-rev-parse-verify.sh @@ -111,8 +111,8 @@ test_expect_success 'master@{n} for various n' ' git rev-parse --verify master@{0} && git rev-parse --verify master@{1} && git rev-parse --verify master@{$Nm1} && - test_must_fail "git rev-parse --verify master@{$N}" && - test_must_fail "git rev-parse --verify master@{$Np1}" + test_must_fail git rev-parse --verify master@{$N} && + test_must_fail git rev-parse --verify master@{$Np1} ' test_done