send-pack --keep: do not explode into loose objects on the receiving end.
[git.git] / send-pack.c
blob54d218c03b68d0b5bf1c2304b7b5bee2d0bd98a6
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "refs.h"
5 #include "pkt-line.h"
6 #include "exec_cmd.h"
8 static const char send_pack_usage[] =
9 "git-send-pack [--all] [--keep] [--exec=git-receive-pack] <remote> [<head>...]\n"
10 " --all and explicit <head> specification are mutually exclusive.";
11 static const char *exec = "git-receive-pack";
12 static int verbose;
13 static int send_all;
14 static int force_update;
15 static int use_thin_pack;
16 static int keep_pack;
18 static int is_zero_sha1(const unsigned char *sha1)
20 int i;
22 for (i = 0; i < 20; i++) {
23 if (*sha1++)
24 return 0;
26 return 1;
29 static void exec_pack_objects(void)
31 static const char *args[] = {
32 "pack-objects",
33 "--stdout",
34 NULL
36 execv_git_cmd(args);
37 die("git-pack-objects exec failed (%s)", strerror(errno));
40 static void exec_rev_list(struct ref *refs)
42 static const char *args[4];
43 int i = 0;
45 args[i++] = "rev-list"; /* 0 */
46 if (use_thin_pack) /* 1 */
47 args[i++] = "--objects-edge";
48 else
49 args[i++] = "--objects";
51 args[i++] = "--stdin";
53 args[i] = NULL;
54 execv_git_cmd(args);
55 die("git-rev-list exec failed (%s)", strerror(errno));
59 * Run "rev-list --stdin | pack-objects" pipe.
61 static void rev_list(int fd, struct ref *refs)
63 int pipe_fd[2];
64 pid_t pack_objects_pid;
66 if (pipe(pipe_fd) < 0)
67 die("rev-list setup: pipe failed");
68 pack_objects_pid = fork();
69 if (!pack_objects_pid) {
70 /* The child becomes pack-objects; reads from pipe
71 * and writes to the original fd
73 dup2(pipe_fd[0], 0);
74 dup2(fd, 1);
75 close(pipe_fd[0]);
76 close(pipe_fd[1]);
77 close(fd);
78 exec_pack_objects();
79 die("pack-objects setup failed");
81 if (pack_objects_pid < 0)
82 die("pack-objects fork failed");
84 /* We become rev-list --stdin; output goes to pipe. */
85 dup2(pipe_fd[1], 1);
86 close(pipe_fd[0]);
87 close(pipe_fd[1]);
88 close(fd);
89 exec_rev_list(refs);
93 * Create "rev-list --stdin | pack-objects" pipe and feed
94 * the refs into the pipeline.
96 static void rev_list_generate(int fd, struct ref *refs)
98 int pipe_fd[2];
99 pid_t rev_list_generate_pid;
101 if (pipe(pipe_fd) < 0)
102 die("rev-list-generate setup: pipe failed");
103 rev_list_generate_pid = fork();
104 if (!rev_list_generate_pid) {
105 /* The child becomes the "rev-list | pack-objects"
106 * pipeline. It takes input from us, and its output
107 * goes to fd.
109 dup2(pipe_fd[0], 0);
110 dup2(fd, 1);
111 close(pipe_fd[0]);
112 close(pipe_fd[1]);
113 close(fd);
114 rev_list(fd, refs);
115 die("rev-list setup failed");
117 if (rev_list_generate_pid < 0)
118 die("rev-list-generate fork failed");
120 /* We feed the rev parameters to them. We do not write into
121 * fd nor read from the pipe.
123 close(pipe_fd[0]);
124 close(fd);
125 while (refs) {
126 char buf[42];
128 if (!is_null_sha1(refs->old_sha1) &&
129 has_sha1_file(refs->old_sha1)) {
130 memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40);
131 buf[0] = '^';
132 buf[41] = '\n';
133 write(pipe_fd[1], buf, 42);
135 if (!is_null_sha1(refs->new_sha1)) {
136 memcpy(buf, sha1_to_hex(refs->new_sha1), 40);
137 buf[40] = '\n';
138 write(pipe_fd[1], buf, 41);
140 refs = refs->next;
142 close(pipe_fd[1]);
143 // waitpid(rev_list_generate_pid);
144 exit(0);
148 * Make a pack stream and spit it out into file descriptor fd
150 static void pack_objects(int fd, struct ref *refs)
152 pid_t rev_list_pid;
154 rev_list_pid = fork();
155 if (!rev_list_pid) {
156 rev_list_generate(fd, refs);
157 die("rev-list setup failed");
159 if (rev_list_pid < 0)
160 die("rev-list fork failed");
162 * We don't wait for the rev-list pipeline in the parent:
163 * we end up waiting for the other end instead
167 static void unmark_and_free(struct commit_list *list, unsigned int mark)
169 while (list) {
170 struct commit_list *temp = list;
171 temp->item->object.flags &= ~mark;
172 list = temp->next;
173 free(temp);
177 static int ref_newer(const unsigned char *new_sha1,
178 const unsigned char *old_sha1)
180 struct object *o;
181 struct commit *old, *new;
182 struct commit_list *list, *used;
183 int found = 0;
185 /* Both new and old must be commit-ish and new is descendant of
186 * old. Otherwise we require --force.
188 o = deref_tag(parse_object(old_sha1), NULL, 0);
189 if (!o || o->type != OBJ_COMMIT)
190 return 0;
191 old = (struct commit *) o;
193 o = deref_tag(parse_object(new_sha1), NULL, 0);
194 if (!o || o->type != OBJ_COMMIT)
195 return 0;
196 new = (struct commit *) o;
198 if (parse_commit(new) < 0)
199 return 0;
201 used = list = NULL;
202 commit_list_insert(new, &list);
203 while (list) {
204 new = pop_most_recent_commit(&list, 1);
205 commit_list_insert(new, &used);
206 if (new == old) {
207 found = 1;
208 break;
211 unmark_and_free(list, 1);
212 unmark_and_free(used, 1);
213 return found;
216 static struct ref *local_refs, **local_tail;
217 static struct ref *remote_refs, **remote_tail;
219 static int one_local_ref(const char *refname, const unsigned char *sha1)
221 struct ref *ref;
222 int len = strlen(refname) + 1;
223 ref = xcalloc(1, sizeof(*ref) + len);
224 hashcpy(ref->new_sha1, sha1);
225 memcpy(ref->name, refname, len);
226 *local_tail = ref;
227 local_tail = &ref->next;
228 return 0;
231 static void get_local_heads(void)
233 local_tail = &local_refs;
234 for_each_ref(one_local_ref);
237 static int receive_status(int in)
239 char line[1000];
240 int ret = 0;
241 int len = packet_read_line(in, line, sizeof(line));
242 if (len < 10 || memcmp(line, "unpack ", 7)) {
243 fprintf(stderr, "did not receive status back\n");
244 return -1;
246 if (memcmp(line, "unpack ok\n", 10)) {
247 fputs(line, stderr);
248 ret = -1;
250 while (1) {
251 len = packet_read_line(in, line, sizeof(line));
252 if (!len)
253 break;
254 if (len < 3 ||
255 (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
256 fprintf(stderr, "protocol error: %s\n", line);
257 ret = -1;
258 break;
260 if (!memcmp(line, "ok", 2))
261 continue;
262 fputs(line, stderr);
263 ret = -1;
265 return ret;
268 static int send_pack(int in, int out, int nr_refspec, char **refspec)
270 struct ref *ref;
271 int new_refs;
272 int ret = 0;
273 int ask_for_status_report = 0;
274 int ask_to_keep_pack = 0;
275 int expect_status_report = 0;
277 /* No funny business with the matcher */
278 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL);
279 get_local_heads();
281 /* Does the other end support the reporting? */
282 if (server_supports("report-status"))
283 ask_for_status_report = 1;
284 if (server_supports("keep-pack") && keep_pack)
285 ask_to_keep_pack = 1;
287 /* match them up */
288 if (!remote_tail)
289 remote_tail = &remote_refs;
290 if (match_refs(local_refs, remote_refs, &remote_tail,
291 nr_refspec, refspec, send_all))
292 return -1;
294 if (!remote_refs) {
295 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
296 return 0;
300 * Finally, tell the other end!
302 new_refs = 0;
303 for (ref = remote_refs; ref; ref = ref->next) {
304 char old_hex[60], *new_hex;
305 if (!ref->peer_ref)
306 continue;
307 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
308 if (verbose)
309 fprintf(stderr, "'%s': up-to-date\n", ref->name);
310 continue;
313 /* This part determines what can overwrite what.
314 * The rules are:
316 * (0) you can always use --force or +A:B notation to
317 * selectively force individual ref pairs.
319 * (1) if the old thing does not exist, it is OK.
321 * (2) if you do not have the old thing, you are not allowed
322 * to overwrite it; you would not know what you are losing
323 * otherwise.
325 * (3) if both new and old are commit-ish, and new is a
326 * descendant of old, it is OK.
329 if (!force_update &&
330 !is_zero_sha1(ref->old_sha1) &&
331 !ref->force) {
332 if (!has_sha1_file(ref->old_sha1) ||
333 !ref_newer(ref->peer_ref->new_sha1,
334 ref->old_sha1)) {
335 /* We do not have the remote ref, or
336 * we know that the remote ref is not
337 * an ancestor of what we are trying to
338 * push. Either way this can be losing
339 * commits at the remote end and likely
340 * we were not up to date to begin with.
342 error("remote '%s' is not a strict "
343 "subset of local ref '%s'. "
344 "maybe you are not up-to-date and "
345 "need to pull first?",
346 ref->name,
347 ref->peer_ref->name);
348 ret = -2;
349 continue;
352 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
353 if (is_zero_sha1(ref->new_sha1)) {
354 error("cannot happen anymore");
355 ret = -3;
356 continue;
358 new_refs++;
359 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
360 new_hex = sha1_to_hex(ref->new_sha1);
362 if (ask_for_status_report || ask_to_keep_pack) {
363 packet_write(out, "%s %s %s%c%s%s",
364 old_hex, new_hex, ref->name, 0,
365 ask_for_status_report
366 ? " report-status" : "",
367 ask_to_keep_pack
368 ? " keep-pack" : "");
369 if (ask_for_status_report)
370 expect_status_report = 1;
371 ask_for_status_report = 0;
372 ask_to_keep_pack = 0;
374 else
375 packet_write(out, "%s %s %s",
376 old_hex, new_hex, ref->name);
377 fprintf(stderr, "updating '%s'", ref->name);
378 if (strcmp(ref->name, ref->peer_ref->name))
379 fprintf(stderr, " using '%s'", ref->peer_ref->name);
380 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
383 packet_flush(out);
384 if (new_refs)
385 pack_objects(out, remote_refs);
386 close(out);
388 if (expect_status_report) {
389 if (receive_status(in))
390 ret = -4;
393 if (!new_refs && ret == 0)
394 fprintf(stderr, "Everything up-to-date\n");
395 return ret;
399 int main(int argc, char **argv)
401 int i, nr_heads = 0;
402 char *dest = NULL;
403 char **heads = NULL;
404 int fd[2], ret;
405 pid_t pid;
407 setup_git_directory();
408 git_config(git_default_config);
410 argv++;
411 for (i = 1; i < argc; i++, argv++) {
412 char *arg = *argv;
414 if (*arg == '-') {
415 if (!strncmp(arg, "--exec=", 7)) {
416 exec = arg + 7;
417 continue;
419 if (!strcmp(arg, "--all")) {
420 send_all = 1;
421 continue;
423 if (!strcmp(arg, "--force")) {
424 force_update = 1;
425 continue;
427 if (!strcmp(arg, "--verbose")) {
428 verbose = 1;
429 continue;
431 if (!strcmp(arg, "--keep")) {
432 keep_pack = 1;
433 continue;
435 if (!strcmp(arg, "--thin")) {
436 use_thin_pack = 1;
437 continue;
439 usage(send_pack_usage);
441 if (!dest) {
442 dest = arg;
443 continue;
445 heads = argv;
446 nr_heads = argc - i;
447 break;
449 if (!dest)
450 usage(send_pack_usage);
451 if (heads && send_all)
452 usage(send_pack_usage);
453 pid = git_connect(fd, dest, exec);
454 if (pid < 0)
455 return 1;
456 ret = send_pack(fd[0], fd[1], nr_heads, heads);
457 close(fd[0]);
458 close(fd[1]);
459 ret |= finish_connect(pid);
460 return !!ret;