]>
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 | ||
15e593e4 | 11 | static const char push_usage[] = "git-push [--all] [--tags] [-f | --force] <repository> [<refspec>...]"; |
755225de | 12 | |
bcc785f6 | 13 | static int all, tags, force, thin = 1, verbose; |
96f1e58f | 14 | static const char *execute; |
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 | ||
9e0ec82c | 35 | if (!strncmp(ref, "tags/", 5)) |
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 | ||
57 | static void set_refspecs(const char **refs, int nr) | |
58 | { | |
59 | if (nr) { | |
411fb8ba JH |
60 | int pass; |
61 | for (pass = 0; pass < 2; pass++) { | |
62 | /* pass 0 counts and allocates, pass 1 fills */ | |
63 | int i, cnt; | |
64 | for (i = cnt = 0; i < nr; i++) { | |
65 | if (!strcmp("tag", refs[i])) { | |
66 | int len; | |
67 | char *tag; | |
68 | if (nr <= ++i) | |
69 | die("tag <tag> shorthand without <tag>"); | |
70 | if (pass) { | |
71 | len = strlen(refs[i]) + 11; | |
72 | tag = xmalloc(len); | |
73 | strcpy(tag, "refs/tags/"); | |
74 | strcat(tag, refs[i]); | |
75 | refspec[cnt] = tag; | |
76 | } | |
77 | cnt++; | |
78 | continue; | |
79 | } | |
80 | if (pass) | |
81 | refspec[cnt] = refs[i]; | |
82 | cnt++; | |
83 | } | |
84 | if (!pass) { | |
85 | size_t bytes = cnt * sizeof(char *); | |
86 | refspec_nr = cnt; | |
87 | refspec = xrealloc(refspec, bytes); | |
88 | } | |
89 | } | |
755225de LT |
90 | } |
91 | expand_refspecs(); | |
92 | } | |
93 | ||
94 | static int get_remotes_uri(const char *repo, const char *uri[MAX_URI]) | |
95 | { | |
96 | int n = 0; | |
97 | FILE *f = fopen(git_path("remotes/%s", repo), "r"); | |
5edbcd8d | 98 | int has_explicit_refspec = refspec_nr || all || tags; |
755225de LT |
99 | |
100 | if (!f) | |
101 | return -1; | |
102 | while (fgets(buffer, BUF_SIZE, f)) { | |
5c477b97 | 103 | int is_refspec; |
755225de LT |
104 | char *s, *p; |
105 | ||
6fe5b7ff | 106 | if (!strncmp("URL:", buffer, 4)) { |
5c477b97 | 107 | is_refspec = 0; |
6fe5b7ff JH |
108 | s = buffer + 4; |
109 | } else if (!strncmp("Push:", buffer, 5)) { | |
5c477b97 | 110 | is_refspec = 1; |
6fe5b7ff | 111 | s = buffer + 5; |
5c477b97 | 112 | } else |
755225de | 113 | continue; |
755225de LT |
114 | |
115 | /* Remove whitespace at the head.. */ | |
116 | while (isspace(*s)) | |
117 | s++; | |
118 | if (!*s) | |
119 | continue; | |
120 | ||
121 | /* ..and at the end */ | |
122 | p = s + strlen(s); | |
123 | while (isspace(p[-1])) | |
124 | *--p = 0; | |
125 | ||
7aaf83da JH |
126 | if (!is_refspec) { |
127 | if (n < MAX_URI) | |
9befac47 | 128 | uri[n++] = xstrdup(s); |
7aaf83da | 129 | else |
82e5a82f | 130 | error("more than %d URL's specified, ignoring the rest", MAX_URI); |
7aaf83da JH |
131 | } |
132 | else if (is_refspec && !has_explicit_refspec) | |
9befac47 | 133 | add_refspec(xstrdup(s)); |
755225de LT |
134 | } |
135 | fclose(f); | |
136 | if (!n) | |
137 | die("remote '%s' has no URL", repo); | |
138 | return n; | |
139 | } | |
140 | ||
97d4df0b JS |
141 | static const char **config_uri; |
142 | static const char *config_repo; | |
143 | static int config_repo_len; | |
144 | static int config_current_uri; | |
145 | static int config_get_refspecs; | |
146 | ||
147 | static int get_remote_config(const char* key, const char* value) | |
148 | { | |
149 | if (!strncmp(key, "remote.", 7) && | |
150 | !strncmp(key + 7, config_repo, config_repo_len)) { | |
151 | if (!strcmp(key + 7 + config_repo_len, ".url")) { | |
152 | if (config_current_uri < MAX_URI) | |
9befac47 | 153 | config_uri[config_current_uri++] = xstrdup(value); |
97d4df0b JS |
154 | else |
155 | error("more than %d URL's specified, ignoring the rest", MAX_URI); | |
156 | } | |
157 | else if (config_get_refspecs && | |
158 | !strcmp(key + 7 + config_repo_len, ".push")) | |
9befac47 | 159 | add_refspec(xstrdup(value)); |
97d4df0b JS |
160 | } |
161 | return 0; | |
162 | } | |
163 | ||
164 | static int get_config_remotes_uri(const char *repo, const char *uri[MAX_URI]) | |
165 | { | |
166 | config_repo_len = strlen(repo); | |
167 | config_repo = repo; | |
168 | config_current_uri = 0; | |
169 | config_uri = uri; | |
5edbcd8d | 170 | config_get_refspecs = !(refspec_nr || all || tags); |
97d4df0b JS |
171 | |
172 | git_config(get_remote_config); | |
173 | return config_current_uri; | |
174 | } | |
175 | ||
755225de LT |
176 | static int get_branches_uri(const char *repo, const char *uri[MAX_URI]) |
177 | { | |
178 | const char *slash = strchr(repo, '/'); | |
179 | int n = slash ? slash - repo : 1000; | |
180 | FILE *f = fopen(git_path("branches/%.*s", n, repo), "r"); | |
181 | char *s, *p; | |
182 | int len; | |
183 | ||
184 | if (!f) | |
185 | return 0; | |
186 | s = fgets(buffer, BUF_SIZE, f); | |
187 | fclose(f); | |
188 | if (!s) | |
189 | return 0; | |
190 | while (isspace(*s)) | |
191 | s++; | |
192 | if (!*s) | |
193 | return 0; | |
194 | p = s + strlen(s); | |
195 | while (isspace(p[-1])) | |
196 | *--p = 0; | |
197 | len = p - s; | |
198 | if (slash) | |
199 | len += strlen(slash); | |
200 | p = xmalloc(len + 1); | |
201 | strcpy(p, s); | |
202 | if (slash) | |
203 | strcat(p, slash); | |
204 | uri[0] = p; | |
205 | return 1; | |
206 | } | |
207 | ||
7aaf83da JH |
208 | /* |
209 | * Read remotes and branches file, fill the push target URI | |
210 | * list. If there is no command line refspecs, read Push: lines | |
211 | * to set up the *refspec list as well. | |
212 | * return the number of push target URIs | |
213 | */ | |
214 | static int read_config(const char *repo, const char *uri[MAX_URI]) | |
755225de LT |
215 | { |
216 | int n; | |
217 | ||
218 | if (*repo != '/') { | |
219 | n = get_remotes_uri(repo, uri); | |
220 | if (n > 0) | |
221 | return n; | |
222 | ||
97d4df0b JS |
223 | n = get_config_remotes_uri(repo, uri); |
224 | if (n > 0) | |
225 | return n; | |
226 | ||
755225de LT |
227 | n = get_branches_uri(repo, uri); |
228 | if (n > 0) | |
229 | return n; | |
230 | } | |
231 | ||
232 | uri[0] = repo; | |
233 | return 1; | |
234 | } | |
235 | ||
236 | static int do_push(const char *repo) | |
237 | { | |
238 | const char *uri[MAX_URI]; | |
7aaf83da | 239 | int i, n; |
441c823e | 240 | int common_argc; |
755225de LT |
241 | const char **argv; |
242 | int argc; | |
243 | ||
7aaf83da | 244 | n = read_config(repo, uri); |
755225de LT |
245 | if (n <= 0) |
246 | die("bad repository '%s'", repo); | |
247 | ||
248 | argv = xmalloc((refspec_nr + 10) * sizeof(char *)); | |
249 | argv[0] = "dummy-send-pack"; | |
250 | argc = 1; | |
251 | if (all) | |
252 | argv[argc++] = "--all"; | |
253 | if (force) | |
254 | argv[argc++] = "--force"; | |
255 | if (execute) | |
256 | argv[argc++] = execute; | |
441c823e | 257 | common_argc = argc; |
755225de LT |
258 | |
259 | for (i = 0; i < n; i++) { | |
60b7f38e | 260 | int err; |
441c823e NH |
261 | int dest_argc = common_argc; |
262 | int dest_refspec_nr = refspec_nr; | |
263 | const char **dest_refspec = refspec; | |
755225de LT |
264 | const char *dest = uri[i]; |
265 | const char *sender = "git-send-pack"; | |
266 | if (!strncmp(dest, "http://", 7) || | |
267 | !strncmp(dest, "https://", 8)) | |
268 | sender = "git-http-push"; | |
441c823e NH |
269 | else if (thin) |
270 | argv[dest_argc++] = "--thin"; | |
755225de | 271 | argv[0] = sender; |
441c823e NH |
272 | argv[dest_argc++] = dest; |
273 | while (dest_refspec_nr--) | |
274 | argv[dest_argc++] = *dest_refspec++; | |
275 | argv[dest_argc] = NULL; | |
bcc785f6 LT |
276 | if (verbose) |
277 | fprintf(stderr, "Pushing to %s\n", dest); | |
60b7f38e PH |
278 | err = run_command_v(argc, argv); |
279 | if (!err) | |
755225de | 280 | continue; |
60b7f38e | 281 | switch (err) { |
755225de LT |
282 | case -ERR_RUN_COMMAND_FORK: |
283 | die("unable to fork for %s", sender); | |
284 | case -ERR_RUN_COMMAND_EXEC: | |
285 | die("unable to exec %s", sender); | |
286 | case -ERR_RUN_COMMAND_WAITPID: | |
287 | case -ERR_RUN_COMMAND_WAITPID_WRONG_PID: | |
288 | case -ERR_RUN_COMMAND_WAITPID_SIGNAL: | |
289 | case -ERR_RUN_COMMAND_WAITPID_NOEXIT: | |
290 | die("%s died with strange error", sender); | |
291 | default: | |
60b7f38e | 292 | return -err; |
755225de LT |
293 | } |
294 | } | |
295 | return 0; | |
296 | } | |
297 | ||
a633fca0 | 298 | int cmd_push(int argc, const char **argv, const char *prefix) |
755225de LT |
299 | { |
300 | int i; | |
a9486b02 | 301 | const char *repo = "origin"; /* default repository */ |
755225de LT |
302 | |
303 | for (i = 1; i < argc; i++) { | |
304 | const char *arg = argv[i]; | |
305 | ||
306 | if (arg[0] != '-') { | |
307 | repo = arg; | |
308 | i++; | |
309 | break; | |
310 | } | |
bcc785f6 LT |
311 | if (!strcmp(arg, "-v")) { |
312 | verbose=1; | |
313 | continue; | |
314 | } | |
315 | if (!strncmp(arg, "--repo=", 7)) { | |
316 | repo = arg+7; | |
317 | continue; | |
318 | } | |
755225de LT |
319 | if (!strcmp(arg, "--all")) { |
320 | all = 1; | |
321 | continue; | |
322 | } | |
323 | if (!strcmp(arg, "--tags")) { | |
324 | tags = 1; | |
325 | continue; | |
326 | } | |
8f615493 | 327 | if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) { |
755225de LT |
328 | force = 1; |
329 | continue; | |
330 | } | |
331 | if (!strcmp(arg, "--thin")) { | |
332 | thin = 1; | |
333 | continue; | |
334 | } | |
335 | if (!strcmp(arg, "--no-thin")) { | |
336 | thin = 0; | |
337 | continue; | |
338 | } | |
339 | if (!strncmp(arg, "--exec=", 7)) { | |
340 | execute = arg; | |
341 | continue; | |
342 | } | |
343 | usage(push_usage); | |
344 | } | |
345 | set_refspecs(argv + i, argc - i); | |
346 | return do_push(repo); | |
347 | } |