#!/bin/sh

# setup secondary git
mkdir bar
cd bar
git init
echo "test" > bar
git add bar
git commit -m "test commit in bar"
echo "adding this will fail later on" > test
cd ..

# setup primary git with alias and script that alias will execute
mkdir foo
cd foo
git init

cat >foo.sh <<EOF
#!/bin/sh -x
cd $PWD/../bar
env | egrep "^(GIT_WORK_TREE|PWD)"
echo "Git log works (bar log shows up):"
git log --oneline | head -1
echo "Git add does not (tries to add files in foo, while PWD is in bar):"
git add test
EOF
chmod 755 foo.sh

cat >>.git/config <<EOF
[alias]
  foo = !$PWD/foo.sh
EOF

git add foo.sh
git commit -m "committing foo.sh"

# test case: using alias, cd into another git tree, and try to do git operation on THAT dir
# GIT_WORK_TREE gets set up into "foo", and while PWD is in "bar", some git ops do work on WRONG TREE!
git foo

