Guig
Guig

Reputation: 10187

git prepush hook: get branches pushed to

How can I get a list of the branches that are being pushed to in the context of a pre push hook? I know how to get the current branch, but it can differ from the one/many branches being pushed to.

I've thought of parsing the command, but I'm worried I might forget about some cases. Like git push, git push origin master will both push to master etc.

Upvotes: 1

Views: 256

Answers (2)

torek
torek

Reputation: 487805

Here's one possible loop, although it's not clear to me what you want to do with the remote branch or tag name, and/or the local ref:

#! /bin/sh

NULLSHA=0000000000000000000000000000000000000000 # 40 0's

say() {
    echo "$@" 1>&2
}

while read localref localhash foreignref foreignhash; do
    case $foreignref in
    refs/heads/*)
        reftype=branch
        shortref=${foreignref#refs/heads/};;
    refs/tags/*)
        reftype=tag
        shortref=${foreignref#refs/tags/};;
    *)  reftype=unknown-ref-type
        shortref=$foreignref;;
    esac
    say "pushing to $reftype $shortref"
    case $localhash,$foreignhash in
    $NULLSHA,*) say "(push with intent to delete)";;
    *,$NULLSHA) say "(push with intent to create)";;
    *) say "(push with intent to update from $foreignhash to $localhash)"
       # for branch updates only, let's see if it's a fast-forward
       if [ $reftype = branch ]; then
           if git merge-base --is-ancestor $foreignhash $localhash; then
               say "(this is a fast-forward)"
           else
               say "(this can only work if forced)"
           fi
       fi;;
    esac
done

(note: this is not well tested).

Upvotes: 0

yorammi
yorammi

Reputation: 6458

while read oldrev newrev refname 
do
     if [[ "$newrev" != "0000000000000000000000000000000000000000" ]] ; then
          branch=${refname#refs/heads/} 
     fi
done

Upvotes: 1

Related Questions