extract_pre_commits.py
91 lines
| 2.8 KiB
| text/x-python
|
PythonLexer
r3133 | # -*- coding: utf-8 -*- | |||
r4306 | # Copyright (C) 2016-2020 RhodeCode GmbH | |||
r3133 | # | |||
# This program is free software: you can redistribute it and/or modify | ||||
# it under the terms of the GNU Affero General Public License, version 3 | ||||
# (only), as published by the Free Software Foundation. | ||||
# | ||||
# This program is distributed in the hope that it will be useful, | ||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
# | ||||
# You should have received a copy of the GNU Affero General Public License | ||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
# | ||||
# This program is dual-licensed. If you wish to learn more about the | ||||
# RhodeCode Enterprise Edition, including its added features, Support services, | ||||
# and proprietary license terms, please see https://rhodecode.com/licenses/ | ||||
""" | ||||
us in hooks:: | ||||
from .helpers import extract_pre_commits | ||||
# returns list of dicts with key-val fetched from extra fields | ||||
commit_list = extract_pre_commits.run(**kwargs) | ||||
""" | ||||
import re | ||||
import collections | ||||
r3211 | import json | |||
r3133 | ||||
def get_hg_commits(repo, refs): | ||||
commits = [] | ||||
return commits | ||||
def get_git_commits(repo, refs): | ||||
commits = [] | ||||
r3211 | ||||
for data in refs: | ||||
# we should now extract commit data | ||||
old_rev = data['old_rev'] | ||||
new_rev = data['new_rev'] | ||||
if '00000000' in old_rev: | ||||
# new branch, we don't need to extract nothing | ||||
return commits | ||||
git_env = dict(data['git_env']) | ||||
r3414 | # https://github.com/git/git/blob/master/Documentation/pretty-formats.txt | |||
r3211 | cmd = [ | |||
'log', | ||||
r3414 | '--pretty=format:{"commit_id": "%H", "author": "%aN <%aE>", "date": "%ad", "message": "%s"}', | |||
r3211 | '{}...{}'.format(old_rev, new_rev) | |||
] | ||||
r3212 | stdout, stderr = repo.run_git_command(cmd, extra_env=git_env) | |||
r3211 | for line in stdout.splitlines(): | |||
try: | ||||
data = json.loads(line) | ||||
commits.append(data) | ||||
except Exception: | ||||
print('Failed to load data from GIT line') | ||||
r3133 | return commits | |||
def run(*args, **kwargs): | ||||
from rhodecode.model.db import Repository | ||||
vcs_type = kwargs['scm'] | ||||
# use temp name then the main one propagated | ||||
repo_name = kwargs.pop('REPOSITORY', None) or kwargs['repository'] | ||||
repo = Repository.get_by_repo_name(repo_name) | ||||
vcs_repo = repo.scm_instance(cache=False) | ||||
commits = [] | ||||
if vcs_type == 'git': | ||||
r3211 | for rev_data in kwargs['commit_ids']: | |||
new_environ = dict((k, v) for k, v in rev_data['git_env']) | ||||
r3133 | commits = get_git_commits(vcs_repo, kwargs['commit_ids']) | |||
if vcs_type == 'hg': | ||||
r3211 | for rev_data in kwargs['commit_ids']: | |||
new_environ = dict((k, v) for k, v in rev_data['hg_env']) | ||||
r3133 | commits = get_hg_commits(vcs_repo, kwargs['commit_ids']) | |||
return commits | ||||