git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Derrick Stolee via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com, peff@peff.net, me@ttaylorr.com,
	avarab@gmail.com, christian.couder@gmail.com,
	johannes.schindelin@gmx.de, jrnieder@gmail.com,
	"brian m. carlson" <sandals@crustytoothpaste.net>,
	Robert Coup <robert.coup@koordinates.com>,
	Derrick Stolee <derrickstolee@github.com>,
	Derrick Stolee <derrickstolee@github.com>
Subject: [PATCH v3 2/2] usage: add warn_once() helper for repeated warnings
Date: Wed, 01 Jun 2022 01:16:13 +0000	[thread overview]
Message-ID: <8e29ac807c6a0cf94ea3a44ee3304011c2ad159c.1654046173.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1237.v3.git.1654046173.gitgitgadget@gmail.com>

From: Derrick Stolee <derrickstolee@github.com>

The previous change added a warning when valid_remote() detects
credentials in the URL. Since remotes are validated multiple times per
process, this causes multiple warnings to print.

To avoid these kinds of repeated, advisory warnings, create a new
warn_once() helper that behaves the same as warning(), but only after
formatting the output string and adding it to a strset. If that addition
signals that the string already exists in the strset, then do not print
the warning.

In the case of the credentials in a URL, the existing test demonstrates
this per-process limitation: 'git clone' runs 'git-remote-curl' as a
child process, giving two messages. This is an improvement over the
previous six messages.

Signed-off-by: Derrick Stolee <derrickstolee@github.com>
---
 git-compat-util.h |  1 +
 remote.c          |  2 +-
 t/t5601-clone.sh  |  5 ++++-
 usage.c           | 22 ++++++++++++++++++++++
 4 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/git-compat-util.h b/git-compat-util.h
index 58fd813bd01..776a5f660aa 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -526,6 +526,7 @@ int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
 int error_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
 void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
 void warning_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
+void warn_once(const char *warn, ...) __attribute__((format (printf, 1, 2)));
 
 #ifndef NO_OPENSSL
 #ifdef APPLE_COMMON_CRYPTO
diff --git a/remote.c b/remote.c
index accf08bf51f..72fffd0d968 100644
--- a/remote.c
+++ b/remote.c
@@ -60,7 +60,7 @@ static void check_if_creds_in_url(const char *url)
 		      "<redacted>", 10);
 
 	if (!strcmp("warn", value))
-		warning(_("URL '%s' uses plaintext credentials"), redacted.buf);
+		warn_once(_("URL '%s' uses plaintext credentials"), redacted.buf);
 	if (!strcmp("die", value))
 		die(_("URL '%s' uses plaintext credentials"), redacted.buf);
 
diff --git a/t/t5601-clone.sh b/t/t5601-clone.sh
index cba3553b7c4..6ae3eec9eb6 100755
--- a/t/t5601-clone.sh
+++ b/t/t5601-clone.sh
@@ -75,7 +75,10 @@ test_expect_success 'clone warns or fails when using username:password' '
 	test_must_fail git -c fetch.credentialsInUrl=allow clone https://username:password@localhost attempt1 2>err &&
 	! grep "URL '\''https://username:<redacted>@localhost'\'' uses plaintext credentials" err &&
 	test_must_fail git -c fetch.credentialsInUrl=warn clone https://username:password@localhost attempt1 2>err &&
-	grep "warning: URL '\''https://username:<redacted>@localhost'\'' uses plaintext credentials" err &&
+	grep "warning: URL '\''https://username:<redacted>@localhost'\'' uses plaintext credentials" err >warnings &&
+	# The warning is printed twice, for the two processes:
+	# "git clone" and "git-remote-curl".
+	test_line_count = 2 warnings &&
 	test_must_fail git -c fetch.credentialsInUrl=die clone https://username:password@localhost attempt2 2>err &&
 	grep "fatal: URL '\''https://username:<redacted>@localhost'\'' uses plaintext credentials" err
 '
diff --git a/usage.c b/usage.c
index b738dd178b3..242633c5f8d 100644
--- a/usage.c
+++ b/usage.c
@@ -5,6 +5,7 @@
  */
 #include "git-compat-util.h"
 #include "cache.h"
