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