]>
Commit | Line | Data |
---|---|---|
75194438 | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
960786e7 | 3 | #include "color.h" |
3ac68a93 | 4 | #include "help.h" |
75194438 | 5 | |
377444b4 | 6 | int advice_fetch_show_forced_updates = 1; |
1184564e | 7 | int advice_push_update_rejected = 1; |
f25950f3 | 8 | int advice_push_non_ff_current = 1; |
f25950f3 | 9 | int advice_push_non_ff_matching = 1; |
b4505682 | 10 | int advice_push_already_exists = 1; |
75e5c0dc JH |
11 | int advice_push_fetch_first = 1; |
12 | int advice_push_needs_force = 1; | |
dd8dd300 | 13 | int advice_push_unqualified_ref_name = 1; |
edf563fb | 14 | int advice_status_hints = 1; |
6a38ef2c | 15 | int advice_status_u_option = 1; |
0a53561a | 16 | int advice_status_ahead_behind_warning = 1; |
4c371f91 | 17 | int advice_commit_before_merge = 1; |
649bf3a4 | 18 | int advice_reset_quiet_warning = 1; |
d38a30df | 19 | int advice_resolve_conflict = 1; |
6a1f9046 | 20 | int advice_sequencer_in_use = 1; |
b706fcfe | 21 | int advice_implicit_identity = 1; |
13be3e31 | 22 | int advice_detached_head = 1; |
caa2036b | 23 | int advice_set_upstream_failure = 1; |
798c35fc | 24 | int advice_object_name_warning = 1; |
431bb23a | 25 | int advice_amworkdir = 1; |
7e309446 | 26 | int advice_rm_hints = 1; |
53213994 | 27 | int advice_add_embedded_repo = 1; |
f805a00a | 28 | int advice_ignored_hook = 1; |
abfb04d0 | 29 | int advice_waiting_for_editor = 1; |
f9f99b3f | 30 | int advice_graft_file_deprecated = 1; |
ad8d5104 | 31 | int advice_checkout_ambiguous_remote_branch_name = 1; |
eea9c1e7 | 32 | int advice_nested_tag = 1; |
4f3e57ef | 33 | int advice_submodule_alternate_error_strategy_die = 1; |
887a0fd5 HW |
34 | int advice_add_ignored_file = 1; |
35 | int advice_add_empty_pathspec = 1; | |
75194438 | 36 | |
960786e7 RD |
37 | static int advice_use_color = -1; |
38 | static char advice_colors[][COLOR_MAXLEN] = { | |
39 | GIT_COLOR_RESET, | |
40 | GIT_COLOR_YELLOW, /* HINT */ | |
41 | }; | |
42 | ||
43 | enum color_advice { | |
44 | ADVICE_COLOR_RESET = 0, | |
45 | ADVICE_COLOR_HINT = 1, | |
46 | }; | |
47 | ||
48 | static 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 | ||
57 | static 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 |
64 | static 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 }, |
fb6fbffb NTND |
76 | { "statusHints", &advice_status_hints }, |
77 | { "statusUoption", &advice_status_u_option }, | |
0a53561a | 78 | { "statusAheadBehindWarning", &advice_status_ahead_behind_warning }, |
fb6fbffb | 79 | { "commitBeforeMerge", &advice_commit_before_merge }, |
649bf3a4 | 80 | { "resetQuiet", &advice_reset_quiet_warning }, |
fb6fbffb | 81 | { "resolveConflict", &advice_resolve_conflict }, |
6a1f9046 | 82 | { "sequencerInUse", &advice_sequencer_in_use }, |
fb6fbffb NTND |
83 | { "implicitIdentity", &advice_implicit_identity }, |
84 | { "detachedHead", &advice_detached_head }, | |
85 | { "setupStreamFailure", &advice_set_upstream_failure }, | |
86 | { "objectNameWarning", &advice_object_name_warning }, | |
431bb23a | 87 | { "amWorkDir", &advice_amworkdir }, |
fb6fbffb NTND |
88 | { "rmHints", &advice_rm_hints }, |
89 | { "addEmbeddedRepo", &advice_add_embedded_repo }, | |
90 | { "ignoredHook", &advice_ignored_hook }, | |
91 | { "waitingForEditor", &advice_waiting_for_editor }, | |
92 | { "graftFileDeprecated", &advice_graft_file_deprecated }, | |
50858edd | 93 | { "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name }, |
eea9c1e7 | 94 | { "nestedTag", &advice_nested_tag }, |
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 | ||
38ef61cf RR |
103 | void advise(const char *advice, ...) |
104 | { | |
23cb5bf3 | 105 | struct strbuf buf = STRBUF_INIT; |
38ef61cf | 106 | va_list params; |
23cb5bf3 | 107 | const char *cp, *np; |
38ef61cf RR |
108 | |
109 | va_start(params, advice); | |
447b99c8 | 110 | strbuf_vaddf(&buf, advice, params); |
38ef61cf | 111 | va_end(params); |
23cb5bf3 JH |
112 | |
113 | for (cp = buf.buf; *cp; cp = np) { | |
114 | np = strchrnul(cp, '\n'); | |
960786e7 RD |
115 | fprintf(stderr, _("%shint: %.*s%s\n"), |
116 | advise_get_color(ADVICE_COLOR_HINT), | |
117 | (int)(np - cp), cp, | |
118 | advise_get_color(ADVICE_COLOR_RESET)); | |
23cb5bf3 JH |
119 | if (*np) |
120 | np++; | |
121 | } | |
122 | strbuf_release(&buf); | |
38ef61cf RR |
123 | } |
124 | ||
75194438 JK |
125 | int git_default_advice_config(const char *var, const char *value) |
126 | { | |
960786e7 | 127 | const char *k, *slot_name; |
75194438 JK |
128 | int i; |
129 | ||
960786e7 RD |
130 | if (!strcmp(var, "color.advice")) { |
131 | advice_use_color = git_config_colorbool(var, value); | |
132 | return 0; | |
133 | } | |
134 | ||
135 | if (skip_prefix(var, "color.advice.", &slot_name)) { | |
136 | int slot = parse_advise_color_slot(slot_name); | |
137 | if (slot < 0) | |
138 | return 0; | |
139 | if (!value) | |
140 | return config_error_nonbool(var); | |
141 | return color_parse(value, advice_colors[slot]); | |
142 | } | |
143 | ||
cf4fff57 JK |
144 | if (!skip_prefix(var, "advice.", &k)) |
145 | return 0; | |
146 | ||
75194438 | 147 | for (i = 0; i < ARRAY_SIZE(advice_config); i++) { |
fb6fbffb | 148 | if (strcasecmp(k, advice_config[i].name)) |
75194438 JK |
149 | continue; |
150 | *advice_config[i].preference = git_config_bool(var, value); | |
151 | return 0; | |
152 | } | |
153 | ||
154 | return 0; | |
155 | } | |
d38a30df | 156 | |
3ac68a93 NTND |
157 | void list_config_advices(struct string_list *list, const char *prefix) |
158 | { | |
159 | int i; | |
160 | ||
161 | for (i = 0; i < ARRAY_SIZE(advice_config); i++) | |
162 | list_config_item(list, prefix, advice_config[i].name); | |
163 | } | |
164 | ||
38ef61cf | 165 | int error_resolve_conflict(const char *me) |
d38a30df | 166 | { |
8785c425 VA |
167 | if (!strcmp(me, "cherry-pick")) |
168 | error(_("Cherry-picking is not possible because you have unmerged files.")); | |
169 | else if (!strcmp(me, "commit")) | |
170 | error(_("Committing is not possible because you have unmerged files.")); | |
171 | else if (!strcmp(me, "merge")) | |
172 | error(_("Merging is not possible because you have unmerged files.")); | |
173 | else if (!strcmp(me, "pull")) | |
174 | error(_("Pulling is not possible because you have unmerged files.")); | |
175 | else if (!strcmp(me, "revert")) | |
176 | error(_("Reverting is not possible because you have unmerged files.")); | |
177 | else | |
178 | error(_("It is not possible to %s because you have unmerged files."), | |
179 | me); | |
180 | ||
23cb5bf3 | 181 | if (advice_resolve_conflict) |
d38a30df MM |
182 | /* |
183 | * Message used both when 'git commit' fails and when | |
184 | * other commands doing a merge do. | |
185 | */ | |
c057b242 | 186 | advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n" |
91e70e00 | 187 | "as appropriate to mark resolution and make a commit.")); |
38ef61cf RR |
188 | return -1; |
189 | } | |
190 | ||
191 | void NORETURN die_resolve_conflict(const char *me) | |
192 | { | |
193 | error_resolve_conflict(me); | |
8785c425 | 194 | die(_("Exiting because of an unresolved conflict.")); |
d38a30df | 195 | } |
2857093b | 196 | |
4a4cf9e8 PT |
197 | void NORETURN die_conclude_merge(void) |
198 | { | |
199 | error(_("You have not concluded your merge (MERGE_HEAD exists).")); | |
200 | if (advice_resolve_conflict) | |
b7447679 | 201 | advise(_("Please, commit your changes before merging.")); |
4a4cf9e8 PT |
202 | die(_("Exiting because of unfinished merge.")); |
203 | } | |
204 | ||
2857093b NTND |
205 | void detach_advice(const char *new_name) |
206 | { | |
e9f3cec4 | 207 | const char *fmt = |
328c6cb8 | 208 | _("Note: switching to '%s'.\n" |
af9ded5b | 209 | "\n" |
2857093b NTND |
210 | "You are in 'detached HEAD' state. You can look around, make experimental\n" |
211 | "changes and commit them, and you can discard any commits you make in this\n" | |
328c6cb8 | 212 | "state without impacting any branches by switching back to a branch.\n" |
af9ded5b | 213 | "\n" |
2857093b | 214 | "If you want to create a new branch to retain commits you create, you may\n" |
328c6cb8 | 215 | "do so (now or later) by using -c with the switch command. Example:\n" |
af9ded5b | 216 | "\n" |
328c6cb8 | 217 | " git switch -c <new-branch-name>\n" |
af9ded5b | 218 | "\n" |
328c6cb8 | 219 | "Or undo this operation with:\n" |
af9ded5b | 220 | "\n" |
328c6cb8 | 221 | " git switch -\n" |
af9ded5b NTND |
222 | "\n" |
223 | "Turn off this advice by setting config variable advice.detachedHead to false\n\n"); | |
2857093b NTND |
224 | |
225 | fprintf(stderr, fmt, new_name); | |
226 | } |