2008-02-20 23:13:13 +01:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
test_description='.git file
|
|
|
|
|
|
|
|
Verify that plumbing commands work when .git is a file
|
|
|
|
'
|
|
|
|
. ./test-lib.sh
|
|
|
|
|
|
|
|
objpath() {
|
2013-12-08 06:56:57 +01:00
|
|
|
echo "$1" | sed -e 's|\(..\)|\1/|'
|
2008-02-20 23:13:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 &&
|
2008-09-30 10:03:55 +02:00
|
|
|
if ! grep "Invalid gitfile format" .err
|
2008-02-20 23:13:13 +01:00
|
|
|
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 &&
|
2008-09-30 10:03:55 +02:00
|
|
|
if ! grep "Not a git repository" .err
|
2008-02-20 23:13:13 +01:00
|
|
|
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 &&
|
2008-07-22 23:17:43 +02:00
|
|
|
test_cmp bar actual
|
2008-02-20 23:13:13 +01:00
|
|
|
'
|
|
|
|
|
|
|
|
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 12:37:35 +02: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
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
2015-09-28 15:06:12 +02:00
|
|
|
test_expect_success 'enter_repo non-strict mode' '
|
|
|
|
test_create_repo enter_repo &&
|
|
|
|
(
|
|
|
|
cd enter_repo &&
|
|
|
|
test_tick &&
|
|
|
|
test_commit foo &&
|
|
|
|
mv .git .realgit &&
|
|
|
|
echo "gitdir: .realgit" >.git
|
|
|
|
) &&
|
|
|
|
git ls-remote enter_repo >actual &&
|
|
|
|
cat >expected <<-\EOF &&
|
|
|
|
946e985ab20de757ca5b872b16d64e92ff3803a9 HEAD
|
|
|
|
946e985ab20de757ca5b872b16d64e92ff3803a9 refs/heads/master
|
|
|
|
946e985ab20de757ca5b872b16d64e92ff3803a9 refs/tags/foo
|
|
|
|
EOF
|
|
|
|
test_cmp expected actual
|
|
|
|
'
|
|
|
|
|
2008-02-20 23:13:13 +01:00
|
|
|
test_done
|