diff --git a/read-tree b/read-tree index cb29ad0..70db41e 100755 --- a/read-tree +++ b/read-tree @@ -7,16 +7,19 @@ import sys import index -_, tree_sha1 = sys.argv -tree = subprocess.check_output(['cat-file', 'tree', tree_sha1]) -entries = [] -while tree: - header, rest = tree.split('\0', 1) - mode, filename = header.split(' ', 1) - rawsha1 = rest[:20] - tree = rest[20:] - # TODO handle directories - if mode != '040000': - entries.append((filename, int(mode, 8), rawsha1)) +def unpack_trees(tree_sha1, base): + tree = subprocess.check_output(['cat-file', 'tree', tree_sha1]) + entries = [] + while tree: + header, rest = tree.split('\0', 1) + mode, filename = header.split(' ', 1) + rawsha1 = rest[:20] + tree = rest[20:] + if mode == '40000': # for directories + entries += unpack_trees(rawsha1.encode('hex'), filename + '/') + else: + entries.append((base + filename, int(mode, 8), rawsha1)) + return entries -index.write_index(entries) +_, tree_sha1 = sys.argv +index.write_index(unpack_trees(tree_sha1, '')) diff --git a/test b/test index d6228b6..e15e6e9 100755 --- a/test +++ b/test @@ -117,20 +117,34 @@ header 'Checkout detached HEAD [checkout]' $ZIT checkout HEAD [[ $(<.zit/HEAD) == $second_commit ]] -header 'Commit executable file, symlink, and directory [add]' +header 'Commit executable file, symlink [add]' chmod +x handle -mkdir and -ln -s ../handle and/spout -git add handle and/spout +ln -s handle spout +git add handle spout EDITOR=tee $ZIT commit <<< 'first stanza' > /dev/null -[[ $($ZIT rev-parse HEAD^{tree}) == 6a8ad12fa34d4f1ea64abb28c8ae143b1e8fde40 ]] +[[ $($ZIT rev-parse HEAD^{tree}) == 32b256d0e79f6c2a3ccca5ef6bd67aa7b37fc6eb ]] header 'Checkout a new branch [checkout, checkout-index]' -rm -rf handle and +rm -f handle spout $ZIT checkout -b stanza [[ $(<.zit/HEAD) == 'ref: refs/heads/stanza' ]] [[ -x handle ]] -[[ $(python -c 'import os; print os.readlink("and/spout")') == ../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' ]] +[[ $(