6 int advice_fetch_show_forced_updates = 1;
7 int advice_push_update_rejected = 1;
8 int advice_push_non_ff_current = 1;
9 int advice_push_non_ff_matching = 1;
10 int advice_push_already_exists = 1;
11 int advice_push_fetch_first = 1;
12 int advice_push_needs_force = 1;
13 int advice_push_unqualified_ref_name = 1;
14 int advice_status_hints = 1;
15 int advice_status_u_option = 1;
16 int advice_status_ahead_behind_warning = 1;
17 int advice_commit_before_merge = 1;
18 int advice_reset_quiet_warning = 1;
19 int advice_resolve_conflict = 1;
20 int advice_sequencer_in_use = 1;
21 int advice_implicit_identity = 1;
22 int advice_detached_head = 1;
23 int advice_set_upstream_failure = 1;
24 int advice_object_name_warning = 1;
25 int advice_amworkdir = 1;
26 int advice_rm_hints = 1;
27 int advice_add_embedded_repo = 1;
28 int advice_ignored_hook = 1;
29 int advice_waiting_for_editor = 1;
30 int advice_graft_file_deprecated = 1;
31 int advice_checkout_ambiguous_remote_branch_name = 1;
32 int advice_nested_tag = 1;
33 int advice_submodule_alternate_error_strategy_die = 1;
35 static int advice_use_color = -1;
36 static char advice_colors[][COLOR_MAXLEN] = {
38 GIT_COLOR_YELLOW, /* HINT */
42 ADVICE_COLOR_RESET = 0,
43 ADVICE_COLOR_HINT = 1,
46 static int parse_advise_color_slot(const char *slot)
48 if (!strcasecmp(slot, "reset"))
49 return ADVICE_COLOR_RESET;
50 if (!strcasecmp(slot, "hint"))
51 return ADVICE_COLOR_HINT;
55 static const char *advise_get_color(enum color_advice ix)
57 if (want_color_stderr(advice_use_color))
58 return advice_colors[ix];
66 { "fetchShowForcedUpdates", &advice_fetch_show_forced_updates },
67 { "pushUpdateRejected", &advice_push_update_rejected },
68 { "pushNonFFCurrent", &advice_push_non_ff_current },
69 { "pushNonFFMatching", &advice_push_non_ff_matching },
70 { "pushAlreadyExists", &advice_push_already_exists },
71 { "pushFetchFirst", &advice_push_fetch_first },
72 { "pushNeedsForce", &advice_push_needs_force },
73 { "pushUnqualifiedRefName", &advice_push_unqualified_ref_name },
74 { "statusHints", &advice_status_hints },
75 { "statusUoption", &advice_status_u_option },
76 { "statusAheadBehindWarning", &advice_status_ahead_behind_warning },
77 { "commitBeforeMerge", &advice_commit_before_merge },
78 { "resetQuiet", &advice_reset_quiet_warning },
79 { "resolveConflict", &advice_resolve_conflict },
80 { "sequencerInUse", &advice_sequencer_in_use },
81 { "implicitIdentity", &advice_implicit_identity },
82 { "detachedHead", &advice_detached_head },
83 { "setUpstreamFailure", &advice_set_upstream_failure },
84 { "objectNameWarning", &advice_object_name_warning },
85 { "amWorkDir", &advice_amworkdir },
86 { "rmHints", &advice_rm_hints },
87 { "addEmbeddedRepo", &advice_add_embedded_repo },
88 { "ignoredHook", &advice_ignored_hook },
89 { "waitingForEditor", &advice_waiting_for_editor },
90 { "graftFileDeprecated", &advice_graft_file_deprecated },
91 { "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name },
92 { "nestedTag", &advice_nested_tag },
93 { "submoduleAlternateErrorStrategyDie", &advice_submodule_alternate_error_strategy_die },
95 /* make this an alias for backward compatibility */
96 { "pushNonFastForward", &advice_push_update_rejected }
99 static void vadvise(const char *advice, va_list params)
101 struct strbuf buf = STRBUF_INIT;
104 strbuf_vaddf(&buf, advice, params);
106 for (cp = buf.buf; *cp; cp = np) {
107 np = strchrnul(cp, '\n');
108 fprintf(stderr, _("%shint: %.*s%s\n"),
109 advise_get_color(ADVICE_COLOR_HINT),
111 advise_get_color(ADVICE_COLOR_RESET));
115 strbuf_release(&buf);
118 void advise(const char *advice, ...)
121 va_start(params, advice);
122 vadvise(advice, params);
126 int git_default_advice_config(const char *var, const char *value)
128 const char *k, *slot_name;
131 if (!strcmp(var, "color.advice")) {
132 advice_use_color = git_config_colorbool(var, value);
136 if (skip_prefix(var, "color.advice.", &slot_name)) {
137 int slot = parse_advise_color_slot(slot_name);
141 return config_error_nonbool(var);
142 return color_parse(value, advice_colors[slot]);
145 if (!skip_prefix(var, "advice.", &k))
148 for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
149 if (strcasecmp(k, advice_config[i].name))
151 *advice_config[i].preference = git_config_bool(var, value);
158 void list_config_advices(struct string_list *list, const char *prefix)
162 for (i = 0; i < ARRAY_SIZE(advice_config); i++)
163 list_config_item(list, prefix, advice_config[i].name);
166 int error_resolve_conflict(const char *me)
168 if (!strcmp(me, "cherry-pick"))
169 error(_("Cherry-picking is not possible because you have unmerged files."));
170 else if (!strcmp(me, "commit"))
171 error(_("Committing is not possible because you have unmerged files."));
172 else if (!strcmp(me, "merge"))
173 error(_("Merging is not possible because you have unmerged files."));
174 else if (!strcmp(me, "pull"))
175 error(_("Pulling is not possible because you have unmerged files."));
176 else if (!strcmp(me, "revert"))
177 error(_("Reverting is not possible because you have unmerged files."));
179 error(_("It is not possible to %s because you have unmerged files."),
182 if (advice_resolve_conflict)
184 * Message used both when 'git commit' fails and when
185 * other commands doing a merge do.
187 advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n"
188 "as appropriate to mark resolution and make a commit."));
192 void NORETURN die_resolve_conflict(const char *me)
194 error_resolve_conflict(me);
195 die(_("Exiting because of an unresolved conflict."));
198 void NORETURN die_conclude_merge(void)
200 error(_("You have not concluded your merge (MERGE_HEAD exists)."));
201 if (advice_resolve_conflict)
202 advise(_("Please, commit your changes before merging."));
203 die(_("Exiting because of unfinished merge."));
206 void detach_advice(const char *new_name)
209 _("Note: switching to '%s'.\n"
211 "You are in 'detached HEAD' state. You can look around, make experimental\n"
212 "changes and commit them, and you can discard any commits you make in this\n"
213 "state without impacting any branches by switching back to a branch.\n"
215 "If you want to create a new branch to retain commits you create, you may\n"
216 "do so (now or later) by using -c with the switch command. Example:\n"
218 " git switch -c <new-branch-name>\n"
220 "Or undo this operation with:\n"
224 "Turn off this advice by setting config variable advice.detachedHead to false\n\n");
226 fprintf(stderr, fmt, new_name);