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[] = {
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];
45 args[i++] = "rev-list"; /* 0 */
46 if (use_thin_pack) /* 1 */
47 args[i++] = "--objects-edge";
49 args[i++] = "--objects";
51 args[i++] = "--stdin";
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)
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
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. */
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)
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
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.
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);
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);
138 write(pipe_fd[1], buf, 41);
143 // waitpid(rev_list_generate_pid);
148 * Make a pack stream and spit it out into file descriptor fd
150 static void pack_objects(int fd, struct ref *refs)
154 rev_list_pid = fork();
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)
170 struct commit_list *temp = list;
171 temp->item->object.flags &= ~mark;
177 static int ref_newer(const unsigned char *new_sha1,
178 const unsigned char *old_sha1)
181 struct commit *old, *new;
182 struct commit_list *list, *used;
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)
191 old = (struct commit *) o;
193 o = deref_tag(parse_object(new_sha1), NULL, 0);
194 if (!o || o->type != OBJ_COMMIT)
196 new = (struct commit *) o;
198 if (parse_commit(new) < 0)
202 commit_list_insert(new, &list);
204 new = pop_most_recent_commit(&list, 1);
205 commit_list_insert(new, &used);
211 unmark_and_free(list, 1);
212 unmark_and_free(used, 1);
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, int flag, void *cb_data)
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);
227 local_tail = &ref->next;
231 static void get_local_heads(void)
233 local_tail = &local_refs;
234 for_each_ref(one_local_ref, NULL);
237 static int receive_status(int in)
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");
246 if (memcmp(line, "unpack ok\n", 10)) {
251 len = packet_read_line(in, line, sizeof(line));
255 (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
256 fprintf(stderr, "protocol error: %s\n", line);
260 if (!memcmp(line, "ok", 2))
268 static int send_pack(int in, int out, int nr_refspec, char **refspec)
273 int ask_for_status_report = 0;
274 int allow_deleting_refs = 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);
281 /* Does the other end support the reporting? */
282 if (server_supports("report-status"))
283 ask_for_status_report = 1;
284 if (server_supports("delete-refs"))
285 allow_deleting_refs = 1;
289 remote_tail = &remote_refs;
290 if (match_refs(local_refs, remote_refs, &remote_tail,
291 nr_refspec, refspec, send_all))
295 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
300 * Finally, tell the other end!
303 for (ref = remote_refs; ref; ref = ref->next) {
304 char old_hex[60], *new_hex;
310 delete_ref = is_null_sha1(ref->peer_ref->new_sha1);
311 if (delete_ref && !allow_deleting_refs) {
312 error("remote does not support deleting refs");
317 !hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
319 fprintf(stderr, "'%s': up-to-date\n", ref->name);
323 /* This part determines what can overwrite what.
326 * (0) you can always use --force or +A:B notation to
327 * selectively force individual ref pairs.
329 * (1) if the old thing does not exist, it is OK.
331 * (2) if you do not have the old thing, you are not allowed
332 * to overwrite it; you would not know what you are losing
335 * (3) if both new and old are commit-ish, and new is a
336 * descendant of old, it is OK.
338 * (4) regardless of all of the above, removing :B is
344 !is_zero_sha1(ref->old_sha1) &&
346 if (!has_sha1_file(ref->old_sha1) ||
347 !ref_newer(ref->peer_ref->new_sha1,
349 /* We do not have the remote ref, or
350 * we know that the remote ref is not
351 * an ancestor of what we are trying to
352 * push. Either way this can be losing
353 * commits at the remote end and likely
354 * we were not up to date to begin with.
356 error("remote '%s' is not a strict "
357 "subset of local ref '%s'. "
358 "maybe you are not up-to-date and "
359 "need to pull first?",
361 ref->peer_ref->name);
366 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
369 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
370 new_hex = sha1_to_hex(ref->new_sha1);
372 if (ask_for_status_report) {
373 packet_write(out, "%s %s %s%c%s",
374 old_hex, new_hex, ref->name, 0,
376 ask_for_status_report = 0;
377 expect_status_report = 1;
380 packet_write(out, "%s %s %s",
381 old_hex, new_hex, ref->name);
383 fprintf(stderr, "deleting '%s'\n", ref->name);
385 fprintf(stderr, "updating '%s'", ref->name);
386 if (strcmp(ref->name, ref->peer_ref->name))
387 fprintf(stderr, " using '%s'",
388 ref->peer_ref->name);
389 fprintf(stderr, "\n from %s\n to %s\n",
396 pack_objects(out, remote_refs);
399 if (expect_status_report) {
400 if (receive_status(in))
404 if (!new_refs && ret == 0)
405 fprintf(stderr, "Everything up-to-date\n");
409 static void verify_remote_names(int nr_heads, char **heads)
413 for (i = 0; i < nr_heads; i++) {
414 const char *remote = strchr(heads[i], ':');
416 remote = remote ? (remote + 1) : heads[i];
417 switch (check_ref_format(remote)) {
419 case -2: /* ok but a single level -- that is fine for
424 die("remote part of refspec is not a valid name in %s",
429 int main(int argc, char **argv)
437 setup_git_directory();
438 git_config(git_default_config);
441 for (i = 1; i < argc; i++, argv++) {
445 if (!strncmp(arg, "--exec=", 7)) {
449 if (!strcmp(arg, "--all")) {
453 if (!strcmp(arg, "--force")) {
457 if (!strcmp(arg, "--verbose")) {
461 if (!strcmp(arg, "--thin")) {
465 usage(send_pack_usage);
476 usage(send_pack_usage);
477 if (heads && send_all)
478 usage(send_pack_usage);
479 verify_remote_names(nr_heads, heads);
481 pid = git_connect(fd, dest, exec);
484 ret = send_pack(fd[0], fd[1], nr_heads, heads);
487 ret |= finish_connect(pid);