blob: a8deee6e6419a1e867e06e90a70300cd2e01b556 [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;
5int advice_push_non_ff_default = 1;
6int 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;
Jeff King75194432009-09-09 07:38:58 -040017
18static struct {
19 const char *name;
20 int *preference;
21} advice_config[] = {
Chris Rorvick11845642012-12-02 21:27:50 -060022 { "pushupdaterejected", &advice_push_update_rejected },
Christopher Tiwaldf25950f2012-03-20 00:31:33 -040023 { "pushnonffcurrent", &advice_push_non_ff_current },
24 { "pushnonffdefault", &advice_push_non_ff_default },
25 { "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 },
Chris Rorvick11845642012-12-02 21:27:50 -060036
37 /* make this an alias for backward compatibility */
38 { "pushnonfastforward", &advice_push_update_rejected }
Jeff King75194432009-09-09 07:38:58 -040039};
40
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053041void advise(const char *advice, ...)
42{
Junio C Hamano23cb5bf2011-12-22 11:21:26 -080043 struct strbuf buf = STRBUF_INIT;
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053044 va_list params;
Junio C Hamano23cb5bf2011-12-22 11:21:26 -080045 const char *cp, *np;
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053046
47 va_start(params, advice);
Jeff King447b99c2012-07-23 14:48:57 -040048 strbuf_vaddf(&buf, advice, params);
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053049 va_end(params);
Junio C Hamano23cb5bf2011-12-22 11:21:26 -080050
51 for (cp = buf.buf; *cp; cp = np) {
52 np = strchrnul(cp, '\n');
53 fprintf(stderr, _("hint: %.*s\n"), (int)(np - cp), cp);
54 if (*np)
55 np++;
56 }
57 strbuf_release(&buf);
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053058}
59
Jeff King75194432009-09-09 07:38:58 -040060int git_default_advice_config(const char *var, const char *value)
61{
62 const char *k = skip_prefix(var, "advice.");
63 int i;
64
65 for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
66 if (strcmp(k, advice_config[i].name))
67 continue;
68 *advice_config[i].preference = git_config_bool(var, value);
69 return 0;
70 }
71
72 return 0;
73}
Matthieu Moyd38a30d2010-01-12 10:54:44 +010074
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053075int error_resolve_conflict(const char *me)
Matthieu Moyd38a30d2010-01-12 10:54:44 +010076{
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053077 error("'%s' is not possible because you have unmerged files.", me);
Junio C Hamano23cb5bf2011-12-22 11:21:26 -080078 if (advice_resolve_conflict)
Matthieu Moyd38a30d2010-01-12 10:54:44 +010079 /*
80 * Message used both when 'git commit' fails and when
81 * other commands doing a merge do.
82 */
Junio C Hamano23cb5bf2011-12-22 11:21:26 -080083 advise(_("Fix them up in the work tree,\n"
84 "and then use 'git add/rm <file>' as\n"
85 "appropriate to mark resolution and make a commit,\n"
86 "or use 'git commit -a'."));
Ramkumar Ramachandra38ef61c2011-08-04 16:08:59 +053087 return -1;
88}
89
90void NORETURN die_resolve_conflict(const char *me)
91{
92 error_resolve_conflict(me);
93 die("Exiting because of an unresolved conflict.");
Matthieu Moyd38a30d2010-01-12 10:54:44 +010094}
Nguyễn Thái Ngọc Duy28570932012-01-16 16:46:16 +070095
96void detach_advice(const char *new_name)
97{
98 const char fmt[] =
99 "Note: checking out '%s'.\n\n"
100 "You are in 'detached HEAD' state. You can look around, make experimental\n"
101 "changes and commit them, and you can discard any commits you make in this\n"
102 "state without impacting any branches by performing another checkout.\n\n"
103 "If you want to create a new branch to retain commits you create, you may\n"
104 "do so (now or later) by using -b with the checkout command again. Example:\n\n"
105 " git checkout -b new_branch_name\n\n";
106
107 fprintf(stderr, fmt, new_name);
108}