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