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_implicit_identity = 1;
21 int advice_detached_head = 1;
22 int advice_set_upstream_failure = 1;
23 int advice_object_name_warning = 1;
24 int advice_amworkdir = 1;
25 int advice_rm_hints = 1;
26 int advice_add_embedded_repo = 1;
27 int advice_ignored_hook = 1;
28 int advice_waiting_for_editor = 1;
29 int advice_graft_file_deprecated = 1;
30 int advice_checkout_ambiguous_remote_branch_name = 1;
31 int advice_nested_tag = 1;
33 static int advice_use_color = -1;
34 static char advice_colors[][COLOR_MAXLEN] = {
36 GIT_COLOR_YELLOW, /* HINT */
40 ADVICE_COLOR_RESET = 0,
41 ADVICE_COLOR_HINT = 1,
44 static int parse_advise_color_slot(const char *slot)
46 if (!strcasecmp(slot, "reset"))
47 return ADVICE_COLOR_RESET;
48 if (!strcasecmp(slot, "hint"))
49 return ADVICE_COLOR_HINT;
53 static const char *advise_get_color(enum color_advice ix)
55 if (want_color_stderr(advice_use_color))
56 return advice_colors[ix];
64 { "fetchShowForcedUpdates", &advice_fetch_show_forced_updates },
65 { "pushUpdateRejected", &advice_push_update_rejected },
66 { "pushNonFFCurrent", &advice_push_non_ff_current },
67 { "pushNonFFMatching", &advice_push_non_ff_matching },
68 { "pushAlreadyExists", &advice_push_already_exists },
69 { "pushFetchFirst", &advice_push_fetch_first },
70 { "pushNeedsForce", &advice_push_needs_force },
71 { "pushUnqualifiedRefName", &advice_push_unqualified_ref_name },
72 { "statusHints", &advice_status_hints },
73 { "statusUoption", &advice_status_u_option },
74 { "statusAheadBehindWarning", &advice_status_ahead_behind_warning },
75 { "commitBeforeMerge", &advice_commit_before_merge },
76 { "resetQuiet", &advice_reset_quiet_warning },
77 { "resolveConflict", &advice_resolve_conflict },
78 { "implicitIdentity", &advice_implicit_identity },
79 { "detachedHead", &advice_detached_head },
80 { "setupStreamFailure", &advice_set_upstream_failure },
81 { "objectNameWarning", &advice_object_name_warning },
82 { "amWorkDir", &advice_amworkdir },
83 { "rmHints", &advice_rm_hints },
84 { "addEmbeddedRepo", &advice_add_embedded_repo },
85 { "ignoredHook", &advice_ignored_hook },
86 { "waitingForEditor", &advice_waiting_for_editor },
87 { "graftFileDeprecated", &advice_graft_file_deprecated },
88 { "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name },
89 { "nestedTag", &advice_nested_tag },
91 /* make this an alias for backward compatibility */
92 { "pushNonFastForward", &advice_push_update_rejected }
95 void advise(const char *advice, ...)
97 struct strbuf buf = STRBUF_INIT;
101 va_start(params, advice);
102 strbuf_vaddf(&buf, advice, params);
105 for (cp = buf.buf; *cp; cp = np) {
106 np = strchrnul(cp, '\n');
107 fprintf(stderr, _("%shint: %.*s%s\n"),
108 advise_get_color(ADVICE_COLOR_HINT),
110 advise_get_color(ADVICE_COLOR_RESET));
114 strbuf_release(&buf);
117 int git_default_advice_config(const char *var, const char *value)
119 const char *k, *slot_name;
122 if (!strcmp(var, "color.advice")) {
123 advice_use_color = git_config_colorbool(var, value);
127 if (skip_prefix(var, "color.advice.", &slot_name)) {
128 int slot = parse_advise_color_slot(slot_name);
132 return config_error_nonbool(var);
133 return color_parse(value, advice_colors[slot]);
136 if (!skip_prefix(var, "advice.", &k))
139 for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
140 if (strcasecmp(k, advice_config[i].name))
142 *advice_config[i].preference = git_config_bool(var, value);
149 void list_config_advices(struct string_list *list, const char *prefix)
153 for (i = 0; i < ARRAY_SIZE(advice_config); i++)
154 list_config_item(list, prefix, advice_config[i].name);
157 int error_resolve_conflict(const char *me)
159 if (!strcmp(me, "cherry-pick"))
160 error(_("Cherry-picking is not possible because you have unmerged files."));
161 else if (!strcmp(me, "commit"))
162 error(_("Committing is not possible because you have unmerged files."));
163 else if (!strcmp(me, "merge"))
164 error(_("Merging is not possible because you have unmerged files."));
165 else if (!strcmp(me, "pull"))
166 error(_("Pulling is not possible because you have unmerged files."));
167 else if (!strcmp(me, "revert"))
168 error(_("Reverting is not possible because you have unmerged files."));
170 error(_("It is not possible to %s because you have unmerged files."),
173 if (advice_resolve_conflict)
175 * Message used both when 'git commit' fails and when
176 * other commands doing a merge do.
178 advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n"
179 "as appropriate to mark resolution and make a commit."));
183 void NORETURN die_resolve_conflict(const char *me)
185 error_resolve_conflict(me);
186 die(_("Exiting because of an unresolved conflict."));
189 void NORETURN die_conclude_merge(void)
191 error(_("You have not concluded your merge (MERGE_HEAD exists)."));
192 if (advice_resolve_conflict)
193 advise(_("Please, commit your changes before merging."));
194 die(_("Exiting because of unfinished merge."));
197 void detach_advice(const char *new_name)
200 _("Note: switching to '%s'.\n"
202 "You are in 'detached HEAD' state. You can look around, make experimental\n"
203 "changes and commit them, and you can discard any commits you make in this\n"
204 "state without impacting any branches by switching back to a branch.\n"
206 "If you want to create a new branch to retain commits you create, you may\n"
207 "do so (now or later) by using -c with the switch command. Example:\n"
209 " git switch -c <new-branch-name>\n"
211 "Or undo this operation with:\n"
215 "Turn off this advice by setting config variable advice.detachedHead to false\n\n");
217 fprintf(stderr, fmt, new_name);