fetch-pack: do not remove .git/shallow file when --depth is not specified
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Mon, 26 Aug 2013 02:17:26 +0000 (09:17 +0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 26 Aug 2013 05:56:03 +0000 (22:56 -0700)
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 <kornet@camk.edu.pl>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
fetch-pack.c
t/t5500-fetch-pack.sh
index 6b5467c6dec9645f53d83cdad2467a158db622c0..76190a8fbd58f2ada7f073f1035003823f55fbdb 100644 (file)
@@ -888,6 +888,8 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
                packet_flush(fd[1]);
        if (args->depth > 0)
                setup_alternate_shallow();
+       else
+               alternate_shallow_file = NULL;
        if (get_pack(args, fd, pack_lockfile))
                die("git fetch-pack: fetch failed.");
 
@@ -978,7 +980,7 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
        }
        ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought, pack_lockfile);
 
-       if (alternate_shallow_file) {
+       if (args->depth > 0 && alternate_shallow_file) {
                if (*alternate_shallow_file == '\0') { /* --unshallow */
                        unlink_or_warn(git_path("shallow"));
                        rollback_lock_file(&shallow_lock);
index fd2598e60190fe04381652c7dfe997e01b4ea0c5..a80584ea0eaba854ff21318e3c9646f72325bee7 100755 (executable)
@@ -505,4 +505,20 @@ test_expect_success 'test --all, --depth, and explicit tag' '
        ) >out-adt 2>error-adt
 '
 
+test_expect_success 'shallow fetch with tags does not break the repository' '
+       mkdir repo1 &&
+       (
+               cd repo1 &&
+               git init &&
+               test_commit 1 &&
+               test_commit 2 &&
+               test_commit 3 &&
+               mkdir repo2 &&
+               cd repo2 &&
+               git init &&
+               git fetch --depth=2 ../.git master:branch &&
+               git fsck
+       )
+'
+
 test_done