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