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