blob: 3fa04fca0b81ce0b4788ad3331ae759de805e0ff [file] [log] [blame]
Jeff King75194432009-09-09 07:38:58 -04001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Jeff King75194432009-09-09 07:38:58 -04003
Chris Rorvick11845642012-12-02 21:27:50 -06004int advice_push_update_rejected = 1;
Christopher Tiwaldf25950f2012-03-20 00:31:33 -04005int advice_push_non_ff_current = 1;
Christopher Tiwaldf25950f2012-03-20 00:31:33 -04006int advice_push_non_ff_matching = 1;
Chris Rorvickb4505682012-12-02 21:27:51 -06007int advice_push_already_exists = 1;
Junio C Hamano75e5c0d2013-01-23 13:55:30 -08008int advice_push_fetch_first = 1;
9int advice_push_needs_force = 1;
Jeff Kingedf563f2009-09-09 07:43:03 -040010int advice_status_hints = 1;
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +070011int advice_status_u_option = 1;
Matthieu Moy4c371f92009-11-22 23:26:17 +010012int advice_commit_before_merge = 1;
Matthieu Moyd38a30d2010-01-12 10:54:44 +010013int advice_resolve_conflict = 1;
Jeff Kingb706fcf2010-01-13 15:17:08 -050014int advice_implicit_identity = 1;
Junio C Hamano13be3e32010-01-29 22:03:24 -080015int advice_detached_head = 1;
Jeff Kingcaa20362013-04-02 15:05:12 -040016int advice_set_upstream_failure = 1;
Nguyễn Thái Ngọc Duy798c35f2013-05-29 19:12:42 +070017int advice_object_name_warning = 1;
Mathieu Lienard--Mayor7e309442013-06-12 10:06:44 +020018int advice_rm_hints = 1;
Jeff King75194432009-09-09 07:38:58 -040019
20static struct {
21 const char *name;
22 int *preference;
23} advice_config[] = {
Chris Rorvick11845642012-12-02 21:27:50 -060024 { "pushupdaterejected", &advice_push_update_rejected },
Christopher Tiwaldf25950f2012-03-20 00:31:33 -040025 { "pushnonffcurrent", &advice_push_non_ff_current },
Christopher Tiwaldf25950f2012-03-20 00:31:33 -040026 { "pushnonffmatching", &advice_push_non_ff_matching },
Chris Rorvickb4505682012-12-02 21:27:51 -060027 { "pushalreadyexists", &advice_push_already_exists },
Junio C Hamano75e5c0d2013-01-23 13:55:30 -080028 { "pushfetchfirst", &advice_push_fetch_first },
29 { "pushneedsforce", &advice_push_needs_force },
Jeff Kingedf563f2009-09-09 07:43:03 -040030 { "statushints", &advice_status_hints },
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +070031 { "statusuoption", &advice_status_u_option },
Matthieu Moy4c371f92009-11-22 23:26:17 +010032 { "commitbeforemerge", &advice_commit_before_merge },
Matthieu Moyd38a30d2010-01-12 10:54:44 +010033 { "resolveconflict", &advice_resolve_conflict },
Jeff Kingb706fcf2010-01-13 15:17:08 -050034 { "implicitidentity", &advice_implicit_identity },
Junio C Hamano13be3e32010-01-29 22:03:24 -080035 { "detachedhead", &advice_detached_head },
Jeff Kingcaa20362013-04-02 15:05:12 -040036 { "setupstreamfailure", &advice_set_upstream_failure },
Thomas Rast8dc84fd2013-07-31 22:23:31 +020037 { "objectnamewarning", &advice_object_name_warning },
Mathieu Lienard--Mayor7e309442013-06-12 10:06:44 +020038 { "rmhints", &advice_rm_hints },
Chris Rorvick11845642012-12-02 21:27:50 -060039
40 /* make this an alias for backward compatibility */
41 { "pushnonfastforward", &advice_push_update_rejected }
Jeff King75194432009-09-09 07:38:58 -040042};
43
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053044void advise(const char *advice, ...)
45{
Junio C Hamano23cb5bf2011-12-22 11:21:26 -080046 struct strbuf buf = STRBUF_INIT;
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053047 va_list params;
Junio C Hamano23cb5bf2011-12-22 11:21:26 -080048 const char *cp, *np;
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053049
50 va_start(params, advice);
Jeff King447b99c2012-07-23 14:48:57 -040051 strbuf_vaddf(&buf, advice, params);
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053052 va_end(params);
Junio C Hamano23cb5bf2011-12-22 11:21:26 -080053
54 for (cp = buf.buf; *cp; cp = np) {
55 np = strchrnul(cp, '\n');
56 fprintf(stderr, _("hint: %.*s\n"), (int)(np - cp), cp);
57 if (*np)
58 np++;
59 }
60 strbuf_release(&buf);
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053061}
62
Jeff King75194432009-09-09 07:38:58 -040063int git_default_advice_config(const char *var, const char *value)
64{
Jeff Kingcf4fff52014-06-18 15:44:19 -040065 const char *k;
Jeff King75194432009-09-09 07:38:58 -040066 int i;
67
Jeff Kingcf4fff52014-06-18 15:44:19 -040068 if (!skip_prefix(var, "advice.", &k))
69 return 0;
70
Jeff King75194432009-09-09 07:38:58 -040071 for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
72 if (strcmp(k, advice_config[i].name))
73 continue;
74 *advice_config[i].preference = git_config_bool(var, value);
75 return 0;
76 }
77
78 return 0;
79}
Matthieu Moyd38a30d2010-01-12 10:54:44 +010080
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053081int error_resolve_conflict(const char *me)
Matthieu Moyd38a30d2010-01-12 10:54:44 +010082{
Vasco Almeida8785c422016-06-17 20:20:52 +000083 if (!strcmp(me, "cherry-pick"))
84 error(_("Cherry-picking is not possible because you have unmerged files."));
85 else if (!strcmp(me, "commit"))
86 error(_("Committing is not possible because you have unmerged files."));
87 else if (!strcmp(me, "merge"))
88 error(_("Merging is not possible because you have unmerged files."));
89 else if (!strcmp(me, "pull"))
90 error(_("Pulling is not possible because you have unmerged files."));
91 else if (!strcmp(me, "revert"))
92 error(_("Reverting is not possible because you have unmerged files."));
93 else
94 error(_("It is not possible to %s because you have unmerged files."),
95 me);
96
Junio C Hamano23cb5bf2011-12-22 11:21:26 -080097 if (advice_resolve_conflict)
Matthieu Moyd38a30d2010-01-12 10:54:44 +010098 /*
99 * Message used both when 'git commit' fails and when
100 * other commands doing a merge do.
101 */
Jeff Kingc057b242014-06-03 03:17:17 -0400102 advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n"
Matthieu Moy91e70e02014-08-28 11:46:58 +0200103 "as appropriate to mark resolution and make a commit."));
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +0530104 return -1;
105}
106
107void NORETURN die_resolve_conflict(const char *me)
108{
109 error_resolve_conflict(me);
Vasco Almeida8785c422016-06-17 20:20:52 +0000110 die(_("Exiting because of an unresolved conflict."));
Matthieu Moyd38a30d2010-01-12 10:54:44 +0100111}
Nguyễn Thái Ngọc Duy28570932012-01-16 16:46:16 +0700112
Paul Tan4a4cf9e2015-06-18 18:54:04 +0800113void NORETURN die_conclude_merge(void)
114{
115 error(_("You have not concluded your merge (MERGE_HEAD exists)."));
116 if (advice_resolve_conflict)
Alex Henrieb7447672015-10-01 22:25:33 -0600117 advise(_("Please, commit your changes before merging."));
Paul Tan4a4cf9e2015-06-18 18:54:04 +0800118 die(_("Exiting because of unfinished merge."));
119}
120
Nguyễn Thái Ngọc Duy28570932012-01-16 16:46:16 +0700121void detach_advice(const char *new_name)
122{
Vasco Almeidae9f3cec2016-06-17 20:20:51 +0000123 const char *fmt =
124 _("Note: checking out '%s'.\n\n"
Nguyễn Thái Ngọc Duy28570932012-01-16 16:46:16 +0700125 "You are in 'detached HEAD' state. You can look around, make experimental\n"
126 "changes and commit them, and you can discard any commits you make in this\n"
127 "state without impacting any branches by performing another checkout.\n\n"
128 "If you want to create a new branch to retain commits you create, you may\n"
129 "do so (now or later) by using -b with the checkout command again. Example:\n\n"
Vasco Almeidae9f3cec2016-06-17 20:20:51 +0000130 " git checkout -b <new-branch-name>\n\n");
Nguyễn Thái Ngọc Duy28570932012-01-16 16:46:16 +0700131
132 fprintf(stderr, fmt, new_name);
133}