6 #include "run-command.h"
10 #include "parse-options.h"
11 #include "submodule.h"
13 static const char * const push_usage[] = {
14 "git push [<options>] [<repository> [<refspec>...]]",
19 static int deleterefs;
20 static const char *receivepack;
22 static int progress = -1;
24 static const char **refspec;
25 static int refspec_nr;
26 static int refspec_alloc;
28 static void add_refspec(const char *ref)
31 ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
32 refspec[refspec_nr-1] = ref;
35 static void set_refspecs(const char **refs, int nr)
38 for (i = 0; i < nr; i++) {
39 const char *ref = refs[i];
40 if (!strcmp("tag", ref)) {
44 die(_("tag shorthand without <tag>"));
45 len = strlen(refs[i]) + 11;
48 strcpy(tag, ":refs/tags/");
51 strcpy(tag, "refs/tags/");
55 } else if (deleterefs && !strchr(ref, ':')) {
57 int len = strlen(ref)+1;
58 delref = xmalloc(len+1);
62 } else if (deleterefs)
63 die(_("--delete only accepts plain target ref names"));
68 static int push_url_of_remote(struct remote *remote, const char ***url_p)
70 if (remote->pushurl_nr) {
71 *url_p = remote->pushurl;
72 return remote->pushurl_nr;
75 return remote->url_nr;
78 static void setup_push_upstream(struct remote *remote)
80 struct strbuf refspec = STRBUF_INIT;
81 struct branch *branch = branch_get(NULL);
83 die(_("You are not currently on a branch.\n"
84 "To push the history leading to the current (detached HEAD)\n"
87 " git push %s HEAD:<name-of-remote-branch>\n"),
89 if (!branch->merge_nr || !branch->merge || !branch->remote_name)
90 die(_("The current branch %s has no upstream branch.\n"
91 "To push the current branch and set the remote as upstream, use\n"
93 " git push --set-upstream %s %s\n"),
97 if (branch->merge_nr != 1)
98 die(_("The current branch %s has multiple upstream branches, "
99 "refusing to push."), branch->name);
100 if (strcmp(branch->remote_name, remote->name))
101 die(_("You are pushing to remote '%s', which is not the upstream of\n"
102 "your current branch '%s', without telling me what to push\n"
103 "to update which remote branch."),
104 remote->name, branch->name);
106 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
107 add_refspec(refspec.buf);
110 static void setup_default_push_refspecs(struct remote *remote)
112 switch (push_default) {
114 case PUSH_DEFAULT_MATCHING:
118 case PUSH_DEFAULT_UPSTREAM:
119 setup_push_upstream(remote);
122 case PUSH_DEFAULT_CURRENT:
126 case PUSH_DEFAULT_NOTHING:
127 die(_("You didn't specify any refspecs to push, and "
128 "push.default is \"nothing\"."));
133 static int push_with_options(struct transport *transport, int flags)
138 transport_set_verbosity(transport, verbosity, progress);
141 transport_set_option(transport,
142 TRANS_OPT_RECEIVEPACK, receivepack);
144 transport_set_option(transport, TRANS_OPT_THIN, "yes");
147 fprintf(stderr, _("Pushing to %s\n"), transport->url);
148 err = transport_push(transport, refspec_nr, refspec, flags,
151 error(_("failed to push some refs to '%s'"), transport->url);
153 err |= transport_disconnect(transport);
158 if (nonfastforward && advice_push_nonfastforward) {
159 fprintf(stderr, _("To prevent you from losing history, non-fast-forward updates were rejected\n"
160 "Merge the remote changes (e.g. 'git pull') before pushing again. See the\n"
161 "'Note about fast-forwards' section of 'git push --help' for details.\n"));
167 static int do_push(const char *repo, int flags)
170 struct remote *remote = remote_get(repo);
176 die(_("bad repository '%s'"), repo);
177 die(_("No configured push destination.\n"
178 "Either specify the URL from the command-line or configure a remote repository using\n"
180 " git remote add <name> <url>\n"
182 "and then push using the remote name\n"
184 " git push <name>\n"));
188 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
190 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
191 if (!strcmp(*refspec, "refs/tags/*"))
192 return error(_("--all and --tags are incompatible"));
193 return error(_("--all can't be combined with refspecs"));
196 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
197 if (!strcmp(*refspec, "refs/tags/*"))
198 return error(_("--mirror and --tags are incompatible"));
199 return error(_("--mirror can't be combined with refspecs"));
202 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
203 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
204 return error(_("--all and --mirror are incompatible"));
207 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
208 if (remote->push_refspec_nr) {
209 refspec = remote->push_refspec;
210 refspec_nr = remote->push_refspec_nr;
211 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
212 setup_default_push_refspecs(remote);
215 url_nr = push_url_of_remote(remote, &url);
217 for (i = 0; i < url_nr; i++) {
218 struct transport *transport =
219 transport_get(remote, url[i]);
220 if (push_with_options(transport, flags))
224 struct transport *transport =
225 transport_get(remote, NULL);
227 if (push_with_options(transport, flags))
233 static int option_parse_recurse_submodules(const struct option *opt,
234 const char *arg, int unset)
236 int *flags = opt->value;
238 if (!strcmp(arg, "check"))
239 *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
241 die("bad %s argument: %s", opt->long_name, arg);
243 die("option %s needs an argument (check)", opt->long_name);
248 int cmd_push(int argc, const char **argv, const char *prefix)
253 const char *repo = NULL; /* default repository */
254 struct option options[] = {
255 OPT__VERBOSITY(&verbosity),
256 OPT_STRING( 0 , "repo", &repo, "repository", "repository"),
257 OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
258 OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
259 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
260 OPT_BOOLEAN( 0, "delete", &deleterefs, "delete refs"),
261 OPT_BOOLEAN( 0 , "tags", &tags, "push tags (can't be used with --all or --mirror)"),
262 OPT_BIT('n' , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
263 OPT_BIT( 0, "porcelain", &flags, "machine-readable output", TRANSPORT_PUSH_PORCELAIN),
264 OPT_BIT('f', "force", &flags, "force updates", TRANSPORT_PUSH_FORCE),
265 { OPTION_CALLBACK, 0, "recurse-submodules", &flags, "check",
266 "controls recursive pushing of submodules",
267 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
268 OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
269 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
270 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
271 OPT_BIT('u', "set-upstream", &flags, "set upstream for git pull/status",
272 TRANSPORT_PUSH_SET_UPSTREAM),
273 OPT_BOOL(0, "progress", &progress, "force progress reporting"),
274 OPT_BIT(0, "prune", &flags, "prune locally removed refs",
275 TRANSPORT_PUSH_PRUNE),
279 packet_trace_identity("push");
280 git_config(git_default_config, NULL);
281 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
283 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
284 die(_("--delete is incompatible with --all, --mirror and --tags"));
285 if (deleterefs && argc < 2)
286 die(_("--delete doesn't make sense without any refs"));
289 add_refspec("refs/tags/*");
293 set_refspecs(argv + 1, argc - 1);
296 rc = do_push(repo, flags);
298 usage_with_options(push_usage, options);