5 static const char send_pack_usage[] = "git-send-pack [--exec=other] destination [heads]*";
6 static const char *exec = "git-receive-pack";
10 unsigned char old_sha1[20];
11 unsigned char new_sha1[20];
15 static int is_zero_sha1(const unsigned char *sha1)
19 for (i = 0; i < 20; i++) {
26 static void exec_pack_objects(void)
28 static char *args[] = {
33 execvp("git-pack-objects", args);
34 die("git-pack-objects exec failed (%s)", strerror(errno));
37 static void exec_rev_list(struct ref *refs)
39 static char *args[1000];
42 args[i++] = "git-rev-list"; /* 0 */
43 args[i++] = "--objects"; /* 1 */
45 char *buf = malloc(100);
47 die("git-rev-list environment overflow");
48 if (!is_zero_sha1(refs->old_sha1)) {
50 snprintf(buf, 50, "^%s", sha1_to_hex(refs->old_sha1));
53 if (!is_zero_sha1(refs->new_sha1)) {
55 snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1));
60 execvp("git-rev-list", args);
61 die("git-rev-list exec failed (%s)", strerror(errno));
64 static void rev_list(int fd, struct ref *refs)
67 pid_t pack_objects_pid;
69 if (pipe(pipe_fd) < 0)
70 die("rev-list setup: pipe failed");
71 pack_objects_pid = fork();
72 if (!pack_objects_pid) {
79 die("pack-objects setup failed");
81 if (pack_objects_pid < 0)
82 die("pack-objects fork failed");
90 static int pack_objects(int fd, struct ref *refs)
94 rev_list_pid = fork();
97 die("rev-list setup failed");
100 die("rev-list fork failed");
102 * We don't wait for the rev-list pipeline in the parent:
103 * we end up waiting for the other end instead
108 static int read_ref(const char *ref, unsigned char *sha1)
113 fd = open(git_path("%s", ref), O_RDONLY);
117 if (read(fd, buffer, sizeof(buffer)) >= 40)
118 ret = get_sha1_hex(buffer, sha1);
123 static int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
125 if (!has_sha1_file(old_sha1))
128 * FIXME! It is not correct to say that the new one is newer
129 * just because we don't have the old one!
131 * We should really see if we can reach the old_sha1 commit
132 * from the new_sha1 one.
137 static int local_ref_nr_match;
138 static char **local_ref_match;
139 static struct ref **local_ref_list;
141 static int try_to_match(const char *refname, const unsigned char *sha1)
146 if (!path_match(refname, local_ref_nr_match, local_ref_match))
149 len = strlen(refname)+1;
150 ref = xmalloc(sizeof(*ref) + len);
151 memset(ref->old_sha1, 0, 20);
152 memcpy(ref->new_sha1, sha1, 20);
153 memcpy(ref->name, refname, len);
155 *local_ref_list = ref;
156 local_ref_list = &ref->next;
160 static int send_pack(int in, int out, int nr_match, char **match)
162 struct ref *ref_list = NULL, **last_ref = &ref_list;
167 * Read all the refs from the other end
170 unsigned char old_sha1[20];
171 static char buffer[1000];
175 len = packet_read_line(in, buffer, sizeof(buffer));
178 if (buffer[len-1] == '\n')
181 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
182 die("protocol error: expected sha/ref, got '%s'", buffer);
184 ref = xmalloc(sizeof(*ref) + len - 40);
185 memcpy(ref->old_sha1, old_sha1, 20);
186 memset(ref->new_sha1, 0, 20);
187 memcpy(ref->name, buffer + 41, len - 40);
190 last_ref = &ref->next;
194 * Go through the refs, see if we want to update
197 for (ref = ref_list; ref; ref = ref->next) {
198 unsigned char new_sha1[20];
199 char *name = ref->name;
201 if (nr_match && !path_match(name, nr_match, match))
204 if (read_ref(name, new_sha1) < 0)
207 if (!memcmp(ref->old_sha1, new_sha1, 20)) {
208 fprintf(stderr, "'%s' unchanged\n", name);
212 if (!ref_newer(new_sha1, ref->old_sha1)) {
213 error("remote '%s' points to object I don't have", name);
217 /* Ok, mark it for update */
218 memcpy(ref->new_sha1, new_sha1, 20);
222 * See if we have any refs that the other end didn't have
225 local_ref_nr_match = nr_match;
226 local_ref_match = match;
227 local_ref_list = last_ref;
228 for_each_ref(try_to_match);
232 * Finally, tell the other end!
235 for (ref = ref_list; ref; ref = ref->next) {
236 char old_hex[60], *new_hex;
237 if (is_zero_sha1(ref->new_sha1))
240 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
241 new_hex = sha1_to_hex(ref->new_sha1);
242 packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);
243 fprintf(stderr, "'%s': updating from %s to %s\n", ref->name, old_hex, new_hex);
248 pack_objects(out, ref_list);
253 int main(int argc, char **argv)
262 for (i = 1; i < argc; i++) {
266 if (!strncmp(arg, "--exec=", 7)) {
270 usage(send_pack_usage);
274 nr_heads = argc - i -1;
278 usage(send_pack_usage);
279 pid = git_connect(fd, dest, exec);
282 ret = send_pack(fd[0], fd[1], nr_heads, heads);