sequencer: advise if skipping cherry-picked commit
[git/debian.git] / advice.c
blobba6e703280d7ebc4f12e971b749109778cbc920d
1 #include "cache.h"
2 #include "config.h"
3 #include "color.h"
4 #include "help.h"
5 #include "string-list.h"
7 int advice_fetch_show_forced_updates = 1;
8 int advice_push_update_rejected = 1;
9 int advice_push_non_ff_current = 1;
10 int advice_push_non_ff_matching = 1;
11 int advice_push_already_exists = 1;
12 int advice_push_fetch_first = 1;
13 int advice_push_needs_force = 1;
14 int advice_push_unqualified_ref_name = 1;
15 int advice_push_ref_needs_update = 1;
16 int advice_status_hints = 1;
17 int advice_status_u_option = 1;
18 int advice_status_ahead_behind_warning = 1;
19 int advice_commit_before_merge = 1;
20 int advice_reset_quiet_warning = 1;
21 int advice_resolve_conflict = 1;
22 int advice_sequencer_in_use = 1;
23 int advice_implicit_identity = 1;
24 int advice_detached_head = 1;
25 int advice_set_upstream_failure = 1;
26 int advice_object_name_warning = 1;
27 int advice_amworkdir = 1;
28 int advice_rm_hints = 1;
29 int advice_add_embedded_repo = 1;
30 int advice_ignored_hook = 1;
31 int advice_waiting_for_editor = 1;
32 int advice_graft_file_deprecated = 1;
33 int advice_checkout_ambiguous_remote_branch_name = 1;
34 int advice_submodule_alternate_error_strategy_die = 1;
35 int advice_add_ignored_file = 1;
36 int advice_add_empty_pathspec = 1;
37 int advice_skipped_cherry_picks = 1;
39 static int advice_use_color = -1;
40 static char advice_colors[][COLOR_MAXLEN] = {
41 GIT_COLOR_RESET,
42 GIT_COLOR_YELLOW, /* HINT */
45 enum color_advice {
46 ADVICE_COLOR_RESET = 0,
47 ADVICE_COLOR_HINT = 1,
50 static int parse_advise_color_slot(const char *slot)
52 if (!strcasecmp(slot, "reset"))
53 return ADVICE_COLOR_RESET;
54 if (!strcasecmp(slot, "hint"))
55 return ADVICE_COLOR_HINT;
56 return -1;
59 static const char *advise_get_color(enum color_advice ix)
61 if (want_color_stderr(advice_use_color))
62 return advice_colors[ix];
63 return "";
66 static struct {
67 const char *name;
68 int *preference;
69 } advice_config[] = {
70 { "fetchShowForcedUpdates", &advice_fetch_show_forced_updates },
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 },
77 { "pushUnqualifiedRefName", &advice_push_unqualified_ref_name },
78 { "pushRefNeedsUpdate", &advice_push_ref_needs_update },
79 { "statusHints", &advice_status_hints },
80 { "statusUoption", &advice_status_u_option },
81 { "statusAheadBehindWarning", &advice_status_ahead_behind_warning },
82 { "commitBeforeMerge", &advice_commit_before_merge },
83 { "resetQuiet", &advice_reset_quiet_warning },
84 { "resolveConflict", &advice_resolve_conflict },
85 { "sequencerInUse", &advice_sequencer_in_use },
86 { "implicitIdentity", &advice_implicit_identity },
87 { "detachedHead", &advice_detached_head },
88 { "setUpstreamFailure", &advice_set_upstream_failure },
89 { "objectNameWarning", &advice_object_name_warning },
90 { "amWorkDir", &advice_amworkdir },
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 },
96 { "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name },
97 { "submoduleAlternateErrorStrategyDie", &advice_submodule_alternate_error_strategy_die },
98 { "addIgnoredFile", &advice_add_ignored_file },
99 { "addEmptyPathspec", &advice_add_empty_pathspec },
100 { "skippedCherryPicks", &advice_skipped_cherry_picks },
102 /* make this an alias for backward compatibility */
103 { "pushNonFastForward", &advice_push_update_rejected }
106 static 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 },
124 [ADVICE_PUSH_REF_NEEDS_UPDATE] = { "pushRefNeedsUpdate", 1 },
126 /* make this an alias for backward compatibility */
127 [ADVICE_PUSH_UPDATE_REJECTED_ALIAS] = { "pushNonFastForward", 1 },
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 },
138 [ADVICE_SKIPPED_CHERRY_PICKS] = { "skippedCherryPicks", 1 },
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 },
143 [ADVICE_UPDATE_SPARSE_PATH] = { "updateSparsePath", 1 },
144 [ADVICE_WAITING_FOR_EDITOR] = { "waitingForEditor", 1 },
147 static const char turn_off_instructions[] =
148 N_("\n"
149 "Disable this message with \"git config advice.%s false\"");
151 static void vadvise(const char *advice, int display_instructions,
152 const char *key, va_list params)
154 struct strbuf buf = STRBUF_INIT;
155 const char *cp, *np;
157 strbuf_vaddf(&buf, advice, params);
159 if (display_instructions)
160 strbuf_addf(&buf, turn_off_instructions, key);
162 for (cp = buf.buf; *cp; cp = np) {
163 np = strchrnul(cp, '\n');
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));
168 if (*np)
169 np++;
171 strbuf_release(&buf);
174 void advise(const char *advice, ...)
176 va_list params;
177 va_start(params, advice);
178 vadvise(advice, 0, "", params);
179 va_end(params);
182 int advice_enabled(enum advice_type type)
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;
193 void advise_if_enabled(enum advice_type type, const char *advice, ...)
195 va_list params;
197 if (!advice_enabled(type))
198 return;
200 va_start(params, advice);
201 vadvise(advice, 1, advice_setting[type].key, params);
202 va_end(params);
205 int git_default_advice_config(const char *var, const char *value)
207 const char *k, *slot_name;
208 int i;
210 if (!strcmp(var, "color.advice")) {
211 advice_use_color = git_config_colorbool(var, value);
212 return 0;
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]);
224 if (!skip_prefix(var, "advice.", &k))
225 return 0;
227 for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
228 if (strcasecmp(k, advice_config[i].name))
229 continue;
230 *advice_config[i].preference = git_config_bool(var, value);
231 break;
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);
238 return 0;
241 return 0;
244 void list_config_advices(struct string_list *list, const char *prefix)
246 int i;
248 for (i = 0; i < ARRAY_SIZE(advice_setting); i++)
249 list_config_item(list, prefix, advice_setting[i].key);
252 int error_resolve_conflict(const char *me)
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);
268 if (advice_resolve_conflict)
270 * Message used both when 'git commit' fails and when
271 * other commands doing a merge do.
273 advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n"
274 "as appropriate to mark resolution and make a commit."));
275 return -1;
278 void NORETURN die_resolve_conflict(const char *me)
280 error_resolve_conflict(me);
281 die(_("Exiting because of an unresolved conflict."));
284 void NORETURN die_conclude_merge(void)
286 error(_("You have not concluded your merge (MERGE_HEAD exists)."));
287 if (advice_resolve_conflict)
288 advise(_("Please, commit your changes before merging."));
289 die(_("Exiting because of unfinished merge."));
292 void advise_on_updating_sparse_paths(struct string_list *pathspec_list)
294 struct string_list_item *item;
296 if (!pathspec_list->nr)
297 return;
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);
305 advise_if_enabled(ADVICE_UPDATE_SPARSE_PATH,
306 _("Disable or modify the sparsity rules if you intend"
307 " to update such entries."));
310 void detach_advice(const char *new_name)
312 const char *fmt =
313 _("Note: switching to '%s'.\n"
314 "\n"
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"
317 "state without impacting any branches by switching back to a branch.\n"
318 "\n"
319 "If you want to create a new branch to retain commits you create, you may\n"
320 "do so (now or later) by using -c with the switch command. Example:\n"
321 "\n"
322 " git switch -c <new-branch-name>\n"
323 "\n"
324 "Or undo this operation with:\n"
325 "\n"
326 " git switch -\n"
327 "\n"
328 "Turn off this advice by setting config variable advice.detachedHead to false\n\n");
330 fprintf(stderr, fmt, new_name);