get_ref_dir(): require that the dirname argument ends in '/'
[gitweb.git] / builtin / push.c
index 5633f0ade49f7c845665b7aab202be18a8cc9d8d..19c40d7a55199984d7cdc1efbacfca7b628a4d20 100644 (file)
@@ -8,25 +8,29 @@
 #include "remote.h"
 #include "transport.h"
 #include "parse-options.h"
+#include "submodule.h"
 
 static const char * const push_usage[] = {
-       "git push [<options>] [<repository> <refspec>...]",
+       "git push [<options>] [<repository> [<refspec>...]]",
        NULL,
 };
 
 static int thin;
 static int deleterefs;
 static const char *receivepack;
+static int verbosity;
+static int progress = -1;
 
 static const char **refspec;
 static int refspec_nr;
+static int refspec_alloc;
+static int default_matching_used;
 
 static void add_refspec(const char *ref)
 {
-       int nr = refspec_nr + 1;
-       refspec = xrealloc(refspec, nr * sizeof(char *));
-       refspec[nr-1] = ref;
-       refspec_nr = nr;
+       refspec_nr++;
+       ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
+       refspec[refspec_nr-1] = ref;
 }
 
 static void set_refspecs(const char **refs, int nr)
@@ -38,7 +42,7 @@ static void set_refspecs(const char **refs, int nr)
                        char *tag;
                        int len;
                        if (nr <= ++i)
-                               die("tag shorthand without <tag>");
+                               die(_("tag shorthand without <tag>"));
                        len = strlen(refs[i]) + 11;
                        if (deleterefs) {
                                tag = xmalloc(len+1);
@@ -57,37 +61,66 @@ static void set_refspecs(const char **refs, int nr)
                        strcat(delref, ref);
                        ref = delref;
                } else if (deleterefs)
-                       die("--delete only accepts plain target ref names");
+                       die(_("--delete only accepts plain target ref names"));
                add_refspec(ref);
        }
 }
 
