parse-options: disable option abbreviation with PARSE_OPT_KEEP_UNKNOWN
[git.git] / advice.c
blob567209aa79afee0443dd0d7049343e1a004ae784
1 #include "cache.h"
2 #include "config.h"
3 #include "color.h"
4 #include "help.h"
6 int advice_push_update_rejected = 1;
7 int advice_push_non_ff_current = 1;
8 int advice_push_non_ff_matching = 1;
9 int advice_push_already_exists = 1;
10 int advice_push_fetch_first = 1;
11 int advice_push_needs_force = 1;
12 int advice_push_unqualified_ref_name = 1;
13 int advice_status_hints = 1;
14 int advice_status_u_option = 1;
15 int advice_commit_before_merge = 1;
16 int advice_reset_quiet_warning = 1;
17 int advice_resolve_conflict = 1;
18 int advice_implicit_identity = 1;
19 int advice_detached_head = 1;
20 int advice_set_upstream_failure = 1;
21 int advice_object_name_warning = 1;
22 int advice_amworkdir = 1;
23 int advice_rm_hints = 1;
24 int advice_add_embedded_repo = 1;
25 int advice_ignored_hook = 1;
26 int advice_waiting_for_editor = 1;
27 int advice_graft_file_deprecated = 1;
28 int advice_checkout_ambiguous_remote_branch_name = 1;
30 static int advice_use_color = -1;
31 static char advice_colors[][COLOR_MAXLEN] = {
32 GIT_COLOR_RESET,
33 GIT_COLOR_YELLOW, /* HINT */
36 enum color_advice {
37 ADVICE_COLOR_RESET = 0,
38 ADVICE_COLOR_HINT = 1,
41 static int parse_advise_color_slot(const char *slot)
43 if (!strcasecmp(slot, "reset"))
44 return ADVICE_COLOR_RESET;
45 if (!strcasecmp(slot, "hint"))
46 return ADVICE_COLOR_HINT;
47 return -1;
50 static const char *advise_get_color(enum color_advice ix)
52 if (want_color_stderr(advice_use_color))
53 return advice_colors[ix];
54 return "";
57 static struct {
58 const char *name;
59 int *preference;
60 } advice_config[] = {
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 },
67 { "pushUnqualifiedRefName", &advice_push_unqualified_ref_name },
68 { "statusHints", &advice_status_hints },
69 { "statusUoption", &advice_status_u_option },
70 { "commitBeforeMerge", &advice_commit_before_merge },
71 { "resetQuiet", &advice_reset_quiet_warning },
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 },
77 { "amWorkDir", &advice_amworkdir },
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 },
83 { "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name },
85 /* make this an alias for backward compatibility */
86 { "pushNonFastForward", &advice_push_update_rejected }
89 void advise(const char *advice, ...)
91 struct strbuf buf = STRBUF_INIT;
92 va_list params;
93 const char *cp, *np;
95 va_start(params, advice);
96 strbuf_vaddf(&buf, advice, params);
97 va_end(params);
99 for (cp = buf.buf; *cp; cp = np) {
100 np = strchrnul(cp, '\n');
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));
105 if (*np)
106 np++;
108 strbuf_release(&buf);
111 int git_default_advice_config(const char *var, const char *value)
113 const char *k, *slot_name;
114 int i;
116 if (!strcmp(var, "color.advice")) {
117 advice_use_color = git_config_colorbool(var, value);
118 return 0;
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]);
130 if (!skip_prefix(var, "advice.", &k))
131 return 0;
133 for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
134 if (strcasecmp(k, advice_config[i].name))
135 continue;
136 *advice_config[i].preference = git_config_bool(var, value);
137 return 0;
140 return 0;
143 void list_config_advices(struct string_list *list, const char *prefix)
145 int i;
147 for (i = 0; i < ARRAY_SIZE(advice_config); i++)
148 list_config_item(list, prefix, advice_config[i].name);
151 int error_resolve_conflict(const char *me)
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);
167 if (advice_resolve_conflict)
169 * Message used both when 'git commit' fails and when
170 * other commands doing a merge do.
172 advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n"
173 "as appropriate to mark resolution and make a commit."));
174 return -1;
177 void NORETURN die_resolve_conflict(const char *me)
179 error_resolve_conflict(me);
180 die(_("Exiting because of an unresolved conflict."));
183 void NORETURN die_conclude_merge(void)
185 error(_("You have not concluded your merge (MERGE_HEAD exists)."));
186 if (advice_resolve_conflict)
187 advise(_("Please, commit your changes before merging."));
188 die(_("Exiting because of unfinished merge."));
191 void detach_advice(const char *new_name)
193 const char *fmt =
194 _("Note: checking out '%s'.\n\n"
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"
200 " git checkout -b <new-branch-name>\n\n");
202 fprintf(stderr, fmt, new_name);