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