12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- #!/opt/mcfi/venv/bin/python3
- #
- # This is a script for notifying the forge when PUSH happens. It's called after
- # a "git push" by the post-receive hook.
- from pygit2 import Repository
- import os, requests, sys
- if len(sys.argv) != 4:
- print(sys.argv[0] + ': Bad input arguments.')
- exit()
- # ENV variable set by Git
- GIT_DIR = os.environ['GIT_DIR']
- # ENV variable set by the webserver (HTTPD) when it authenticated the user
- # before allowing to push content via HTTPS. Probably empty if pushed via
- # SSH.
- # TODO check if SSH sets any kind of "USER" variable
- REMOTE_USER = os.environ['REMOTE_USER']
- # Arguments passed from the git post-receive hook
- oldrev = sys.argv[1]
- newrev = sys.argv[2]
- refname = sys.argv[3]
- # Get absolute path. By default Git chdir to repository's .git folder.
- repository_path = os.path.abspath(GIT_DIR)
- # Split at the second / from the right and remove .git.
- # Example: /var/lib/gitolite3/repositories/zplus/repo.git => zplus/repo
- repository_name = '/'.join(repository_path.rsplit('/', 2)[-2:])[:-4]
- repository = Repository(repository_path)
- # Retrieve a Git object.
- # https://www.pygit2.org/objects.html
- commit = repository.get(newrev)
- # Collect commits
- commits = []
- while True:
- if commit.id.__str__() == oldrev:
- break
-
- commits.append({
- 'id': commit.id.__str__(),
- 'author': {
- 'name': commit.author.name,
- 'email': commit.author.email,
- 'time': commit.author.time
- },
- 'committer': {
- 'name': commit.committer.name,
- 'email': commit.committer.email,
- 'time': commit.committer.time
- },
- 'message': commit.message,
- 'commit_time': commit.commit_time
- })
-
- if commit.parents is None or len(commit.parents) == 0:
- break
-
- commit = commit.parents[0]
- # Callback the local forge to notify the git push
- requests.post('http://localhost:9000/git-hooks-post-receive', json={
- 'REMOTE_USER': REMOTE_USER,
- 'GIT_DIR': GIT_DIR,
- 'oldrev': oldrev,
- 'newrev': newrev,
- 'refname': refname,
- 'repository_name': repository_name,
- 'commits': commits })
|