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