1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-11-01 14:57:52 +01:00
git/t/t1410-reflog.sh
Ronnie Sahlberg 482b8f3208 checkout.c: use ref_exists instead of file_exist
Change checkout.c to check if a ref exists instead of checking if a loose ref
file exists when deciding if to delete an orphaned log file. Otherwise, if a
ref only exists as a packed ref without a corresponding loose ref for the
currently checked out branch, we risk that the reflog will be deleted when we
switch to a different branch.

Update the reflog tests to check for this bug.

The following reproduces the bug:
$ git init-db
$ git config core.logallrefupdates true
$ git commit -m Initial --allow-empty
    [master (root-commit) bb11abe] Initial
$ git reflog master
    [8561dcb master@{0}: commit (initial): Initial]
$ find .git/{refs,logs} -type f | grep master
    [.git/refs/heads/master]
    [.git/logs/refs/heads/master]
$ git branch foo
$ git pack-refs --all
$ find .git/{refs,logs} -type f | grep master
    [.git/logs/refs/heads/master]
$ git checkout foo
$ find .git/{refs,logs} -type f | grep master
    ... reflog file is missing ...
$ git reflog master
    ... nothing ...

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
Acked-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2014-05-08 14:31:44 -07:00

256 lines
5 KiB
Bash
Executable file

#!/bin/sh
#
# Copyright (c) 2007 Junio C Hamano
#
test_description='Test prune and reflog expiration'
. ./test-lib.sh
check_have () {
gaah= &&
for N in "$@"
do
eval "o=\$$N" && git cat-file -t $o || {
echo Gaah $N
gaah=$N
break
}
done &&
test -z "$gaah"
}
check_fsck () {
output=$(git fsck --full)
case "$1" in
'')
test -z "$output" ;;
*)
echo "$output" | grep "$1" ;;
esac
}
corrupt () {
aa=${1%??????????????????????????????????????} zz=${1#??}
mv .git/objects/$aa/$zz .git/$aa$zz
}
recover () {
aa=${1%??????????????????????????????????????} zz=${1#??}
mkdir -p .git/objects/$aa
mv .git/$aa$zz .git/objects/$aa/$zz
}
check_dont_have () {
gaah= &&
for N in "$@"
do
eval "o=\$$N"
git cat-file -t $o && {
echo Gaah $N
gaah=$N
break
}
done
test -z "$gaah"
}
test_expect_success setup '
mkdir -p A/B &&
echo rat >C &&
echo ox >A/D &&
echo tiger >A/B/E &&
git add . &&
test_tick && git commit -m rabbit &&
H=`git rev-parse --verify HEAD` &&
A=`git rev-parse --verify HEAD:A` &&
B=`git rev-parse --verify HEAD:A/B` &&
C=`git rev-parse --verify HEAD:C` &&
D=`git rev-parse --verify HEAD:A/D` &&
E=`git rev-parse --verify HEAD:A/B/E` &&
check_fsck &&
test_chmod +x C &&
git add C &&
test_tick && git commit -m dragon &&
L=`git rev-parse --verify HEAD` &&
check_fsck &&
rm -f C A/B/E &&
echo snake >F &&
echo horse >A/G &&
git add F A/G &&
test_tick && git commit -a -m sheep &&
F=`git rev-parse --verify HEAD:F` &&
G=`git rev-parse --verify HEAD:A/G` &&
I=`git rev-parse --verify HEAD:A` &&
J=`git rev-parse --verify HEAD` &&
check_fsck &&
rm -f A/G &&
test_tick && git commit -a -m monkey &&
K=`git rev-parse --verify HEAD` &&
check_fsck &&
check_have A B C D E F G H I J K L &&
git prune &&
check_have A B C D E F G H I J K L &&
check_fsck &&
test_line_count = 4 .git/logs/refs/heads/master
'
test_expect_success rewind '
test_tick && git reset --hard HEAD~2 &&
test -f C &&
test -f A/B/E &&
! test -f F &&
! test -f A/G &&
check_have A B C D E F G H I J K L &&
git prune &&
check_have A B C D E F G H I J K L &&
test_line_count = 5 .git/logs/refs/heads/master
'
test_expect_success 'corrupt and check' '
corrupt $F &&
check_fsck "missing blob $F"
'
test_expect_success 'reflog expire --dry-run should not touch reflog' '
git reflog expire --dry-run \
--expire=$(($test_tick - 10000)) \
--expire-unreachable=$(($test_tick - 10000)) \
--stale-fix \
--all &&
test_line_count = 5 .git/logs/refs/heads/master &&
check_fsck "missing blob $F"
'
test_expect_success 'reflog expire' '
git reflog expire --verbose \
--expire=$(($test_tick - 10000)) \
--expire-unreachable=$(($test_tick - 10000)) \
--stale-fix \
--all &&
test_line_count = 2 .git/logs/refs/heads/master &&
check_fsck "dangling commit $K"
'
test_expect_success 'prune and fsck' '
git prune &&
check_fsck &&
check_have A B C D E H L &&
check_dont_have F G I J K
'
test_expect_success 'recover and check' '
recover $F &&
check_fsck "dangling blob $F"
'
test_expect_success 'delete' '
echo 1 > C &&
test_tick &&
git commit -m rat C &&
echo 2 > C &&
test_tick &&
git commit -m ox C &&
echo 3 > C &&
test_tick &&
git commit -m tiger C &&
HEAD_entry_count=$(git reflog | wc -l) &&
master_entry_count=$(git reflog show master | wc -l) &&
test $HEAD_entry_count = 5 &&
test $master_entry_count = 5 &&
git reflog delete master@{1} &&
git reflog show master > output &&
test $(($master_entry_count - 1)) = $(wc -l < output) &&
test $HEAD_entry_count = $(git reflog | wc -l) &&
! grep ox < output &&
master_entry_count=$(wc -l < output) &&
git reflog delete HEAD@{1} &&
test $(($HEAD_entry_count -1)) = $(git reflog | wc -l) &&
test $master_entry_count = $(git reflog show master | wc -l) &&
HEAD_entry_count=$(git reflog | wc -l) &&
git reflog delete master@{07.04.2005.15:15:00.-0700} &&
git reflog show master > output &&
test $(($master_entry_count - 1)) = $(wc -l < output) &&
! grep dragon < output
'
test_expect_success 'rewind2' '
test_tick && git reset --hard HEAD~2 &&
test_line_count = 4 .git/logs/refs/heads/master
'
test_expect_success '--expire=never' '
git reflog expire --verbose \
--expire=never \
--expire-unreachable=never \
--all &&
test_line_count = 4 .git/logs/refs/heads/master
'
test_expect_success 'gc.reflogexpire=never' '
git config gc.reflogexpire never &&
git config gc.reflogexpireunreachable never &&
git reflog expire --verbose --all &&
test_line_count = 4 .git/logs/refs/heads/master
'
test_expect_success 'gc.reflogexpire=false' '
git config gc.reflogexpire false &&
git config gc.reflogexpireunreachable false &&
git reflog expire --verbose --all &&
test_line_count = 4 .git/logs/refs/heads/master &&
git config --unset gc.reflogexpire &&
git config --unset gc.reflogexpireunreachable
'
test_expect_success 'checkout should not delete log for packed ref' '
test $(git reflog master | wc -l) = 4 &&
git branch foo &&
git pack-refs --all &&
git checkout foo &&
test $(git reflog master | wc -l) = 4
'
test_done