Skip to contentSkip to conversation

object-name: fix quiet @{u} parsing

PATCH In review Felipe Contreras 2 participants 2 messages Mar 16, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 object-name: fix quiet @{u} parsing

Currently git rev-parse --quiet @{u} is not actually quiet when upstream isn't configured:

fatal: no upstream configured for branch 'foo'

Make it so.

---
 object-name.c                 | 5 +++--
 t/t1507-rev-parse-upstream.sh | 5 +++++
 2 files changed, 8 insertions(+), 2 deletions(-)
object-name.c +3 -2
index 2dd1a0f56e..d9f3a176d8 100644
@@ -898,6 +898,7 @@ static int get_oid_basic(struct repository *r, const char *str, int len,
 	char *real_ref = NULL;
 	int refs_found = 0;
 	int at, reflog_len, nth_prior = 0;
+	int fatal = !(flags & GET_OID_QUIETLY);
 
 	if (len == r->hash_algo->hexsz && !get_oid_hex(str, oid)) {
 		if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) {
@@ -952,11 +953,11 @@ static int get_oid_basic(struct repository *r, const char *str, int len,
 
 	if (!len && reflog_len)
 		/* allow "@{...}" to mean the current branch reflog */
-		refs_found = repo_dwim_ref(r, "HEAD", 4, oid, &real_ref, 0);
+		refs_found = repo_dwim_ref(r, "HEAD", 4, oid, &real_ref, !fatal);
 	else if (reflog_len)
 		refs_found = repo_dwim_log(r, str, len, oid, &real_ref);
 	else
-		refs_found = repo_dwim_ref(r, str, len, oid, &real_ref, 0);
+		refs_found = repo_dwim_ref(r, str, len, oid, &real_ref, !fatal);
 
 	if (!refs_found)
 		return -1;
t/t1507-rev-parse-upstream.sh +5 -0
index c34714ffe3..549eb315a9 100755
@@ -183,6 +183,11 @@ test_expect_success '@{u} error message when no upstream' '
 	test_cmp expect actual
 '
 
+test_expect_success '@{u} silent error when no upstream' '
+	test_must_fail git rev-parse --verify --quiet @{u} 2>actual &&
+	test_must_be_empty actual
+'
+
 test_expect_success 'branch@{u} error message with misspelt branch' '
 	cat >expect <<-EOF &&
 	fatal: no such branch: ${SQ}no-such-branch${SQ}
Signature · 1 line
2.39.2.13.g1fb56cf030
#1 lore raw
Quoted 29 lines · Felipe Contreras

Currently git rev-parse --quiet @{u} is not actually quiet when upstream isn't configured:

fatal: no upstream configured for branch 'foo'

Make it so.

---
 object-name.c                 | 5 +++--
 t/t1507-rev-parse-upstream.sh | 5 +++++
 2 files changed, 8 insertions(+), 2 deletions(-)
object-name.c +1 -0
index 2dd1a0f56e..d9f3a176d8 100644
@@ -898,6 +898,7 @@ static int get_oid_basic(struct repository *r, const char *str, int len,
 	char *real_ref = NULL;
 	int refs_found = 0;
 	int at, reflog_len, nth_prior = 0;
+	int fatal = !(flags & GET_OID_QUIETLY);
 
 	if (len == r->hash_algo->hexsz && !get_oid_hex(str, oid)) {
 		if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) {
@@ -952,11 +953,11 @@ static int get_oid_basic(struct repository *r, const char *str, int len,
 
 	if (!len && reflog_len)
 		/* allow "@{...}" to mean the current branch reflog */

object-name.c static int get_oid_basic(struct repository *r, const char *str, int len, · line 963

Quoted 10 lines
object-name.c +2 -2
-		refs_found = repo_dwim_ref(r, "HEAD", 4, oid, &real_ref, 0);
+		refs_found = repo_dwim_ref(r, "HEAD", 4, oid, &real_ref, !fatal);
 	else if (reflog_len)
 		refs_found = repo_dwim_log(r, str, len, oid, &real_ref);
 	else
-		refs_found = repo_dwim_ref(r, str, len, oid, &real_ref, 0);
+		refs_found = repo_dwim_ref(r, str, len, oid, &real_ref, !fatal);
 
 	if (!refs_found)
 		return -1;

All of the above look sensible.

t/t1507-rev-parse-upstream.sh test_expect_success '@{u} error message when no upstream' ' · line 189

Quoted 12 lines
t/t1507-rev-parse-upstream.sh +4 -0
index c34714ffe3..549eb315a9 100755
@@ -183,6 +183,11 @@ test_expect_success '@{u} error message when no upstream' '
 	test_cmp expect actual
 '
 
+test_expect_success '@{u} silent error when no upstream' '
+	test_must_fail git rev-parse --verify --quiet @{u} 2>actual &&
+	test_must_be_empty actual
+'

OK. This does not check what comes out to the standard output stream at all, and only cares that the standard error stream emits nothing. Which is absolutely the right thing to do. When a command exits non-zero, we do not guarantee what output it emits, and the invoker is expected to check the exit status.

Will queue. Thanks.