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
;
30 static void add_refspec(const char *ref
)
33 ALLOC_GROW(refspec
, refspec_nr
, refspec_alloc
);
34 refspec
[refspec_nr
-1] = ref
;
37 static void set_refspecs(const char **refs
, int nr
)
40 for (i
= 0; i
< nr
; i
++) {
41 const char *ref
= refs
[i
];
42 if (!strcmp("tag", ref
)) {
46 die(_("tag shorthand without <tag>"));
47 len
= strlen(refs
[i
]) + 11;
50 strcpy(tag
, ":refs/tags/");
53 strcpy(tag
, "refs/tags/");
57 } else if (deleterefs
&& !strchr(ref
, ':')) {
59 int len
= strlen(ref
)+1;
60 delref
= xmalloc(len
+1);
64 } else if (deleterefs
)
65 die(_("--delete only accepts plain target ref names"));
70 static int push_url_of_remote(struct remote
*remote
, const char ***url_p
)
72 if (remote
->pushurl_nr
) {
73 *url_p
= remote
->pushurl
;
74 return remote
->pushurl_nr
;
77 return remote
->url_nr
;
80 static NORETURN
int die_push_simple(struct branch
*branch
, struct remote
*remote
) {
82 * There's no point in using shorten_unambiguous_ref here,
83 * as the ambiguity would be on the remote side, not what
84 * we have locally. Plus, this is supposed to be the simple
85 * mode. If the user is doing something crazy like setting
86 * upstream to a non-branch, we should probably be showing
87 * them the big ugly fully qualified ref.
89 const char *advice_maybe
= "";
90 const char *short_upstream
=
91 skip_prefix(branch
->merge
[0]->src
, "refs/heads/");
94 short_upstream
= branch
->merge
[0]->src
;
96 * Don't show advice for people who explicitly set
99 if (push_default
== PUSH_DEFAULT_UNSPECIFIED
)
100 advice_maybe
= _("\n"
101 "To choose either option permanently, "
102 "see push.default in 'git help config'.");
103 die(_("The upstream branch of your current branch does not match\n"
104 "the name of your current branch. To push to the upstream branch\n"
105 "on the remote, use\n"
107 " git push %s HEAD:%s\n"
109 "To push to the branch of the same name on the remote, use\n"
113 remote
->name
, short_upstream
,
114 remote
->name
, branch
->name
, advice_maybe
);
117 static const char message_detached_head_die
[] =
118 N_("You are not currently on a branch.\n"
119 "To push the history leading to the current (detached HEAD)\n"
122 " git push %s HEAD:<name-of-remote-branch>\n");
124 static void setup_push_upstream(struct remote
*remote
, struct branch
*branch
,
127 struct strbuf refspec
= STRBUF_INIT
;
130 die(_(message_detached_head_die
), remote
->name
);
131 if (!branch
->merge_nr
|| !branch
->merge
|| !branch
->remote_name
)
132 die(_("The current branch %s has no upstream branch.\n"
133 "To push the current branch and set the remote as upstream, use\n"
135 " git push --set-upstream %s %s\n"),
139 if (branch
->merge_nr
!= 1)
140 die(_("The current branch %s has multiple upstream branches, "
141 "refusing to push."), branch
->name
);
143 die(_("You are pushing to remote '%s', which is not the upstream of\n"
144 "your current branch '%s', without telling me what to push\n"
145 "to update which remote branch."),
146 remote
->name
, branch
->name
);
148 if (push_default
== PUSH_DEFAULT_SIMPLE
) {
149 /* Additional safety */
150 if (strcmp(branch
->refname
, branch
->merge
[0]->src
))
151 die_push_simple(branch
, remote
);
154 strbuf_addf(&refspec
, "%s:%s", branch
->name
, branch
->merge
[0]->src
);
155 add_refspec(refspec
.buf
);
158 static void setup_push_current(struct remote
*remote
, struct branch
*branch
)
161 die(_(message_detached_head_die
), remote
->name
);
162 add_refspec(branch
->name
);
165 static char warn_unspecified_push_default_msg
[] =
166 N_("push.default is unset; its implicit value has changed in\n"
167 "Git 2.0 from 'matching' to 'simple'. To squelch this message\n"
168 "and maintain the traditional behavior, use:\n"
170 " git config --global push.default matching\n"
172 "To squelch this message and adopt the new behavior now, use:\n"
174 " git config --global push.default simple\n"
176 "When push.default is set to 'matching', git will push local branches\n"
177 "to the remote branches that already exist with the same name.\n"
179 "Since Git 2.0, Git defaults to the more conservative 'simple'\n"
180 "behavior, which only pushes the current branch to the corresponding\n"
181 "remote branch that 'git pull' uses to update the current branch.\n"
183 "See 'git help config' and search for 'push.default' for further information.\n"
184 "(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode\n"
185 "'current' instead of 'simple' if you sometimes use older versions of Git)");
187 static void warn_unspecified_push_default_configuration(void)
189 static int warn_once
;
193 warning("%s\n", _(warn_unspecified_push_default_msg
));
196 static int is_workflow_triangular(struct remote
*remote
)
198 struct remote
*fetch_remote
= remote_get(NULL
);
199 return (fetch_remote
&& fetch_remote
!= remote
);
202 static void setup_default_push_refspecs(struct remote
*remote
)
204 struct branch
*branch
= branch_get(NULL
);
205 int triangular
= is_workflow_triangular(remote
);
207 switch (push_default
) {
209 case PUSH_DEFAULT_MATCHING
:
213 case PUSH_DEFAULT_UNSPECIFIED
:
214 warn_unspecified_push_default_configuration();
217 case PUSH_DEFAULT_SIMPLE
:
219 setup_push_current(remote
, branch
);
221 setup_push_upstream(remote
, branch
, triangular
);
224 case PUSH_DEFAULT_UPSTREAM
:
225 setup_push_upstream(remote
, branch
, triangular
);
228 case PUSH_DEFAULT_CURRENT
:
229 setup_push_current(remote
, branch
);
232 case PUSH_DEFAULT_NOTHING
:
233 die(_("You didn't specify any refspecs to push, and "
234 "push.default is \"nothing\"."));
239 static const char message_advice_pull_before_push
[] =
240 N_("Updates were rejected because the tip of your current branch is behind\n"
241 "its remote counterpart. Integrate the remote changes (e.g.\n"
242 "'git pull ...') before pushing again.\n"
243 "See the 'Note about fast-forwards' in 'git push --help' for details.");
245 static const char message_advice_checkout_pull_push
[] =
246 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
247 "counterpart. Check out this branch and 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_fetch_first
[] =
252 N_("Updates were rejected because the remote contains work that you do\n"
253 "not have locally. This is usually caused by another repository pushing\n"
254 "to the same ref. You may want to first integrate the remote changes\n"
255 "(e.g., 'git pull ...') before pushing again.\n"
256 "See the 'Note about fast-forwards' in 'git push --help' for details.");
258 static const char message_advice_ref_already_exists
[] =
259 N_("Updates were rejected because the tag already exists in the remote.");
261 static const char message_advice_ref_needs_force
[] =
262 N_("You cannot update a remote ref that points at a non-commit object,\n"
263 "or update a remote ref to make it point at a non-commit object,\n"
264 "without using the '--force' option.\n");
266 static void advise_pull_before_push(void)
268 if (!advice_push_non_ff_current
|| !advice_push_update_rejected
)
270 advise(_(message_advice_pull_before_push
));
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 advise_checkout_pull_push();
335 } else if (reject_reasons
& REJECT_ALREADY_EXISTS
) {
336 advise_ref_already_exists();
337 } else if (reject_reasons
& REJECT_FETCH_FIRST
) {
338 advise_ref_fetch_first();
339 } else if (reject_reasons
& REJECT_NEEDS_FORCE
) {
340 advise_ref_needs_force();
346 static int do_push(const char *repo
, int flags
)
349 struct remote
*remote
= pushremote_get(repo
);
355 die(_("bad repository '%s'"), repo
);
356 die(_("No configured push destination.\n"
357 "Either specify the URL from the command-line or configure a remote repository using\n"
359 " git remote add <name> <url>\n"
361 "and then push using the remote name\n"
363 " git push <name>\n"));
367 flags
|= (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
);
369 if ((flags
& TRANSPORT_PUSH_ALL
) && refspec
) {
370 if (!strcmp(*refspec
, "refs/tags/*"))
371 return error(_("--all and --tags are incompatible"));
372 return error(_("--all can't be combined with refspecs"));
375 if ((flags
& TRANSPORT_PUSH_MIRROR
) && refspec
) {
376 if (!strcmp(*refspec
, "refs/tags/*"))
377 return error(_("--mirror and --tags are incompatible"));
378 return error(_("--mirror can't be combined with refspecs"));
381 if ((flags
& (TRANSPORT_PUSH_ALL
|TRANSPORT_PUSH_MIRROR
)) ==
382 (TRANSPORT_PUSH_ALL
|TRANSPORT_PUSH_MIRROR
)) {
383 return error(_("--all and --mirror are incompatible"));
386 if (!refspec
&& !(flags
& TRANSPORT_PUSH_ALL
)) {
387 if (remote
->push_refspec_nr
) {
388 refspec
= remote
->push_refspec
;
389 refspec_nr
= remote
->push_refspec_nr
;
390 } else if (!(flags
& TRANSPORT_PUSH_MIRROR
))
391 setup_default_push_refspecs(remote
);
394 url_nr
= push_url_of_remote(remote
, &url
);
396 for (i
= 0; i
< url_nr
; i
++) {
397 struct transport
*transport
=
398 transport_get(remote
, url
[i
]);
399 if (push_with_options(transport
, flags
))
403 struct transport
*transport
=
404 transport_get(remote
, NULL
);
406 if (push_with_options(transport
, flags
))
412 static int option_parse_recurse_submodules(const struct option
*opt
,
413 const char *arg
, int unset
)
415 int *flags
= opt
->value
;
417 if (*flags
& (TRANSPORT_RECURSE_SUBMODULES_CHECK
|
418 TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
))
419 die("%s can only be used once.", opt
->long_name
);
422 if (!strcmp(arg
, "check"))
423 *flags
|= TRANSPORT_RECURSE_SUBMODULES_CHECK
;
424 else if (!strcmp(arg
, "on-demand"))
425 *flags
|= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
;
427 die("bad %s argument: %s", opt
->long_name
, arg
);
429 die("option %s needs an argument (check|on-demand)",
435 int cmd_push(int argc
, const char **argv
, const char *prefix
)
440 const char *repo
= NULL
; /* default repository */
441 struct option options
[] = {
442 OPT__VERBOSITY(&verbosity
),
443 OPT_STRING( 0 , "repo", &repo
, N_("repository"), N_("repository")),
444 OPT_BIT( 0 , "all", &flags
, N_("push all refs"), TRANSPORT_PUSH_ALL
),
445 OPT_BIT( 0 , "mirror", &flags
, N_("mirror all refs"),
446 (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
)),
447 OPT_BOOL( 0, "delete", &deleterefs
, N_("delete refs")),
448 OPT_BOOL( 0 , "tags", &tags
, N_("push tags (can't be used with --all or --mirror)")),
449 OPT_BIT('n' , "dry-run", &flags
, N_("dry run"), TRANSPORT_PUSH_DRY_RUN
),
450 OPT_BIT( 0, "porcelain", &flags
, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN
),
451 OPT_BIT('f', "force", &flags
, N_("force updates"), TRANSPORT_PUSH_FORCE
),
453 0, CAS_OPT_NAME
, &cas
, N_("refname>:<expect"),
454 N_("require old value of ref to be at this value"),
455 PARSE_OPT_OPTARG
, parseopt_push_cas_option
},
456 { OPTION_CALLBACK
, 0, "recurse-submodules", &flags
, N_("check"),
457 N_("control recursive pushing of submodules"),
458 PARSE_OPT_OPTARG
, option_parse_recurse_submodules
},
459 OPT_BOOL( 0 , "thin", &thin
, N_("use thin pack")),
460 OPT_STRING( 0 , "receive-pack", &receivepack
, "receive-pack", N_("receive pack program")),
461 OPT_STRING( 0 , "exec", &receivepack
, "receive-pack", N_("receive pack program")),
462 OPT_BIT('u', "set-upstream", &flags
, N_("set upstream for git pull/status"),
463 TRANSPORT_PUSH_SET_UPSTREAM
),
464 OPT_BOOL(0, "progress", &progress
, N_("force progress reporting")),
465 OPT_BIT(0, "prune", &flags
, N_("prune locally removed refs"),
466 TRANSPORT_PUSH_PRUNE
),
467 OPT_BIT(0, "no-verify", &flags
, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK
),
468 OPT_BIT(0, "follow-tags", &flags
, N_("push missing but relevant tags"),
469 TRANSPORT_PUSH_FOLLOW_TAGS
),
473 packet_trace_identity("push");
474 git_config(git_default_config
, NULL
);
475 argc
= parse_options(argc
, argv
, prefix
, options
, push_usage
, 0);
477 if (deleterefs
&& (tags
|| (flags
& (TRANSPORT_PUSH_ALL
| TRANSPORT_PUSH_MIRROR
))))
478 die(_("--delete is incompatible with --all, --mirror and --tags"));
479 if (deleterefs
&& argc
< 2)
480 die(_("--delete doesn't make sense without any refs"));
483 add_refspec("refs/tags/*");
487 set_refspecs(argv
+ 1, argc
- 1);
490 rc
= do_push(repo
, flags
);
492 usage_with_options(push_usage
, options
);