I\'m writing a pre-receive hook to do some validation before accepting commits on the push. It works fine with existing branches since I use following git command to get a list
Try this:
git rev-list $new_sha1 $(git for-each-ref --format '^%(refname:short)' refs/heads/)
This uses git for-each-ref
to print known refs; specifies in --format
that the short name be output along with a preceding caret; and says with refs/heads/
to selecting just local heads. You end up running something like this:
git rev-list $new_sha1 ^master ^foo ^bar ^baz
The caret means “exclude commits reachable from here”. So this will give you all commits that are reachable from $new_sha1
, but aren’t from any of the existing refs.
To handle the new-ref and existing-ref cases at once, you can use bash’s array support:
if ! [ $old_sha1 = 0000000000000000000000000000000000000000 ] ; then
excludes=( ^$old_sha1 )
else
excludes=( $(git for-each-ref --format '^%(refname:short)' refs/heads/) )
fi
git rev-list $new_sha1 "${excludes[@]}"