# HG changeset patch # User Marcin Kuzminski # Date 2018-05-14 12:21:25 # Node ID 74eb96f26110042fabce0419d1ad6e65bdc48244 # Parent b6e06d501bfe37b9d3114d481f35baf1121ab64c svn: make hooks safe and fully backward compatible. - fixes problem with older protocol hooks that doesn't support given methods. - in cases of older SVN we should simply skip, and not fail. diff --git a/vcsserver/hooks.py b/vcsserver/hooks.py --- a/vcsserver/hooks.py +++ b/vcsserver/hooks.py @@ -483,17 +483,33 @@ def git_post_receive(unused_repo_path, r return _call_hook('post_push', extras, GitMessageWriter()) +def _get_extras_from_txn_id(path, txn_id): + extras = {} + try: + cmd = ['svnlook', 'pget', + '-t', txn_id, + '--revprop', path, 'rc-scm-extras'] + stdout, stderr = subprocessio.run_command( + cmd, env=os.environ.copy()) + extras = json.loads(base64.urlsafe_b64decode(stdout)) + except Exception: + log.exception('Failed to extract extras info from txn_id') + + return extras + + def svn_pre_commit(repo_path, commit_data, env): path, txn_id = commit_data branches = [] tags = [] - cmd = ['svnlook', 'pget', - '-t', txn_id, - '--revprop', path, 'rc-scm-extras'] - stdout, stderr = subprocessio.run_command( - cmd, env=os.environ.copy()) - extras = json.loads(base64.urlsafe_b64decode(stdout)) + if env.get('RC_SCM_DATA'): + extras = json.loads(env['RC_SCM_DATA']) + else: + # fallback method to read from TXN-ID stored data + extras = _get_extras_from_txn_id(path, txn_id) + if not extras: + return 0 extras['commit_ids'] = [] extras['txn_id'] = txn_id @@ -502,10 +518,25 @@ def svn_pre_commit(repo_path, commit_dat 'bookmarks': [], 'tags': tags, } - sys.stderr.write(str(extras)) + return _call_hook('pre_push', extras, SvnMessageWriter()) +def _get_extras_from_commit_id(commit_id, path): + extras = {} + try: + cmd = ['svnlook', 'pget', + '-r', commit_id, + '--revprop', path, 'rc-scm-extras'] + stdout, stderr = subprocessio.run_command( + cmd, env=os.environ.copy()) + extras = json.loads(base64.urlsafe_b64decode(stdout)) + except Exception: + log.exception('Failed to extract extras info from commit_id') + + return extras + + def svn_post_commit(repo_path, commit_data, env): """ commit_data is path, rev, txn_id @@ -514,13 +545,13 @@ def svn_post_commit(repo_path, commit_da branches = [] tags = [] - cmd = ['svnlook', 'pget', - '-r', commit_id, - '--revprop', path, 'rc-scm-extras'] - stdout, stderr = subprocessio.run_command( - cmd, env=os.environ.copy()) - - extras = json.loads(base64.urlsafe_b64decode(stdout)) + if env.get('RC_SCM_DATA'): + extras = json.loads(env['RC_SCM_DATA']) + else: + # fallback method to read from TXN-ID stored data + extras = _get_extras_from_commit_id(commit_id, path) + if not extras: + return 0 extras['commit_ids'] = [commit_id] extras['txn_id'] = txn_id @@ -533,9 +564,7 @@ def svn_post_commit(repo_path, commit_da if 'repo_size' in extras['hooks']: try: _call_hook('repo_size', extras, SvnMessageWriter()) - except: + except Exception: pass return _call_hook('post_push', extras, SvnMessageWriter()) - -