Skip to content

Commit 1339078

Browse files
jonathantanmygitster
authored andcommitted
fetch-pack: respect --no-update-shallow in v2
In protocol v0, when sending "shallow" lines, the server distinguishes between lines caused by the remote repo being shallow and lines caused by client-specified depth settings. Unless "--update-shallow" is specified, there is a difference in behavior: refs that reach the former "shallow" lines, but not the latter, are rejected. But in v2, the server does not, and the client treats all "shallow" lines like lines caused by client-specified depth settings. Full restoration of v0 functionality is not possible without protocol change, but we can implement a heuristic: if we specify any depth setting, treat all "shallow" lines like lines caused by client-specified depth settings (that is, unaffected by "--no-update-shallow"), but otherwise, treat them like lines caused by the remote repo being shallow (that is, affected by "--no-update-shallow"). This restores most of v0 behavior, except in the case where a client fetches from a shallow repository with depth settings. This patch causes a test that previously failed with GIT_TEST_PROTOCOL_VERSION=2 to pass. Signed-off-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1e7d440 commit 1339078

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

fetch-pack.c

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,9 +1253,11 @@ static int process_acks(struct fetch_negotiator *negotiator,
12531253
}
12541254

12551255
static void receive_shallow_info(struct fetch_pack_args *args,
1256-
struct packet_reader *reader)
1256+
struct packet_reader *reader,
1257+
struct oid_array *shallows,
1258+
struct shallow_info *si)
12571259
{
1258-
int line_received = 0;
1260+
int unshallow_received = 0;
12591261

12601262
process_section_header(reader, "shallow-info", 0);
12611263
while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
@@ -1265,8 +1267,7 @@ static void receive_shallow_info(struct fetch_pack_args *args,
12651267
if (skip_prefix(reader->line, "shallow ", &arg)) {
12661268
if (get_oid_hex(arg, &oid))
12671269
die(_("invalid shallow line: %s"), reader->line);
1268-
register_shallow(the_repository, &oid);
1269-
line_received = 1;
1270+
oid_array_append(shallows, &oid);
12701271
continue;
12711272
}
12721273
if (skip_prefix(reader->line, "unshallow ", &arg)) {
@@ -1279,7 +1280,7 @@ static void receive_shallow_info(struct fetch_pack_args *args,
12791280
die(_("error in object: %s"), reader->line);
12801281
if (unregister_shallow(&oid))
12811282
die(_("no shallow found: %s"), reader->line);
1282-
line_received = 1;
1283+
unshallow_received = 1;
12831284
continue;
12841285
}
12851286
die(_("expected shallow/unshallow, got %s"), reader->line);
@@ -1289,10 +1290,31 @@ static void receive_shallow_info(struct fetch_pack_args *args,
12891290
reader->status != PACKET_READ_DELIM)
12901291
die(_("error processing shallow info: %d"), reader->status);
12911292

1292-
if (line_received) {
1293+
if (args->deepen || unshallow_received) {
1294+
/*
1295+
* Treat these as shallow lines caused by our depth settings.
1296+
* In v0, these lines cannot cause refs to be rejected; do the
1297+
* same.
1298+
*/
1299+
int i;
1300+
1301+
for (i = 0; i < shallows->nr; i++)
1302+
register_shallow(the_repository, &shallows->oid[i]);
12931303
setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
12941304
NULL);
12951305
args->deepen = 1;
1306+
} else if (shallows->nr) {
1307+
/*
1308+
* Treat these as shallow lines caused by the remote being
1309+
* shallow. In v0, remote refs that reach these objects are
1310+
* rejected (unless --update-shallow is set); do the same.
1311+
*/
1312+
prepare_shallow_info(si, shallows);
1313+
if (si->nr_ours || si->nr_theirs)
1314+
alternate_shallow_file =
1315+
setup_temporary_shallow(si->shallow);
1316+
else
1317+
alternate_shallow_file = NULL;
12961318
} else {
12971319
alternate_shallow_file = NULL;
12981320
}
@@ -1337,6 +1359,8 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
13371359
int fd[2],
13381360
const struct ref *orig_ref,
13391361
struct ref **sought, int nr_sought,
1362+
struct oid_array *shallows,
1363+
struct shallow_info *si,
13401364
char **pack_lockfile)
13411365
{
13421366
struct ref *ref = copy_ref_list(orig_ref);
@@ -1411,7 +1435,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
14111435
case FETCH_GET_PACK:
14121436
/* Check for shallow-info section */
14131437
if (process_section_header(&reader, "shallow-info", 1))
1414-
receive_shallow_info(args, &reader);
1438+
receive_shallow_info(args, &reader, shallows, si);
14151439

14161440
if (process_section_header(&reader, "wanted-refs", 1))
14171441
receive_wanted_refs(&reader, sought, nr_sought);
@@ -1625,6 +1649,7 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
16251649
{
16261650
struct ref *ref_cpy;
16271651
struct shallow_info si;
1652+
struct oid_array shallows_scratch = OID_ARRAY_INIT;
16281653

16291654
fetch_pack_setup();
16301655
if (nr_sought)
@@ -1653,6 +1678,7 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
16531678
BUG("Protocol V2 does not provide shallows at this point in the fetch");
16541679
memset(&si, 0, sizeof(si));
16551680
ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1681+
&shallows_scratch, &si,
16561682
pack_lockfile);
16571683
} else {
16581684
prepare_shallow_info(&si, shallow);
@@ -1680,6 +1706,7 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
16801706
update_shallow(args, sought, nr_sought, &si);
16811707
cleanup:
16821708
clear_shallow_info(&si);
1709+
oid_array_clear(&shallows_scratch);
16831710
return ref_cpy;
16841711
}
16851712

0 commit comments

Comments
 (0)