613
}
614
}
615
616
-static void send_shallow(struct commit_list *result)
616
+static void send_shallow(struct packet_writer *writer,
617
+ struct commit_list *result)
618
{
619
while (result) {
620
struct object *object = &result->item->object;
621
if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
621
- packet_write_fmt(1, "shallow %s",
622
- oid_to_hex(&object->oid));
622
+ packet_writer_write(writer, "shallow %s",
623
+ oid_to_hex(&object->oid));
624
register_shallow(the_repository, &object->oid);
625
shallow_nr++;
626
}
628
}
629
}
630
630
-static void send_unshallow(const struct object_array *shallows,
631
+static void send_unshallow(struct packet_writer *writer,
632
+ const struct object_array *shallows,
633
struct object_array *want_obj)
634
{
635
int i;
638
struct object *object = shallows->objects[i].item;
639
if (object->flags & NOT_SHALLOW) {
640
struct commit_list *parents;
639
- packet_write_fmt(1, "unshallow %s",
640
- oid_to_hex(&object->oid));
641
+ packet_writer_write(writer, "unshallow %s",
642
+ oid_to_hex(&object->oid));
643
object->flags &= ~CLIENT_SHALLOW;
644
/*
645
* We want to _register_ "object" as shallow, but we
664
}
665
}
666
665
-static void deepen(int depth, int deepen_relative,
667
+static void deepen(struct packet_writer *writer, int depth, int deepen_relative,
668
struct object_array *shallows, struct object_array *want_obj)
669
{
670
if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
682
result = get_shallow_commits(&reachable_shallows,
683
depth + 1,
684
SHALLOW, NOT_SHALLOW);
683
- send_shallow(result);
685
+ send_shallow(writer, result);
686
free_commit_list(result);
687
object_array_clear(&reachable_shallows);
688
} else {
690
691
result = get_shallow_commits(want_obj, depth,
692
SHALLOW, NOT_SHALLOW);
691
- send_shallow(result);
693
+ send_shallow(writer, result);
694
free_commit_list(result);
695
}
696
695
- send_unshallow(shallows, want_obj);
697
+ send_unshallow(writer, shallows, want_obj);
698
}
699
698
-static void deepen_by_rev_list(int ac, const char **av,
700
+static void deepen_by_rev_list(struct packet_writer *writer, int ac,
701
+ const char **av,
702
struct object_array *shallows,
703
struct object_array *want_obj)
704
{
706
707
close_commit_graph(the_repository);
708
result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
706
- send_shallow(result);
709
+ send_shallow(writer, result);
710
free_commit_list(result);
708
- send_unshallow(shallows, want_obj);
711
+ send_unshallow(writer, shallows, want_obj);
712
}
713
714
/* Returns 1 if a shallow list is sent or 0 otherwise */
712
-static int send_shallow_list(int depth, int deepen_rev_list,
715
+static int send_shallow_list(struct packet_writer *writer,
716
+ int depth, int deepen_rev_list,
717
timestamp_t deepen_since,
718
struct string_list *deepen_not,
719
struct object_array *shallows,
724
if (depth > 0 && deepen_rev_list)
725
die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
726
if (depth > 0) {
723
- deepen(depth, deepen_relative, shallows, want_obj);
727
+ deepen(writer, depth, deepen_relative, shallows, want_obj);
728
ret = 1;
729
} else if (deepen_rev_list) {
730
struct argv_array av = ARGV_ARRAY_INIT;
745
struct object *o = want_obj->objects[i].item;
746
argv_array_push(&av, oid_to_hex(&o->oid));
747
}
744
- deepen_by_rev_list(av.argc, av.argv, shallows, want_obj);
748
+ deepen_by_rev_list(writer, av.argc, av.argv, shallows, want_obj);
749
argv_array_clear(&av);
750
ret = 1;
751
} else {
838
int has_non_tip = 0;
839
timestamp_t deepen_since = 0;
840
int deepen_rev_list = 0;
841
+ struct packet_writer writer;
842
843
shallow_nr = 0;
844
+ packet_writer_init(&writer, 1);
845
for (;;) {
846
struct object *o;
847
const char *features;
898
899
o = parse_object(the_repository, &oid_buf);
900
if (!o) {
895
- packet_write_fmt(1,
896
- "ERR upload-pack: not our ref %s",
897
- oid_to_hex(&oid_buf));
901
+ packet_writer_error(&writer,
902
+ "upload-pack: not our ref %s",
903
+ oid_to_hex(&oid_buf));
904
die("git upload-pack: not our ref %s",
905
oid_to_hex(&oid_buf));
906
}
929
if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
930
return;
931
926
- if (send_shallow_list(depth, deepen_rev_list, deepen_since,
932
+ if (send_shallow_list(&writer, depth, deepen_rev_list, deepen_since,
933
&deepen_not, &shallows, want_obj))
934
packet_flush(1);
935
object_array_clear(&shallows);
1108
int deepen_rev_list;
1109
int deepen_relative;
1110
1111
+ struct packet_writer writer;
1112
+
1113
unsigned stateless_rpc : 1;
1114
1115
unsigned use_thin_pack : 1;
1133
data->haves = haves;
1134
data->shallows = shallows;
1135
data->deepen_not = deepen_not;
1136
+ packet_writer_init(&data->writer, 1);
1137
}
1138
1139
static void upload_pack_data_clear(struct upload_pack_data *data)
1145
string_list_clear(&data->deepen_not, 0);
1146
}
1147
1139
-static int parse_want(const char *line, struct object_array *want_obj)
1148
+static int parse_want(struct packet_writer *writer, const char *line,
1149
+ struct object_array *want_obj)
1150
{
1151
const char *arg;
1152
if (skip_prefix(line, "want ", &arg)) {
1159
1160
o = parse_object(the_repository, &oid);
1161
if (!o) {
1152
- packet_write_fmt(1,
1153
- "ERR upload-pack: not our ref %s",
1154
- oid_to_hex(&oid));
1162
+ packet_writer_error(writer,
1163
+ "upload-pack: not our ref %s",
1164
+ oid_to_hex(&oid));
1165
die("git upload-pack: not our ref %s",
1166
oid_to_hex(&oid));
1167
}
1177
return 0;
1178
}
1179
1170
-static int parse_want_ref(const char *line, struct string_list *wanted_refs,
1180
+static int parse_want_ref(struct packet_writer *writer, const char *line,
1181
+ struct string_list *wanted_refs,
1182
struct object_array *want_obj)
1183
{
1184
const char *arg;
1188
struct object *o;
1189
1190
if (read_ref(arg, &oid)) {
1180
- packet_write_fmt(1, "ERR unknown ref %s", arg);
1191
+ packet_writer_error(writer, "unknown ref %s", arg);
1192
die("unknown ref %s", arg);
1193
}
1194
1231
const char *p;
1232
1233
/* process want */
1223
- if (parse_want(arg, want_obj))
1234
+ if (parse_want(&data->writer, arg, want_obj))
1235
continue;
1236
if (allow_ref_in_want &&
1226
- parse_want_ref(arg, &data->wanted_refs, want_obj))
1237
+ parse_want_ref(&data->writer, arg, &data->wanted_refs,
1238
+ want_obj))
1239
continue;
1240
/* process have line */
1241
if (parse_have(arg, &data->haves))
1329
return 0;
1330
}
1331
1320
-static int send_acks(struct oid_array *acks, struct strbuf *response,
1332
+static int send_acks(struct packet_writer *writer, struct oid_array *acks,
1333
const struct object_array *have_obj,
1334
struct object_array *want_obj)
1335
{
1336
int i;
1337
1326
- packet_buf_write(response, "acknowledgments\n");
1338
+ packet_writer_write(writer, "acknowledgments\n");
1339
1340
/* Send Acks */
1341
if (!acks->nr)
1330
- packet_buf_write(response, "NAK\n");
1342
+ packet_writer_write(writer, "NAK\n");
1343
1344
for (i = 0; i < acks->nr; i++) {
1333
- packet_buf_write(response, "ACK %s\n",
1334
- oid_to_hex(&acks->oid[i]));
1345
+ packet_writer_write(writer, "ACK %s\n",
1346
+ oid_to_hex(&acks->oid[i]));
1347
}
1348
1349
if (ok_to_give_up(have_obj, want_obj)) {
1350
/* Send Ready */
1339
- packet_buf_write(response, "ready\n");
1351
+ packet_writer_write(writer, "ready\n");
1352
return 1;
1353
}
1354
1360
struct object_array *want_obj)
1361
{
1362
struct oid_array common = OID_ARRAY_INIT;
1351
- struct strbuf response = STRBUF_INIT;
1363
int ret = 0;
1364
1365
process_haves(&data->haves, &common, have_obj);
1366
if (data->done) {
1367
ret = 1;
1357
- } else if (send_acks(&common, &response, have_obj, want_obj)) {
1358
- packet_buf_delim(&response);
1368
+ } else if (send_acks(&data->writer, &common, have_obj, want_obj)) {
1369
+ packet_writer_delim(&data->writer);
1370
ret = 1;
1371
} else {
1372
/* Add Flush */
1362
- packet_buf_flush(&response);
1373
+ packet_writer_flush(&data->writer);
1374
ret = 0;
1375
}
1376
1366
- /* Send response */
1367
- write_or_die(1, response.buf, response.len);
1368
- strbuf_release(&response);
1369
-
1377
oid_array_clear(&data->haves);
1378
oid_array_clear(&common);
1379
return ret;
1386
if (!data->wanted_refs.nr)
1387
return;
1388
1382
- packet_write_fmt(1, "wanted-refs\n");
1389
+ packet_writer_write(&data->writer, "wanted-refs\n");
1390
1391
for_each_string_list_item(item, &data->wanted_refs) {
1385
- packet_write_fmt(1, "%s %s\n",
1386
- oid_to_hex(item->util),
1387
- item->string);
1392
+ packet_writer_write(&data->writer, "%s %s\n",
1393
+ oid_to_hex(item->util),
1394
+ item->string);
1395
}
1396
1390
- packet_delim(1);
1397
+ packet_writer_delim(&data->writer);
1398
}
1399
1400
static void send_shallow_info(struct upload_pack_data *data,
1405
!is_repository_shallow(the_repository))
1406
return;
1407
1401
- packet_write_fmt(1, "shallow-info\n");
1408
+ packet_writer_write(&data->writer, "shallow-info\n");
1409
1403
- if (!send_shallow_list(data->depth, data->deepen_rev_list,
1410
+ if (!send_shallow_list(&data->writer, data->depth,
1411
+ data->deepen_rev_list,
1412
data->deepen_since, &data->deepen_not,
1413
&data->shallows, want_obj) &&
1414
is_repository_shallow(the_repository))
1407
- deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows,
1408
- want_obj);
1415
+ deepen(&data->writer, INFINITE_DEPTH, data->deepen_relative,
1416
+ &data->shallows, want_obj);
1417
1418
packet_delim(1);
1419
}
1475
send_wanted_ref_info(&data);
1476
send_shallow_info(&data, &want_obj);
1477
1470
- packet_write_fmt(1, "packfile\n");
1478
+ packet_writer_write(&data.writer, "packfile\n");
1479
create_pack_file(&have_obj, &want_obj);
1480
state = FETCH_DONE;
1481
break;