fetch set_head: move warn advice into advise_if_enabled

Advice about what to do when getting a warning is typed out explicitly twice and is printed as regular output. The output is also tested for. Extract the advice message into a single place and use a wrapper function, so if later the advice is made more chatty the signature only needs to be changed in once place. Remove the testing for the advice output in the tests. Signed-off-by: Bence Ferdinandy <bence@ferdinandy.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Bence Ferdinandy committed Dec 5, 2024 at 13:16 UTC ad739f525eec917198887055f1a815e78d7c66be
4 files changed +15 -6
advice.c
+1
@@ -53,6 +53,7 @@ static struct {
53 [ADVICE_COMMIT_BEFORE_MERGE] = { "commitBeforeMerge" },
54 [ADVICE_DETACHED_HEAD] = { "detachedHead" },
55 [ADVICE_DIVERGING] = { "diverging" },
56 + [ADVICE_FETCH_SET_HEAD_WARN] = { "fetchRemoteHEADWarn" },
57 [ADVICE_FETCH_SHOW_FORCED_UPDATES] = { "fetchShowForcedUpdates" },
58 [ADVICE_FORCE_DELETE_BRANCH] = { "forceDeleteBranch" },
59 [ADVICE_GRAFT_FILE_DEPRECATED] = { "graftFileDeprecated" },
advice.h
+1
@@ -20,6 +20,7 @@ enum advice_type {
20 ADVICE_COMMIT_BEFORE_MERGE,
21 ADVICE_DETACHED_HEAD,
22 ADVICE_DIVERGING,
23 + ADVICE_FETCH_SET_HEAD_WARN,
24 ADVICE_FETCH_SHOW_FORCED_UPDATES,
25 ADVICE_FORCE_DELETE_BRANCH,
26 ADVICE_GRAFT_FILE_DEPRECATED,
builtin/fetch.c
+13 -4
@@ -1579,6 +1579,17 @@ static const char *strip_refshead(const char *name){
1579 return name;
1580 }
1581
1582 +static void set_head_advice_msg(const char *remote, const char *head_name)
1583 +{
1584 + const char message_advice_set_head[] =
1585 + N_("Run 'git remote set-head %s %s' to follow the change, or set\n"
1586 + "'remote.%s.followRemoteHEAD' configuration option to a different value\n"
1587 + "if you do not want to see this message.");
1588 +
1589 + advise_if_enabled(ADVICE_FETCH_SET_HEAD_WARN, _(message_advice_set_head),
1590 + remote, head_name, remote);
1591 +}
1592 +
1593 static void report_set_head(const char *remote, const char *head_name,
1594 struct strbuf *buf_prev, int updateres) {
1595 struct strbuf buf_prefix = STRBUF_INIT;
@@ -1590,15 +1601,13 @@ static void report_set_head(const char *remote, const char *head_name,
1601 if (prev_head && strcmp(prev_head, head_name)) {
1602 printf("'HEAD' at '%s' is '%s', but we have '%s' locally.\n",
1603 remote, head_name, prev_head);
1593 - printf("Run 'git remote set-head %s %s' to follow the change.\n",
1594 - remote, head_name);
1604 + set_head_advice_msg(remote, head_name);
1605 }
1606 else if (updateres && buf_prev->len) {
1607 printf("'HEAD' at '%s' is '%s', "
1608 "but we have a detached HEAD pointing to '%s' locally.\n",
1609 remote, head_name, buf_prev->buf);
1600 - printf("Run 'git remote set-head %s %s' to follow the change.\n",
1601 - remote, head_name);
1610 + set_head_advice_msg(remote, head_name);
1611 }
1612 strbuf_release(&buf_prefix);
1613 }
t/t5510-fetch.sh
-2
@@ -124,7 +124,6 @@ test_expect_success "fetch test followRemoteHEAD warn no change" '
124 git fetch >output &&
125 echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
126 "but we have ${SQ}other${SQ} locally." >expect &&
127 - echo "Run ${SQ}git remote set-head origin main${SQ} to follow the change." >>expect &&
127 test_cmp expect output &&
128 head=$(git rev-parse refs/remotes/origin/HEAD) &&
129 branch=$(git rev-parse refs/remotes/origin/other) &&
@@ -161,7 +160,6 @@ test_expect_success "fetch test followRemoteHEAD warn detached" '
160 echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
161 "but we have a detached HEAD pointing to" \
162 "${SQ}${HEAD}${SQ} locally." >expect &&
164 - echo "Run ${SQ}git remote set-head origin main${SQ} to follow the change." >>expect &&
163 test_cmp expect output
164 )
165 '