blob: 4dc5cf10a8533b1bfc3bd1483e91df115aa8f868 [file] [log] [blame]
Jeff King75194432009-09-09 07:38:58 -04001#include "cache.h"
2
Chris Rorvick11845642012-12-02 21:27:50 -06003int advice_push_update_rejected = 1;
Christopher Tiwaldf25950f2012-03-20 00:31:33 -04004int advice_push_non_ff_current = 1;
Christopher Tiwaldf25950f2012-03-20 00:31:33 -04005int advice_push_non_ff_matching = 1;
Chris Rorvickb4505682012-12-02 21:27:51 -06006int advice_push_already_exists = 1;
Junio C Hamano75e5c0d2013-01-23 13:55:30 -08007int advice_push_fetch_first = 1;
8int advice_push_needs_force = 1;
Jeff Kingedf563f2009-09-09 07:43:03 -04009int advice_status_hints = 1;
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +070010int advice_status_u_option = 1;
Matthieu Moy4c371f92009-11-22 23:26:17 +010011int advice_commit_before_merge = 1;
Matthieu Moyd38a30d2010-01-12 10:54:44 +010012int advice_resolve_conflict = 1;
Jeff Kingb706fcf2010-01-13 15:17:08 -050013int advice_implicit_identity = 1;
Junio C Hamano13be3e32010-01-29 22:03:24 -080014int advice_detached_head = 1;
Jeff Kingcaa20362013-04-02 15:05:12 -040015int advice_set_upstream_failure = 1;
Nguyễn Thái Ngọc Duy798c35f2013-05-29 19:12:42 +070016int advice_object_name_warning = 1;
Mathieu Lienard--Mayor7e309442013-06-12 10:06:44 +020017int advice_rm_hints = 1;
Jeff King75194432009-09-09 07:38:58 -040018
19static struct {
20 const char *name;
21 int *preference;
22} advice_config[] = {
Chris Rorvick11845642012-12-02 21:27:50 -060023 { "pushupdaterejected", &advice_push_update_rejected },
Christopher Tiwaldf25950f2012-03-20 00:31:33 -040024 { "pushnonffcurrent", &advice_push_non_ff_current },
Christopher Tiwaldf25950f2012-03-20 00:31:33 -040025 { "pushnonffmatching", &advice_push_non_ff_matching },
Chris Rorvickb4505682012-12-02 21:27:51 -060026 { "pushalreadyexists", &advice_push_already_exists },
Junio C Hamano75e5c0d2013-01-23 13:55:30 -080027 { "pushfetchfirst", &advice_push_fetch_first },
28 { "pushneedsforce", &advice_push_needs_force },
Jeff Kingedf563f2009-09-09 07:43:03 -040029 { "statushints", &advice_status_hints },
Nguyễn Thái Ngọc Duy6a38ef22013-03-13 19:59:16 +070030 { "statusuoption", &advice_status_u_option },
Matthieu Moy4c371f92009-11-22 23:26:17 +010031 { "commitbeforemerge", &advice_commit_before_merge },
Matthieu Moyd38a30d2010-01-12 10:54:44 +010032 { "resolveconflict", &advice_resolve_conflict },
Jeff Kingb706fcf2010-01-13 15:17:08 -050033 { "implicitidentity", &advice_implicit_identity },
Junio C Hamano13be3e32010-01-29 22:03:24 -080034 { "detachedhead", &advice_detached_head },
Jeff Kingcaa20362013-04-02 15:05:12 -040035 { "setupstreamfailure", &advice_set_upstream_failure },
Thomas Rast8dc84fd2013-07-31 22:23:31 +020036 { "objectnamewarning", &advice_object_name_warning },
Mathieu Lienard--Mayor7e309442013-06-12 10:06:44 +020037 { "rmhints", &advice_rm_hints },
Chris Rorvick11845642012-12-02 21:27:50 -060038
39 /* make this an alias for backward compatibility */
40 { "pushnonfastforward", &advice_push_update_rejected }
Jeff King75194432009-09-09 07:38:58 -040041};
42
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053043void advise(const char *advice, ...)
44{
Junio C Hamano23cb5bf2011-12-22 11:21:26 -080045 struct strbuf buf = STRBUF_INIT;
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053046 va_list params;
Junio C Hamano23cb5bf2011-12-22 11:21:26 -080047 const char *cp, *np;
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053048
49 va_start(params, advice);
Jeff King447b99c2012-07-23 14:48:57 -040050 strbuf_vaddf(&buf, advice, params);
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053051 va_end(params);
Junio C Hamano23cb5bf2011-12-22 11:21:26 -080052
53 for (cp = buf.buf; *cp; cp = np) {
54 np = strchrnul(cp, '\n');
55 fprintf(stderr, _("hint: %.*s\n"), (int)(np - cp), cp);
56 if (*np)
57 np++;
58 }
59 strbuf_release(&buf);
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053060}
61
Jeff King75194432009-09-09 07:38:58 -040062int git_default_advice_config(const char *var, const char *value)
63{
Jeff Kingcf4fff52014-06-18 15:44:19 -040064 const char *k;
Jeff King75194432009-09-09 07:38:58 -040065 int i;
66
Jeff Kingcf4fff52014-06-18 15:44:19 -040067 if (!skip_prefix(var, "advice.", &k))
68 return 0;
69
Jeff King75194432009-09-09 07:38:58 -040070 for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
71 if (strcmp(k, advice_config[i].name))
72 continue;
73 *advice_config[i].preference = git_config_bool(var, value);
74 return 0;
75 }
76
77 return 0;
78}
Matthieu Moyd38a30d2010-01-12 10:54:44 +010079
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053080int error_resolve_conflict(const char *me)
Matthieu Moyd38a30d2010-01-12 10:54:44 +010081{
Jeff Kingd7952162014-06-03 03:23:49 -040082 error("%s is not possible because you have unmerged files.", me);
Junio C Hamano23cb5bf2011-12-22 11:21:26 -080083 if (advice_resolve_conflict)
Matthieu Moyd38a30d2010-01-12 10:54:44 +010084 /*
85 * Message used both when 'git commit' fails and when
86 * other commands doing a merge do.
87 */
Jeff Kingc057b242014-06-03 03:17:17 -040088 advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n"
Matthieu Moy91e70e02014-08-28 11:46:58 +020089 "as appropriate to mark resolution and make a commit."));
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053090 return -1;
91}
92
93void NORETURN die_resolve_conflict(const char *me)
94{
95 error_resolve_conflict(me);
96 die("Exiting because of an unresolved conflict.");
Matthieu Moyd38a30d2010-01-12 10:54:44 +010097}
Nguyễn Thái Ngọc Duy28570932012-01-16 16:46:16 +070098
Paul Tan4a4cf9e2015-06-18 18:54:04 +080099void NORETURN die_conclude_merge(void)
100{
101 error(_("You have not concluded your merge (MERGE_HEAD exists)."));
102 if (advice_resolve_conflict)
Alex Henrieb7447672015-10-01 22:25:33 -0600103 advise(_("Please, commit your changes before merging."));
Paul Tan4a4cf9e2015-06-18 18:54:04 +0800104 die(_("Exiting because of unfinished merge."));
105}
106
Nguyễn Thái Ngọc Duy28570932012-01-16 16:46:16 +0700107void detach_advice(const char *new_name)
108{
109 const char fmt[] =
110 "Note: checking out '%s'.\n\n"
111 "You are in 'detached HEAD' state. You can look around, make experimental\n"
112 "changes and commit them, and you can discard any commits you make in this\n"
113 "state without impacting any branches by performing another checkout.\n\n"
114 "If you want to create a new branch to retain commits you create, you may\n"
115 "do so (now or later) by using -b with the checkout command again. Example:\n\n"
Alex Henrie9c9b4f22015-01-13 00:44:47 -0700116 " git checkout -b <new-branch-name>\n\n";
Nguyễn Thái Ngọc Duy28570932012-01-16 16:46:16 +0700117
118 fprintf(stderr, fmt, new_name);
119}