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