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_tracking(void)
69 struct strbuf refspec = STRBUF_INIT;
70 struct branch *branch = branch_get(NULL);
72 die("You are not currently on a branch.");
73 if (!branch->merge_nr || !branch->merge)
74 die("The current branch %s is not tracking anything.",
76 if (branch->merge_nr != 1)
77 die("The current branch %s is tracking multiple branches, "
78 "refusing to push.", branch->name);
79 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
80 add_refspec(refspec.buf);
83 static void setup_default_push_refspecs(void)
85 switch (push_default) {
87 case PUSH_DEFAULT_MATCHING:
91 case PUSH_DEFAULT_TRACKING:
92 setup_push_tracking();
95 case PUSH_DEFAULT_CURRENT:
99 case PUSH_DEFAULT_NOTHING:
100 die("You didn't specify any refspecs to push, and "
101 "push.default is \"nothing\".");
106 static int push_with_options(struct transport *transport, int flags)
111 transport_set_verbosity(transport, verbosity, progress);
114 transport_set_option(transport,
115 TRANS_OPT_RECEIVEPACK, receivepack);
117 transport_set_option(transport, TRANS_OPT_THIN, "yes");
120 fprintf(stderr, "Pushing to %s\n", transport->url);
121 err = transport_push(transport, refspec_nr, refspec, flags,
124 error("failed to push some refs to '%s'", transport->url);
126 err |= transport_disconnect(transport);
131 if (nonfastforward && advice_push_nonfastforward) {
132 fprintf(stderr, "To prevent you from losing history, non-fast-forward updates were rejected\n"
133 "Merge the remote changes (e.g. 'git pull') before pushing again. See the\n"
134 "'Note about fast-forwards' section of 'git push --help' for details.\n");
140 static int do_push(const char *repo, int flags)
143 struct remote *remote = remote_get(repo);
149 die("bad repository '%s'", repo);
150 die("No destination configured to push to.");
154 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
156 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
157 if (!strcmp(*refspec, "refs/tags/*"))
158 return error("--all and --tags are incompatible");
159 return error("--all can't be combined with refspecs");
162 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
163 if (!strcmp(*refspec, "refs/tags/*"))
164 return error("--mirror and --tags are incompatible");
165 return error("--mirror can't be combined with refspecs");
168 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
169 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
170 return error("--all and --mirror are incompatible");
173 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
174 if (remote->push_refspec_nr) {
175 refspec = remote->push_refspec;
176 refspec_nr = remote->push_refspec_nr;
177 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
178 setup_default_push_refspecs();
181 if (remote->pushurl_nr) {
182 url = remote->pushurl;
183 url_nr = remote->pushurl_nr;
186 url_nr = remote->url_nr;
189 for (i = 0; i < url_nr; i++) {
190 struct transport *transport =
191 transport_get(remote, url[i]);
192 if (push_with_options(transport, flags))
196 struct transport *transport =
197 transport_get(remote, NULL);
199 if (push_with_options(transport, flags))
205 int cmd_push(int argc, const char **argv, const char *prefix)
210 const char *repo = NULL; /* default repository */
211 struct option options[] = {
212 OPT__VERBOSITY(&verbosity),
213 OPT_STRING( 0 , "repo", &repo, "repository", "repository"),
214 OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
215 OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
216 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
217 OPT_BOOLEAN( 0, "delete", &deleterefs, "delete refs"),
218 OPT_BOOLEAN( 0 , "tags", &tags, "push tags (can't be used with --all or --mirror)"),
219 OPT_BIT('n' , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
220 OPT_BIT( 0, "porcelain", &flags, "machine-readable output", TRANSPORT_PUSH_PORCELAIN),
221 OPT_BIT('f', "force", &flags, "force updates", TRANSPORT_PUSH_FORCE),
222 OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
223 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
224 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
225 OPT_BIT('u', "set-upstream", &flags, "set upstream for git pull/status",
226 TRANSPORT_PUSH_SET_UPSTREAM),
227 OPT_BOOLEAN(0, "progress", &progress, "force progress reporting"),
231 git_config(git_default_config, NULL);
232 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
234 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
235 die("--delete is incompatible with --all, --mirror and --tags");
236 if (deleterefs && argc < 2)
237 die("--delete doesn't make sense without any refs");
240 add_refspec("refs/tags/*");
244 set_refspecs(argv + 1, argc - 1);
247 rc = do_push(repo, flags);
249 usage_with_options(push_usage, options);