Skip to content

Commit 732ce7a

Browse files
committed
Merge branch 'jt/fetch-no-update-shallow-in-proto-v2'
Fix for protocol v2 support in "git fetch-pack" of shallow clones. * jt/fetch-no-update-shallow-in-proto-v2: fetch-pack: respect --no-update-shallow in v2 fetch-pack: call prepare_shallow_info only if v0
2 parents abd7ccd + 1339078 commit 732ce7a

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

commit.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ extern void setup_alternate_shallow(struct lock_file *shallow_lock,
263263
extern const char *setup_temporary_shallow(const struct oid_array *extra);
264264
extern void advertise_shallow_grafts(int);
265265

266+
/*
267+
* Initialize with prepare_shallow_info() or zero-initialize (equivalent to
268+
* prepare_shallow_info with a NULL oid_array).
269+
*/
266270
struct shallow_info {
267271
struct oid_array *shallow;
268272
int *ours, nr_ours;

fetch-pack.c

Lines changed: 41 additions & 10 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
}
@@ -1338,6 +1360,8 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
13381360
int fd[2],
13391361
const struct ref *orig_ref,
13401362
struct ref **sought, int nr_sought,
1363+
struct oid_array *shallows,
1364+
struct shallow_info *si,
13411365
char **pack_lockfile)
13421366
{
13431367
struct ref *ref = copy_ref_list(orig_ref);
@@ -1412,7 +1436,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
14121436
case FETCH_GET_PACK:
14131437
/* Check for shallow-info section */
14141438
if (process_section_header(&reader, "shallow-info", 1))
1415-
receive_shallow_info(args, &reader);
1439+
receive_shallow_info(args, &reader, shallows, si);
14161440

14171441
if (process_section_header(&reader, "wanted-refs", 1))
14181442
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)
@@ -1648,13 +1673,18 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
16481673
packet_flush(fd[1]);
16491674
die(_("no matching remote head"));
16501675
}
1651-
prepare_shallow_info(&si, shallow);
1652-
if (version == protocol_v2)
1676+
if (version == protocol_v2) {
1677+
if (shallow->nr)
1678+
BUG("Protocol V2 does not provide shallows at this point in the fetch");
1679+
memset(&si, 0, sizeof(si));
16531680
ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1681+
&shallows_scratch, &si,
16541682
pack_lockfile);
1655-
else
1683+
} else {
1684+
prepare_shallow_info(&si, shallow);
16561685
ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
16571686
&si, pack_lockfile);
1687+
}
16581688
reprepare_packed_git(the_repository);
16591689

16601690
if (!args->cloning && args->deepen) {
@@ -1676,6 +1706,7 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
16761706
update_shallow(args, sought, nr_sought, &si);
16771707
cleanup:
16781708
clear_shallow_info(&si);
1709+
oid_array_clear(&shallows_scratch);
16791710
return ref_cpy;
16801711
}
16811712

0 commit comments

Comments
 (0)