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
)) {
47 die(_("tag shorthand without <tag>"));
48 len
= strlen(refs
[i
]) + 11;
51 strcpy(tag
, ":refs/tags/");
54 strcpy(tag
, "refs/tags/");
58 } else if (deleterefs
&& !strchr(ref
, ':')) {
60 int len
= strlen(ref
)+1;
61 delref
= xmalloc(len
+1);
65 } else if (deleterefs
)
66 die(_("--delete only accepts plain target ref names"));
71 static int push_url_of_remote(struct remote
*remote
, const char ***url_p
)
73 if (remote
->pushurl_nr
) {
74 *url_p
= remote
->pushurl
;
75 return remote
->pushurl_nr
;
78 return remote
->url_nr
;
81 static NORETURN
int die_push_simple(struct branch
*branch
, struct remote
*remote
) {
83 * There's no point in using shorten_unambiguous_ref here,
84 * as the ambiguity would be on the remote side, not what
85 * we have locally. Plus, this is supposed to be the simple
86 * mode. If the user is doing something crazy like setting
87 * upstream to a non-branch, we should probably be showing
88 * them the big ugly fully qualified ref.
90 const char *advice_maybe
= "";
91 const char *short_upstream
=
92 skip_prefix(branch
->merge
[0]->src
, "refs/heads/");
95 short_upstream
= branch
->merge
[0]->src
;
97 * Don't show advice for people who explicitly set
100 if (push_default
== PUSH_DEFAULT_UNSPECIFIED
)
101 advice_maybe
= _("\n"
102 "To choose either option permanently, "
103 "see push.default in 'git help config'.");
104 die(_("The upstream branch of your current branch does not match\n"
105 "the name of your current branch. To push to the upstream branch\n"
106 "on the remote, use\n"
108 " git push %s HEAD:%s\n"
110 "To push to the branch of the same name on the remote, use\n"
114 remote
->name
, short_upstream
,
115 remote
->name
, branch
->name
, advice_maybe
);
118 static const char message_detached_head_die
[] =
119 N_("You are not currently on a branch.\n"
120 "To push the history leading to the current (detached HEAD)\n"
123 " git push %s HEAD:<name-of-remote-branch>\n");
125 static void setup_push_upstream(struct remote
*remote
, struct branch
*branch
,
128 struct strbuf refspec
= STRBUF_INIT
;
131 die(_(message_detached_head_die
), remote
->name
);
132 if (!branch
->merge_nr
|| !branch
->merge
|| !branch
->remote_name
)
133 die(_("The current branch %s has no upstream branch.\n"
134 "To push the current branch and set the remote as upstream, use\n"
136 " git push --set-upstream %s %s\n"),
140 if (branch
->merge_nr
!= 1)
141 die(_("The current branch %s has multiple upstream branches, "
142 "refusing to push."), branch
->name
);
144 die(_("You are pushing to remote '%s', which is not the upstream of\n"
145 "your current branch '%s', without telling me what to push\n"
146 "to update which remote branch."),
147 remote
->name
, branch
->name
);
149 if (push_default
== PUSH_DEFAULT_SIMPLE
) {
150 /* Additional safety */
151 if (strcmp(branch
->refname
, branch
->merge
[0]->src
))
152 die_push_simple(branch
, remote
);
155 strbuf_addf(&refspec
, "%s:%s", branch
->name
, branch
->merge
[0]->src
);
156 add_refspec(refspec
.buf
);
159 static void setup_push_current(struct remote
*remote
, struct branch
*branch
)
162 die(_(message_detached_head_die
), remote
->name
);
163 add_refspec(branch
->name
);
166 static char warn_unspecified_push_default_msg
[] =
167 N_("push.default is unset; its implicit value is changing in\n"
168 "Git 2.0 from 'matching' to 'simple'. To squelch this message\n"
169 "and maintain the current behavior after the default changes, use:\n"
171 " git config --global push.default matching\n"
173 "To squelch this message and adopt the new behavior now, use:\n"
175 " git config --global push.default simple\n"
177 "See 'git help config' and search for 'push.default' for further information.\n"
178 "(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode\n"
179 "'current' instead of 'simple' if you sometimes use older versions of Git)");
181 static void warn_unspecified_push_default_configuration(void)
183 static int warn_once
;
187 warning("%s\n", _(warn_unspecified_push_default_msg
));
190 static int is_workflow_triangular(struct remote
*remote
)
192 struct remote
*fetch_remote
= remote_get(NULL
);
193 return (fetch_remote
&& fetch_remote
!= remote
);
196 static void setup_default_push_refspecs(struct remote
*remote
)
198 struct branch
*branch
= branch_get(NULL
);
199 int triangular
= is_workflow_triangular(remote
);
201 switch (push_default
) {
203 case PUSH_DEFAULT_UNSPECIFIED
:
204 default_matching_used
= 1;
205 warn_unspecified_push_default_configuration();
207 case PUSH_DEFAULT_MATCHING
:
211 case PUSH_DEFAULT_SIMPLE
:
213 setup_push_current(remote
, branch
);
215 setup_push_upstream(remote
, branch
, triangular
);
218 case PUSH_DEFAULT_UPSTREAM
:
219 setup_push_upstream(remote
, branch
, triangular
);
222 case PUSH_DEFAULT_CURRENT
:
223 setup_push_current(remote
, branch
);
226 case PUSH_DEFAULT_NOTHING
:
227 die(_("You didn't specify any refspecs to push, and "
228 "push.default is \"nothing\"."));
233 static const char message_advice_pull_before_push
[] =
234 N_("Updates were rejected because the tip of your current branch is behind\n"
235 "its remote counterpart. Integrate the remote changes (e.g.\n"
236 "'git pull ...') before pushing again.\n"
237 "See the 'Note about fast-forwards' in 'git push --help' for details.");
239 static const char message_advice_use_upstream
[] =
240 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
241 "counterpart. If you did not intend to push that branch, you may want to\n"
242 "specify branches to push or set the 'push.default' configuration variable\n"
243 "to 'simple', 'current' or 'upstream' to push only the current branch.");
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_use_upstream(void)
275 if (!advice_push_non_ff_default
|| !advice_push_update_rejected
)
277 advise(_(message_advice_use_upstream
));
280 static void advise_checkout_pull_push(void)
282 if (!advice_push_non_ff_matching
|| !advice_push_update_rejected
)
284 advise(_(message_advice_checkout_pull_push
));
287 static void advise_ref_already_exists(void)
289 if (!advice_push_already_exists
|| !advice_push_update_rejected
)
291 advise(_(message_advice_ref_already_exists
));
294 static void advise_ref_fetch_first(void)
296 if (!advice_push_fetch_first
|| !advice_push_update_rejected
)
298 advise(_(message_advice_ref_fetch_first
));
301 static void advise_ref_needs_force(void)
303 if (!advice_push_needs_force
|| !advice_push_update_rejected
)
305 advise(_(message_advice_ref_needs_force
));
308 static int push_with_options(struct transport
*transport
, int flags
)
311 unsigned int reject_reasons
;
313 transport_set_verbosity(transport
, verbosity
, progress
);
316 transport_set_option(transport
,
317 TRANS_OPT_RECEIVEPACK
, receivepack
);
319 transport_set_option(transport
, TRANS_OPT_THIN
, "yes");
321 if (!is_empty_cas(&cas
)) {
322 if (!transport
->smart_options
)
323 die("underlying transport does not support --%s option",
325 transport
->smart_options
->cas
= &cas
;
329 fprintf(stderr
, _("Pushing to %s\n"), transport
->url
);
330 err
= transport_push(transport
, refspec_nr
, refspec
, flags
,
333 error(_("failed to push some refs to '%s'"), transport
->url
);
335 err
|= transport_disconnect(transport
);
339 if (reject_reasons
& REJECT_NON_FF_HEAD
) {
340 advise_pull_before_push();
341 } else if (reject_reasons
& REJECT_NON_FF_OTHER
) {
342 if (default_matching_used
)
343 advise_use_upstream();
345 advise_checkout_pull_push();
346 } else if (reject_reasons
& REJECT_ALREADY_EXISTS
) {
347 advise_ref_already_exists();
348 } else if (reject_reasons
& REJECT_FETCH_FIRST
) {
349 advise_ref_fetch_first();
350 } else if (reject_reasons
& REJECT_NEEDS_FORCE
) {
351 advise_ref_needs_force();
357 static int do_push(const char *repo
, int flags
)
360 struct remote
*remote
= pushremote_get(repo
);
366 die(_("bad repository '%s'"), repo
);
367 die(_("No configured push destination.\n"
368 "Either specify the URL from the command-line or configure a remote repository using\n"
370 " git remote add <name> <url>\n"
372 "and then push using the remote name\n"
374 " git push <name>\n"));
378 flags
|= (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
);
380 if ((flags
& TRANSPORT_PUSH_ALL
) && refspec
) {
381 if (!strcmp(*refspec
, "refs/tags/*"))
382 return error(_("--all and --tags are incompatible"));
383 return error(_("--all can't be combined with refspecs"));
386 if ((flags
& TRANSPORT_PUSH_MIRROR
) && refspec
) {
387 if (!strcmp(*refspec
, "refs/tags/*"))
388 return error(_("--mirror and --tags are incompatible"));
389 return error(_("--mirror can't be combined with refspecs"));
392 if ((flags
& (TRANSPORT_PUSH_ALL
|TRANSPORT_PUSH_MIRROR
)) ==
393 (TRANSPORT_PUSH_ALL
|TRANSPORT_PUSH_MIRROR
)) {
394 return error(_("--all and --mirror are incompatible"));
397 if (!refspec
&& !(flags
& TRANSPORT_PUSH_ALL
)) {
398 if (remote
->push_refspec_nr
) {
399 refspec
= remote
->push_refspec
;
400 refspec_nr
= remote
->push_refspec_nr
;
401 } else if (!(flags
& TRANSPORT_PUSH_MIRROR
))
402 setup_default_push_refspecs(remote
);
405 url_nr
= push_url_of_remote(remote
, &url
);
407 for (i
= 0; i
< url_nr
; i
++) {
408 struct transport
*transport
=
409 transport_get(remote
, url
[i
]);
410 if (push_with_options(transport
, flags
))
414 struct transport
*transport
=
415 transport_get(remote
, NULL
);
417 if (push_with_options(transport
, flags
))
423 static int option_parse_recurse_submodules(const struct option
*opt
,
424 const char *arg
, int unset
)
426 int *flags
= opt
->value
;
428 if (*flags
& (TRANSPORT_RECURSE_SUBMODULES_CHECK
|
429 TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
))
430 die("%s can only be used once.", opt
->long_name
);
433 if (!strcmp(arg
, "check"))
434 *flags
|= TRANSPORT_RECURSE_SUBMODULES_CHECK
;
435 else if (!strcmp(arg
, "on-demand"))
436 *flags
|= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
;
438 die("bad %s argument: %s", opt
->long_name
, arg
);
440 die("option %s needs an argument (check|on-demand)",
446 int cmd_push(int argc
, const char **argv
, const char *prefix
)
451 const char *repo
= NULL
; /* default repository */
452 struct option options
[] = {
453 OPT__VERBOSITY(&verbosity
),
454 OPT_STRING( 0 , "repo", &repo
, N_("repository"), N_("repository")),
455 OPT_BIT( 0 , "all", &flags
, N_("push all refs"), TRANSPORT_PUSH_ALL
),
456 OPT_BIT( 0 , "mirror", &flags
, N_("mirror all refs"),
457 (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
)),
458 OPT_BOOL( 0, "delete", &deleterefs
, N_("delete refs")),
459 OPT_BOOL( 0 , "tags", &tags
, N_("push tags (can't be used with --all or --mirror)")),
460 OPT_BIT('n' , "dry-run", &flags
, N_("dry run"), TRANSPORT_PUSH_DRY_RUN
),
461 OPT_BIT( 0, "porcelain", &flags
, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN
),
462 OPT_BIT('f', "force", &flags
, N_("force updates"), TRANSPORT_PUSH_FORCE
),
464 0, CAS_OPT_NAME
, &cas
, N_("refname>:<expect"),
465 N_("require old value of ref to be at this value"),
466 PARSE_OPT_OPTARG
, parseopt_push_cas_option
},
467 { OPTION_CALLBACK
, 0, "recurse-submodules", &flags
, N_("check"),
468 N_("control recursive pushing of submodules"),
469 PARSE_OPT_OPTARG
, option_parse_recurse_submodules
},
470 OPT_BOOL( 0 , "thin", &thin
, N_("use thin pack")),
471 OPT_STRING( 0 , "receive-pack", &receivepack
, "receive-pack", N_("receive pack program")),
472 OPT_STRING( 0 , "exec", &receivepack
, "receive-pack", N_("receive pack program")),
473 OPT_BIT('u', "set-upstream", &flags
, N_("set upstream for git pull/status"),
474 TRANSPORT_PUSH_SET_UPSTREAM
),
475 OPT_BOOL(0, "progress", &progress
, N_("force progress reporting")),
476 OPT_BIT(0, "prune", &flags
, N_("prune locally removed refs"),
477 TRANSPORT_PUSH_PRUNE
),
478 OPT_BIT(0, "no-verify", &flags
, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK
),
479 OPT_BIT(0, "follow-tags", &flags
, N_("push missing but relevant tags"),
480 TRANSPORT_PUSH_FOLLOW_TAGS
),
484 packet_trace_identity("push");
485 git_config(git_default_config
, NULL
);
486 argc
= parse_options(argc
, argv
, prefix
, options
, push_usage
, 0);
488 if (deleterefs
&& (tags
|| (flags
& (TRANSPORT_PUSH_ALL
| TRANSPORT_PUSH_MIRROR
))))
489 die(_("--delete is incompatible with --all, --mirror and --tags"));
490 if (deleterefs
&& argc
< 2)
491 die(_("--delete doesn't make sense without any refs"));
494 add_refspec("refs/tags/*");
498 set_refspecs(argv
+ 1, argc
- 1);
501 rc
= do_push(repo
, flags
);
503 usage_with_options(push_usage
, options
);