git/t/t0002-gitfile.sh

120 lines
2.2 KiB
Bash
Raw Normal View History

#!/bin/sh
test_description='.git file
Verify that plumbing commands work when .git is a file
'
. ./test-lib.sh
objpath() {
echo "$1" | sed -e 's|\(..\)|\1/|'
}
objck() {
p=$(objpath "$1")
if test ! -f "$REAL/objects/$p"
then
echo "Object not found: $REAL/objects/$p"
false
fi
}
test_expect_success 'initial setup' '
REAL="$(pwd)/.real" &&
mv .git "$REAL"
'
test_expect_success 'bad setup: invalid .git file format' '
echo "gitdir $REAL" >.git &&
if git rev-parse 2>.err
then
echo "git rev-parse accepted an invalid .git file"
false
fi &&
if ! grep "Invalid gitfile format" .err
then
echo "git rev-parse returned wrong error"
false
fi
'
test_expect_success 'bad setup: invalid .git file path' '
echo "gitdir: $REAL.not" >.git &&
if git rev-parse 2>.err
then
echo "git rev-parse accepted an invalid .git file path"
false
fi &&
if ! grep "Not a git repository" .err
then
echo "git rev-parse returned wrong error"
false
fi
'
test_expect_success 'final setup + check rev-parse --git-dir' '
echo "gitdir: $REAL" >.git &&
test "$REAL" = "$(git rev-parse --git-dir)"
'
test_expect_success 'check hash-object' '
echo "foo" >bar &&
SHA=$(cat bar | git hash-object -w --stdin) &&
objck $SHA
'
test_expect_success 'check cat-file' '
git cat-file blob $SHA >actual &&
test_cmp bar actual
'
test_expect_success 'check update-index' '
if test -f "$REAL/index"
then
echo "Hmm, $REAL/index exists?"
false
fi &&
rm -f "$REAL/objects/$(objpath $SHA)" &&
git update-index --add bar &&
if ! test -f "$REAL/index"
then
echo "$REAL/index not found"
false
fi &&
objck $SHA
'
test_expect_success 'check write-tree' '
SHA=$(git write-tree) &&
objck $SHA
'
test_expect_success 'check commit-tree' '
SHA=$(echo "commit bar" | git commit-tree $SHA) &&
objck $SHA
'
test_expect_success 'check rev-list' '
echo $SHA >"$REAL/HEAD" &&
test "$SHA" = "$(git rev-list HEAD)"
'
setup: set env $GIT_WORK_TREE when work tree is set, like $GIT_DIR In the test case, we run setup_git_dir_gently() the first time to read $GIT_DIR/config so that we can resolve aliases. We'll enter setup_discovered_git_dir() and may or may not call set_git_dir() near the end of the function, depending on whether the detected git dir is ".git" or not. This set_git_dir() will set env var $GIT_DIR. For normal repo, git dir detected via setup_discovered_git_dir() will be ".git", and set_git_dir() is not called. If .git file is used however, the git dir can't be ".git" and set_git_dir() is called and $GIT_DIR set. This is the key of this problem. If we expand an alias (or autocorrect command names), then setup_git_dir_gently() is run the second time. If $GIT_DIR is not set in the first run, we run the same setup_discovered_git_dir() as before. Nothing to see. If it is, however, we'll enter setup_explicit_git_dir() this time. This is where the "fun" is. If $GIT_WORK_TREE is not set but $GIT_DIR is, you are supposed to be at the root level of the worktree. But if you are in a subdir "foo/bar" (real worktree's top is "foo"), this rule bites you: your detected worktree is now "foo/bar", even though the first run correctly detected worktree as "foo". You get "internal error: work tree has already been set" as a result. Bottom line is, when $GIT_DIR is set, $GIT_WORK_TREE should be set too unless there's no work tree. But setting $GIT_WORK_TREE inside set_git_dir() may backfire. We don't know at that point if work tree is already configured by the caller. So set it when work tree is detected. It does not harm if $GIT_WORK_TREE is set while $GIT_DIR is not. Reported-by: Bjørnar Snoksrud <snoksrud@gmail.com> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-06-26 10:37:35 +00:00
test_expect_success 'setup_git_dir twice in subdir' '
git init sgd &&
(
cd sgd &&
git config alias.lsfi ls-files &&
mv .git .realgit &&
echo "gitdir: .realgit" >.git &&
mkdir subdir &&
cd subdir &&
>foo &&
git add foo &&
git lsfi >actual &&
echo foo >expected &&
test_cmp expected actual
)
'
test_done