threads / patch / 56642

patch, 5 partsadvice API: simplification, small fixes, readability

Subject: [PATCH 0/5] advice API: simplification, small fixes, readability

## tl;dr

6 messages between Oct 4, 2021 and Oct 4, 2021. Diffs are folded; open one to read it.

replies: 5people: 1as markdown or json

Ævar Arnfjörð Bjarmason· Oct 4, 2021, 01:58 UTC · lore

A follow-up to recent advice.[ch] improvements in fd0d7036e0d (Merge branch 'ab/retire-advice-config', 2021-09-10). There are no in-flight additions of new advice variables, and this doesn't conflict with "seen". So now seems like a good time to submit this.

The API carried some complexity due to needing to alias one of the variables, which goes away in 3/5, 4/5 then further reduces boilerplate.

Ævar Arnfjörð Bjarmason (5):
  advice.h: don't indent top-level enum
  advice output: emit empty lines as "hint:", not "hint: "
  advice.[ch]: remove advice.pushNonFastForward alias
  advice API: revamp the API, again
  advice.[ch]: sort enum fields in alphabetical order
 advice.c                                    | 100 +++++++++-----------
 advice.h                                    |   5 +-
 contrib/mw-to-git/git-remote-mediawiki.perl |   2 +-
 t/t7004-tag.sh                              |   4 +-
 4 files changed, 51 insertions(+), 60 deletions(-)
-- 
2.33.0.1404.g7bcfc82b295
Ævar Arnfjörð Bjarmason· Oct 4, 2021, 01:58 UTC · re: Ævar Arnfjörð Bjarmason · lore

[PATCH 2/5] advice output: emit empty lines as "hint:", not "hint: "

Let's not needlessly add this trailing whitespace to empty "hint:" lines, this makes copy/pasting them better, and also makes the tests more straightforward.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 advice.c       | 9 +++++++--
 t/t7004-tag.sh | 4 ++--
 2 files changed, 9 insertions(+), 4 deletions(-)
Show changes to 2 files +9 −4

advice.c, t/t7004-tag.sh

