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