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