Index: commit-id =================================================================== --- 6ad600e20c89323c1d3049f75b8ca9b0a2d72167/commit-id (mode:100755 sha1:4efcb6bdfdb2b2c5744f5d4d47d92beb7777ed59) +++ uncommitted/commit-id (mode:100775) @@ -9,22 +9,30 @@ SHA1ONLY="^$SHA1$" id=$1 + if [ ! "$id" ] || [ "$id" = "this" ] || [ "$id" = "HEAD" ]; then id=$(cat .git/HEAD) -fi -if (echo $id | egrep -vq "$SHA1ONLY") && [ -r ".git/refs/tags/$id" ]; then +elif [ -r ".git/refs/tags/$id" ]; then id=$(cat ".git/refs/tags/$id") -fi -if (echo $id | egrep -vq "$SHA1ONLY") && [ -r ".git/refs/heads/$id" ]; then +elif [ -r ".git/refs/heads/$id" ]; then id=$(cat ".git/refs/heads/$id") -fi -idpref=$(echo "$id" | cut -c -2) -idpost=$(echo "$id" | cut -c 3-) -if [ $(find ".git/objects/$idpref" -name "$idpost*" 2>/dev/null | wc -l) -eq 1 ]; then - id=$idpref$(basename $(echo .git/objects/$idpref/$idpost*)) +# Short id's must be lower case and at least 4 digits. +elif [[ "$id" == [0-9a-z][0-9a-z][0-9a-z][0-9a-z]* ]]; then + idpost=${id#??} + idpref=${id%$idpost} + + # Assign array elements to matching names + idmatch=($(echo .git/objects/$idpref/$idpost*)) + + if [ ${#idmatch[*]} -eq 1 ] && [ -r "$idmatch" ]; then + id=$idpref${idmatch#.git/objects/$idpref/} + elif [ ${#idmatch[*]} -gt 1 ]; then + echo "Ambiguous id: $id" >&2 + exit 1 + fi fi if echo $id | egrep -vq "$SHA1ONLY"; then Index: tree-id =================================================================== --- 6ad600e20c89323c1d3049f75b8ca9b0a2d72167/tree-id (mode:100755 sha1:1495ff78af71b57e21653512932bcda88fe05454) +++ uncommitted/tree-id (mode:100775) @@ -7,8 +7,35 @@ SHA1="[A-Za-z0-9]{40}" TREE="^tree $SHA1$" +SHA1ONLY="^$SHA1$" -id=$(cat-file commit $(commit-id "$1") | egrep "$TREE" | cut -d ' ' -f 2) +id=$1 + +# Is it a commit? +commit=$(commit-id $id 2>/dev/null) +if [ "$commit" ]; then + id=$(cat-file commit "$commit") | egrep "$TREE" | cut -d ' ' -f 2) + +# Short id's must be lower case and at least 4 digits. +elif [[ "$id" == [0-9a-z][0-9a-z][0-9a-z][0-9a-z]* ]]; then + idpost=${id#??} + idpref=${id%$idpost} + + # Assign array elements to matching names + idmatch=($(echo .git/objects/$idpref/$idpost*)) + + if [ ${#idmatch[*]} -eq 1 ] && [ -r "$idmatch" ]; then + id=$idpref${idmatch#.git/objects/$idpref/} + elif [ ${#idmatch[*]} -gt 1 ]; then + echo "Ambiguous id: $id" >&2 + exit 1 + fi +fi + +if echo $id | egrep -vq "$SHA1ONLY"; then + echo "Invalid id: $id" >&2 + exit 1 +fi if [ "$(cat-file -t "$id")" != "tree" ]; then echo "Invalid id: $id" >&2 Index: parent-id =================================================================== --- 6ad600e20c89323c1d3049f75b8ca9b0a2d72167/parent-id (mode:100755 sha1:f35877a6aa5b68d2fb4a388dcfa9b3e64262604e) +++ uncommitted/parent-id (mode:100775) @@ -5,7 +5,8 @@ # # Takes ID of the current commit, defaults to HEAD. -PARENT="^parent [A-Za-z0-9]{40}$" +SHA1="[A-Za-z0-9]{40}" +PARENT="^parent $SHA1$" id=$(commit-id $1) || exit 1