Skip to content

Commit 0b9333f

Browse files
jonathantanmygitster
authored andcommitted
upload-pack: make have_obj not global
Because upload_pack_v2() can be invoked multiple times in the same process, the static variable have_obj may not be empty when it is invoked. To make further analysis of this situation easier, make the variable local; analysis will be done in a subsequent patch. The new local variable in upload_pack_v2() is static to preserve existing behavior; this is not necessary in upload_pack() because upload_pack() is only invoked once per process. Signed-off-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a4b8ab5 commit 0b9333f

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

upload-pack.c

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ static int no_progress, daemon_mode;
5353
#define ALLOW_ANY_SHA1 07
5454
static unsigned int allow_unadvertised_object_request;
5555
static int shallow_nr;
56-
static struct object_array have_obj;
5756
static struct object_array want_obj;
5857
static struct object_array extra_edge_obj;
5958
static unsigned int timeout;
@@ -100,7 +99,7 @@ static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
10099
return 0;
101100
}
102101

103-
static void create_pack_file(void)
102+
static void create_pack_file(const struct object_array *have_obj)
104103
{
105104
struct child_process pack_objects = CHILD_PROCESS_INIT;
106105
char data[8193], progress[128];
@@ -165,9 +164,9 @@ static void create_pack_file(void)
165164
fprintf(pipe_fd, "%s\n",
166165
oid_to_hex(&want_obj.objects[i].item->oid));
167166
fprintf(pipe_fd, "--not\n");
168-
for (i = 0; i < have_obj.nr; i++)
167+
for (i = 0; i < have_obj->nr; i++)
169168
fprintf(pipe_fd, "%s\n",
170-
oid_to_hex(&have_obj.objects[i].item->oid));
169+
oid_to_hex(&have_obj->objects[i].item->oid));
171170
for (i = 0; i < extra_edge_obj.nr; i++)
172171
fprintf(pipe_fd, "%s\n",
173172
oid_to_hex(&extra_edge_obj.objects[i].item->oid));
@@ -304,7 +303,8 @@ static void create_pack_file(void)
304303
die("git upload-pack: %s", abort_msg);
305304
}
306305

307-
static int got_oid(const char *hex, struct object_id *oid)
306+
static int got_oid(const char *hex, struct object_id *oid,
307+
struct object_array *have_obj)
308308
{
309309
struct object *o;
310310
int we_knew_they_have = 0;
@@ -332,25 +332,25 @@ static int got_oid(const char *hex, struct object_id *oid)
332332
parents->item->object.flags |= THEY_HAVE;
333333
}
334334
if (!we_knew_they_have) {
335-
add_object_array(o, NULL, &have_obj);
335+
add_object_array(o, NULL, have_obj);
336336
return 1;
337337
}
338338
return 0;
339339
}
340340

341-
static int ok_to_give_up(void)
341+
static int ok_to_give_up(const struct object_array *have_obj)
342342
{
343343
uint32_t min_generation = GENERATION_NUMBER_ZERO;
344344

345-
if (!have_obj.nr)
345+
if (!have_obj->nr)
346346
return 0;
347347

348348
return can_all_from_reach_with_flag(&want_obj, THEY_HAVE,
349349
COMMON_KNOWN, oldest_have,
350350
min_generation);
351351
}
352352

