Jeff King | 7519443 | 2009-09-09 07:38:58 -0400 | [diff] [blame] | 1 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 2 | #include "config.h" |
Ryan Dammrose | 960786e | 2018-04-21 12:10:00 +0200 | [diff] [blame] | 3 | #include "color.h" |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 4 | #include "help.h" |
Jeff King | 7519443 | 2009-09-09 07:38:58 -0400 | [diff] [blame] | 5 | |
Chris Rorvick | 1184564 | 2012-12-02 21:27:50 -0600 | [diff] [blame] | 6 | int advice_push_update_rejected = 1; |
Christopher Tiwald | f25950f | 2012-03-20 00:31:33 -0400 | [diff] [blame] | 7 | int advice_push_non_ff_current = 1; |
Christopher Tiwald | f25950f | 2012-03-20 00:31:33 -0400 | [diff] [blame] | 8 | int advice_push_non_ff_matching = 1; |
Chris Rorvick | b450568 | 2012-12-02 21:27:51 -0600 | [diff] [blame] | 9 | int advice_push_already_exists = 1; |
Junio C Hamano | 75e5c0d | 2013-01-23 13:55:30 -0800 | [diff] [blame] | 10 | int advice_push_fetch_first = 1; |
| 11 | int advice_push_needs_force = 1; |
Jeff King | edf563f | 2009-09-09 07:43:03 -0400 | [diff] [blame] | 12 | int advice_status_hints = 1; |
Nguyễn Thái Ngọc Duy | 6a38ef2 | 2013-03-13 19:59:16 +0700 | [diff] [blame] | 13 | int advice_status_u_option = 1; |
Matthieu Moy | 4c371f9 | 2009-11-22 23:26:17 +0100 | [diff] [blame] | 14 | int advice_commit_before_merge = 1; |
Ben Peart | 649bf3a | 2018-10-23 15:04:23 -0400 | [diff] [blame] | 15 | int advice_reset_quiet_warning = 1; |
Matthieu Moy | d38a30d | 2010-01-12 10:54:44 +0100 | [diff] [blame] | 16 | int advice_resolve_conflict = 1; |
Jeff King | b706fcf | 2010-01-13 15:17:08 -0500 | [diff] [blame] | 17 | int advice_implicit_identity = 1; |
Junio C Hamano | 13be3e3 | 2010-01-29 22:03:24 -0800 | [diff] [blame] | 18 | int advice_detached_head = 1; |
Jeff King | caa2036 | 2013-04-02 15:05:12 -0400 | [diff] [blame] | 19 | int advice_set_upstream_failure = 1; |
Nguyễn Thái Ngọc Duy | 798c35f | 2013-05-29 19:12:42 +0700 | [diff] [blame] | 20 | int advice_object_name_warning = 1; |
Nguyễn Thái Ngọc Duy | 431bb23 | 2018-05-26 15:55:27 +0200 | [diff] [blame] | 21 | int advice_amworkdir = 1; |
Mathieu Lienard--Mayor | 7e30944 | 2013-06-12 10:06:44 +0200 | [diff] [blame] | 22 | int advice_rm_hints = 1; |
Jeff King | 5321399 | 2017-06-14 06:58:22 -0400 | [diff] [blame] | 23 | int advice_add_embedded_repo = 1; |
Damien Marié | f805a00 | 2017-10-06 08:07:55 +0000 | [diff] [blame] | 24 | int advice_ignored_hook = 1; |
Lars Schneider | abfb04d | 2017-12-07 16:16:41 +0100 | [diff] [blame] | 25 | int advice_waiting_for_editor = 1; |
Johannes Schindelin | f9f99b3 | 2018-04-29 00:44:44 +0200 | [diff] [blame] | 26 | int advice_graft_file_deprecated = 1; |
Ævar Arnfjörð Bjarmason | ad8d510 | 2018-06-05 14:40:48 +0000 | [diff] [blame] | 27 | int advice_checkout_ambiguous_remote_branch_name = 1; |
Jeff King | 7519443 | 2009-09-09 07:38:58 -0400 | [diff] [blame] | 28 | |
Ryan Dammrose | 960786e | 2018-04-21 12:10:00 +0200 | [diff] [blame] | 29 | static int advice_use_color = -1; |
| 30 | static char advice_colors[][COLOR_MAXLEN] = { |
| 31 | GIT_COLOR_RESET, |
| 32 | GIT_COLOR_YELLOW, /* HINT */ |
| 33 | }; |
| 34 | |
| 35 | enum color_advice { |
| 36 | ADVICE_COLOR_RESET = 0, |
| 37 | ADVICE_COLOR_HINT = 1, |
| 38 | }; |
| 39 | |
| 40 | static int parse_advise_color_slot(const char *slot) |
| 41 | { |
| 42 | if (!strcasecmp(slot, "reset")) |
| 43 | return ADVICE_COLOR_RESET; |
| 44 | if (!strcasecmp(slot, "hint")) |
| 45 | return ADVICE_COLOR_HINT; |
| 46 | return -1; |
| 47 | } |
| 48 | |
| 49 | static const char *advise_get_color(enum color_advice ix) |
| 50 | { |
| 51 | if (want_color_stderr(advice_use_color)) |
| 52 | return advice_colors[ix]; |
| 53 | return ""; |
| 54 | } |
| 55 | |
Jeff King | 7519443 | 2009-09-09 07:38:58 -0400 | [diff] [blame] | 56 | static struct { |
| 57 | const char *name; |
| 58 | int *preference; |
| 59 | } advice_config[] = { |
Nguyễn Thái Ngọc Duy | fb6fbff | 2018-05-26 15:55:26 +0200 | [diff] [blame] | 60 | { "pushUpdateRejected", &advice_push_update_rejected }, |
| 61 | { "pushNonFFCurrent", &advice_push_non_ff_current }, |
| 62 | { "pushNonFFMatching", &advice_push_non_ff_matching }, |
| 63 | { "pushAlreadyExists", &advice_push_already_exists }, |
| 64 | { "pushFetchFirst", &advice_push_fetch_first }, |
| 65 | { "pushNeedsForce", &advice_push_needs_force }, |
| 66 | { "statusHints", &advice_status_hints }, |
| 67 | { "statusUoption", &advice_status_u_option }, |
| 68 | { "commitBeforeMerge", &advice_commit_before_merge }, |
Ben Peart | 649bf3a | 2018-10-23 15:04:23 -0400 | [diff] [blame] | 69 | { "resetQuiet", &advice_reset_quiet_warning }, |
Nguyễn Thái Ngọc Duy | fb6fbff | 2018-05-26 15:55:26 +0200 | [diff] [blame] | 70 | { "resolveConflict", &advice_resolve_conflict }, |
| 71 | { "implicitIdentity", &advice_implicit_identity }, |
| 72 | { "detachedHead", &advice_detached_head }, |
| 73 | { "setupStreamFailure", &advice_set_upstream_failure }, |
| 74 | { "objectNameWarning", &advice_object_name_warning }, |
Nguyễn Thái Ngọc Duy | 431bb23 | 2018-05-26 15:55:27 +0200 | [diff] [blame] | 75 | { "amWorkDir", &advice_amworkdir }, |
Nguyễn Thái Ngọc Duy | fb6fbff | 2018-05-26 15:55:26 +0200 | [diff] [blame] | 76 | { "rmHints", &advice_rm_hints }, |
| 77 | { "addEmbeddedRepo", &advice_add_embedded_repo }, |
| 78 | { "ignoredHook", &advice_ignored_hook }, |
| 79 | { "waitingForEditor", &advice_waiting_for_editor }, |
| 80 | { "graftFileDeprecated", &advice_graft_file_deprecated }, |
Junio C Hamano | 50858ed | 2018-08-02 15:30:41 -0700 | [diff] [blame] | 81 | { "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name }, |
Chris Rorvick | 1184564 | 2012-12-02 21:27:50 -0600 | [diff] [blame] | 82 | |
| 83 | /* make this an alias for backward compatibility */ |
Nguyễn Thái Ngọc Duy | fb6fbff | 2018-05-26 15:55:26 +0200 | [diff] [blame] | 84 | { "pushNonFastForward", &advice_push_update_rejected } |
Jeff King | 7519443 | 2009-09-09 07:38:58 -0400 | [diff] [blame] | 85 | }; |
| 86 | |
Ramkumar Ramachandra | 38ef61c | 2011-08-04 16:08:59 +0530 | [diff] [blame] | 87 | void advise(const char *advice, ...) |
| 88 | { |
Junio C Hamano | 23cb5bf | 2011-12-22 11:21:26 -0800 | [diff] [blame] | 89 | struct strbuf buf = STRBUF_INIT; |
Ramkumar Ramachandra | 38ef61c | 2011-08-04 16:08:59 +0530 | [diff] [blame] | 90 | va_list params; |
Junio C Hamano | 23cb5bf | 2011-12-22 11:21:26 -0800 | [diff] [blame] | 91 | const char *cp, *np; |
Ramkumar Ramachandra | 38ef61c | 2011-08-04 16:08:59 +0530 | [diff] [blame] | 92 | |
| 93 | va_start(params, advice); |
Jeff King | 447b99c | 2012-07-23 14:48:57 -0400 | [diff] [blame] | 94 | strbuf_vaddf(&buf, advice, params); |
Ramkumar Ramachandra | 38ef61c | 2011-08-04 16:08:59 +0530 | [diff] [blame] | 95 | va_end(params); |
Junio C Hamano | 23cb5bf | 2011-12-22 11:21:26 -0800 | [diff] [blame] | 96 | |
| 97 | for (cp = buf.buf; *cp; cp = np) { |
| 98 | np = strchrnul(cp, '\n'); |
Ryan Dammrose | 960786e | 2018-04-21 12:10:00 +0200 | [diff] [blame] | 99 | fprintf(stderr, _("%shint: %.*s%s\n"), |
| 100 | advise_get_color(ADVICE_COLOR_HINT), |
| 101 | (int)(np - cp), cp, |
| 102 | advise_get_color(ADVICE_COLOR_RESET)); |
Junio C Hamano | 23cb5bf | 2011-12-22 11:21:26 -0800 | [diff] [blame] | 103 | if (*np) |
| 104 | np++; |
| 105 | } |
| 106 | strbuf_release(&buf); |
Ramkumar Ramachandra | 38ef61c | 2011-08-04 16:08:59 +0530 | [diff] [blame] | 107 | } |
| 108 | |
Jeff King | 7519443 | 2009-09-09 07:38:58 -0400 | [diff] [blame] | 109 | int git_default_advice_config(const char *var, const char *value) |
| 110 | { |
Ryan Dammrose | 960786e | 2018-04-21 12:10:00 +0200 | [diff] [blame] | 111 | const char *k, *slot_name; |
Jeff King | 7519443 | 2009-09-09 07:38:58 -0400 | [diff] [blame] | 112 | int i; |
| 113 | |
Ryan Dammrose | 960786e | 2018-04-21 12:10:00 +0200 | [diff] [blame] | 114 | if (!strcmp(var, "color.advice")) { |
| 115 | advice_use_color = git_config_colorbool(var, value); |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | if (skip_prefix(var, "color.advice.", &slot_name)) { |
| 120 | int slot = parse_advise_color_slot(slot_name); |
| 121 | if (slot < 0) |
| 122 | return 0; |
| 123 | if (!value) |
| 124 | return config_error_nonbool(var); |
| 125 | return color_parse(value, advice_colors[slot]); |
| 126 | } |
| 127 | |
Jeff King | cf4fff5 | 2014-06-18 15:44:19 -0400 | [diff] [blame] | 128 | if (!skip_prefix(var, "advice.", &k)) |
| 129 | return 0; |
| 130 | |
Jeff King | 7519443 | 2009-09-09 07:38:58 -0400 | [diff] [blame] | 131 | for (i = 0; i < ARRAY_SIZE(advice_config); i++) { |
Nguyễn Thái Ngọc Duy | fb6fbff | 2018-05-26 15:55:26 +0200 | [diff] [blame] | 132 | if (strcasecmp(k, advice_config[i].name)) |
Jeff King | 7519443 | 2009-09-09 07:38:58 -0400 | [diff] [blame] | 133 | continue; |
| 134 | *advice_config[i].preference = git_config_bool(var, value); |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
Matthieu Moy | d38a30d | 2010-01-12 10:54:44 +0100 | [diff] [blame] | 140 | |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 141 | void list_config_advices(struct string_list *list, const char *prefix) |
| 142 | { |
| 143 | int i; |
| 144 | |
| 145 | for (i = 0; i < ARRAY_SIZE(advice_config); i++) |
| 146 | list_config_item(list, prefix, advice_config[i].name); |
| 147 | } |
| 148 | |
Ramkumar Ramachandra | 38ef61c | 2011-08-04 16:08:59 +0530 | [diff] [blame] | 149 | int error_resolve_conflict(const char *me) |
Matthieu Moy | d38a30d | 2010-01-12 10:54:44 +0100 | [diff] [blame] | 150 | { |
Vasco Almeida | 8785c42 | 2016-06-17 20:20:52 +0000 | [diff] [blame] | 151 | if (!strcmp(me, "cherry-pick")) |
| 152 | error(_("Cherry-picking is not possible because you have unmerged files.")); |
| 153 | else if (!strcmp(me, "commit")) |
| 154 | error(_("Committing is not possible because you have unmerged files.")); |
| 155 | else if (!strcmp(me, "merge")) |
| 156 | error(_("Merging is not possible because you have unmerged files.")); |
| 157 | else if (!strcmp(me, "pull")) |
| 158 | error(_("Pulling is not possible because you have unmerged files.")); |
| 159 | else if (!strcmp(me, "revert")) |
| 160 | error(_("Reverting is not possible because you have unmerged files.")); |
| 161 | else |
| 162 | error(_("It is not possible to %s because you have unmerged files."), |
| 163 | me); |
| 164 | |
Junio C Hamano | 23cb5bf | 2011-12-22 11:21:26 -0800 | [diff] [blame] | 165 | if (advice_resolve_conflict) |
Matthieu Moy | d38a30d | 2010-01-12 10:54:44 +0100 | [diff] [blame] | 166 | /* |
| 167 | * Message used both when 'git commit' fails and when |
| 168 | * other commands doing a merge do. |
| 169 | */ |
Jeff King | c057b24 | 2014-06-03 03:17:17 -0400 | [diff] [blame] | 170 | advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n" |
Matthieu Moy | 91e70e0 | 2014-08-28 11:46:58 +0200 | [diff] [blame] | 171 | "as appropriate to mark resolution and make a commit.")); |
Ramkumar Ramachandra | 38ef61c | 2011-08-04 16:08:59 +0530 | [diff] [blame] | 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | void NORETURN die_resolve_conflict(const char *me) |
| 176 | { |
| 177 | error_resolve_conflict(me); |
Vasco Almeida | 8785c42 | 2016-06-17 20:20:52 +0000 | [diff] [blame] | 178 | die(_("Exiting because of an unresolved conflict.")); |
Matthieu Moy | d38a30d | 2010-01-12 10:54:44 +0100 | [diff] [blame] | 179 | } |
Nguyễn Thái Ngọc Duy | 2857093 | 2012-01-16 16:46:16 +0700 | [diff] [blame] | 180 | |
Paul Tan | 4a4cf9e | 2015-06-18 18:54:04 +0800 | [diff] [blame] | 181 | void NORETURN die_conclude_merge(void) |
| 182 | { |
| 183 | error(_("You have not concluded your merge (MERGE_HEAD exists).")); |
| 184 | if (advice_resolve_conflict) |
Alex Henrie | b744767 | 2015-10-01 22:25:33 -0600 | [diff] [blame] | 185 | advise(_("Please, commit your changes before merging.")); |
Paul Tan | 4a4cf9e | 2015-06-18 18:54:04 +0800 | [diff] [blame] | 186 | die(_("Exiting because of unfinished merge.")); |
| 187 | } |
| 188 | |
Nguyễn Thái Ngọc Duy | 2857093 | 2012-01-16 16:46:16 +0700 | [diff] [blame] | 189 | void detach_advice(const char *new_name) |
| 190 | { |
Vasco Almeida | e9f3cec | 2016-06-17 20:20:51 +0000 | [diff] [blame] | 191 | const char *fmt = |
| 192 | _("Note: checking out '%s'.\n\n" |
Nguyễn Thái Ngọc Duy | 2857093 | 2012-01-16 16:46:16 +0700 | [diff] [blame] | 193 | "You are in 'detached HEAD' state. You can look around, make experimental\n" |
| 194 | "changes and commit them, and you can discard any commits you make in this\n" |
| 195 | "state without impacting any branches by performing another checkout.\n\n" |
| 196 | "If you want to create a new branch to retain commits you create, you may\n" |
| 197 | "do so (now or later) by using -b with the checkout command again. Example:\n\n" |
Vasco Almeida | e9f3cec | 2016-06-17 20:20:51 +0000 | [diff] [blame] | 198 | " git checkout -b <new-branch-name>\n\n"); |
Nguyễn Thái Ngọc Duy | 2857093 | 2012-01-16 16:46:16 +0700 | [diff] [blame] | 199 | |
| 200 | fprintf(stderr, fmt, new_name); |
| 201 | } |