advice: revamp advise API
[git] / advice.c
1 #include "cache.h"
2 #include "config.h"
3 #include "color.h"
4 #include "help.h"
5
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;
34
35 static int advice_use_color = -1;
36 static char advice_colors[][COLOR_MAXLEN] = {
37         GIT_COLOR_RESET,
38         GIT_COLOR_YELLOW,       /* HINT */
39 };
40
41 enum color_advice {
42         ADVICE_COLOR_RESET = 0,
43         ADVICE_COLOR_HINT = 1,
44 };
45
46 static int parse_advise_color_slot(const char *slot)
47 {
48         if (!strcasecmp(slot, "reset"))
49                 return ADVICE_COLOR_RESET;
50         if (!strcasecmp(slot, "hint"))
51                 return ADVICE_COLOR_HINT;
52         return -1;
53 }
54
55 static const char *advise_get_color(enum color_advice ix)
56 {
57         if (want_color_stderr(advice_use_color))
58                 return advice_colors[ix];
59         return "";
60 }
61
62 static struct {
63         const char *name;
64         int *preference;
65 } advice_config[] = {
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 },
94
95         /* make this an alias for backward compatibility */
96         { "pushNonFastForward", &advice_push_update_rejected }
97 };
98
99 static struct {
100         const char *key;
101         int enabled;
102 } advice_setting[] = {
103         [ADVICE_ADD_EMBEDDED_REPO]                      = { "addEmbeddedRepo", 1 },
104         [ADVICE_AM_WORK_DIR]                            = { "amWorkDir", 1 },
105         [ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME]  = { "checkoutAmbiguousRemoteBranchName", 1 },
106         [ADVICE_COMMIT_BEFORE_MERGE]                    = { "commitBeforeMerge", 1 },
107         [ADVICE_DETACHED_HEAD]                          = { "detachedHead", 1 },
108         [ADVICE_FETCH_SHOW_FORCED_UPDATES]              = { "fetchShowForcedUpdates", 1 },
109         [ADVICE_GRAFT_FILE_DEPRECATED]                  = { "graftFileDeprecated", 1 },
110         [ADVICE_IGNORED_HOOK]                           = { "ignoredHook", 1 },
111         [ADVICE_IMPLICIT_IDENTITY]                      = { "implicitIdentity", 1 },
112         [ADVICE_NESTED_TAG]                             = { "nestedTag", 1 },
113         [ADVICE_OBJECT_NAME_WARNING]                    = { "objectNameWarning", 1 },
114         [ADVICE_PUSH_ALREADY_EXISTS]                    = { "pushAlreadyExists", 1 },
115         [ADVICE_PUSH_FETCH_FIRST]                       = { "pushFetchFirst", 1 },
116         [ADVICE_PUSH_NEEDS_FORCE]                       = { "pushNeedsForce", 1 },
117
118         /* make this an alias for backward compatibility */
119         [ADVICE_PUSH_UPDATE_REJECTED_ALIAS]             = { "pushNonFastForward", 1 },
120
121         [ADVICE_PUSH_NON_FF_CURRENT]                    = { "pushNonFFCurrent", 1 },
122         [ADVICE_PUSH_NON_FF_MATCHING]                   = { "pushNonFFMatching", 1 },
123         [ADVICE_PUSH_UNQUALIFIED_REF_NAME]              = { "pushUnqualifiedRefName", 1 },
124         [ADVICE_PUSH_UPDATE_REJECTED]                   = { "pushUpdateRejected", 1 },
125         [ADVICE_RESET_QUIET_WARNING]                    = { "resetQuiet", 1 },
126         [ADVICE_RESOLVE_CONFLICT]                       = { "resolveConflict", 1 },
127         [ADVICE_RM_HINTS]                               = { "rmHints", 1 },
128         [ADVICE_SEQUENCER_IN_USE]                       = { "sequencerInUse", 1 },
129         [ADVICE_SET_UPSTREAM_FAILURE]                   = { "setUpstreamFailure", 1 },
130         [ADVICE_STATUS_AHEAD_BEHIND_WARNING]            = { "statusAheadBehindWarning", 1 },
131         [ADVICE_STATUS_HINTS]                           = { "statusHints", 1 },
132         [ADVICE_STATUS_U_OPTION]                        = { "statusUoption", 1 },
133         [ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = { "submoduleAlternateErrorStrategyDie", 1 },
134         [ADVICE_WAITING_FOR_EDITOR]                     = { "waitingForEditor", 1 },
135 };
136
137 static const char turn_off_instructions[] =
138 N_("\n"
139    "Disable this message with \"git config advice.%s false\"");
140
141 static void vadvise(const char *advice, int display_instructions,
142                     const char *key, va_list params)
143 {
144         struct strbuf buf = STRBUF_INIT;
145         const char *cp, *np;
146
147         strbuf_vaddf(&buf, advice, params);
148
149         if (display_instructions)
150                 strbuf_addf(&buf, turn_off_instructions, key);
151
152         for (cp = buf.buf; *cp; cp = np) {
153                 np = strchrnul(cp, '\n');
154                 fprintf(stderr, _("%shint: %.*s%s\n"),
155                         advise_get_color(ADVICE_COLOR_HINT),
156                         (int)(np - cp), cp,
157                         advise_get_color(ADVICE_COLOR_RESET));
158                 if (*np)
159                         np++;
160         }
161         strbuf_release(&buf);
162 }
163
164 void advise(const char *advice, ...)
165 {
166         va_list params;
167         va_start(params, advice);
168         vadvise(advice, 0, "", params);
169         va_end(params);
170 }
171
172 int advice_enabled(enum advice_type type)
173 {
174         switch(type) {
175         case ADVICE_PUSH_UPDATE_REJECTED:
176                 return advice_setting[ADVICE_PUSH_UPDATE_REJECTED].enabled &&
177                        advice_setting[ADVICE_PUSH_UPDATE_REJECTED_ALIAS].enabled;
178         default:
179                 return advice_setting[type].enabled;
180         }
181 }
182
183 void advise_if_enabled(enum advice_type type, const char *advice, ...)
184 {
185         va_list params;
186
187         if (!advice_enabled(type))
188                 return;
189
190         va_start(params, advice);
191         vadvise(advice, 1, advice_setting[type].key, params);
192         va_end(params);
193 }
194
195 int git_default_advice_config(const char *var, const char *value)
196 {
197         const char *k, *slot_name;
198         int i;
199
200         if (!strcmp(var, "color.advice")) {
201                 advice_use_color = git_config_colorbool(var, value);
202                 return 0;
203         }
204
205         if (skip_prefix(var, "color.advice.", &slot_name)) {
206                 int slot = parse_advise_color_slot(slot_name);
207                 if (slot < 0)
208                         return 0;
209                 if (!value)
210                         return config_error_nonbool(var);
211                 return color_parse(value, advice_colors[slot]);
212         }
213
214         if (!skip_prefix(var, "advice.", &k))
215                 return 0;
216
217         for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
218                 if (strcasecmp(k, advice_config[i].name))
219                         continue;
220                 *advice_config[i].preference = git_config_bool(var, value);
221                 break;
222         }
223
224         for (i = 0; i < ARRAY_SIZE(advice_setting); i++) {
225                 if (strcasecmp(k, advice_setting[i].key))
226                         continue;
227                 advice_setting[i].enabled = git_config_bool(var, value);
228                 return 0;
229         }
230
231         return 0;
232 }
233
234 void list_config_advices(struct string_list *list, const char *prefix)
235 {
236         int i;
237
238         for (i = 0; i < ARRAY_SIZE(advice_setting); i++)
239                 list_config_item(list, prefix, advice_setting[i].key);
240 }
241
242 int error_resolve_conflict(const char *me)
243 {
244         if (!strcmp(me, "cherry-pick"))
245                 error(_("Cherry-picking is not possible because you have unmerged files."));
246         else if (!strcmp(me, "commit"))
247                 error(_("Committing is not possible because you have unmerged files."));
248         else if (!strcmp(me, "merge"))
249                 error(_("Merging is not possible because you have unmerged files."));
250         else if (!strcmp(me, "pull"))
251                 error(_("Pulling is not possible because you have unmerged files."));
252         else if (!strcmp(me, "revert"))
253                 error(_("Reverting is not possible because you have unmerged files."));
254         else
255                 error(_("It is not possible to %s because you have unmerged files."),
256                         me);
257
258         if (advice_resolve_conflict)
259                 /*
260                  * Message used both when 'git commit' fails and when
261                  * other commands doing a merge do.
262                  */
263                 advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n"
264                          "as appropriate to mark resolution and make a commit."));
265         return -1;
266 }
267
268 void NORETURN die_resolve_conflict(const char *me)
269 {
270         error_resolve_conflict(me);
271         die(_("Exiting because of an unresolved conflict."));
272 }
273
274 void NORETURN die_conclude_merge(void)
275 {
276         error(_("You have not concluded your merge (MERGE_HEAD exists)."));
277         if (advice_resolve_conflict)
278                 advise(_("Please, commit your changes before merging."));
279         die(_("Exiting because of unfinished merge."));
280 }
281
282 void detach_advice(const char *new_name)
283 {
284         const char *fmt =
285         _("Note: switching to '%s'.\n"
286         "\n"
287         "You are in 'detached HEAD' state. You can look around, make experimental\n"
288         "changes and commit them, and you can discard any commits you make in this\n"
289         "state without impacting any branches by switching back to a branch.\n"
290         "\n"
291         "If you want to create a new branch to retain commits you create, you may\n"
292         "do so (now or later) by using -c with the switch command. Example:\n"
293         "\n"
294         "  git switch -c <new-branch-name>\n"
295         "\n"
296         "Or undo this operation with:\n"
297         "\n"
298         "  git switch -\n"
299         "\n"
300         "Turn off this advice by setting config variable advice.detachedHead to false\n\n");
301
302         fprintf(stderr, fmt, new_name);
303 }