]> git.ipfire.org Git - thirdparty/git.git/blame - advice.c
sequencer: run post-commit hook
[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;
dd8dd300 12int advice_push_unqualified_ref_name = 1;
edf563fb 13int advice_status_hints = 1;
6a38ef2c 14int advice_status_u_option = 1;
4c371f91 15int advice_commit_before_merge = 1;
649bf3a4 16int advice_reset_quiet_warning = 1;
d38a30df 17int advice_resolve_conflict = 1;
b706fcfe 18int advice_implicit_identity = 1;
13be3e31 19int advice_detached_head = 1;
caa2036b 20int advice_set_upstream_failure = 1;
798c35fc 21int advice_object_name_warning = 1;
431bb23a 22int advice_amworkdir = 1;
7e309446 23int advice_rm_hints = 1;
53213994 24int advice_add_embedded_repo = 1;
f805a00a 25int advice_ignored_hook = 1;
abfb04d0 26int advice_waiting_for_editor = 1;
f9f99b3f 27int advice_graft_file_deprecated = 1;
ad8d5104 28int advice_checkout_ambiguous_remote_branch_name = 1;
eea9c1e7 29int advice_nested_tag = 1;
75194438 30
960786e7
RD
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
75194438
JK
58static struct {
59 const char *name;
60 int *preference;
61} advice_config[] = {
fb6fbffb
NTND
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 },
dd8dd300 68 { "pushUnqualifiedRefName", &advice_push_unqualified_ref_name },
fb6fbffb
NTND
69 { "statusHints", &advice_status_hints },
70 { "statusUoption", &advice_status_u_option },
71 { "commitBeforeMerge", &advice_commit_before_merge },
649bf3a4 72 { "resetQuiet", &advice_reset_quiet_warning },
fb6fbffb
NTND
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 },
431bb23a 78 { "amWorkDir", &advice_amworkdir },
fb6fbffb
NTND
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 },
50858edd 84 { "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name },
eea9c1e7 85 { "nestedTag", &advice_nested_tag },
1184564e
CR
86
87 /* make this an alias for backward compatibility */
fb6fbffb 88 { "pushNonFastForward", &advice_push_update_rejected }
75194438
JK
89};
90
38ef61cf
RR
91void advise(const char *advice, ...)
92{
23cb5bf3 93 struct strbuf buf = STRBUF_INIT;
38ef61cf 94 va_list params;
23cb5bf3 95 const char *cp, *np;
38ef61cf
RR
96
97 va_start(params, advice);
447b99c8 98 strbuf_vaddf(&buf, advice, params);
38ef61cf 99 va_end(params);
23cb5bf3
JH
100
101 for (cp = buf.buf; *cp; cp = np) {
102 np = strchrnul(cp, '\n');
960786e7
RD
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));
23cb5bf3
JH
107 if (*np)
108 np++;
109 }
110 strbuf_release(&buf);
38ef61cf
RR
111}
112
75194438
JK
113int git_default_advice_config(const char *var, const char *value)
114{
960786e7 115 const char *k, *slot_name;
75194438
JK
116 int i;
117
960786e7
RD
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
cf4fff57
JK
132 if (!skip_prefix(var, "advice.", &k))
133 return 0;
134
75194438 135 for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
fb6fbffb 136 if (strcasecmp(k, advice_config[i].name))
75194438
JK
137 continue;
138 *advice_config[i].preference = git_config_bool(var, value);
139 return 0;
140 }
141
142 return 0;
143}
d38a30df 144
3ac68a93
NTND
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
38ef61cf 153int error_resolve_conflict(const char *me)
d38a30df 154{
8785c425
VA
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
23cb5bf3 169 if (advice_resolve_conflict)
d38a30df
MM
170 /*
171 * Message used both when 'git commit' fails and when
172 * other commands doing a merge do.
173 */
c057b242 174 advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n"
91e70e00 175 "as appropriate to mark resolution and make a commit."));
38ef61cf
RR
176 return -1;
177}
178
179void NORETURN die_resolve_conflict(const char *me)
180{
181 error_resolve_conflict(me);
8785c425 182 die(_("Exiting because of an unresolved conflict."));
d38a30df 183}
2857093b 184
4a4cf9e8
PT
185void NORETURN die_conclude_merge(void)
186{
187 error(_("You have not concluded your merge (MERGE_HEAD exists)."));
188 if (advice_resolve_conflict)
b7447679 189 advise(_("Please, commit your changes before merging."));
4a4cf9e8
PT
190 die(_("Exiting because of unfinished merge."));
191}
192
2857093b
NTND
193void detach_advice(const char *new_name)
194{
e9f3cec4
VA
195 const char *fmt =
196 _("Note: checking out '%s'.\n\n"
2857093b
NTND
197 "You are in 'detached HEAD' state. You can look around, make experimental\n"
198 "changes and commit them, and you can discard any commits you make in this\n"
199 "state without impacting any branches by performing another checkout.\n\n"
200 "If you want to create a new branch to retain commits you create, you may\n"
201 "do so (now or later) by using -b with the checkout command again. Example:\n\n"
e9f3cec4 202 " git checkout -b <new-branch-name>\n\n");
2857093b
NTND
203
204 fprintf(stderr, fmt, new_name);
205}