#!/usr/bin/env bash
count=0
for repo in "$@"
do {
    git clone repo "$count"
    cd "$count"
    mkdir -p objs
    git log --stat --all --pretty=format:'commit %H%nparents %P%nsubject %s%nbody%n%b%nendbody' | sed -ne '/^body/,/^endbody/p; /^\(commit\|parents\|subject\)/p; s/^ \(.*\) | \(.*\) .*/file \1 \2/p' | {
        while read type name detail
        do
            echo "$type" "$name" "$detail"
            case "$type" in
            commit)
                commit="$name"
                ;;
            parents)
                parents="$name $detail"
                ;;
            file)
                for parent in $parents
                do
                    echo "$parent"
                    git ls-tree "$parent" "$name"
                done | while read parent
                do
                    read mode type object path
                    if [ -n "$object" ] && ! [ -e "objs/$object" ]
                    then
                        if [ -e "objs/$object" ]
                        then
                            echo object "$parent" "$object"
                        else
                            echo beginobject "$parent" "$object" "$type" "$path"
                            git cat-file "$type" "$object"
                            echo endobject "$parent" "$object" "$type" "$path"
                            touch "objs/$object"
                        fi
                    fi
                done
                echo beginpatch "$commit" "$name"
                git log -1 --patch --follow --find-renames --pretty=format: "$commit" -- "$name"
                echo endpatch "$commit" "$name"
                ;;
            esac
        done
    }
} & ((count++)); done
