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;
22 static const char **refspec;
23 static int refspec_nr;
25 static void add_refspec(const char *ref)
27 int nr = refspec_nr + 1;
28 refspec = xrealloc(refspec, nr * sizeof(char *));
33 static void set_refspecs(const char **refs, int nr)
36 for (i = 0; i < nr; i++) {
37 const char *ref = refs[i];
38 if (!strcmp("tag", ref)) {
42 die("tag shorthand without <tag>");
43 len = strlen(refs[i]) + 11;
46 strcpy(tag, ":refs/tags/");
49 strcpy(tag, "refs/tags/");
53 } else if (deleterefs && !strchr(ref, ':')) {
55 int len = strlen(ref)+1;
56 delref = xmalloc(len+1);
60 } else if (deleterefs)
61 die("--delete only accepts plain target ref names");
66 static void setup_push_tracking(void)
68 struct strbuf refspec = STRBUF_INIT;
69 struct branch *branch = branch_get(NULL);
71 die("You are not currently on a branch.");
72 if (!branch->merge_nr)
73 die("The current branch %s is not tracking anything.",
75 if (branch->merge_nr != 1)
76 die("The current branch %s is tracking multiple branches, "
77 "refusing to push.", branch->name);
78 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
79 add_refspec(refspec.buf);
82 static void setup_default_push_refspecs(void)
84 switch (push_default) {
86 case PUSH_DEFAULT_MATCHING:
90 case PUSH_DEFAULT_TRACKING:
91 setup_push_tracking();
94 case PUSH_DEFAULT_CURRENT:
98 case PUSH_DEFAULT_NOTHING:
99 die("You didn't specify any refspecs to push, and "
100 "push.default is \"nothing\".");
105 static int push_with_options(struct transport *transport, int flags)
110 transport_set_verbosity(transport, verbosity, 0);
113 transport_set_option(transport,
114 TRANS_OPT_RECEIVEPACK, receivepack);
116 transport_set_option(transport, TRANS_OPT_THIN, "yes");
119 fprintf(stderr, "Pushing to %s\n", transport->url);
120 err = transport_push(transport, refspec_nr, refspec, flags,
123 error("failed to push some refs to '%s'", transport->url);
125 err |= transport_disconnect(transport);
130 if (nonfastforward && advice_push_nonfastforward) {
131 printf("To prevent you from losing history, non-fast-forward updates were rejected\n"
132 "Merge the remote changes before pushing again. See the 'Note about\n"
133 "fast-forwards' section of 'git push --help' for details.\n");
139 static int do_push(const char *repo, int flags)
142 struct remote *remote = remote_get(repo);
148 die("bad repository '%s'", repo);
149 die("No destination configured to push to.");
153 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
155 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
156 if (!strcmp(*refspec, "refs/tags/*"))
157 return error("--all and --tags are incompatible");
158 return error("--all can't be combined with refspecs");
161 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
162 if (!strcmp(*refspec, "refs/tags/*"))
163 return error("--mirror and --tags are incompatible");
164 return error("--mirror can't be combined with refspecs");
167 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
168 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
169 return error("--all and --mirror are incompatible");
172 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
173 if (remote->push_refspec_nr) {
174 refspec = remote->push_refspec;
175 refspec_nr = remote->push_refspec_nr;
176 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
177 setup_default_push_refspecs();
180 if (remote->pushurl_nr) {
181 url = remote->pushurl;
182 url_nr = remote->pushurl_nr;
185 url_nr = remote->url_nr;
188 for (i = 0; i < url_nr; i++) {
189 struct transport *transport =
190 transport_get(remote, url[i]);
191 if (push_with_options(transport, flags))
195 struct transport *transport =
196 transport_get(remote, NULL);
198 if (push_with_options(transport, flags))
204 int cmd_push(int argc, const char **argv, const char *prefix)
209 const char *repo = NULL; /* default repository */
210 struct option options[] = {
211 OPT__VERBOSITY(&verbosity),
212 OPT_STRING( 0 , "repo", &repo, "repository", "repository"),
213 OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
214 OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
215 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
216 OPT_BOOLEAN( 0, "delete", &deleterefs, "delete refs"),
217 OPT_BOOLEAN( 0 , "tags", &tags, "push tags (can't be used with --all or --mirror)"),
218 OPT_BIT('n' , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
219 OPT_BIT( 0, "porcelain", &flags, "machine-readable output", TRANSPORT_PUSH_PORCELAIN),
220 OPT_BIT('f', "force", &flags, "force updates", TRANSPORT_PUSH_FORCE),
221 OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
222 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
223 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
224 OPT_BIT('u', "set-upstream", &flags, "set upstream for git pull/status",
225 TRANSPORT_PUSH_SET_UPSTREAM),
229 git_config(git_default_config, NULL);
230 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
232 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
233 die("--delete is incompatible with --all, --mirror and --tags");
234 if (deleterefs && argc < 2)
235 die("--delete doesn't make sense without any refs");
238 add_refspec("refs/tags/*");
242 set_refspecs(argv + 1, argc - 1);
245 rc = do_push(repo, flags);
247 usage_with_options(push_usage, options);