1#include "cache.h"
2#include "refs.h"
3#include "pkt-line.h"
4
5static const char send_pack_usage[] = "git-send-pack [--exec=other] destination [heads]*";
6static const char *exec = "git-receive-pack";
7
8struct ref {
9 struct ref *next;
10 unsigned char old_sha1[20];
11 unsigned char new_sha1[20];
12 char name[0];
13};
14
15static 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
26static 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
37static 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");
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 }
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
64static 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
90static 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 */
105 return 0;
106}
107
108static int read_ref(const char *ref, unsigned char *sha1)
109{
110 int fd, ret;
111 char buffer[60];
112
113 fd = open(git_path("%s", ref), O_RDONLY);
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
123static 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
137static int local_ref_nr_match;
138static char **local_ref_match;
139static struct ref **local_ref_list;
140
141static 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
160static int send_pack(int in, int out, int nr_match, char **match)
161{
162 struct ref *ref_list = NULL, **last_ref = &ref_list;
163 struct ref *ref;
164 int new_refs;
165
166 /*
167 * Read all the refs from the other end
168 */
169 for (;;) {
170 unsigned char old_sha1[20];
171 static char buffer[1000];
172 char *name;
173 int len;
174
175 len = packet_read_line(in, buffer, sizeof(buffer));
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;
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
201 if (nr_match && !path_match(name, nr_match, match))
202 continue;
203
204 if (read_ref(name, new_sha1) < 0)
205 continue;
206
207 if (nr_match && !path_match(name, nr_match, match))
208 continue;
209
210 if (!memcmp(ref->old_sha1, new_sha1, 20)) {
211 fprintf(stderr, "'%s' unchanged\n", name);
212 continue;
213 }
214
215 if (!ref_newer(new_sha1, ref->old_sha1)) {
216 error("remote '%s' points to object I don't have", name);
217 continue;
218 }
219
220 /* Ok, mark it for update */
221 memcpy(ref->new_sha1, new_sha1, 20);
222 }
223
224 /*
225 * See if we have any refs that the other end didn't have
226 */
227 if (nr_match) {
228 local_ref_nr_match = nr_match;
229 local_ref_match = match;
230 local_ref_list = last_ref;
231 for_each_ref(try_to_match);
232 }
233
234 /*
235 * Finally, tell the other end!
236 */
237 new_refs = 0;
238 for (ref = ref_list; ref; ref = ref->next) {
239 char old_hex[60], *new_hex;
240 if (is_zero_sha1(ref->new_sha1))
241 continue;
242 new_refs++;
243 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
244 new_hex = sha1_to_hex(ref->new_sha1);
245 packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);
246 fprintf(stderr, "'%s': updating from %s to %s\n", ref->name, old_hex, new_hex);
247 }
248
249 packet_flush(out);
250 if (new_refs)
251 pack_objects(out, ref_list);
252 close(out);
253 return 0;
254}
255
256int main(int argc, char **argv)
257{
258 int i, nr_heads = 0;
259 char *dest = NULL;
260 char **heads = NULL;
261 int fd[2], ret;
262 pid_t pid;
263
264 argv++;
265 for (i = 1; i < argc; i++) {
266 char *arg = *argv++;
267
268 if (*arg == '-') {
269 if (!strncmp(arg, "--exec=", 7)) {
270 exec = arg + 7;
271 continue;
272 }
273 usage(send_pack_usage);
274 }
275 dest = arg;
276 heads = argv;
277 nr_heads = argc - i -1;
278 break;
279 }
280 if (!dest)
281 usage(send_pack_usage);
282 pid = git_connect(fd, dest, exec);
283 if (pid < 0)
284 return 1;
285 ret = send_pack(fd[0], fd[1], nr_heads, heads);
286 close(fd[0]);
287 close(fd[1]);
288 finish_connect(pid);
289 return ret;
290}