1
0
mirror of https://github.com/git/git.git synced 2025-02-06 09:26:16 +00:00

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>
This commit is contained in:
Bence Ferdinandy 2024-12-05 13:16:20 +01:00 committed by Junio C Hamano
parent b7f7d16562
commit ad739f525e
4 changed files with 15 additions and 6 deletions

View File

@ -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" },

View File

@ -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,

View 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);
}

View File

@ -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
)
'