6 #include "run-command.h"
10 #include "parse-options.h"
12 static const char * const push_usage[] = {
13 "git push [<options>] [<repository> [<refspec>...]]",
18 static int deleterefs;
19 static const char *receivepack;
23 static const char **refspec;
24 static int refspec_nr;
25 static int refspec_alloc;
27 static void add_refspec(const char *ref)
30 ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
31 refspec[refspec_nr-1] = ref;
34 static void set_refspecs(const char **refs, int nr)
37 for (i = 0; i < nr; i++) {
38 const char *ref = refs[i];
39 if (!strcmp("tag", ref)) {
43 die("tag shorthand without <tag>");
44 len = strlen(refs[i]) + 11;
47 strcpy(tag, ":refs/tags/");
50 strcpy(tag, "refs/tags/");
54 } else if (deleterefs && !strchr(ref, ':')) {
56 int len = strlen(ref)+1;
57 delref = xmalloc(len+1);
61 } else if (deleterefs)
62 die("--delete only accepts plain target ref names");
67 static void setup_push_upstream(struct remote *remote)
69 struct strbuf refspec = STRBUF_INIT;
70 struct branch *branch = branch_get(NULL);
72 die("You are not currently on a branch.\n"
73 "To push the history leading to the current (detached HEAD)\n"
76 " git push %s HEAD:<name-of-remote-branch>\n",
78 if (!branch->merge_nr || !branch->merge)
79 die("The current branch %s has no upstream branch.\n"
80 "To push the current branch and set the remote as upstream, use\n"
82 " git push --set-upstream %s %s\n",
86 if (branch->merge_nr != 1)
87 die("The current branch %s has multiple upstream branches, "
88 "refusing to push.", branch->name);
89 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
90 add_refspec(refspec.buf);
93 static void setup_default_push_refspecs(struct remote *remote)
95 switch (push_default) {
97 case PUSH_DEFAULT_MATCHING:
101 case PUSH_DEFAULT_UPSTREAM:
102 setup_push_upstream(remote);
105 case PUSH_DEFAULT_CURRENT:
109 case PUSH_DEFAULT_NOTHING:
110 die("You didn't specify any refspecs to push, and "
111 "push.default is \"nothing\".");
116 static int push_with_options(struct transport *transport, int flags)
121 transport_set_verbosity(transport, verbosity, progress);
124 transport_set_option(transport,
125 TRANS_OPT_RECEIVEPACK, receivepack);
127 transport_set_option(transport, TRANS_OPT_THIN, "yes");
130 fprintf(stderr, "Pushing to %s\n", transport->url);
131 err = transport_push(transport, refspec_nr, refspec, flags,
134 error("failed to push some refs to '%s'", transport->url);
136 err |= transport_disconnect(transport);
141 if (nonfastforward && advice_push_nonfastforward) {
142 fprintf(stderr, "To prevent you from losing history, non-fast-forward updates were rejected\n"
143 "Merge the remote changes (e.g. 'git pull') before pushing again. See the\n"
144 "'Note about fast-forwards' section of 'git push --help' for details.\n");
150 static int do_push(const char *repo, int flags)
153 struct remote *remote = remote_get(repo);
159 die("bad repository '%s'", repo);
160 die("No configured push destination.\n"
161 "Either specify the URL from the command-line or configure a remote repository using\n"
163 " git remote add <name> <url>\n"
165 "and then push using the remote name\n"
167 " git push <name>\n");
171 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
173 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
174 if (!strcmp(*refspec, "refs/tags/*"))
175 return error("--all and --tags are incompatible");
176 return error("--all can't be combined with refspecs");
179 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
180 if (!strcmp(*refspec, "refs/tags/*"))
181 return error("--mirror and --tags are incompatible");
182 return error("--mirror can't be combined with refspecs");
185 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
186 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
187 return error("--all and --mirror are incompatible");
190 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
191 if (remote->push_refspec_nr) {
192 refspec = remote->push_refspec;
193 refspec_nr = remote->push_refspec_nr;
194 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
195 setup_default_push_refspecs(remote);
198 if (remote->pushurl_nr) {
199 url = remote->pushurl;
200 url_nr = remote->pushurl_nr;
203 url_nr = remote->url_nr;
206 for (i = 0; i < url_nr; i++) {
207 struct transport *transport =
208 transport_get(remote, url[i]);
209 if (push_with_options(transport, flags))
213 struct transport *transport =
214 transport_get(remote, NULL);
216 if (push_with_options(transport, flags))
222 int cmd_push(int argc, const char **argv, const char *prefix)
227 const char *repo = NULL; /* default repository */
228 struct option options[] = {
229 OPT__VERBOSITY(&verbosity),
230 OPT_STRING( 0 , "repo", &repo, "repository", "repository"),
231 OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
232 OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
233 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
234 OPT_BOOLEAN( 0, "delete", &deleterefs, "delete refs"),
235 OPT_BOOLEAN( 0 , "tags", &tags, "push tags (can't be used with --all or --mirror)"),
236 OPT_BIT('n' , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
237 OPT_BIT( 0, "porcelain", &flags, "machine-readable output", TRANSPORT_PUSH_PORCELAIN),
238 OPT_BIT('f', "force", &flags, "force updates", TRANSPORT_PUSH_FORCE),
239 OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
240 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
241 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
242 OPT_BIT('u', "set-upstream", &flags, "set upstream for git pull/status",
243 TRANSPORT_PUSH_SET_UPSTREAM),
244 OPT_BOOLEAN(0, "progress", &progress, "force progress reporting"),
248 packet_trace_identity("push");
249 git_config(git_default_config, NULL);
250 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
252 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
253 die("--delete is incompatible with --all, --mirror and --tags");
254 if (deleterefs && argc < 2)
255 die("--delete doesn't make sense without any refs");
258 add_refspec("refs/tags/*");
262 set_refspecs(argv + 1, argc - 1);
265 rc = do_push(repo, flags);
267 usage_with_options(push_usage, options);