]> git.ipfire.org Git - thirdparty/git.git/commitdiff
fetch set_head: move warn advice into advise_if_enabled
authorBence Ferdinandy <bence@ferdinandy.com>
Thu, 5 Dec 2024 12:16:20 +0000 (13:16 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 5 Dec 2024 17:59:16 +0000 (02:59 +0900)
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>
advice.c
advice.h
builtin/fetch.c
t/t5510-fetch.sh

index 6b879d805c03049a2db67aaebfe3a1cd3aa2297c..66461fdce9eb06c4d900156427df9aae8d5e5100 100644 (file)
--- a/advice.c
+++ b/advice.c
@@ -53,6 +53,7 @@ static struct {
        [ADVICE_COMMIT_BEFORE_MERGE]                    = { "commitBeforeMerge" },
        [ADVICE_DETACHED_HEAD]                          = { "detachedHead" },
        [ADVICE_DIVERGING]                              = { "diverging" },
+       [ADVICE_FETCH_SET_HEAD_WARN]                    = { "fetchRemoteHEADWarn" },
        [ADVICE_FETCH_SHOW_FORCED_UPDATES]              = { "fetchShowForcedUpdates" },
        [ADVICE_FORCE_DELETE_BRANCH]                    = { "forceDeleteBranch" },
        [ADVICE_GRAFT_FILE_DEPRECATED]                  = { "graftFileDeprecated" },
index d7466bc0ef20264fb475d57e408abca5ca81fad4..cf2284ec43864b294da74810fc19519b739876f3 100644 (file)
--- a/advice.h
+++ b/advice.h
@@ -20,6 +20,7 @@ enum advice_type {
        ADVICE_COMMIT_BEFORE_MERGE,
        ADVICE_DETACHED_HEAD,
        ADVICE_DIVERGING,
+       ADVICE_FETCH_SET_HEAD_WARN,
        ADVICE_FETCH_SHOW_FORCED_UPDATES,
        ADVICE_FORCE_DELETE_BRANCH,
        ADVICE_GRAFT_FILE_DEPRECATED,
index 88c5c5d7818753e48e82281e9e7e53ec9beae2b1..897e71325fef3dee4d63e448264c8fb12e881757 100644 (file)
@@ -1579,6 +1579,17 @@ static const char *strip_refshead(const char *name){
        return name;
 }
 
+static void set_head_advice_msg(const char *remote, const char *head_name)
+{
+       const char message_advice_set_head[] =
+       N_("Run 'git remote set-head %s %s' to follow the change, or set\n"
+          "'remote.%s.followRemoteHEAD' configuration option to a different value\n"
+          "if you do not want to see this message.");
+
+       advise_if_enabled(ADVICE_FETCH_SET_HEAD_WARN, _(message_advice_set_head),
+                       remote, head_name, remote);
+}
+
 static void report_set_head(const char *remote, const char *head_name,
                        struct strbuf *buf_prev, int updateres) {
        struct strbuf buf_prefix = STRBUF_INIT;
@@ -1590,15 +1601,13 @@ static void report_set_head(const char *remote, const char *head_name,
        if (prev_head && strcmp(prev_head, head_name)) {
                printf("'HEAD' at '%s' is '%s', but we have '%s' locally.\n",
                        remote, head_name, prev_head);
-               printf("Run 'git remote set-head %s %s' to follow the change.\n",
-                       remote, head_name);
+               set_head_advice_msg(remote, head_name);
        }
        else if (updateres && buf_prev->len) {
                printf("'HEAD' at '%s' is '%s', "
                        "but we have a detached HEAD pointing to '%s' locally.\n",
                        remote, head_name, buf_prev->buf);
-               printf("Run 'git remote set-head %s %s' to follow the change.\n",
-                       remote, head_name);
+               set_head_advice_msg(remote, head_name);
        }
        strbuf_release(&buf_prefix);
 }
index 2467027d34de99e81b622f3172dfc76ea5094fd0..5c96591b9e83247bb2ad1db34b3e05d14a7769cb 100755 (executable)
@@ -124,7 +124,6 @@ test_expect_success "fetch test followRemoteHEAD warn no change" '
                git fetch >output &&
                echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
                        "but we have ${SQ}other${SQ} locally." >expect &&
-               echo "Run ${SQ}git remote set-head origin main${SQ} to follow the change." >>expect &&
                test_cmp expect output &&
                head=$(git rev-parse refs/remotes/origin/HEAD) &&
                branch=$(git rev-parse refs/remotes/origin/other) &&
@@ -161,7 +160,6 @@ test_expect_success "fetch test followRemoteHEAD warn detached" '
                echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
                        "but we have a detached HEAD pointing to" \
                        "${SQ}${HEAD}${SQ} locally." >expect &&
-               echo "Run ${SQ}git remote set-head origin main${SQ} to follow the change." >>expect &&
                test_cmp expect output
        )
 '