advice: change "setupStreamFailure" to "setUpstreamFailure"
[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 void vadvise(const char *advice, va_list params)
100 {
101         struct strbuf buf = STRBUF_INIT;
102         const char *cp, *np;
103
104         strbuf_vaddf(&buf, advice, params);
105
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),
110                         (int)(np - cp), cp,
111                         advise_get_color(ADVICE_COLOR_RESET));
112                 if (*np)
113                         np++;
114         }
115         strbuf_release(&buf);
116 }
117
118 void advise(const char *advice, ...)
119 {
120         va_list params;
121         va_start(params, advice);
122         vadvise(advice, params);
123         va_end(params);
124 }
125
126 int git_default_advice_config(const char *var, const char *value)
127 {
128         const char *k, *slot_name;
129         int i;
130
131         if (!strcmp(var, "color.advice")) {
132                 advice_use_color = git_config_colorbool(var, value);
133                 return 0;
134         }
135
136         if (skip_prefix(var, "color.advice.", &slot_name)) {
137                 int slot = parse_advise_color_slot(slot_name);
138                 if (slot < 0)
139                         return 0;
140                 if (!value)
141                         return config_error_nonbool(var);
142                 return color_parse(value, advice_colors[slot]);
143         }
144
145         if (!skip_prefix(var, "advice.", &k))
146                 return 0;
147
148         for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
149                 if (strcasecmp(k, advice_config[i].name))
150                         continue;
151                 *advice_config[i].preference = git_config_bool(var, value);
152                 return 0;
153         }
154
155         return 0;
156 }
157
158 void list_config_advices(struct string_list *list, const char *prefix)
159 {
160         int i;
161
162         for (i = 0; i < ARRAY_SIZE(advice_config); i++)
163                 list_config_item(list, prefix, advice_config[i].name);
164 }
165
166 int error_resolve_conflict(const char *me)
167 {
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."));
178         else
179                 error(_("It is not possible to %s because you have unmerged files."),
180                         me);
181
182         if (advice_resolve_conflict)
183                 /*
184                  * Message used both when 'git commit' fails and when
185                  * other commands doing a merge do.
186                  */
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."));
189         return -1;
190 }
191
192 void NORETURN die_resolve_conflict(const char *me)
193 {
194         error_resolve_conflict(me);
195         die(_("Exiting because of an unresolved conflict."));
196 }
197
198 void NORETURN die_conclude_merge(void)
199 {
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."));
204 }
205
206 void detach_advice(const char *new_name)
207 {
208         const char *fmt =
209         _("Note: switching to '%s'.\n"
210         "\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"
214         "\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"
217         "\n"
218         "  git switch -c <new-branch-name>\n"
219         "\n"
220         "Or undo this operation with:\n"
221         "\n"
222         "  git switch -\n"
223         "\n"
224         "Turn off this advice by setting config variable advice.detachedHead to false\n\n");
225
226         fprintf(stderr, fmt, new_name);
227 }