]> git.ipfire.org Git - thirdparty/git.git/blame - advice.c
fetch: do not pass ref-prefixes for fetch by exact SHA1
[thirdparty/git.git] / advice.c
CommitLineData
75194438 1#include "cache.h"
b2141fc1 2#include "config.h"
960786e7 3#include "color.h"
75194438 4
1184564e 5int advice_push_update_rejected = 1;
f25950f3 6int advice_push_non_ff_current = 1;
f25950f3 7int advice_push_non_ff_matching = 1;
b4505682 8int advice_push_already_exists = 1;
75e5c0dc
JH
9int advice_push_fetch_first = 1;
10int advice_push_needs_force = 1;
edf563fb 11int advice_status_hints = 1;
6a38ef2c 12int advice_status_u_option = 1;
4c371f91 13int advice_commit_before_merge = 1;
d38a30df 14int advice_resolve_conflict = 1;
b706fcfe 15int advice_implicit_identity = 1;
13be3e31 16int advice_detached_head = 1;
caa2036b 17int advice_set_upstream_failure = 1;
798c35fc 18int advice_object_name_warning = 1;
7e309446 19int advice_rm_hints = 1;
53213994 20int advice_add_embedded_repo = 1;
f805a00a 21int advice_ignored_hook = 1;
abfb04d0 22int advice_waiting_for_editor = 1;
75194438 23
960786e7
RD
24static int advice_use_color = -1;
25static char advice_colors[][COLOR_MAXLEN] = {
26 GIT_COLOR_RESET,
27 GIT_COLOR_YELLOW, /* HINT */
28};
29
30enum color_advice {
31 ADVICE_COLOR_RESET = 0,
32 ADVICE_COLOR_HINT = 1,
33};
34
35static int parse_advise_color_slot(const char *slot)
36{
37 if (!strcasecmp(slot, "reset"))
38 return ADVICE_COLOR_RESET;
39 if (!strcasecmp(slot, "hint"))
40 return ADVICE_COLOR_HINT;
41 return -1;
42}
43
44static const char *advise_get_color(enum color_advice ix)
45{
46 if (want_color_stderr(advice_use_color))
47 return advice_colors[ix];
48 return "";
49}
50
75194438
JK
51static struct {
52 const char *name;
53 int *preference;
54} advice_config[] = {
1184564e 55 { "pushupdaterejected", &advice_push_update_rejected },
f25950f3 56 { "pushnonffcurrent", &advice_push_non_ff_current },
f25950f3 57 { "pushnonffmatching", &advice_push_non_ff_matching },
b4505682 58 { "pushalreadyexists", &advice_push_already_exists },
75e5c0dc
JH
59 { "pushfetchfirst", &advice_push_fetch_first },
60 { "pushneedsforce", &advice_push_needs_force },
edf563fb 61 { "statushints", &advice_status_hints },
6a38ef2c 62 { "statusuoption", &advice_status_u_option },
4c371f91 63 { "commitbeforemerge", &advice_commit_before_merge },
d38a30df 64 { "resolveconflict", &advice_resolve_conflict },
b706fcfe 65 { "implicitidentity", &advice_implicit_identity },
13be3e31 66 { "detachedhead", &advice_detached_head },
caa2036b 67 { "setupstreamfailure", &advice_set_upstream_failure },
8dc84fdc 68 { "objectnamewarning", &advice_object_name_warning },
7e309446 69 { "rmhints", &advice_rm_hints },
53213994 70 { "addembeddedrepo", &advice_add_embedded_repo },
f805a00a 71 { "ignoredhook", &advice_ignored_hook },
abfb04d0 72 { "waitingforeditor", &advice_waiting_for_editor },
1184564e
CR
73
74 /* make this an alias for backward compatibility */
75 { "pushnonfastforward", &advice_push_update_rejected }
75194438
JK
76};
77
38ef61cf
RR
78void advise(const char *advice, ...)
79{
23cb5bf3 80 struct strbuf buf = STRBUF_INIT;
38ef61cf 81 va_list params;
23cb5bf3 82 const char *cp, *np;
38ef61cf
RR
83
84 va_start(params, advice);
447b99c8 85 strbuf_vaddf(&buf, advice, params);
38ef61cf 86 va_end(params);
23cb5bf3
JH
87
88 for (cp = buf.buf; *cp; cp = np) {
89 np = strchrnul(cp, '\n');
960786e7
RD
90 fprintf(stderr, _("%shint: %.*s%s\n"),
91 advise_get_color(ADVICE_COLOR_HINT),
92 (int)(np - cp), cp,
93 advise_get_color(ADVICE_COLOR_RESET));
23cb5bf3
JH
94 if (*np)
95 np++;
96 }
97 strbuf_release(&buf);
38ef61cf
RR
98}
99
75194438
JK
100int git_default_advice_config(const char *var, const char *value)
101{
960786e7 102 const char *k, *slot_name;
75194438
JK
103 int i;
104
960786e7
RD
105 if (!strcmp(var, "color.advice")) {
106 advice_use_color = git_config_colorbool(var, value);
107 return 0;
108 }
109
110 if (skip_prefix(var, "color.advice.", &slot_name)) {
111 int slot = parse_advise_color_slot(slot_name);
112 if (slot < 0)
113 return 0;
114 if (!value)
115 return config_error_nonbool(var);
116 return color_parse(value, advice_colors[slot]);
117 }
118
cf4fff57
JK
119 if (!skip_prefix(var, "advice.", &k))
120 return 0;
121
75194438
JK
122 for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
123 if (strcmp(k, advice_config[i].name))
124 continue;
125 *advice_config[i].preference = git_config_bool(var, value);
126 return 0;
127 }
128
129 return 0;
130}
d38a30df 131
38ef61cf 132int error_resolve_conflict(const char *me)
d38a30df 133{
8785c425
VA
134 if (!strcmp(me, "cherry-pick"))
135 error(_("Cherry-picking is not possible because you have unmerged files."));
136 else if (!strcmp(me, "commit"))
137 error(_("Committing is not possible because you have unmerged files."));
138 else if (!strcmp(me, "merge"))
139 error(_("Merging is not possible because you have unmerged files."));
140 else if (!strcmp(me, "pull"))
141 error(_("Pulling is not possible because you have unmerged files."));
142 else if (!strcmp(me, "revert"))
143 error(_("Reverting is not possible because you have unmerged files."));
144 else
145 error(_("It is not possible to %s because you have unmerged files."),
146 me);
147
23cb5bf3 148 if (advice_resolve_conflict)
d38a30df
MM
149 /*
150 * Message used both when 'git commit' fails and when
151 * other commands doing a merge do.
152 */
c057b242 153 advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n"
91e70e00 154 "as appropriate to mark resolution and make a commit."));
38ef61cf
RR
155 return -1;
156}
157
158void NORETURN die_resolve_conflict(const char *me)
159{
160 error_resolve_conflict(me);
8785c425 161 die(_("Exiting because of an unresolved conflict."));
d38a30df 162}
2857093b 163
4a4cf9e8
PT
164void NORETURN die_conclude_merge(void)
165{
166 error(_("You have not concluded your merge (MERGE_HEAD exists)."));
167 if (advice_resolve_conflict)
b7447679 168 advise(_("Please, commit your changes before merging."));
4a4cf9e8
PT
169 die(_("Exiting because of unfinished merge."));
170}
171
2857093b
NTND
172void detach_advice(const char *new_name)
173{
e9f3cec4
VA
174 const char *fmt =
175 _("Note: checking out '%s'.\n\n"
2857093b
NTND
176 "You are in 'detached HEAD' state. You can look around, make experimental\n"
177 "changes and commit them, and you can discard any commits you make in this\n"
178 "state without impacting any branches by performing another checkout.\n\n"
179 "If you want to create a new branch to retain commits you create, you may\n"
180 "do so (now or later) by using -b with the checkout command again. Example:\n\n"
e9f3cec4 181 " git checkout -b <new-branch-name>\n\n");
2857093b
NTND
182
183 fprintf(stderr, fmt, new_name);
184}