]>
Commit | Line | Data |
---|---|---|
755225de LT |
1 | /* |
2 | * "git push" | |
3 | */ | |
4 | #include "cache.h" | |
5 | #include "refs.h" | |
6 | #include "run-command.h" | |
7 | #include "builtin.h" | |
8 | ||
9 | #define MAX_URI (16) | |
10 | ||
d23842fd | 11 | static const char push_usage[] = "git-push [--all] [--tags] [--receive-pack=<git-receive-pack>] [--repo=all] [-f | --force] [-v] [<repository> <refspec>...]"; |
755225de | 12 | |
bcc785f6 | 13 | static int all, tags, force, thin = 1, verbose; |
d23842fd | 14 | static const char *receivepack; |
755225de LT |
15 | |
16 | #define BUF_SIZE (2084) | |
17 | static char buffer[BUF_SIZE]; | |
18 | ||
96f1e58f DR |
19 | static const char **refspec; |
20 | static int refspec_nr; | |
755225de LT |
21 | |
22 | static void add_refspec(const char *ref) | |
23 | { | |
24 | int nr = refspec_nr + 1; | |
25 | refspec = xrealloc(refspec, nr * sizeof(char *)); | |
26 | refspec[nr-1] = ref; | |
27 | refspec_nr = nr; | |
28 | } | |
29 | ||
8da19775 | 30 | static int expand_one_ref(const char *ref, const unsigned char *sha1, int flag, void *cb_data) |
755225de LT |
31 | { |
32 | /* Ignore the "refs/" at the beginning of the refname */ | |
33 | ref += 5; | |
34 | ||
cc44c765 | 35 | if (!prefixcmp(ref, "tags/")) |
9befac47 | 36 | add_refspec(xstrdup(ref)); |
755225de LT |
37 | return 0; |
38 | } | |
39 | ||
40 | static void expand_refspecs(void) | |
41 | { | |
42 | if (all) { | |
43 | if (refspec_nr) | |
44 | die("cannot mix '--all' and a refspec"); | |
45 | ||
46 | /* | |
47 | * No need to expand "--all" - we'll just use | |
48 | * the "--all" flag to send-pack | |
49 | */ | |
50 | return; | |
51 | } | |
52 | if (!tags) | |
53 | return; | |
cb5d709f | 54 | for_each_ref(expand_one_ref, NULL); |
755225de LT |
55 | } |
56 | ||
d46ae3f0 JH |
57 | struct wildcard_cb { |
58 | const char *from_prefix; | |
59 | int from_prefix_len; | |
60 | const char *to_prefix; | |
61 | int to_prefix_len; | |
62 | int force; | |
63 | }; | |
64 | ||
65 | static int expand_wildcard_ref(const char *ref, const unsigned char *sha1, int flag, void *cb_data) | |
66 | { | |
67 | struct wildcard_cb *cb = cb_data; | |
68 | int len = strlen(ref); | |
69 | char *expanded, *newref; | |
70 | ||
71 | if (len < cb->from_prefix_len || | |
72 | memcmp(cb->from_prefix, ref, cb->from_prefix_len)) | |
73 | return 0; | |
74 | expanded = xmalloc(len * 2 + cb->force + | |
75 | (cb->to_prefix_len - cb->from_prefix_len) + 2); | |
76 | newref = expanded + cb->force; | |
77 | if (cb->force) | |
78 | expanded[0] = '+'; | |
79 | memcpy(newref, ref, len); | |
80 | newref[len] = ':'; | |
81 | memcpy(newref + len + 1, cb->to_prefix, cb->to_prefix_len); | |
82 | strcpy(newref + len + 1 + cb->to_prefix_len, | |
83 | ref + cb->from_prefix_len); | |
84 | add_refspec(expanded); | |
85 | return 0; | |
86 | } | |
87 | ||
88 | static int wildcard_ref(const char *ref) | |
89 | { | |
90 | int len; | |
91 | const char *colon; | |
92 | struct wildcard_cb cb; | |
93 | ||
94 | memset(&cb, 0, sizeof(cb)); | |
95 | if (ref[0] == '+') { | |
96 | cb.force = 1; | |
97 | ref++; | |
98 | } | |
99 | len = strlen(ref); | |
100 | colon = strchr(ref, ':'); | |
101 | if (! (colon && ref < colon && | |
102 | colon[-2] == '/' && colon[-1] == '*' && | |
103 | /* "<mine>/<asterisk>:<yours>/<asterisk>" is at least 7 bytes */ | |
104 | 7 <= len && | |
105 | ref[len-2] == '/' && ref[len-1] == '*') ) | |
106 | return 0 ; | |
107 | cb.from_prefix = ref; | |
108 | cb.from_prefix_len = colon - ref - 1; | |
109 | cb.to_prefix = colon + 1; | |
110 | cb.to_prefix_len = len - (colon - ref) - 2; | |
111 | for_each_ref(expand_wildcard_ref, &cb); | |
112 | return 1; | |
113 | } | |
114 | ||
755225de LT |
115 | static void set_refspecs(const char **refs, int nr) |
116 | { | |
117 | if (nr) { | |
d46ae3f0 JH |
118 | int i; |
119 | for (i = 0; i < nr; i++) { | |
120 | const char *ref = refs[i]; | |
121 | if (!strcmp("tag", ref)) { | |
122 | char *tag; | |
123 | int len; | |
124 | if (nr <= ++i) | |
125 | die("tag shorthand without <tag>"); | |
126 | len = strlen(refs[i]) + 11; | |
127 | tag = xmalloc(len); | |
128 | strcpy(tag, "refs/tags/"); | |
129 | strcat(tag, refs[i]); | |
130 | ref = tag; | |
411fb8ba | 131 | } |
d46ae3f0 JH |
132 | else if (wildcard_ref(ref)) |
133 | continue; | |
134 | add_refspec(ref); | |
411fb8ba | 135 | } |
755225de LT |
136 | } |
137 | expand_refspecs(); | |
138 | } | |
139 | ||
140 | static int get_remotes_uri(const char *repo, const char *uri[MAX_URI]) | |
141 | { | |
142 | int n = 0; | |
143 | FILE *f = fopen(git_path("remotes/%s", repo), "r"); | |
5edbcd8d | 144 | int has_explicit_refspec = refspec_nr || all || tags; |
755225de LT |
145 | |
146 | if (!f) | |
147 | return -1; | |
148 | while (fgets(buffer, BUF_SIZE, f)) { | |
5c477b97 | 149 | int is_refspec; |
755225de LT |
150 | char *s, *p; |
151 | ||
599065a3 | 152 | if (!prefixcmp(buffer, "URL:")) { |
5c477b97 | 153 | is_refspec = 0; |
6fe5b7ff | 154 | s = buffer + 4; |
599065a3 | 155 | } else if (!prefixcmp(buffer, "Push:")) { |
5c477b97 | 156 | is_refspec = 1; |
6fe5b7ff | 157 | s = buffer + 5; |
5c477b97 | 158 | } else |
755225de | 159 | continue; |
755225de LT |
160 | |
161 | /* Remove whitespace at the head.. */ | |
162 | while (isspace(*s)) | |
163 | s++; | |
164 | if (!*s) | |
165 | continue; | |
166 | ||
167 | /* ..and at the end */ | |
168 | p = s + strlen(s); | |
169 | while (isspace(p[-1])) | |
170 | *--p = 0; | |
171 | ||
7aaf83da JH |
172 | if (!is_refspec) { |
173 | if (n < MAX_URI) | |
9befac47 | 174 | uri[n++] = xstrdup(s); |
7aaf83da | 175 | else |
82e5a82f | 176 | error("more than %d URL's specified, ignoring the rest", MAX_URI); |
7aaf83da | 177 | } |
d46ae3f0 JH |
178 | else if (is_refspec && !has_explicit_refspec) { |
179 | if (!wildcard_ref(s)) | |
180 | add_refspec(xstrdup(s)); | |
181 | } | |
755225de LT |
182 | } |
183 | fclose(f); | |
184 | if (!n) | |
185 | die("remote '%s' has no URL", repo); | |
186 | return n; | |
187 | } | |
188 | ||
97d4df0b JS |
189 | static const char **config_uri; |
190 | static const char *config_repo; | |
191 | static int config_repo_len; | |
192 | static int config_current_uri; | |
193 | static int config_get_refspecs; | |
060aafc1 | 194 | static int config_get_receivepack; |
97d4df0b JS |
195 | |
196 | static int get_remote_config(const char* key, const char* value) | |
197 | { | |
cc44c765 | 198 | if (!prefixcmp(key, "remote.") && |
97d4df0b JS |
199 | !strncmp(key + 7, config_repo, config_repo_len)) { |
200 | if (!strcmp(key + 7 + config_repo_len, ".url")) { | |
201 | if (config_current_uri < MAX_URI) | |
9befac47 | 202 | config_uri[config_current_uri++] = xstrdup(value); |
97d4df0b JS |
203 | else |
204 | error("more than %d URL's specified, ignoring the rest", MAX_URI); | |
205 | } | |
206 | else if (config_get_refspecs && | |
d46ae3f0 JH |
207 | !strcmp(key + 7 + config_repo_len, ".push")) { |
208 | if (!wildcard_ref(value)) | |
209 | add_refspec(xstrdup(value)); | |
210 | } | |
060aafc1 UKK |
211 | else if (config_get_receivepack && |
212 | !strcmp(key + 7 + config_repo_len, ".receivepack")) { | |
d23842fd UKK |
213 | if (!receivepack) { |
214 | char *rp = xmalloc(strlen(value) + 16); | |
215 | sprintf(rp, "--receive-pack=%s", value); | |
216 | receivepack = rp; | |
060aafc1 UKK |
217 | } else |
218 | error("more than one receivepack given, using the first"); | |
219 | } | |
97d4df0b JS |
220 | } |
221 | return 0; | |
222 | } | |
223 | ||
224 | static int get_config_remotes_uri(const char *repo, const char *uri[MAX_URI]) | |
225 | { | |
226 | config_repo_len = strlen(repo); | |
227 | config_repo = repo; | |
228 | config_current_uri = 0; | |
229 | config_uri = uri; | |
5edbcd8d | 230 | config_get_refspecs = !(refspec_nr || all || tags); |
d23842fd | 231 | config_get_receivepack = (receivepack == NULL); |
97d4df0b JS |
232 | |
233 | git_config(get_remote_config); | |
234 | return config_current_uri; | |
235 | } | |
236 | ||
755225de LT |
237 | static int get_branches_uri(const char *repo, const char *uri[MAX_URI]) |
238 | { | |
239 | const char *slash = strchr(repo, '/'); | |
240 | int n = slash ? slash - repo : 1000; | |
241 | FILE *f = fopen(git_path("branches/%.*s", n, repo), "r"); | |
242 | char *s, *p; | |
243 | int len; | |
244 | ||
245 | if (!f) | |
246 | return 0; | |
247 | s = fgets(buffer, BUF_SIZE, f); | |
248 | fclose(f); | |
249 | if (!s) | |
250 | return 0; | |
251 | while (isspace(*s)) | |
252 | s++; | |
253 | if (!*s) | |
254 | return 0; | |
255 | p = s + strlen(s); | |
256 | while (isspace(p[-1])) | |
257 | *--p = 0; | |
258 | len = p - s; | |
259 | if (slash) | |
260 | len += strlen(slash); | |
261 | p = xmalloc(len + 1); | |
262 | strcpy(p, s); | |
263 | if (slash) | |
264 | strcat(p, slash); | |
265 | uri[0] = p; | |
266 | return 1; | |
267 | } | |
268 | ||
7aaf83da JH |
269 | /* |
270 | * Read remotes and branches file, fill the push target URI | |
271 | * list. If there is no command line refspecs, read Push: lines | |
272 | * to set up the *refspec list as well. | |
273 | * return the number of push target URIs | |
274 | */ | |
275 | static int read_config(const char *repo, const char *uri[MAX_URI]) | |
755225de LT |
276 | { |
277 | int n; | |
278 | ||
279 | if (*repo != '/') { | |
280 | n = get_remotes_uri(repo, uri); | |
281 | if (n > 0) | |
282 | return n; | |
283 | ||
97d4df0b JS |
284 | n = get_config_remotes_uri(repo, uri); |
285 | if (n > 0) | |
286 | return n; | |
287 | ||
755225de LT |
288 | n = get_branches_uri(repo, uri); |
289 | if (n > 0) | |
290 | return n; | |
291 | } | |
292 | ||
293 | uri[0] = repo; | |
294 | return 1; | |
295 | } | |
296 | ||
297 | static int do_push(const char *repo) | |
298 | { | |
299 | const char *uri[MAX_URI]; | |
7aaf83da | 300 | int i, n; |
441c823e | 301 | int common_argc; |
755225de LT |
302 | const char **argv; |
303 | int argc; | |
304 | ||
7aaf83da | 305 | n = read_config(repo, uri); |
755225de LT |
306 | if (n <= 0) |
307 | die("bad repository '%s'", repo); | |
308 | ||
309 | argv = xmalloc((refspec_nr + 10) * sizeof(char *)); | |
310 | argv[0] = "dummy-send-pack"; | |
311 | argc = 1; | |
312 | if (all) | |
313 | argv[argc++] = "--all"; | |
314 | if (force) | |
315 | argv[argc++] = "--force"; | |
d23842fd UKK |
316 | if (receivepack) |
317 | argv[argc++] = receivepack; | |
441c823e | 318 | common_argc = argc; |
755225de LT |
319 | |
320 | for (i = 0; i < n; i++) { | |
60b7f38e | 321 | int err; |
441c823e NH |
322 | int dest_argc = common_argc; |
323 | int dest_refspec_nr = refspec_nr; | |
324 | const char **dest_refspec = refspec; | |
755225de | 325 | const char *dest = uri[i]; |
df91ba36 | 326 | const char *sender = "send-pack"; |
cc44c765 JH |
327 | if (!prefixcmp(dest, "http://") || |
328 | !prefixcmp(dest, "https://")) | |
df91ba36 | 329 | sender = "http-push"; |
441c823e NH |
330 | else if (thin) |
331 | argv[dest_argc++] = "--thin"; | |
755225de | 332 | argv[0] = sender; |
441c823e NH |
333 | argv[dest_argc++] = dest; |
334 | while (dest_refspec_nr--) | |
335 | argv[dest_argc++] = *dest_refspec++; | |
336 | argv[dest_argc] = NULL; | |
bcc785f6 LT |
337 | if (verbose) |
338 | fprintf(stderr, "Pushing to %s\n", dest); | |
df91ba36 | 339 | err = run_command_v_opt(argv, RUN_GIT_CMD); |
60b7f38e | 340 | if (!err) |
755225de | 341 | continue; |
60b7f38e | 342 | switch (err) { |
755225de LT |
343 | case -ERR_RUN_COMMAND_FORK: |
344 | die("unable to fork for %s", sender); | |
345 | case -ERR_RUN_COMMAND_EXEC: | |
346 | die("unable to exec %s", sender); | |
347 | case -ERR_RUN_COMMAND_WAITPID: | |
348 | case -ERR_RUN_COMMAND_WAITPID_WRONG_PID: | |
349 | case -ERR_RUN_COMMAND_WAITPID_SIGNAL: | |
350 | case -ERR_RUN_COMMAND_WAITPID_NOEXIT: | |
351 | die("%s died with strange error", sender); | |
352 | default: | |
60b7f38e | 353 | return -err; |
755225de LT |
354 | } |
355 | } | |
356 | return 0; | |
357 | } | |
358 | ||
a633fca0 | 359 | int cmd_push(int argc, const char **argv, const char *prefix) |
755225de LT |
360 | { |
361 | int i; | |
a9486b02 | 362 | const char *repo = "origin"; /* default repository */ |
755225de LT |
363 | |
364 | for (i = 1; i < argc; i++) { | |
365 | const char *arg = argv[i]; | |
366 | ||
367 | if (arg[0] != '-') { | |
368 | repo = arg; | |
369 | i++; | |
370 | break; | |
371 | } | |
bcc785f6 LT |
372 | if (!strcmp(arg, "-v")) { |
373 | verbose=1; | |
374 | continue; | |
375 | } | |
cc44c765 | 376 | if (!prefixcmp(arg, "--repo=")) { |
bcc785f6 LT |
377 | repo = arg+7; |
378 | continue; | |
379 | } | |
755225de LT |
380 | if (!strcmp(arg, "--all")) { |
381 | all = 1; | |
382 | continue; | |
383 | } | |
384 | if (!strcmp(arg, "--tags")) { | |
385 | tags = 1; | |
386 | continue; | |
387 | } | |
8f615493 | 388 | if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) { |
755225de LT |
389 | force = 1; |
390 | continue; | |
391 | } | |
392 | if (!strcmp(arg, "--thin")) { | |
393 | thin = 1; | |
394 | continue; | |
395 | } | |
396 | if (!strcmp(arg, "--no-thin")) { | |
397 | thin = 0; | |
398 | continue; | |
399 | } | |
cc44c765 | 400 | if (!prefixcmp(arg, "--receive-pack=")) { |
d23842fd UKK |
401 | receivepack = arg; |
402 | continue; | |
403 | } | |
cc44c765 | 404 | if (!prefixcmp(arg, "--exec=")) { |
d23842fd | 405 | receivepack = arg; |
755225de LT |
406 | continue; |
407 | } | |
408 | usage(push_usage); | |
409 | } | |
410 | set_refspecs(argv + i, argc - i); | |
411 | return do_push(repo); | |
412 | } |