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
;
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 void setup_push_upstream(struct remote
*remote
)
70 struct strbuf refspec
= STRBUF_INIT
;
71 struct branch
*branch
= branch_get(NULL
);
73 die(_("You are not currently on a branch.\n"
74 "To push the history leading to the current (detached HEAD)\n"
77 " git push %s HEAD:<name-of-remote-branch>\n"),
79 if (!branch
->merge_nr
|| !branch
->merge
)
80 die(_("The current branch %s has no upstream branch.\n"
81 "To push the current branch and set the remote as upstream, use\n"
83 " git push --set-upstream %s %s\n"),
87 if (branch
->merge_nr
!= 1)
88 die(_("The current branch %s has multiple upstream branches, "
89 "refusing to push."), branch
->name
);
90 strbuf_addf(&refspec
, "%s:%s", branch
->name
, branch
->merge
[0]->src
);
91 add_refspec(refspec
.buf
);
94 static void setup_default_push_refspecs(struct remote
*remote
)
96 switch (push_default
) {
98 case PUSH_DEFAULT_MATCHING
:
102 case PUSH_DEFAULT_UPSTREAM
:
103 setup_push_upstream(remote
);
106 case PUSH_DEFAULT_CURRENT
:
110 case PUSH_DEFAULT_NOTHING
:
111 die(_("You didn't specify any refspecs to push, and "
112 "push.default is \"nothing\"."));
117 static int push_with_options(struct transport
*transport
, int flags
)
122 transport_set_verbosity(transport
, verbosity
, progress
);
125 transport_set_option(transport
,
126 TRANS_OPT_RECEIVEPACK
, receivepack
);
128 transport_set_option(transport
, TRANS_OPT_THIN
, "yes");
131 fprintf(stderr
, _("Pushing to %s\n"), transport
->url
);
132 err
= transport_push(transport
, refspec_nr
, refspec
, flags
,
135 error(_("failed to push some refs to '%s'"), transport
->url
);
137 err
|= transport_disconnect(transport
);
142 if (nonfastforward
&& advice_push_nonfastforward
) {
143 fprintf(stderr
, _("To prevent you from losing history, non-fast-forward updates were rejected\n"
144 "Merge the remote changes (e.g. 'git pull') before pushing again. See the\n"
145 "'Note about fast-forwards' section of 'git push --help' for details.\n"));
151 static int do_push(const char *repo
, int flags
)
154 struct remote
*remote
= remote_get(repo
);
160 die(_("bad repository '%s'"), repo
);
161 die(_("No configured push destination.\n"
162 "Either specify the URL from the command-line or configure a remote repository using\n"
164 " git remote add <name> <url>\n"
166 "and then push using the remote name\n"
168 " git push <name>\n"));
172 flags
|= (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
);
174 if ((flags
& TRANSPORT_PUSH_ALL
) && refspec
) {
175 if (!strcmp(*refspec
, "refs/tags/*"))
176 return error(_("--all and --tags are incompatible"));
177 return error(_("--all can't be combined with refspecs"));
180 if ((flags
& TRANSPORT_PUSH_MIRROR
) && refspec
) {
181 if (!strcmp(*refspec
, "refs/tags/*"))
182 return error(_("--mirror and --tags are incompatible"));
183 return error(_("--mirror can't be combined with refspecs"));
186 if ((flags
& (TRANSPORT_PUSH_ALL
|TRANSPORT_PUSH_MIRROR
)) ==
187 (TRANSPORT_PUSH_ALL
|TRANSPORT_PUSH_MIRROR
)) {
188 return error(_("--all and --mirror are incompatible"));
191 if (!refspec
&& !(flags
& TRANSPORT_PUSH_ALL
)) {
192 if (remote
->push_refspec_nr
) {
193 refspec
= remote
->push_refspec
;
194 refspec_nr
= remote
->push_refspec_nr
;
195 } else if (!(flags
& TRANSPORT_PUSH_MIRROR
))
196 setup_default_push_refspecs(remote
);
199 if (remote
->pushurl_nr
) {
200 url
= remote
->pushurl
;
201 url_nr
= remote
->pushurl_nr
;
204 url_nr
= remote
->url_nr
;
207 for (i
= 0; i
< url_nr
; i
++) {
208 struct transport
*transport
=
209 transport_get(remote
, url
[i
]);
210 if (push_with_options(transport
, flags
))
214 struct transport
*transport
=
215 transport_get(remote
, NULL
);
217 if (push_with_options(transport
, flags
))
223 static int option_parse_recurse_submodules(const struct option
*opt
,
224 const char *arg
, int unset
)
226 int *flags
= opt
->value
;
228 if (*flags
& (TRANSPORT_RECURSE_SUBMODULES_CHECK
|
229 TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
))
230 die("%s can only be used once.", opt
->long_name
);
233 if (!strcmp(arg
, "check"))
234 *flags
|= TRANSPORT_RECURSE_SUBMODULES_CHECK
;
235 else if (!strcmp(arg
, "on-demand"))
236 *flags
|= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
;
238 die("bad %s argument: %s", opt
->long_name
, arg
);
240 die("option %s needs an argument (check|on-demand)",
246 int cmd_push(int argc
, const char **argv
, const char *prefix
)
251 const char *repo
= NULL
; /* default repository */
252 struct option options
[] = {
253 OPT__VERBOSITY(&verbosity
),
254 OPT_STRING( 0 , "repo", &repo
, "repository", "repository"),
255 OPT_BIT( 0 , "all", &flags
, "push all refs", TRANSPORT_PUSH_ALL
),
256 OPT_BIT( 0 , "mirror", &flags
, "mirror all refs",
257 (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
)),
258 OPT_BOOLEAN( 0, "delete", &deleterefs
, "delete refs"),
259 OPT_BOOLEAN( 0 , "tags", &tags
, "push tags (can't be used with --all or --mirror)"),
260 OPT_BIT('n' , "dry-run", &flags
, "dry run", TRANSPORT_PUSH_DRY_RUN
),
261 OPT_BIT( 0, "porcelain", &flags
, "machine-readable output", TRANSPORT_PUSH_PORCELAIN
),
262 OPT_BIT('f', "force", &flags
, "force updates", TRANSPORT_PUSH_FORCE
),
263 { OPTION_CALLBACK
, 0, "recurse-submodules", &flags
, "check",
264 "controls recursive pushing of submodules",
265 PARSE_OPT_OPTARG
, option_parse_recurse_submodules
},
266 OPT_BOOLEAN( 0 , "thin", &thin
, "use thin pack"),
267 OPT_STRING( 0 , "receive-pack", &receivepack
, "receive-pack", "receive pack program"),
268 OPT_STRING( 0 , "exec", &receivepack
, "receive-pack", "receive pack program"),
269 OPT_BIT('u', "set-upstream", &flags
, "set upstream for git pull/status",
270 TRANSPORT_PUSH_SET_UPSTREAM
),
271 OPT_BOOLEAN(0, "progress", &progress
, "force progress reporting"),
275 packet_trace_identity("push");
276 git_config(git_default_config
, NULL
);
277 argc
= parse_options(argc
, argv
, prefix
, options
, push_usage
, 0);
279 if (deleterefs
&& (tags
|| (flags
& (TRANSPORT_PUSH_ALL
| TRANSPORT_PUSH_MIRROR
))))
280 die(_("--delete is incompatible with --all, --mirror and --tags"));
281 if (deleterefs
&& argc
< 2)
282 die(_("--delete doesn't make sense without any refs"));
285 add_refspec("refs/tags/*");
289 set_refspecs(argv
+ 1, argc
- 1);
292 rc
= do_push(repo
, flags
);
294 usage_with_options(push_usage
, options
);