THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
6.4KB

  1. #include "cache.h"
  2. #include "config.h"
  3. #include "color.h"
  4. #include "help.h"
  5. int advice_fetch_show_forced_updates = 1;
  6. int advice_push_update_rejected = 1;
  7. int advice_push_non_ff_current = 1;
  8. int advice_push_non_ff_matching = 1;
  9. int advice_push_already_exists = 1;
  10. int advice_push_fetch_first = 1;
  11. int advice_push_needs_force = 1;
  12. int advice_push_unqualified_ref_name = 1;
  13. int advice_status_hints = 1;
  14. int advice_status_u_option = 1;
  15. int advice_status_ahead_behind_warning = 1;
  16. int advice_commit_before_merge = 1;
  17. int advice_reset_quiet_warning = 1;
  18. int advice_resolve_conflict = 1;
  19. int advice_sequencer_in_use = 1;
  20. int advice_implicit_identity = 1;
  21. int advice_detached_head = 1;
  22. int advice_set_upstream_failure = 1;
  23. int advice_object_name_warning = 1;
  24. int advice_amworkdir = 1;
  25. int advice_rm_hints = 1;
  26. int advice_add_embedded_repo = 1;
  27. int advice_ignored_hook = 1;
  28. int advice_waiting_for_editor = 1;
  29. int advice_graft_file_deprecated = 1;
  30. int advice_checkout_ambiguous_remote_branch_name = 1;
  31. int advice_nested_tag = 1;
  32. static int advice_use_color = -1;
  33. static char advice_colors[][COLOR_MAXLEN] = {
  34. GIT_COLOR_RESET,
  35. GIT_COLOR_YELLOW, /* HINT */
  36. };
  37. enum color_advice {
  38. ADVICE_COLOR_RESET = 0,
  39. ADVICE_COLOR_HINT = 1,
  40. };
  41. static int parse_advise_color_slot(const char *slot)
  42. {
  43. if (!strcasecmp(slot, "reset"))
  44. return ADVICE_COLOR_RESET;
  45. if (!strcasecmp(slot, "hint"))
  46. return ADVICE_COLOR_HINT;
  47. return -1;
  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. static struct {
  56. const char *name;
  57. int *preference;
  58. } advice_config[] = {
  59. { "fetchShowForcedUpdates", &advice_fetch_show_forced_updates },
  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. { "pushUnqualifiedRefName", &advice_push_unqualified_ref_name },
  67. { "statusHints", &advice_status_hints },
  68. { "statusUoption", &advice_status_u_option },
  69. { "statusAheadBehindWarning", &advice_status_ahead_behind_warning },
  70. { "commitBeforeMerge", &advice_commit_before_merge },
  71. { "resetQuiet", &advice_reset_quiet_warning },
  72. { "resolveConflict", &advice_resolve_conflict },
  73. { "sequencerInUse", &advice_sequencer_in_use },
  74. { "implicitIdentity", &advice_implicit_identity },
  75. { "detachedHead", &advice_detached_head },
  76. { "setupStreamFailure", &advice_set_upstream_failure },
  77. { "objectNameWarning", &advice_object_name_warning },
  78. { "amWorkDir", &advice_amworkdir },
  79. { "rmHints", &advice_rm_hints },
  80. { "addEmbeddedRepo", &advice_add_embedded_repo },
  81. { "ignoredHook", &advice_ignored_hook },
  82. { "waitingForEditor", &advice_waiting_for_editor },
  83. { "graftFileDeprecated", &advice_graft_file_deprecated },
  84. { "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name },
  85. { "nestedTag", &advice_nested_tag },
  86. /* make this an alias for backward compatibility */
  87. { "pushNonFastForward", &advice_push_update_rejected }
  88. };
  89. void advise(const char *advice, ...)
  90. {
  91. struct strbuf buf = STRBUF_INIT;
  92. va_list params;
  93. const char *cp, *np;
  94. va_start(params, advice);
  95. strbuf_vaddf(&buf, advice, params);
  96. va_end(params);
  97. for (cp = buf.buf; *cp; cp = np) {
  98. np = strchrnul(cp, '\n');
  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));
  103. if (*np)
  104. np++;
  105. }
  106. strbuf_release(&buf);
  107. }
  108. int git_default_advice_config(const char *var, const char *value)
  109. {
  110. const char *k, *slot_name;
  111. int i;
  112. if (!strcmp(var, "color.advice")) {
  113. advice_use_color = git_config_colorbool(var, value);
  114. return 0;
  115. }
  116. if (skip_prefix(var, "color.advice.", &slot_name)) {
  117. int slot = parse_advise_color_slot(slot_name);
  118. if (slot < 0)
  119. return 0;
  120. if (!value)
  121. return config_error_nonbool(var);
  122. return color_parse(value, advice_colors[slot]);
  123. }
  124. if (!skip_prefix(var, "advice.", &k))
  125. return 0;
  126. for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
  127. if (strcasecmp(k, advice_config[i].name))
  128. continue;
  129. *advice_config[i].preference = git_config_bool(var, value);
  130. return 0;
  131. }
  132. return 0;
  133. }
  134. void list_config_advices(struct string_list *list, const char *prefix)
  135. {
  136. int i;
  137. for (i = 0; i < ARRAY_SIZE(advice_config); i++)
  138. list_config_item(list, prefix, advice_config[i].name);
  139. }
  140. int error_resolve_conflict(const char *me)
  141. {
  142. if (!strcmp(me, "cherry-pick"))
  143. error(_("Cherry-picking is not possible because you have unmerged files."));
  144. else if (!strcmp(me, "commit"))
  145. error(_("Committing is not possible because you have unmerged files."));
  146. else if (!strcmp(me, "merge"))
  147. error(_("Merging is not possible because you have unmerged files."));
  148. else if (!strcmp(me, "pull"))
  149. error(_("Pulling is not possible because you have unmerged files."));
  150. else if (!strcmp(me, "revert"))
  151. error(_("Reverting is not possible because you have unmerged files."));
  152. else
  153. error(_("It is not possible to %s because you have unmerged files."),
  154. me);
  155. if (advice_resolve_conflict)
  156. /*
  157. * Message used both when 'git commit' fails and when
  158. * other commands doing a merge do.
  159. */
  160. advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n"
  161. "as appropriate to mark resolution and make a commit."));
  162. return -1;
  163. }
  164. void NORETURN die_resolve_conflict(const char *me)
  165. {
  166. error_resolve_conflict(me);
  167. die(_("Exiting because of an unresolved conflict."));
  168. }
  169. void NORETURN die_conclude_merge(void)
  170. {
  171. error(_("You have not concluded your merge (MERGE_HEAD exists)."));
  172. if (advice_resolve_conflict)
  173. advise(_("Please, commit your changes before merging."));
  174. die(_("Exiting because of unfinished merge."));
  175. }
  176. void detach_advice(const char *new_name)
  177. {
  178. const char *fmt =
  179. _("Note: switching to '%s'.\n"
  180. "\n"
  181. "You are in 'detached HEAD' state. You can look around, make experimental\n"
  182. "changes and commit them, and you can discard any commits you make in this\n"
  183. "state without impacting any branches by switching back to a branch.\n"
  184. "\n"
  185. "If you want to create a new branch to retain commits you create, you may\n"
  186. "do so (now or later) by using -c with the switch command. Example:\n"
  187. "\n"
  188. " git switch -c <new-branch-name>\n"
  189. "\n"
  190. "Or undo this operation with:\n"
  191. "\n"
  192. " git switch -\n"
  193. "\n"
  194. "Turn off this advice by setting config variable advice.detachedHead to false\n\n");
  195. fprintf(stderr, fmt, new_name);
  196. }