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