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