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