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)) {
48 snprintf(buf, 50, "^%s", sha1_to_hex(refs->old_sha1));
51 if (!is_zero_sha1(refs->new_sha1)) {
53 snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1));
58 execvp("git-rev-list", args);
59 die("git-rev-list exec failed (%s)", strerror(errno));
62 static void rev_list(int fd, struct ref *refs)
65 pid_t pack_objects_pid;
67 if (pipe(pipe_fd) < 0)
68 die("rev-list setup: pipe failed");
69 pack_objects_pid = fork();
70 if (!pack_objects_pid) {
77 die("pack-objects setup failed");
79 if (pack_objects_pid < 0)
80 die("pack-objects fork failed");
88 static int pack_objects(int fd, struct ref *refs)
92 rev_list_pid = fork();
95 die("rev-list setup failed");
98 die("rev-list fork failed");
100 * We don't wait for the rev-list pipeline in the parent:
101 * we end up waiting for the other end instead
106 static int read_ref(const char *ref, unsigned char *sha1)
111 fd = open(git_path("%s", ref), O_RDONLY);
115 if (read(fd, buffer, sizeof(buffer)) >= 40)
116 ret = get_sha1_hex(buffer, sha1);
121 static int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
123 struct commit *new, *old;
124 struct commit_list *list;
128 old = lookup_commit_reference(old_sha1);
131 new = lookup_commit_reference(new_sha1);
134 if (parse_commit(new) < 0)
137 commit_list_insert(new, &list);
139 new = pop_most_recent_commit(&list, 1);
146 static int local_ref_nr_match;
147 static char **local_ref_match;
148 static struct ref *local_ref_list;
149 static struct ref **local_last_ref;
151 static int try_to_match(const char *refname, const unsigned char *sha1)
156 if (!path_match(refname, local_ref_nr_match, local_ref_match)) {
160 /* If we have it listed already, skip it */
161 for (ref = local_ref_list ; ref ; ref = ref->next) {
162 if (!strcmp(ref->name, refname))
167 len = strlen(refname)+1;
168 ref = xmalloc(sizeof(*ref) + len);
169 memset(ref->old_sha1, 0, 20);
170 memcpy(ref->new_sha1, sha1, 20);
171 memcpy(ref->name, refname, len);
173 *local_last_ref = ref;
174 local_last_ref = &ref->next;
178 static int send_pack(int in, int out, int nr_match, char **match)
180 struct ref *ref_list, **last_ref;
184 /* First we get all heads, whether matching or not.. */
185 last_ref = get_remote_heads(in, &ref_list, 0, NULL);
188 * Go through the refs, see if we want to update
191 for (ref = ref_list; ref; ref = ref->next) {
192 unsigned char new_sha1[20];
193 char *name = ref->name;
195 if (nr_match && !path_match(name, nr_match, match))
198 if (read_ref(name, new_sha1) < 0)
201 if (!memcmp(ref->old_sha1, new_sha1, 20)) {
202 fprintf(stderr, "'%s' unchanged\n", name);
206 if (!ref_newer(new_sha1, ref->old_sha1)) {
207 error("remote '%s' isn't a strict parent of local", name);
211 /* Ok, mark it for update */
212 memcpy(ref->new_sha1, new_sha1, 20);
216 * See if we have any refs that the other end didn't have
218 if (nr_match || send_all) {
219 local_ref_nr_match = nr_match;
220 local_ref_match = match;
221 local_ref_list = ref_list;
222 local_last_ref = last_ref;
223 for_each_ref(try_to_match);
227 * Finally, tell the other end!
230 for (ref = ref_list; ref; ref = ref->next) {
231 char old_hex[60], *new_hex;
232 if (is_zero_sha1(ref->new_sha1))
235 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
236 new_hex = sha1_to_hex(ref->new_sha1);
237 packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);
238 fprintf(stderr, "'%s': updating from %s to %s\n", ref->name, old_hex, new_hex);
243 pack_objects(out, ref_list);
248 int main(int argc, char **argv)
257 for (i = 1; i < argc; i++, argv++) {
261 if (!strncmp(arg, "--exec=", 7)) {
265 if (!strcmp(arg, "--all")) {
269 if (!strcmp(arg, "--force")) {
273 usage(send_pack_usage);
284 usage(send_pack_usage);
285 if (heads && send_all)
286 usage(send_pack_usage);
287 pid = git_connect(fd, dest, exec);
290 ret = send_pack(fd[0], fd[1], nr_heads, heads);