@@ -1569,30 +1569,29 @@ static void queue_commands_from_cert(struct command **tail,
15691569 }
15701570}
15711571
1572- static struct command * read_head_info (struct oid_array * shallow )
1572+ static struct command * read_head_info (struct packet_reader * reader ,
1573+ struct oid_array * shallow )
15731574{
15741575 struct command * commands = NULL ;
15751576 struct command * * p = & commands ;
15761577 for (;;) {
1577- char * line ;
1578- int len , linelen ;
1578+ int linelen ;
15791579
1580- line = packet_read_line (0 , & len );
1581- if (!line )
1580+ if (packet_reader_read (reader ) != PACKET_READ_NORMAL )
15821581 break ;
15831582
1584- if (len > 8 && starts_with (line , "shallow " )) {
1583+ if (reader -> pktlen > 8 && starts_with (reader -> line , "shallow " )) {
15851584 struct object_id oid ;
1586- if (get_oid_hex (line + 8 , & oid ))
1585+ if (get_oid_hex (reader -> line + 8 , & oid ))
15871586 die ("protocol error: expected shallow sha, got '%s'" ,
1588- line + 8 );
1587+ reader -> line + 8 );
15891588 oid_array_append (shallow , & oid );
15901589 continue ;
15911590 }
15921591
1593- linelen = strlen (line );
1594- if (linelen < len ) {
1595- const char * feature_list = line + linelen + 1 ;
1592+ linelen = strlen (reader -> line );
1593+ if (linelen < reader -> pktlen ) {
1594+ const char * feature_list = reader -> line + linelen + 1 ;
15961595 if (parse_feature_request (feature_list , "report-status" ))
15971596 report_status = 1 ;
15981597 if (parse_feature_request (feature_list , "side-band-64k" ))
@@ -1607,28 +1606,32 @@ static struct command *read_head_info(struct oid_array *shallow)
16071606 use_push_options = 1 ;
16081607 }
16091608
1610- if (!strcmp (line , "push-cert" )) {
1609+ if (!strcmp (reader -> line , "push-cert" )) {
16111610 int true_flush = 0 ;
1612- char certbuf [1024 ];
1611+ int saved_options = reader -> options ;
1612+ reader -> options &= ~PACKET_READ_CHOMP_NEWLINE ;
16131613
16141614 for (;;) {
1615- len = packet_read (0 , NULL , NULL ,
1616- certbuf , sizeof (certbuf ), 0 );
1617- if (!len ) {
1615+ packet_reader_read (reader );
1616+ if (reader -> status == PACKET_READ_FLUSH ) {
16181617 true_flush = 1 ;
16191618 break ;
16201619 }
1621- if (!strcmp (certbuf , "push-cert-end\n" ))
1620+ if (reader -> status != PACKET_READ_NORMAL ) {
1621+ die ("protocol error: got an unexpected packet" );
1622+ }
1623+ if (!strcmp (reader -> line , "push-cert-end\n" ))
16221624 break ; /* end of cert */
1623- strbuf_addstr (& push_cert , certbuf );
1625+ strbuf_addstr (& push_cert , reader -> line );
16241626 }
1627+ reader -> options = saved_options ;
16251628
16261629 if (true_flush )
16271630 break ;
16281631 continue ;
16291632 }
16301633
1631- p = queue_command (p , line , linelen );
1634+ p = queue_command (p , reader -> line , linelen );
16321635 }
16331636
16341637 if (push_cert .len )
@@ -1637,18 +1640,14 @@ static struct command *read_head_info(struct oid_array *shallow)
16371640 return commands ;
16381641}
16391642
1640- static void read_push_options (struct string_list * options )
1643+ static void read_push_options (struct packet_reader * reader ,
1644+ struct string_list * options )
16411645{
16421646 while (1 ) {
1643- char * line ;
1644- int len ;
1645-
1646- line = packet_read_line (0 , & len );
1647-
1648- if (!line )
1647+ if (packet_reader_read (reader ) != PACKET_READ_NORMAL )
16491648 break ;
16501649
1651- string_list_append (options , line );
1650+ string_list_append (options , reader -> line );
16521651 }
16531652}
16541653
@@ -1924,6 +1923,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
19241923 struct oid_array shallow = OID_ARRAY_INIT ;
19251924 struct oid_array ref = OID_ARRAY_INIT ;
19261925 struct shallow_info si ;
1926+ struct packet_reader reader ;
19271927
19281928 struct option options [] = {
19291929 OPT__QUIET (& quiet , N_ ("quiet" )),
@@ -1986,12 +1986,16 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
19861986 if (advertise_refs )
19871987 return 0 ;
19881988
1989- if ((commands = read_head_info (& shallow )) != NULL ) {
1989+ packet_reader_init (& reader , 0 , NULL , 0 ,
1990+ PACKET_READ_CHOMP_NEWLINE |
1991+ PACKET_READ_DIE_ON_ERR_PACKET );
1992+
1993+ if ((commands = read_head_info (& reader , & shallow )) != NULL ) {
19901994 const char * unpack_status = NULL ;
19911995 struct string_list push_options = STRING_LIST_INIT_DUP ;
19921996
19931997 if (use_push_options )
1994- read_push_options (& push_options );
1998+ read_push_options (& reader , & push_options );
19951999 if (!check_cert_push_options (& push_options )) {
19962000 struct command * cmd ;
19972001 for (cmd = commands ; cmd ; cmd = cmd -> next )
0 commit comments