Commit | Line | Data |
---|---|---|
61221472 | 1 | #include "cache.h" |
584c6cc9 | 2 | #include "refs.h" |
f3a3214e | 3 | #include "pkt-line.h" |
61221472 | 4 | |
2a245013 JH |
5 | static const char send_pack_usage[] = |
6 | "git-send-pack [--exec=git-receive-pack] [host:]directory [heads]*"; | |
61221472 LT |
7 | static const char *exec = "git-receive-pack"; |
8 | ||
e4b5c7ff LT |
9 | struct ref { |
10 | struct ref *next; | |
11 | unsigned char old_sha1[20]; | |
12 | unsigned char new_sha1[20]; | |
13 | char name[0]; | |
14 | }; | |
15 | ||
584c6cc9 LT |
16 | static int is_zero_sha1(const unsigned char *sha1) |
17 | { | |
18 | int i; | |
19 | ||
20 | for (i = 0; i < 20; i++) { | |
21 | if (*sha1++) | |
22 | return 0; | |
23 | } | |
24 | return 1; | |
25 | } | |
26 | ||
94fdb7aa LT |
27 | static void exec_pack_objects(void) |
28 | { | |
29 | static char *args[] = { | |
30 | "git-pack-objects", | |
31 | "--stdout", | |
32 | NULL | |
33 | }; | |
34 | execvp("git-pack-objects", args); | |
35 | die("git-pack-objects exec failed (%s)", strerror(errno)); | |
36 | } | |
37 | ||
38 | static void exec_rev_list(struct ref *refs) | |
39 | { | |
40 | static char *args[1000]; | |
41 | int i = 0; | |
42 | ||
43 | args[i++] = "git-rev-list"; /* 0 */ | |
44 | args[i++] = "--objects"; /* 1 */ | |
45 | while (refs) { | |
46 | char *buf = malloc(100); | |
47 | if (i > 900) | |
48 | die("git-rev-list environment overflow"); | |
584c6cc9 LT |
49 | if (!is_zero_sha1(refs->old_sha1)) { |
50 | args[i++] = buf; | |
51 | snprintf(buf, 50, "^%s", sha1_to_hex(refs->old_sha1)); | |
52 | buf += 50; | |
53 | } | |
54 | if (!is_zero_sha1(refs->new_sha1)) { | |
55 | args[i++] = buf; | |
56 | snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1)); | |
57 | } | |
94fdb7aa LT |
58 | refs = refs->next; |
59 | } | |
60 | args[i] = NULL; | |
61 | execvp("git-rev-list", args); | |
62 | die("git-rev-list exec failed (%s)", strerror(errno)); | |
63 | } | |
64 | ||
65 | static void rev_list(int fd, struct ref *refs) | |
66 | { | |
67 | int pipe_fd[2]; | |
68 | pid_t pack_objects_pid; | |
69 | ||
70 | if (pipe(pipe_fd) < 0) | |
71 | die("rev-list setup: pipe failed"); | |
72 | pack_objects_pid = fork(); | |
73 | if (!pack_objects_pid) { | |
74 | dup2(pipe_fd[0], 0); | |
75 | dup2(fd, 1); | |
76 | close(pipe_fd[0]); | |
77 | close(pipe_fd[1]); | |
78 | close(fd); | |
79 | exec_pack_objects(); | |
80 | die("pack-objects setup failed"); | |
81 | } | |
82 | if (pack_objects_pid < 0) | |
83 | die("pack-objects fork failed"); | |
84 | dup2(pipe_fd[1], 1); | |
85 | close(pipe_fd[0]); | |
86 | close(pipe_fd[1]); | |
87 | close(fd); | |
88 | exec_rev_list(refs); | |
89 | } | |
90 | ||
91 | static int pack_objects(int fd, struct ref *refs) | |
92 | { | |
93 | pid_t rev_list_pid; | |
94 | ||
95 | rev_list_pid = fork(); | |
96 | if (!rev_list_pid) { | |
97 | rev_list(fd, refs); | |
98 | die("rev-list setup failed"); | |
99 | } | |
100 | if (rev_list_pid < 0) | |
101 | die("rev-list fork failed"); | |
102 | /* | |
103 | * We don't wait for the rev-list pipeline in the parent: | |
104 | * we end up waiting for the other end instead | |
105 | */ | |
7ec4e608 | 106 | return 0; |
94fdb7aa | 107 | } |
e4b5c7ff LT |
108 | |
109 | static int read_ref(const char *ref, unsigned char *sha1) | |
110 | { | |
111 | int fd, ret; | |
e4b5c7ff | 112 | char buffer[60]; |
e4b5c7ff | 113 | |
723c31fe | 114 | fd = open(git_path("%s", ref), O_RDONLY); |
e4b5c7ff LT |
115 | if (fd < 0) |
116 | return -1; | |
117 | ret = -1; | |
118 | if (read(fd, buffer, sizeof(buffer)) >= 40) | |
119 | ret = get_sha1_hex(buffer, sha1); | |
120 | close(fd); | |
121 | return ret; | |
122 | } | |
123 | ||
584c6cc9 LT |
124 | static int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1) |
125 | { | |
126 | if (!has_sha1_file(old_sha1)) | |
127 | return 0; | |
128 | /* | |
129 | * FIXME! It is not correct to say that the new one is newer | |
130 | * just because we don't have the old one! | |
131 | * | |
132 | * We should really see if we can reach the old_sha1 commit | |
133 | * from the new_sha1 one. | |
134 | */ | |
135 | return 1; | |
136 | } | |
137 | ||
138 | static int local_ref_nr_match; | |
139 | static char **local_ref_match; | |
140 | static struct ref **local_ref_list; | |
141 | ||
142 | static int try_to_match(const char *refname, const unsigned char *sha1) | |
143 | { | |
144 | struct ref *ref; | |
145 | int len; | |
146 | ||
147 | if (!path_match(refname, local_ref_nr_match, local_ref_match)) | |
148 | return 0; | |
149 | ||
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); | |
155 | ref->next = NULL; | |
156 | *local_ref_list = ref; | |
157 | local_ref_list = &ref->next; | |
158 | return 0; | |
159 | } | |
160 | ||
d0efc8a7 | 161 | static int send_pack(int in, int out, int nr_match, char **match) |
61221472 | 162 | { |
94fdb7aa | 163 | struct ref *ref_list = NULL, **last_ref = &ref_list; |
7f8e9828 | 164 | struct ref *ref; |
584c6cc9 | 165 | int new_refs; |
7f8e9828 | 166 | |
584c6cc9 LT |
167 | /* |
168 | * Read all the refs from the other end | |
169 | */ | |
61221472 | 170 | for (;;) { |
e4b5c7ff | 171 | unsigned char old_sha1[20]; |
61221472 | 172 | static char buffer[1000]; |
e4b5c7ff | 173 | char *name; |
f3a3214e LT |
174 | int len; |
175 | ||
176 | len = packet_read_line(in, buffer, sizeof(buffer)); | |
e4b5c7ff LT |
177 | if (!len) |
178 | break; | |
179 | if (buffer[len-1] == '\n') | |
180 | buffer[--len] = 0; | |
181 | ||
182 | if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ') | |
183 | die("protocol error: expected sha/ref, got '%s'", buffer); | |
184 | name = buffer + 41; | |
584c6cc9 LT |
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); | |
189 | ref->next = NULL; | |
190 | *last_ref = ref; | |
191 | last_ref = &ref->next; | |
192 | } | |
193 | ||
194 | /* | |
195 | * Go through the refs, see if we want to update | |
196 | * any of them.. | |
197 | */ | |
198 | for (ref = ref_list; ref; ref = ref->next) { | |
199 | unsigned char new_sha1[20]; | |
200 | char *name = ref->name; | |
201 | ||
d0efc8a7 LT |
202 | if (nr_match && !path_match(name, nr_match, match)) |
203 | continue; | |
584c6cc9 | 204 | |
7f8e9828 | 205 | if (read_ref(name, new_sha1) < 0) |
584c6cc9 LT |
206 | continue; |
207 | ||
584c6cc9 | 208 | if (!memcmp(ref->old_sha1, new_sha1, 20)) { |
e4b5c7ff | 209 | fprintf(stderr, "'%s' unchanged\n", name); |
7f8e9828 | 210 | continue; |
e4b5c7ff | 211 | } |
584c6cc9 LT |
212 | |
213 | if (!ref_newer(new_sha1, ref->old_sha1)) { | |
214 | error("remote '%s' points to object I don't have", name); | |
215 | continue; | |
216 | } | |
217 | ||
218 | /* Ok, mark it for update */ | |
7f8e9828 | 219 | memcpy(ref->new_sha1, new_sha1, 20); |
7f8e9828 LT |
220 | } |
221 | ||
584c6cc9 LT |
222 | /* |
223 | * See if we have any refs that the other end didn't have | |
224 | */ | |
225 | if (nr_match) { | |
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); | |
230 | } | |
231 | ||
232 | /* | |
233 | * Finally, tell the other end! | |
234 | */ | |
235 | new_refs = 0; | |
7f8e9828 LT |
236 | for (ref = ref_list; ref; ref = ref->next) { |
237 | char old_hex[60], *new_hex; | |
584c6cc9 LT |
238 | if (is_zero_sha1(ref->new_sha1)) |
239 | continue; | |
240 | new_refs++; | |
7f8e9828 LT |
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); | |
61221472 | 245 | } |
7f8e9828 | 246 | |
f3a3214e | 247 | packet_flush(out); |
584c6cc9 | 248 | if (new_refs) |
94fdb7aa | 249 | pack_objects(out, ref_list); |
61221472 LT |
250 | close(out); |
251 | return 0; | |
252 | } | |
253 | ||
61221472 LT |
254 | int main(int argc, char **argv) |
255 | { | |
256 | int i, nr_heads = 0; | |
257 | char *dest = NULL; | |
258 | char **heads = NULL; | |
7f8e9828 LT |
259 | int fd[2], ret; |
260 | pid_t pid; | |
61221472 LT |
261 | |
262 | argv++; | |
263 | for (i = 1; i < argc; i++) { | |
264 | char *arg = *argv++; | |
265 | ||
266 | if (*arg == '-') { | |
267 | if (!strncmp(arg, "--exec=", 7)) { | |
268 | exec = arg + 7; | |
269 | continue; | |
270 | } | |
271 | usage(send_pack_usage); | |
272 | } | |
273 | dest = arg; | |
274 | heads = argv; | |
275 | nr_heads = argc - i -1; | |
276 | break; | |
277 | } | |
278 | if (!dest) | |
279 | usage(send_pack_usage); | |
f7192598 | 280 | pid = git_connect(fd, dest, exec); |
7f8e9828 | 281 | if (pid < 0) |
61221472 | 282 | return 1; |
d0efc8a7 | 283 | ret = send_pack(fd[0], fd[1], nr_heads, heads); |
7f8e9828 LT |
284 | close(fd[0]); |
285 | close(fd[1]); | |
f7192598 | 286 | finish_connect(pid); |
7f8e9828 | 287 | return ret; |
61221472 | 288 | } |