upload-pack: lift MAX_NEEDS and MAX_HAS limitation
By using object_array data structure, lift the old limitation of MAX_HAS/MAX_NEEDS. While we are at it, rename the variables that hold the objects we use to compute common ancestor to match the message used at the protocol level. What the other end has and we also do are "have"s, and what the other end asks for are "want"s. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Jul 5, 2006 at 18:00 UTC
b1e9fff7e76c13cc16f0b4a4eda19600320bd7e7
1 file changed
+41
-52
upload-pack.c
+41
-52
@@ -14,12 +14,10 @@ static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=n
14
#define THEY_HAVE (1U << 0)
15
#define OUR_REF (1U << 1)
16
#define WANTED (1U << 2)
17
-#define MAX_HAS 256
18
-#define MAX_NEEDS 256
19
-static int nr_has = 0, nr_needs = 0, multi_ack = 0, nr_our_refs = 0;
17
+static int multi_ack = 0, nr_our_refs = 0;
18
static int use_thin_pack = 0;
21
-static struct object *has_tip[MAX_HAS];
22
-static struct object *needs_tip[MAX_NEEDS];
19
+static struct object_array have_obj;
20
+static struct object_array want_obj;
21
static unsigned int timeout = 0;
22
static int use_sideband = 0;
23
@@ -79,7 +77,7 @@ static void create_pack_file(void)
77
*/
78
int lp_pipe[2], pu_pipe[2], pe_pipe[2];
79
pid_t pid_rev_list, pid_pack_objects;
82
- int create_full_pack = (nr_our_refs == nr_needs && !nr_has);
80
+ int create_full_pack = (nr_our_refs == want_obj.nr && !have_obj.nr);
81
char data[8193], progress[128];
82
char abort_msg[] = "aborting due to possible repository "
83
"corruption on the remote side.";
@@ -103,7 +101,7 @@ static void create_pack_file(void)
101
use_thin_pack = 0; /* no point doing it */
102
}
103
else
106
- args = nr_has + nr_needs + 5;
104
+ args = have_obj.nr + want_obj.nr + 5;
105
p = xmalloc(args * sizeof(char *));
106
argv = (const char **) p;
107
buf = xmalloc(args * 45);
@@ -114,20 +112,22 @@ static void create_pack_file(void)
112
close(lp_pipe[1]);
113
*p++ = "rev-list";
114
*p++ = use_thin_pack ? "--objects-edge" : "--objects";
117
- if (create_full_pack || MAX_NEEDS <= nr_needs)
115
+ if (create_full_pack)
116
*p++ = "--all";
117
else {
120
- for (i = 0; i < nr_needs; i++) {
118
+ for (i = 0; i < want_obj.nr; i++) {
119
+ struct object *o = want_obj.objects[i].item;
120
*p++ = buf;
122
- memcpy(buf, sha1_to_hex(needs_tip[i]->sha1), 41);
121
+ memcpy(buf, sha1_to_hex(o->sha1), 41);
122
buf += 41;
123
}
124
}
125
if (!create_full_pack)
127
- for (i = 0; i < nr_has; i++) {
126
+ for (i = 0; i < have_obj.nr; i++) {
127
+ struct object *o = have_obj.objects[i].item;
128
*p++ = buf;
129
*buf++ = '^';
130
- memcpy(buf, sha1_to_hex(has_tip[i]->sha1), 41);
130
+ memcpy(buf, sha1_to_hex(o->sha1), 41);
131
buf += 41;
132
}
133
*p++ = NULL;
@@ -316,28 +316,29 @@ static void create_pack_file(void)
316
317
static int got_sha1(char *hex, unsigned char *sha1)
318
{
319
+ struct object *o;
320
+
321
if (get_sha1_hex(hex, sha1))
322
die("git-upload-pack: expected SHA1 object, got '%s'", hex);
323
if (!has_sha1_file(sha1))
324
return 0;
323
- if (nr_has < MAX_HAS) {
324
- struct object *o = lookup_object(sha1);
325
- if (!(o && o->parsed))
326
- o = parse_object(sha1);
327
- if (!o)
328
- die("oops (%s)", sha1_to_hex(sha1));
329
- if (o->type == TYPE_COMMIT) {
330
- struct commit_list *parents;
331
- if (o->flags & THEY_HAVE)
332
- return 0;
333
- o->flags |= THEY_HAVE;
334
- for (parents = ((struct commit*)o)->parents;
335
- parents;
336
- parents = parents->next)
337
- parents->item->object.flags |= THEY_HAVE;
338
- }
339
- has_tip[nr_has++] = o;
325
+
326
+ o = lookup_object(sha1);
327
+ if (!(o && o->parsed))
328
+ o = parse_object(sha1);
329
+ if (!o)
330
+ die("oops (%s)", sha1_to_hex(sha1));
331
+ if (o->type == TYPE_COMMIT) {
332
+ struct commit_list *parents;
333
+ if (o->flags & THEY_HAVE)
334
+ return 0;
335
+ o->flags |= THEY_HAVE;
336
+ for (parents = ((struct commit*)o)->parents;
337
+ parents;
338
+ parents = parents->next)
339
+ parents->item->object.flags |= THEY_HAVE;
340
}
341
+ add_object_array(o, NULL, &have_obj);
342
return 1;
343
}
344
@@ -355,26 +356,24 @@ static int get_common_commits(void)
356
reset_timeout();
357
358
if (!len) {
358
- if (nr_has == 0 || multi_ack)
359
+ if (have_obj.nr == 0 || multi_ack)
360
packet_write(1, "NAK\n");
361
continue;
362
}
363
len = strip(line, len);
364
if (!strncmp(line, "have ", 5)) {
365
if (got_sha1(line+5, sha1) &&
365
- (multi_ack || nr_has == 1)) {
366
- if (nr_has >= MAX_HAS)
367
- multi_ack = 0;
366
+ (multi_ack || have_obj.nr == 1)) {
367
packet_write(1, "ACK %s%s\n",
369
- sha1_to_hex(sha1),
370
- multi_ack ? " continue" : "");
368
+ sha1_to_hex(sha1),
369
+ multi_ack ? " continue" : "");
370
if (multi_ack)
371
memcpy(last_sha1, sha1, 20);
372
}
373
continue;
374
}
375
if (!strcmp(line, "done")) {
377
- if (nr_has > 0) {
376
+ if (have_obj.nr > 0) {
377
if (multi_ack)
378
packet_write(1, "ACK %s\n",
379
sha1_to_hex(last_sha1));
@@ -387,26 +386,18 @@ static int get_common_commits(void)
386
}
387
}
388
390
-static int receive_needs(void)
389
+static void receive_needs(void)
390
{
391
static char line[1000];
393
- int len, needs;
392
+ int len;
393
395
- needs = 0;
394
for (;;) {
395
struct object *o;
396
unsigned char sha1_buf[20];
397
len = packet_read_line(0, line, sizeof(line));
398
reset_timeout();
399
if (!len)
402
- return needs;
403
-
404
- if (needs == MAX_NEEDS) {
405
- fprintf(stderr,
406
- "warning: supporting only a max of %d requests. "
407
- "sending everything instead.\n",
408
- MAX_NEEDS);
409
- }
400
+ return;
401
402
if (strncmp("want ", line, 5) ||
403
get_sha1_hex(line+5, sha1_buf))
@@ -432,9 +423,7 @@ static int receive_needs(void)
423
die("git-upload-pack: not our ref %s", line+5);
424
if (!(o->flags & WANTED)) {
425
o->flags |= WANTED;
435
- if (needs < MAX_NEEDS)
436
- needs_tip[needs] = o;
437
- needs++;
426
+ add_object_array(o, NULL, &want_obj);
427
}
428
}
429
}
@@ -470,8 +459,8 @@ static int upload_pack(void)
459
head_ref(send_ref);
460
for_each_ref(send_ref);
461
packet_flush(1);
473
- nr_needs = receive_needs();
474
- if (!nr_needs)
462
+ receive_needs();
463
+ if (!want_obj.nr)
464
return 0;
465
get_common_commits();
466
create_pack_file();