Skip to content

Commit 6da8bdc

Browse files
pcloudsgitster
authored andcommitted
fetch-pack: do not remove .git/shallow file when --depth is not specified
fetch_pack() can remove .git/shallow file when a shallow repository becomes a full one again. This behavior is triggered incorrectly when tags are also fetched because fetch_pack() will be called twice. At the first fetch_pack() call: - shallow_lock is set up - alternate_shallow_file points to shallow_lock.filename, which is "shallow.lock" - commit_lock_file is called, which sets shallow_lock.filename to "". alternate_shallow_file also becomes "" because it points to the same memory. At the second call, setup_alternate_shallow() is not called and alternate_shallow_file remains "". It's mistaken as unshallow case and .git/shallow is removed. The end result is a broken repository. Fix this by always initializing alternate_shallow_file when fetch_pack() is called. As an extra measure, check if args->depth > 0 before commit/rollback shallow file. Reported-by: Kacper Kornet <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a3bc3d0 commit 6da8bdc

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

fetch-pack.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,8 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
888888
packet_flush(fd[1]);
889889
if (args->depth > 0)
890890
setup_alternate_shallow();
891+
else
892+
alternate_shallow_file = NULL;
891893
if (get_pack(args, fd, pack_lockfile))
892894
die("git fetch-pack: fetch failed.");
893895

@@ -978,7 +980,7 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
978980
}
979981
ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought, pack_lockfile);
980982

981-
if (alternate_shallow_file) {
983+
if (args->depth > 0 && alternate_shallow_file) {
982984
if (*alternate_shallow_file == '\0') { /* --unshallow */
983985
unlink_or_warn(git_path("shallow"));
984986
rollback_lock_file(&shallow_lock);

t/t5500-fetch-pack.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,4 +505,20 @@ test_expect_success 'test --all, --depth, and explicit tag' '
505505
) >out-adt 2>error-adt
506506
'
507507

508+
test_expect_success 'shallow fetch with tags does not break the repository' '
509+
mkdir repo1 &&
510+
(
511+
cd repo1 &&
512+
git init &&
513+
test_commit 1 &&
514+
test_commit 2 &&
515+
test_commit 3 &&
516+
mkdir repo2 &&
517+
cd repo2 &&
518+
git init &&
519+
git fetch --depth=2 ../.git master:branch &&
520+
git fsck
521+
)
522+
'
523+
508524
test_done

0 commit comments

Comments
 (0)