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);
137 while ((new = pop_most_recent_commit(&list, 1)) != NULL) {
144 static int local_ref_nr_match;
145 static char **local_ref_match;
146 static struct ref *local_ref_list;
147 static struct ref **local_last_ref;
149 static int try_to_match(const char *refname, const unsigned char *sha1)
154 if (!path_match(refname, local_ref_nr_match, local_ref_match)) {
158 /* If we have it listed already, skip it */
159 for (ref = local_ref_list ; ref ; ref = ref->next) {
160 if (!strcmp(ref->name, refname))
165 len = strlen(refname)+1;
166 ref = xmalloc(sizeof(*ref) + len);
167 memset(ref->old_sha1, 0, 20);
168 memcpy(ref->new_sha1, sha1, 20);
169 memcpy(ref->name, refname, len);
171 *local_last_ref = ref;
172 local_last_ref = &ref->next;
176 static int send_pack(int in, int out, int nr_match, char **match)
178 struct ref *ref_list, **last_ref;
182 /* First we get all heads, whether matching or not.. */
183 last_ref = get_remote_heads(in, &ref_list, 0, NULL);
186 * Go through the refs, see if we want to update
189 for (ref = ref_list; ref; ref = ref->next) {
190 unsigned char new_sha1[20];
191 char *name = ref->name;
193 if (nr_match && !path_match(name, nr_match, match))
196 if (read_ref(name, new_sha1) < 0)
199 if (!memcmp(ref->old_sha1, new_sha1, 20)) {
200 fprintf(stderr, "'%s' unchanged\n", name);
204 if (!ref_newer(new_sha1, ref->old_sha1)) {
205 error("remote '%s' isn't a strict parent of local", name);
209 /* Ok, mark it for update */
210 memcpy(ref->new_sha1, new_sha1, 20);
214 * See if we have any refs that the other end didn't have
217 local_ref_nr_match = nr_match;
218 local_ref_match = match;
219 local_ref_list = ref_list;
220 local_last_ref = last_ref;
221 for_each_ref(try_to_match);
225 * Finally, tell the other end!
228 for (ref = ref_list; ref; ref = ref->next) {
229 char old_hex[60], *new_hex;
230 if (is_zero_sha1(ref->new_sha1))
233 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
234 new_hex = sha1_to_hex(ref->new_sha1);
235 packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);
236 fprintf(stderr, "'%s': updating from %s to %s\n", ref->name, old_hex, new_hex);
241 pack_objects(out, ref_list);
246 int main(int argc, char **argv)
255 for (i = 1; i < argc; i++, argv++) {
259 if (!strncmp(arg, "--exec=", 7)) {
263 if (!strcmp(arg, "--all")) {
267 if (!strcmp(arg, "--force")) {
271 usage(send_pack_usage);
282 usage(send_pack_usage);
283 pid = git_connect(fd, dest, exec);
286 ret = send_pack(fd[0], fd[1], nr_heads, heads);