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