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;
21 static const char **refspec;
22 static int refspec_nr;
24 static void add_refspec(const char *ref)
26 int nr = refspec_nr + 1;
27 refspec = xrealloc(refspec, nr * sizeof(char *));
32 static void set_refspecs(const char **refs, int nr)
35 for (i = 0; i < nr; i++) {
36 const char *ref = refs[i];
37 if (!strcmp("tag", ref)) {
41 die("tag shorthand without <tag>");
42 len = strlen(refs[i]) + 11;
45 strcpy(tag, ":refs/tags/");
48 strcpy(tag, "refs/tags/");
52 } else if (deleterefs && !strchr(ref, ':')) {
54 int len = strlen(ref)+1;
55 delref = xmalloc(len);
59 } else if (deleterefs)
60 die("--delete only accepts plain target ref names");
65 static void setup_push_tracking(void)
67 struct strbuf refspec = STRBUF_INIT;
68 struct branch *branch = branch_get(NULL);
70 die("You are not currently on a branch.");
71 if (!branch->merge_nr)
72 die("The current branch %s is not tracking anything.",
74 if (branch->merge_nr != 1)
75 die("The current branch %s is tracking multiple branches, "
76 "refusing to push.", branch->name);
77 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
78 add_refspec(refspec.buf);
81 static void setup_default_push_refspecs(void)
83 switch (push_default) {
85 case PUSH_DEFAULT_MATCHING:
89 case PUSH_DEFAULT_TRACKING:
90 setup_push_tracking();
93 case PUSH_DEFAULT_CURRENT:
97 case PUSH_DEFAULT_NOTHING:
98 die("You didn't specify any refspecs to push, and "
99 "push.default is \"nothing\".");
104 static int push_with_options(struct transport *transport, int flags)
109 transport_set_option(transport,
110 TRANS_OPT_RECEIVEPACK, receivepack);
112 transport_set_option(transport, TRANS_OPT_THIN, "yes");
114 if (flags & TRANSPORT_PUSH_VERBOSE)
115 fprintf(stderr, "Pushing to %s\n", transport->url);
116 err = transport_push(transport, refspec_nr, refspec, flags,
119 error("failed to push some refs to '%s'", transport->url);
121 err |= transport_disconnect(transport);
126 if (nonfastforward && advice_push_nonfastforward) {
127 printf("To prevent you from losing history, non-fast-forward updates were rejected\n"
128 "Merge the remote changes before pushing again. See the 'Note about\n"
129 "fast-forwards' section of 'git push --help' for details.\n");
135 static int do_push(const char *repo, int flags)
138 struct remote *remote = remote_get(repo);
144 die("bad repository '%s'", repo);
145 die("No destination configured to push to.");
149 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
151 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
152 if (!strcmp(*refspec, "refs/tags/*"))
153 return error("--all and --tags are incompatible");
154 return error("--all can't be combined with refspecs");
157 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
158 if (!strcmp(*refspec, "refs/tags/*"))
159 return error("--mirror and --tags are incompatible");
160 return error("--mirror can't be combined with refspecs");
163 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
164 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
165 return error("--all and --mirror are incompatible");
168 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
169 if (remote->push_refspec_nr) {
170 refspec = remote->push_refspec;
171 refspec_nr = remote->push_refspec_nr;
172 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
173 setup_default_push_refspecs();
176 if (remote->pushurl_nr) {
177 url = remote->pushurl;
178 url_nr = remote->pushurl_nr;
181 url_nr = remote->url_nr;
184 for (i = 0; i < url_nr; i++) {
185 struct transport *transport =
186 transport_get(remote, url[i]);
187 if (push_with_options(transport, flags))
191 struct transport *transport =
192 transport_get(remote, NULL);
194 if (push_with_options(transport, flags))
200 int cmd_push(int argc, const char **argv, const char *prefix)
205 const char *repo = NULL; /* default repository */
206 struct option options[] = {
207 OPT_BIT('q', "quiet", &flags, "be quiet", TRANSPORT_PUSH_QUIET),
208 OPT_BIT('v', "verbose", &flags, "be verbose", TRANSPORT_PUSH_VERBOSE),
209 OPT_STRING( 0 , "repo", &repo, "repository", "repository"),
210 OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
211 OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
212 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
213 OPT_BOOLEAN( 0, "delete", &deleterefs, "delete refs"),
214 OPT_BOOLEAN( 0 , "tags", &tags, "push tags (can't be used with --all or --mirror)"),
215 OPT_BIT('n' , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
216 OPT_BIT( 0, "porcelain", &flags, "machine-readable output", TRANSPORT_PUSH_PORCELAIN),
217 OPT_BIT('f', "force", &flags, "force updates", TRANSPORT_PUSH_FORCE),
218 OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
219 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
220 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
221 OPT_BIT('u', "set-upstream", &flags, "set upstream for git pull/status",
222 TRANSPORT_PUSH_SET_UPSTREAM),
226 git_config(git_default_config, NULL);
227 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
229 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
230 die("--delete is incompatible with --all, --mirror and --tags");
231 if (deleterefs && argc < 2)
232 die("--delete doesn't make sense without any refs");
235 add_refspec("refs/tags/*");
239 set_refspecs(argv + 1, argc - 1);
242 rc = do_push(repo, flags);
244 usage_with_options(push_usage, options);