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