Fix build problems related to profile-directed optimization
[alt-git.git] / builtin / push.c
blob35cce532f2bb632e01c0de0a8e6f9e1395eece88
1 /*
2 * "git push"
3 */
4 #include "cache.h"
5 #include "refs.h"
6 #include "run-command.h"
7 #include "builtin.h"
8 #include "remote.h"
9 #include "transport.h"
10 #include "parse-options.h"
11 #include "submodule.h"
13 static const char * const push_usage[] = {
14 "git push [<options>] [<repository> [<refspec>...]]",
15 NULL,
18 static int thin;
19 static int deleterefs;
20 static const char *receivepack;
21 static int verbosity;
22 static int progress;
24 static const char **refspec;
25 static int refspec_nr;
26 static int refspec_alloc;
28 static void add_refspec(const char *ref)
30 refspec_nr++;
31 ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
32 refspec[refspec_nr-1] = ref;
35 static void set_refspecs(const char **refs, int nr)
37 int i;
38 for (i = 0; i < nr; i++) {
39 const char *ref = refs[i];
40 if (!strcmp("tag", ref)) {
41 char *tag;
42 int len;
43 if (nr <= ++i)
44 die(_("tag shorthand without <tag>"));
45 len = strlen(refs[i]) + 11;
46 if (deleterefs) {
47 tag = xmalloc(len+1);
48 strcpy(tag, ":refs/tags/");
49 } else {
50 tag = xmalloc(len);
51 strcpy(tag, "refs/tags/");
53 strcat(tag, refs[i]);
54 ref = tag;
55 } else if (deleterefs && !strchr(ref, ':')) {
56 char *delref;
57 int len = strlen(ref)+1;
58 delref = xmalloc(len+1);
59 strcpy(delref, ":");
60 strcat(delref, ref);
61 ref = delref;
62 } else if (deleterefs)
63 die(_("--delete only accepts plain target ref names"));
64 add_refspec(ref);
68 static void setup_push_upstream(struct remote *remote)
70 struct strbuf refspec = STRBUF_INIT;
71 struct branch *branch = branch_get(NULL);
72 if (!branch)
73 die(_("You are not currently on a branch.\n"
74 "To push the history leading to the current (detached HEAD)\n"
75 "state now, use\n"
76 "\n"
77 " git push %s HEAD:<name-of-remote-branch>\n"),
78 remote->name);
79 if (!branch->merge_nr || !branch->merge)
80 die(_("The current branch %s has no upstream branch.\n"
81 "To push the current branch and set the remote as upstream, use\n"
82 "\n"
83 " git push --set-upstream %s %s\n"),
84 branch->name,
85 remote->name,
86 branch->name);
87 if (branch->merge_nr != 1)
88 die(_("The current branch %s has multiple upstream branches, "
89 "refusing to push."), branch->name);
90 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
91 add_refspec(refspec.buf);
94 static void setup_default_push_refspecs(struct remote *remote)
96 switch (push_default) {
97 default:
98 case PUSH_DEFAULT_MATCHING:
99 add_refspec(":");
100 break;
102 case PUSH_DEFAULT_UPSTREAM:
103 setup_push_upstream(remote);
104 break;
106 case PUSH_DEFAULT_CURRENT:
107 add_refspec("HEAD");
108 break;
110 case PUSH_DEFAULT_NOTHING:
111 die(_("You didn't specify any refspecs to push, and "
112 "push.default is \"nothing\"."));
113 break;
117 static int push_with_options(struct transport *transport, int flags)
119 int err;
120 int nonfastforward;
122 transport_set_verbosity(transport, verbosity, progress);
124 if (receivepack)
125 transport_set_option(transport,
126 TRANS_OPT_RECEIVEPACK, receivepack);
127 if (thin)
128 transport_set_option(transport, TRANS_OPT_THIN, "yes");
130 if (verbosity > 0)
131 fprintf(stderr, _("Pushing to %s\n"), transport->url);
132 err = transport_push(transport, refspec_nr, refspec, flags,
133 &nonfastforward);
134 if (err != 0)
135 error(_("failed to push some refs to '%s'"), transport->url);
137 err |= transport_disconnect(transport);
139 if (!err)
140 return 0;
142 if (nonfastforward && advice_push_nonfastforward) {
143 fprintf(stderr, _("To prevent you from losing history, non-fast-forward updates were rejected\n"
144 "Merge the remote changes (e.g. 'git pull') before pushing again. See the\n"
145 "'Note about fast-forwards' section of 'git push --help' for details.\n"));
148 return 1;
151 static int do_push(const char *repo, int flags)
153 int i, errs;
154 struct remote *remote = remote_get(repo);
155 const char **url;
156 int url_nr;
158 if (!remote) {
159 if (repo)
160 die(_("bad repository '%s'"), repo);
161 die(_("No configured push destination.\n"
162 "Either specify the URL from the command-line or configure a remote repository using\n"
163 "\n"
164 " git remote add <name> <url>\n"
165 "\n"
166 "and then push using the remote name\n"
167 "\n"
168 " git push <name>\n"));
171 if (remote->mirror)
172 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
174 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
175 if (!strcmp(*refspec, "refs/tags/*"))
176 return error(_("--all and --tags are incompatible"));
177 return error(_("--all can't be combined with refspecs"));
180 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
181 if (!strcmp(*refspec, "refs/tags/*"))
182 return error(_("--mirror and --tags are incompatible"));
183 return error(_("--mirror can't be combined with refspecs"));
186 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
187 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
188 return error(_("--all and --mirror are incompatible"));
191 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
192 if (remote->push_refspec_nr) {
193 refspec = remote->push_refspec;
194 refspec_nr = remote->push_refspec_nr;
195 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
196 setup_default_push_refspecs(remote);
198 errs = 0;
199 if (remote->pushurl_nr) {
200 url = remote->pushurl;
201 url_nr = remote->pushurl_nr;
202 } else {
203 url = remote->url;
204 url_nr = remote->url_nr;
206 if (url_nr) {
207 for (i = 0; i < url_nr; i++) {
208 struct transport *transport =
209 transport_get(remote, url[i]);
210 if (push_with_options(transport, flags))
211 errs++;
213 } else {
214 struct transport *transport =
215 transport_get(remote, NULL);
217 if (push_with_options(transport, flags))
218 errs++;
220 return !!errs;
223 static int option_parse_recurse_submodules(const struct option *opt,
224 const char *arg, int unset)
226 int *flags = opt->value;
227 if (arg) {
228 if (!strcmp(arg, "check"))
229 *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
230 else
231 die("bad %s argument: %s", opt->long_name, arg);
232 } else
233 die("option %s needs an argument (check)", opt->long_name);
235 return 0;
238 int cmd_push(int argc, const char **argv, const char *prefix)
240 int flags = 0;
241 int tags = 0;
242 int rc;
243 const char *repo = NULL; /* default repository */
244 struct option options[] = {
245 OPT__VERBOSITY(&verbosity),
246 OPT_STRING( 0 , "repo", &repo, "repository", "repository"),
247 OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
248 OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
249 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
250 OPT_BOOLEAN( 0, "delete", &deleterefs, "delete refs"),
251 OPT_BOOLEAN( 0 , "tags", &tags, "push tags (can't be used with --all or --mirror)"),
252 OPT_BIT('n' , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
253 OPT_BIT( 0, "porcelain", &flags, "machine-readable output", TRANSPORT_PUSH_PORCELAIN),
254 OPT_BIT('f', "force", &flags, "force updates", TRANSPORT_PUSH_FORCE),
255 { OPTION_CALLBACK, 0, "recurse-submodules", &flags, "check",
256 "controls recursive pushing of submodules",
257 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
258 OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
259 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
260 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
261 OPT_BIT('u', "set-upstream", &flags, "set upstream for git pull/status",
262 TRANSPORT_PUSH_SET_UPSTREAM),
263 OPT_BOOLEAN(0, "progress", &progress, "force progress reporting"),
264 OPT_END()
267 packet_trace_identity("push");
268 git_config(git_default_config, NULL);
269 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
271 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
272 die(_("--delete is incompatible with --all, --mirror and --tags"));
273 if (deleterefs && argc < 2)
274 die(_("--delete doesn't make sense without any refs"));
276 if (tags)
277 add_refspec("refs/tags/*");
279 if (argc > 0) {
280 repo = argv[0];
281 set_refspecs(argv + 1, argc - 1);
284 rc = do_push(repo, flags);
285 if (rc == -1)
286 usage_with_options(push_usage, options);
287 else
288 return rc;