Merge branch 'jk/misc-resolve-ref-unsafe-fixes' into maint
[git] / advice.c
1 #include "cache.h"
2 #include "config.h"
3
4 int advice_push_update_rejected = 1;
5 int advice_push_non_ff_current = 1;
6 int advice_push_non_ff_matching = 1;
7 int advice_push_already_exists = 1;
8 int advice_push_fetch_first = 1;
9 int advice_push_needs_force = 1;
10 int advice_status_hints = 1;
11 int advice_status_u_option = 1;
12 int advice_commit_before_merge = 1;
13 int advice_resolve_conflict = 1;
14 int advice_implicit_identity = 1;
15 int advice_detached_head = 1;
16 int advice_set_upstream_failure = 1;
17 int advice_object_name_warning = 1;
18 int advice_rm_hints = 1;
19 int advice_add_embedded_repo = 1;
20
21 static struct {
22         const char *name;
23         int *preference;
24 } advice_config[] = {
25         { "pushupdaterejected", &advice_push_update_rejected },
26         { "pushnonffcurrent", &advice_push_non_ff_current },
27         { "pushnonffmatching", &advice_push_non_ff_matching },
28         { "pushalreadyexists", &advice_push_already_exists },
29         { "pushfetchfirst", &advice_push_fetch_first },
30         { "pushneedsforce", &advice_push_needs_force },
31         { "statushints", &advice_status_hints },
32         { "statusuoption", &advice_status_u_option },
33         { "commitbeforemerge", &advice_commit_before_merge },
34         { "resolveconflict", &advice_resolve_conflict },
35         { "implicitidentity", &advice_implicit_identity },
36         { "detachedhead", &advice_detached_head },
37         { "setupstreamfailure", &advice_set_upstream_failure },
38         { "objectnamewarning", &advice_object_name_warning },
39         { "rmhints", &advice_rm_hints },
40         { "addembeddedrepo", &advice_add_embedded_repo },
41
42         /* make this an alias for backward compatibility */
43         { "pushnonfastforward", &advice_push_update_rejected }
44 };
45
46 void advise(const char *advice, ...)
47 {
48         struct strbuf buf = STRBUF_INIT;
49         va_list params;
50         const char *cp, *np;
51
52         va_start(params, advice);
53         strbuf_vaddf(&buf, advice, params);
54         va_end(params);
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);
63 }
64
65 int git_default_advice_config(const char *var, const char *value)
66 {
67         const char *k;
68         int i;
69
70         if (!skip_prefix(var, "advice.", &k))
71                 return 0;
72
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 }
82
83 int error_resolve_conflict(const char *me)
84 {
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
99         if (advice_resolve_conflict)
100                 /*
101                  * Message used both when 'git commit' fails and when
102                  * other commands doing a merge do.
103                  */
104                 advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n"
105                          "as appropriate to mark resolution and make a commit."));
106         return -1;
107 }
108
109 void NORETURN die_resolve_conflict(const char *me)
110 {
111         error_resolve_conflict(me);
112         die(_("Exiting because of an unresolved conflict."));
113 }
114
115 void NORETURN die_conclude_merge(void)
116 {
117         error(_("You have not concluded your merge (MERGE_HEAD exists)."));
118         if (advice_resolve_conflict)
119                 advise(_("Please, commit your changes before merging."));
120         die(_("Exiting because of unfinished merge."));
121 }
122
123 void detach_advice(const char *new_name)
124 {
125         const char *fmt =
126         _("Note: checking out '%s'.\n\n"
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"
132         "  git checkout -b <new-branch-name>\n\n");
133
134         fprintf(stderr, fmt, new_name);
135 }