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