Skip to content

Instantly share code, notes, and snippets.

@chalstrick
Created January 15, 2018 16:50
Show Gist options
  • Save chalstrick/4cba478996fcbfbdc5a276487bb01547 to your computer and use it in GitHub Desktop.
Save chalstrick/4cba478996fcbfbdc5a276487bb01547 to your computer and use it in GitHub Desktop.
test all(?) combinations of content (even file<->dir), skipWorktree bits, sparse-checkout content during a git checkout
#!/bin/bash
#
# Sets up a lot of repos to test git-checkout operations. Setup test repos for all (?) possible
# combinations of contents in merge commit, index, workingtree and also for all combinations
# of skip-worktree bit and .git/info/sparse-checkout content
# After the repos are setup try to checkout and print status regarding worktree content, HEAD
#
# Requires that there is an executable 'jgit' in $PATH which is the command-line version
#
# usage: testAllCheckoutSituations.sh [-jgit]
for merge in 0 a D ;do
for index in 0 a bb ;do
for work in 0 a bb ccc D ;do
for oldSWBit in true false ;do
for newSWBit in true false ;do
testCaseName=repo_$merge$index$work$oldSWBit$newSWBit
(
# Ignore redundant tests
[ $index == bb -a $merge != a ] && continue
[ $work == ccc -a $index != bb ] && continue
[ $index == 0 -a $oldSWBit == true ] && continue
echo "############################# $testCaseName"
# Create new repo, configure sparseCheckout, create initial commit
rm -fr $testCaseName
git init -q $testCaseName
cd $testCaseName
git config core.sparsecheckout true
# Create the 'MERGE' commit which should be checked out
[ $merge == a ] && echo $merge >f
[ $merge == D ] && mkdir f && echo $merge >f/f2
[ -e f ] && git add f
git commit -q -mMERGE --allow-empty
git tag MERGE
# Create the index (including skipWorktree bit) and the sparse-checkout file
if [ $merge != $index ] ;then
if [ $index == 0 ] ;then
git rm -fqr f
else
[ -d f ] && rm -fr f
echo $index >f
git add f
fi
fi
[ $index != 0 -a $oldSWBit == true ] && git update-index --skip-worktree f
[ $newSWBit == true ] && echo f >.git/info/sparse-checkout
# Create the workingTree
if [ $index != $work ] ;then
if [ $work == D ] ;then
rm -fr f
mkdir f; echo $merge >f/f2
elif [ $work == 0 ] ;then
rm -fr f
else
[ -d f ] && rm -fr f
echo $work >f
fi
fi
# print the status before the checkout
echo "State before checkout"
[ $index != 0 ] && echo "index state:"
[ $index != 0 ] && git ls-files -sv --debug | grep -v "ctime:\|mtime:\|dev:"
[ $index != 0 ] && echo "index content: " && git show :f
[ $merge != 0 ] && echo "content in MERGE: " && git show MERGE:f
[ -e f ] && echo "worktree content: " && ls -l f | cut -d' ' -f1-8 && cat f
[ -f .git/info/sparse-checkout ] && echo "sparse-checkout content: " && cat .git/info/sparse-checkout
# now try to checkout
if [ "$1" == "-jgit" ] ;then
jgit checkout -f MERGE
else
git checkout -q -f MERGE
fi
# print the status
echo "State after checkout with rc=$?"
echo "HEAD: " $(git describe --all)
if git ls-files -q --error-unmatch -- f >/dev/null 2>&1 ;then
echo "index state:"
git ls-files -sv --debug | grep -v "ctime:\|mtime:\|dev:"
echo "index content: " && git show :f
fi
[ -e f ] && echo "worktree content: " && ls -l f | cut -d' ' -f1-8 && cat f
)
done
done
done
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment