Skip to content

Commit ad739f5

Browse files
ferdinandybgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b7f7d16 commit ad739f5

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

advice.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ static struct {
5353
[ADVICE_COMMIT_BEFORE_MERGE] = { "commitBeforeMerge" },
5454
[ADVICE_DETACHED_HEAD] = { "detachedHead" },
5555
[ADVICE_DIVERGING] = { "diverging" },
56+
[ADVICE_FETCH_SET_HEAD_WARN] = { "fetchRemoteHEADWarn" },
5657
[ADVICE_FETCH_SHOW_FORCED_UPDATES] = { "fetchShowForcedUpdates" },
5758
[ADVICE_FORCE_DELETE_BRANCH] = { "forceDeleteBranch" },
5859
[ADVICE_GRAFT_FILE_DEPRECATED] = { "graftFileDeprecated" },

advice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ enum advice_type {
2020
ADVICE_COMMIT_BEFORE_MERGE,
2121
ADVICE_DETACHED_HEAD,
2222
ADVICE_DIVERGING,
23+
ADVICE_FETCH_SET_HEAD_WARN,
2324
ADVICE_FETCH_SHOW_FORCED_UPDATES,
2425
ADVICE_FORCE_DELETE_BRANCH,
2526
ADVICE_GRAFT_FILE_DEPRECATED,

builtin/fetch.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,17 @@ static const char *strip_refshead(const char *name){
15791579
return name;
15801580
}
15811581

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+
15821593
static void report_set_head(const char *remote, const char *head_name,
15831594
struct strbuf *buf_prev, int updateres) {
15841595
struct strbuf buf_prefix = STRBUF_INIT;
@@ -1590,15 +1601,13 @@ static void report_set_head(const char *remote, const char *head_name,
15901601
if (prev_head && strcmp(prev_head, head_name)) {
15911602
printf("'HEAD' at '%s' is '%s', but we have '%s' locally.\n",
15921603
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);
15951605
}
15961606
else if (updateres && buf_prev->len) {
15971607
printf("'HEAD' at '%s' is '%s', "
15981608
"but we have a detached HEAD pointing to '%s' locally.\n",
15991609
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);
16021611
}
16031612
strbuf_release(&buf_prefix);
16041613
}

t/t5510-fetch.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ test_expect_success "fetch test followRemoteHEAD warn no change" '
124124
git fetch >output &&
125125
echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
126126
"but we have ${SQ}other${SQ} locally." >expect &&
127-
echo "Run ${SQ}git remote set-head origin main${SQ} to follow the change." >>expect &&
128127
test_cmp expect output &&
129128
head=$(git rev-parse refs/remotes/origin/HEAD) &&
130129
branch=$(git rev-parse refs/remotes/origin/other) &&
@@ -161,7 +160,6 @@ test_expect_success "fetch test followRemoteHEAD warn detached" '
161160
echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
162161
"but we have a detached HEAD pointing to" \
163162
"${SQ}${HEAD}${SQ} locally." >expect &&
164-
echo "Run ${SQ}git remote set-head origin main${SQ} to follow the change." >>expect &&
165163
test_cmp expect output
166164
)
167165
'

0 commit comments

Comments
 (0)