6 static const char send_pack_usage[] =
7 "git-send-pack [--all] [--exec=git-receive-pack] <remote> [<head>...]\n"
8 " --all and explicit <head> specification are mutually exclusive.";
9 static const char *exec = "git-receive-pack";
10 static int send_all = 0;
11 static int force_update = 0;
13 static int is_zero_sha1(const unsigned char *sha1)
17 for (i = 0; i < 20; i++) {
24 static void exec_pack_objects(void)
26 static char *args[] = {
31 execvp("git-pack-objects", args);
32 die("git-pack-objects exec failed (%s)", strerror(errno));
35 static void exec_rev_list(struct ref *refs)
37 static char *args[1000];
40 args[i++] = "git-rev-list"; /* 0 */
41 args[i++] = "--objects"; /* 1 */
43 char *buf = malloc(100);
45 die("git-rev-list environment overflow");
46 if (!is_zero_sha1(refs->old_sha1) &&
47 has_sha1_file(refs->old_sha1)) {
49 snprintf(buf, 50, "^%s", sha1_to_hex(refs->old_sha1));
52 if (!is_zero_sha1(refs->new_sha1)) {
54 snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1));
59 execvp("git-rev-list", args);
60 die("git-rev-list exec failed (%s)", strerror(errno));
63 static void rev_list(int fd, struct ref *refs)
66 pid_t pack_objects_pid;
68 if (pipe(pipe_fd) < 0)
69 die("rev-list setup: pipe failed");
70 pack_objects_pid = fork();
71 if (!pack_objects_pid) {
78 die("pack-objects setup failed");
80 if (pack_objects_pid < 0)
81 die("pack-objects fork failed");
89 static int pack_objects(int fd, struct ref *refs)
93 rev_list_pid = fork();
96 die("rev-list setup failed");
99 die("rev-list fork failed");
101 * We don't wait for the rev-list pipeline in the parent:
102 * we end up waiting for the other end instead
107 static int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
109 struct commit *new, *old;
110 struct commit_list *list;
114 old = lookup_commit_reference(old_sha1);
117 new = lookup_commit_reference(new_sha1);
120 if (parse_commit(new) < 0)
123 commit_list_insert(new, &list);
125 new = pop_most_recent_commit(&list, 1);
132 static struct ref *local_refs, **local_tail;
133 static struct ref *remote_refs, **remote_tail;
135 static int one_local_ref(const char *refname, const unsigned char *sha1)
138 int len = strlen(refname) + 1;
139 ref = xcalloc(1, sizeof(*ref) + len);
140 memcpy(ref->new_sha1, sha1, 20);
141 memcpy(ref->name, refname, len);
143 local_tail = &ref->next;
147 static void get_local_heads(void)
149 local_tail = &local_refs;
150 for_each_ref(one_local_ref);
153 static int send_pack(int in, int out, int nr_refspec, char **refspec)
158 /* No funny business with the matcher */
159 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL);
164 remote_tail = &remote_refs;
165 if (match_refs(local_refs, remote_refs, &remote_tail,
166 nr_refspec, refspec, send_all))
169 * Finally, tell the other end!
172 for (ref = remote_refs; ref; ref = ref->next) {
173 char old_hex[60], *new_hex;
176 if (!is_zero_sha1(ref->old_sha1)) {
177 if (!has_sha1_file(ref->old_sha1)) {
178 error("remote '%s' object %s does not "
180 ref->name, sha1_to_hex(ref->old_sha1));
183 if (!ref_newer(ref->peer_ref->new_sha1,
185 error("remote ref '%s' is not a strict "
186 "subset of local ref '%s'.", ref->name,
187 ref->peer_ref->name);
191 if (!memcmp(ref->old_sha1, ref->peer_ref->new_sha1, 20)) {
192 fprintf(stderr, "'%s': up-to-date\n", ref->name);
195 memcpy(ref->new_sha1, ref->peer_ref->new_sha1, 20);
196 if (is_zero_sha1(ref->new_sha1)) {
197 error("cannot happen anymore");
201 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
202 new_hex = sha1_to_hex(ref->new_sha1);
203 packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);
204 fprintf(stderr, "updating '%s'", ref->name);
205 if (strcmp(ref->name, ref->peer_ref->name))
206 fprintf(stderr, " using '%s'", ref->peer_ref->name);
207 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
212 pack_objects(out, remote_refs);
218 int main(int argc, char **argv)
227 for (i = 1; i < argc; i++, argv++) {
231 if (!strncmp(arg, "--exec=", 7)) {
235 if (!strcmp(arg, "--all")) {
239 if (!strcmp(arg, "--force")) {
243 usage(send_pack_usage);
254 usage(send_pack_usage);
255 if (heads && send_all)
256 usage(send_pack_usage);
257 pid = git_connect(fd, dest, exec);
260 ret = send_pack(fd[0], fd[1], nr_heads, heads);