Sync with 2.24.4
[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 void advise(const char *advice, ...)
100 {
101         struct strbuf buf = STRBUF_INIT;
102         va_list params;
103         const char *cp, *np;
104
105         va_start(params, advice);
106         strbuf_vaddf(&buf, advice, params);
107         va_end(params);
108
109         for (cp = buf.buf; *cp; cp = np) {
110                 np = strchrnul(cp, '\n');
111                 fprintf(stderr, _("%shint: %.*s%s\n"),
112                         advise_get_color(ADVICE_COLOR_HINT),
113                         (int)(np - cp), cp,
114                         advise_get_color(ADVICE_COLOR_RESET));
115                 if (*np)
116                         np++;
117         }
118         strbuf_release(&buf);
119 }
120
121 int git_default_advice_config(const char *var, const char *value)
122 {
123         const char *k, *slot_name;
124         int i;
125
126         if (!strcmp(var, "color.advice")) {
127                 advice_use_color = git_config_colorbool(var, value);
128                 return 0;
129         }
130
131         if (skip_prefix(var, "color.advice.", &slot_name)) {
132                 int slot = parse_advise_color_slot(slot_name);
133                 if (slot < 0)
134                         return 0;
135                 if (!value)
136                         return config_error_nonbool(var);
137                 return color_parse(value, advice_colors[slot]);
138         }
139
140         if (!skip_prefix(var, "advice.", &k))
141                 return 0;
142
143         for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
144                 if (strcasecmp(k, advice_config[i].name))
145                         continue;
146                 *advice_config[i].preference = git_config_bool(var, value);
147                 return 0;
148         }
149
150         return 0;
151 }
152
153 void list_config_advices(struct string_list *list, const char *prefix)
154 {
155         int i;
156
157         for (i = 0; i < ARRAY_SIZE(advice_config); i++)
158                 list_config_item(list, prefix, advice_config[i].name);
159 }
160
161 int error_resolve_conflict(const char *me)
162 {
163         if (!strcmp(me, "cherry-pick"))
164                 error(_("Cherry-picking is not possible because you have unmerged files."));
165         else if (!strcmp(me, "commit"))
166                 error(_("Committing is not possible because you have unmerged files."));
167         else if (!strcmp(me, "merge"))
168                 error(_("Merging is not possible because you have unmerged files."));
169         else if (!strcmp(me, "pull"))
170                 error(_("Pulling is not possible because you have unmerged files."));
171         else if (!strcmp(me, "revert"))
172                 error(_("Reverting is not possible because you have unmerged files."));
173         else
174                 error(_("It is not possible to %s because you have unmerged files."),
175                         me);
176
177         if (advice_resolve_conflict)
178                 /*
179                  * Message used both when 'git commit' fails and when
180                  * other commands doing a merge do.
181                  */
182                 advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n"
183                          "as appropriate to mark resolution and make a commit."));
184         return -1;
185 }
186
187 void NORETURN die_resolve_conflict(const char *me)
188 {
189         error_resolve_conflict(me);
190         die(_("Exiting because of an unresolved conflict."));
191 }
192
193 void NORETURN die_conclude_merge(void)
194 {
195         error(_("You have not concluded your merge (MERGE_HEAD exists)."));
196         if (advice_resolve_conflict)
197                 advise(_("Please, commit your changes before merging."));
198         die(_("Exiting because of unfinished merge."));
199 }
200
201 void detach_advice(const char *new_name)
202 {
203         const char *fmt =
204         _("Note: switching to '%s'.\n"
205         "\n"
206         "You are in 'detached HEAD' state. You can look around, make experimental\n"
207         "changes and commit them, and you can discard any commits you make in this\n"
208         "state without impacting any branches by switching back to a branch.\n"
209         "\n"
210         "If you want to create a new branch to retain commits you create, you may\n"
211         "do so (now or later) by using -c with the switch command. Example:\n"
212         "\n"
213         "  git switch -c <new-branch-name>\n"
214         "\n"
215         "Or undo this operation with:\n"
216         "\n"
217         "  git switch -\n"
218         "\n"
219         "Turn off this advice by setting config variable advice.detachedHead to false\n\n");
220
221         fprintf(stderr, fmt, new_name);
222 }