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 do_push(const char *repo, int flags)
107 struct remote *remote = remote_get(repo);
113 die("bad repository '%s'", repo);
114 die("No destination configured to push to.");
118 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
120 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
121 if (!strcmp(*refspec, "refs/tags/*"))
122 return error("--all and --tags are incompatible");
123 return error("--all can't be combined with refspecs");
126 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
127 if (!strcmp(*refspec, "refs/tags/*"))
128 return error("--mirror and --tags are incompatible");
129 return error("--mirror can't be combined with refspecs");
132 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
133 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
134 return error("--all and --mirror are incompatible");
137 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
138 if (remote->push_refspec_nr) {
139 refspec = remote->push_refspec;
140 refspec_nr = remote->push_refspec_nr;
141 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
142 setup_default_push_refspecs();
145 if (remote->pushurl_nr) {
146 url = remote->pushurl;
147 url_nr = remote->pushurl_nr;
150 url_nr = remote->url_nr;
152 for (i = 0; i < url_nr; i++) {
153 struct transport *transport =
154 transport_get(remote, url[i]);
158 transport_set_option(transport,
159 TRANS_OPT_RECEIVEPACK, receivepack);
161 transport_set_option(transport, TRANS_OPT_THIN, "yes");
163 if (flags & TRANSPORT_PUSH_VERBOSE)
164 fprintf(stderr, "Pushing to %s\n", url[i]);
165 err = transport_push(transport, refspec_nr, refspec, flags,
167 err |= transport_disconnect(transport);
172 error("failed to push some refs to '%s'", url[i]);
173 if (nonfastforward && advice_push_nonfastforward) {
174 printf("To prevent you from losing history, non-fast-forward updates were rejected\n"
175 "Merge the remote changes before pushing again. See the 'non-fast-forward'\n"
176 "section of 'git push --help' for details.\n");
183 int cmd_push(int argc, const char **argv, const char *prefix)
188 const char *repo = NULL; /* default repository */
189 struct option options[] = {
190 OPT_BIT('q', "quiet", &flags, "be quiet", TRANSPORT_PUSH_QUIET),
191 OPT_BIT('v', "verbose", &flags, "be verbose", TRANSPORT_PUSH_VERBOSE),
192 OPT_STRING( 0 , "repo", &repo, "repository", "repository"),
193 OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
194 OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
195 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
196 OPT_BOOLEAN( 0, "delete", &deleterefs, "delete refs"),
197 OPT_BOOLEAN( 0 , "tags", &tags, "push tags (can't be used with --all or --mirror)"),
198 OPT_BIT('n' , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
199 OPT_BIT( 0, "porcelain", &flags, "machine-readable output", TRANSPORT_PUSH_PORCELAIN),
200 OPT_BIT('f', "force", &flags, "force updates", TRANSPORT_PUSH_FORCE),
201 OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
202 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
203 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
207 git_config(git_default_config, NULL);
208 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
210 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
211 die("--delete is incompatible with --all, --mirror and --tags");
212 if (deleterefs && argc < 2)
213 die("--delete doesn't make sense without any refs");
216 add_refspec("refs/tags/*");
220 set_refspecs(argv + 1, argc - 1);
223 rc = do_push(repo, flags);
225 usage_with_options(push_usage, options);