8 static const char send_pack_usage[] =
9 "git-send-pack [--all] [--exec=git-receive-pack] <remote> [<head>...]\n"
10 " --all and explicit <head> specification are mutually exclusive.";
11 static const char *exec = "git-receive-pack";
14 static int force_update;
15 static int use_thin_pack;
17 static int is_zero_sha1(const unsigned char *sha1)
21 for (i = 0; i < 20; i++) {
28 static void exec_pack_objects(void)
30 static const char *args[] = {
36 die("git-pack-objects exec failed (%s)", strerror(errno));
39 static void exec_rev_list(struct ref *refs)
41 static const char *args[4];
44 args[i++] = "rev-list"; /* 0 */
45 if (use_thin_pack) /* 1 */
46 args[i++] = "--objects-edge";
48 args[i++] = "--objects";
50 args[i++] = "--stdin";
54 die("git-rev-list exec failed (%s)", strerror(errno));
58 * Run "rev-list --stdin | pack-objects" pipe.
60 static void rev_list(int fd, struct ref *refs)
63 pid_t pack_objects_pid;
65 if (pipe(pipe_fd) < 0)
66 die("rev-list setup: pipe failed");
67 pack_objects_pid = fork();
68 if (!pack_objects_pid) {
69 /* The child becomes pack-objects; reads from pipe
70 * and writes to the original fd
78 die("pack-objects setup failed");
80 if (pack_objects_pid < 0)
81 die("pack-objects fork failed");
83 /* We become rev-list --stdin; output goes to pipe. */
92 * Create "rev-list --stdin | pack-objects" pipe and feed
93 * the refs into the pipeline.
95 static void rev_list_generate(int fd, struct ref *refs)
98 pid_t rev_list_generate_pid;
100 if (pipe(pipe_fd) < 0)
101 die("rev-list-generate setup: pipe failed");
102 rev_list_generate_pid = fork();
103 if (!rev_list_generate_pid) {
104 /* The child becomes the "rev-list | pack-objects"
105 * pipeline. It takes input from us, and its output
114 die("rev-list setup failed");
116 if (rev_list_generate_pid < 0)
117 die("rev-list-generate fork failed");
119 /* We feed the rev parameters to them. We do not write into
120 * fd nor read from the pipe.
127 if (!is_null_sha1(refs->old_sha1) &&
128 has_sha1_file(refs->old_sha1)) {
129 memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40);
132 write(pipe_fd[1], buf, 42);
134 if (!is_null_sha1(refs->new_sha1)) {
135 memcpy(buf, sha1_to_hex(refs->new_sha1), 40);
137 write(pipe_fd[1], buf, 41);
142 // waitpid(rev_list_generate_pid);
147 * Make a pack stream and spit it out into file descriptor fd
149 static void pack_objects(int fd, struct ref *refs)
153 rev_list_pid = fork();
155 rev_list_generate(fd, refs);
156 die("rev-list setup failed");
158 if (rev_list_pid < 0)
159 die("rev-list fork failed");
161 * We don't wait for the rev-list pipeline in the parent:
162 * we end up waiting for the other end instead
166 static void unmark_and_free(struct commit_list *list, unsigned int mark)
169 struct commit_list *temp = list;
170 temp->item->object.flags &= ~mark;
176 static int ref_newer(const unsigned char *new_sha1,
177 const unsigned char *old_sha1)
180 struct commit *old, *new;
181 struct commit_list *list, *used;
184 /* Both new and old must be commit-ish and new is descendant of
185 * old. Otherwise we require --force.
187 o = deref_tag(parse_object(old_sha1), NULL, 0);
188 if (!o || o->type != OBJ_COMMIT)
190 old = (struct commit *) o;
192 o = deref_tag(parse_object(new_sha1), NULL, 0);
193 if (!o || o->type != OBJ_COMMIT)
195 new = (struct commit *) o;
197 if (parse_commit(new) < 0)
201 commit_list_insert(new, &list);
203 new = pop_most_recent_commit(&list, 1);
204 commit_list_insert(new, &used);
210 unmark_and_free(list, 1);
211 unmark_and_free(used, 1);
215 static struct ref *local_refs, **local_tail;
216 static struct ref *remote_refs, **remote_tail;
218 static int one_local_ref(const char *refname, const unsigned char *sha1)
221 int len = strlen(refname) + 1;
222 ref = xcalloc(1, sizeof(*ref) + len);
223 hashcpy(ref->new_sha1, sha1);
224 memcpy(ref->name, refname, len);
226 local_tail = &ref->next;
230 static void get_local_heads(void)
232 local_tail = &local_refs;
233 for_each_ref(one_local_ref);
236 static int receive_status(int in)
240 int len = packet_read_line(in, line, sizeof(line));
241 if (len < 10 || memcmp(line, "unpack ", 7)) {
242 fprintf(stderr, "did not receive status back\n");
245 if (memcmp(line, "unpack ok\n", 10)) {
250 len = packet_read_line(in, line, sizeof(line));
254 (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
255 fprintf(stderr, "protocol error: %s\n", line);
259 if (!memcmp(line, "ok", 2))
267 static int send_pack(int in, int out, int nr_refspec, char **refspec)
272 int ask_for_status_report = 0;
273 int expect_status_report = 0;
275 /* No funny business with the matcher */
276 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL);
279 /* Does the other end support the reporting? */
280 if (server_supports("report-status"))
281 ask_for_status_report = 1;
285 remote_tail = &remote_refs;
286 if (match_refs(local_refs, remote_refs, &remote_tail,
287 nr_refspec, refspec, send_all))
291 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
296 * Finally, tell the other end!
299 for (ref = remote_refs; ref; ref = ref->next) {
300 char old_hex[60], *new_hex;
303 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
305 fprintf(stderr, "'%s': up-to-date\n", ref->name);
309 /* This part determines what can overwrite what.
312 * (0) you can always use --force or +A:B notation to
313 * selectively force individual ref pairs.
315 * (1) if the old thing does not exist, it is OK.
317 * (2) if you do not have the old thing, you are not allowed
318 * to overwrite it; you would not know what you are losing
321 * (3) if both new and old are commit-ish, and new is a
322 * descendant of old, it is OK.
326 !is_zero_sha1(ref->old_sha1) &&
328 if (!has_sha1_file(ref->old_sha1) ||
329 !ref_newer(ref->peer_ref->new_sha1,
331 /* We do not have the remote ref, or
332 * we know that the remote ref is not
333 * an ancestor of what we are trying to
334 * push. Either way this can be losing
335 * commits at the remote end and likely
336 * we were not up to date to begin with.
338 error("remote '%s' is not a strict "
339 "subset of local ref '%s'. "
340 "maybe you are not up-to-date and "
341 "need to pull first?",
343 ref->peer_ref->name);
348 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
349 if (is_zero_sha1(ref->new_sha1)) {
350 error("cannot happen anymore");
355 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
356 new_hex = sha1_to_hex(ref->new_sha1);
358 if (ask_for_status_report) {
359 packet_write(out, "%s %s %s%c%s",
360 old_hex, new_hex, ref->name, 0,
362 ask_for_status_report = 0;
363 expect_status_report = 1;
366 packet_write(out, "%s %s %s",
367 old_hex, new_hex, ref->name);
368 fprintf(stderr, "updating '%s'", ref->name);
369 if (strcmp(ref->name, ref->peer_ref->name))
370 fprintf(stderr, " using '%s'", ref->peer_ref->name);
371 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
376 pack_objects(out, remote_refs);
379 if (expect_status_report) {
380 if (receive_status(in))
384 if (!new_refs && ret == 0)
385 fprintf(stderr, "Everything up-to-date\n");
390 int main(int argc, char **argv)
398 setup_git_directory();
399 git_config(git_default_config);
402 for (i = 1; i < argc; i++, argv++) {
406 if (!strncmp(arg, "--exec=", 7)) {
410 if (!strcmp(arg, "--all")) {
414 if (!strcmp(arg, "--force")) {
418 if (!strcmp(arg, "--verbose")) {
422 if (!strcmp(arg, "--thin")) {
426 usage(send_pack_usage);
437 usage(send_pack_usage);
438 if (heads && send_all)
439 usage(send_pack_usage);
440 pid = git_connect(fd, dest, exec);
443 ret = send_pack(fd[0], fd[1], nr_heads, heads);
446 ret |= finish_connect(pid);