🌐 AI搜索 & 代理 主页
blob: f0a3d32d20687d1caadc1534aa15cf50543c27c0 [file] [log] [blame]
Jeff King75194432009-09-09 11:38:581#include "cache.h"
Brandon Williamsb2141fc2017-06-14 18:07:362#include "config.h"
Ryan Dammrose960786e2018-04-21 10:10:003#include "color.h"
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 13:55:244#include "help.h"
Jeff King75194432009-09-09 11:38:585
Derrick Stolee377444b2019-06-18 20:25:276int advice_fetch_show_forced_updates = 1;
Chris Rorvick11845642012-12-03 03:27:507int advice_push_update_rejected = 1;
Christopher Tiwaldf25950f2012-03-20 04:31:338int advice_push_non_ff_current = 1;
Christopher Tiwaldf25950f2012-03-20 04:31:339int advice_push_non_ff_matching = 1;
Chris Rorvickb4505682012-12-03 03:27:5110int advice_push_already_exists = 1;
Junio C Hamano75e5c0d2013-01-23 21:55:3011int advice_push_fetch_first = 1;
12int advice_push_needs_force = 1;
Ævar Arnfjörð Bjarmasondd8dd302018-11-13 19:52:4313int advice_push_unqualified_ref_name = 1;
Jeff Kingedf563f2009-09-09 11:43:0314int advice_status_hints = 1;
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 12:59:1615int advice_status_u_option = 1;
Jeff Hostetler0a535612019-06-18 20:21:2716int advice_status_ahead_behind_warning = 1;
Matthieu Moy4c371f92009-11-22 22:26:1717int advice_commit_before_merge = 1;
Ben Peart649bf3a2018-10-23 19:04:2318int advice_reset_quiet_warning = 1;
Matthieu Moyd38a30d2010-01-12 09:54:4419int advice_resolve_conflict = 1;
Rohit Ashiwal6a1f9042019-07-02 09:11:2520int advice_sequencer_in_use = 1;
Jeff Kingb706fcf2010-01-13 20:17:0821int advice_implicit_identity = 1;
Junio C Hamano13be3e32010-01-30 06:03:2422int advice_detached_head = 1;
Jeff Kingcaa20362013-04-02 19:05:1223int advice_set_upstream_failure = 1;
Nguyễn Thái Ngọc Duy798c35f2013-05-29 12:12:4224int advice_object_name_warning = 1;
Nguyễn Thái Ngọc Duy431bb232018-05-26 13:55:2725int advice_amworkdir = 1;
Mathieu Lienard--Mayor7e309442013-06-12 08:06:4426int advice_rm_hints = 1;
Jeff King53213992017-06-14 10:58:2227int advice_add_embedded_repo = 1;
Damien Mariéf805a002017-10-06 08:07:5528int advice_ignored_hook = 1;
Lars Schneiderabfb04d2017-12-07 15:16:4129int advice_waiting_for_editor = 1;
Johannes Schindelinf9f99b32018-04-28 22:44:4430int advice_graft_file_deprecated = 1;
Ævar Arnfjörð Bjarmasonad8d5102018-06-05 14:40:4831int advice_checkout_ambiguous_remote_branch_name = 1;
Jonathan Tan4f3e57e2019-12-02 19:57:5232int advice_submodule_alternate_error_strategy_die = 1;
Heba Waly887a0fd2020-02-06 10:57:3033int advice_add_ignored_file = 1;
34int advice_add_empty_pathspec = 1;
Jeff King75194432009-09-09 11:38:5835
Ryan Dammrose960786e2018-04-21 10:10:0036static int advice_use_color = -1;
37static char advice_colors[][COLOR_MAXLEN] = {
38 GIT_COLOR_RESET,
39 GIT_COLOR_YELLOW, /* HINT */
40};
41
42enum color_advice {
43 ADVICE_COLOR_RESET = 0,
44 ADVICE_COLOR_HINT = 1,
45};
46
47static int parse_advise_color_slot(const char *slot)
48{
49 if (!strcasecmp(slot, "reset"))
50 return ADVICE_COLOR_RESET;
51 if (!strcasecmp(slot, "hint"))
52 return ADVICE_COLOR_HINT;
53 return -1;
54}
55
56static const char *advise_get_color(enum color_advice ix)
57{
58 if (want_color_stderr(advice_use_color))
59 return advice_colors[ix];
60 return "";
61}
62
Jeff King75194432009-09-09 11:38:5863static struct {
64 const char *name;
65 int *preference;
66} advice_config[] = {
Derrick Stolee377444b2019-06-18 20:25:2767 { "fetchShowForcedUpdates", &advice_fetch_show_forced_updates },
Nguyễn Thái Ngọc Duyfb6fbff2018-05-26 13:55:2668 { "pushUpdateRejected", &advice_push_update_rejected },
69 { "pushNonFFCurrent", &advice_push_non_ff_current },
70 { "pushNonFFMatching", &advice_push_non_ff_matching },
71 { "pushAlreadyExists", &advice_push_already_exists },
72 { "pushFetchFirst", &advice_push_fetch_first },
73 { "pushNeedsForce", &advice_push_needs_force },
Ævar Arnfjörð Bjarmasondd8dd302018-11-13 19:52:4374 { "pushUnqualifiedRefName", &advice_push_unqualified_ref_name },
Nguyễn Thái Ngọc Duyfb6fbff2018-05-26 13:55:2675 { "statusHints", &advice_status_hints },
76 { "statusUoption", &advice_status_u_option },
Jeff Hostetler0a535612019-06-18 20:21:2777 { "statusAheadBehindWarning", &advice_status_ahead_behind_warning },
Nguyễn Thái Ngọc Duyfb6fbff2018-05-26 13:55:2678 { "commitBeforeMerge", &advice_commit_before_merge },
Ben Peart649bf3a2018-10-23 19:04:2379 { "resetQuiet", &advice_reset_quiet_warning },
Nguyễn Thái Ngọc Duyfb6fbff2018-05-26 13:55:2680 { "resolveConflict", &advice_resolve_conflict },
Rohit Ashiwal6a1f9042019-07-02 09:11:2581 { "sequencerInUse", &advice_sequencer_in_use },
Nguyễn Thái Ngọc Duyfb6fbff2018-05-26 13:55:2682 { "implicitIdentity", &advice_implicit_identity },
83 { "detachedHead", &advice_detached_head },
Heba Walyfef0c762020-03-02 20:01:5884 { "setUpstreamFailure", &advice_set_upstream_failure },
Nguyễn Thái Ngọc Duyfb6fbff2018-05-26 13:55:2685 { "objectNameWarning", &advice_object_name_warning },
Nguyễn Thái Ngọc Duy431bb232018-05-26 13:55:2786 { "amWorkDir", &advice_amworkdir },
Nguyễn Thái Ngọc Duyfb6fbff2018-05-26 13:55:2687 { "rmHints", &advice_rm_hints },
88 { "addEmbeddedRepo", &advice_add_embedded_repo },
89 { "ignoredHook", &advice_ignored_hook },
90 { "waitingForEditor", &advice_waiting_for_editor },
91 { "graftFileDeprecated", &advice_graft_file_deprecated },
Junio C Hamano50858ed2018-08-02 22:30:4192 { "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name },
Jonathan Tan4f3e57e2019-12-02 19:57:5293 { "submoduleAlternateErrorStrategyDie", &advice_submodule_alternate_error_strategy_die },
Heba Waly887a0fd2020-02-06 10:57:3094 { "addIgnoredFile", &advice_add_ignored_file },
95 { "addEmptyPathspec", &advice_add_empty_pathspec },
Chris Rorvick11845642012-12-03 03:27:5096
97 /* make this an alias for backward compatibility */
Nguyễn Thái Ngọc Duyfb6fbff2018-05-26 13:55:2698 { "pushNonFastForward", &advice_push_update_rejected }
Jeff King75194432009-09-09 11:38:5899};
100
Heba Walyb3b18d12020-03-02 20:01:59101static struct {
102 const char *key;
103 int enabled;
104} advice_setting[] = {
105 [ADVICE_ADD_EMBEDDED_REPO] = { "addEmbeddedRepo", 1 },
106 [ADVICE_AM_WORK_DIR] = { "amWorkDir", 1 },
107 [ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME] = { "checkoutAmbiguousRemoteBranchName", 1 },
108 [ADVICE_COMMIT_BEFORE_MERGE] = { "commitBeforeMerge", 1 },
109 [ADVICE_DETACHED_HEAD] = { "detachedHead", 1 },
110 [ADVICE_FETCH_SHOW_FORCED_UPDATES] = { "fetchShowForcedUpdates", 1 },
111 [ADVICE_GRAFT_FILE_DEPRECATED] = { "graftFileDeprecated", 1 },
112 [ADVICE_IGNORED_HOOK] = { "ignoredHook", 1 },
113 [ADVICE_IMPLICIT_IDENTITY] = { "implicitIdentity", 1 },
114 [ADVICE_NESTED_TAG] = { "nestedTag", 1 },
115 [ADVICE_OBJECT_NAME_WARNING] = { "objectNameWarning", 1 },
116 [ADVICE_PUSH_ALREADY_EXISTS] = { "pushAlreadyExists", 1 },
117 [ADVICE_PUSH_FETCH_FIRST] = { "pushFetchFirst", 1 },
118 [ADVICE_PUSH_NEEDS_FORCE] = { "pushNeedsForce", 1 },
119
120 /* make this an alias for backward compatibility */
121 [ADVICE_PUSH_UPDATE_REJECTED_ALIAS] = { "pushNonFastForward", 1 },
122
123 [ADVICE_PUSH_NON_FF_CURRENT] = { "pushNonFFCurrent", 1 },
124 [ADVICE_PUSH_NON_FF_MATCHING] = { "pushNonFFMatching", 1 },
125 [ADVICE_PUSH_UNQUALIFIED_REF_NAME] = { "pushUnqualifiedRefName", 1 },
126 [ADVICE_PUSH_UPDATE_REJECTED] = { "pushUpdateRejected", 1 },
127 [ADVICE_RESET_QUIET_WARNING] = { "resetQuiet", 1 },
128 [ADVICE_RESOLVE_CONFLICT] = { "resolveConflict", 1 },
129 [ADVICE_RM_HINTS] = { "rmHints", 1 },
130 [ADVICE_SEQUENCER_IN_USE] = { "sequencerInUse", 1 },
131 [ADVICE_SET_UPSTREAM_FAILURE] = { "setUpstreamFailure", 1 },
132 [ADVICE_STATUS_AHEAD_BEHIND_WARNING] = { "statusAheadBehindWarning", 1 },
133 [ADVICE_STATUS_HINTS] = { "statusHints", 1 },
134 [ADVICE_STATUS_U_OPTION] = { "statusUoption", 1 },
135 [ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = { "submoduleAlternateErrorStrategyDie", 1 },
136 [ADVICE_WAITING_FOR_EDITOR] = { "waitingForEditor", 1 },
137};
138
139static const char turn_off_instructions[] =
140N_("\n"
141 "Disable this message with \"git config advice.%s false\"");
142
143static void vadvise(const char *advice, int display_instructions,
144 const char *key, va_list params)
Ramkumar Ramachandra38ef61c2011-08-04 10:38:59145{
Junio C Hamano23cb5bf2011-12-22 19:21:26146 struct strbuf buf = STRBUF_INIT;
Junio C Hamano23cb5bf2011-12-22 19:21:26147 const char *cp, *np;
Ramkumar Ramachandra38ef61c2011-08-04 10:38:59148
Jeff King447b99c2012-07-23 18:48:57149 strbuf_vaddf(&buf, advice, params);
Junio C Hamano23cb5bf2011-12-22 19:21:26150
Heba Walyb3b18d12020-03-02 20:01:59151 if (display_instructions)
152 strbuf_addf(&buf, turn_off_instructions, key);
Junio C Hamano23cb5bf2011-12-22 19:21:26153
154 for (cp = buf.buf; *cp; cp = np) {
155 np = strchrnul(cp, '\n');
Ryan Dammrose960786e2018-04-21 10:10:00156 fprintf(stderr, _("%shint: %.*s%s\n"),
157 advise_get_color(ADVICE_COLOR_HINT),
158 (int)(np - cp), cp,
159 advise_get_color(ADVICE_COLOR_RESET));
Junio C Hamano23cb5bf2011-12-22 19:21:26160 if (*np)
161 np++;
162 }
163 strbuf_release(&buf);
Ramkumar Ramachandra38ef61c2011-08-04 10:38:59164}
165
Heba Waly06ac2b32020-03-02 20:01:57166void advise(const char *advice, ...)
167{
168 va_list params;
169 va_start(params, advice);
Heba Walyb3b18d12020-03-02 20:01:59170 vadvise(advice, 0, "", params);
171 va_end(params);
172}
173
174int advice_enabled(enum advice_type type)
175{
176 switch(type) {
177 case ADVICE_PUSH_UPDATE_REJECTED:
178 return advice_setting[ADVICE_PUSH_UPDATE_REJECTED].enabled &&
179 advice_setting[ADVICE_PUSH_UPDATE_REJECTED_ALIAS].enabled;
180 default:
181 return advice_setting[type].enabled;
182 }
183}
184
185void advise_if_enabled(enum advice_type type, const char *advice, ...)
186{
187 va_list params;
188
189 if (!advice_enabled(type))
190 return;
191
192 va_start(params, advice);
193 vadvise(advice, 1, advice_setting[type].key, params);
Heba Waly06ac2b32020-03-02 20:01:57194 va_end(params);
195}
196
Jeff King75194432009-09-09 11:38:58197int git_default_advice_config(const char *var, const char *value)
198{
Ryan Dammrose960786e2018-04-21 10:10:00199 const char *k, *slot_name;
Jeff King75194432009-09-09 11:38:58200 int i;
201
Ryan Dammrose960786e2018-04-21 10:10:00202 if (!strcmp(var, "color.advice")) {
203 advice_use_color = git_config_colorbool(var, value);
204 return 0;
205 }
206
207 if (skip_prefix(var, "color.advice.", &slot_name)) {
208 int slot = parse_advise_color_slot(slot_name);
209 if (slot < 0)
210 return 0;
211 if (!value)
212 return config_error_nonbool(var);
213 return color_parse(value, advice_colors[slot]);
214 }
215
Jeff Kingcf4fff52014-06-18 19:44:19216 if (!skip_prefix(var, "advice.", &k))
217 return 0;
218
Jeff King75194432009-09-09 11:38:58219 for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
Nguyễn Thái Ngọc Duyfb6fbff2018-05-26 13:55:26220 if (strcasecmp(k, advice_config[i].name))
Jeff King75194432009-09-09 11:38:58221 continue;
222 *advice_config[i].preference = git_config_bool(var, value);
Heba Walyb3b18d12020-03-02 20:01:59223 break;
224 }
225
226 for (i = 0; i < ARRAY_SIZE(advice_setting); i++) {
227 if (strcasecmp(k, advice_setting[i].key))
228 continue;
229 advice_setting[i].enabled = git_config_bool(var, value);
Jeff King75194432009-09-09 11:38:58230 return 0;
231 }
232
233 return 0;
234}
Matthieu Moyd38a30d2010-01-12 09:54:44235
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 13:55:24236void list_config_advices(struct string_list *list, const char *prefix)
237{
238 int i;
239
Heba Walyb3b18d12020-03-02 20:01:59240 for (i = 0; i < ARRAY_SIZE(advice_setting); i++)
241 list_config_item(list, prefix, advice_setting[i].key);
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 13:55:24242}
243
Ramkumar Ramachandra38ef61c2011-08-04 10:38:59244int error_resolve_conflict(const char *me)
Matthieu Moyd38a30d2010-01-12 09:54:44245{
Vasco Almeida8785c422016-06-17 20:20:52246 if (!strcmp(me, "cherry-pick"))
247 error(_("Cherry-picking is not possible because you have unmerged files."));
248 else if (!strcmp(me, "commit"))
249 error(_("Committing is not possible because you have unmerged files."));
250 else if (!strcmp(me, "merge"))
251 error(_("Merging is not possible because you have unmerged files."));
252 else if (!strcmp(me, "pull"))
253 error(_("Pulling is not possible because you have unmerged files."));
254 else if (!strcmp(me, "revert"))
255 error(_("Reverting is not possible because you have unmerged files."));
256 else
257 error(_("It is not possible to %s because you have unmerged files."),
258 me);
259
Junio C Hamano23cb5bf2011-12-22 19:21:26260 if (advice_resolve_conflict)
Matthieu Moyd38a30d2010-01-12 09:54:44261 /*
262 * Message used both when 'git commit' fails and when
263 * other commands doing a merge do.
264 */
Jeff Kingc057b242014-06-03 07:17:17265 advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n"
Matthieu Moy91e70e02014-08-28 09:46:58266 "as appropriate to mark resolution and make a commit."));
Ramkumar Ramachandra38ef61c2011-08-04 10:38:59267 return -1;
268}
269
270void NORETURN die_resolve_conflict(const char *me)
271{
272 error_resolve_conflict(me);
Vasco Almeida8785c422016-06-17 20:20:52273 die(_("Exiting because of an unresolved conflict."));
Matthieu Moyd38a30d2010-01-12 09:54:44274}
Nguyễn Thái Ngọc Duy28570932012-01-16 09:46:16275
Paul Tan4a4cf9e2015-06-18 10:54:04276void NORETURN die_conclude_merge(void)
277{
278 error(_("You have not concluded your merge (MERGE_HEAD exists)."));
279 if (advice_resolve_conflict)
Alex Henrieb7447672015-10-02 04:25:33280 advise(_("Please, commit your changes before merging."));
Paul Tan4a4cf9e2015-06-18 10:54:04281 die(_("Exiting because of unfinished merge."));
282}
283
Nguyễn Thái Ngọc Duy28570932012-01-16 09:46:16284void detach_advice(const char *new_name)
285{
Vasco Almeidae9f3cec2016-06-17 20:20:51286 const char *fmt =
Nguyễn Thái Ngọc Duy328c6cb2019-03-29 10:39:19287 _("Note: switching to '%s'.\n"
Nguyễn Thái Ngọc Duyaf9ded52019-03-29 10:38:58288 "\n"
Nguyễn Thái Ngọc Duy28570932012-01-16 09:46:16289 "You are in 'detached HEAD' state. You can look around, make experimental\n"
290 "changes and commit them, and you can discard any commits you make in this\n"
Nguyễn Thái Ngọc Duy328c6cb2019-03-29 10:39:19291 "state without impacting any branches by switching back to a branch.\n"
Nguyễn Thái Ngọc Duyaf9ded52019-03-29 10:38:58292 "\n"
Nguyễn Thái Ngọc Duy28570932012-01-16 09:46:16293 "If you want to create a new branch to retain commits you create, you may\n"
Nguyễn Thái Ngọc Duy328c6cb2019-03-29 10:39:19294 "do so (now or later) by using -c with the switch command. Example:\n"
Nguyễn Thái Ngọc Duyaf9ded52019-03-29 10:38:58295 "\n"
Nguyễn Thái Ngọc Duy328c6cb2019-03-29 10:39:19296 " git switch -c <new-branch-name>\n"
Nguyễn Thái Ngọc Duyaf9ded52019-03-29 10:38:58297 "\n"
Nguyễn Thái Ngọc Duy328c6cb2019-03-29 10:39:19298 "Or undo this operation with:\n"
Nguyễn Thái Ngọc Duyaf9ded52019-03-29 10:38:58299 "\n"
Nguyễn Thái Ngọc Duy328c6cb2019-03-29 10:39:19300 " git switch -\n"
Nguyễn Thái Ngọc Duyaf9ded52019-03-29 10:38:58301 "\n"
302 "Turn off this advice by setting config variable advice.detachedHead to false\n\n");
Nguyễn Thái Ngọc Duy28570932012-01-16 09:46:16303
304 fprintf(stderr, fmt, new_name);
305}