advice.con commit repository: enable initialization of submodules (96dc883)
   1#include "cache.h"
   2#include "config.h"
   3
   4int advice_push_update_rejected = 1;
   5int advice_push_non_ff_current = 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_status_u_option = 1;
  12int advice_commit_before_merge = 1;
  13int advice_resolve_conflict = 1;
  14int advice_implicit_identity = 1;
  15int advice_detached_head = 1;
  16int advice_set_upstream_failure = 1;
  17int advice_object_name_warning = 1;
  18int advice_rm_hints = 1;
  19
  20static struct {
  21        const char *name;
  22        int *preference;
  23} advice_config[] = {
  24        { "pushupdaterejected", &advice_push_update_rejected },
  25        { "pushnonffcurrent", &advice_push_non_ff_current },
  26        { "pushnonffmatching", &advice_push_non_ff_matching },
  27        { "pushalreadyexists", &advice_push_already_exists },
  28        { "pushfetchfirst", &advice_push_fetch_first },
  29        { "pushneedsforce", &advice_push_needs_force },
  30        { "statushints", &advice_status_hints },
  31        { "statusuoption", &advice_status_u_option },
  32        { "commitbeforemerge", &advice_commit_before_merge },
  33        { "resolveconflict", &advice_resolve_conflict },
  34        { "implicitidentity", &advice_implicit_identity },
  35        { "detachedhead", &advice_detached_head },
  36        { "setupstreamfailure", &advice_set_upstream_failure },
  37        { "objectnamewarning", &advice_object_name_warning },
  38        { "rmhints", &advice_rm_hints },
  39
  40        /* make this an alias for backward compatibility */
  41        { "pushnonfastforward", &advice_push_update_rejected }
  42};
  43
  44void advise(const char *advice, ...)
  45{
  46        struct strbuf buf = STRBUF_INIT;
  47        va_list params;
  48        const char *cp, *np;
  49
  50        va_start(params, advice);
  51        strbuf_vaddf(&buf, advice, params);
  52        va_end(params);
  53
  54        for (cp = buf.buf; *cp; cp = np) {
  55                np = strchrnul(cp, '\n');
  56                fprintf(stderr, _("hint: %.*s\n"), (int)(np - cp), cp);
  57                if (*np)
  58                        np++;
  59        }
  60        strbuf_release(&buf);
  61}
  62
  63int git_default_advice_config(const char *var, const char *value)
  64{
  65        const char *k;
  66        int i;
  67
  68        if (!skip_prefix(var, "advice.", &k))
  69                return 0;
  70
  71        for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
  72                if (strcmp(k, advice_config[i].name))
  73                        continue;
  74                *advice_config[i].preference = git_config_bool(var, value);
  75                return 0;
  76        }
  77
  78        return 0;
  79}
  80
  81int error_resolve_conflict(const char *me)
  82{
  83        if (!strcmp(me, "cherry-pick"))
  84                error(_("Cherry-picking is not possible because you have unmerged files."));
  85        else if (!strcmp(me, "commit"))
  86                error(_("Committing is not possible because you have unmerged files."));
  87        else if (!strcmp(me, "merge"))
  88                error(_("Merging is not possible because you have unmerged files."));
  89        else if (!strcmp(me, "pull"))
  90                error(_("Pulling is not possible because you have unmerged files."));
  91        else if (!strcmp(me, "revert"))
  92                error(_("Reverting is not possible because you have unmerged files."));
  93        else
  94                error(_("It is not possible to %s because you have unmerged files."),
  95                        me);
  96
  97        if (advice_resolve_conflict)
  98                /*
  99                 * Message used both when 'git commit' fails and when
 100                 * other commands doing a merge do.
 101                 */
 102                advise(_("Fix them up in the work tree, and then use 'git add/rm <file>'\n"
 103                         "as appropriate to mark resolution and make a commit."));
 104        return -1;
 105}
 106
 107void NORETURN die_resolve_conflict(const char *me)
 108{
 109        error_resolve_conflict(me);
 110        die(_("Exiting because of an unresolved conflict."));
 111}
 112
 113void NORETURN die_conclude_merge(void)
 114{
 115        error(_("You have not concluded your merge (MERGE_HEAD exists)."));
 116        if (advice_resolve_conflict)
 117                advise(_("Please, commit your changes before merging."));
 118        die(_("Exiting because of unfinished merge."));
 119}
 120
 121void detach_advice(const char *new_name)
 122{
 123        const char *fmt =
 124        _("Note: checking out '%s'.\n\n"
 125        "You are in 'detached HEAD' state. You can look around, make experimental\n"
 126        "changes and commit them, and you can discard any commits you make in this\n"
 127        "state without impacting any branches by performing another checkout.\n\n"
 128        "If you want to create a new branch to retain commits you create, you may\n"
 129        "do so (now or later) by using -b with the checkout command again. Example:\n\n"
 130        "  git checkout -b <new-branch-name>\n\n");
 131
 132        fprintf(stderr, fmt, new_name);
 133}