kbro kbro - 4 months ago 65
Git Question

Git - how to list ALL objects in the database

Is there a better way of getting a raw list of SHA1s for ALL objects in a repository than doing

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.

Answer Source

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

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

  • Used --git-dir instead of --show-cdup to support bare repos
  • Avoided error when there are no packs
  • Used perl because OS X Mountain Lion's BSD-style sed doesn't support -r

#!/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:' \
;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download