@@ -1008,6 +1008,26 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
10081008 return ref ;
10091009}
10101010
1011+ static void add_shallow_requests (struct strbuf * req_buf ,
1012+ const struct fetch_pack_args * args )
1013+ {
1014+ if (is_repository_shallow ())
1015+ write_shallow_commits (req_buf , 1 , NULL );
1016+ if (args -> depth > 0 )
1017+ packet_buf_write (req_buf , "deepen %d" , args -> depth );
1018+ if (args -> deepen_since ) {
1019+ timestamp_t max_age = approxidate (args -> deepen_since );
1020+ packet_buf_write (req_buf , "deepen-since %" PRItime , max_age );
1021+ }
1022+ if (args -> deepen_not ) {
1023+ int i ;
1024+ for (i = 0 ; i < args -> deepen_not -> nr ; i ++ ) {
1025+ struct string_list_item * s = args -> deepen_not -> items + i ;
1026+ packet_buf_write (req_buf , "deepen-not %s" , s -> string );
1027+ }
1028+ }
1029+ }
1030+
10111031static void add_wants (const struct ref * wants , struct strbuf * req_buf )
10121032{
10131033 for ( ; wants ; wants = wants -> next ) {
@@ -1093,6 +1113,12 @@ static int send_fetch_request(int fd_out, const struct fetch_pack_args *args,
10931113 if (prefer_ofs_delta )
10941114 packet_buf_write (& req_buf , "ofs-delta" );
10951115
1116+ /* Add shallow-info and deepen request */
1117+ if (server_supports_feature ("fetch" , "shallow" , 0 ))
1118+ add_shallow_requests (& req_buf , args );
1119+ else if (is_repository_shallow () || args -> deepen )
1120+ die (_ ("Server does not support shallow requests" ));
1121+
10961122 /* add wants */
10971123 add_wants (wants , & req_buf );
10981124
@@ -1122,7 +1148,7 @@ static int process_section_header(struct packet_reader *reader,
11221148 int ret ;
11231149
11241150 if (packet_reader_peek (reader ) != PACKET_READ_NORMAL )
1125- die ("error reading packet" );
1151+ die ("error reading section header '%s'" , section );
11261152
11271153 ret = !strcmp (reader -> line , section );
11281154
@@ -1177,6 +1203,43 @@ static int process_acks(struct packet_reader *reader, struct oidset *common)
11771203 return received_ready ? 2 : (received_ack ? 1 : 0 );
11781204}
11791205
1206+ static void receive_shallow_info (struct fetch_pack_args * args ,
1207+ struct packet_reader * reader )
1208+ {
1209+ process_section_header (reader , "shallow-info" , 0 );
1210+ while (packet_reader_read (reader ) == PACKET_READ_NORMAL ) {
1211+ const char * arg ;
1212+ struct object_id oid ;
1213+
1214+ if (skip_prefix (reader -> line , "shallow " , & arg )) {
1215+ if (get_oid_hex (arg , & oid ))
1216+ die (_ ("invalid shallow line: %s" ), reader -> line );
1217+ register_shallow (& oid );
1218+ continue ;
1219+ }
1220+ if (skip_prefix (reader -> line , "unshallow " , & arg )) {
1221+ if (get_oid_hex (arg , & oid ))
1222+ die (_ ("invalid unshallow line: %s" ), reader -> line );
1223+ if (!lookup_object (oid .hash ))
1224+ die (_ ("object not found: %s" ), reader -> line );
1225+ /* make sure that it is parsed as shallow */
1226+ if (!parse_object (& oid ))
1227+ die (_ ("error in object: %s" ), reader -> line );
1228+ if (unregister_shallow (& oid ))
1229+ die (_ ("no shallow found: %s" ), reader -> line );
1230+ continue ;
1231+ }
1232+ die (_ ("expected shallow/unshallow, got %s" ), reader -> line );
1233+ }
1234+
1235+ if (reader -> status != PACKET_READ_FLUSH &&
1236+ reader -> status != PACKET_READ_DELIM )
1237+ die ("error processing shallow info: %d" , reader -> status );
1238+
1239+ setup_alternate_shallow (& shallow_lock , & alternate_shallow_file , NULL );
1240+ args -> deepen = 1 ;
1241+ }
1242+
11801243enum fetch_state {
11811244 FETCH_CHECK_LOCAL = 0 ,
11821245 FETCH_SEND_REQUEST ,
@@ -1209,6 +1272,8 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
12091272 /* v2 supports these by default */
12101273 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1 ;
12111274 use_sideband = 2 ;
1275+ if (args -> depth > 0 || args -> deepen_since || args -> deepen_not )
1276+ args -> deepen = 1 ;
12121277
12131278 if (marked )
12141279 for_each_ref (clear_marks , NULL );
@@ -1245,6 +1310,10 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
12451310 }
12461311 break ;
12471312 case FETCH_GET_PACK :
1313+ /* Check for shallow-info section */
1314+ if (process_section_header (& reader , "shallow-info" , 1 ))
1315+ receive_shallow_info (args , & reader );
1316+
12481317 /* get the pack */
12491318 process_section_header (& reader , "packfile" , 0 );
12501319 if (get_pack (args , fd , pack_lockfile ))
0 commit comments