diff --git a/advice.c b/advice.c
index 2f5499a5e18..9691c7e7899 100644
--- a/advice.c
+++ b/advice.c
@@ -90,10 +90,15 @@ static void vadvise(const char *advice, int display_instructions,
 		strbuf_addf(&buf, turn_off_instructions, key);
 
 	for (cp = buf.buf; *cp; cp = np) {
+		int len;
+
 		np = strchrnul(cp, '\n');
-		fprintf(stderr,	_("%shint: %.*s%s\n"),
+		len = np - cp;
+
+		fprintf(stderr,	_("%shint:%s%.*s%s\n"),
 			advise_get_color(ADVICE_COLOR_HINT),
-			(int)(np - cp), cp,
+			len ? " " : "",
+			len, cp,
 			advise_get_color(ADVICE_COLOR_RESET));
 		if (*np)
 			np++;
diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
index 082be85dffc..a7041e64ccf 100755
--- a/t/t7004-tag.sh
+++ b/t/t7004-tag.sh
@@ -1749,10 +1749,10 @@ test_expect_success '--points-at finds annotated tags of tags' '
 '
 
 test_expect_success 'recursive tagging should give advice' '
-	sed -e "s/|$//" <<-EOF >expect &&
+	cat <<-EOF >expect &&
 	hint: You have created a nested tag. The object referred to by your new tag is
 	hint: already a tag. If you meant to tag the object that it points to, use:
-	hint: |
+	hint:
 	hint: 	git tag -f nested annotated-v4.0^{}
 	hint: Disable this message with "git config advice.nestedTag false"
 	EOF
-- 
2.33.0.1404.g7bcfc82b295
Ævar Arnfjörð Bjarmason· Oct 4, 2021, 01:58 UTC · re: Ævar Arnfjörð Bjarmason · lore

[PATCH 1/5] advice.h: don't indent top-level enum

Fix a style nit introduced in b3b18d16213 (advice: revamp advise API, 2020-03-02) and remove the leading whitespace before "enum".

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 advice.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to advice.h +1 −1
diff --git a/advice.h b/advice.h
index 601265fd107..694219f5cb2 100644
--- a/advice.h
+++ b/advice.h
@@ -12,7 +12,7 @@ struct string_list;
  * Add the new config variable to Documentation/config/advice.txt.
  * Call advise_if_enabled to print your advice.
  */
- enum advice_type {
+enum advice_type {
 	ADVICE_ADD_EMBEDDED_REPO,
 	ADVICE_ADD_EMPTY_PATHSPEC,
 	ADVICE_ADD_IGNORED_FILE,
-- 
2.33.0.1404.g7bcfc82b295
Ævar Arnfjörð Bjarmason· Oct 4, 2021, 01:58 UTC · re: Ævar Arnfjörð Bjarmason · lore

[PATCH 3/5] advice.[ch]: remove advice.pushNonFastForward alias

The advice.pushNonFastForward variable has been primarily known as advice.pushUpdateRejected since 1184564eac8 (push: rename config variable for more general use, 2012-12-02). It's been long enough by far that we can declare this deprecation period over and remove it.

The only downside for users is going to be that they'll potentially see a more verbose warning that they'll have to re-squash by changing their config, but that's hardly an undue burden.

Doing this allows us to simplify the advice_enabled() function down to something truly trivial.

Change the only in-tree user of this, it was added in fd47d7b94db (git-remote-mediawiki: obey advice.pushNonFastForward, 2011-09-27), and not renamed in 1184564eac8 along with the rest.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 advice.c                                    | 12 +-----------
 advice.h                                    |  1 -
 contrib/mw-to-git/git-remote-mediawiki.perl |  2 +-
 3 files changed, 2 insertions(+), 13 deletions(-)
Show changes to 3 files +2 −13

advice.c, advice.h, contrib/mw-to-git/git-remote-mediawiki.perl

diff --git a/advice.c b/advice.c
index 9691c7e7899..b395bc7d187 100644
--- a/advice.c
+++ b/advice.c
@@ -52,10 +52,6 @@ static struct {
 	[ADVICE_PUSH_FETCH_FIRST]			= { "pushFetchFirst", 1 },
 	[ADVICE_PUSH_NEEDS_FORCE]			= { "pushNeedsForce", 1 },
 	[ADVICE_PUSH_REF_NEEDS_UPDATE]			= { "pushRefNeedsUpdate", 1 },
-
-	/* make this an alias for backward compatibility */
-	[ADVICE_PUSH_UPDATE_REJECTED_ALIAS]		= { "pushNonFastForward", 1 },
-
 	[ADVICE_PUSH_NON_FF_CURRENT]			= { "pushNonFFCurrent", 1 },
 	[ADVICE_PUSH_NON_FF_MATCHING]			= { "pushNonFFMatching", 1 },
 	[ADVICE_PUSH_UNQUALIFIED_REF_NAME]		= { "pushUnqualifiedRefName", 1 },
@@ -116,13 +112,7 @@ void advise(const char *advice, ...)
 
 int advice_enabled(enum advice_type type)
 {
-	switch(type) {
-	case ADVICE_PUSH_UPDATE_REJECTED:
-		return advice_setting[ADVICE_PUSH_UPDATE_REJECTED].enabled &&
-		       advice_setting[ADVICE_PUSH_UPDATE_REJECTED_ALIAS].enabled;
-	default:
-		return advice_setting[type].enabled;
-	}
+	return advice_setting[type].enabled;
 }
 
 void advise_if_enabled(enum advice_type type, const char *advice, ...)
diff --git a/advice.h b/advice.h
index 694219f5cb2..ab25c6ba9a0 100644
--- a/advice.h
+++ b/advice.h
@@ -32,7 +32,6 @@ enum advice_type {
 	ADVICE_PUSH_NON_FF_CURRENT,
 	ADVICE_PUSH_NON_FF_MATCHING,
 	ADVICE_PUSH_UNQUALIFIED_REF_NAME,
-	ADVICE_PUSH_UPDATE_REJECTED_ALIAS,
 	ADVICE_PUSH_UPDATE_REJECTED,
 	ADVICE_PUSH_REF_NEEDS_UPDATE,
 	ADVICE_RESET_QUIET_WARNING,
diff --git a/contrib/mw-to-git/git-remote-mediawiki.perl b/contrib/mw-to-git/git-remote-mediawiki.perl
index a5624413dc1..158e89e5048 100755
--- a/contrib/mw-to-git/git-remote-mediawiki.perl
+++ b/contrib/mw-to-git/git-remote-mediawiki.perl
@@ -983,7 +983,7 @@ sub mw_import_revids {
 }
 
 sub error_non_fast_forward {
-	my $advice = run_git_quoted(["config", "--bool", "advice.pushNonFastForward"]);
+	my $advice = run_git_quoted(["config", "--bool", "advice.pushUpdateRejected"]);
 	chomp($advice);
 	if ($advice ne 'false') {
 		# Native git-push would show this after the summary.
-- 
2.33.0.1404.g7bcfc82b295
Ævar Arnfjörð Bjarmason· Oct 4, 2021, 01:58 UTC · re: Ævar Arnfjörð Bjarmason · lore

[PATCH 4/5] advice API: revamp the API, again

Follow-up b3b18d16213 (advice: revamp advise API, 2020-03-02) and revamp the declaration of the advice variables to:

 * Make it trivial to grep for them, by giving them the "advice."
   prefix explicitly, which also saves us from stripping it off
   during config parsing.
 * All of the advice is enabled by default, so we can do this more
   succinctly by having a "disabled" slot, instead of "enabled", and
   then rely on the compiler to implicitly zero out the struct.
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 advice.c | 81 +++++++++++++++++++++++++++-----------------------------
 1 file changed, 39 insertions(+), 42 deletions(-)
Show changes to advice.c +39 −42
diff --git a/advice.c b/advice.c
index b395bc7d187..f7cedac7549 100644
--- a/advice.c
+++ b/advice.c
@@ -33,46 +33,46 @@ static const char *advise_get_color(enum color_advice ix)
 
 static struct {
 	const char *key;
-	int enabled;
+	int disabled;
 } advice_setting[] = {
-	[ADVICE_ADD_EMBEDDED_REPO]			= { "addEmbeddedRepo", 1 },
-	[ADVICE_ADD_EMPTY_PATHSPEC]			= { "addEmptyPathspec", 1 },
-	[ADVICE_ADD_IGNORED_FILE]			= { "addIgnoredFile", 1 },
-	[ADVICE_AM_WORK_DIR] 				= { "amWorkDir", 1 },
-	[ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME] 	= { "checkoutAmbiguousRemoteBranchName", 1 },
-	[ADVICE_COMMIT_BEFORE_MERGE]			= { "commitBeforeMerge", 1 },
-	[ADVICE_DETACHED_HEAD]				= { "detachedHead", 1 },
-	[ADVICE_FETCH_SHOW_FORCED_UPDATES]		= { "fetchShowForcedUpdates", 1 },
-	[ADVICE_GRAFT_FILE_DEPRECATED]			= { "graftFileDeprecated", 1 },
-	[ADVICE_IGNORED_HOOK]				= { "ignoredHook", 1 },
-	[ADVICE_IMPLICIT_IDENTITY]			= { "implicitIdentity", 1 },
-	[ADVICE_NESTED_TAG]				= { "nestedTag", 1 },
-	[ADVICE_OBJECT_NAME_WARNING]			= { "objectNameWarning", 1 },
-	[ADVICE_PUSH_ALREADY_EXISTS]			= { "pushAlreadyExists", 1 },
-	[ADVICE_PUSH_FETCH_FIRST]			= { "pushFetchFirst", 1 },
-	[ADVICE_PUSH_NEEDS_FORCE]			= { "pushNeedsForce", 1 },
-	[ADVICE_PUSH_REF_NEEDS_UPDATE]			= { "pushRefNeedsUpdate", 1 },
-	[ADVICE_PUSH_NON_FF_CURRENT]			= { "pushNonFFCurrent", 1 },
-	[ADVICE_PUSH_NON_FF_MATCHING]			= { "pushNonFFMatching", 1 },
-	[ADVICE_PUSH_UNQUALIFIED_REF_NAME]		= { "pushUnqualifiedRefName", 1 },
-	[ADVICE_PUSH_UPDATE_REJECTED]			= { "pushUpdateRejected", 1 },
-	[ADVICE_RESET_QUIET_WARNING]			= { "resetQuiet", 1 },
-	[ADVICE_RESOLVE_CONFLICT]			= { "resolveConflict", 1 },
-	[ADVICE_RM_HINTS]				= { "rmHints", 1 },
-	[ADVICE_SEQUENCER_IN_USE]			= { "sequencerInUse", 1 },
-	[ADVICE_SET_UPSTREAM_FAILURE]			= { "setUpstreamFailure", 1 },
-	[ADVICE_SKIPPED_CHERRY_PICKS]			= { "skippedCherryPicks", 1 },
-	[ADVICE_STATUS_AHEAD_BEHIND_WARNING]		= { "statusAheadBehindWarning", 1 },
-	[ADVICE_STATUS_HINTS]				= { "statusHints", 1 },
-	[ADVICE_STATUS_U_OPTION]			= { "statusUoption", 1 },
-	[ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = { "submoduleAlternateErrorStrategyDie", 1 },
-	[ADVICE_UPDATE_SPARSE_PATH]			= { "updateSparsePath", 1 },
-	[ADVICE_WAITING_FOR_EDITOR]			= { "waitingForEditor", 1 },
+	[ADVICE_ADD_EMBEDDED_REPO]			= { "advice.addEmbeddedRepo" },
+	[ADVICE_ADD_EMPTY_PATHSPEC]			= { "advice.addEmptyPathspec" },
+	[ADVICE_ADD_IGNORED_FILE]			= { "advice.addIgnoredFile" },
+	[ADVICE_AM_WORK_DIR] 				= { "advice.amWorkDir" },
+	[ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME] 	= { "advice.checkoutAmbiguousRemoteBranchName" },
+	[ADVICE_COMMIT_BEFORE_MERGE]			= { "advice.commitBeforeMerge" },
+	[ADVICE_DETACHED_HEAD]				= { "advice.detachedHead" },
+	[ADVICE_FETCH_SHOW_FORCED_UPDATES]		= { "advice.fetchShowForcedUpdates" },
+	[ADVICE_GRAFT_FILE_DEPRECATED]			= { "advice.graftFileDeprecated" },
+	[ADVICE_IGNORED_HOOK]				= { "advice.ignoredHook" },
+	[ADVICE_IMPLICIT_IDENTITY]			= { "advice.implicitIdentity" },
+	[ADVICE_NESTED_TAG]				= { "advice.nestedTag" },
+	[ADVICE_OBJECT_NAME_WARNING]			= { "advice.objectNameWarning" },
+	[ADVICE_PUSH_ALREADY_EXISTS]			= { "advice.pushAlreadyExists" },
+	[ADVICE_PUSH_FETCH_FIRST]			= { "advice.pushFetchFirst" },
+	[ADVICE_PUSH_NEEDS_FORCE]			= { "advice.pushNeedsForce" },
+	[ADVICE_PUSH_REF_NEEDS_UPDATE]			= { "advice.pushRefNeedsUpdate" },
+	[ADVICE_PUSH_NON_FF_CURRENT]			= { "advice.pushNonFFCurrent" },
+	[ADVICE_PUSH_NON_FF_MATCHING]			= { "advice.pushNonFFMatching" },
+	[ADVICE_PUSH_UNQUALIFIED_REF_NAME]		= { "advice.pushUnqualifiedRefName" },
+	[ADVICE_PUSH_UPDATE_REJECTED]			= { "advice.pushUpdateRejected" },
+	[ADVICE_RESET_QUIET_WARNING]			= { "advice.resetQuiet" },
+	[ADVICE_RESOLVE_CONFLICT]			= { "advice.resolveConflict" },
+	[ADVICE_RM_HINTS]				= { "advice.rmHints" },
+	[ADVICE_SEQUENCER_IN_USE]			= { "advice.sequencerInUse" },
+	[ADVICE_SET_UPSTREAM_FAILURE]			= { "advice.setUpstreamFailure" },
+	[ADVICE_SKIPPED_CHERRY_PICKS]			= { "advice.skippedCherryPicks" },
+	[ADVICE_STATUS_AHEAD_BEHIND_WARNING]		= { "advice.statusAheadBehindWarning" },
+	[ADVICE_STATUS_HINTS]				= { "advice.statusHints" },
+	[ADVICE_STATUS_U_OPTION]			= { "advice.statusUoption" },
+	[ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = { "advice.submoduleAlternateErrorStrategyDie" },
+	[ADVICE_UPDATE_SPARSE_PATH]			= { "advice.updateSparsePath" },
+	[ADVICE_WAITING_FOR_EDITOR]			= { "advice.waitingForEditor" },
 };
 
 static const char turn_off_instructions[] =
 N_("\n"
-   "Disable this message with \"git config advice.%s false\"");
+   "Disable this message with \"git config %s false\"");
 
 static void vadvise(const char *advice, int display_instructions,
 		    const char *key, va_list params)
@@ -112,7 +112,7 @@ void advise(const char *advice, ...)
 
 int advice_enabled(enum advice_type type)
 {
-	return advice_setting[type].enabled;
+	return !advice_setting[type].disabled;
 }
 
 void advise_if_enabled(enum advice_type type, const char *advice, ...)
@@ -129,7 +129,7 @@ void advise_if_enabled(enum advice_type type, const char *advice, ...)
 
 int git_default_advice_config(const char *var, const char *value)
 {
-	const char *k, *slot_name;
+	const char *slot_name;
 	int i;
 
 	if (!strcmp(var, "color.advice")) {
@@ -146,13 +146,10 @@ int git_default_advice_config(const char *var, const char *value)
 		return color_parse(value, advice_colors[slot]);
 	}
 
-	if (!skip_prefix(var, "advice.", &k))
-		return 0;
-
 	for (i = 0; i < ARRAY_SIZE(advice_setting); i++) {
-		if (strcasecmp(k, advice_setting[i].key))
+		if (strcasecmp(var, advice_setting[i].key))
 			continue;
-		advice_setting[i].enabled = git_config_bool(var, value);
+		advice_setting[i].disabled = !git_config_bool(var, value);
 		return 0;
 	}
 
-- 
2.33.0.1404.g7bcfc82b295
Ævar Arnfjörð Bjarmason· Oct 4, 2021, 01:58 UTC · re: Ævar Arnfjörð Bjarmason · lore

[PATCH 5/5] advice.[ch]: sort enum fields in alphabetical order

These fields have slightly drifted out of order, let's re-order them.
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 advice.c | 2 +-
 advice.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
Show changes to 2 files +2 −2

advice.c, advice.h

diff --git a/advice.c b/advice.c
index f7cedac7549..e0ee8ca4f2f 100644
--- a/advice.c
+++ b/advice.c
@@ -51,9 +51,9 @@ static struct {
 	[ADVICE_PUSH_ALREADY_EXISTS]			= { "advice.pushAlreadyExists" },
 	[ADVICE_PUSH_FETCH_FIRST]			= { "advice.pushFetchFirst" },
 	[ADVICE_PUSH_NEEDS_FORCE]			= { "advice.pushNeedsForce" },
-	[ADVICE_PUSH_REF_NEEDS_UPDATE]			= { "advice.pushRefNeedsUpdate" },
 	[ADVICE_PUSH_NON_FF_CURRENT]			= { "advice.pushNonFFCurrent" },
 	[ADVICE_PUSH_NON_FF_MATCHING]			= { "advice.pushNonFFMatching" },
+	[ADVICE_PUSH_REF_NEEDS_UPDATE]			= { "advice.pushRefNeedsUpdate" },
 	[ADVICE_PUSH_UNQUALIFIED_REF_NAME]		= { "advice.pushUnqualifiedRefName" },
 	[ADVICE_PUSH_UPDATE_REJECTED]			= { "advice.pushUpdateRejected" },
 	[ADVICE_RESET_QUIET_WARNING]			= { "advice.resetQuiet" },
diff --git a/advice.h b/advice.h
index ab25c6ba9a0..a6aca9684c7 100644
--- a/advice.h
+++ b/advice.h
@@ -31,9 +31,9 @@ enum advice_type {
 	ADVICE_PUSH_NEEDS_FORCE,
 	ADVICE_PUSH_NON_FF_CURRENT,
 	ADVICE_PUSH_NON_FF_MATCHING,
+	ADVICE_PUSH_REF_NEEDS_UPDATE,
 	ADVICE_PUSH_UNQUALIFIED_REF_NAME,
 	ADVICE_PUSH_UPDATE_REJECTED,
-	ADVICE_PUSH_REF_NEEDS_UPDATE,
 	ADVICE_RESET_QUIET_WARNING,
 	ADVICE_RESOLVE_CONFLICT,
 	ADVICE_RM_HINTS,
-- 
2.33.0.1404.g7bcfc82b295

← back to recent threads