-static void setup_push_tracking(void)
+static int push_url_of_remote(struct remote *remote, const char ***url_p)
+{
+       if (remote->pushurl_nr) {
+               *url_p = remote->pushurl;
+               return remote->pushurl_nr;
+       }
+       *url_p = remote->url;
+       return remote->url_nr;
+}
+
+static void setup_push_upstream(struct remote *remote)
 {
        struct strbuf refspec = STRBUF_INIT;
        struct branch *branch = branch_get(NULL);
        if (!branch)
-               die("You are not currently on a branch.");
-       if (!branch->merge_nr)
-               die("The current branch %s is not tracking anything.",
+               die(_("You are not currently on a branch.\n"
+                   "To push the history leading to the current (detached HEAD)\n"
+                   "state now, use\n"
+                   "\n"
+                   "    git push %s HEAD:<name-of-remote-branch>\n"),
+                   remote->name);
+       if (!branch->merge_nr || !branch->merge || !branch->remote_name)
+               die(_("The current branch %s has no upstream branch.\n"
+                   "To push the current branch and set the remote as upstream, use\n"
+                   "\n"
+                   "    git push --set-upstream %s %s\n"),
+                   branch->name,
+                   remote->name,
                    branch->name);
        if (branch->merge_nr != 1)
-               die("The current branch %s is tracking multiple branches, "
-                   "refusing to push.", branch->name);
+               die(_("The current branch %s has multiple upstream branches, "
+                   "refusing to push."), branch->name);
+       if (strcmp(branch->remote_name, remote->name))
+               die(_("You are pushing to remote '%s', which is not the upstream of\n"
+                     "your current branch '%s', without telling me what to push\n"
+                     "to update which remote branch."),
+                   remote->name, branch->name);
+
        strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
        add_refspec(refspec.buf);
 }
 
-static void setup_default_push_refspecs(void)
+static void setup_default_push_refspecs(struct remote *remote)
 {
        switch (push_default) {
        default:
+       case PUSH_DEFAULT_UNSPECIFIED:
+               default_matching_used = 1;
+               /* fallthru */
        case PUSH_DEFAULT_MATCHING:
                add_refspec(":");
                break;
 
-       case PUSH_DEFAULT_TRACKING:
-               setup_push_tracking();
+       case PUSH_DEFAULT_UPSTREAM:
+               setup_push_upstream(remote);
                break;
 
        case PUSH_DEFAULT_CURRENT:
@@ -95,38 +128,87 @@ static void setup_default_push_refspecs(void)
                break;
 
        case PUSH_DEFAULT_NOTHING:
-               die("You didn't specify any refspecs to push, and "
-                   "push.default is \"nothing\".");
+               die(_("You didn't specify any refspecs to push, and "
+                   "push.default is \"nothing\"."));
                break;
        }
 }
 
+static const char message_advice_pull_before_push[] =
+       N_("Updates were rejected because the tip of your current branch is behind\n"
+          "its remote counterpart. Merge the remote changes (e.g. 'git pull')\n"
+          "before pushing again.\n"
+          "See the 'Note about fast-forwards' in 'git push --help' for details.");
+
+static const char message_advice_use_upstream[] =
+       N_("Updates were rejected because a pushed branch tip is behind its remote\n"
+          "counterpart. If you did not intend to push that branch, you may want to\n"
+          "specify branches to push or set the 'push.default' configuration\n"
+          "variable to 'current' or 'upstream' to push only the current branch.");
+
+static const char message_advice_checkout_pull_push[] =
+       N_("Updates were rejected because a pushed branch tip is behind its remote\n"
+          "counterpart. Check out this branch and merge the remote changes\n"
+          "(e.g. 'git pull') before pushing again.\n"
+          "See the 'Note about fast-forwards' in 'git push --help' for details.");
+
+static void advise_pull_before_push(void)
+{
+       if (!advice_push_non_ff_current || !advice_push_nonfastforward)
+               return;
+       advise(_(message_advice_pull_before_push));
+}
+
+static void advise_use_upstream(void)
+{
+       if (!advice_push_non_ff_default || !advice_push_nonfastforward)
+               return;
+       advise(_(message_advice_use_upstream));
+}
+
+static void advise_checkout_pull_push(void)
+{
+       if (!advice_push_non_ff_matching || !advice_push_nonfastforward)
+               return;
+       advise(_(message_advice_checkout_pull_push));
+}
+
 static int push_with_options(struct transport *transport, int flags)
 {
        int err;
        int nonfastforward;
+
+       transport_set_verbosity(transport, verbosity, progress);
+
        if (receivepack)
                transport_set_option(transport,
                                     TRANS_OPT_RECEIVEPACK, receivepack);
        if (thin)
                transport_set_option(transport, TRANS_OPT_THIN, "yes");
 
-       if (flags & TRANSPORT_PUSH_VERBOSE)
-               fprintf(stderr, "Pushing to %s\n", transport->url);
+       if (verbosity > 0)
+               fprintf(stderr, _("Pushing to %s\n"), transport->url);
        err = transport_push(transport, refspec_nr, refspec, flags,
                             &nonfastforward);
        if (err != 0)
-               error("failed to push some refs to '%s'", transport->url);
+               error(_("failed to push some refs to '%s'"), transport->url);
 
        err |= transport_disconnect(transport);
-
        if (!err)
                return 0;
 
-       if (nonfastforward && advice_push_nonfastforward) {
-               printf("To prevent you from losing history, non-fast-forward updates were rejected\n"
-                      "Merge the remote changes before pushing again.  See the 'Note about\n"
-                      "fast-forwards' section of 'git push --help' for details.\n");
+       switch (nonfastforward) {
+       default:
+               break;
+       case NON_FF_HEAD:
+               advise_pull_before_push();
+               break;
+       case NON_FF_OTHER:
+               if (default_matching_used)
+                       advise_use_upstream();
+               else
+                       advise_checkout_pull_push();
+               break;
        }
 
        return 1;
@@ -141,8 +223,15 @@ static int do_push(const char *repo, int flags)
 
        if (!remote) {
                if (repo)
-                       die("bad repository '%s'", repo);
-               die("No destination configured to push to.");
+                       die(_("bad repository '%s'"), repo);
+               die(_("No configured push destination.\n"
+                   "Either specify the URL from the command-line or configure a remote repository using\n"
+                   "\n"
+                   "    git remote add <name> <url>\n"
+                   "\n"
+                   "and then push using the remote name\n"
+                   "\n"
+                   "    git push <name>\n"));
        }
 
        if (remote->mirror)
@@ -150,19 +239,19 @@ static int do_push(const char *repo, int flags)
 
        if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
                if (!strcmp(*refspec, "refs/tags/*"))
-                       return error("--all and --tags are incompatible");
-               return error("--all can't be combined with refspecs");
+                       return error(_("--all and --tags are incompatible"));
+               return error(_("--all can't be combined with refspecs"));
        }
 
        if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
                if (!strcmp(*refspec, "refs/tags/*"))
-                       return error("--mirror and --tags are incompatible");
-               return error("--mirror can't be combined with refspecs");
+                       return error(_("--mirror and --tags are incompatible"));
+               return error(_("--mirror can't be combined with refspecs"));
        }
 
        if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
                                (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
-               return error("--all and --mirror are incompatible");
+               return error(_("--all and --mirror are incompatible"));
        }
 
        if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
@@ -170,16 +259,10 @@ static int do_push(const char *repo, int flags)
                        refspec = remote->push_refspec;
                        refspec_nr = remote->push_refspec_nr;
                } else if (!(flags & TRANSPORT_PUSH_MIRROR))