+#include "strmap.h"
 
 static void vreportf(const char *prefix, const char *err, va_list params)
 {
@@ -287,6 +288,27 @@ void warning(const char *warn, ...)
 	va_end(params);
 }
 
+static struct strset prev_warnings = STRSET_INIT;
+
+void warn_once(const char *warn, ...)
+{
+	char buf[1024];
+	va_list params;
+	va_start(params, warn);
+
+	if (vsnprintf(buf, sizeof(buf), warn, params) >= 0) {
+		if (!strset_add(&prev_warnings, buf)) {
+			va_end(params);
+			return;
+		}
+	}
+	va_end(params);
+
+	va_start(params, warn);
+	warn_routine(warn, params);
+	va_end(params);
+}
+
 /* Only set this, ever, from t/helper/, when verifying that bugs are caught. */
 int BUG_exit_code;
 
-- 
gitgitgadget

  parent reply	other threads:[~2022-06-01  1:16 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-23 18:04 [PATCH] urlmatch: create fetch.credentialsInUrl config Derrick Stolee via GitGitGadget
2022-05-23 19:06 ` Junio C Hamano
2022-05-23 20:31   ` Derrick Stolee
2022-05-23 21:14     ` Junio C Hamano
2022-05-24 11:46     ` Johannes Schindelin
2022-05-24 20:14       ` Derrick Stolee
2022-05-23 20:37   ` Junio C Hamano
2022-05-24 11:51   ` Johannes Schindelin
2022-05-24  8:18 ` Ævar Arnfjörð Bjarmason
2022-05-24 13:50   ` Derrick Stolee
2022-05-24 21:01     ` Ævar Arnfjörð Bjarmason
2022-05-25 14:03       ` Derrick Stolee
2022-05-24 11:42 ` Johannes Schindelin
2022-05-24 20:16   ` Derrick Stolee
2022-05-27 13:27 ` [PATCH v2] " Derrick Stolee via GitGitGadget
2022-05-27 14:22   ` Ævar Arnfjörð Bjarmason
2022-05-27 14:43     ` Derrick Stolee
2022-05-27 18:09   ` Junio C Hamano
2022-05-27 18:40     ` Junio C Hamano
2022-05-30  0:16   ` Junio C Hamano
2022-05-31 13:32     ` Derrick Stolee
2022-06-01  1:16   ` [PATCH v3 0/2] fetch: " Derrick Stolee via GitGitGadget
2022-06-01  1:16     ` [PATCH v3 1/2] remote: " Derrick Stolee via GitGitGadget
2022-06-01 19:19       ` Ævar Arnfjörð Bjarmason
2022-06-02 13:38         ` Derrick Stolee
2022-06-01  1:16     ` Derrick Stolee via GitGitGadget [this message]
2022-06-01 12:29       ` [PATCH v3 2/2] usage: add warn_once() helper for repeated warnings Ævar Arnfjörð Bjarmason
2022-06-01 18:42         ` Derrick Stolee
2022-06-01 19:33           ` Ævar Arnfjörð Bjarmason
2022-06-02 13:43             ` Derrick Stolee
2022-06-01 20:21           ` Junio C Hamano
2022-06-02 14:24             ` Derrick Stolee
2022-06-02 17:53               ` Junio C Hamano
2022-06-01 20:40       ` Junio C Hamano
2022-06-02 17:20     ` [PATCH v4] remote: create fetch.credentialsInUrl config Derrick Stolee via GitGitGadget
2022-06-02 21:20       ` Junio C Hamano
2022-06-03 12:54         ` Derrick Stolee
2022-06-06 15:37           ` Junio C Hamano
2022-06-06 14:36       ` [PATCH v5] " Derrick Stolee via GitGitGadget
2022-06-06 16:34         ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8e29ac807c6a0cf94ea3a44ee3304011c2ad159c.1654046173.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=avarab@gmail.com \
    --cc=christian.couder@gmail.com \
    --cc=derrickstolee@github.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=johannes.schindelin@gmx.de \
    --cc=jrnieder@gmail.com \
    --cc=me@ttaylorr.com \
    --cc=peff@peff.net \
    --cc=robert.coup@koordinates.com \
    --cc=sandals@crustytoothpaste.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).