Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 486 lines (441 sloc) 10.952 kb
e3a5629 upload-pack.c: <sys/poll.h> includes <ctype.h> on OpenBSD 3.8
Junio C Hamano authored
1 #include <signal.h>
2 #include <sys/wait.h>
3 #include <sys/poll.h>
def88e9 Commit first cut at "git-fetch-pack"
Linus Torvalds authored
4 #include "cache.h"
5 #include "refs.h"
6 #include "pkt-line.h"
958c24b Move sideband server side support into reusable form.
Junio C Hamano authored
7 #include "sideband.h"
f6b42a8 Show peeled onion from upload-pack and server-info.
Junio C Hamano authored
8 #include "tag.h"
9 #include "object.h"
f0243f2 dscho git-upload-pack: More efficient usage of the has_sha1 array
dscho authored
10 #include "commit.h"
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
11 #include "exec_cmd.h"
def88e9 Commit first cut at "git-fetch-pack"
Linus Torvalds authored
12
960decc git-daemon: timeout, eliminate double DWIM
H. Peter Anvin authored
13 static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
def88e9 Commit first cut at "git-fetch-pack"
Linus Torvalds authored
14
f0243f2 dscho git-upload-pack: More efficient usage of the has_sha1 array
dscho authored
15 #define THEY_HAVE (1U << 0)
565ebbf upload-pack: tighten request validation.
Junio C Hamano authored
16 #define OUR_REF (1U << 1)
17 #define WANTED (1U << 2)
96f1e58 remove unnecessary initializations
David Rientjes authored
18 static int multi_ack, nr_our_refs;
19 static int use_thin_pack;
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
20 static struct object_array have_obj;
21 static struct object_array want_obj;
96f1e58 remove unnecessary initializations
David Rientjes authored
22 static unsigned int timeout;
23 static int use_sideband;
960decc git-daemon: timeout, eliminate double DWIM
H. Peter Anvin authored
24
25 static void reset_timeout(void)
26 {
27 alarm(timeout);
28 }
fb9040c Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
Linus Torvalds authored
29
75bfc6c Make git-fetch-pack actually do all the unpacking etc.
Linus Torvalds authored
30 static int strip(char *line, int len)
31 {
32 if (len && line[len-1] == '\n')
33 line[--len] = 0;
34 return len;
35 }
36
583b7ea upload-pack/fetch-pack: support side-band communication
Junio C Hamano authored
37 static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
38 {
958c24b Move sideband server side support into reusable form.
Junio C Hamano authored
39 if (use_sideband)
40 return send_sideband(1, fd, data, sz, DEFAULT_PACKET_MAX);
583b7ea upload-pack/fetch-pack: support side-band communication
Junio C Hamano authored
41
958c24b Move sideband server side support into reusable form.
Junio C Hamano authored
42 if (fd == 3)
43 /* emergency quit */
44 fd = 2;
45 if (fd == 2) {
46 xwrite(fd, data, sz);
47 return sz;
583b7ea upload-pack/fetch-pack: support side-band communication
Junio C Hamano authored
48 }
958c24b Move sideband server side support into reusable form.
Junio C Hamano authored
49 return safe_write(fd, data, sz);
583b7ea upload-pack/fetch-pack: support side-band communication
Junio C Hamano authored
50 }
51
fb9040c Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
Linus Torvalds authored
52 static void create_pack_file(void)
53 {
363b781 upload-pack: prepare for sideband message support.
Junio C Hamano authored
54 /* Pipes between rev-list to pack-objects, pack-objects to us
55 * and pack-objects error stream for progress bar.
56 */
57 int lp_pipe[2], pu_pipe[2], pe_pipe[2];
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
58 pid_t pid_rev_list, pid_pack_objects;
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
59 int create_full_pack = (nr_our_refs == want_obj.nr && !have_obj.nr);
363b781 upload-pack: prepare for sideband message support.
Junio C Hamano authored
60 char data[8193], progress[128];
583b7ea upload-pack/fetch-pack: support side-band communication
Junio C Hamano authored
61 char abort_msg[] = "aborting due to possible repository "
62 "corruption on the remote side.";
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
63 int buffered = -1;
75bfc6c Make git-fetch-pack actually do all the unpacking etc.
Linus Torvalds authored
64
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
65 if (pipe(lp_pipe) < 0)
75bfc6c Make git-fetch-pack actually do all the unpacking etc.
Linus Torvalds authored
66 die("git-upload-pack: unable to create pipe");
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
67 pid_rev_list = fork();
68 if (pid_rev_list < 0)
75bfc6c Make git-fetch-pack actually do all the unpacking etc.
Linus Torvalds authored
69 die("git-upload-pack: unable to fork git-rev-list");
70
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
71 if (!pid_rev_list) {
75bfc6c Make git-fetch-pack actually do all the unpacking etc.
Linus Torvalds authored
72 int i;
e091eb9 upload-pack: Do not choke on too many heads request.
Junio C Hamano authored
73 int args;
9201c70 Const tightening.
Junio C Hamano authored
74 const char **argv;
554fe20 tihirvon Make some strings const
tihirvon authored
75 const char **p;
e091eb9 upload-pack: Do not choke on too many heads request.
Junio C Hamano authored
76 char *buf;
77
b19696c Use thin pack transfer in "git fetch".
Junio C Hamano authored
78 if (create_full_pack) {
565ebbf upload-pack: tighten request validation.
Junio C Hamano authored
79 args = 10;
b19696c Use thin pack transfer in "git fetch".
Junio C Hamano authored
80 use_thin_pack = 0; /* no point doing it */
81 }
e091eb9 upload-pack: Do not choke on too many heads request.
Junio C Hamano authored
82 else
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
83 args = have_obj.nr + want_obj.nr + 5;
9201c70 Const tightening.
Junio C Hamano authored
84 p = xmalloc(args * sizeof(char *));
85 argv = (const char **) p;
e091eb9 upload-pack: Do not choke on too many heads request.
Junio C Hamano authored
86 buf = xmalloc(args * 45);
75bfc6c Make git-fetch-pack actually do all the unpacking etc.
Linus Torvalds authored
87
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
88 dup2(lp_pipe[1], 1);
75bfc6c Make git-fetch-pack actually do all the unpacking etc.
Linus Torvalds authored
89 close(0);
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
90 close(lp_pipe[0]);
91 close(lp_pipe[1]);
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
92 *p++ = "rev-list";
b19696c Use thin pack transfer in "git fetch".
Junio C Hamano authored
93 *p++ = use_thin_pack ? "--objects-edge" : "--objects";
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
94 if (create_full_pack)
e091eb9 upload-pack: Do not choke on too many heads request.
Junio C Hamano authored
95 *p++ = "--all";
96 else {
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
97 for (i = 0; i < want_obj.nr; i++) {
98 struct object *o = want_obj.objects[i].item;
e091eb9 upload-pack: Do not choke on too many heads request.
Junio C Hamano authored
99 *p++ = buf;
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
100 memcpy(buf, sha1_to_hex(o->sha1), 41);
e091eb9 upload-pack: Do not choke on too many heads request.
Junio C Hamano authored
101 buf += 41;
102 }
75bfc6c Make git-fetch-pack actually do all the unpacking etc.
Linus Torvalds authored
103 }
b5c367f dscho Fix cloning (memory corruption)
dscho authored
104 if (!create_full_pack)
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
105 for (i = 0; i < have_obj.nr; i++) {
106 struct object *o = have_obj.objects[i].item;
b5c367f dscho Fix cloning (memory corruption)
dscho authored
107 *p++ = buf;
108 *buf++ = '^';
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
109 memcpy(buf, sha1_to_hex(o->sha1), 41);
b5c367f dscho Fix cloning (memory corruption)
dscho authored
110 buf += 41;
111 }
75bfc6c Make git-fetch-pack actually do all the unpacking etc.
Linus Torvalds authored
112 *p++ = NULL;
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
113 execv_git_cmd(argv);
75bfc6c Make git-fetch-pack actually do all the unpacking etc.
Linus Torvalds authored
114 die("git-upload-pack: unable to exec git-rev-list");
115 }
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
116
117 if (pipe(pu_pipe) < 0)
118 die("git-upload-pack: unable to create pipe");
363b781 upload-pack: prepare for sideband message support.
Junio C Hamano authored
119 if (pipe(pe_pipe) < 0)
120 die("git-upload-pack: unable to create pipe");
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
121 pid_pack_objects = fork();
122 if (pid_pack_objects < 0) {
123 /* daemon sets things up to ignore TERM */
124 kill(pid_rev_list, SIGKILL);
125 die("git-upload-pack: unable to fork git-pack-objects");
126 }
127 if (!pid_pack_objects) {
128 dup2(lp_pipe[0], 0);
129 dup2(pu_pipe[1], 1);
363b781 upload-pack: prepare for sideband message support.
Junio C Hamano authored
130 dup2(pe_pipe[1], 2);
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
131
132 close(lp_pipe[0]);
133 close(lp_pipe[1]);
134 close(pu_pipe[0]);
135 close(pu_pipe[1]);
363b781 upload-pack: prepare for sideband message support.
Junio C Hamano authored
136 close(pe_pipe[0]);
137 close(pe_pipe[1]);
138 execl_git_cmd("pack-objects", "--stdout", "--progress", NULL);
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
139 kill(pid_rev_list, SIGKILL);
140 die("git-upload-pack: unable to exec git-pack-objects");
141 }
142
143 close(lp_pipe[0]);
144 close(lp_pipe[1]);
145
363b781 upload-pack: prepare for sideband message support.
Junio C Hamano authored
146 /* We read from pe_pipe[0] to capture stderr output for
147 * progress bar, and pu_pipe[0] to capture the pack data.
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
148 */
363b781 upload-pack: prepare for sideband message support.
Junio C Hamano authored
149 close(pe_pipe[1]);
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
150 close(pu_pipe[1]);
151
152 while (1) {
153 const char *who;
154 struct pollfd pfd[2];
155 pid_t pid;
156 int status;
157 ssize_t sz;
363b781 upload-pack: prepare for sideband message support.
Junio C Hamano authored
158 int pe, pu, pollsize;
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
159
0d516ad Matthias Lederhofer upload-pack: fix timeout in create_pack_file
matled authored
160 reset_timeout();
161
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
162 pollsize = 0;
363b781 upload-pack: prepare for sideband message support.
Junio C Hamano authored
163 pe = pu = -1;
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
164
165 if (0 <= pu_pipe[0]) {
166 pfd[pollsize].fd = pu_pipe[0];
167 pfd[pollsize].events = POLLIN;
168 pu = pollsize;
169 pollsize++;
170 }
363b781 upload-pack: prepare for sideband message support.
Junio C Hamano authored
171 if (0 <= pe_pipe[0]) {
172 pfd[pollsize].fd = pe_pipe[0];
173 pfd[pollsize].events = POLLIN;
174 pe = pollsize;
175 pollsize++;
176 }
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
177
178 if (pollsize) {
179 if (poll(pfd, pollsize, -1) < 0) {
180 if (errno != EINTR) {
181 error("poll failed, resuming: %s",
182 strerror(errno));
183 sleep(1);
184 }
185 continue;
186 }
187 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
188 /* Data ready; we keep the last byte
189 * to ourselves in case we detect
190 * broken rev-list, so that we can
191 * leave the stream corrupted. This
192 * is unfortunate -- unpack-objects
193 * would happily accept a valid pack
194 * data with trailing garbage, so
195 * appending garbage after we pass all
196 * the pack data is not good enough to
197 * signal breakage to downstream.
198 */
199 char *cp = data;
200 ssize_t outsz = 0;
201 if (0 <= buffered) {
202 *cp++ = buffered;
203 outsz++;
204 }
205 sz = read(pu_pipe[0], cp,
206 sizeof(data) - outsz);
207 if (0 < sz)
208 ;
209 else if (sz == 0) {
210 close(pu_pipe[0]);
211 pu_pipe[0] = -1;
212 }
213 else
214 goto fail;
215 sz += outsz;
216 if (1 < sz) {
217 buffered = data[sz-1] & 0xFF;
218 sz--;
219 }
220 else
221 buffered = -1;
583b7ea upload-pack/fetch-pack: support side-band communication
Junio C Hamano authored
222 sz = send_client_data(1, data, sz);
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
223 if (sz < 0)
224 goto fail;
225 }
363b781 upload-pack: prepare for sideband message support.
Junio C Hamano authored
226 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
583b7ea upload-pack/fetch-pack: support side-band communication
Junio C Hamano authored
227 /* Status ready; we ship that in the side-band
228 * or dump to the standard error.
363b781 upload-pack: prepare for sideband message support.
Junio C Hamano authored
229 */
230 sz = read(pe_pipe[0], progress,
231 sizeof(progress));
232 if (0 < sz)
583b7ea upload-pack/fetch-pack: support side-band communication
Junio C Hamano authored
233 send_client_data(2, progress, sz);
363b781 upload-pack: prepare for sideband message support.
Junio C Hamano authored
234 else if (sz == 0) {
235 close(pe_pipe[0]);
236 pe_pipe[0] = -1;
237 }
238 else
239 goto fail;
240 }
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
241 }
242
243 /* See if the children are still there */
244 if (pid_rev_list || pid_pack_objects) {
245 pid = waitpid(-1, &status, WNOHANG);
246 if (!pid)
247 continue;
248 who = ((pid == pid_rev_list) ? "git-rev-list" :
249 (pid == pid_pack_objects) ? "git-pack-objects" :
250 NULL);
251 if (!who) {
252 if (pid < 0) {
253 error("git-upload-pack: %s",
254 strerror(errno));
255 goto fail;
256 }
257 error("git-upload-pack: we weren't "
258 "waiting for %d", pid);
259 continue;
260 }
261 if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) {
262 error("git-upload-pack: %s died with error.",
263 who);
264 goto fail;
265 }
266 if (pid == pid_rev_list)
267 pid_rev_list = 0;
268 if (pid == pid_pack_objects)
269 pid_pack_objects = 0;
270 if (pid_rev_list || pid_pack_objects)
271 continue;
272 }
273
274 /* both died happily */
275 if (pollsize)
276 continue;
277
278 /* flush the data */
279 if (0 <= buffered) {
280 data[0] = buffered;
583b7ea upload-pack/fetch-pack: support side-band communication
Junio C Hamano authored
281 sz = send_client_data(1, data, 1);
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
282 if (sz < 0)
283 goto fail;
284 fprintf(stderr, "flushed.\n");
285 }
958c24b Move sideband server side support into reusable form.
Junio C Hamano authored
286 if (use_sideband)
287 packet_flush(1);
b1c71b7 upload-pack: avoid sending an incomplete pack upon failure
Junio C Hamano authored
288 return;
289 }
290 fail:
291 if (pid_pack_objects)
292 kill(pid_pack_objects, SIGKILL);
293 if (pid_rev_list)
294 kill(pid_rev_list, SIGKILL);
583b7ea upload-pack/fetch-pack: support side-band communication
Junio C Hamano authored
295 send_client_data(3, abort_msg, sizeof(abort_msg));
296 die("git-upload-pack: %s", abort_msg);
fb9040c Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
Linus Torvalds authored
297 }
298
def88e9 Commit first cut at "git-fetch-pack"
Linus Torvalds authored
299 static int got_sha1(char *hex, unsigned char *sha1)
300 {
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
301 struct object *o;
302
def88e9 Commit first cut at "git-fetch-pack"
Linus Torvalds authored
303 if (get_sha1_hex(hex, sha1))
304 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
fb9040c Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
Linus Torvalds authored
305 if (!has_sha1_file(sha1))
306 return 0;
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
307
308 o = lookup_object(sha1);
309 if (!(o && o->parsed))
310 o = parse_object(sha1);
311 if (!o)
312 die("oops (%s)", sha1_to_hex(sha1));
182a8da Merge branch 'jc/upload-pack'
Junio C Hamano authored
313 if (o->type == OBJ_COMMIT) {
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
314 struct commit_list *parents;
315 if (o->flags & THEY_HAVE)
316 return 0;
317 o->flags |= THEY_HAVE;
318 for (parents = ((struct commit*)o)->parents;
319 parents;
320 parents = parents->next)
321 parents->item->object.flags |= THEY_HAVE;
fb9040c Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
Linus Torvalds authored
322 }
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
323 add_object_array(o, NULL, &have_obj);
fb9040c Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
Linus Torvalds authored
324 return 1;
def88e9 Commit first cut at "git-fetch-pack"
Linus Torvalds authored
325 }
326
327 static int get_common_commits(void)
328 {
329 static char line[1000];
1bd8c8f dscho git-upload-pack: Support the multi_ack protocol
dscho authored
330 unsigned char sha1[20], last_sha1[20];
def88e9 Commit first cut at "git-fetch-pack"
Linus Torvalds authored
331 int len;
332
f0243f2 dscho git-upload-pack: More efficient usage of the has_sha1 array
dscho authored
333 track_object_refs = 0;
334 save_commit_buffer = 0;
335
def88e9 Commit first cut at "git-fetch-pack"
Linus Torvalds authored
336 for(;;) {
337 len = packet_read_line(0, line, sizeof(line));
960decc git-daemon: timeout, eliminate double DWIM
H. Peter Anvin authored
338 reset_timeout();
def88e9 Commit first cut at "git-fetch-pack"
Linus Torvalds authored
339
340 if (!len) {
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
341 if (have_obj.nr == 0 || multi_ack)
1bd8c8f dscho git-upload-pack: Support the multi_ack protocol
dscho authored
342 packet_write(1, "NAK\n");
def88e9 Commit first cut at "git-fetch-pack"
Linus Torvalds authored
343 continue;
344 }
75bfc6c Make git-fetch-pack actually do all the unpacking etc.
Linus Torvalds authored
345 len = strip(line, len);
def88e9 Commit first cut at "git-fetch-pack"
Linus Torvalds authored
346 if (!strncmp(line, "have ", 5)) {
1bd8c8f dscho git-upload-pack: Support the multi_ack protocol
dscho authored
347 if (got_sha1(line+5, sha1) &&
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
348 (multi_ack || have_obj.nr == 1)) {
1bd8c8f dscho git-upload-pack: Support the multi_ack protocol
dscho authored
349 packet_write(1, "ACK %s%s\n",
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
350 sha1_to_hex(sha1),
351 multi_ack ? " continue" : "");
1bd8c8f dscho git-upload-pack: Support the multi_ack protocol
dscho authored
352 if (multi_ack)
e702496 Shawn O. Pearce Convert memcpy(a,b,20) to hashcpy(a,b).
spearce authored
353 hashcpy(last_sha1, sha1);
af2d3aa Revert recent fetch-pack/upload-pack updates.
Junio C Hamano authored
354 }
def88e9 Commit first cut at "git-fetch-pack"
Linus Torvalds authored
355 continue;
356 }
357 if (!strcmp(line, "done")) {
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
358 if (have_obj.nr > 0) {
1bd8c8f dscho git-upload-pack: Support the multi_ack protocol
dscho authored
359 if (multi_ack)
360 packet_write(1, "ACK %s\n",
361 sha1_to_hex(last_sha1));
362 return 0;
363 }
def88e9 Commit first cut at "git-fetch-pack"
Linus Torvalds authored
364 packet_write(1, "NAK\n");
365 return -1;
366 }
367 die("git-upload-pack: expected SHA1 list, got '%s'", line);
368 }
369 }
370
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
371 static void receive_needs(void)
fb9040c Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
Linus Torvalds authored
372 {
373 static char line[1000];
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
374 int len;
fb9040c Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
Linus Torvalds authored
375
376 for (;;) {
565ebbf upload-pack: tighten request validation.
Junio C Hamano authored
377 struct object *o;
6ece0d3 upload-pack: use object pointer not copy of sha1 to keep track of has/ne...
Junio C Hamano authored
378 unsigned char sha1_buf[20];
fb9040c Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
Linus Torvalds authored
379 len = packet_read_line(0, line, sizeof(line));
960decc git-daemon: timeout, eliminate double DWIM
H. Peter Anvin authored
380 reset_timeout();
fb9040c Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
Linus Torvalds authored
381 if (!len)
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
382 return;
e091eb9 upload-pack: Do not choke on too many heads request.
Junio C Hamano authored
383
6ece0d3 upload-pack: use object pointer not copy of sha1 to keep track of has/ne...
Junio C Hamano authored
384 if (strncmp("want ", line, 5) ||
385 get_sha1_hex(line+5, sha1_buf))
e091eb9 upload-pack: Do not choke on too many heads request.
Junio C Hamano authored
386 die("git-upload-pack: protocol error, "
387 "expected to get sha, not '%s'", line);
1bd8c8f dscho git-upload-pack: Support the multi_ack protocol
dscho authored
388 if (strstr(line+45, "multi_ack"))
389 multi_ack = 1;
b19696c Use thin pack transfer in "git fetch".
Junio C Hamano authored
390 if (strstr(line+45, "thin-pack"))
391 use_thin_pack = 1;
583b7ea upload-pack/fetch-pack: support side-band communication
Junio C Hamano authored
392 if (strstr(line+45, "side-band"))
393 use_sideband = 1;
565ebbf upload-pack: tighten request validation.
Junio C Hamano authored
394
395 /* We have sent all our refs already, and the other end
396 * should have chosen out of them; otherwise they are
397 * asking for nonsense.
398 *
399 * Hmph. We may later want to allow "want" line that
400 * asks for something like "master~10" (symbolic)...
401 * would it make sense? I don't know.
402 */
403 o = lookup_object(sha1_buf);
404 if (!o || !(o->flags & OUR_REF))
405 die("git-upload-pack: not our ref %s", line+5);
406 if (!(o->flags & WANTED)) {
407 o->flags |= WANTED;
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
408 add_object_array(o, NULL, &want_obj);
565ebbf upload-pack: tighten request validation.
Junio C Hamano authored
409 }
fb9040c Make git-fetch-pack and git-upload-pack negotiate needs/haves fully
Linus Torvalds authored
410 }
411 }
412
def88e9 Commit first cut at "git-fetch-pack"
Linus Torvalds authored
413 static int send_ref(const char *refname, const unsigned char *sha1)
414 {
554fe20 tihirvon Make some strings const
tihirvon authored
415 static const char *capabilities = "multi_ack thin-pack side-band";
f6b42a8 Show peeled onion from upload-pack and server-info.
Junio C Hamano authored
416 struct object *o = parse_object(sha1);
417
b5b1699 cworth-gh Prevent git-upload-pack segfault if object cannot be found
cworth-gh authored
418 if (!o)
419 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1));
420
1f5881b dscho fix multi_ack.
dscho authored
421 if (capabilities)
422 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname,
423 0, capabilities);
424 else
425 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
426 capabilities = NULL;
565ebbf upload-pack: tighten request validation.
Junio C Hamano authored
427 if (!(o->flags & OUR_REF)) {
428 o->flags |= OUR_REF;
429 nr_our_refs++;
430 }
1974632 Remove TYPE_* constant macros and use object_type enums consistently.
Linus Torvalds authored
431 if (o->type == OBJ_TAG) {
9534f40 Be careful when dereferencing tags.
Junio C Hamano authored
432 o = deref_tag(o, refname, 0);
f6b42a8 Show peeled onion from upload-pack and server-info.
Junio C Hamano authored
433 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
434 }
def88e9 Commit first cut at "git-fetch-pack"
Linus Torvalds authored
435 return 0;
436 }
437
59076eb Make upload_pack void and remove conditional return.
David Rientjes authored
438 static void upload_pack(void)
def88e9 Commit first cut at "git-fetch-pack"
Linus Torvalds authored
439 {
960decc git-daemon: timeout, eliminate double DWIM
H. Peter Anvin authored
440 reset_timeout();
723c31f Add "git_path()" and "head_ref()" helper functions.
Linus Torvalds authored
441 head_ref(send_ref);
def88e9 Commit first cut at "git-fetch-pack"
Linus Torvalds authored
442 for_each_ref(send_ref);
443 packet_flush(1);
b1e9fff upload-pack: lift MAX_NEEDS and MAX_HAS limitation
Junio C Hamano authored
444 receive_needs();
59076eb Make upload_pack void and remove conditional return.
David Rientjes authored
445 if (want_obj.nr) {
446 get_common_commits();
447 create_pack_file();
448 }
def88e9 Commit first cut at "git-fetch-pack"
Linus Torvalds authored
449 }
450
451 int main(int argc, char **argv)
452 {
8d63013 Server-side support for user-relative paths.
Andreas Ericsson authored
453 char *dir;
960decc git-daemon: timeout, eliminate double DWIM
H. Peter Anvin authored
454 int i;
455 int strict = 0;
456
457 for (i = 1; i < argc; i++) {
458 char *arg = argv[i];
459
460 if (arg[0] != '-')
461 break;
462 if (!strcmp(arg, "--strict")) {
463 strict = 1;
464 continue;
465 }
466 if (!strncmp(arg, "--timeout=", 10)) {
467 timeout = atoi(arg+10);
468 continue;
469 }
470 if (!strcmp(arg, "--")) {
471 i++;
472 break;
473 }
474 }
475
476 if (i != argc-1)
def88e9 Commit first cut at "git-fetch-pack"
Linus Torvalds authored
477 usage(upload_pack_usage);
960decc git-daemon: timeout, eliminate double DWIM
H. Peter Anvin authored
478 dir = argv[i];
113b947 Make "upload-pack" match git-fetch-pack usage
Linus Torvalds authored
479
8d63013 Server-side support for user-relative paths.
Andreas Ericsson authored
480 if (!enter_repo(dir, strict))
481 die("'%s': unable to chdir or not a git archive", dir);
960decc git-daemon: timeout, eliminate double DWIM
H. Peter Anvin authored
482
def88e9 Commit first cut at "git-fetch-pack"
Linus Torvalds authored
483 upload_pack();
484 return 0;
485 }
Something went wrong with that request. Please try again.