blob: 5f35656409b1d51abf111efa5bbcc7f5d570aaf0 [file] [log] [blame]
Jeff King75194432009-09-09 07:38:58 -04001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Ryan Dammrose960786e2018-04-21 12:10:00 +02003#include "color.h"
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +02004#include "help.h"
Jeff King75194432009-09-09 07:38:58 -04005
Chris Rorvick11845642012-12-02 21:27:50 -06006int advice_push_update_rejected = 1;
Christopher Tiwaldf25950f2012-03-20 00:31:33 -04007int advice_push_non_ff_current = 1;
Christopher Tiwaldf25950f2012-03-20 00:31:33 -04008int advice_push_non_ff_matching = 1;
Chris Rorvickb4505682012-12-02 21:27:51 -06009int advice_push_already_exists = 1;
Junio C Hamano75e5c0d2013-01-23 13:55:30 -080010int advice_push_fetch_first = 1;
11int advice_push_needs_force = 1;
Jeff Kingedf563f2009-09-09 07:43:03 -040012int advice_status_hints = 1;
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +070013int advice_status_u_option = 1;
Matthieu Moy4c371f92009-11-22 23:26:17 +010014int advice_commit_before_merge = 1;
Ben Peart649bf3a2018-10-23 15:04:23 -040015int advice_reset_quiet_warning = 1;
Matthieu Moyd38a30d2010-01-12 10:54:44 +010016int advice_resolve_conflict = 1;
Jeff Kingb706fcf2010-01-13 15:17:08 -050017int advice_implicit_identity = 1;
Junio C Hamano13be3e32010-01-29 22:03:24 -080018int advice_detached_head = 1;
Jeff Kingcaa20362013-04-02 15:05:12 -040019int advice_set_upstream_failure = 1;
Nguyễn Thái Ngọc Duy798c35f2013-05-29 19:12:42 +070020int advice_object_name_warning = 1;
Nguyễn Thái Ngọc Duy431bb232018-05-26 15:55:27 +020021int advice_amworkdir = 1;
Mathieu Lienard--Mayor7e309442013-06-12 10:06:44 +020022int advice_rm_hints = 1;
Jeff King53213992017-06-14 06:58:22 -040023int advice_add_embedded_repo = 1;
Damien Mariéf805a002017-10-06 08:07:55 +000024int advice_ignored_hook = 1;
Lars Schneiderabfb04d2017-12-07 16:16:41 +010025int advice_waiting_for_editor = 1;
Johannes Schindelinf9f99b32018-04-29 00:44:44 +020026int advice_graft_file_deprecated = 1;
Ævar Arnfjörð Bjarmasonad8d5102018-06-05 14:40:48 +000027int advice_checkout_ambiguous_remote_branch_name = 1;
Jeff King75194432009-09-09 07:38:58 -040028
Ryan Dammrose960786e2018-04-21 12:10:00 +020029static int advice_use_color = -1;
30static char advice_colors[][COLOR_MAXLEN] = {
31 GIT_COLOR_RESET,
32 GIT_COLOR_YELLOW, /* HINT */
33};
34
35enum color_advice {
36 ADVICE_COLOR_RESET = 0,
37 ADVICE_COLOR_HINT = 1,
38};
39
40static int parse_advise_color_slot(const char *slot)
41{
42 if (!strcasecmp(slot, "reset"))
43 return ADVICE_COLOR_RESET;
44 if (!strcasecmp(slot, "hint"))
45 return ADVICE_COLOR_HINT;
46 return -1;
47}
48
49static const char *advise_get_color(enum color_advice ix)
50{
51 if (want_color_stderr(advice_use_color))
52 return advice_colors[ix];
53 return "";
54}
55
Jeff King75194432009-09-09 07:38:58 -040056static struct {
57 const char *name;
58 int *preference;
59} advice_config[] = {
Nguyễn Thái Ngọc Duyfb6fbff2018-05-26 15:55:26 +020060 { "pushUpdateRejected", &advice_push_update_rejected },
61 { "pushNonFFCurrent", &advice_push_non_ff_current },
62 { "pushNonFFMatching", &advice_push_non_ff_matching },
63 { "pushAlreadyExists", &advice_push_already_exists },
64 { "pushFetchFirst", &advice_push_fetch_first },
65 { "pushNeedsForce", &advice_push_needs_force },
66 { "statusHints", &advice_status_hints },
67 { "statusUoption", &advice_status_u_option },
68 { "commitBeforeMerge", &advice_commit_before_merge },
Ben Peart649bf3a2018-10-23 15:04:23 -040069 { "resetQuiet", &advice_reset_quiet_warning },
Nguyễn Thái Ngọc Duyfb6fbff2018-05-26 15:55:26 +020070 { "resolveConflict", &advice_resolve_conflict },
71 { "implicitIdentity", &advice_implicit_identity },
72 { "detachedHead", &advice_detached_head },
73 { "setupStreamFailure", &advice_set_upstream_failure },
74 { "objectNameWarning", &advice_object_name_warning },
Nguyễn Thái Ngọc Duy431bb232018-05-26 15:55:27 +020075 { "amWorkDir", &advice_amworkdir },
Nguyễn Thái Ngọc Duyfb6fbff2018-05-26 15:55:26 +020076 { "rmHints", &advice_rm_hints },
77 { "addEmbeddedRepo", &advice_add_embedded_repo },
78 { "ignoredHook", &advice_ignored_hook },
79 { "waitingForEditor", &advice_waiting_for_editor },
80 { "graftFileDeprecated", &advice_graft_file_deprecated },
Junio C Hamano50858ed2018-08-02 15:30:41 -070081 { "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name },
Chris Rorvick11845642012-12-02 21:27:50 -060082
83 /* make this an alias for backward compatibility */
Nguyễn Thái Ngọc Duyfb6fbff2018-05-26 15:55:26 +020084 { "pushNonFastForward", &advice_push_update_rejected }
Jeff King75194432009-09-09 07:38:58 -040085};
86
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053087void advise(const char *advice, ...)
88{
Junio C Hamano23cb5bf2011-12-22 11:21:26 -080089 struct strbuf buf = STRBUF_INIT;
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053090 va_list params;
Junio C Hamano23cb5bf2011-12-22 11:21:26 -080091 const char *cp, *np;
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053092
93 va_start(params, advice);
Jeff King447b99c2012-07-23 14:48:57 -040094 strbuf_vaddf(&buf, advice, params);
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053095 va_end(params);
Junio C Hamano23cb5bf2011-12-22 11:21:26 -080096
97 for (cp = buf.buf; *cp; cp = np) {
98 np = strchrnul(cp, '\n');
Ryan Dammrose960786e2018-04-21 12:10:00 +020099 fprintf(stderr, _("%shint: %.*s%s\n"),
100 advise_get_color(ADVICE_COLOR_HINT),
101 (int)(np - cp), cp,
102 advise_get_color(ADVICE_COLOR_RESET));
Junio C Hamano23cb5bf2011-12-22 11:21:26 -0800103 if (*np)
104 np++;
105 }
106 strbuf_release(&buf);
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +0530107}
108
Jeff King75194432009-09-09 07:38:58 -0400109int git_default_advice_config(const char *var, const char *value)
110{
Ryan Dammrose960786e2018-04-21 12:10:00 +0200111 const char *k, *slot_name;
Jeff King75194432009-09-09 07:38:58 -0400112 int i;
113
Ryan Dammrose960786e2018-04-21 12:10:00 +0200114 if (!strcmp(var, "color.advice")) {
115 advice_use_color = git_config_colorbool(var, value);
116 return 0;
117 }
118
119 if (skip_prefix(var, "color.advice.", &slot_name)) {
120 int slot = parse_advise_color_slot(slot_name);
121 if (slot < 0)
122 return 0;
123 if (!value)
124 return config_error_nonbool(var);
125 return color_parse(value, advice_colors[slot]);
126 }
127
Jeff Kingcf4fff52014-06-18 15:44:19 -0400128 if (!skip_prefix(var, "advice.", &k))
129 return 0;
130
Jeff King75194432009-09-09 07:38:58 -0400131 for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
Nguyễn Thái Ngọc Duyfb6fbff2018-05-26 15:55:26 +0200132 if (strcasecmp(k, advice_config[i].name))
Jeff King75194432009-09-09 07:38:58 -0400133 continue;
134 *advice_config[i].preference = git_config_bool(var, value);
135 return 0;
136 }
137
138 return 0;
139}
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100140
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +0200141void list_config_advices(struct string_list *list, const char *prefix)
142{
143 int i;
144
145 for (i = 0; i < ARRAY_SIZE(advice_config); i++)
146 list_config_item(list, prefix, advice_config[i].name);
147}
148
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +0530149int error_resolve_conflict(const char *me)
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100150{
Vasco Almeida8785c422016-06-17 20:20:52 +0000151 if (!strcmp(me, "cherry-pick"))
152 error(_("Cherry-picking is not possible because you have unmerged files."));
153 else if (!strcmp(me, "commit"))
154 error(_("Committing is not possible because you have unmerged files."));
155 else if (!strcmp(me, "merge"))
156 error(_("Merging is not possible because you have unmerged files."));
157 else if (!strcmp(me, "pull"))
158 error(_("Pulling is not possible because you have unmerged files."));
159 else if (!strcmp(me, "revert"))
160 error(_("Reverting is not possible because you have unmerged files."));
161 else
162 error(_("It is not possible to %s because you have unmerged files."),
163 me);
164
Junio C Hamano23cb5bf2011-12-22 11:21:26 -0800165 if (advice_resolve_conflict)
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100166 /*
167 * Message used both when 'git commit' fails and when
168 * other commands doing a merge do.
169 */
Jeff Kingc057b242014-06-03 03:17:17 -0400170 advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n"
Matthieu Moy91e70e02014-08-28 11:46:58 +0200171 "as appropriate to mark resolution and make a commit."));
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +0530172 return -1;
173}
174
175void NORETURN die_resolve_conflict(const char *me)
176{
177 error_resolve_conflict(me);
Vasco Almeida8785c422016-06-17 20:20:52 +0000178 die(_("Exiting because of an unresolved conflict."));
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100179}
Nguyễn Thái Ngọc Duy28570932012-01-16 16:46:16 +0700180
Paul Tan4a4cf9e2015-06-18 18:54:04 +0800181void NORETURN die_conclude_merge(void)
182{
183 error(_("You have not concluded your merge (MERGE_HEAD exists)."));
184 if (advice_resolve_conflict)
Alex Henrieb7447672015-10-01 22:25:33 -0600185 advise(_("Please, commit your changes before merging."));
Paul Tan4a4cf9e2015-06-18 18:54:04 +0800186 die(_("Exiting because of unfinished merge."));
187}
188
Nguyễn Thái Ngọc Duy28570932012-01-16 16:46:16 +0700189void detach_advice(const char *new_name)
190{
Vasco Almeidae9f3cec2016-06-17 20:20:51 +0000191 const char *fmt =
192 _("Note: checking out '%s'.\n\n"
Nguyễn Thái Ngọc Duy28570932012-01-16 16:46:16 +0700193 "You are in 'detached HEAD' state. You can look around, make experimental\n"
194 "changes and commit them, and you can discard any commits you make in this\n"
195 "state without impacting any branches by performing another checkout.\n\n"
196 "If you want to create a new branch to retain commits you create, you may\n"
197 "do so (now or later) by using -b with the checkout command again. Example:\n\n"
Vasco Almeidae9f3cec2016-06-17 20:20:51 +0000198 " git checkout -b <new-branch-name>\n\n");
Nguyễn Thái Ngọc Duy28570932012-01-16 16:46:16 +0700199
200 fprintf(stderr, fmt, new_name);
201}