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