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
;
27 static int default_matching_used
;
29 static void add_refspec(const char *ref
)
32 ALLOC_GROW(refspec
, refspec_nr
, refspec_alloc
);
33 refspec
[refspec_nr
-1] = ref
;
36 static void set_refspecs(const char **refs
, int nr
)
39 for (i
= 0; i
< nr
; i
++) {
40 const char *ref
= refs
[i
];
41 if (!strcmp("tag", ref
)) {
45 die(_("tag shorthand without <tag>"));
46 len
= strlen(refs
[i
]) + 11;
49 strcpy(tag
, ":refs/tags/");
52 strcpy(tag
, "refs/tags/");
56 } else if (deleterefs
&& !strchr(ref
, ':')) {
58 int len
= strlen(ref
)+1;
59 delref
= xmalloc(len
+1);
63 } else if (deleterefs
)
64 die(_("--delete only accepts plain target ref names"));
69 static void setup_push_upstream(struct remote
*remote
)
71 struct strbuf refspec
= STRBUF_INIT
;
72 struct branch
*branch
= branch_get(NULL
);
74 die(_("You are not currently on a branch.\n"
75 "To push the history leading to the current (detached HEAD)\n"
78 " git push %s HEAD:<name-of-remote-branch>\n"),
80 if (!branch
->merge_nr
|| !branch
->merge
)
81 die(_("The current branch %s has no upstream branch.\n"
82 "To push the current branch and set the remote as upstream, use\n"
84 " git push --set-upstream %s %s\n"),
88 if (branch
->merge_nr
!= 1)
89 die(_("The current branch %s has multiple upstream branches, "
90 "refusing to push."), branch
->name
);
91 strbuf_addf(&refspec
, "%s:%s", branch
->name
, branch
->merge
[0]->src
);
92 add_refspec(refspec
.buf
);
95 static char warn_unspecified_push_default_msg
[] =
96 N_("push.default is unset; its implicit value is changing in\n"
97 "Git 2.0 from 'matching' to 'upstream'. To squelch this message\n"
98 "and maintain the current behavior after the default changes, use:\n"
100 " git config --global push.default matching\n"
102 "To squelch this message and adopt the new behavior now, use:\n"
104 " git config --global push.default upstream\n"
106 "See 'git help config' and search for 'push.default' for further information.");
108 static void warn_unspecified_push_default_configuration(void)
110 static int warn_once
;
114 warning("%s\n", _(warn_unspecified_push_default_msg
));
117 static void setup_default_push_refspecs(struct remote
*remote
)
119 switch (push_default
) {
121 case PUSH_DEFAULT_UNSPECIFIED
:
122 default_matching_used
= 1;
123 warn_unspecified_push_default_configuration();
125 case PUSH_DEFAULT_MATCHING
:
129 case PUSH_DEFAULT_UPSTREAM
:
130 setup_push_upstream(remote
);
133 case PUSH_DEFAULT_CURRENT
:
137 case PUSH_DEFAULT_NOTHING
:
138 die(_("You didn't specify any refspecs to push, and "
139 "push.default is \"nothing\"."));
144 static const char message_advice_pull_before_push
[] =
145 N_("Updates were rejected because the tip of your current branch is behind\n"
146 "its remote counterpart. Merge the remote changes (e.g. 'git pull')\n"
147 "before pushing again.\n"
148 "See the 'Note about fast-forwards' in 'git push --help' for details.");
150 static const char message_advice_use_upstream
[] =
151 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
152 "counterpart. If you did not intend to push that branch, you may want to\n"
153 "specify branches to push or set the 'push.default' configuration\n"
154 "variable to 'current' or 'upstream' to push only the current branch.");
156 static const char message_advice_checkout_pull_push
[] =
157 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
158 "counterpart. Check out this branch and merge the remote changes\n"
159 "(e.g. 'git pull') before pushing again.\n"
160 "See the 'Note about fast-forwards' in 'git push --help' for details.");
162 static void advise_pull_before_push(void)
164 if (!advice_push_non_ff_current
|| !advice_push_nonfastforward
)
166 advise(_(message_advice_pull_before_push
));
169 static void advise_use_upstream(void)
171 if (!advice_push_non_ff_default
|| !advice_push_nonfastforward
)
173 advise(_(message_advice_use_upstream
));
176 static void advise_checkout_pull_push(void)
178 if (!advice_push_non_ff_matching
|| !advice_push_nonfastforward
)
180 advise(_(message_advice_checkout_pull_push
));
183 static int push_with_options(struct transport
*transport
, int flags
)
188 transport_set_verbosity(transport
, verbosity
, progress
);
191 transport_set_option(transport
,
192 TRANS_OPT_RECEIVEPACK
, receivepack
);
194 transport_set_option(transport
, TRANS_OPT_THIN
, "yes");
197 fprintf(stderr
, _("Pushing to %s\n"), transport
->url
);
198 err
= transport_push(transport
, refspec_nr
, refspec
, flags
,
201 error(_("failed to push some refs to '%s'"), transport
->url
);
203 err
|= transport_disconnect(transport
);
207 switch (nonfastforward
) {
211 advise_pull_before_push();
214 if (default_matching_used
)
215 advise_use_upstream();
217 advise_checkout_pull_push();
224 static int do_push(const char *repo
, int flags
)
227 struct remote
*remote
= remote_get(repo
);
233 die(_("bad repository '%s'"), repo
);
234 die(_("No configured push destination.\n"
235 "Either specify the URL from the command-line or configure a remote repository using\n"
237 " git remote add <name> <url>\n"
239 "and then push using the remote name\n"
241 " git push <name>\n"));
245 flags
|= (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
);
247 if ((flags
& TRANSPORT_PUSH_ALL
) && refspec
) {
248 if (!strcmp(*refspec
, "refs/tags/*"))
249 return error(_("--all and --tags are incompatible"));
250 return error(_("--all can't be combined with refspecs"));
253 if ((flags
& TRANSPORT_PUSH_MIRROR
) && refspec
) {
254 if (!strcmp(*refspec
, "refs/tags/*"))
255 return error(_("--mirror and --tags are incompatible"));
256 return error(_("--mirror can't be combined with refspecs"));
259 if ((flags
& (TRANSPORT_PUSH_ALL
|TRANSPORT_PUSH_MIRROR
)) ==
260 (TRANSPORT_PUSH_ALL
|TRANSPORT_PUSH_MIRROR
)) {
261 return error(_("--all and --mirror are incompatible"));
264 if (!refspec
&& !(flags
& TRANSPORT_PUSH_ALL
)) {
265 if (remote
->push_refspec_nr
) {
266 refspec
= remote
->push_refspec
;
267 refspec_nr
= remote
->push_refspec_nr
;
268 } else if (!(flags
& TRANSPORT_PUSH_MIRROR
))
269 setup_default_push_refspecs(remote
);
272 if (remote
->pushurl_nr
) {
273 url
= remote
->pushurl
;
274 url_nr
= remote
->pushurl_nr
;
277 url_nr
= remote
->url_nr
;
280 for (i
= 0; i
< url_nr
; i
++) {
281 struct transport
*transport
=
282 transport_get(remote
, url
[i
]);
283 if (push_with_options(transport
, flags
))
287 struct transport
*transport
=
288 transport_get(remote
, NULL
);
290 if (push_with_options(transport
, flags
))
296 static int option_parse_recurse_submodules(const struct option
*opt
,
297 const char *arg
, int unset
)
299 int *flags
= opt
->value
;
301 if (!strcmp(arg
, "check"))
302 *flags
|= TRANSPORT_RECURSE_SUBMODULES_CHECK
;
304 die("bad %s argument: %s", opt
->long_name
, arg
);
306 die("option %s needs an argument (check)", opt
->long_name
);
311 int cmd_push(int argc
, const char **argv
, const char *prefix
)
316 const char *repo
= NULL
; /* default repository */
317 struct option options
[] = {
318 OPT__VERBOSITY(&verbosity
),
319 OPT_STRING( 0 , "repo", &repo
, "repository", "repository"),
320 OPT_BIT( 0 , "all", &flags
, "push all refs", TRANSPORT_PUSH_ALL
),
321 OPT_BIT( 0 , "mirror", &flags
, "mirror all refs",
322 (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
)),
323 OPT_BOOLEAN( 0, "delete", &deleterefs
, "delete refs"),
324 OPT_BOOLEAN( 0 , "tags", &tags
, "push tags (can't be used with --all or --mirror)"),
325 OPT_BIT('n' , "dry-run", &flags
, "dry run", TRANSPORT_PUSH_DRY_RUN
),
326 OPT_BIT( 0, "porcelain", &flags
, "machine-readable output", TRANSPORT_PUSH_PORCELAIN
),
327 OPT_BIT('f', "force", &flags
, "force updates", TRANSPORT_PUSH_FORCE
),
328 { OPTION_CALLBACK
, 0, "recurse-submodules", &flags
, "check",
329 "controls recursive pushing of submodules",
330 PARSE_OPT_OPTARG
, option_parse_recurse_submodules
},
331 OPT_BOOLEAN( 0 , "thin", &thin
, "use thin pack"),
332 OPT_STRING( 0 , "receive-pack", &receivepack
, "receive-pack", "receive pack program"),
333 OPT_STRING( 0 , "exec", &receivepack
, "receive-pack", "receive pack program"),
334 OPT_BIT('u', "set-upstream", &flags
, "set upstream for git pull/status",
335 TRANSPORT_PUSH_SET_UPSTREAM
),
336 OPT_BOOL(0, "progress", &progress
, "force progress reporting"),
337 OPT_BIT(0, "prune", &flags
, "prune locally removed refs",
338 TRANSPORT_PUSH_PRUNE
),
342 packet_trace_identity("push");
343 git_config(git_default_config
, NULL
);
344 argc
= parse_options(argc
, argv
, prefix
, options
, push_usage
, 0);
346 if (deleterefs
&& (tags
|| (flags
& (TRANSPORT_PUSH_ALL
| TRANSPORT_PUSH_MIRROR
))))
347 die(_("--delete is incompatible with --all, --mirror and --tags"));
348 if (deleterefs
&& argc
< 2)
349 die(_("--delete doesn't make sense without any refs"));
352 add_refspec("refs/tags/*");
356 set_refspecs(argv
+ 1, argc
- 1);
359 rc
= do_push(repo
, flags
);
361 usage_with_options(push_usage
, options
);