git-p4: narrow the scope of exceptions caught when parsing an int
[git.git] / advice.c
blob89fda1de55bfc80ba884ab45dd7a31087dea66df
1 #include "cache.h"
2 #include "config.h"
3 #include "color.h"
5 int advice_push_update_rejected = 1;
6 int advice_push_non_ff_current = 1;
7 int advice_push_non_ff_matching = 1;
8 int advice_push_already_exists = 1;
9 int advice_push_fetch_first = 1;
10 int advice_push_needs_force = 1;
11 int advice_status_hints = 1;
12 int advice_status_u_option = 1;
13 int advice_commit_before_merge = 1;
14 int advice_resolve_conflict = 1;
15 int advice_implicit_identity = 1;
16 int advice_detached_head = 1;
17 int advice_set_upstream_failure = 1;
18 int advice_object_name_warning = 1;
19 int advice_rm_hints = 1;
20 int advice_add_embedded_repo = 1;
21 int advice_ignored_hook = 1;
22 int advice_waiting_for_editor = 1;
24 static int advice_use_color = -1;
25 static char advice_colors[][COLOR_MAXLEN] = {
26 GIT_COLOR_RESET,
27 GIT_COLOR_YELLOW, /* HINT */
30 enum color_advice {
31 ADVICE_COLOR_RESET = 0,
32 ADVICE_COLOR_HINT = 1,
35 static int parse_advise_color_slot(const char *slot)
37 if (!strcasecmp(slot, "reset"))
38 return ADVICE_COLOR_RESET;
39 if (!strcasecmp(slot, "hint"))
40 return ADVICE_COLOR_HINT;
41 return -1;
44 static const char *advise_get_color(enum color_advice ix)
46 if (want_color_stderr(advice_use_color))
47 return advice_colors[ix];
48 return "";
51 static struct {
52 const char *name;
53 int *preference;
54 } advice_config[] = {
55 { "pushupdaterejected", &advice_push_update_rejected },
56 { "pushnonffcurrent", &advice_push_non_ff_current },
57 { "pushnonffmatching", &advice_push_non_ff_matching },
58 { "pushalreadyexists", &advice_push_already_exists },
59 { "pushfetchfirst", &advice_push_fetch_first },
60 { "pushneedsforce", &advice_push_needs_force },
61 { "statushints", &advice_status_hints },
62 { "statusuoption", &advice_status_u_option },
63 { "commitbeforemerge", &advice_commit_before_merge },
64 { "resolveconflict", &advice_resolve_conflict },
65 { "implicitidentity", &advice_implicit_identity },
66 { "detachedhead", &advice_detached_head },
67 { "setupstreamfailure", &advice_set_upstream_failure },
68 { "objectnamewarning", &advice_object_name_warning },
69 { "rmhints", &advice_rm_hints },
70 { "addembeddedrepo", &advice_add_embedded_repo },
71 { "ignoredhook", &advice_ignored_hook },
72 { "waitingforeditor", &advice_waiting_for_editor },
74 /* make this an alias for backward compatibility */
75 { "pushnonfastforward", &advice_push_update_rejected }
78 void advise(const char *advice, ...)
80 struct strbuf buf = STRBUF_INIT;
81 va_list params;
82 const char *cp, *np;
84 va_start(params, advice);
85 strbuf_vaddf(&buf, advice, params);
86 va_end(params);
88 for (cp = buf.buf; *cp; cp = np) {
89 np = strchrnul(cp, '\n');
90 fprintf(stderr, _("%shint: %.*s%s\n"),
91 advise_get_color(ADVICE_COLOR_HINT),
92 (int)(np - cp), cp,
93 advise_get_color(ADVICE_COLOR_RESET));
94 if (*np)
95 np++;
97 strbuf_release(&buf);
100 int git_default_advice_config(const char *var, const char *value)
102 const char *k, *slot_name;
103 int i;
105 if (!strcmp(var, "color.advice")) {
106 advice_use_color = git_config_colorbool(var, value);
107 return 0;
110 if (skip_prefix(var, "color.advice.", &slot_name)) {
111 int slot = parse_advise_color_slot(slot_name);
112 if (slot < 0)
113 return 0;
114 if (!value)
115 return config_error_nonbool(var);
116 return color_parse(value, advice_colors[slot]);
119 if (!skip_prefix(var, "advice.", &k))
120 return 0;
122 for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
123 if (strcmp(k, advice_config[i].name))
124 continue;
125 *advice_config[i].preference = git_config_bool(var, value);
126 return 0;
129 return 0;
132 int error_resolve_conflict(const char *me)
134 if (!strcmp(me, "cherry-pick"))
135 error(_("Cherry-picking is not possible because you have unmerged files."));
136 else if (!strcmp(me, "commit"))
137 error(_("Committing is not possible because you have unmerged files."));
138 else if (!strcmp(me, "merge"))
139 error(_("Merging is not possible because you have unmerged files."));
140 else if (!strcmp(me, "pull"))
141 error(_("Pulling is not possible because you have unmerged files."));
142 else if (!strcmp(me, "revert"))
143 error(_("Reverting is not possible because you have unmerged files."));
144 else
145 error(_("It is not possible to %s because you have unmerged files."),
146 me);
148 if (advice_resolve_conflict)
150 * Message used both when 'git commit' fails and when
151 * other commands doing a merge do.
153 advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n"
154 "as appropriate to mark resolution and make a commit."));
155 return -1;
158 void NORETURN die_resolve_conflict(const char *me)
160 error_resolve_conflict(me);
161 die(_("Exiting because of an unresolved conflict."));
164 void NORETURN die_conclude_merge(void)
166 error(_("You have not concluded your merge (MERGE_HEAD exists)."));
167 if (advice_resolve_conflict)
168 advise(_("Please, commit your changes before merging."));
169 die(_("Exiting because of unfinished merge."));
172 void detach_advice(const char *new_name)
174 const char *fmt =
175 _("Note: checking out '%s'.\n\n"
176 "You are in 'detached HEAD' state. You can look around, make experimental\n"
177 "changes and commit them, and you can discard any commits you make in this\n"
178 "state without impacting any branches by performing another checkout.\n\n"
179 "If you want to create a new branch to retain commits you create, you may\n"
180 "do so (now or later) by using -b with the checkout command again. Example:\n\n"
181 " git checkout -b <new-branch-name>\n\n");
183 fprintf(stderr, fmt, new_name);