gitgit-showgit-rev-list

How to list ALL git objects in the database?


Is there a better way of getting a raw list of SHA1s for ALL objects in a repository than:
ls .git/objects/??/\*
and
cat .git/objects/pack/*.idx | git show-index

I know about git rev-list --all but that only lists commit objects that are referenced by .git/refs, and I'm looking for everything, including unreferenced objects that are created by git-hash-object, git-mktree etc.


Solution

  • Edit: Aristotle posted an even better answer, which should be marked as correct.

    Edit: the script contained a syntax error, missing backslash at the end of the grep -v line

    Mark's answer worked for me, after a few modifications:

    #!/bin/sh
    
    set -e
    
    cd "$(git rev-parse --git-dir)"
    
    # Find all the objects that are in packs:
    
    find objects/pack -name 'pack-*.idx' | while read p ; do
        git show-index < $p | cut -f 2 -d ' '
    done
    
    # And now find all loose objects:
    
    find objects/ \
        | egrep '[0-9a-f]{38}' \
        | grep -v /pack/ \
        | perl -pe 's:^.*([0-9a-f][0-9a-f])/([0-9a-f]{38}):\1\2:' \
    ;