Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

githooks - git post-receive hook that grabs commit messages and posts back to URL

We are using a ticketing system that I want to automatically update as developers push their changes to the server. In order to update it, I only need to provide a specific URL with the commit message as a GET variable. The page being called will then log this change. I know my way to go is with hooks, but I am not familiar with Bash nor Perl so it is quite challenging.

I want to achieve this:

  • Developer PUSHes to the server
  • post-receive hook runs and checks which different commits are new (because there could be several in one push)
  • It loops through them, and for each commit, it will open a URL with the commit message (curl http://server.com/logthis.asp?msg=Here_goes_the_commit_message, something like that)

That's it. Although I have checked out some samples related to this kind of idea, none do exactly this. How could this be done?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The main PITA is to isolate the correct list of new revisions, which I borrowed from /usr/share/doc/git/contrib/hooks/post-receive-email(show_new_revisions).

while read oval nval ref ; do
    if expr "$ref" : "^refs/heads/"; then
        if expr "$oval" : '0*$' >/dev/null
        then
            revspec=$nval
        else
            revspec=$oval..$nval
        fi
        other_branches=$(git for-each-ref --format='%(refname)' refs/heads/ |
            grep -F -v $ref)

        # You may want to collect the revisions to sort out
        # duplicates before the transmission to the bugtracker,
        # but not sorting is easier ;-)
        for revision in `git rev-parse --not $other_branches | git rev-list --stdin $revspec`; do
                    # I don't know if you need to url-escape the content
                    # Also you may want to transmit the data in a POST request,
            wget "http://server.com/logthis.asp?msg=$(git log $revision~1..$revision)"
        done
    fi
done

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...