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
) &&
47 has_sha1_file(refs
->old_sha1
)) {
49 snprintf(buf
, 50, "^%s", sha1_to_hex(refs
->old_sha1
));
52 if (!is_zero_sha1(refs
->new_sha1
)) {
54 snprintf(buf
, 50, "%s", sha1_to_hex(refs
->new_sha1
));
59 execvp("git-rev-list", args
);
60 die("git-rev-list exec failed (%s)", strerror(errno
));
63 static void rev_list(int fd
, struct ref
*refs
)
66 pid_t pack_objects_pid
;
68 if (pipe(pipe_fd
) < 0)
69 die("rev-list setup: pipe failed");
70 pack_objects_pid
= fork();
71 if (!pack_objects_pid
) {
78 die("pack-objects setup failed");
80 if (pack_objects_pid
< 0)
81 die("pack-objects fork failed");
89 static int pack_objects(int fd
, struct ref
*refs
)
93 rev_list_pid
= fork();
96 die("rev-list setup failed");
99 die("rev-list fork failed");
101 * We don't wait for the rev-list pipeline in the parent:
102 * we end up waiting for the other end instead
107 static int read_ref(const char *ref
, unsigned char *sha1
)
112 fd
= open(git_path("%s", ref
), O_RDONLY
);
116 if (read(fd
, buffer
, sizeof(buffer
)) >= 40)
117 ret
= get_sha1_hex(buffer
, sha1
);
122 static int ref_newer(const unsigned char *new_sha1
, const unsigned char *old_sha1
)
124 struct commit
*new, *old
;
125 struct commit_list
*list
;
129 old
= lookup_commit_reference(old_sha1
);
132 new = lookup_commit_reference(new_sha1
);
135 if (parse_commit(new) < 0)
138 commit_list_insert(new, &list
);
140 new = pop_most_recent_commit(&list
, 1);
147 static int local_ref_nr_match
;
148 static char **local_ref_match
;
149 static struct ref
*local_ref_list
;
150 static struct ref
**local_last_ref
;
152 static int try_to_match(const char *refname
, const unsigned char *sha1
)
157 if (!path_match(refname
, local_ref_nr_match
, local_ref_match
)) {
161 /* If we have it listed already, skip it */
162 for (ref
= local_ref_list
; ref
; ref
= ref
->next
) {
163 if (!strcmp(ref
->name
, refname
))
168 len
= strlen(refname
)+1;
169 ref
= xmalloc(sizeof(*ref
) + len
);
170 memset(ref
->old_sha1
, 0, 20);
171 memcpy(ref
->new_sha1
, sha1
, 20);
172 memcpy(ref
->name
, refname
, len
);
174 *local_last_ref
= ref
;
175 local_last_ref
= &ref
->next
;
179 static int send_pack(int in
, int out
, int nr_match
, char **match
)
181 struct ref
*ref_list
, **last_ref
;
185 /* First we get all heads, whether matching or not.. */
186 last_ref
= get_remote_heads(in
, &ref_list
, 0, NULL
);
189 * Go through the refs, see if we want to update
192 for (ref
= ref_list
; ref
; ref
= ref
->next
) {
193 unsigned char new_sha1
[20];
194 char *name
= ref
->name
;
196 if (nr_match
&& !path_match(name
, nr_match
, match
))
199 if (read_ref(name
, new_sha1
) < 0)
202 if (!memcmp(ref
->old_sha1
, new_sha1
, 20)) {
203 fprintf(stderr
, "'%s' unchanged\n", name
);
207 if (!has_sha1_file(ref
->old_sha1
)) {
208 error("remote '%s' object %s does not exist on local",
209 name
, sha1_to_hex(ref
->old_sha1
));
213 if (!ref_newer(new_sha1
, ref
->old_sha1
)) {
214 error("remote '%s' isn't a strict parent of local", 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
225 if (nr_match
|| send_all
) {
226 local_ref_nr_match
= nr_match
;
227 local_ref_match
= match
;
228 local_ref_list
= ref_list
;
229 local_last_ref
= last_ref
;
230 for_each_ref(try_to_match
);
234 * Finally, tell the other end!
237 for (ref
= ref_list
; ref
; ref
= ref
->next
) {
238 char old_hex
[60], *new_hex
;
239 if (is_zero_sha1(ref
->new_sha1
))
242 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
243 new_hex
= sha1_to_hex(ref
->new_sha1
);
244 packet_write(out
, "%s %s %s", old_hex
, new_hex
, ref
->name
);
245 fprintf(stderr
, "'%s': updating from %s to %s\n", ref
->name
, old_hex
, new_hex
);
250 pack_objects(out
, ref_list
);
255 int main(int argc
, char **argv
)
264 for (i
= 1; i
< argc
; i
++, argv
++) {
268 if (!strncmp(arg
, "--exec=", 7)) {
272 if (!strcmp(arg
, "--all")) {
276 if (!strcmp(arg
, "--force")) {
280 usage(send_pack_usage
);
291 usage(send_pack_usage
);
292 if (heads
&& send_all
)
293 usage(send_pack_usage
);
294 pid
= git_connect(fd
, dest
, exec
);
297 ret
= send_pack(fd
[0], fd
[1], nr_heads
, heads
);