]> git.ipfire.org Git - thirdparty/git.git/blame - advice.c
advice: change "setupStreamFailure" to "setUpstreamFailure"
[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 },
fef0c76f 83 { "setUpstreamFailure", &advice_set_upstream_failure },
fb6fbffb 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
06ac2b3b 99static void vadvise(const char *advice, va_list params)
38ef61cf 100{
23cb5bf3 101 struct strbuf buf = STRBUF_INIT;
23cb5bf3 102 const char *cp, *np;
38ef61cf 103
447b99c8 104 strbuf_vaddf(&buf, advice, params);
23cb5bf3
JH
105
106 for (cp = buf.buf; *cp; cp = np) {
107 np = strchrnul(cp, '\n');
960786e7
RD
108 fprintf(stderr, _("%shint: %.*s%s\n"),
109 advise_get_color(ADVICE_COLOR_HINT),
110 (int)(np - cp), cp,
111 advise_get_color(ADVICE_COLOR_RESET));
23cb5bf3
JH
112 if (*np)
113 np++;
114 }
115 strbuf_release(&buf);
38ef61cf
RR
116}
117
06ac2b3b
HW
118void advise(const char *advice, ...)
119{
120 va_list params;
121 va_start(params, advice);
122 vadvise(advice, params);
123 va_end(params);
124}
125
75194438
JK
126int git_default_advice_config(const char *var, const char *value)
127{
960786e7 128 const char *k, *slot_name;
75194438
JK
129 int i;
130
960786e7
RD
131 if (!strcmp(var, "color.advice")) {
132 advice_use_color = git_config_colorbool(var, value);
133 return 0;
134 }
135
136 if (skip_prefix(var, "color.advice.", &slot_name)) {
137 int slot = parse_advise_color_slot(slot_name);
138 if (slot < 0)
139 return 0;
140 if (!value)
141 return config_error_nonbool(var);
142 return color_parse(value, advice_colors[slot]);
143 }
144
cf4fff57
JK
145 if (!skip_prefix(var, "advice.", &k))
146 return 0;
147
75194438 148 for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
fb6fbffb 149 if (strcasecmp(k, advice_config[i].name))
75194438
JK
150 continue;
151 *advice_config[i].preference = git_config_bool(var, value);
152 return 0;
153 }
154
155 return 0;
156}
d38a30df 157
3ac68a93
NTND
158void list_config_advices(struct string_list *list, const char *prefix)
159{
160 int i;
161
162 for (i = 0; i < ARRAY_SIZE(advice_config); i++)
163 list_config_item(list, prefix, advice_config[i].name);
164}
165
38ef61cf 166int error_resolve_conflict(const char *me)
d38a30df 167{
8785c425
VA
168 if (!strcmp(me, "cherry-pick"))
169 error(_("Cherry-picking is not possible because you have unmerged files."));
170 else if (!strcmp(me, "commit"))
171 error(_("Committing is not possible because you have unmerged files."));
172 else if (!strcmp(me, "merge"))
173 error(_("Merging is not possible because you have unmerged files."));
174 else if (!strcmp(me, "pull"))
175 error(_("Pulling is not possible because you have unmerged files."));
176 else if (!strcmp(me, "revert"))
177 error(_("Reverting is not possible because you have unmerged files."));
178 else
179 error(_("It is not possible to %s because you have unmerged files."),
180 me);
181
23cb5bf3 182 if (advice_resolve_conflict)
d38a30df
MM
183 /*
184 * Message used both when 'git commit' fails and when
185 * other commands doing a merge do.
186 */
c057b242 187 advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n"
91e70e00 188 "as appropriate to mark resolution and make a commit."));
38ef61cf
RR
189 return -1;
190}
191
192void NORETURN die_resolve_conflict(const char *me)
193{
194 error_resolve_conflict(me);
8785c425 195 die(_("Exiting because of an unresolved conflict."));
d38a30df 196}
2857093b 197
4a4cf9e8
PT
198void NORETURN die_conclude_merge(void)
199{
200 error(_("You have not concluded your merge (MERGE_HEAD exists)."));
201 if (advice_resolve_conflict)
b7447679 202 advise(_("Please, commit your changes before merging."));
4a4cf9e8
PT
203 die(_("Exiting because of unfinished merge."));
204}
205
2857093b
NTND
206void detach_advice(const char *new_name)
207{
e9f3cec4 208 const char *fmt =
328c6cb8 209 _("Note: switching to '%s'.\n"
af9ded5b 210 "\n"
2857093b
NTND
211 "You are in 'detached HEAD' state. You can look around, make experimental\n"
212 "changes and commit them, and you can discard any commits you make in this\n"
328c6cb8 213 "state without impacting any branches by switching back to a branch.\n"
af9ded5b 214 "\n"
2857093b 215 "If you want to create a new branch to retain commits you create, you may\n"
328c6cb8 216 "do so (now or later) by using -c with the switch command. Example:\n"
af9ded5b 217 "\n"
328c6cb8 218 " git switch -c <new-branch-name>\n"
af9ded5b 219 "\n"
328c6cb8 220 "Or undo this operation with:\n"
af9ded5b 221 "\n"
328c6cb8 222 " git switch -\n"
af9ded5b
NTND
223 "\n"
224 "Turn off this advice by setting config variable advice.detachedHead to false\n\n");
2857093b
NTND
225
226 fprintf(stderr, fmt, new_name);
227}