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 const char **refspec
;
25 static int refspec_nr
;
26 static int refspec_alloc
;
28 static void add_refspec(const char *ref
)
31 ALLOC_GROW(refspec
, refspec_nr
, refspec_alloc
);
32 refspec
[refspec_nr
-1] = ref
;
35 static void set_refspecs(const char **refs
, int nr
)
38 for (i
= 0; i
< nr
; i
++) {
39 const char *ref
= refs
[i
];
40 if (!strcmp("tag", ref
)) {
44 die(_("tag shorthand without <tag>"));
45 len
= strlen(refs
[i
]) + 11;
48 strcpy(tag
, ":refs/tags/");
51 strcpy(tag
, "refs/tags/");
55 } else if (deleterefs
&& !strchr(ref
, ':')) {
57 int len
= strlen(ref
)+1;
58 delref
= xmalloc(len
+1);
62 } else if (deleterefs
)
63 die(_("--delete only accepts plain target ref names"));
68 static int push_url_of_remote(struct remote
*remote
, const char ***url_p
)
70 if (remote
->pushurl_nr
) {
71 *url_p
= remote
->pushurl
;
72 return remote
->pushurl_nr
;
75 return remote
->url_nr
;
78 static NORETURN
int die_push_simple(struct branch
*branch
, struct remote
*remote
) {
80 * There's no point in using shorten_unambiguous_ref here,
81 * as the ambiguity would be on the remote side, not what
82 * we have locally. Plus, this is supposed to be the simple
83 * mode. If the user is doing something crazy like setting
84 * upstream to a non-branch, we should probably be showing
85 * them the big ugly fully qualified ref.
87 const char *advice_maybe
= "";
88 const char *short_upstream
=
89 skip_prefix(branch
->merge
[0]->src
, "refs/heads/");
92 short_upstream
= branch
->merge
[0]->src
;
94 * Don't show advice for people who explicitely set
97 if (push_default
== PUSH_DEFAULT_UNSPECIFIED
)
99 "To choose either option permanently, "
100 "see push.default in 'git help config'.");
101 die(_("The upstream branch of your current branch does not match\n"
102 "the name of your current branch. To push to the upstream branch\n"
103 "on the remote, use\n"
105 " git push %s HEAD:%s\n"
107 "To push to the branch of the same name on the remote, use\n"
111 remote
->name
, short_upstream
,
112 remote
->name
, branch
->name
, advice_maybe
);
115 static void setup_push_upstream(struct remote
*remote
, int simple
)
117 struct strbuf refspec
= STRBUF_INIT
;
118 struct branch
*branch
= branch_get(NULL
);
120 die(_("You are not currently on a branch.\n"
121 "To push the history leading to the current (detached HEAD)\n"
124 " git push %s HEAD:<name-of-remote-branch>\n"),
126 if (!branch
->merge_nr
|| !branch
->merge
|| !branch
->remote_name
)
127 die(_("The current branch %s has no upstream branch.\n"
128 "To push the current branch and set the remote as upstream, use\n"
130 " git push --set-upstream %s %s\n"),
134 if (branch
->merge_nr
!= 1)
135 die(_("The current branch %s has multiple upstream branches, "
136 "refusing to push."), branch
->name
);
137 if (strcmp(branch
->remote_name
, remote
->name
))
138 die(_("You are pushing to remote '%s', which is not the upstream of\n"
139 "your current branch '%s', without telling me what to push\n"
140 "to update which remote branch."),
141 remote
->name
, branch
->name
);
142 if (simple
&& strcmp(branch
->refname
, branch
->merge
[0]->src
))
143 die_push_simple(branch
, remote
);
145 strbuf_addf(&refspec
, "%s:%s", branch
->name
, branch
->merge
[0]->src
);
146 add_refspec(refspec
.buf
);
149 static char warn_unspecified_push_default_msg
[] =
150 N_("push.default is unset; its implicit value has changed in\n"
151 "Git 2.0 from 'matching' to 'simple'. To squelch this message\n"
152 "and maintain the traditional behavior, use:\n"
154 " git config --global push.default matching\n"
156 "To squelch this message and adopt the new behavior now, use:\n"
158 " git config --global push.default simple\n"
160 "See 'git help config' and search for 'push.default' for further information.\n"
161 "(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode\n"
162 "'current' instead of 'simple' if you sometimes use older versions of Git)");
164 static void warn_unspecified_push_default_configuration(void)
166 static int warn_once
;
170 warning("%s\n", _(warn_unspecified_push_default_msg
));
173 static void setup_default_push_refspecs(struct remote
*remote
)
175 switch (push_default
) {
177 case PUSH_DEFAULT_MATCHING
:
181 case PUSH_DEFAULT_UNSPECIFIED
:
182 warn_unspecified_push_default_configuration();
185 case PUSH_DEFAULT_SIMPLE
:
186 setup_push_upstream(remote
, 1);
189 case PUSH_DEFAULT_UPSTREAM
:
190 setup_push_upstream(remote
, 0);
193 case PUSH_DEFAULT_CURRENT
:
197 case PUSH_DEFAULT_NOTHING
:
198 die(_("You didn't specify any refspecs to push, and "
199 "push.default is \"nothing\"."));
204 static const char message_advice_pull_before_push
[] =
205 N_("Updates were rejected because the tip of your current branch is behind\n"
206 "its remote counterpart. Merge the remote changes (e.g. 'git pull')\n"
207 "before pushing again.\n"
208 "See the 'Note about fast-forwards' in 'git push --help' for details.");
210 static const char message_advice_checkout_pull_push
[] =
211 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
212 "counterpart. Check out this branch and merge the remote changes\n"
213 "(e.g. 'git pull') before pushing again.\n"
214 "See the 'Note about fast-forwards' in 'git push --help' for details.");
216 static const char message_advice_ref_already_exists
[] =
217 N_("Updates were rejected because the destination reference already exists\n"
220 static void advise_pull_before_push(void)
222 if (!advice_push_non_ff_current
|| !advice_push_update_rejected
)
224 advise(_(message_advice_pull_before_push
));
227 static void advise_checkout_pull_push(void)
229 if (!advice_push_non_ff_matching
|| !advice_push_update_rejected
)
231 advise(_(message_advice_checkout_pull_push
));
234 static void advise_ref_already_exists(void)
236 if (!advice_push_already_exists
|| !advice_push_update_rejected
)
238 advise(_(message_advice_ref_already_exists
));
241 static int push_with_options(struct transport
*transport
, int flags
)
244 unsigned int reject_reasons
;
246 transport_set_verbosity(transport
, verbosity
, progress
);
249 transport_set_option(transport
,
250 TRANS_OPT_RECEIVEPACK
, receivepack
);
252 transport_set_option(transport
, TRANS_OPT_THIN
, "yes");
255 fprintf(stderr
, _("Pushing to %s\n"), transport
->url
);
256 err
= transport_push(transport
, refspec_nr
, refspec
, flags
,
259 error(_("failed to push some refs to '%s'"), transport
->url
);
261 err
|= transport_disconnect(transport
);
265 if (reject_reasons
& REJECT_NON_FF_HEAD
) {
266 advise_pull_before_push();
267 } else if (reject_reasons
& REJECT_NON_FF_OTHER
) {
268 advise_checkout_pull_push();
269 } else if (reject_reasons
& REJECT_ALREADY_EXISTS
) {
270 advise_ref_already_exists();
276 static int do_push(const char *repo
, int flags
)
279 struct remote
*remote
= remote_get(repo
);
285 die(_("bad repository '%s'"), repo
);
286 die(_("No configured push destination.\n"
287 "Either specify the URL from the command-line or configure a remote repository using\n"
289 " git remote add <name> <url>\n"
291 "and then push using the remote name\n"
293 " git push <name>\n"));
297 flags
|= (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
);
299 if ((flags
& TRANSPORT_PUSH_ALL
) && refspec
) {
300 if (!strcmp(*refspec
, "refs/tags/*"))
301 return error(_("--all and --tags are incompatible"));
302 return error(_("--all can't be combined with refspecs"));
305 if ((flags
& TRANSPORT_PUSH_MIRROR
) && refspec
) {
306 if (!strcmp(*refspec
, "refs/tags/*"))
307 return error(_("--mirror and --tags are incompatible"));
308 return error(_("--mirror can't be combined with refspecs"));
311 if ((flags
& (TRANSPORT_PUSH_ALL
|TRANSPORT_PUSH_MIRROR
)) ==
312 (TRANSPORT_PUSH_ALL
|TRANSPORT_PUSH_MIRROR
)) {
313 return error(_("--all and --mirror are incompatible"));
316 if (!refspec
&& !(flags
& TRANSPORT_PUSH_ALL
)) {
317 if (remote
->push_refspec_nr
) {
318 refspec
= remote
->push_refspec
;
319 refspec_nr
= remote
->push_refspec_nr
;
320 } else if (!(flags
& TRANSPORT_PUSH_MIRROR
))
321 setup_default_push_refspecs(remote
);
324 url_nr
= push_url_of_remote(remote
, &url
);
326 for (i
= 0; i
< url_nr
; i
++) {
327 struct transport
*transport
=
328 transport_get(remote
, url
[i
]);
329 if (push_with_options(transport
, flags
))
333 struct transport
*transport
=
334 transport_get(remote
, NULL
);
336 if (push_with_options(transport
, flags
))
342 static int option_parse_recurse_submodules(const struct option
*opt
,
343 const char *arg
, int unset
)
345 int *flags
= opt
->value
;
347 if (*flags
& (TRANSPORT_RECURSE_SUBMODULES_CHECK
|
348 TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
))
349 die("%s can only be used once.", opt
->long_name
);
352 if (!strcmp(arg
, "check"))
353 *flags
|= TRANSPORT_RECURSE_SUBMODULES_CHECK
;
354 else if (!strcmp(arg
, "on-demand"))
355 *flags
|= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
;
357 die("bad %s argument: %s", opt
->long_name
, arg
);
359 die("option %s needs an argument (check|on-demand)",
365 int cmd_push(int argc
, const char **argv
, const char *prefix
)
370 const char *repo
= NULL
; /* default repository */
371 struct option options
[] = {
372 OPT__VERBOSITY(&verbosity
),
373 OPT_STRING( 0 , "repo", &repo
, N_("repository"), N_("repository")),
374 OPT_BIT( 0 , "all", &flags
, N_("push all refs"), TRANSPORT_PUSH_ALL
),
375 OPT_BIT( 0 , "mirror", &flags
, N_("mirror all refs"),
376 (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
)),
377 OPT_BOOLEAN( 0, "delete", &deleterefs
, N_("delete refs")),
378 OPT_BOOLEAN( 0 , "tags", &tags
, N_("push tags (can't be used with --all or --mirror)")),
379 OPT_BIT('n' , "dry-run", &flags
, N_("dry run"), TRANSPORT_PUSH_DRY_RUN
),
380 OPT_BIT( 0, "porcelain", &flags
, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN
),
381 OPT_BIT('f', "force", &flags
, N_("force updates"), TRANSPORT_PUSH_FORCE
),
382 { OPTION_CALLBACK
, 0, "recurse-submodules", &flags
, N_("check"),
383 N_("control recursive pushing of submodules"),
384 PARSE_OPT_OPTARG
, option_parse_recurse_submodules
},
385 OPT_BOOLEAN( 0 , "thin", &thin
, N_("use thin pack")),
386 OPT_STRING( 0 , "receive-pack", &receivepack
, "receive-pack", N_("receive pack program")),
387 OPT_STRING( 0 , "exec", &receivepack
, "receive-pack", N_("receive pack program")),
388 OPT_BIT('u', "set-upstream", &flags
, N_("set upstream for git pull/status"),
389 TRANSPORT_PUSH_SET_UPSTREAM
),
390 OPT_BOOL(0, "progress", &progress
, N_("force progress reporting")),
391 OPT_BIT(0, "prune", &flags
, N_("prune locally removed refs"),
392 TRANSPORT_PUSH_PRUNE
),
396 packet_trace_identity("push");
397 git_config(git_default_config
, NULL
);
398 argc
= parse_options(argc
, argv
, prefix
, options
, push_usage
, 0);
400 if (deleterefs
&& (tags
|| (flags
& (TRANSPORT_PUSH_ALL
| TRANSPORT_PUSH_MIRROR
))))
401 die(_("--delete is incompatible with --all, --mirror and --tags"));
402 if (deleterefs
&& argc
< 2)
403 die(_("--delete doesn't make sense without any refs"));
406 add_refspec("refs/tags/*");
410 set_refspecs(argv
+ 1, argc
- 1);
413 rc
= do_push(repo
, flags
);
415 usage_with_options(push_usage
, options
);