6 #include "run-command.h"
10 #include "parse-options.h"
11 #include "submodule.h"
13 static const char * const push_usage
[] = {
14 "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
;
27 static int default_matching_used
;
29 static void add_refspec(const char *ref
)
32 ALLOC_GROW(refspec
, refspec_nr
, refspec_alloc
);
33 refspec
[refspec_nr
-1] = ref
;
36 static void set_refspecs(const char **refs
, int nr
)
39 for (i
= 0; i
< nr
; i
++) {
40 const char *ref
= refs
[i
];
41 if (!strcmp("tag", ref
)) {
45 die(_("tag shorthand without <tag>"));
46 len
= strlen(refs
[i
]) + 11;
49 strcpy(tag
, ":refs/tags/");
52 strcpy(tag
, "refs/tags/");
56 } else if (deleterefs
&& !strchr(ref
, ':')) {
58 int len
= strlen(ref
)+1;
59 delref
= xmalloc(len
+1);
63 } else if (deleterefs
)
64 die(_("--delete only accepts plain target ref names"));
69 static int push_url_of_remote(struct remote
*remote
, const char ***url_p
)
71 if (remote
->pushurl_nr
) {
72 *url_p
= remote
->pushurl
;
73 return remote
->pushurl_nr
;
76 return remote
->url_nr
;
79 static void setup_push_upstream(struct remote
*remote
)
81 struct strbuf refspec
= STRBUF_INIT
;
82 struct branch
*branch
= branch_get(NULL
);
84 die(_("You are not currently on a branch.\n"
85 "To push the history leading to the current (detached HEAD)\n"
88 " git push %s HEAD:<name-of-remote-branch>\n"),
90 if (!branch
->merge_nr
|| !branch
->merge
|| !branch
->remote_name
)
91 die(_("The current branch %s has no upstream branch.\n"
92 "To push the current branch and set the remote as upstream, use\n"
94 " git push --set-upstream %s %s\n"),
98 if (branch
->merge_nr
!= 1)
99 die(_("The current branch %s has multiple upstream branches, "
100 "refusing to push."), branch
->name
);
101 if (strcmp(branch
->remote_name
, remote
->name
))
102 die(_("You are pushing to remote '%s', which is not the upstream of\n"
103 "your current branch '%s', without telling me what to push\n"
104 "to update which remote branch."),
105 remote
->name
, branch
->name
);
107 strbuf_addf(&refspec
, "%s:%s", branch
->name
, branch
->merge
[0]->src
);
108 add_refspec(refspec
.buf
);
111 static void setup_default_push_refspecs(struct remote
*remote
)
113 switch (push_default
) {
115 case PUSH_DEFAULT_UNSPECIFIED
:
116 default_matching_used
= 1;
118 case PUSH_DEFAULT_MATCHING
:
122 case PUSH_DEFAULT_UPSTREAM
:
123 setup_push_upstream(remote
);
126 case PUSH_DEFAULT_CURRENT
:
130 case PUSH_DEFAULT_NOTHING
:
131 die(_("You didn't specify any refspecs to push, and "
132 "push.default is \"nothing\"."));
137 static const char message_advice_pull_before_push
[] =
138 N_("Updates were rejected because the tip of your current branch is behind\n"
139 "its remote counterpart. Merge the remote changes (e.g. 'git pull')\n"
140 "before pushing again.\n"
141 "See the 'Note about fast-forwards' in 'git push --help' for details.");
143 static const char message_advice_use_upstream
[] =
144 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
145 "counterpart. If you did not intend to push that branch, you may want to\n"
146 "specify branches to push or set the 'push.default' configuration\n"
147 "variable to 'current' or 'upstream' to push only the current branch.");
149 static const char message_advice_checkout_pull_push
[] =
150 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
151 "counterpart. Check out this branch and merge the remote changes\n"
152 "(e.g. 'git pull') before pushing again.\n"
153 "See the 'Note about fast-forwards' in 'git push --help' for details.");
155 static void advise_pull_before_push(void)
157 if (!advice_push_non_ff_current
|| !advice_push_nonfastforward
)
159 advise(_(message_advice_pull_before_push
));
162 static void advise_use_upstream(void)
164 if (!advice_push_non_ff_default
|| !advice_push_nonfastforward
)
166 advise(_(message_advice_use_upstream
));
169 static void advise_checkout_pull_push(void)
171 if (!advice_push_non_ff_matching
|| !advice_push_nonfastforward
)
173 advise(_(message_advice_checkout_pull_push
));
176 static int push_with_options(struct transport
*transport
, int flags
)
181 transport_set_verbosity(transport
, verbosity
, progress
);
184 transport_set_option(transport
,
185 TRANS_OPT_RECEIVEPACK
, receivepack
);
187 transport_set_option(transport
, TRANS_OPT_THIN
, "yes");
190 fprintf(stderr
, _("Pushing to %s\n"), transport
->url
);
191 err
= transport_push(transport
, refspec_nr
, refspec
, flags
,
194 error(_("failed to push some refs to '%s'"), transport
->url
);
196 err
|= transport_disconnect(transport
);
200 switch (nonfastforward
) {
204 advise_pull_before_push();
207 if (default_matching_used
)
208 advise_use_upstream();
210 advise_checkout_pull_push();
217 static int do_push(const char *repo
, int flags
)
220 struct remote
*remote
= remote_get(repo
);
226 die(_("bad repository '%s'"), repo
);
227 die(_("No configured push destination.\n"
228 "Either specify the URL from the command-line or configure a remote repository using\n"
230 " git remote add <name> <url>\n"
232 "and then push using the remote name\n"
234 " git push <name>\n"));
238 flags
|= (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
);
240 if ((flags
& TRANSPORT_PUSH_ALL
) && refspec
) {
241 if (!strcmp(*refspec
, "refs/tags/*"))
242 return error(_("--all and --tags are incompatible"));
243 return error(_("--all can't be combined with refspecs"));
246 if ((flags
& TRANSPORT_PUSH_MIRROR
) && refspec
) {
247 if (!strcmp(*refspec
, "refs/tags/*"))
248 return error(_("--mirror and --tags are incompatible"));
249 return error(_("--mirror can't be combined with refspecs"));
252 if ((flags
& (TRANSPORT_PUSH_ALL
|TRANSPORT_PUSH_MIRROR
)) ==
253 (TRANSPORT_PUSH_ALL
|TRANSPORT_PUSH_MIRROR
)) {
254 return error(_("--all and --mirror are incompatible"));
257 if (!refspec
&& !(flags
& TRANSPORT_PUSH_ALL
)) {
258 if (remote
->push_refspec_nr
) {
259 refspec
= remote
->push_refspec
;
260 refspec_nr
= remote
->push_refspec_nr
;
261 } else if (!(flags
& TRANSPORT_PUSH_MIRROR
))
262 setup_default_push_refspecs(remote
);
265 url_nr
= push_url_of_remote(remote
, &url
);
267 for (i
= 0; i
< url_nr
; i
++) {
268 struct transport
*transport
=
269 transport_get(remote
, url
[i
]);
270 if (push_with_options(transport
, flags
))
274 struct transport
*transport
=
275 transport_get(remote
, NULL
);
277 if (push_with_options(transport
, flags
))
283 static int option_parse_recurse_submodules(const struct option
*opt
,
284 const char *arg
, int unset
)
286 int *flags
= opt
->value
;
288 if (!strcmp(arg
, "check"))
289 *flags
|= TRANSPORT_RECURSE_SUBMODULES_CHECK
;
291 die("bad %s argument: %s", opt
->long_name
, arg
);
293 die("option %s needs an argument (check)", opt
->long_name
);
298 int cmd_push(int argc
, const char **argv
, const char *prefix
)
303 const char *repo
= NULL
; /* default repository */
304 struct option options
[] = {
305 OPT__VERBOSITY(&verbosity
),
306 OPT_STRING( 0 , "repo", &repo
, "repository", "repository"),
307 OPT_BIT( 0 , "all", &flags
, "push all refs", TRANSPORT_PUSH_ALL
),
308 OPT_BIT( 0 , "mirror", &flags
, "mirror all refs",
309 (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
)),
310 OPT_BOOLEAN( 0, "delete", &deleterefs
, "delete refs"),
311 OPT_BOOLEAN( 0 , "tags", &tags
, "push tags (can't be used with --all or --mirror)"),
312 OPT_BIT('n' , "dry-run", &flags
, "dry run", TRANSPORT_PUSH_DRY_RUN
),
313 OPT_BIT( 0, "porcelain", &flags
, "machine-readable output", TRANSPORT_PUSH_PORCELAIN
),
314 OPT_BIT('f', "force", &flags
, "force updates", TRANSPORT_PUSH_FORCE
),
315 { OPTION_CALLBACK
, 0, "recurse-submodules", &flags
, "check",
316 "controls recursive pushing of submodules",
317 PARSE_OPT_OPTARG
, option_parse_recurse_submodules
},
318 OPT_BOOLEAN( 0 , "thin", &thin
, "use thin pack"),
319 OPT_STRING( 0 , "receive-pack", &receivepack
, "receive-pack", "receive pack program"),
320 OPT_STRING( 0 , "exec", &receivepack
, "receive-pack", "receive pack program"),
321 OPT_BIT('u', "set-upstream", &flags
, "set upstream for git pull/status",
322 TRANSPORT_PUSH_SET_UPSTREAM
),
323 OPT_BOOL(0, "progress", &progress
, "force progress reporting"),
324 OPT_BIT(0, "prune", &flags
, "prune locally removed refs",
325 TRANSPORT_PUSH_PRUNE
),
329 packet_trace_identity("push");
330 git_config(git_default_config
, NULL
);
331 argc
= parse_options(argc
, argv
, prefix
, options
, push_usage
, 0);
333 if (deleterefs
&& (tags
|| (flags
& (TRANSPORT_PUSH_ALL
| TRANSPORT_PUSH_MIRROR
))))
334 die(_("--delete is incompatible with --all, --mirror and --tags"));
335 if (deleterefs
&& argc
< 2)
336 die(_("--delete doesn't make sense without any refs"));
339 add_refspec("refs/tags/*");
343 set_refspecs(argv
+ 1, argc
- 1);
346 rc
= do_push(repo
, flags
);
348 usage_with_options(push_usage
, options
);