5 static const char send_pack_usage[] =
6 "git-send-pack [--exec=git-receive-pack] [host:]directory [heads]*";
7 static const char *exec = "git-receive-pack";
8 static int send_all = 0;
12 unsigned char old_sha1[20];
13 unsigned char new_sha1[20];
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 char *args[] = {
35 execvp("git-pack-objects", args);
36 die("git-pack-objects exec failed (%s)", strerror(errno));
39 static void exec_rev_list(struct ref *refs)
41 static char *args[1000];
44 args[i++] = "git-rev-list"; /* 0 */
45 args[i++] = "--objects"; /* 1 */
47 char *buf = malloc(100);
49 die("git-rev-list environment overflow");
50 if (!is_zero_sha1(refs->old_sha1)) {
52 snprintf(buf, 50, "^%s", sha1_to_hex(refs->old_sha1));
55 if (!is_zero_sha1(refs->new_sha1)) {
57 snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1));
62 execvp("git-rev-list", args);
63 die("git-rev-list exec failed (%s)", strerror(errno));
66 static void rev_list(int fd, struct ref *refs)
69 pid_t pack_objects_pid;
71 if (pipe(pipe_fd) < 0)
72 die("rev-list setup: pipe failed");
73 pack_objects_pid = fork();
74 if (!pack_objects_pid) {
81 die("pack-objects setup failed");
83 if (pack_objects_pid < 0)
84 die("pack-objects fork failed");
92 static int pack_objects(int fd, struct ref *refs)
96 rev_list_pid = fork();
99 die("rev-list setup failed");
101 if (rev_list_pid < 0)
102 die("rev-list fork failed");
104 * We don't wait for the rev-list pipeline in the parent:
105 * we end up waiting for the other end instead
110 static int read_ref(const char *ref, unsigned char *sha1)
115 fd = open(git_path("%s", ref), O_RDONLY);
119 if (read(fd, buffer, sizeof(buffer)) >= 40)
120 ret = get_sha1_hex(buffer, sha1);
125 static int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
127 if (!has_sha1_file(old_sha1))
130 * FIXME! It is not correct to say that the new one is newer
131 * just because we don't have the old one!
133 * We should really see if we can reach the old_sha1 commit
134 * from the new_sha1 one.
139 static int local_ref_nr_match;
140 static char **local_ref_match;
141 static struct ref *local_ref_list;
142 static struct ref **local_last_ref;
144 static int try_to_match(const char *refname, const unsigned char *sha1)
149 if (!path_match(refname, local_ref_nr_match, local_ref_match)) {
153 /* If we have it listed already, skip it */
154 for (ref = local_ref_list ; ref ; ref = ref->next) {
155 if (!strcmp(ref->name, refname))
160 len = strlen(refname)+1;
161 ref = xmalloc(sizeof(*ref) + len);
162 memset(ref->old_sha1, 0, 20);
163 memcpy(ref->new_sha1, sha1, 20);
164 memcpy(ref->name, refname, len);
166 *local_last_ref = ref;
167 local_last_ref = &ref->next;
171 static int send_pack(int in, int out, int nr_match, char **match)
173 struct ref *ref_list = NULL, **last_ref = &ref_list;
178 * Read all the refs from the other end
181 unsigned char old_sha1[20];
182 static char buffer[1000];
186 len = packet_read_line(in, buffer, sizeof(buffer));
189 if (buffer[len-1] == '\n')
192 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
193 die("protocol error: expected sha/ref, got '%s'", buffer);
195 ref = xmalloc(sizeof(*ref) + len - 40);
196 memcpy(ref->old_sha1, old_sha1, 20);
197 memset(ref->new_sha1, 0, 20);
198 memcpy(ref->name, buffer + 41, len - 40);
201 last_ref = &ref->next;
205 * Go through the refs, see if we want to update
208 for (ref = ref_list; ref; ref = ref->next) {
209 unsigned char new_sha1[20];
210 char *name = ref->name;
212 if (nr_match && !path_match(name, nr_match, match))
215 if (read_ref(name, new_sha1) < 0)
218 if (!memcmp(ref->old_sha1, new_sha1, 20)) {
219 fprintf(stderr, "'%s' unchanged\n", name);
223 if (!ref_newer(new_sha1, ref->old_sha1)) {
224 error("remote '%s' points to object I don't have", name);
228 /* Ok, mark it for update */
229 memcpy(ref->new_sha1, new_sha1, 20);
233 * See if we have any refs that the other end didn't have
236 local_ref_nr_match = nr_match;
237 local_ref_match = match;
238 local_ref_list = ref_list;
239 local_last_ref = last_ref;
240 for_each_ref(try_to_match);
244 * Finally, tell the other end!
247 for (ref = ref_list; ref; ref = ref->next) {
248 char old_hex[60], *new_hex;
249 if (is_zero_sha1(ref->new_sha1))
252 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
253 new_hex = sha1_to_hex(ref->new_sha1);
254 packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);
255 fprintf(stderr, "'%s': updating from %s to %s\n", ref->name, old_hex, new_hex);
260 pack_objects(out, ref_list);
265 int main(int argc, char **argv)
274 for (i = 1; i < argc; i++, argv++) {
278 if (!strncmp(arg, "--exec=", 7)) {
282 if (!strcmp(arg, "--all")) {
286 usage(send_pack_usage);
297 usage(send_pack_usage);
298 pid = git_connect(fd, dest, exec);
301 ret = send_pack(fd[0], fd[1], nr_heads, heads);