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 void exec_pack_objects(void)
19 static const char *args[] = {
26 die("git-pack-objects exec failed (%s)", strerror(errno));
29 static void exec_rev_list(struct ref *refs)
31 static const char *args[4];
34 args[i++] = "rev-list"; /* 0 */
35 if (use_thin_pack) /* 1 */
36 args[i++] = "--objects-edge";
38 args[i++] = "--objects";
40 args[i++] = "--stdin";
44 die("git-rev-list exec failed (%s)", strerror(errno));
48 * Run "rev-list --stdin | pack-objects" pipe.
50 static void rev_list(struct ref *refs)
53 pid_t pack_objects_pid;
55 if (pipe(pipe_fd) < 0)
56 die("rev-list setup: pipe failed");
57 pack_objects_pid = fork();
58 if (!pack_objects_pid) {
59 /* The child becomes pack-objects; reads from pipe
60 * and writes to the original fd
66 die("pack-objects setup failed");
68 if (pack_objects_pid < 0)
69 die("pack-objects fork failed");
71 /* We become rev-list --stdin; output goes to pipe. */
79 * Create "rev-list --stdin | pack-objects" pipe and feed
80 * the refs into the pipeline.
82 static void rev_list_generate(int fd, struct ref *refs)
85 pid_t rev_list_generate_pid;
87 if (pipe(pipe_fd) < 0)
88 die("rev-list-generate setup: pipe failed");
89 rev_list_generate_pid = fork();
90 if (!rev_list_generate_pid) {
91 /* The child becomes the "rev-list | pack-objects"
92 * pipeline. It takes input from us, and its output
101 die("rev-list setup failed");
103 if (rev_list_generate_pid < 0)
104 die("rev-list-generate fork failed");
106 /* We feed the rev parameters to them. We do not write into
107 * fd nor read from the pipe.
114 if (!is_null_sha1(refs->old_sha1) &&
115 has_sha1_file(refs->old_sha1)) {
116 memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40);
119 write(pipe_fd[1], buf, 42);
121 if (!is_null_sha1(refs->new_sha1)) {
122 memcpy(buf, sha1_to_hex(refs->new_sha1), 40);
124 write(pipe_fd[1], buf, 41);
129 // waitpid(rev_list_generate_pid);
134 * Make a pack stream and spit it out into file descriptor fd
136 static void pack_objects(int fd, struct ref *refs)
140 rev_list_pid = fork();
142 rev_list_generate(fd, refs);
143 die("rev-list setup failed");
145 if (rev_list_pid < 0)
146 die("rev-list fork failed");
148 * We don't wait for the rev-list pipeline in the parent:
149 * we end up waiting for the other end instead
153 static void unmark_and_free(struct commit_list *list, unsigned int mark)
156 struct commit_list *temp = list;
157 temp->item->object.flags &= ~mark;
163 static int ref_newer(const unsigned char *new_sha1,
164 const unsigned char *old_sha1)
167 struct commit *old, *new;
168 struct commit_list *list, *used;
171 /* Both new and old must be commit-ish and new is descendant of
172 * old. Otherwise we require --force.
174 o = deref_tag(parse_object(old_sha1), NULL, 0);
175 if (!o || o->type != OBJ_COMMIT)
177 old = (struct commit *) o;
179 o = deref_tag(parse_object(new_sha1), NULL, 0);
180 if (!o || o->type != OBJ_COMMIT)
182 new = (struct commit *) o;
184 if (parse_commit(new) < 0)
188 commit_list_insert(new, &list);
190 new = pop_most_recent_commit(&list, 1);
191 commit_list_insert(new, &used);
197 unmark_and_free(list, 1);
198 unmark_and_free(used, 1);
202 static struct ref *local_refs, **local_tail;
203 static struct ref *remote_refs, **remote_tail;
205 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
208 int len = strlen(refname) + 1;
209 ref = xcalloc(1, sizeof(*ref) + len);
210 hashcpy(ref->new_sha1, sha1);
211 memcpy(ref->name, refname, len);
213 local_tail = &ref->next;
217 static void get_local_heads(void)
219 local_tail = &local_refs;
220 for_each_ref(one_local_ref, NULL);
223 static int receive_status(int in)
227 int len = packet_read_line(in, line, sizeof(line));
228 if (len < 10 || memcmp(line, "unpack ", 7)) {
229 fprintf(stderr, "did not receive status back\n");
232 if (memcmp(line, "unpack ok\n", 10)) {
237 len = packet_read_line(in, line, sizeof(line));
241 (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
242 fprintf(stderr, "protocol error: %s\n", line);
246 if (!memcmp(line, "ok", 2))
254 static int send_pack(int in, int out, int nr_refspec, char **refspec)
259 int ask_for_status_report = 0;
260 int allow_deleting_refs = 0;
261 int expect_status_report = 0;
263 /* No funny business with the matcher */
264 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL);
267 /* Does the other end support the reporting? */
268 if (server_supports("report-status"))
269 ask_for_status_report = 1;
270 if (server_supports("delete-refs"))
271 allow_deleting_refs = 1;
275 remote_tail = &remote_refs;
276 if (match_refs(local_refs, remote_refs, &remote_tail,
277 nr_refspec, refspec, send_all))
281 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
286 * Finally, tell the other end!
289 for (ref = remote_refs; ref; ref = ref->next) {
290 char old_hex[60], *new_hex;
296 delete_ref = is_null_sha1(ref->peer_ref->new_sha1);
297 if (delete_ref && !allow_deleting_refs) {
298 error("remote does not support deleting refs");
303 !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.
324 * (4) regardless of all of the above, removing :B is
330 !is_null_sha1(ref->old_sha1) &&
332 if (!has_sha1_file(ref->old_sha1) ||
333 !ref_newer(ref->peer_ref->new_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?",
347 ref->peer_ref->name);
352 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
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);
369 fprintf(stderr, "deleting '%s'\n", ref->name);
371 fprintf(stderr, "updating '%s'", ref->name);
372 if (strcmp(ref->name, ref->peer_ref->name))
373 fprintf(stderr, " using '%s'",
374 ref->peer_ref->name);
375 fprintf(stderr, "\n from %s\n to %s\n",
382 pack_objects(out, remote_refs);
385 if (expect_status_report) {
386 if (receive_status(in))
390 if (!new_refs && ret == 0)
391 fprintf(stderr, "Everything up-to-date\n");
395 static void verify_remote_names(int nr_heads, char **heads)
399 for (i = 0; i < nr_heads; i++) {
400 const char *remote = strchr(heads[i], ':');
402 remote = remote ? (remote + 1) : heads[i];
403 switch (check_ref_format(remote)) {
405 case -2: /* ok but a single level -- that is fine for
410 die("remote part of refspec is not a valid name in %s",
415 int main(int argc, char **argv)
423 setup_git_directory();
424 git_config(git_default_config);
427 for (i = 1; i < argc; i++, argv++) {
431 if (!strncmp(arg, "--exec=", 7)) {
435 if (!strcmp(arg, "--all")) {
439 if (!strcmp(arg, "--force")) {
443 if (!strcmp(arg, "--verbose")) {
447 if (!strcmp(arg, "--thin")) {
451 usage(send_pack_usage);
462 usage(send_pack_usage);
463 if (heads && send_all)
464 usage(send_pack_usage);
465 verify_remote_names(nr_heads, heads);
467 pid = git_connect(fd, dest, exec);
470 ret = send_pack(fd[0], fd[1], nr_heads, heads);
473 ret |= finish_connect(pid);