6 static const char send_pack_usage[] =
7 "git-send-pack [--exec=git-receive-pack] [host:]directory [heads]*";
8 static const char *exec = "git-receive-pack";
9 static int send_all = 0;
10 static int force_update = 0;
12 static int is_zero_sha1(const unsigned char *sha1)
16 for (i = 0; i < 20; i++) {
23 static void exec_pack_objects(void)
25 static char *args[] = {
30 execvp("git-pack-objects", args);
31 die("git-pack-objects exec failed (%s)", strerror(errno));
34 static void exec_rev_list(struct ref *refs)
36 static char *args[1000];
39 args[i++] = "git-rev-list"; /* 0 */
40 args[i++] = "--objects"; /* 1 */
42 char *buf = malloc(100);
44 die("git-rev-list environment overflow");
45 if (!is_zero_sha1(refs->old_sha1)) {
47 snprintf(buf, 50, "^%s", sha1_to_hex(refs->old_sha1));
50 if (!is_zero_sha1(refs->new_sha1)) {
52 snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1));
57 execvp("git-rev-list", args);
58 die("git-rev-list exec failed (%s)", strerror(errno));
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) {
76 die("pack-objects setup failed");
78 if (pack_objects_pid < 0)
79 die("pack-objects fork failed");
87 static int pack_objects(int fd, struct ref *refs)
91 rev_list_pid = fork();
94 die("rev-list setup failed");
97 die("rev-list fork failed");
99 * We don't wait for the rev-list pipeline in the parent:
100 * we end up waiting for the other end instead
105 static int read_ref(const char *ref, unsigned char *sha1)
110 fd = open(git_path("%s", ref), O_RDONLY);
114 if (read(fd, buffer, sizeof(buffer)) >= 40)
115 ret = get_sha1_hex(buffer, sha1);
120 static int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
122 struct commit *new, *old;
123 struct commit_list *list;
127 old = lookup_commit_reference(old_sha1);
130 new = lookup_commit_reference(new_sha1);
133 if (parse_commit(new) < 0)
136 commit_list_insert(new, &list);
138 new = pop_most_recent_commit(&list, 1);
145 static int local_ref_nr_match;
146 static char **local_ref_match;
147 static struct ref *local_ref_list;
148 static struct ref **local_last_ref;
150 static int try_to_match(const char *refname, const unsigned char *sha1)
155 if (!path_match(refname, local_ref_nr_match, local_ref_match)) {
159 /* If we have it listed already, skip it */
160 for (ref = local_ref_list ; ref ; ref = ref->next) {
161 if (!strcmp(ref->name, refname))
166 len = strlen(refname)+1;
167 ref = xmalloc(sizeof(*ref) + len);
168 memset(ref->old_sha1, 0, 20);
169 memcpy(ref->new_sha1, sha1, 20);
170 memcpy(ref->name, refname, len);
172 *local_last_ref = ref;
173 local_last_ref = &ref->next;
177 static int send_pack(int in, int out, int nr_match, char **match)
179 struct ref *ref_list, **last_ref;
183 /* First we get all heads, whether matching or not.. */
184 last_ref = get_remote_heads(in, &ref_list, 0, NULL);
187 * Go through the refs, see if we want to update
190 for (ref = ref_list; ref; ref = ref->next) {
191 unsigned char new_sha1[20];
192 char *name = ref->name;
194 if (nr_match && !path_match(name, nr_match, match))
197 if (read_ref(name, new_sha1) < 0)
200 if (!memcmp(ref->old_sha1, new_sha1, 20)) {
201 fprintf(stderr, "'%s' unchanged\n", name);
205 if (!ref_newer(new_sha1, ref->old_sha1)) {
206 error("remote '%s' isn't a strict parent of local", name);
210 /* Ok, mark it for update */
211 memcpy(ref->new_sha1, new_sha1, 20);
215 * See if we have any refs that the other end didn't have
218 local_ref_nr_match = nr_match;
219 local_ref_match = match;
220 local_ref_list = ref_list;
221 local_last_ref = last_ref;
222 for_each_ref(try_to_match);
226 * Finally, tell the other end!
229 for (ref = ref_list; ref; ref = ref->next) {
230 char old_hex[60], *new_hex;
231 if (is_zero_sha1(ref->new_sha1))
234 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
235 new_hex = sha1_to_hex(ref->new_sha1);
236 packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);
237 fprintf(stderr, "'%s': updating from %s to %s\n", ref->name, old_hex, new_hex);
242 pack_objects(out, ref_list);
247 int main(int argc, char **argv)
256 for (i = 1; i < argc; i++, argv++) {
260 if (!strncmp(arg, "--exec=", 7)) {
264 if (!strcmp(arg, "--all")) {
268 if (!strcmp(arg, "--force")) {
272 usage(send_pack_usage);
283 usage(send_pack_usage);
284 pid = git_connect(fd, dest, exec);
287 ret = send_pack(fd[0], fd[1], nr_heads, heads);