blob: ac9ac1592d818da5ae90ec065581e3bf218ffc39 [file] [log] [blame]
Linus Torvaldsdef88e92005-07-04 13:26:53 -07001#include "cache.h"
2#include "refs.h"
3#include "pkt-line.h"
Junio C Hamano958c24b2006-09-10 03:20:24 -07004#include "sideband.h"
Junio C Hamanof6b42a82005-10-13 18:57:40 -07005#include "tag.h"
6#include "object.h"
Johannes Schindelinf0243f22005-10-28 04:48:32 +02007#include "commit.h"
Michal Ostrowski77cb17e2006-01-10 21:12:17 -05008#include "exec_cmd.h"
Johannes Schindelin9b8dc262006-10-30 20:08:43 +01009#include "diff.h"
10#include "revision.h"
11#include "list-objects.h"
Johannes Sixtcc41fa82007-10-19 21:47:59 +020012#include "run-command.h"
Junio C Hamano47a59182013-07-08 13:56:53 -070013#include "connect.h"
Junio C Hamano051e4002011-08-05 13:54:06 -070014#include "sigchain.h"
Jeff Kingff5effd2012-08-03 12:19:16 -040015#include "version.h"
Junio C Hamanodaebaa72013-01-18 16:08:30 -080016#include "string-list.h"
Linus Torvaldsdef88e92005-07-04 13:26:53 -070017
Štěpán Němec62b46982010-10-08 19:31:15 +020018static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=<n>] <dir>";
Linus Torvaldsdef88e92005-07-04 13:26:53 -070019
Nguyễn Thái Ngọc Duy208acbf2014-03-25 20:23:26 +070020/* Remember to update object flag allocation in object.h */
Junio C Hamano937a5152006-07-05 21:28:20 -070021#define THEY_HAVE (1u << 11)
22#define OUR_REF (1u << 12)
23#define WANTED (1u << 13)
24#define COMMON_KNOWN (1u << 14)
25#define REACHABLE (1u << 15)
26
Johannes Schindelinf53514b2006-10-30 20:09:53 +010027#define SHALLOW (1u << 16)
28#define NOT_SHALLOW (1u << 17)
29#define CLIENT_SHALLOW (1u << 18)
Junio C Hamano390eb362013-01-28 21:49:57 -080030#define HIDDEN_REF (1u << 19)
Johannes Schindelinf53514b2006-10-30 20:09:53 +010031
Junio C Hamano3fbe2d52006-11-24 02:34:27 -080032static unsigned long oldest_have;
Junio C Hamano937a5152006-07-05 21:28:20 -070033
Junio C Hamano3f1da572013-01-28 20:45:43 -080034static int multi_ack;
Junio C Hamano4e10cf92011-03-29 12:29:10 -070035static int no_done;
Shawn O. Pearce348e3902008-03-03 22:27:33 -050036static int use_thin_pack, use_ofs_delta, use_include_tag;
Johannes Sixt9462e3f2009-06-16 20:41:16 +020037static int no_progress, daemon_mode;
Junio C Hamano390eb362013-01-28 21:49:57 -080038static int allow_tip_sha1_in_want;
Nick Edelenf0cea832009-06-10 01:50:18 +020039static int shallow_nr;
Junio C Hamanob1e9fff2006-07-05 18:00:02 -070040static struct object_array have_obj;
41static struct object_array want_obj;
Nicolas Pitre65230782009-09-03 19:08:33 -040042static struct object_array extra_edge_obj;
David Rientjes96f1e582006-08-15 10:23:48 -070043static unsigned int timeout;
Jeff King115dedd2013-09-08 05:02:06 -040044static int keepalive = 5;
Junio C Hamanod47f3db2006-09-10 16:27:08 -070045/* 0 for no sideband,
46 * otherwise maximum packet size (up to 65520 bytes).
47 */
David Rientjes96f1e582006-08-15 10:23:48 -070048static int use_sideband;
Shawn O. Pearce42526b42009-10-30 17:47:33 -070049static int advertise_refs;
50static int stateless_rpc;
H. Peter Anvin960decc2005-10-19 14:27:01 -070051
52static void reset_timeout(void)
53{
54 alarm(timeout);
55}
Linus Torvaldsfb9040c2005-07-04 15:29:17 -070056
Junio C Hamano583b7ea2006-06-21 00:30:21 -070057static ssize_t send_client_data(int fd, const char *data, ssize_t sz)
58{
Junio C Hamano958c24b2006-09-10 03:20:24 -070059 if (use_sideband)
Junio C Hamanod47f3db2006-09-10 16:27:08 -070060 return send_sideband(1, fd, data, sz, use_sideband);
Junio C Hamano958c24b2006-09-10 03:20:24 -070061 if (fd == 3)
62 /* emergency quit */
63 fd = 2;
64 if (fd == 2) {
Andy Whitcroft93822c22007-01-08 15:58:23 +000065 /* XXX: are we happy to lose stuff here? */
Junio C Hamano958c24b2006-09-10 03:20:24 -070066 xwrite(fd, data, sz);
67 return sz;
Junio C Hamano583b7ea2006-06-21 00:30:21 -070068 }
Jeff Kingcdf4fb82013-02-20 15:01:56 -050069 write_or_die(fd, data, sz);
70 return sz;
Junio C Hamano583b7ea2006-06-21 00:30:21 -070071}
72
Nguyễn Thái Ngọc Duyb790e0f2014-03-11 19:59:46 +070073static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
74{
75 FILE *fp = cb_data;
76 if (graft->nr_parent == -1)
77 fprintf(fp, "--shallow %s\n", sha1_to_hex(graft->sha1));
78 return 0;
79}
80
Linus Torvaldsfb9040c2005-07-04 15:29:17 -070081static void create_pack_file(void)
82{
René Scharfed3180272014-08-19 21:09:35 +020083 struct child_process pack_objects = CHILD_PROCESS_INIT;
Junio C Hamano363b7812006-06-20 22:48:23 -070084 char data[8193], progress[128];
Junio C Hamano583b7ea2006-06-21 00:30:21 -070085 char abort_msg[] = "aborting due to possible repository "
86 "corruption on the remote side.";
Junio C Hamanob1c71b72006-06-20 18:26:34 -070087 int buffered = -1;
Junio C Hamano1456b042009-12-10 12:17:11 -080088 ssize_t sz;
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +070089 const char *argv[12];
90 int i, arg = 0;
91 FILE *pipe_fd;
Linus Torvalds75bfc6c2005-07-04 16:35:13 -070092
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +070093 if (shallow_nr) {
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +070094 argv[arg++] = "--shallow-file";
Nguyễn Thái Ngọc Duyb790e0f2014-03-11 19:59:46 +070095 argv[arg++] = "";
Nick Edelenf0cea832009-06-10 01:50:18 +020096 }
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +070097 argv[arg++] = "pack-objects";
98 argv[arg++] = "--revs";
99 if (use_thin_pack)
100 argv[arg++] = "--thin";
Linus Torvalds75bfc6c2005-07-04 16:35:13 -0700101
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200102 argv[arg++] = "--stdout";
103 if (!no_progress)
104 argv[arg++] = "--progress";
105 if (use_ofs_delta)
106 argv[arg++] = "--delta-base-offset";
Shawn O. Pearce348e3902008-03-03 22:27:33 -0500107 if (use_include_tag)
108 argv[arg++] = "--include-tag";
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200109 argv[arg++] = NULL;
110
Jeff Kingb9612192011-04-06 17:33:33 -0400111 pack_objects.in = -1;
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200112 pack_objects.out = -1;
113 pack_objects.err = -1;
114 pack_objects.git_cmd = 1;
115 pack_objects.argv = argv;
Johannes Sixt21edd3f2007-10-19 21:48:03 +0200116
Johannes Sixt4c324c02007-11-04 20:46:48 +0100117 if (start_command(&pack_objects))
Junio C Hamano7e44c932008-08-31 09:39:19 -0700118 die("git upload-pack: unable to fork git-pack-objects");
Johannes Schindelin83a5ad62007-02-20 03:01:44 +0100119
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700120 pipe_fd = xfdopen(pack_objects.in, "w");
Nick Edelenf0cea832009-06-10 01:50:18 +0200121
Nguyễn Thái Ngọc Duyb790e0f2014-03-11 19:59:46 +0700122 if (shallow_nr)
123 for_each_commit_graft(write_one_shallow, pipe_fd);
124
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700125 for (i = 0; i < want_obj.nr; i++)
126 fprintf(pipe_fd, "%s\n",
127 sha1_to_hex(want_obj.objects[i].item->sha1));
128 fprintf(pipe_fd, "--not\n");
129 for (i = 0; i < have_obj.nr; i++)
130 fprintf(pipe_fd, "%s\n",
131 sha1_to_hex(have_obj.objects[i].item->sha1));
132 for (i = 0; i < extra_edge_obj.nr; i++)
133 fprintf(pipe_fd, "%s\n",
134 sha1_to_hex(extra_edge_obj.objects[i].item->sha1));
135 fprintf(pipe_fd, "\n");
136 fflush(pipe_fd);
137 fclose(pipe_fd);
Nick Edelenf0cea832009-06-10 01:50:18 +0200138
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200139 /* We read from pack_objects.err to capture stderr output for
140 * progress bar, and pack_objects.out to capture the pack data.
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700141 */
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700142
143 while (1) {
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700144 struct pollfd pfd[2];
Junio C Hamano363b7812006-06-20 22:48:23 -0700145 int pe, pu, pollsize;
Jeff King05e95152013-09-08 05:01:31 -0400146 int ret;
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700147
Matthias Lederhofer0d516ad2006-07-18 19:14:51 +0200148 reset_timeout();
149
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700150 pollsize = 0;
Junio C Hamano363b7812006-06-20 22:48:23 -0700151 pe = pu = -1;
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700152
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200153 if (0 <= pack_objects.out) {
154 pfd[pollsize].fd = pack_objects.out;
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700155 pfd[pollsize].events = POLLIN;
156 pu = pollsize;
157 pollsize++;
158 }
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200159 if (0 <= pack_objects.err) {
160 pfd[pollsize].fd = pack_objects.err;
Junio C Hamano363b7812006-06-20 22:48:23 -0700161 pfd[pollsize].events = POLLIN;
162 pe = pollsize;
163 pollsize++;
164 }
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700165
Johannes Sixt4c324c02007-11-04 20:46:48 +0100166 if (!pollsize)
167 break;
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700168
Edward Thomson6c71f8b2014-08-22 15:19:11 +0000169 ret = poll(pfd, pollsize,
170 keepalive < 0 ? -1 : 1000 * keepalive);
171
Jeff King05e95152013-09-08 05:01:31 -0400172 if (ret < 0) {
Johannes Sixt4c324c02007-11-04 20:46:48 +0100173 if (errno != EINTR) {
174 error("poll failed, resuming: %s",
175 strerror(errno));
176 sleep(1);
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700177 }
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700178 continue;
Johannes Sixt4c324c02007-11-04 20:46:48 +0100179 }
Nicolas Pitre6b59f512009-11-11 17:24:42 -0500180 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
181 /* Status ready; we ship that in the side-band
182 * or dump to the standard error.
183 */
184 sz = xread(pack_objects.err, progress,
185 sizeof(progress));
186 if (0 < sz)
187 send_client_data(2, progress, sz);
188 else if (sz == 0) {
189 close(pack_objects.err);
190 pack_objects.err = -1;
191 }
192 else
193 goto fail;
194 /* give priority to status messages */
195 continue;
196 }
Johannes Sixt4c324c02007-11-04 20:46:48 +0100197 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
198 /* Data ready; we keep the last byte to ourselves
199 * in case we detect broken rev-list, so that we
200 * can leave the stream corrupted. This is
201 * unfortunate -- unpack-objects would happily
202 * accept a valid packdata with trailing garbage,
203 * so appending garbage after we pass all the
204 * pack data is not good enough to signal
205 * breakage to downstream.
206 */
207 char *cp = data;
208 ssize_t outsz = 0;
209 if (0 <= buffered) {
210 *cp++ = buffered;
211 outsz++;
212 }
213 sz = xread(pack_objects.out, cp,
214 sizeof(data) - outsz);
215 if (0 < sz)
Junio C Hamano1456b042009-12-10 12:17:11 -0800216 ;
Johannes Sixt4c324c02007-11-04 20:46:48 +0100217 else if (sz == 0) {
218 close(pack_objects.out);
219 pack_objects.out = -1;
220 }
221 else
222 goto fail;
223 sz += outsz;
224 if (1 < sz) {
225 buffered = data[sz-1] & 0xFF;
226 sz--;
227 }
228 else
229 buffered = -1;
230 sz = send_client_data(1, data, sz);
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700231 if (sz < 0)
232 goto fail;
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700233 }
Jeff King05e95152013-09-08 05:01:31 -0400234
235 /*
236 * We hit the keepalive timeout without saying anything; send
237 * an empty message on the data sideband just to let the other
238 * side know we're still working on it, but don't have any data
239 * yet.
240 *
241 * If we don't have a sideband channel, there's no room in the
242 * protocol to say anything, so those clients are just out of
243 * luck.
244 */
245 if (!ret && use_sideband) {
246 static const char buf[] = "0005\1";
247 write_or_die(1, buf, 5);
248 }
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700249 }
Johannes Sixt4c324c02007-11-04 20:46:48 +0100250
251 if (finish_command(&pack_objects)) {
Junio C Hamano7e44c932008-08-31 09:39:19 -0700252 error("git upload-pack: git-pack-objects died with error.");
Johannes Sixt4c324c02007-11-04 20:46:48 +0100253 goto fail;
254 }
Johannes Sixt4c324c02007-11-04 20:46:48 +0100255
256 /* flush the data */
257 if (0 <= buffered) {
258 data[0] = buffered;
259 sz = send_client_data(1, data, 1);
260 if (sz < 0)
261 goto fail;
262 fprintf(stderr, "flushed.\n");
263 }
264 if (use_sideband)
265 packet_flush(1);
266 return;
267
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700268 fail:
Junio C Hamano583b7ea2006-06-21 00:30:21 -0700269 send_client_data(3, abort_msg, sizeof(abort_msg));
Junio C Hamano7e44c932008-08-31 09:39:19 -0700270 die("git upload-pack: %s", abort_msg);
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700271}
272
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700273static int got_sha1(char *hex, unsigned char *sha1)
274{
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700275 struct object *o;
Junio C Hamano937a5152006-07-05 21:28:20 -0700276 int we_knew_they_have = 0;
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700277
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700278 if (get_sha1_hex(hex, sha1))
Junio C Hamano7e44c932008-08-31 09:39:19 -0700279 die("git upload-pack: expected SHA1 object, got '%s'", hex);
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700280 if (!has_sha1_file(sha1))
Junio C Hamano937a5152006-07-05 21:28:20 -0700281 return -1;
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700282
Jeff Kinga6eec122013-03-16 06:25:25 -0400283 o = parse_object(sha1);
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700284 if (!o)
285 die("oops (%s)", sha1_to_hex(sha1));
Junio C Hamano182a8da2006-08-12 22:16:51 -0700286 if (o->type == OBJ_COMMIT) {
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700287 struct commit_list *parents;
Junio C Hamano937a5152006-07-05 21:28:20 -0700288 struct commit *commit = (struct commit *)o;
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700289 if (o->flags & THEY_HAVE)
Junio C Hamano937a5152006-07-05 21:28:20 -0700290 we_knew_they_have = 1;
291 else
292 o->flags |= THEY_HAVE;
293 if (!oldest_have || (commit->date < oldest_have))
294 oldest_have = commit->date;
295 for (parents = commit->parents;
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700296 parents;
297 parents = parents->next)
298 parents->item->object.flags |= THEY_HAVE;
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700299 }
Junio C Hamano937a5152006-07-05 21:28:20 -0700300 if (!we_knew_they_have) {
301 add_object_array(o, NULL, &have_obj);
302 return 1;
303 }
304 return 0;
305}
306
307static int reachable(struct commit *want)
308{
309 struct commit_list *work = NULL;
310
Thiago Farina47e44ed2010-11-26 23:58:14 -0200311 commit_list_insert_by_date(want, &work);
Junio C Hamano937a5152006-07-05 21:28:20 -0700312 while (work) {
313 struct commit_list *list = work->next;
314 struct commit *commit = work->item;
315 free(work);
316 work = list;
317
318 if (commit->object.flags & THEY_HAVE) {
319 want->object.flags |= COMMON_KNOWN;
320 break;
321 }
322 if (!commit->object.parsed)
323 parse_object(commit->object.sha1);
324 if (commit->object.flags & REACHABLE)
325 continue;
326 commit->object.flags |= REACHABLE;
327 if (commit->date < oldest_have)
328 continue;
329 for (list = commit->parents; list; list = list->next) {
330 struct commit *parent = list->item;
331 if (!(parent->object.flags & REACHABLE))
Thiago Farina47e44ed2010-11-26 23:58:14 -0200332 commit_list_insert_by_date(parent, &work);
Junio C Hamano937a5152006-07-05 21:28:20 -0700333 }
334 }
335 want->object.flags |= REACHABLE;
336 clear_commit_marks(want, REACHABLE);
337 free_commit_list(work);
338 return (want->object.flags & COMMON_KNOWN);
339}
340
341static int ok_to_give_up(void)
342{
343 int i;
344
345 if (!have_obj.nr)
346 return 0;
347
348 for (i = 0; i < want_obj.nr; i++) {
349 struct object *want = want_obj.objects[i].item;
350
351 if (want->flags & COMMON_KNOWN)
352 continue;
353 want = deref_tag(want, "a want line", 0);
354 if (!want || want->type != OBJ_COMMIT) {
355 /* no way to tell if this is reachable by
356 * looking at the ancestry chain alone, so
357 * leave a note to ourselves not to worry about
358 * this object anymore.
359 */
360 want_obj.objects[i].item->flags |= COMMON_KNOWN;
361 continue;
362 }
363 if (!reachable((struct commit *)want))
364 return 0;
365 }
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700366 return 1;
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700367}
368
369static int get_common_commits(void)
370{
Junio C Hamanoc04c4e52006-07-05 18:12:12 -0700371 unsigned char sha1[20];
Shawn O. Pearce78affc42009-10-30 17:47:25 -0700372 char last_hex[41];
Shawn O. Pearce49bee712011-03-14 16:48:39 -0700373 int got_common = 0;
374 int got_other = 0;
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700375 int sent_ready = 0;
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700376
Johannes Schindelinf0243f22005-10-28 04:48:32 +0200377 save_commit_buffer = 0;
378
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400379 for (;;) {
Jeff King74543a02013-02-20 15:02:57 -0500380 char *line = packet_read_line(0, NULL);
H. Peter Anvin960decc2005-10-19 14:27:01 -0700381 reset_timeout();
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700382
Jeff King74543a02013-02-20 15:02:57 -0500383 if (!line) {
Shawn O. Pearce49bee712011-03-14 16:48:39 -0700384 if (multi_ack == 2 && got_common
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700385 && !got_other && ok_to_give_up()) {
386 sent_ready = 1;
Shawn O. Pearce49bee712011-03-14 16:48:39 -0700387 packet_write(1, "ACK %s ready\n", last_hex);
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700388 }
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700389 if (have_obj.nr == 0 || multi_ack)
Johannes Schindelin1bd8c8f2005-10-28 04:49:16 +0200390 packet_write(1, "NAK\n");
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700391
392 if (no_done && sent_ready) {
393 packet_write(1, "ACK %s\n", last_hex);
394 return 0;
395 }
Shawn O. Pearce42526b42009-10-30 17:47:33 -0700396 if (stateless_rpc)
397 exit(0);
Shawn O. Pearce49bee712011-03-14 16:48:39 -0700398 got_common = 0;
399 got_other = 0;
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700400 continue;
401 }
Christian Couder59556542013-11-30 21:55:40 +0100402 if (starts_with(line, "have ")) {
Junio C Hamano937a5152006-07-05 21:28:20 -0700403 switch (got_sha1(line+5, sha1)) {
404 case -1: /* they have what we do not */
Shawn O. Pearce49bee712011-03-14 16:48:39 -0700405 got_other = 1;
Shawn O. Pearce78affc42009-10-30 17:47:25 -0700406 if (multi_ack && ok_to_give_up()) {
407 const char *hex = sha1_to_hex(sha1);
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700408 if (multi_ack == 2) {
409 sent_ready = 1;
Shawn O. Pearce78affc42009-10-30 17:47:25 -0700410 packet_write(1, "ACK %s ready\n", hex);
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700411 } else
Shawn O. Pearce78affc42009-10-30 17:47:25 -0700412 packet_write(1, "ACK %s continue\n", hex);
413 }
Junio C Hamano937a5152006-07-05 21:28:20 -0700414 break;
415 default:
Shawn O. Pearce49bee712011-03-14 16:48:39 -0700416 got_common = 1;
Shawn O. Pearce78affc42009-10-30 17:47:25 -0700417 memcpy(last_hex, sha1_to_hex(sha1), 41);
418 if (multi_ack == 2)
419 packet_write(1, "ACK %s common\n", last_hex);
420 else if (multi_ack)
421 packet_write(1, "ACK %s continue\n", last_hex);
Junio C Hamanoc04c4e52006-07-05 18:12:12 -0700422 else if (have_obj.nr == 1)
Shawn O. Pearce78affc42009-10-30 17:47:25 -0700423 packet_write(1, "ACK %s\n", last_hex);
Junio C Hamano937a5152006-07-05 21:28:20 -0700424 break;
Junio C Hamanoaf2d3aa2005-10-25 14:55:24 -0700425 }
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700426 continue;
427 }
428 if (!strcmp(line, "done")) {
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700429 if (have_obj.nr > 0) {
Johannes Schindelin1bd8c8f2005-10-28 04:49:16 +0200430 if (multi_ack)
Junio C Hamanoc04c4e52006-07-05 18:12:12 -0700431 packet_write(1, "ACK %s\n", last_hex);
Johannes Schindelin1bd8c8f2005-10-28 04:49:16 +0200432 return 0;
433 }
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700434 packet_write(1, "NAK\n");
435 return -1;
436 }
Junio C Hamano7e44c932008-08-31 09:39:19 -0700437 die("git upload-pack: expected SHA1 list, got '%s'", line);
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700438 }
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700439}
440
Junio C Hamano390eb362013-01-28 21:49:57 -0800441static int is_our_ref(struct object *o)
442{
443 return o->flags &
444 ((allow_tip_sha1_in_want ? HIDDEN_REF : 0) | OUR_REF);
445}
446
Junio C Hamano051e4002011-08-05 13:54:06 -0700447static void check_non_tip(void)
448{
449 static const char *argv[] = {
450 "rev-list", "--stdin", NULL,
451 };
René Scharfed3180272014-08-19 21:09:35 +0200452 static struct child_process cmd = CHILD_PROCESS_INIT;
Junio C Hamano051e4002011-08-05 13:54:06 -0700453 struct object *o;
454 char namebuf[42]; /* ^ + SHA-1 + LF */
455 int i;
456
457 /* In the normal in-process case non-tip request can never happen */
458 if (!stateless_rpc)
459 goto error;
460
461 cmd.argv = argv;
462 cmd.git_cmd = 1;
463 cmd.no_stderr = 1;
464 cmd.in = -1;
465 cmd.out = -1;
466
467 if (start_command(&cmd))
468 goto error;
469
470 /*
471 * If rev-list --stdin encounters an unknown commit, it
472 * terminates, which will cause SIGPIPE in the write loop
473 * below.
474 */
475 sigchain_push(SIGPIPE, SIG_IGN);
476
477 namebuf[0] = '^';
478 namebuf[41] = '\n';
479 for (i = get_max_object_index(); 0 < i; ) {
480 o = get_indexed_object(--i);
Brian Harring2a745322011-08-23 22:47:17 -0700481 if (!o)
482 continue;
Junio C Hamano390eb362013-01-28 21:49:57 -0800483 if (!is_our_ref(o))
Junio C Hamano051e4002011-08-05 13:54:06 -0700484 continue;
485 memcpy(namebuf + 1, sha1_to_hex(o->sha1), 40);
486 if (write_in_full(cmd.in, namebuf, 42) < 0)
487 goto error;
488 }
489 namebuf[40] = '\n';
490 for (i = 0; i < want_obj.nr; i++) {
491 o = want_obj.objects[i].item;
Junio C Hamano390eb362013-01-28 21:49:57 -0800492 if (is_our_ref(o))
Junio C Hamano051e4002011-08-05 13:54:06 -0700493 continue;
494 memcpy(namebuf, sha1_to_hex(o->sha1), 40);
495 if (write_in_full(cmd.in, namebuf, 41) < 0)
496 goto error;
497 }
498 close(cmd.in);
499
500 sigchain_pop(SIGPIPE);
501
502 /*
503 * The commits out of the rev-list are not ancestors of
504 * our ref.
505 */
506 i = read_in_full(cmd.out, namebuf, 1);
507 if (i)
508 goto error;
509 close(cmd.out);
510
511 /*
512 * rev-list may have died by encountering a bad commit
513 * in the history, in which case we do want to bail out
514 * even when it showed no commit.
515 */
516 if (finish_command(&cmd))
517 goto error;
518
519 /* All the non-tip ones are ancestors of what we advertised */
520 return;
521
522error:
523 /* Pick one of them (we know there at least is one) */
524 for (i = 0; i < want_obj.nr; i++) {
525 o = want_obj.objects[i].item;
Junio C Hamano390eb362013-01-28 21:49:57 -0800526 if (!is_our_ref(o))
Junio C Hamano051e4002011-08-05 13:54:06 -0700527 die("git upload-pack: not our ref %s",
528 sha1_to_hex(o->sha1));
529 }
530}
531
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700532static void receive_needs(void)
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700533{
Thiago Farina3cd47452010-08-28 23:04:17 -0300534 struct object_array shallows = OBJECT_ARRAY_INIT;
Jeff King74543a02013-02-20 15:02:57 -0500535 int depth = 0;
Junio C Hamano051e4002011-08-05 13:54:06 -0700536 int has_non_tip = 0;
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700537
Nick Edelenf0cea832009-06-10 01:50:18 +0200538 shallow_nr = 0;
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700539 for (;;) {
Junio C Hamano565ebbf2005-10-24 18:59:18 -0700540 struct object *o;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100541 const char *features;
Junio C Hamano6ece0d32006-07-05 17:41:39 -0700542 unsigned char sha1_buf[20];
Jeff King74543a02013-02-20 15:02:57 -0500543 char *line = packet_read_line(0, NULL);
H. Peter Anvin960decc2005-10-19 14:27:01 -0700544 reset_timeout();
Jeff King74543a02013-02-20 15:02:57 -0500545 if (!line)
Johannes Schindelined09aef2006-10-30 20:09:06 +0100546 break;
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700547
Christian Couder59556542013-11-30 21:55:40 +0100548 if (starts_with(line, "shallow ")) {
Johannes Schindelined09aef2006-10-30 20:09:06 +0100549 unsigned char sha1[20];
550 struct object *object;
Jeff Kingb7b02172013-02-20 14:53:33 -0500551 if (get_sha1_hex(line + 8, sha1))
Johannes Schindelined09aef2006-10-30 20:09:06 +0100552 die("invalid shallow line: %s", line);
553 object = parse_object(sha1);
554 if (!object)
Michael Heemskerkaf04fa22013-04-28 22:32:04 -0700555 continue;
Nguyễn Thái Ngọc Duy6293ded2013-01-08 18:32:36 +0700556 if (object->type != OBJ_COMMIT)
557 die("invalid shallow object %s", sha1_to_hex(sha1));
Jeff Kinge58e57e2013-02-20 14:54:57 -0500558 if (!(object->flags & CLIENT_SHALLOW)) {
559 object->flags |= CLIENT_SHALLOW;
560 add_object_array(object, NULL, &shallows);
561 }
Johannes Schindelined09aef2006-10-30 20:09:06 +0100562 continue;
563 }
Christian Couder59556542013-11-30 21:55:40 +0100564 if (starts_with(line, "deepen ")) {
Johannes Schindelin016e6cc2006-10-30 20:09:29 +0100565 char *end;
566 depth = strtol(line + 7, &end, 0);
567 if (end == line + 7 || depth <= 0)
568 die("Invalid deepen: %s", line);
569 continue;
570 }
Christian Couder59556542013-11-30 21:55:40 +0100571 if (!starts_with(line, "want ") ||
Junio C Hamano6ece0d32006-07-05 17:41:39 -0700572 get_sha1_hex(line+5, sha1_buf))
Junio C Hamano7e44c932008-08-31 09:39:19 -0700573 die("git upload-pack: protocol error, "
Junio C Hamanoe091eb92005-10-05 14:49:54 -0700574 "expected to get sha, not '%s'", line);
Junio C Hamanof47182c2012-01-08 22:06:19 +0100575
576 features = line + 45;
577
578 if (parse_feature_request(features, "multi_ack_detailed"))
Shawn O. Pearce78affc42009-10-30 17:47:25 -0700579 multi_ack = 2;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100580 else if (parse_feature_request(features, "multi_ack"))
Johannes Schindelin1bd8c8f2005-10-28 04:49:16 +0200581 multi_ack = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100582 if (parse_feature_request(features, "no-done"))
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700583 no_done = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100584 if (parse_feature_request(features, "thin-pack"))
Junio C Hamanob19696c2006-02-20 00:38:39 -0800585 use_thin_pack = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100586 if (parse_feature_request(features, "ofs-delta"))
Nicolas Pitree4fe4b82006-09-26 11:27:39 -0400587 use_ofs_delta = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100588 if (parse_feature_request(features, "side-band-64k"))
Junio C Hamanod47f3db2006-09-10 16:27:08 -0700589 use_sideband = LARGE_PACKET_MAX;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100590 else if (parse_feature_request(features, "side-band"))
Junio C Hamanod47f3db2006-09-10 16:27:08 -0700591 use_sideband = DEFAULT_PACKET_MAX;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100592 if (parse_feature_request(features, "no-progress"))
Johannes Schindelinb0e90892007-02-23 20:03:10 +0100593 no_progress = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100594 if (parse_feature_request(features, "include-tag"))
Shawn O. Pearce348e3902008-03-03 22:27:33 -0500595 use_include_tag = 1;
Junio C Hamano565ebbf2005-10-24 18:59:18 -0700596
Jeff Kingf59de5d2013-03-16 06:28:30 -0400597 o = parse_object(sha1_buf);
Junio C Hamano051e4002011-08-05 13:54:06 -0700598 if (!o)
Elijah Newren9f9aa762010-07-31 14:11:46 -0600599 die("git upload-pack: not our ref %s",
600 sha1_to_hex(sha1_buf));
Junio C Hamano565ebbf2005-10-24 18:59:18 -0700601 if (!(o->flags & WANTED)) {
602 o->flags |= WANTED;
Junio C Hamano390eb362013-01-28 21:49:57 -0800603 if (!is_our_ref(o))
Junio C Hamano051e4002011-08-05 13:54:06 -0700604 has_non_tip = 1;
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700605 add_object_array(o, NULL, &want_obj);
Junio C Hamano565ebbf2005-10-24 18:59:18 -0700606 }
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700607 }
Johannes Sixt9462e3f2009-06-16 20:41:16 +0200608
Junio C Hamano051e4002011-08-05 13:54:06 -0700609 /*
610 * We have sent all our refs already, and the other end
611 * should have chosen out of them. When we are operating
612 * in the stateless RPC mode, however, their choice may
613 * have been based on the set of older refs advertised
614 * by another process that handled the initial request.
615 */
616 if (has_non_tip)
617 check_non_tip();
618
Johannes Sixt9462e3f2009-06-16 20:41:16 +0200619 if (!use_sideband && daemon_mode)
620 no_progress = 1;
621
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100622 if (depth == 0 && shallows.nr == 0)
623 return;
Johannes Schindelin016e6cc2006-10-30 20:09:29 +0100624 if (depth > 0) {
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +0700625 struct commit_list *result = NULL, *backup = NULL;
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100626 int i;
Nguyễn Thái Ngọc Duy79d3a232013-12-05 20:02:41 +0700627 if (depth == INFINITE_DEPTH && !is_repository_shallow())
Nguyễn Thái Ngọc Duy4dcb1672013-01-11 16:05:46 +0700628 for (i = 0; i < shallows.nr; i++) {
629 struct object *object = shallows.objects[i].item;
630 object->flags |= NOT_SHALLOW;
631 }
632 else
633 backup = result =
634 get_shallow_commits(&want_obj, depth,
635 SHALLOW, NOT_SHALLOW);
Johannes Schindelin016e6cc2006-10-30 20:09:29 +0100636 while (result) {
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100637 struct object *object = &result->item->object;
Alexandre Julliard1f2de762006-11-24 15:58:25 +0100638 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100639 packet_write(1, "shallow %s",
640 sha1_to_hex(object->sha1));
641 register_shallow(object->sha1);
Nick Edelenf0cea832009-06-10 01:50:18 +0200642 shallow_nr++;
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100643 }
Johannes Schindelin016e6cc2006-10-30 20:09:29 +0100644 result = result->next;
645 }
646 free_commit_list(backup);
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100647 for (i = 0; i < shallows.nr; i++) {
648 struct object *object = shallows.objects[i].item;
649 if (object->flags & NOT_SHALLOW) {
650 struct commit_list *parents;
651 packet_write(1, "unshallow %s",
652 sha1_to_hex(object->sha1));
653 object->flags &= ~CLIENT_SHALLOW;
654 /* make sure the real parents are parsed */
655 unregister_shallow(object->sha1);
Junio C Hamano176d45c2006-11-13 22:47:46 -0800656 object->parsed = 0;
Jeff King367068e2013-10-24 04:54:01 -0400657 parse_commit_or_die((struct commit *)object);
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100658 parents = ((struct commit *)object)->parents;
659 while (parents) {
660 add_object_array(&parents->item->object,
661 NULL, &want_obj);
662 parents = parents->next;
663 }
Nicolas Pitre65230782009-09-03 19:08:33 -0400664 add_object_array(object, NULL, &extra_edge_obj);
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100665 }
666 /* make sure commit traversal conforms to client */
667 register_shallow(object->sha1);
668 }
669 packet_flush(1);
670 } else
671 if (shallows.nr > 0) {
672 int i;
673 for (i = 0; i < shallows.nr; i++)
674 register_shallow(shallows.objects[i].item->sha1);
675 }
Nick Edelenf0cea832009-06-10 01:50:18 +0200676
677 shallow_nr += shallows.nr;
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100678 free(shallows.objects);
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700679}
680
Junio C Hamanodaebaa72013-01-18 16:08:30 -0800681/* return non-zero if the ref is hidden, otherwise 0 */
Junio C Hamanocbbe50d2013-01-18 15:48:49 -0800682static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
683{
684 struct object *o = lookup_unknown_object(sha1);
Junio C Hamanodaebaa72013-01-18 16:08:30 -0800685
Junio C Hamano390eb362013-01-28 21:49:57 -0800686 if (ref_is_hidden(refname)) {
687 o->flags |= HIDDEN_REF;
Junio C Hamanodaebaa72013-01-18 16:08:30 -0800688 return 1;
Junio C Hamano390eb362013-01-28 21:49:57 -0800689 }
Junio C Hamanocbbe50d2013-01-18 15:48:49 -0800690 if (!o)
691 die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1));
Junio C Hamano3f1da572013-01-28 20:45:43 -0800692 o->flags |= OUR_REF;
Junio C Hamanocbbe50d2013-01-18 15:48:49 -0800693 return 0;
694}
695
Junio C Hamano7171d8c2013-09-17 16:17:33 -0700696static void format_symref_info(struct strbuf *buf, struct string_list *symref)
697{
698 struct string_list_item *item;
699
700 if (!symref->nr)
701 return;
702 for_each_string_list_item(item, symref)
703 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
704}
705
Junio C Hamano8da19772006-09-20 22:02:01 -0700706static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700707{
Johannes Schindelined09aef2006-10-30 20:09:06 +0100708 static const char *capabilities = "multi_ack thin-pack side-band"
Shawn O. Pearce348e3902008-03-03 22:27:33 -0500709 " side-band-64k ofs-delta shallow no-progress"
Shawn O. Pearce78affc42009-10-30 17:47:25 -0700710 " include-tag multi_ack_detailed";
Josh Triplett6b01ecf2011-07-08 16:13:32 -0700711 const char *refname_nons = strip_namespace(refname);
Jeff King435c8332012-10-04 04:03:33 -0400712 unsigned char peeled[20];
Carl Worthb5b16992006-02-17 16:14:52 -0800713
Junio C Hamanoa4d695d2013-09-17 16:03:32 -0700714 if (mark_our_ref(refname, sha1, flag, NULL))
Junio C Hamanodaebaa72013-01-18 16:08:30 -0800715 return 0;
Junio C Hamanocbbe50d2013-01-18 15:48:49 -0800716
Junio C Hamano7171d8c2013-09-17 16:17:33 -0700717 if (capabilities) {
718 struct strbuf symref_info = STRBUF_INIT;
719
720 format_symref_info(&symref_info, cb_data);
721 packet_write(1, "%s %s%c%s%s%s%s agent=%s\n",
Jeff Kingff5effd2012-08-03 12:19:16 -0400722 sha1_to_hex(sha1), refname_nons,
Junio C Hamanocf2ad8e2011-03-29 10:24:59 -0700723 0, capabilities,
Junio C Hamano390eb362013-01-28 21:49:57 -0800724 allow_tip_sha1_in_want ? " allow-tip-sha1-in-want" : "",
Jeff Kingff5effd2012-08-03 12:19:16 -0400725 stateless_rpc ? " no-done" : "",
Junio C Hamano7171d8c2013-09-17 16:17:33 -0700726 symref_info.buf,
Jeff Kingff5effd2012-08-03 12:19:16 -0400727 git_user_agent_sanitized());
Junio C Hamano7171d8c2013-09-17 16:17:33 -0700728 strbuf_release(&symref_info);
729 } else {
Josh Triplett6b01ecf2011-07-08 16:13:32 -0700730 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons);
Junio C Hamano7171d8c2013-09-17 16:17:33 -0700731 }
Johannes Schindelin1f5881b2005-10-28 05:56:41 +0200732 capabilities = NULL;
Jeff King435c8332012-10-04 04:03:33 -0400733 if (!peel_ref(refname, peeled))
734 packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons);
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700735 return 0;
736}
737
Junio C Hamano7171d8c2013-09-17 16:17:33 -0700738static int find_symref(const char *refname, const unsigned char *sha1, int flag,
739 void *cb_data)
740{
741 const char *symref_target;
742 struct string_list_item *item;
743 unsigned char unused[20];
744
745 if ((flag & REF_ISSYMREF) == 0)
746 return 0;
Ronnie Sahlberg7695d112014-07-15 12:59:36 -0700747 symref_target = resolve_ref_unsafe(refname, 0, unused, &flag);
Junio C Hamano7171d8c2013-09-17 16:17:33 -0700748 if (!symref_target || (flag & REF_ISSYMREF) == 0)
749 die("'%s' is a symref but it is not?", refname);
750 item = string_list_append(cb_data, refname);
751 item->util = xstrdup(symref_target);
752 return 0;
753}
754
David Rientjes59076eb2006-08-14 13:40:51 -0700755static void upload_pack(void)
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700756{
Junio C Hamano7171d8c2013-09-17 16:17:33 -0700757 struct string_list symref = STRING_LIST_INIT_DUP;
758
759 head_ref_namespaced(find_symref, &symref);
760
Shawn O. Pearce42526b42009-10-30 17:47:33 -0700761 if (advertise_refs || !stateless_rpc) {
762 reset_timeout();
Junio C Hamano7171d8c2013-09-17 16:17:33 -0700763 head_ref_namespaced(send_ref, &symref);
764 for_each_namespaced_ref(send_ref, &symref);
Nguyễn Thái Ngọc Duyad491362013-12-05 20:02:32 +0700765 advertise_shallow_grafts(1);
Shawn O. Pearce42526b42009-10-30 17:47:33 -0700766 packet_flush(1);
767 } else {
Josh Triplett6b01ecf2011-07-08 16:13:32 -0700768 head_ref_namespaced(mark_our_ref, NULL);
769 for_each_namespaced_ref(mark_our_ref, NULL);
Shawn O. Pearce42526b42009-10-30 17:47:33 -0700770 }
Junio C Hamano7171d8c2013-09-17 16:17:33 -0700771 string_list_clear(&symref, 1);
Shawn O. Pearce42526b42009-10-30 17:47:33 -0700772 if (advertise_refs)
773 return;
774
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700775 receive_needs();
David Rientjes59076eb2006-08-14 13:40:51 -0700776 if (want_obj.nr) {
777 get_common_commits();
778 create_pack_file();
779 }
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700780}
781
Junio C Hamanodaebaa72013-01-18 16:08:30 -0800782static int upload_pack_config(const char *var, const char *value, void *unused)
783{
Junio C Hamano390eb362013-01-28 21:49:57 -0800784 if (!strcmp("uploadpack.allowtipsha1inwant", var))
785 allow_tip_sha1_in_want = git_config_bool(var, value);
Jeff King05e95152013-09-08 05:01:31 -0400786 else if (!strcmp("uploadpack.keepalive", var)) {
787 keepalive = git_config_int(var, value);
788 if (!keepalive)
789 keepalive = -1;
790 }
Junio C Hamanodaebaa72013-01-18 16:08:30 -0800791 return parse_hide_refs_config(var, value, "uploadpack");
792}
793
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700794int main(int argc, char **argv)
795{
Andreas Ericsson8d630132005-11-17 20:37:14 +0100796 char *dir;
H. Peter Anvin960decc2005-10-19 14:27:01 -0700797 int i;
798 int strict = 0;
799
Ævar Arnfjörð Bjarmason5e9637c2011-11-18 00:14:42 +0100800 git_setup_gettext();
801
Jeff Kingbbc30f92011-02-24 09:30:19 -0500802 packet_trace_identity("upload-pack");
Steffen Prohaska2fb3f6d2009-01-18 13:00:12 +0100803 git_extract_argv0_path(argv[0]);
Michael Haggertyafc711b2014-02-18 12:24:55 +0100804 check_replace_refs = 0;
Steffen Prohaska2fb3f6d2009-01-18 13:00:12 +0100805
H. Peter Anvin960decc2005-10-19 14:27:01 -0700806 for (i = 1; i < argc; i++) {
807 char *arg = argv[i];
808
809 if (arg[0] != '-')
810 break;
Shawn O. Pearce42526b42009-10-30 17:47:33 -0700811 if (!strcmp(arg, "--advertise-refs")) {
812 advertise_refs = 1;
813 continue;
814 }
815 if (!strcmp(arg, "--stateless-rpc")) {
816 stateless_rpc = 1;
817 continue;
818 }
H. Peter Anvin960decc2005-10-19 14:27:01 -0700819 if (!strcmp(arg, "--strict")) {
820 strict = 1;
821 continue;
822 }
Christian Couder59556542013-11-30 21:55:40 +0100823 if (starts_with(arg, "--timeout=")) {
H. Peter Anvin960decc2005-10-19 14:27:01 -0700824 timeout = atoi(arg+10);
Johannes Sixt9462e3f2009-06-16 20:41:16 +0200825 daemon_mode = 1;
H. Peter Anvin960decc2005-10-19 14:27:01 -0700826 continue;
827 }
828 if (!strcmp(arg, "--")) {
829 i++;
830 break;
831 }
832 }
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700833
H. Peter Anvin960decc2005-10-19 14:27:01 -0700834 if (i != argc-1)
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700835 usage(upload_pack_usage);
Johannes Sixt04b33052008-02-12 12:28:01 +0100836
Johannes Sixte1464ca2008-07-21 21:19:52 +0200837 setup_path();
Johannes Sixt04b33052008-02-12 12:28:01 +0100838
H. Peter Anvin960decc2005-10-19 14:27:01 -0700839 dir = argv[i];
Linus Torvalds113b9472005-07-08 16:22:22 -0700840
Andreas Ericsson8d630132005-11-17 20:37:14 +0100841 if (!enter_repo(dir, strict))
Jeff King05ac6b32009-03-04 03:32:29 -0500842 die("'%s' does not appear to be a git repository", dir);
Nguyễn Thái Ngọc Duyad491362013-12-05 20:02:32 +0700843
Junio C Hamanodaebaa72013-01-18 16:08:30 -0800844 git_config(upload_pack_config, NULL);
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700845 upload_pack();
846 return 0;
847}