6 #include "run-command.h"
10 #include "parse-options.h"
11 #include "submodule.h"
13 static const char * const push_usage
[] = {
14 N_("git push [<options>] [<repository> [<refspec>...]]"),
19 static int deleterefs
;
20 static const char *receivepack
;
22 static int progress
= -1;
24 static struct push_cas_option cas
;
26 static const char **refspec
;
27 static int refspec_nr
;
28 static int refspec_alloc
;
29 static int default_matching_used
;
31 static void add_refspec(const char *ref
)
34 ALLOC_GROW(refspec
, refspec_nr
, refspec_alloc
);
35 refspec
[refspec_nr
-1] = ref
;
38 static void set_refspecs(const char **refs
, int nr
)
41 for (i
= 0; i
< nr
; i
++) {
42 const char *ref
= refs
[i
];
43 if (!strcmp("tag", ref
)) {
44 struct strbuf tagref
= STRBUF_INIT
;
46 die(_("tag shorthand without <tag>"));
49 strbuf_addf(&tagref
, ":refs/tags/%s", ref
);
51 strbuf_addf(&tagref
, "refs/tags/%s", ref
);
52 ref
= strbuf_detach(&tagref
, NULL
);
53 } else if (deleterefs
) {
54 struct strbuf delref
= STRBUF_INIT
;
56 die(_("--delete only accepts plain target ref names"));
57 strbuf_addf(&delref
, ":%s", ref
);
58 ref
= strbuf_detach(&delref
, NULL
);
64 static int push_url_of_remote(struct remote
*remote
, const char ***url_p
)
66 if (remote
->pushurl_nr
) {
67 *url_p
= remote
->pushurl
;
68 return remote
->pushurl_nr
;
71 return remote
->url_nr
;
74 static NORETURN
int die_push_simple(struct branch
*branch
, struct remote
*remote
) {
76 * There's no point in using shorten_unambiguous_ref here,
77 * as the ambiguity would be on the remote side, not what
78 * we have locally. Plus, this is supposed to be the simple
79 * mode. If the user is doing something crazy like setting
80 * upstream to a non-branch, we should probably be showing
81 * them the big ugly fully qualified ref.
83 const char *advice_maybe
= "";
84 const char *short_upstream
=
85 skip_prefix(branch
->merge
[0]->src
, "refs/heads/");
88 short_upstream
= branch
->merge
[0]->src
;
90 * Don't show advice for people who explicitly set
93 if (push_default
== PUSH_DEFAULT_UNSPECIFIED
)
95 "To choose either option permanently, "
96 "see push.default in 'git help config'.");
97 die(_("The upstream branch of your current branch does not match\n"
98 "the name of your current branch. To push to the upstream branch\n"
99 "on the remote, use\n"
101 " git push %s HEAD:%s\n"
103 "To push to the branch of the same name on the remote, use\n"
107 remote
->name
, short_upstream
,
108 remote
->name
, branch
->name
, advice_maybe
);
111 static const char message_detached_head_die
[] =
112 N_("You are not currently on a branch.\n"
113 "To push the history leading to the current (detached HEAD)\n"
116 " git push %s HEAD:<name-of-remote-branch>\n");
118 static void setup_push_upstream(struct remote
*remote
, struct branch
*branch
,
121 struct strbuf refspec
= STRBUF_INIT
;
124 die(_(message_detached_head_die
), remote
->name
);
125 if (!branch
->merge_nr
|| !branch
->merge
|| !branch
->remote_name
)
126 die(_("The current branch %s has no upstream branch.\n"
127 "To push the current branch and set the remote as upstream, use\n"
129 " git push --set-upstream %s %s\n"),
133 if (branch
->merge_nr
!= 1)
134 die(_("The current branch %s has multiple upstream branches, "
135 "refusing to push."), branch
->name
);
137 die(_("You are pushing to remote '%s', which is not the upstream of\n"
138 "your current branch '%s', without telling me what to push\n"
139 "to update which remote branch."),
140 remote
->name
, branch
->name
);
142 if (push_default
== PUSH_DEFAULT_SIMPLE
) {
143 /* Additional safety */
144 if (strcmp(branch
->refname
, branch
->merge
[0]->src
))
145 die_push_simple(branch
, remote
);
148 strbuf_addf(&refspec
, "%s:%s", branch
->name
, branch
->merge
[0]->src
);
149 add_refspec(refspec
.buf
);
152 static void setup_push_current(struct remote
*remote
, struct branch
*branch
)
155 die(_(message_detached_head_die
), remote
->name
);
156 add_refspec(branch
->name
);
159 static char warn_unspecified_push_default_msg
[] =
160 N_("push.default is unset; its implicit value is changing in\n"
161 "Git 2.0 from 'matching' to 'simple'. To squelch this message\n"
162 "and maintain the current behavior after the default changes, use:\n"
164 " git config --global push.default matching\n"
166 "To squelch this message and adopt the new behavior now, use:\n"
168 " git config --global push.default simple\n"
170 "See 'git help config' and search for 'push.default' for further information.\n"
171 "(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode\n"
172 "'current' instead of 'simple' if you sometimes use older versions of Git)");
174 static void warn_unspecified_push_default_configuration(void)
176 static int warn_once
;
180 warning("%s\n", _(warn_unspecified_push_default_msg
));
183 static int is_workflow_triangular(struct remote
*remote
)
185 struct remote
*fetch_remote
= remote_get(NULL
);
186 return (fetch_remote
&& fetch_remote
!= remote
);
189 static void setup_default_push_refspecs(struct remote
*remote
)
191 struct branch
*branch
= branch_get(NULL
);
192 int triangular
= is_workflow_triangular(remote
);
194 switch (push_default
) {
196 case PUSH_DEFAULT_UNSPECIFIED
:
197 default_matching_used
= 1;
198 warn_unspecified_push_default_configuration();
200 case PUSH_DEFAULT_MATCHING
:
204 case PUSH_DEFAULT_SIMPLE
:
206 setup_push_current(remote
, branch
);
208 setup_push_upstream(remote
, branch
, triangular
);
211 case PUSH_DEFAULT_UPSTREAM
:
212 setup_push_upstream(remote
, branch
, triangular
);
215 case PUSH_DEFAULT_CURRENT
:
216 setup_push_current(remote
, branch
);
219 case PUSH_DEFAULT_NOTHING
:
220 die(_("You didn't specify any refspecs to push, and "
221 "push.default is \"nothing\"."));
226 static const char message_advice_pull_before_push
[] =
227 N_("Updates were rejected because the tip of your current branch is behind\n"
228 "its remote counterpart. Integrate the remote changes (e.g.\n"
229 "'git pull ...') before pushing again.\n"
230 "See the 'Note about fast-forwards' in 'git push --help' for details.");
232 static const char message_advice_use_upstream
[] =
233 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
234 "counterpart. If you did not intend to push that branch, you may want to\n"
235 "specify branches to push or set the 'push.default' configuration variable\n"
236 "to 'simple', 'current' or 'upstream' to push only the current branch.");
238 static const char message_advice_checkout_pull_push
[] =
239 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
240 "counterpart. Check out this branch and integrate the remote changes\n"
241 "(e.g. 'git pull ...') before pushing again.\n"
242 "See the 'Note about fast-forwards' in 'git push --help' for details.");
244 static const char message_advice_ref_fetch_first
[] =
245 N_("Updates were rejected because the remote contains work that you do\n"
246 "not have locally. This is usually caused by another repository pushing\n"
247 "to the same ref. You may want to first integrate the remote changes\n"
248 "(e.g., 'git pull ...') before pushing again.\n"
249 "See the 'Note about fast-forwards' in 'git push --help' for details.");
251 static const char message_advice_ref_already_exists
[] =
252 N_("Updates were rejected because the tag already exists in the remote.");
254 static const char message_advice_ref_needs_force
[] =
255 N_("You cannot update a remote ref that points at a non-commit object,\n"
256 "or update a remote ref to make it point at a non-commit object,\n"
257 "without using the '--force' option.\n");
259 static void advise_pull_before_push(void)
261 if (!advice_push_non_ff_current
|| !advice_push_update_rejected
)
263 advise(_(message_advice_pull_before_push
));
266 static void advise_use_upstream(void)
268 if (!advice_push_non_ff_default
|| !advice_push_update_rejected
)
270 advise(_(message_advice_use_upstream
));
273 static void advise_checkout_pull_push(void)
275 if (!advice_push_non_ff_matching
|| !advice_push_update_rejected
)
277 advise(_(message_advice_checkout_pull_push
));
280 static void advise_ref_already_exists(void)
282 if (!advice_push_already_exists
|| !advice_push_update_rejected
)
284 advise(_(message_advice_ref_already_exists
));
287 static void advise_ref_fetch_first(void)
289 if (!advice_push_fetch_first
|| !advice_push_update_rejected
)
291 advise(_(message_advice_ref_fetch_first
));
294 static void advise_ref_needs_force(void)
296 if (!advice_push_needs_force
|| !advice_push_update_rejected
)
298 advise(_(message_advice_ref_needs_force
));
301 static int push_with_options(struct transport
*transport
, int flags
)
304 unsigned int reject_reasons
;
306 transport_set_verbosity(transport
, verbosity
, progress
);
309 transport_set_option(transport
,
310 TRANS_OPT_RECEIVEPACK
, receivepack
);
311 transport_set_option(transport
, TRANS_OPT_THIN
, thin
? "yes" : NULL
);
313 if (!is_empty_cas(&cas
)) {
314 if (!transport
->smart_options
)
315 die("underlying transport does not support --%s option",
317 transport
->smart_options
->cas
= &cas
;
321 fprintf(stderr
, _("Pushing to %s\n"), transport
->url
);
322 err
= transport_push(transport
, refspec_nr
, refspec
, flags
,
325 error(_("failed to push some refs to '%s'"), transport
->url
);
327 err
|= transport_disconnect(transport
);
331 if (reject_reasons
& REJECT_NON_FF_HEAD
) {
332 advise_pull_before_push();
333 } else if (reject_reasons
& REJECT_NON_FF_OTHER
) {
334 if (default_matching_used
)
335 advise_use_upstream();
337 advise_checkout_pull_push();
338 } else if (reject_reasons
& REJECT_ALREADY_EXISTS
) {
339 advise_ref_already_exists();
340 } else if (reject_reasons
& REJECT_FETCH_FIRST
) {
341 advise_ref_fetch_first();
342 } else if (reject_reasons
& REJECT_NEEDS_FORCE
) {
343 advise_ref_needs_force();
349 static int do_push(const char *repo
, int flags
)
352 struct remote
*remote
= pushremote_get(repo
);
358 die(_("bad repository '%s'"), repo
);
359 die(_("No configured push destination.\n"
360 "Either specify the URL from the command-line or configure a remote repository using\n"
362 " git remote add <name> <url>\n"
364 "and then push using the remote name\n"
366 " git push <name>\n"));
370 flags
|= (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
);
372 if ((flags
& TRANSPORT_PUSH_ALL
) && refspec
) {
373 if (!strcmp(*refspec
, "refs/tags/*"))
374 return error(_("--all and --tags are incompatible"));
375 return error(_("--all can't be combined with refspecs"));
378 if ((flags
& TRANSPORT_PUSH_MIRROR
) && refspec
) {
379 if (!strcmp(*refspec
, "refs/tags/*"))
380 return error(_("--mirror and --tags are incompatible"));
381 return error(_("--mirror can't be combined with refspecs"));
384 if ((flags
& (TRANSPORT_PUSH_ALL
|TRANSPORT_PUSH_MIRROR
)) ==
385 (TRANSPORT_PUSH_ALL
|TRANSPORT_PUSH_MIRROR
)) {
386 return error(_("--all and --mirror are incompatible"));
389 if (!refspec
&& !(flags
& TRANSPORT_PUSH_ALL
)) {
390 if (remote
->push_refspec_nr
) {
391 refspec
= remote
->push_refspec
;
392 refspec_nr
= remote
->push_refspec_nr
;
393 } else if (!(flags
& TRANSPORT_PUSH_MIRROR
))
394 setup_default_push_refspecs(remote
);
397 url_nr
= push_url_of_remote(remote
, &url
);
399 for (i
= 0; i
< url_nr
; i
++) {
400 struct transport
*transport
=
401 transport_get(remote
, url
[i
]);
402 if (push_with_options(transport
, flags
))
406 struct transport
*transport
=
407 transport_get(remote
, NULL
);
409 if (push_with_options(transport
, flags
))
415 static int option_parse_recurse_submodules(const struct option
*opt
,
416 const char *arg
, int unset
)
418 int *flags
= opt
->value
;
420 if (*flags
& (TRANSPORT_RECURSE_SUBMODULES_CHECK
|
421 TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
))
422 die("%s can only be used once.", opt
->long_name
);
425 if (!strcmp(arg
, "check"))
426 *flags
|= TRANSPORT_RECURSE_SUBMODULES_CHECK
;
427 else if (!strcmp(arg
, "on-demand"))
428 *flags
|= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
;
430 die("bad %s argument: %s", opt
->long_name
, arg
);
432 die("option %s needs an argument (check|on-demand)",
438 int cmd_push(int argc
, const char **argv
, const char *prefix
)
443 const char *repo
= NULL
; /* default repository */
444 struct option options
[] = {
445 OPT__VERBOSITY(&verbosity
),
446 OPT_STRING( 0 , "repo", &repo
, N_("repository"), N_("repository")),
447 OPT_BIT( 0 , "all", &flags
, N_("push all refs"), TRANSPORT_PUSH_ALL
),
448 OPT_BIT( 0 , "mirror", &flags
, N_("mirror all refs"),
449 (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
)),
450 OPT_BOOL( 0, "delete", &deleterefs
, N_("delete refs")),
451 OPT_BOOL( 0 , "tags", &tags
, N_("push tags (can't be used with --all or --mirror)")),
452 OPT_BIT('n' , "dry-run", &flags
, N_("dry run"), TRANSPORT_PUSH_DRY_RUN
),
453 OPT_BIT( 0, "porcelain", &flags
, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN
),
454 OPT_BIT('f', "force", &flags
, N_("force updates"), TRANSPORT_PUSH_FORCE
),
456 0, CAS_OPT_NAME
, &cas
, N_("refname>:<expect"),
457 N_("require old value of ref to be at this value"),
458 PARSE_OPT_OPTARG
, parseopt_push_cas_option
},
459 { OPTION_CALLBACK
, 0, "recurse-submodules", &flags
, N_("check"),
460 N_("control recursive pushing of submodules"),
461 PARSE_OPT_OPTARG
, option_parse_recurse_submodules
},
462 OPT_BOOL( 0 , "thin", &thin
, N_("use thin pack")),
463 OPT_STRING( 0 , "receive-pack", &receivepack
, "receive-pack", N_("receive pack program")),
464 OPT_STRING( 0 , "exec", &receivepack
, "receive-pack", N_("receive pack program")),
465 OPT_BIT('u', "set-upstream", &flags
, N_("set upstream for git pull/status"),
466 TRANSPORT_PUSH_SET_UPSTREAM
),
467 OPT_BOOL(0, "progress", &progress
, N_("force progress reporting")),
468 OPT_BIT(0, "prune", &flags
, N_("prune locally removed refs"),
469 TRANSPORT_PUSH_PRUNE
),
470 OPT_BIT(0, "no-verify", &flags
, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK
),
471 OPT_BIT(0, "follow-tags", &flags
, N_("push missing but relevant tags"),
472 TRANSPORT_PUSH_FOLLOW_TAGS
),
476 packet_trace_identity("push");
477 git_config(git_default_config
, NULL
);
478 argc
= parse_options(argc
, argv
, prefix
, options
, push_usage
, 0);
480 if (deleterefs
&& (tags
|| (flags
& (TRANSPORT_PUSH_ALL
| TRANSPORT_PUSH_MIRROR
))))
481 die(_("--delete is incompatible with --all, --mirror and --tags"));
482 if (deleterefs
&& argc
< 2)
483 die(_("--delete doesn't make sense without any refs"));
486 add_refspec("refs/tags/*");
490 set_refspecs(argv
+ 1, argc
- 1);
493 rc
= do_push(repo
, flags
);
495 usage_with_options(push_usage
, options
);