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