jmou/zit
git clone https://github.com/jmou/zit.git
git clone git@github.com:jmou/zit.git
Loading…
(top)/test
language: Shell
5.29 KB / 151 lines / 122 loc
History
View raw
#!/bin/bash -ex
# TODO make compatible with actual git, probably by accepting compatible flags

header() {
    echo -e "\n\033[32m$@\033[0m"
}

get_md5() {
    if type md5sum &> /dev/null; then
        md5sum $1 | cut -d' ' -f1
    else
        md5 -s $1
    fi
}

header 'Initializing repo... [zit, init]'
[[ $1 == --keep ]] && { keep=1; shift; }
ZIT="${1-zit}"
trap 'echo -e "\e[31mFAIL\e[0m"' ERR
repo="$PWD/test.tmp"
rm -rf "$repo"
$ZIT init "$repo"
[[ -n $keep ]] || trap 'rm -rf "$repo"' EXIT
cd "$repo"
[[ $(<.zit/HEAD) == 'ref: refs/heads/master' ]]
[[ ${ZIT::1} == / ]] || ZIT="../$ZIT"

header 'Writing a blob object... [hash-object]'
echo "I'm a little teapot" > lullaby
[[ $($ZIT hash-object blob lullaby) == 3eb7f9afc452afe452b99d478d89264380622248 ]]

header 'Reading an object... [cat-file]'
[[ $($ZIT cat-file -t 3eb7f9afc452afe452b99d478d89264380622248 ) == blob ]]
[[ $($ZIT cat-file blob 3eb7f9afc452afe452b99d478d89264380622248 ) == "I'm a little teapot" ]]

header 'Writing to the index... [index.py]'
PYTHONPATH=$(dirname "$ZIT") /usr/bin/python -c 'import index; index.write_index([("lullaby", 0100644, "\x3e\xb7\xf9\xaf\xc4\x52\xaf\xe4\x52\xb9\x9d\x47\x8d\x89\x26\x43\x80\x62\x22\x48")])'
[[ $(get_md5 .zit/index) == 06432001fc95f78cd6353321f7c95009 ]]

header 'Adding a file to the index... [add]'
echo here > handle
$ZIT add handle
[[ $($ZIT cat-file blob 012ea92e81171f5ef7486696878656c5263cf722 ) == here ]]
[[ $(get_md5 .zit/index) == 61728e18ee75d24576d9ea2b626fb1ff ]]

header 'Reading the index... [index.py]'
[[ $(PYTHONPATH=$(dirname "$ZIT") /usr/bin/python -c 'import index; print index.read_index()') == "[('handle', 33188, '\x01.\xa9.\x81\x17\x1f^\xf7Hf\x96\x87\x86V\xc5&<\xf7\"'), ('lullaby', 33188, '>\xb7\xf9\xaf\xc4R\xaf\xe4R\xb9\x9dG\x8d\x89&C\x80b\"H')]" ]]

header 'Writing the index to a tree object... [write-tree]'
initial_tree=$($ZIT write-tree)
[[ $initial_tree == f65ccd4f10cb3416c7be6601bafd2371a784054f ]]

