refactor "dumb" terminal determination
[git/git.git] / advice.c
CommitLineData
75194438 1#include "cache.h"
b2141fc1 2#include "config.h"
75194438 3
1184564e 4int advice_push_update_rejected = 1;
f25950f3 5int advice_push_non_ff_current = 1;
f25950f3 6int advice_push_non_ff_matching = 1;
b4505682 7int advice_push_already_exists = 1;
75e5c0dc
JH
8int advice_push_fetch_first = 1;
9int advice_push_needs_force = 1;
edf563fb 10int advice_status_hints = 1;
6a38ef2c 11int advice_status_u_option = 1;
4c371f91 12int advice_commit_before_merge = 1;
d38a30df 13int advice_resolve_conflict = 1;
b706fcfe 14int advice_implicit_identity = 1;
13be3e31 15int advice_detached_head = 1;
caa2036b 16int advice_set_upstream_failure = 1;
798c35fc 17int advice_object_name_warning = 1;
7e309446 18int advice_rm_hints = 1;
53213994 19int advice_add_embedded_repo = 1;
75194438
JK
20
21static struct {
22 const char *name;
23 int *preference;
24} advice_config[] = {
1184564e 25 { "pushupdaterejected", &advice_push_update_rejected },
f25950f3 26 { "pushnonffcurrent", &advice_push_non_ff_current },
f25950f3 27 { "pushnonffmatching", &advice_push_non_ff_matching },
b4505682 28 { "pushalreadyexists", &advice_push_already_exists },
75e5c0dc
JH
29 { "pushfetchfirst", &advice_push_fetch_first },
30 { "pushneedsforce", &advice_push_needs_force },
edf563fb 31 { "statushints", &advice_status_hints },
6a38ef2c 32 { "statusuoption", &advice_status_u_option },
4c371f91 33 { "commitbeforemerge", &advice_commit_before_merge },
d38a30df 34 { "resolveconflict", &advice_resolve_conflict },
b706fcfe 35 { "implicitidentity", &advice_implicit_identity },
13be3e31 36 { "detachedhead", &advice_detached_head },
caa2036b 37 { "setupstreamfailure", &advice_set_upstream_failure },
8dc84fdc 38 { "objectnamewarning", &advice_object_name_warning },
7e309446 39 { "rmhints", &advice_rm_hints },
53213994 40 { "addembeddedrepo", &advice_add_embedded_repo },
1184564e
CR
41
42 /* make this an alias for backward compatibility */
43 { "pushnonfastforward", &advice_push_update_rejected }
75194438
JK
44};
45
38ef61cf
RR
46void advise(const char *advice, ...)
47{
23cb5bf3 48 struct strbuf buf = STRBUF_INIT;
38ef61cf 49 va_list params;
23cb5bf3 50 const char *cp, *np;
38ef61cf
RR
51
52 va_start(params, advice);
447b99c8 53 strbuf_vaddf(&buf, advice, params);
38ef61cf 54 va_end(params);
23cb5bf3
JH
55
56 for (cp = buf.buf; *cp; cp = np) {
57 np = strchrnul(cp, '\n');
58 fprintf(stderr, _("hint: %.*s\n"), (int)(np - cp), cp);
59 if (*np)
60 np++;
61 }
62 strbuf_release(&buf);
38ef61cf
RR
63}
64
75194438
JK
65int git_default_advice_config(const char *var, const char *value)
66{
cf4fff57 67 const char *k;
75194438
JK
68 int i;
69
cf4fff57
JK
70 if (!skip_prefix(var, "advice.", &k))
71 return 0;
72
75194438
JK
73 for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
74 if (strcmp(k, advice_config[i].name))
75 continue;
76 *advice_config[i].preference = git_config_bool(var, value);
77 return 0;
78 }
79
80 return 0;
81}
d38a30df 82
38ef61cf 83int error_resolve_conflict(const char *me)
d38a30df 84{
8785c425
VA
85 if (!strcmp(me, "cherry-pick"))
86 error(_("Cherry-picking is not possible because you have unmerged files."));
87 else if (!strcmp(me, "commit"))
88 error(_("Committing is not possible because you have unmerged files."));
89 else if (!strcmp(me, "merge"))
90 error(_("Merging is not possible because you have unmerged files."));
91 else if (!strcmp(me, "pull"))
92 error(_("Pulling is not possible because you have unmerged files."));
93 else if (!strcmp(me, "revert"))
94 error(_("Reverting is not possible because you have unmerged files."));
95 else
96 error(_("It is not possible to %s because you have unmerged files."),
97 me);
98
23cb5bf3 99 if (advice_resolve_conflict)
d38a30df
MM
100 /*
101 * Message used both when 'git commit' fails and when
102 * other commands doing a merge do.
103 */
c057b242 104 advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n"
91e70e00 105 "as appropriate to mark resolution and make a commit."));
38ef61cf
RR
106 return -1;
107}
108
109void NORETURN die_resolve_conflict(const char *me)
110{
111 error_resolve_conflict(me);
8785c425 112 die(_("Exiting because of an unresolved conflict."));
d38a30df 113}
2857093b 114
4a4cf9e8
PT
115void NORETURN die_conclude_merge(void)
116{
117 error(_("You have not concluded your merge (MERGE_HEAD exists)."));
118 if (advice_resolve_conflict)
b7447679 119 advise(_("Please, commit your changes before merging."));
4a4cf9e8
PT
120 die(_("Exiting because of unfinished merge."));
121}
122
2857093b
NTND
123void detach_advice(const char *new_name)
124{
e9f3cec4
VA
125 const char *fmt =
126 _("Note: checking out '%s'.\n\n"
2857093b
NTND
127 "You are in 'detached HEAD' state. You can look around, make experimental\n"
128 "changes and commit them, and you can discard any commits you make in this\n"
129 "state without impacting any branches by performing another checkout.\n\n"
130 "If you want to create a new branch to retain commits you create, you may\n"
131 "do so (now or later) by using -b with the checkout command again. Example:\n\n"
e9f3cec4 132 " git checkout -b <new-branch-name>\n\n");
2857093b
NTND
133
134 fprintf(stderr, fmt, new_name);
135}