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