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";
11 unsigned char old_sha1
[20];
12 unsigned char new_sha1
[20];
16 static int is_zero_sha1(const unsigned char *sha1
)
20 for (i
= 0; i
< 20; i
++) {
27 static void exec_pack_objects(void)
29 static char *args
[] = {
34 execvp("git-pack-objects", args
);
35 die("git-pack-objects exec failed (%s)", strerror(errno
));
38 static void exec_rev_list(struct ref
*refs
)
40 static char *args
[1000];
43 args
[i
++] = "git-rev-list"; /* 0 */
44 args
[i
++] = "--objects"; /* 1 */
46 char *buf
= malloc(100);
48 die("git-rev-list environment overflow");
49 if (!is_zero_sha1(refs
->old_sha1
)) {
51 snprintf(buf
, 50, "^%s", sha1_to_hex(refs
->old_sha1
));
54 if (!is_zero_sha1(refs
->new_sha1
)) {
56 snprintf(buf
, 50, "%s", sha1_to_hex(refs
->new_sha1
));
61 execvp("git-rev-list", args
);
62 die("git-rev-list exec failed (%s)", strerror(errno
));
65 static void rev_list(int fd
, struct ref
*refs
)
68 pid_t pack_objects_pid
;
70 if (pipe(pipe_fd
) < 0)
71 die("rev-list setup: pipe failed");
72 pack_objects_pid
= fork();
73 if (!pack_objects_pid
) {
80 die("pack-objects setup failed");
82 if (pack_objects_pid
< 0)
83 die("pack-objects fork failed");
91 static int pack_objects(int fd
, struct ref
*refs
)
95 rev_list_pid
= fork();
98 die("rev-list setup failed");
100 if (rev_list_pid
< 0)
101 die("rev-list fork failed");
103 * We don't wait for the rev-list pipeline in the parent:
104 * we end up waiting for the other end instead
109 static int read_ref(const char *ref
, unsigned char *sha1
)
114 fd
= open(git_path("%s", ref
), O_RDONLY
);
118 if (read(fd
, buffer
, sizeof(buffer
)) >= 40)
119 ret
= get_sha1_hex(buffer
, sha1
);
124 static int ref_newer(const unsigned char *new_sha1
, const unsigned char *old_sha1
)
126 if (!has_sha1_file(old_sha1
))
129 * FIXME! It is not correct to say that the new one is newer
130 * just because we don't have the old one!
132 * We should really see if we can reach the old_sha1 commit
133 * from the new_sha1 one.
138 static int local_ref_nr_match
;
139 static char **local_ref_match
;
140 static struct ref
**local_ref_list
;
142 static int try_to_match(const char *refname
, const unsigned char *sha1
)
147 if (!path_match(refname
, local_ref_nr_match
, local_ref_match
))
150 len
= strlen(refname
)+1;
151 ref
= xmalloc(sizeof(*ref
) + len
);
152 memset(ref
->old_sha1
, 0, 20);
153 memcpy(ref
->new_sha1
, sha1
, 20);
154 memcpy(ref
->name
, refname
, len
);
156 *local_ref_list
= ref
;
157 local_ref_list
= &ref
->next
;
161 static int send_pack(int in
, int out
, int nr_match
, char **match
)
163 struct ref
*ref_list
= NULL
, **last_ref
= &ref_list
;
168 * Read all the refs from the other end
171 unsigned char old_sha1
[20];
172 static char buffer
[1000];
176 len
= packet_read_line(in
, buffer
, sizeof(buffer
));
179 if (buffer
[len
-1] == '\n')
182 if (len
< 42 || get_sha1_hex(buffer
, old_sha1
) || buffer
[40] != ' ')
183 die("protocol error: expected sha/ref, got '%s'", buffer
);
185 ref
= xmalloc(sizeof(*ref
) + len
- 40);
186 memcpy(ref
->old_sha1
, old_sha1
, 20);
187 memset(ref
->new_sha1
, 0, 20);
188 memcpy(ref
->name
, buffer
+ 41, len
- 40);
191 last_ref
= &ref
->next
;
195 * Go through the refs, see if we want to update
198 for (ref
= ref_list
; ref
; ref
= ref
->next
) {
199 unsigned char new_sha1
[20];
200 char *name
= ref
->name
;
202 if (nr_match
&& !path_match(name
, nr_match
, match
))
205 if (read_ref(name
, new_sha1
) < 0)
208 if (!memcmp(ref
->old_sha1
, new_sha1
, 20)) {
209 fprintf(stderr
, "'%s' unchanged\n", name
);
213 if (!ref_newer(new_sha1
, ref
->old_sha1
)) {
214 error("remote '%s' points to object I don't have", name
);
218 /* Ok, mark it for update */
219 memcpy(ref
->new_sha1
, new_sha1
, 20);
223 * See if we have any refs that the other end didn't have
226 local_ref_nr_match
= nr_match
;
227 local_ref_match
= match
;
228 local_ref_list
= last_ref
;
229 for_each_ref(try_to_match
);
233 * Finally, tell the other end!
236 for (ref
= ref_list
; ref
; ref
= ref
->next
) {
237 char old_hex
[60], *new_hex
;
238 if (is_zero_sha1(ref
->new_sha1
))
241 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
242 new_hex
= sha1_to_hex(ref
->new_sha1
);
243 packet_write(out
, "%s %s %s", old_hex
, new_hex
, ref
->name
);
244 fprintf(stderr
, "'%s': updating from %s to %s\n", ref
->name
, old_hex
, new_hex
);
249 pack_objects(out
, ref_list
);
254 int main(int argc
, char **argv
)
263 for (i
= 1; i
< argc
; i
++) {
267 if (!strncmp(arg
, "--exec=", 7)) {
271 usage(send_pack_usage
);
275 nr_heads
= argc
- i
-1;
279 usage(send_pack_usage
);
280 pid
= git_connect(fd
, dest
, exec
);
283 ret
= send_pack(fd
[0], fd
[1], nr_heads
, heads
);