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