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 const char *map_refspec(const char *ref
,
39 struct remote
*remote
, struct ref
*local_refs
)
41 struct ref
*matched
= NULL
;
43 /* Does "ref" uniquely name our ref? */
44 if (count_refspec_match(ref
, local_refs
, &matched
) != 1)
49 memset(&query
, 0, sizeof(struct refspec
));
50 query
.src
= matched
->name
;
51 if (!query_refspecs(remote
->push
, remote
->push_refspec_nr
, &query
) &&
53 struct strbuf buf
= STRBUF_INIT
;
54 strbuf_addf(&buf
, "%s%s:%s",
55 query
.force
? "+" : "",
56 query
.src
, query
.dst
);
57 return strbuf_detach(&buf
, NULL
);
61 if (push_default
== PUSH_DEFAULT_UPSTREAM
&&
62 !prefixcmp(matched
->name
, "refs/heads/")) {
63 struct branch
*branch
= branch_get(matched
->name
+ 11);
64 if (branch
->merge_nr
== 1 && branch
->merge
[0]->src
) {
65 struct strbuf buf
= STRBUF_INIT
;
66 strbuf_addf(&buf
, "%s:%s",
67 ref
, branch
->merge
[0]->src
);
68 return strbuf_detach(&buf
, NULL
);
75 static void set_refspecs(const char **refs
, int nr
, const char *repo
)
77 struct remote
*remote
= NULL
;
78 struct ref
*local_refs
= NULL
;
81 for (i
= 0; i
< nr
; i
++) {
82 const char *ref
= refs
[i
];
83 if (!strcmp("tag", ref
)) {
84 struct strbuf tagref
= STRBUF_INIT
;
86 die(_("tag shorthand without <tag>"));
89 strbuf_addf(&tagref
, ":refs/tags/%s", ref
);
91 strbuf_addf(&tagref
, "refs/tags/%s", ref
);
92 ref
= strbuf_detach(&tagref
, NULL
);
93 } else if (deleterefs
) {
94 struct strbuf delref
= STRBUF_INIT
;
96 die(_("--delete only accepts plain target ref names"));
97 strbuf_addf(&delref
, ":%s", ref
);
98 ref
= strbuf_detach(&delref
, NULL
);
99 } else if (!strchr(ref
, ':')) {
101 /* lazily grab remote and local_refs */
102 remote
= remote_get(repo
);
103 local_refs
= get_local_heads();
105 ref
= map_refspec(ref
, remote
, local_refs
);
111 static int push_url_of_remote(struct remote
*remote
, const char ***url_p
)
113 if (remote
->pushurl_nr
) {
114 *url_p
= remote
->pushurl
;
115 return remote
->pushurl_nr
;
117 *url_p
= remote
->url
;
118 return remote
->url_nr
;
121 static NORETURN
int die_push_simple(struct branch
*branch
, struct remote
*remote
) {
123 * There's no point in using shorten_unambiguous_ref here,
124 * as the ambiguity would be on the remote side, not what
125 * we have locally. Plus, this is supposed to be the simple
126 * mode. If the user is doing something crazy like setting
127 * upstream to a non-branch, we should probably be showing
128 * them the big ugly fully qualified ref.
130 const char *advice_maybe
= "";
131 const char *short_upstream
=
132 skip_prefix(branch
->merge
[0]->src
, "refs/heads/");
135 short_upstream
= branch
->merge
[0]->src
;
137 * Don't show advice for people who explicitly set
140 if (push_default
== PUSH_DEFAULT_UNSPECIFIED
)
141 advice_maybe
= _("\n"
142 "To choose either option permanently, "
143 "see push.default in 'git help config'.");
144 die(_("The upstream branch of your current branch does not match\n"
145 "the name of your current branch. To push to the upstream branch\n"
146 "on the remote, use\n"
148 " git push %s HEAD:%s\n"
150 "To push to the branch of the same name on the remote, use\n"
154 remote
->name
, short_upstream
,
155 remote
->name
, branch
->name
, advice_maybe
);
158 static const char message_detached_head_die
[] =
159 N_("You are not currently on a branch.\n"
160 "To push the history leading to the current (detached HEAD)\n"
163 " git push %s HEAD:<name-of-remote-branch>\n");
165 static void setup_push_upstream(struct remote
*remote
, struct branch
*branch
,
168 struct strbuf refspec
= STRBUF_INIT
;
171 die(_(message_detached_head_die
), remote
->name
);
172 if (!branch
->merge_nr
|| !branch
->merge
|| !branch
->remote_name
)
173 die(_("The current branch %s has no upstream branch.\n"
174 "To push the current branch and set the remote as upstream, use\n"
176 " git push --set-upstream %s %s\n"),
180 if (branch
->merge_nr
!= 1)
181 die(_("The current branch %s has multiple upstream branches, "
182 "refusing to push."), branch
->name
);
184 die(_("You are pushing to remote '%s', which is not the upstream of\n"
185 "your current branch '%s', without telling me what to push\n"
186 "to update which remote branch."),
187 remote
->name
, branch
->name
);
189 if (push_default
== PUSH_DEFAULT_SIMPLE
) {
190 /* Additional safety */
191 if (strcmp(branch
->refname
, branch
->merge
[0]->src
))
192 die_push_simple(branch
, remote
);
195 strbuf_addf(&refspec
, "%s:%s", branch
->name
, branch
->merge
[0]->src
);
196 add_refspec(refspec
.buf
);
199 static void setup_push_current(struct remote
*remote
, struct branch
*branch
)
202 die(_(message_detached_head_die
), remote
->name
);
203 add_refspec(branch
->name
);
206 static char warn_unspecified_push_default_msg
[] =
207 N_("push.default is unset; its implicit value is changing in\n"
208 "Git 2.0 from 'matching' to 'simple'. To squelch this message\n"
209 "and maintain the current behavior after the default changes, use:\n"
211 " git config --global push.default matching\n"
213 "To squelch this message and adopt the new behavior now, use:\n"
215 " git config --global push.default simple\n"
217 "When push.default is set to 'matching', git will push local branches\n"
218 "to the remote branches that already exist with the same name.\n"
220 "In Git 2.0, Git will default to the more conservative 'simple'\n"
221 "behavior, which only pushes the current branch to the corresponding\n"
222 "remote branch that 'git pull' uses to update the current branch.\n"
224 "See 'git help config' and search for 'push.default' for further information.\n"
225 "(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode\n"
226 "'current' instead of 'simple' if you sometimes use older versions of Git)");
228 static void warn_unspecified_push_default_configuration(void)
230 static int warn_once
;
234 warning("%s\n", _(warn_unspecified_push_default_msg
));
237 static int is_workflow_triangular(struct remote
*remote
)
239 struct remote
*fetch_remote
= remote_get(NULL
);
240 return (fetch_remote
&& fetch_remote
!= remote
);
243 static void setup_default_push_refspecs(struct remote
*remote
)
245 struct branch
*branch
= branch_get(NULL
);
246 int triangular
= is_workflow_triangular(remote
);
248 switch (push_default
) {
250 case PUSH_DEFAULT_UNSPECIFIED
:
251 default_matching_used
= 1;
252 warn_unspecified_push_default_configuration();
254 case PUSH_DEFAULT_MATCHING
:
258 case PUSH_DEFAULT_SIMPLE
:
260 setup_push_current(remote
, branch
);
262 setup_push_upstream(remote
, branch
, triangular
);
265 case PUSH_DEFAULT_UPSTREAM
:
266 setup_push_upstream(remote
, branch
, triangular
);
269 case PUSH_DEFAULT_CURRENT
:
270 setup_push_current(remote
, branch
);
273 case PUSH_DEFAULT_NOTHING
:
274 die(_("You didn't specify any refspecs to push, and "
275 "push.default is \"nothing\"."));
280 static const char message_advice_pull_before_push
[] =
281 N_("Updates were rejected because the tip of your current branch is behind\n"
282 "its remote counterpart. Integrate the remote changes (e.g.\n"
283 "'git pull ...') before pushing again.\n"
284 "See the 'Note about fast-forwards' in 'git push --help' for details.");
286 static const char message_advice_use_upstream
[] =
287 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
288 "counterpart. If you did not intend to push that branch, you may want to\n"
289 "specify branches to push or set the 'push.default' configuration variable\n"
290 "to 'simple', 'current' or 'upstream' to push only the current branch.");
292 static const char message_advice_checkout_pull_push
[] =
293 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
294 "counterpart. Check out this branch and integrate the remote changes\n"
295 "(e.g. 'git pull ...') before pushing again.\n"
296 "See the 'Note about fast-forwards' in 'git push --help' for details.");
298 static const char message_advice_ref_fetch_first
[] =
299 N_("Updates were rejected because the remote contains work that you do\n"
300 "not have locally. This is usually caused by another repository pushing\n"
301 "to the same ref. You may want to first integrate the remote changes\n"
302 "(e.g., 'git pull ...') before pushing again.\n"
303 "See the 'Note about fast-forwards' in 'git push --help' for details.");
305 static const char message_advice_ref_already_exists
[] =
306 N_("Updates were rejected because the tag already exists in the remote.");
308 static const char message_advice_ref_needs_force
[] =
309 N_("You cannot update a remote ref that points at a non-commit object,\n"
310 "or update a remote ref to make it point at a non-commit object,\n"
311 "without using the '--force' option.\n");
313 static void advise_pull_before_push(void)
315 if (!advice_push_non_ff_current
|| !advice_push_update_rejected
)
317 advise(_(message_advice_pull_before_push
));
320 static void advise_use_upstream(void)
322 if (!advice_push_non_ff_default
|| !advice_push_update_rejected
)
324 advise(_(message_advice_use_upstream
));
327 static void advise_checkout_pull_push(void)
329 if (!advice_push_non_ff_matching
|| !advice_push_update_rejected
)
331 advise(_(message_advice_checkout_pull_push
));
334 static void advise_ref_already_exists(void)
336 if (!advice_push_already_exists
|| !advice_push_update_rejected
)
338 advise(_(message_advice_ref_already_exists
));
341 static void advise_ref_fetch_first(void)
343 if (!advice_push_fetch_first
|| !advice_push_update_rejected
)
345 advise(_(message_advice_ref_fetch_first
));
348 static void advise_ref_needs_force(void)
350 if (!advice_push_needs_force
|| !advice_push_update_rejected
)
352 advise(_(message_advice_ref_needs_force
));
355 static int push_with_options(struct transport
*transport
, int flags
)
358 unsigned int reject_reasons
;
360 transport_set_verbosity(transport
, verbosity
, progress
);
363 transport_set_option(transport
,
364 TRANS_OPT_RECEIVEPACK
, receivepack
);
365 transport_set_option(transport
, TRANS_OPT_THIN
, thin
? "yes" : NULL
);
367 if (!is_empty_cas(&cas
)) {
368 if (!transport
->smart_options
)
369 die("underlying transport does not support --%s option",
371 transport
->smart_options
->cas
= &cas
;
375 fprintf(stderr
, _("Pushing to %s\n"), transport
->url
);
376 err
= transport_push(transport
, refspec_nr
, refspec
, flags
,
379 error(_("failed to push some refs to '%s'"), transport
->url
);
381 err
|= transport_disconnect(transport
);
385 if (reject_reasons
& REJECT_NON_FF_HEAD
) {
386 advise_pull_before_push();
387 } else if (reject_reasons
& REJECT_NON_FF_OTHER
) {
388 if (default_matching_used
)
389 advise_use_upstream();
391 advise_checkout_pull_push();
392 } else if (reject_reasons
& REJECT_ALREADY_EXISTS
) {
393 advise_ref_already_exists();
394 } else if (reject_reasons
& REJECT_FETCH_FIRST
) {
395 advise_ref_fetch_first();
396 } else if (reject_reasons
& REJECT_NEEDS_FORCE
) {
397 advise_ref_needs_force();
403 static int do_push(const char *repo
, int flags
)
406 struct remote
*remote
= pushremote_get(repo
);
412 die(_("bad repository '%s'"), repo
);
413 die(_("No configured push destination.\n"
414 "Either specify the URL from the command-line or configure a remote repository using\n"
416 " git remote add <name> <url>\n"
418 "and then push using the remote name\n"
420 " git push <name>\n"));
424 flags
|= (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
);
426 if ((flags
& TRANSPORT_PUSH_ALL
) && refspec
) {
427 if (!strcmp(*refspec
, "refs/tags/*"))
428 return error(_("--all and --tags are incompatible"));
429 return error(_("--all can't be combined with refspecs"));
432 if ((flags
& TRANSPORT_PUSH_MIRROR
) && refspec
) {
433 if (!strcmp(*refspec
, "refs/tags/*"))
434 return error(_("--mirror and --tags are incompatible"));
435 return error(_("--mirror can't be combined with refspecs"));
438 if ((flags
& (TRANSPORT_PUSH_ALL
|TRANSPORT_PUSH_MIRROR
)) ==
439 (TRANSPORT_PUSH_ALL
|TRANSPORT_PUSH_MIRROR
)) {
440 return error(_("--all and --mirror are incompatible"));
443 if (!refspec
&& !(flags
& TRANSPORT_PUSH_ALL
)) {
444 if (remote
->push_refspec_nr
) {
445 refspec
= remote
->push_refspec
;
446 refspec_nr
= remote
->push_refspec_nr
;
447 } else if (!(flags
& TRANSPORT_PUSH_MIRROR
))
448 setup_default_push_refspecs(remote
);
451 url_nr
= push_url_of_remote(remote
, &url
);
453 for (i
= 0; i
< url_nr
; i
++) {
454 struct transport
*transport
=
455 transport_get(remote
, url
[i
]);
456 if (push_with_options(transport
, flags
))
460 struct transport
*transport
=
461 transport_get(remote
, NULL
);
463 if (push_with_options(transport
, flags
))
469 static int option_parse_recurse_submodules(const struct option
*opt
,
470 const char *arg
, int unset
)
472 int *flags
= opt
->value
;
474 if (*flags
& (TRANSPORT_RECURSE_SUBMODULES_CHECK
|
475 TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
))
476 die("%s can only be used once.", opt
->long_name
);
479 if (!strcmp(arg
, "check"))
480 *flags
|= TRANSPORT_RECURSE_SUBMODULES_CHECK
;
481 else if (!strcmp(arg
, "on-demand"))
482 *flags
|= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
;
484 die("bad %s argument: %s", opt
->long_name
, arg
);
486 die("option %s needs an argument (check|on-demand)",
492 int cmd_push(int argc
, const char **argv
, const char *prefix
)
497 const char *repo
= NULL
; /* default repository */
498 struct option options
[] = {
499 OPT__VERBOSITY(&verbosity
),
500 OPT_STRING( 0 , "repo", &repo
, N_("repository"), N_("repository")),
501 OPT_BIT( 0 , "all", &flags
, N_("push all refs"), TRANSPORT_PUSH_ALL
),
502 OPT_BIT( 0 , "mirror", &flags
, N_("mirror all refs"),
503 (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
)),
504 OPT_BOOL( 0, "delete", &deleterefs
, N_("delete refs")),
505 OPT_BOOL( 0 , "tags", &tags
, N_("push tags (can't be used with --all or --mirror)")),
506 OPT_BIT('n' , "dry-run", &flags
, N_("dry run"), TRANSPORT_PUSH_DRY_RUN
),
507 OPT_BIT( 0, "porcelain", &flags
, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN
),
508 OPT_BIT('f', "force", &flags
, N_("force updates"), TRANSPORT_PUSH_FORCE
),
510 0, CAS_OPT_NAME
, &cas
, N_("refname>:<expect"),
511 N_("require old value of ref to be at this value"),
512 PARSE_OPT_OPTARG
, parseopt_push_cas_option
},
513 { OPTION_CALLBACK
, 0, "recurse-submodules", &flags
, N_("check"),
514 N_("control recursive pushing of submodules"),
515 PARSE_OPT_OPTARG
, option_parse_recurse_submodules
},
516 OPT_BOOL( 0 , "thin", &thin
, N_("use thin pack")),
517 OPT_STRING( 0 , "receive-pack", &receivepack
, "receive-pack", N_("receive pack program")),
518 OPT_STRING( 0 , "exec", &receivepack
, "receive-pack", N_("receive pack program")),
519 OPT_BIT('u', "set-upstream", &flags
, N_("set upstream for git pull/status"),
520 TRANSPORT_PUSH_SET_UPSTREAM
),
521 OPT_BOOL(0, "progress", &progress
, N_("force progress reporting")),
522 OPT_BIT(0, "prune", &flags
, N_("prune locally removed refs"),
523 TRANSPORT_PUSH_PRUNE
),
524 OPT_BIT(0, "no-verify", &flags
, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK
),
525 OPT_BIT(0, "follow-tags", &flags
, N_("push missing but relevant tags"),
526 TRANSPORT_PUSH_FOLLOW_TAGS
),
530 packet_trace_identity("push");
531 git_config(git_default_config
, NULL
);
532 argc
= parse_options(argc
, argv
, prefix
, options
, push_usage
, 0);
534 if (deleterefs
&& (tags
|| (flags
& (TRANSPORT_PUSH_ALL
| TRANSPORT_PUSH_MIRROR
))))
535 die(_("--delete is incompatible with --all, --mirror and --tags"));
536 if (deleterefs
&& argc
< 2)
537 die(_("--delete doesn't make sense without any refs"));
540 add_refspec("refs/tags/*");
544 set_refspecs(argv
+ 1, argc
- 1, repo
);
547 rc
= do_push(repo
, flags
);
549 usage_with_options(push_usage
, options
);