Merge branch 'nd/corrupt-worktrees'
[git.git] / advice.c
blobce5f374ecd4917346145c8a380ac8c0e45e849a4
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;
29 int advice_nested_tag = 1;
31 static int advice_use_color = -1;
32 static char advice_colors[][COLOR_MAXLEN] = {
33 GIT_COLOR_RESET,
34 GIT_COLOR_YELLOW, /* HINT */
37 enum color_advice {
38 ADVICE_COLOR_RESET = 0,
39 ADVICE_COLOR_HINT = 1,
42 static int parse_advise_color_slot(const char *slot)
44 if (!strcasecmp(slot, "reset"))
45 return ADVICE_COLOR_RESET;
46 if (!strcasecmp(slot, "hint"))
47 return ADVICE_COLOR_HINT;
48 return -1;
51 static const char *advise_get_color(enum color_advice ix)
53 if (want_color_stderr(advice_use_color))
54 return advice_colors[ix];
55 return "";
58 static struct {
59 const char *name;
60 int *preference;
61 } advice_config[] = {
62 { "pushUpdateRejected", &advice_push_update_rejected },
63 { "pushNonFFCurrent", &advice_push_non_ff_current },
64 { "pushNonFFMatching", &advice_push_non_ff_matching },
65 { "pushAlreadyExists", &advice_push_already_exists },
66 { "pushFetchFirst", &advice_push_fetch_first },
67 { "pushNeedsForce", &advice_push_needs_force },
68 { "pushUnqualifiedRefName", &advice_push_unqualified_ref_name },
69 { "statusHints", &advice_status_hints },
70 { "statusUoption", &advice_status_u_option },
71 { "commitBeforeMerge", &advice_commit_before_merge },
72 { "resetQuiet", &advice_reset_quiet_warning },
73 { "resolveConflict", &advice_resolve_conflict },
74 { "implicitIdentity", &advice_implicit_identity },
75 { "detachedHead", &advice_detached_head },
76 { "setupStreamFailure", &advice_set_upstream_failure },
77 { "objectNameWarning", &advice_object_name_warning },
78 { "amWorkDir", &advice_amworkdir },
79 { "rmHints", &advice_rm_hints },
80 { "addEmbeddedRepo", &advice_add_embedded_repo },
81 { "ignoredHook", &advice_ignored_hook },
82 { "waitingForEditor", &advice_waiting_for_editor },
83 { "graftFileDeprecated", &advice_graft_file_deprecated },
84 { "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name },
85 { "nestedTag", &advice_nested_tag },
87 /* make this an alias for backward compatibility */
88 { "pushNonFastForward", &advice_push_update_rejected }
91 void advise(const char *advice, ...)
93 struct strbuf buf = STRBUF_INIT;
94 va_list params;
95 const char *cp, *np;
97 va_start(params, advice);
98 strbuf_vaddf(&buf, advice, params);
99 va_end(params);
101 for (cp = buf.buf; *cp; cp = np) {
102 np = strchrnul(cp, '\n');
103 fprintf(stderr, _("%shint: %.*s%s\n"),
104 advise_get_color(ADVICE_COLOR_HINT),
105 (int)(np - cp), cp,
106 advise_get_color(ADVICE_COLOR_RESET));
107 if (*np)
108 np++;
110 strbuf_release(&buf);
113 int git_default_advice_config(const char *var, const char *value)
115 const char *k, *slot_name;
116 int i;
118 if (!strcmp(var, "color.advice")) {
119 advice_use_color = git_config_colorbool(var, value);
120 return 0;
123 if (skip_prefix(var, "color.advice.", &slot_name)) {
124 int slot = parse_advise_color_slot(slot_name);
125 if (slot < 0)
126 return 0;
127 if (!value)
128 return config_error_nonbool(var);
129 return color_parse(value, advice_colors[slot]);
132 if (!skip_prefix(var, "advice.", &k))
133 return 0;
135 for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
136 if (strcasecmp(k, advice_config[i].name))
137 continue;
138 *advice_config[i].preference = git_config_bool(var, value);
139 return 0;
142 return 0;
145 void list_config_advices(struct string_list *list, const char *prefix)
147 int i;
149 for (i = 0; i < ARRAY_SIZE(advice_config); i++)
150 list_config_item(list, prefix, advice_config[i].name);
153 int error_resolve_conflict(const char *me)
155 if (!strcmp(me, "cherry-pick"))
156 error(_("Cherry-picking is not possible because you have unmerged files."));
157 else if (!strcmp(me, "commit"))
158 error(_("Committing is not possible because you have unmerged files."));
159 else if (!strcmp(me, "merge"))
160 error(_("Merging is not possible because you have unmerged files."));
161 else if (!strcmp(me, "pull"))
162 error(_("Pulling is not possible because you have unmerged files."));
163 else if (!strcmp(me, "revert"))
164 error(_("Reverting is not possible because you have unmerged files."));
165 else
166 error(_("It is not possible to %s because you have unmerged files."),
167 me);
169 if (advice_resolve_conflict)
171 * Message used both when 'git commit' fails and when
172 * other commands doing a merge do.
174 advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n"
175 "as appropriate to mark resolution and make a commit."));
176 return -1;
179 void NORETURN die_resolve_conflict(const char *me)
181 error_resolve_conflict(me);
182 die(_("Exiting because of an unresolved conflict."));
185 void NORETURN die_conclude_merge(void)
187 error(_("You have not concluded your merge (MERGE_HEAD exists)."));
188 if (advice_resolve_conflict)
189 advise(_("Please, commit your changes before merging."));
190 die(_("Exiting because of unfinished merge."));
193 void detach_advice(const char *new_name)
195 const char *fmt =
196 _("Note: checking out '%s'.\n\n"
197 "You are in 'detached HEAD' state. You can look around, make experimental\n"
198 "changes and commit them, and you can discard any commits you make in this\n"
199 "state without impacting any branches by performing another checkout.\n\n"
200 "If you want to create a new branch to retain commits you create, you may\n"
201 "do so (now or later) by using -b with the checkout command again. Example:\n\n"
202 " git checkout -b <new-branch-name>\n\n");
204 fprintf(stderr, fmt, new_name);