header 'Writing an initial commit object... [commit-tree]'
initial_commit=$(echo initial commit | $ZIT commit-tree $initial_tree)
[[ $($ZIT cat-file commit $initial_commit ) =~ ^"tree $initial_tree
author "[^\<]*" <"[^\>]*"> "[0-9]*\ [+-][0-9]{4}"
committer "[^\<]*" <"[^\>]*"> "[0-9]*\ [+-][0-9]{4}"

initial commit"$ ]]

header 'Create a branch ref [update-ref]'
$ZIT update-ref refs/heads/initial $initial_commit
[[ $(<.zit/refs/heads/initial) == $initial_commit ]]

header 'Read HEAD symbolic ref [symbolic-ref]'
[[ $($ZIT symbolic-ref HEAD) == refs/heads/master ]]

header 'Birth master branch [update-ref, get-sha1-basic, rev-parse]'
$ZIT update-ref HEAD $initial_commit
[[ $($ZIT rev-parse HEAD) == $initial_commit ]]

header 'Delete and create a branch [branch]'
$ZIT branch -d initial
[[ ! -f .zit/refs/heads/initial ]]
$ZIT branch initial
[[ $(<.zit/refs/heads/initial) == $initial_commit ]]

header 'Writing another commit object... [commit, commit-tree]'
echo 'short and stout' > lullaby
$ZIT add lullaby
EDITOR=tee $ZIT commit <<< 'second verse' > /dev/null
second_tree=3c1bea2d45d260a3836794e04a4bbef34e3c2459
second_commit=$($ZIT rev-parse HEAD)
[[ $($ZIT cat-file commit $second_commit ) =~ ^"tree $second_tree
parent $initial_commit
author "[^\<]*" <"[^\>]*"> "[0-9]*\ [+-][0-9]{4}"
committer "[^\<]*" <"[^\>]*"> "[0-9]*\ [+-][0-9]{4}"

second verse"$ ]]

header 'Parse ^ and ~ revisions [rev-parse]'
[[ $($ZIT rev-parse HEAD^) == $initial_commit ]]
[[ $($ZIT rev-parse HEAD~1) == $initial_commit ]]
[[ $($ZIT rev-parse initial~0) == $initial_commit ]]

header 'Parse ^{type} revisions [rev-parse]'
[[ $($ZIT rev-parse HEAD^{tree}) == $second_tree ]]
[[ $($ZIT rev-parse HEAD^^{tree}) == $initial_tree ]]

header 'Reading tree object into the index... [read-tree]'
$ZIT read-tree $initial_tree
[[ $(get_md5 .zit/index) == 61728e18ee75d24576d9ea2b626fb1ff ]]

header 'Checkout from the index [checkout-index]'
$ZIT checkout-index
[[ $(<lullaby) == "I'm a little teapot" ]]

header 'Update HEAD symbolic ref [symbolic-ref]'
$ZIT symbolic-ref HEAD refs/heads/initial
[[ $(<.zit/HEAD) == 'ref: refs/heads/initial' ]]

header 'Checkout master branch [checkout]'
$ZIT checkout master
[[ $(<lullaby) == 'short and stout' ]]
[[ $(<.zit/HEAD) == 'ref: refs/heads/master' ]]

header 'Checkout detached HEAD [checkout]'
$ZIT checkout HEAD
[[ $(<.zit/HEAD) == $second_commit ]]

header 'Commit executable file, symlink [add]'
chmod +x handle
ln -s handle spout
git add handle spout
EDITOR=tee $ZIT commit <<< 'first stanza' > /dev/null
[[ $($ZIT rev-parse HEAD^{tree}) == 32b256d0e79f6c2a3ccca5ef6bd67aa7b37fc6eb ]]

header 'Checkout a new branch [checkout, checkout-index]'
rm -f handle spout
$ZIT checkout -b stanza
[[ $(<.zit/HEAD) == 'ref: refs/heads/stanza' ]]
[[ -x handle ]]
[[ $(python -c 'import os; print os.readlink("spout")') == handle ]]

header 'Commit directory [write-tree]'
mkdir stanza2
echo 'When I get all steamed up' > stanza2/line1
echo 'hear me shout' > stanza2/line2
echo 'tip me over and pour me out' > stanza2/line3
git add stanza2
EDITOR=tee $ZIT commit <<< 'second stanza' > /dev/null
[[ $($ZIT rev-parse HEAD^{tree}) == 211fce66889894e23c9d710e2ae9df93066be410 ]]

header 'Checkout a new branch [checkout, checkout-index]'
rm -rf stanza
$ZIT checkout stanza
[[ $(<.zit/HEAD) == 'ref: refs/heads/stanza' ]]
[[ $(<stanza2/line1) == 'When I get all steamed up' ]]

[[ -n $keep ]] || { rm -rf "$repo"; trap '' EXIT; }
header PASS