Eric Sunshine <[email protected]> 于2019年2月1日周五 下午2:11写道:
> > Everything below will be done inside master.git? Avoid cd'ing
> > > around in random places in the test script, as a failure in any of
> > > the steps that does cd would start later tests in an unexpected
> > > place, if you can.
> >
> > The first 10 test cases will run inside master.git, and others will
> > run inside shared.git. Only run cd inside the two `setup` test cases.
>
> That's not what Junio meant. It's okay for tests to 'cd', but each
> test which does so _must_ ensure that the 'cd' is undone at the end of
> the test, even if the test fails. The correct way to do this within
> each test is by using 'cd' in a subhsell, like this:
>
> test_expect_success 'setup master.git' '
> git init --bare master.git &&
> (
> cd master.git &&
> create_commits
> )
> '
create_commits should not run in sub-shell, or variables set are lost.
I write a commit_commits_in function :
# Usage: create_commits_in <repo> A B C ...
# Note: DO NOT run it in sub shell, or variables are not set
create_commits_in () {
repo="$1" &&
parent=$(git -C "$repo" rev-parse HEAD^{} 2>/dev/null) || parent=
T=$(git -C "$repo" write-tree) &&
shift &&
while test $# -gt 0
do
name=$1 &&
test_tick &&
if test -z "$parent"
then
oid=$(echo $name | git -C "$repo" commit-tree $T)
else
oid=$(echo $name | git -C "$repo"
commit-tree -p $parent $T)
fi &&
eval $name=$oid &&
parent=$oid &&
shift ||
return 1
done
git -C "$repo" update-ref refs/heads/master $oid
}
and use it to create commits like:
create_commits_in master.git A B C D E F G ...