]>
Commit | Line | Data |
---|---|---|
61221472 | 1 | #include "cache.h" |
2a9c3fe8 | 2 | #include "commit.h" |
37fde874 | 3 | #include "tag.h" |
584c6cc9 | 4 | #include "refs.h" |
f3a3214e | 5 | #include "pkt-line.h" |
77cb17e9 | 6 | #include "exec_cmd.h" |
61221472 | 7 | |
2a245013 | 8 | static const char send_pack_usage[] = |
d23842fd | 9 | "git-send-pack [--all] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n" |
18bd8821 | 10 | " --all and explicit <ref> specification are mutually exclusive."; |
d23842fd | 11 | static const char *receivepack = "git-receive-pack"; |
96f1e58f DR |
12 | static int verbose; |
13 | static int send_all; | |
14 | static int force_update; | |
15 | static int use_thin_pack; | |
61221472 | 16 | |
c727fe2a | 17 | /* |
0ae5f98c | 18 | * Make a pack stream and spit it out into file descriptor fd |
c727fe2a | 19 | */ |
0ae5f98c | 20 | static int pack_objects(int fd, struct ref *refs) |
c727fe2a AW |
21 | { |
22 | int pipe_fd[2]; | |
0ae5f98c | 23 | pid_t pid; |
c727fe2a AW |
24 | |
25 | if (pipe(pipe_fd) < 0) | |
0ae5f98c JH |
26 | return error("send-pack: pipe failed"); |
27 | pid = fork(); | |
28 | if (!pid) { | |
29 | /* | |
30 | * The child becomes pack-objects --revs; we feed | |
31 | * the revision parameters to it via its stdin and | |
32 | * let its stdout go back to the other end. | |
c727fe2a | 33 | */ |
0ae5f98c JH |
34 | static const char *args[] = { |
35 | "pack-objects", | |
36 | "--all-progress", | |
37 | "--revs", | |
38 | "--stdout", | |
39 | NULL, | |
40 | NULL, | |
41 | }; | |
42 | if (use_thin_pack) | |
43 | args[4] = "--thin"; | |
c727fe2a AW |
44 | dup2(pipe_fd[0], 0); |
45 | dup2(fd, 1); | |
46 | close(pipe_fd[0]); | |
47 | close(pipe_fd[1]); | |
48 | close(fd); | |
0ae5f98c JH |
49 | execv_git_cmd(args); |
50 | die("git-pack-objects exec failed (%s)", strerror(errno)); | |
c727fe2a | 51 | } |
c727fe2a | 52 | |
0ae5f98c JH |
53 | /* |
54 | * We feed the pack-objects we just spawned with revision | |
55 | * parameters by writing to the pipe. | |
c727fe2a AW |
56 | */ |
57 | close(pipe_fd[0]); | |
58 | close(fd); | |
0ae5f98c | 59 | |
c727fe2a AW |
60 | while (refs) { |
61 | char buf[42]; | |
62 | ||
63 | if (!is_null_sha1(refs->old_sha1) && | |
64 | has_sha1_file(refs->old_sha1)) { | |
65 | memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40); | |
66 | buf[0] = '^'; | |
67 | buf[41] = '\n'; | |
e0814056 | 68 | if (!write_or_whine(pipe_fd[1], buf, 42, |
825cee7b AW |
69 | "send-pack: send refs")) |
70 | break; | |
c727fe2a AW |
71 | } |
72 | if (!is_null_sha1(refs->new_sha1)) { | |
73 | memcpy(buf, sha1_to_hex(refs->new_sha1), 40); | |
74 | buf[40] = '\n'; | |
e0814056 | 75 | if (!write_or_whine(pipe_fd[1], buf, 41, |
825cee7b AW |
76 | "send-pack: send refs")) |
77 | break; | |
c727fe2a AW |
78 | } |
79 | refs = refs->next; | |
80 | } | |
81 | close(pipe_fd[1]); | |
c727fe2a | 82 | |
0ae5f98c JH |
83 | for (;;) { |
84 | int status, code; | |
85 | pid_t waiting = waitpid(pid, &status, 0); | |
94fdb7aa | 86 | |
0ae5f98c JH |
87 | if (waiting < 0) { |
88 | if (errno == EINTR) | |
89 | continue; | |
90 | return error("waitpid failed (%s)", strerror(errno)); | |
91 | } | |
92 | if ((waiting != pid) || WIFSIGNALED(status) || | |
93 | !WIFEXITED(status)) | |
94 | return error("pack-objects died with strange error"); | |
95 | code = WEXITSTATUS(status); | |
96 | if (code) | |
97 | return -code; | |
98 | return 0; | |
94fdb7aa | 99 | } |
94fdb7aa | 100 | } |
e4b5c7ff | 101 | |
51b0fca0 JH |
102 | static void unmark_and_free(struct commit_list *list, unsigned int mark) |
103 | { | |
104 | while (list) { | |
105 | struct commit_list *temp = list; | |
106 | temp->item->object.flags &= ~mark; | |
107 | list = temp->next; | |
108 | free(temp); | |
109 | } | |
110 | } | |
111 | ||
37fde874 JH |
112 | static int ref_newer(const unsigned char *new_sha1, |
113 | const unsigned char *old_sha1) | |
584c6cc9 | 114 | { |
37fde874 JH |
115 | struct object *o; |
116 | struct commit *old, *new; | |
51b0fca0 JH |
117 | struct commit_list *list, *used; |
118 | int found = 0; | |
2a9c3fe8 | 119 | |
37fde874 JH |
120 | /* Both new and old must be commit-ish and new is descendant of |
121 | * old. Otherwise we require --force. | |
122 | */ | |
9534f40b | 123 | o = deref_tag(parse_object(old_sha1), NULL, 0); |
1974632c | 124 | if (!o || o->type != OBJ_COMMIT) |
584c6cc9 | 125 | return 0; |
37fde874 JH |
126 | old = (struct commit *) o; |
127 | ||
9534f40b | 128 | o = deref_tag(parse_object(new_sha1), NULL, 0); |
1974632c | 129 | if (!o || o->type != OBJ_COMMIT) |
2a9c3fe8 | 130 | return 0; |
37fde874 JH |
131 | new = (struct commit *) o; |
132 | ||
2a9c3fe8 LT |
133 | if (parse_commit(new) < 0) |
134 | return 0; | |
51b0fca0 JH |
135 | |
136 | used = list = NULL; | |
2a9c3fe8 | 137 | commit_list_insert(new, &list); |
bdf25142 LT |
138 | while (list) { |
139 | new = pop_most_recent_commit(&list, 1); | |
51b0fca0 JH |
140 | commit_list_insert(new, &used); |
141 | if (new == old) { | |
142 | found = 1; | |
143 | break; | |
144 | } | |
2a9c3fe8 | 145 | } |
51b0fca0 JH |
146 | unmark_and_free(list, 1); |
147 | unmark_and_free(used, 1); | |
148 | return found; | |
584c6cc9 LT |
149 | } |
150 | ||
f88395ac JH |
151 | static struct ref *local_refs, **local_tail; |
152 | static struct ref *remote_refs, **remote_tail; | |
584c6cc9 | 153 | |
8da19775 | 154 | static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
584c6cc9 LT |
155 | { |
156 | struct ref *ref; | |
f88395ac JH |
157 | int len = strlen(refname) + 1; |
158 | ref = xcalloc(1, sizeof(*ref) + len); | |
e702496e | 159 | hashcpy(ref->new_sha1, sha1); |
584c6cc9 | 160 | memcpy(ref->name, refname, len); |
f88395ac JH |
161 | *local_tail = ref; |
162 | local_tail = &ref->next; | |
584c6cc9 LT |
163 | return 0; |
164 | } | |
165 | ||
f88395ac JH |
166 | static void get_local_heads(void) |
167 | { | |
168 | local_tail = &local_refs; | |
cb5d709f | 169 | for_each_ref(one_local_ref, NULL); |
f88395ac JH |
170 | } |
171 | ||
cfee10a7 JH |
172 | static int receive_status(int in) |
173 | { | |
174 | char line[1000]; | |
175 | int ret = 0; | |
176 | int len = packet_read_line(in, line, sizeof(line)); | |
177 | if (len < 10 || memcmp(line, "unpack ", 7)) { | |
178 | fprintf(stderr, "did not receive status back\n"); | |
179 | return -1; | |
180 | } | |
181 | if (memcmp(line, "unpack ok\n", 10)) { | |
182 | fputs(line, stderr); | |
183 | ret = -1; | |
184 | } | |
185 | while (1) { | |
186 | len = packet_read_line(in, line, sizeof(line)); | |
187 | if (!len) | |
188 | break; | |
189 | if (len < 3 || | |
190 | (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) { | |
191 | fprintf(stderr, "protocol error: %s\n", line); | |
192 | ret = -1; | |
193 | break; | |
194 | } | |
195 | if (!memcmp(line, "ok", 2)) | |
196 | continue; | |
197 | fputs(line, stderr); | |
198 | ret = -1; | |
199 | } | |
200 | return ret; | |
201 | } | |
202 | ||
f88395ac | 203 | static int send_pack(int in, int out, int nr_refspec, char **refspec) |
61221472 | 204 | { |
7f8e9828 | 205 | struct ref *ref; |
584c6cc9 | 206 | int new_refs; |
ed24928e | 207 | int ret = 0; |
cfee10a7 | 208 | int ask_for_status_report = 0; |
d4f694ba | 209 | int allow_deleting_refs = 0; |
cfee10a7 | 210 | int expect_status_report = 0; |
7f8e9828 | 211 | |
f88395ac | 212 | /* No funny business with the matcher */ |
2718ff09 | 213 | remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL); |
f88395ac | 214 | get_local_heads(); |
584c6cc9 | 215 | |
cfee10a7 JH |
216 | /* Does the other end support the reporting? */ |
217 | if (server_supports("report-status")) | |
218 | ask_for_status_report = 1; | |
d4f694ba JH |
219 | if (server_supports("delete-refs")) |
220 | allow_deleting_refs = 1; | |
cfee10a7 | 221 | |
f88395ac JH |
222 | /* match them up */ |
223 | if (!remote_tail) | |
224 | remote_tail = &remote_refs; | |
225 | if (match_refs(local_refs, remote_refs, &remote_tail, | |
226 | nr_refspec, refspec, send_all)) | |
227 | return -1; | |
4c353e89 DB |
228 | |
229 | if (!remote_refs) { | |
230 | fprintf(stderr, "No refs in common and none specified; doing nothing.\n"); | |
231 | return 0; | |
232 | } | |
233 | ||
584c6cc9 | 234 | /* |
f88395ac | 235 | * Finally, tell the other end! |
584c6cc9 | 236 | */ |
f88395ac JH |
237 | new_refs = 0; |
238 | for (ref = remote_refs; ref; ref = ref->next) { | |
239 | char old_hex[60], *new_hex; | |
d4f694ba JH |
240 | int delete_ref; |
241 | ||
f88395ac | 242 | if (!ref->peer_ref) |
7f8e9828 | 243 | continue; |
d4f694ba JH |
244 | |
245 | delete_ref = is_null_sha1(ref->peer_ref->new_sha1); | |
246 | if (delete_ref && !allow_deleting_refs) { | |
247 | error("remote does not support deleting refs"); | |
248 | ret = -2; | |
249 | continue; | |
250 | } | |
251 | if (!delete_ref && | |
252 | !hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) { | |
41f93a2c LT |
253 | if (verbose) |
254 | fprintf(stderr, "'%s': up-to-date\n", ref->name); | |
37fde874 JH |
255 | continue; |
256 | } | |
257 | ||
258 | /* This part determines what can overwrite what. | |
259 | * The rules are: | |
260 | * | |
ff27adf3 JH |
261 | * (0) you can always use --force or +A:B notation to |
262 | * selectively force individual ref pairs. | |
37fde874 JH |
263 | * |
264 | * (1) if the old thing does not exist, it is OK. | |
265 | * | |
266 | * (2) if you do not have the old thing, you are not allowed | |
267 | * to overwrite it; you would not know what you are losing | |
268 | * otherwise. | |
269 | * | |
270 | * (3) if both new and old are commit-ish, and new is a | |
271 | * descendant of old, it is OK. | |
d4f694ba JH |
272 | * |
273 | * (4) regardless of all of the above, removing :B is | |
274 | * always allowed. | |
37fde874 JH |
275 | */ |
276 | ||
ff27adf3 | 277 | if (!force_update && |
d4f694ba | 278 | !delete_ref && |
4b4ee90e | 279 | !is_null_sha1(ref->old_sha1) && |
ff27adf3 | 280 | !ref->force) { |
69310a34 JH |
281 | if (!has_sha1_file(ref->old_sha1) || |
282 | !ref_newer(ref->peer_ref->new_sha1, | |
f88395ac | 283 | ref->old_sha1)) { |
69310a34 JH |
284 | /* We do not have the remote ref, or |
285 | * we know that the remote ref is not | |
286 | * an ancestor of what we are trying to | |
287 | * push. Either way this can be losing | |
288 | * commits at the remote end and likely | |
289 | * we were not up to date to begin with. | |
290 | */ | |
291 | error("remote '%s' is not a strict " | |
292 | "subset of local ref '%s'. " | |
293 | "maybe you are not up-to-date and " | |
294 | "need to pull first?", | |
295 | ref->name, | |
f88395ac | 296 | ref->peer_ref->name); |
ed24928e | 297 | ret = -2; |
f88395ac JH |
298 | continue; |
299 | } | |
e4b5c7ff | 300 | } |
e702496e | 301 | hashcpy(ref->new_sha1, ref->peer_ref->new_sha1); |
d4f694ba JH |
302 | if (!delete_ref) |
303 | new_refs++; | |
7f8e9828 LT |
304 | strcpy(old_hex, sha1_to_hex(ref->old_sha1)); |
305 | new_hex = sha1_to_hex(ref->new_sha1); | |
cfee10a7 JH |
306 | |
307 | if (ask_for_status_report) { | |
308 | packet_write(out, "%s %s %s%c%s", | |
309 | old_hex, new_hex, ref->name, 0, | |
310 | "report-status"); | |
311 | ask_for_status_report = 0; | |
312 | expect_status_report = 1; | |
313 | } | |
314 | else | |
315 | packet_write(out, "%s %s %s", | |
316 | old_hex, new_hex, ref->name); | |
d4f694ba JH |
317 | if (delete_ref) |
318 | fprintf(stderr, "deleting '%s'\n", ref->name); | |
319 | else { | |
320 | fprintf(stderr, "updating '%s'", ref->name); | |
321 | if (strcmp(ref->name, ref->peer_ref->name)) | |
322 | fprintf(stderr, " using '%s'", | |
323 | ref->peer_ref->name); | |
324 | fprintf(stderr, "\n from %s\n to %s\n", | |
325 | old_hex, new_hex); | |
326 | } | |
61221472 | 327 | } |
f88395ac | 328 | |
f3a3214e | 329 | packet_flush(out); |
584c6cc9 | 330 | if (new_refs) |
0ae5f98c | 331 | ret = pack_objects(out, remote_refs); |
61221472 | 332 | close(out); |
cfee10a7 JH |
333 | |
334 | if (expect_status_report) { | |
335 | if (receive_status(in)) | |
336 | ret = -4; | |
337 | } | |
338 | ||
339 | if (!new_refs && ret == 0) | |
340 | fprintf(stderr, "Everything up-to-date\n"); | |
ed24928e | 341 | return ret; |
61221472 LT |
342 | } |
343 | ||
37adac76 JH |
344 | static void verify_remote_names(int nr_heads, char **heads) |
345 | { | |
346 | int i; | |
347 | ||
348 | for (i = 0; i < nr_heads; i++) { | |
349 | const char *remote = strchr(heads[i], ':'); | |
350 | ||
351 | remote = remote ? (remote + 1) : heads[i]; | |
352 | switch (check_ref_format(remote)) { | |
353 | case 0: /* ok */ | |
354 | case -2: /* ok but a single level -- that is fine for | |
355 | * a match pattern. | |
356 | */ | |
357 | continue; | |
358 | } | |
359 | die("remote part of refspec is not a valid name in %s", | |
360 | heads[i]); | |
361 | } | |
362 | } | |
f88395ac | 363 | |
61221472 LT |
364 | int main(int argc, char **argv) |
365 | { | |
366 | int i, nr_heads = 0; | |
367 | char *dest = NULL; | |
368 | char **heads = NULL; | |
7f8e9828 LT |
369 | int fd[2], ret; |
370 | pid_t pid; | |
61221472 | 371 | |
5a327713 | 372 | setup_git_directory(); |
84a9b58c JH |
373 | git_config(git_default_config); |
374 | ||
61221472 | 375 | argv++; |
d089391c LT |
376 | for (i = 1; i < argc; i++, argv++) { |
377 | char *arg = *argv; | |
61221472 LT |
378 | |
379 | if (*arg == '-') { | |
d23842fd UKK |
380 | if (!strncmp(arg, "--receive-pack=", 15)) { |
381 | receivepack = arg + 15; | |
382 | continue; | |
383 | } | |
61221472 | 384 | if (!strncmp(arg, "--exec=", 7)) { |
d23842fd | 385 | receivepack = arg + 7; |
61221472 LT |
386 | continue; |
387 | } | |
d089391c LT |
388 | if (!strcmp(arg, "--all")) { |
389 | send_all = 1; | |
390 | continue; | |
391 | } | |
2a9c3fe8 LT |
392 | if (!strcmp(arg, "--force")) { |
393 | force_update = 1; | |
394 | continue; | |
395 | } | |
41f93a2c LT |
396 | if (!strcmp(arg, "--verbose")) { |
397 | verbose = 1; | |
398 | continue; | |
399 | } | |
2245be3e JH |
400 | if (!strcmp(arg, "--thin")) { |
401 | use_thin_pack = 1; | |
402 | continue; | |
403 | } | |
61221472 LT |
404 | usage(send_pack_usage); |
405 | } | |
d089391c LT |
406 | if (!dest) { |
407 | dest = arg; | |
408 | continue; | |
409 | } | |
61221472 | 410 | heads = argv; |
d089391c | 411 | nr_heads = argc - i; |
61221472 LT |
412 | break; |
413 | } | |
414 | if (!dest) | |
415 | usage(send_pack_usage); | |
0bc3cdfc JH |
416 | if (heads && send_all) |
417 | usage(send_pack_usage); | |
37adac76 JH |
418 | verify_remote_names(nr_heads, heads); |
419 | ||
d23842fd | 420 | pid = git_connect(fd, dest, receivepack); |
7f8e9828 | 421 | if (pid < 0) |
61221472 | 422 | return 1; |
d0efc8a7 | 423 | ret = send_pack(fd[0], fd[1], nr_heads, heads); |
7f8e9828 LT |
424 | close(fd[0]); |
425 | close(fd[1]); | |
8a5dbef8 FBH |
426 | ret |= finish_connect(pid); |
427 | return !!ret; | |
61221472 | 428 | } |