branch: mention start_name in set-upstream error messages
[git/git.git] / advice.c
CommitLineData
75194438
JK
1#include "cache.h"
2
1184564e 3int advice_push_update_rejected = 1;
f25950f3
CT
4int advice_push_non_ff_current = 1;
5int advice_push_non_ff_default = 1;
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;
75194438
JK
16
17static struct {
18 const char *name;
19 int *preference;
20} advice_config[] = {
1184564e 21 { "pushupdaterejected", &advice_push_update_rejected },
f25950f3
CT
22 { "pushnonffcurrent", &advice_push_non_ff_current },
23 { "pushnonffdefault", &advice_push_non_ff_default },
24 { "pushnonffmatching", &advice_push_non_ff_matching },
b4505682 25 { "pushalreadyexists", &advice_push_already_exists },
75e5c0dc
JH
26 { "pushfetchfirst", &advice_push_fetch_first },
27 { "pushneedsforce", &advice_push_needs_force },
edf563fb 28 { "statushints", &advice_status_hints },
6a38ef2c 29 { "statusuoption", &advice_status_u_option },
4c371f91 30 { "commitbeforemerge", &advice_commit_before_merge },
d38a30df 31 { "resolveconflict", &advice_resolve_conflict },
b706fcfe 32 { "implicitidentity", &advice_implicit_identity },
13be3e31 33 { "detachedhead", &advice_detached_head },
1184564e
CR
34
35 /* make this an alias for backward compatibility */
36 { "pushnonfastforward", &advice_push_update_rejected }
75194438
JK
37};
38
38ef61cf
RR
39void advise(const char *advice, ...)
40{
23cb5bf3 41 struct strbuf buf = STRBUF_INIT;
38ef61cf 42 va_list params;
23cb5bf3 43 const char *cp, *np;
38ef61cf
RR
44
45 va_start(params, advice);
447b99c8 46 strbuf_vaddf(&buf, advice, params);
38ef61cf 47 va_end(params);
23cb5bf3
JH
48
49 for (cp = buf.buf; *cp; cp = np) {
50 np = strchrnul(cp, '\n');
51 fprintf(stderr, _("hint: %.*s\n"), (int)(np - cp), cp);
52 if (*np)
53 np++;
54 }
55 strbuf_release(&buf);
38ef61cf
RR
56}
57
75194438
JK
58int git_default_advice_config(const char *var, const char *value)
59{
60 const char *k = skip_prefix(var, "advice.");
61 int i;
62
63 for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
64 if (strcmp(k, advice_config[i].name))
65 continue;
66 *advice_config[i].preference = git_config_bool(var, value);
67 return 0;
68 }
69
70 return 0;
71}
d38a30df 72
38ef61cf 73int error_resolve_conflict(const char *me)
d38a30df 74{
38ef61cf 75 error("'%s' is not possible because you have unmerged files.", me);
23cb5bf3 76 if (advice_resolve_conflict)
d38a30df
MM
77 /*
78 * Message used both when 'git commit' fails and when
79 * other commands doing a merge do.
80 */
23cb5bf3
JH
81 advise(_("Fix them up in the work tree,\n"
82 "and then use 'git add/rm <file>' as\n"
83 "appropriate to mark resolution and make a commit,\n"
84 "or use 'git commit -a'."));
38ef61cf
RR
85 return -1;
86}
87
88void NORETURN die_resolve_conflict(const char *me)
89{
90 error_resolve_conflict(me);
91 die("Exiting because of an unresolved conflict.");
d38a30df 92}
2857093b
NTND
93
94void detach_advice(const char *new_name)
95{
96 const char fmt[] =
97 "Note: checking out '%s'.\n\n"
98 "You are in 'detached HEAD' state. You can look around, make experimental\n"
99 "changes and commit them, and you can discard any commits you make in this\n"
100 "state without impacting any branches by performing another checkout.\n\n"
101 "If you want to create a new branch to retain commits you create, you may\n"
102 "do so (now or later) by using -b with the checkout command again. Example:\n\n"
103 " git checkout -b new_branch_name\n\n";
104
105 fprintf(stderr, fmt, new_name);
106}