开发者

Get all commits when pushing to server

开发者 https://www.devze.com 2023-03-11 05:17 出处:网络
I am making a git post-commit hook to post my commit messages to Twitter.I have set up the hook on the server, which means it only runs when I call git push.

I am making a git post-commit hook to post my commit messages to Twitter. I have set up the hook on the server, which means it only runs when I call git push.

To interface with git from python, I am using GitPython. In my code, I am using repo.head.commit.message to get the latest commit message. Which means if I push multiple commits, it only gets the last one.

This is what I have so far.

class GITHelper:
    "This class interacts with GIT for us"
    def __init__(self, path):
        repo = git.Repo(path)
        headcommit = repo.head.commit
        self.message = headcommit.message
        self.author = headcommit.author.name

How can I get all commits from a push? Or, how can I get the number of commits that were pushed?

repo.iter_commits('master', max_count=5) can get as many commits as I want, so if I knew how many commits there were, I could use that.

EDIT: I was testing, and when I run git push, it seems this hook gets the head from the last commit, not the one I just pushed. How do I make a post-commit hook that gets the messages from the commits I just pushed to the server?

EDIT 2: I am actually using the update hook, not the post-commit hook, is that the correct hook to use on the serv开发者_StackOverflower?


The githooks docs say:

The hook executes once for each ref to be updated, and takes three parameters:
  - the name of the ref being updated,
  - the old object name stored in the ref,
  - and the new objectname to be stored in the ref.

So, check the arguments your script gets, you should also get the new ref, and then you can figure out the commits in between the old and new refs. If it were a shell script, you could do:

git log --oneline $oldRef..$newRef
0

精彩评论

暂无评论...
验证码 换一张
取 消