svn: added example to validate SVN file size and paths.
This commit is contained in:
parent
8501ae81be
commit
cdbfcd99f7
2 changed files with 58 additions and 9 deletions
|
|
@ -28,8 +28,8 @@ def _pre_push_hook(*args, **kwargs):
|
|||
[{u'hg_env|git_env': ...,
|
||||
u'multiple_heads': [],
|
||||
u'name': u'default',
|
||||
u'new_rev': u'd0befe0692e722e01d5677f27a104631cf798b69',
|
||||
u'old_rev': u'd0befe0692e722e01d5677f27a104631cf798b69',
|
||||
u'new_rev': u'd0b2ae0692e722e01d5677f27a104631cf798b69',
|
||||
u'old_rev': u'd0b1ae0692e722e01d5677f27a104631cf798b69',
|
||||
u'ref': u'',
|
||||
u'total_commits': 2,
|
||||
u'type': u'branch'}]
|
||||
|
|
@ -47,13 +47,17 @@ def _pre_push_hook(*args, **kwargs):
|
|||
forbid_files = repo_extra_fields.get('forbid_files_glob', {}).get('field_value')
|
||||
forbid_files = aslist(forbid_files)
|
||||
|
||||
# forbid_files = ['*'] # example pattern
|
||||
|
||||
# optionally get bytes limit for a single file, e.g 1024 for 1KB
|
||||
forbid_size_over = repo_extra_fields.get('forbid_size_over', {}).get('field_value')
|
||||
forbid_size_over = int(forbid_size_over or 0)
|
||||
|
||||
# forbid_size_over = 1024 # example 1024
|
||||
|
||||
def validate_file_name_and_size(file_data, forbidden_files=None, size_limit=None):
|
||||
"""
|
||||
This function validates commited files against some sort of rules.
|
||||
This function validates comited files against some sort of rules.
|
||||
It should return a valid boolean, and a reason for failure
|
||||
|
||||
file_data =[
|
||||
|
|
@ -87,7 +91,10 @@ def _pre_push_hook(*args, **kwargs):
|
|||
|
||||
# validate A(dded) files and size
|
||||
if size_limit and operation == 'A':
|
||||
size = len(file_data['raw_diff'])
|
||||
if 'file_size' in file_data:
|
||||
size = file_data['file_size']
|
||||
else:
|
||||
size = len(file_data['raw_diff'])
|
||||
|
||||
reason = 'File {} size of {} bytes exceeds limit {}'.format(
|
||||
file_name, format_byte_size_binary(size),
|
||||
|
|
|
|||
|
|
@ -34,12 +34,51 @@ from rhodecode.lib.vcs.backends.hg.diff import MercurialDiff
|
|||
from rhodecode.lib.vcs.backends.git.diff import GitDiff
|
||||
|
||||
|
||||
def get_hg_files(repo, refs):
|
||||
def get_svn_files(repo, vcs_repo, refs):
|
||||
txn_id = refs[0]
|
||||
files = []
|
||||
stdout, stderr = vcs_repo.run_svn_command(
|
||||
['svnlook', 'changed', repo.repo_full_path, '--transaction', txn_id])
|
||||
|
||||
svn_op_to_rc_op = {
|
||||
'A': 'A',
|
||||
'U': 'M',
|
||||
'D': 'D',
|
||||
}
|
||||
|
||||
for entry in stdout.splitlines():
|
||||
parsed_entry = {
|
||||
'raw_diff': '',
|
||||
'filename': '',
|
||||
'chunks': [],
|
||||
'ops': {},
|
||||
'file_size': 0
|
||||
}
|
||||
|
||||
op = entry[0]
|
||||
path = entry[1:].strip()
|
||||
|
||||
rc_op = svn_op_to_rc_op.get(op) or '?'
|
||||
parsed_entry['filename'] = path
|
||||
parsed_entry['operation'] = rc_op
|
||||
|
||||
if rc_op in ['A', 'M']:
|
||||
stdout, stderr = vcs_repo.run_svn_command(
|
||||
['svnlook', 'filesize', repo.repo_full_path, path, '--transaction', txn_id])
|
||||
file_size = int(stdout.strip())
|
||||
parsed_entry['file_size'] = file_size
|
||||
|
||||
files.append(parsed_entry)
|
||||
|
||||
return files
|
||||
|
||||
|
||||
def get_hg_files(repo, vcs_repo, refs):
|
||||
files = []
|
||||
return files
|
||||
|
||||
|
||||
def get_git_files(repo, refs):
|
||||
def get_git_files(repo, vcs_repo, refs):
|
||||
files = []
|
||||
|
||||
for data in refs:
|
||||
|
|
@ -57,7 +96,7 @@ def get_git_files(repo, refs):
|
|||
'diff', old_rev, new_rev
|
||||
]
|
||||
|
||||
stdout, stderr = repo.run_git_command(cmd, extra_env=git_env)
|
||||
stdout, stderr = vcs_repo.run_git_command(cmd, extra_env=git_env)
|
||||
vcs_diff = GitDiff(stdout)
|
||||
|
||||
diff_processor = diffs.DiffProcessor(vcs_diff, format='newdiff')
|
||||
|
|
@ -86,11 +125,14 @@ def run(*args, **kwargs):
|
|||
if vcs_type == 'git':
|
||||
for rev_data in kwargs['commit_ids']:
|
||||
new_environ = dict((k, v) for k, v in rev_data['git_env'])
|
||||
files = get_git_files(vcs_repo, kwargs['commit_ids'])
|
||||
files = get_git_files(repo, vcs_repo, kwargs['commit_ids'])
|
||||
|
||||
if vcs_type == 'hg':
|
||||
for rev_data in kwargs['commit_ids']:
|
||||
new_environ = dict((k, v) for k, v in rev_data['hg_env'])
|
||||
files = get_hg_files(vcs_repo, kwargs['commit_ids'])
|
||||
files = get_hg_files(repo, vcs_repo, kwargs['commit_ids'])
|
||||
|
||||
if vcs_type == 'svn':
|
||||
files = get_svn_files(repo, vcs_repo, kwargs['commit_ids'])
|
||||
|
||||
return files
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue