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;
10 static int is_zero_sha1(const unsigned char *sha1
)
14 for (i
= 0; i
< 20; i
++) {
21 static void exec_pack_objects(void)
23 static char *args
[] = {
28 execvp("git-pack-objects", args
);
29 die("git-pack-objects exec failed (%s)", strerror(errno
));
32 static void exec_rev_list(struct ref
*refs
)
34 static char *args
[1000];
37 args
[i
++] = "git-rev-list"; /* 0 */
38 args
[i
++] = "--objects"; /* 1 */
40 char *buf
= malloc(100);
42 die("git-rev-list environment overflow");
43 if (!is_zero_sha1(refs
->old_sha1
)) {
45 snprintf(buf
, 50, "^%s", sha1_to_hex(refs
->old_sha1
));
48 if (!is_zero_sha1(refs
->new_sha1
)) {
50 snprintf(buf
, 50, "%s", sha1_to_hex(refs
->new_sha1
));
55 execvp("git-rev-list", args
);
56 die("git-rev-list exec failed (%s)", strerror(errno
));
59 static void rev_list(int fd
, struct ref
*refs
)
62 pid_t pack_objects_pid
;
64 if (pipe(pipe_fd
) < 0)
65 die("rev-list setup: pipe failed");
66 pack_objects_pid
= fork();
67 if (!pack_objects_pid
) {
74 die("pack-objects setup failed");
76 if (pack_objects_pid
< 0)
77 die("pack-objects fork failed");
85 static int pack_objects(int fd
, struct ref
*refs
)
89 rev_list_pid
= fork();
92 die("rev-list setup failed");
95 die("rev-list fork failed");
97 * We don't wait for the rev-list pipeline in the parent:
98 * we end up waiting for the other end instead
103 static int read_ref(const char *ref
, unsigned char *sha1
)
108 fd
= open(git_path("%s", ref
), O_RDONLY
);
112 if (read(fd
, buffer
, sizeof(buffer
)) >= 40)
113 ret
= get_sha1_hex(buffer
, sha1
);
118 static int ref_newer(const unsigned char *new_sha1
, const unsigned char *old_sha1
)
120 if (!has_sha1_file(old_sha1
))
123 * FIXME! It is not correct to say that the new one is newer
124 * just because we don't have the old one!
126 * We should really see if we can reach the old_sha1 commit
127 * from the new_sha1 one.
132 static int local_ref_nr_match
;
133 static char **local_ref_match
;
134 static struct ref
*local_ref_list
;
135 static struct ref
**local_last_ref
;
137 static int try_to_match(const char *refname
, const unsigned char *sha1
)
142 if (!path_match(refname
, local_ref_nr_match
, local_ref_match
)) {
146 /* If we have it listed already, skip it */
147 for (ref
= local_ref_list
; ref
; ref
= ref
->next
) {
148 if (!strcmp(ref
->name
, refname
))
153 len
= strlen(refname
)+1;
154 ref
= xmalloc(sizeof(*ref
) + len
);
155 memset(ref
->old_sha1
, 0, 20);
156 memcpy(ref
->new_sha1
, sha1
, 20);
157 memcpy(ref
->name
, refname
, len
);
159 *local_last_ref
= ref
;
160 local_last_ref
= &ref
->next
;
164 static int send_pack(int in
, int out
, int nr_match
, char **match
)
166 struct ref
*ref_list
, **last_ref
;
170 /* First we get all heads, whether matching or not.. */
171 last_ref
= get_remote_heads(in
, &ref_list
, 0, NULL
);
174 * Go through the refs, see if we want to update
177 for (ref
= ref_list
; ref
; ref
= ref
->next
) {
178 unsigned char new_sha1
[20];
179 char *name
= ref
->name
;
181 if (nr_match
&& !path_match(name
, nr_match
, match
))
184 if (read_ref(name
, new_sha1
) < 0)
187 if (!memcmp(ref
->old_sha1
, new_sha1
, 20)) {
188 fprintf(stderr
, "'%s' unchanged\n", name
);
192 if (!ref_newer(new_sha1
, ref
->old_sha1
)) {
193 error("remote '%s' points to object I don't have", name
);
197 /* Ok, mark it for update */
198 memcpy(ref
->new_sha1
, new_sha1
, 20);
202 * See if we have any refs that the other end didn't have
205 local_ref_nr_match
= nr_match
;
206 local_ref_match
= match
;
207 local_ref_list
= ref_list
;
208 local_last_ref
= last_ref
;
209 for_each_ref(try_to_match
);
213 * Finally, tell the other end!
216 for (ref
= ref_list
; ref
; ref
= ref
->next
) {
217 char old_hex
[60], *new_hex
;
218 if (is_zero_sha1(ref
->new_sha1
))
221 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
222 new_hex
= sha1_to_hex(ref
->new_sha1
);
223 packet_write(out
, "%s %s %s", old_hex
, new_hex
, ref
->name
);
224 fprintf(stderr
, "'%s': updating from %s to %s\n", ref
->name
, old_hex
, new_hex
);
229 pack_objects(out
, ref_list
);
234 int main(int argc
, char **argv
)
243 for (i
= 1; i
< argc
; i
++, argv
++) {
247 if (!strncmp(arg
, "--exec=", 7)) {
251 if (!strcmp(arg
, "--all")) {
255 usage(send_pack_usage
);
266 usage(send_pack_usage
);
267 pid
= git_connect(fd
, dest
, exec
);
270 ret
= send_pack(fd
[0], fd
[1], nr_heads
, heads
);