advice.con commit CodingGuidelines: add Python coding guidelines (9ef43dd)
   1#include "cache.h"
   2
   3int advice_push_update_rejected = 1;
   4int advice_push_non_ff_current = 1;
   5int advice_push_non_ff_default = 1;
   6int advice_push_non_ff_matching = 1;
   7int advice_push_already_exists = 1;
   8int advice_status_hints = 1;
   9int advice_commit_before_merge = 1;
  10int advice_resolve_conflict = 1;
  11int advice_implicit_identity = 1;
  12int advice_detached_head = 1;
  13
  14static struct {
  15        const char *name;
  16        int *preference;
  17} advice_config[] = {
  18        { "pushupdaterejected", &advice_push_update_rejected },
  19        { "pushnonffcurrent", &advice_push_non_ff_current },
  20        { "pushnonffdefault", &advice_push_non_ff_default },
  21        { "pushnonffmatching", &advice_push_non_ff_matching },
  22        { "pushalreadyexists", &advice_push_already_exists },
  23        { "statushints", &advice_status_hints },
  24        { "commitbeforemerge", &advice_commit_before_merge },
  25        { "resolveconflict", &advice_resolve_conflict },
  26        { "implicitidentity", &advice_implicit_identity },
  27        { "detachedhead", &advice_detached_head },
  28
  29        /* make this an alias for backward compatibility */
  30        { "pushnonfastforward", &advice_push_update_rejected }
  31};
  32
  33void advise(const char *advice, ...)
  34{
  35        struct strbuf buf = STRBUF_INIT;
  36        va_list params;
  37        const char *cp, *np;
  38
  39        va_start(params, advice);
  40        strbuf_vaddf(&buf, advice, params);
  41        va_end(params);
  42
  43        for (cp = buf.buf; *cp; cp = np) {
  44                np = strchrnul(cp, '\n');
  45                fprintf(stderr, _("hint: %.*s\n"), (int)(np - cp), cp);
  46                if (*np)
  47                        np++;
  48        }
  49        strbuf_release(&buf);
  50}
  51
  52int git_default_advice_config(const char *var, const char *value)
  53{
  54        const char *k = skip_prefix(var, "advice.");
  55        int i;
  56
  57        for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
  58                if (strcmp(k, advice_config[i].name))
  59                        continue;
  60                *advice_config[i].preference = git_config_bool(var, value);
  61                return 0;
  62        }
  63
  64        return 0;
  65}
  66
  67int error_resolve_conflict(const char *me)
  68{
  69        error("'%s' is not possible because you have unmerged files.", me);
  70        if (advice_resolve_conflict)
  71                /*
  72                 * Message used both when 'git commit' fails and when
  73                 * other commands doing a merge do.
  74                 */
  75                advise(_("Fix them up in the work tree,\n"
  76                         "and then use 'git add/rm <file>' as\n"
  77                         "appropriate to mark resolution and make a commit,\n"
  78                         "or use 'git commit -a'."));
  79        return -1;
  80}
  81
  82void NORETURN die_resolve_conflict(const char *me)
  83{
  84        error_resolve_conflict(me);
  85        die("Exiting because of an unresolved conflict.");
  86}
  87
  88void detach_advice(const char *new_name)
  89{
  90        const char fmt[] =
  91        "Note: checking out '%s'.\n\n"
  92        "You are in 'detached HEAD' state. You can look around, make experimental\n"
  93        "changes and commit them, and you can discard any commits you make in this\n"
  94        "state without impacting any branches by performing another checkout.\n\n"
  95        "If you want to create a new branch to retain commits you create, you may\n"
  96        "do so (now or later) by using -b with the checkout command again. Example:\n\n"
  97        "  git checkout -b new_branch_name\n\n";
  98
  99        fprintf(stderr, fmt, new_name);
 100}