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