353-
static int get_common_commits(void)
353+
static int get_common_commits(struct object_array *have_obj)
354354
{
355355
struct object_id oid;
356356
char last_hex[GIT_MAX_HEXSZ + 1];
@@ -368,11 +368,11 @@ static int get_common_commits(void)
368368

369369
if (!line) {
370370
if (multi_ack == 2 && got_common
371-
&& !got_other && ok_to_give_up()) {
371+
&& !got_other && ok_to_give_up(have_obj)) {
372372
sent_ready = 1;
373373
packet_write_fmt(1, "ACK %s ready\n", last_hex);
374374
}
375-
if (have_obj.nr == 0 || multi_ack)
375+
if (have_obj->nr == 0 || multi_ack)
376376
packet_write_fmt(1, "NAK\n");
377377

378378
if (no_done && sent_ready) {
@@ -386,10 +386,10 @@ static int get_common_commits(void)
386386
continue;
387387
}
388388
if (skip_prefix(line, "have ", &arg)) {
389-
switch (got_oid(arg, &oid)) {
389+
switch (got_oid(arg, &oid, have_obj)) {
390390
case -1: /* they have what we do not */
391391
got_other = 1;
392-
if (multi_ack && ok_to_give_up()) {
392+
if (multi_ack && ok_to_give_up(have_obj)) {
393393
const char *hex = oid_to_hex(&oid);
394394
if (multi_ack == 2) {
395395
sent_ready = 1;
@@ -405,14 +405,14 @@ static int get_common_commits(void)
405405
packet_write_fmt(1, "ACK %s common\n", last_hex);
406406
else if (multi_ack)
407407
packet_write_fmt(1, "ACK %s continue\n", last_hex);
408-
else if (have_obj.nr == 1)
408+
else if (have_obj->nr == 1)
409409
packet_write_fmt(1, "ACK %s\n", last_hex);
410410
break;
411411
}
412412
continue;
413413
}
414414
if (!strcmp(line, "done")) {
415-
if (have_obj.nr > 0) {
415+
if (have_obj->nr > 0) {
416416
if (multi_ack)
417417
packet_write_fmt(1, "ACK %s\n", last_hex);
418418
return 0;
@@ -1067,8 +1067,9 @@ void upload_pack(struct upload_pack_options *options)
10671067

10681068
receive_needs();
10691069
if (want_obj.nr) {
1070-
get_common_commits();
1071-
create_pack_file();
1070+
struct object_array have_obj = OBJECT_ARRAY_INIT;
1071+
get_common_commits(&have_obj);
1072+
create_pack_file(&have_obj);
10721073
}
10731074
}
10741075

@@ -1256,7 +1257,8 @@ static void process_args(struct packet_reader *request,
12561257
}
12571258
}
12581259

1259-
static int process_haves(struct oid_array *haves, struct oid_array *common)
1260+
static int process_haves(struct oid_array *haves, struct oid_array *common,
1261+
struct object_array *have_obj)
12601262
{
12611263
int i;
12621264

@@ -1289,13 +1291,14 @@ static int process_haves(struct oid_array *haves, struct oid_array *common)
12891291
parents->item->object.flags |= THEY_HAVE;
12901292
}
12911293
if (!we_knew_they_have)
1292-
add_object_array(o, NULL, &have_obj);
1294+
add_object_array(o, NULL, have_obj);
12931295
}
12941296

12951297
return 0;
12961298
}
12971299

1298-
static int send_acks(struct oid_array *acks, struct strbuf *response)
1300+
static int send_acks(struct oid_array *acks, struct strbuf *response,
1301+
const struct object_array *have_obj)
12991302
{
13001303
int i;
13011304

@@ -1310,7 +1313,7 @@ static int send_acks(struct oid_array *acks, struct strbuf *response)
13101313
oid_to_hex(&acks->oid[i]));
13111314
}
13121315

1313-
if (ok_to_give_up()) {
1316+
if (ok_to_give_up(have_obj)) {
13141317
/* Send Ready */
13151318
packet_buf_write(response, "ready\n");
13161319
return 1;
@@ -1319,16 +1322,17 @@ static int send_acks(struct oid_array *acks, struct strbuf *response)
13191322
return 0;
13201323
}
13211324

1322-
static int process_haves_and_send_acks(struct upload_pack_data *data)
1325+
static int process_haves_and_send_acks(struct upload_pack_data *data,
1326+
struct object_array *have_obj)
13231327
{
13241328
struct oid_array common = OID_ARRAY_INIT;
13251329
struct strbuf response = STRBUF_INIT;
13261330
int ret = 0;
13271331

1328-
process_haves(&data->haves, &common);
1332+
process_haves(&data->haves, &common, have_obj);
13291333
if (data->done) {
13301334
ret = 1;
1331-
} else if (send_acks(&common, &response)) {
1335+
} else if (send_acks(&common, &response, have_obj)) {
13321336
packet_buf_delim(&response);
13331337
ret = 1;
13341338
} else {
@@ -1394,6 +1398,8 @@ int upload_pack_v2(struct repository *r, struct argv_array *keys,
13941398
{
13951399
enum fetch_state state = FETCH_PROCESS_ARGS;
13961400
struct upload_pack_data data;
1401+
/* NEEDSWORK: make this non-static */
1402+
static struct object_array have_obj;
13971403

13981404
git_config(upload_pack_config, NULL);
13991405

@@ -1425,7 +1431,7 @@ int upload_pack_v2(struct repository *r, struct argv_array *keys,
14251431
}
14261432
break;
14271433
case FETCH_SEND_ACKS:
1428-
if (process_haves_and_send_acks(&data))
1434+
if (process_haves_and_send_acks(&data, &have_obj))
14291435
state = FETCH_SEND_PACK;
14301436
else
14311437
state = FETCH_DONE;
@@ -1435,7 +1441,7 @@ int upload_pack_v2(struct repository *r, struct argv_array *keys,
14351441
send_shallow_info(&data);
14361442

14371443
packet_write_fmt(1, "packfile\n");
1438-
create_pack_file();
1444+
create_pack_file(&have_obj);
14391445
state = FETCH_DONE;
14401446
break;
14411447
case FETCH_DONE:

0 commit comments

Comments
 (0)