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