-                       setup_default_push_refspecs();
+                       setup_default_push_refspecs(remote);
        }
        errs = 0;
-       if (remote->pushurl_nr) {
-               url = remote->pushurl;
-               url_nr = remote->pushurl_nr;
-       } else {
-               url = remote->url;
-               url_nr = remote->url_nr;
-       }
+       url_nr = push_url_of_remote(remote, &url);
        if (url_nr) {
                for (i = 0; i < url_nr; i++) {
                        struct transport *transport =
@@ -197,6 +280,29 @@ static int do_push(const char *repo, int flags)
        return !!errs;
 }
 
+static int option_parse_recurse_submodules(const struct option *opt,
+                                  const char *arg, int unset)
+{
+       int *flags = opt->value;
+
+       if (*flags & (TRANSPORT_RECURSE_SUBMODULES_CHECK |
+                     TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND))
+               die("%s can only be used once.", opt->long_name);
+
+       if (arg) {
+               if (!strcmp(arg, "check"))
+                       *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
+               else if (!strcmp(arg, "on-demand"))
+                       *flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
+               else
+                       die("bad %s argument: %s", opt->long_name, arg);
+       } else
+               die("option %s needs an argument (check|on-demand)",
+                               opt->long_name);
+
+       return 0;
+}
+
 int cmd_push(int argc, const char **argv, const char *prefix)
 {
        int flags = 0;
@@ -204,8 +310,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)
        int rc;
        const char *repo = NULL;        /* default repository */
        struct option options[] = {
-               OPT_BIT('q', "quiet", &flags, "be quiet", TRANSPORT_PUSH_QUIET),
-               OPT_BIT('v', "verbose", &flags, "be verbose", TRANSPORT_PUSH_VERBOSE),
+               OPT__VERBOSITY(&verbosity),
                OPT_STRING( 0 , "repo", &repo, "repository", "repository"),
                OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
                OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
@@ -215,21 +320,28 @@ int cmd_push(int argc, const char **argv, const char *prefix)
                OPT_BIT('n' , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
                OPT_BIT( 0,  "porcelain", &flags, "machine-readable output", TRANSPORT_PUSH_PORCELAIN),
                OPT_BIT('f', "force", &flags, "force updates", TRANSPORT_PUSH_FORCE),
+               { OPTION_CALLBACK, 0, "recurse-submodules", &flags, "check",
+                       "controls recursive pushing of submodules",
+                       PARSE_OPT_OPTARG, option_parse_recurse_submodules },
                OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
                OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
                OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
                OPT_BIT('u', "set-upstream", &flags, "set upstream for git pull/status",
                        TRANSPORT_PUSH_SET_UPSTREAM),
+               OPT_BOOL(0, "progress", &progress, "force progress reporting"),
+               OPT_BIT(0, "prune", &flags, "prune locally removed refs",
+                       TRANSPORT_PUSH_PRUNE),
                OPT_END()
        };
 
+       packet_trace_identity("push");
        git_config(git_default_config, NULL);
        argc = parse_options(argc, argv, prefix, options, push_usage, 0);
 
        if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
-               die("--delete is incompatible with --all, --mirror and --tags");
+               die(_("--delete is incompatible with --all, --mirror and --tags"));
        if (deleterefs && argc < 2)
-               die("--delete doesn't make sense without any refs");
+               die(_("--delete doesn't make sense without any refs"));
 
        if (tags)
                add_refspec("refs/tags/*");