##// END OF EJS Templates
save full raw id in push log data for much faster revision lookup
marcink -
r2324:1dbf0773 beta
parent child Browse files
Show More
@@ -1,190 +1,192 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 rhodecode.lib.hooks
3 rhodecode.lib.hooks
4 ~~~~~~~~~~~~~~~~~~~
4 ~~~~~~~~~~~~~~~~~~~
5
5
6 Hooks runned by rhodecode
6 Hooks runned by rhodecode
7
7
8 :created_on: Aug 6, 2010
8 :created_on: Aug 6, 2010
9 :author: marcink
9 :author: marcink
10 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
10 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
11 :license: GPLv3, see COPYING for more details.
11 :license: GPLv3, see COPYING for more details.
12 """
12 """
13 # This program is free software: you can redistribute it and/or modify
13 # This program is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation, either version 3 of the License, or
15 # the Free Software Foundation, either version 3 of the License, or
16 # (at your option) any later version.
16 # (at your option) any later version.
17 #
17 #
18 # This program is distributed in the hope that it will be useful,
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
21 # GNU General Public License for more details.
22 #
22 #
23 # You should have received a copy of the GNU General Public License
23 # You should have received a copy of the GNU General Public License
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 import os
25 import os
26 import sys
26 import sys
27 import binascii
28 from inspect import isfunction
27
29
28 from mercurial.scmutil import revrange
30 from mercurial.scmutil import revrange
29 from mercurial.node import nullrev
31 from mercurial.node import nullrev
32
30 from rhodecode import EXTENSIONS
33 from rhodecode import EXTENSIONS
31 from rhodecode.lib import helpers as h
34 from rhodecode.lib import helpers as h
32 from rhodecode.lib.utils import action_logger
35 from rhodecode.lib.utils import action_logger
33 from inspect import isfunction
34
36
35
37
36 def _get_scm_size(alias, root_path):
38 def _get_scm_size(alias, root_path):
37
39
38 if not alias.startswith('.'):
40 if not alias.startswith('.'):
39 alias += '.'
41 alias += '.'
40
42
41 size_scm, size_root = 0, 0
43 size_scm, size_root = 0, 0
42 for path, dirs, files in os.walk(root_path):
44 for path, dirs, files in os.walk(root_path):
43 if path.find(alias) != -1:
45 if path.find(alias) != -1:
44 for f in files:
46 for f in files:
45 try:
47 try:
46 size_scm += os.path.getsize(os.path.join(path, f))
48 size_scm += os.path.getsize(os.path.join(path, f))
47 except OSError:
49 except OSError:
48 pass
50 pass
49 else:
51 else:
50 for f in files:
52 for f in files:
51 try:
53 try:
52 size_root += os.path.getsize(os.path.join(path, f))
54 size_root += os.path.getsize(os.path.join(path, f))
53 except OSError:
55 except OSError:
54 pass
56 pass
55
57
56 size_scm_f = h.format_byte_size(size_scm)
58 size_scm_f = h.format_byte_size(size_scm)
57 size_root_f = h.format_byte_size(size_root)
59 size_root_f = h.format_byte_size(size_root)
58 size_total_f = h.format_byte_size(size_root + size_scm)
60 size_total_f = h.format_byte_size(size_root + size_scm)
59
61
60 return size_scm_f, size_root_f, size_total_f
62 return size_scm_f, size_root_f, size_total_f
61
63
62
64
63 def repo_size(ui, repo, hooktype=None, **kwargs):
65 def repo_size(ui, repo, hooktype=None, **kwargs):
64 """
66 """
65 Presents size of repository after push
67 Presents size of repository after push
66
68
67 :param ui:
69 :param ui:
68 :param repo:
70 :param repo:
69 :param hooktype:
71 :param hooktype:
70 """
72 """
71
73
72 size_hg_f, size_root_f, size_total_f = _get_scm_size('.hg', repo.root)
74 size_hg_f, size_root_f, size_total_f = _get_scm_size('.hg', repo.root)
73
75
74 last_cs = repo[len(repo) - 1]
76 last_cs = repo[len(repo) - 1]
75
77
76 msg = ('Repository size .hg:%s repo:%s total:%s\n'
78 msg = ('Repository size .hg:%s repo:%s total:%s\n'
77 'Last revision is now r%s:%s\n') % (
79 'Last revision is now r%s:%s\n') % (
78 size_hg_f, size_root_f, size_total_f, last_cs.rev(), last_cs.hex()[:12]
80 size_hg_f, size_root_f, size_total_f, last_cs.rev(), last_cs.hex()[:12]
79 )
81 )
80
82
81 sys.stdout.write(msg)
83 sys.stdout.write(msg)
82
84
83
85
84 def log_pull_action(ui, repo, **kwargs):
86 def log_pull_action(ui, repo, **kwargs):
85 """
87 """
86 Logs user last pull action
88 Logs user last pull action
87
89
88 :param ui:
90 :param ui:
89 :param repo:
91 :param repo:
90 """
92 """
91
93
92 extras = dict(repo.ui.configitems('rhodecode_extras'))
94 extras = dict(repo.ui.configitems('rhodecode_extras'))
93 username = extras['username']
95 username = extras['username']
94 repository = extras['repository']
96 repository = extras['repository']
95 scm = extras['scm']
97 scm = extras['scm']
96 action = 'pull'
98 action = 'pull'
97
99
98 action_logger(username, action, repository, extras['ip'], commit=True)
100 action_logger(username, action, repository, extras['ip'], commit=True)
99 # extension hook call
101 # extension hook call
100 callback = getattr(EXTENSIONS, 'PULL_HOOK', None)
102 callback = getattr(EXTENSIONS, 'PULL_HOOK', None)
101
103
102 if isfunction(callback):
104 if isfunction(callback):
103 kw = {}
105 kw = {}
104 kw.update(extras)
106 kw.update(extras)
105 callback(**kw)
107 callback(**kw)
106 return 0
108 return 0
107
109
108
110
109 def log_push_action(ui, repo, **kwargs):
111 def log_push_action(ui, repo, **kwargs):
110 """
112 """
111 Maps user last push action to new changeset id, from mercurial
113 Maps user last push action to new changeset id, from mercurial
112
114
113 :param ui:
115 :param ui:
114 :param repo: repo object containing the `ui` object
116 :param repo: repo object containing the `ui` object
115 """
117 """
116
118
117 extras = dict(repo.ui.configitems('rhodecode_extras'))
119 extras = dict(repo.ui.configitems('rhodecode_extras'))
118 username = extras['username']
120 username = extras['username']
119 repository = extras['repository']
121 repository = extras['repository']
120 action = extras['action'] + ':%s'
122 action = extras['action'] + ':%s'
121 scm = extras['scm']
123 scm = extras['scm']
122
124
123 if scm == 'hg':
125 if scm == 'hg':
124 node = kwargs['node']
126 node = kwargs['node']
125
127
126 def get_revs(repo, rev_opt):
128 def get_revs(repo, rev_opt):
127 if rev_opt:
129 if rev_opt:
128 revs = revrange(repo, rev_opt)
130 revs = revrange(repo, rev_opt)
129
131
130 if len(revs) == 0:
132 if len(revs) == 0:
131 return (nullrev, nullrev)
133 return (nullrev, nullrev)
132 return (max(revs), min(revs))
134 return (max(revs), min(revs))
133 else:
135 else:
134 return (len(repo) - 1, 0)
136 return (len(repo) - 1, 0)
135
137
136 stop, start = get_revs(repo, [node + ':'])
138 stop, start = get_revs(repo, [node + ':'])
137
139 h = binascii.hexlify
138 revs = (str(repo[r]) for r in xrange(start, stop + 1))
140 revs = (h(repo[r].node()) for r in xrange(start, stop + 1))
139 elif scm == 'git':
141 elif scm == 'git':
140 revs = []
142 revs = []
141
143
142 action = action % ','.join(revs)
144 action = action % ','.join(revs)
143
145
144 action_logger(username, action, repository, extras['ip'], commit=True)
146 action_logger(username, action, repository, extras['ip'], commit=True)
145
147
146 # extension hook call
148 # extension hook call
147 callback = getattr(EXTENSIONS, 'PUSH_HOOK', None)
149 callback = getattr(EXTENSIONS, 'PUSH_HOOK', None)
148 if isfunction(callback):
150 if isfunction(callback):
149 kw = {'pushed_revs': revs}
151 kw = {'pushed_revs': revs}
150 kw.update(extras)
152 kw.update(extras)
151 callback(**kw)
153 callback(**kw)
152 return 0
154 return 0
153
155
154
156
155 def log_create_repository(repository_dict, created_by, **kwargs):
157 def log_create_repository(repository_dict, created_by, **kwargs):
156 """
158 """
157 Post create repository Hook. This is a dummy function for admins to re-use
159 Post create repository Hook. This is a dummy function for admins to re-use
158 if needed. It's taken from rhodecode-extensions module and executed
160 if needed. It's taken from rhodecode-extensions module and executed
159 if present
161 if present
160
162
161 :param repository: dict dump of repository object
163 :param repository: dict dump of repository object
162 :param created_by: username who created repository
164 :param created_by: username who created repository
163 :param created_date: date of creation
165 :param created_date: date of creation
164
166
165 available keys of repository_dict:
167 available keys of repository_dict:
166
168
167 'repo_type',
169 'repo_type',
168 'description',
170 'description',
169 'private',
171 'private',
170 'created_on',
172 'created_on',
171 'enable_downloads',
173 'enable_downloads',
172 'repo_id',
174 'repo_id',
173 'user_id',
175 'user_id',
174 'enable_statistics',
176 'enable_statistics',
175 'clone_uri',
177 'clone_uri',
176 'fork_id',
178 'fork_id',
177 'group_id',
179 'group_id',
178 'repo_name'
180 'repo_name'
179
181
180 """
182 """
181
183
182 callback = getattr(EXTENSIONS, 'CREATE_REPO_HOOK', None)
184 callback = getattr(EXTENSIONS, 'CREATE_REPO_HOOK', None)
183 if isfunction(callback):
185 if isfunction(callback):
184 kw = {}
186 kw = {}
185 kw.update(repository_dict)
187 kw.update(repository_dict)
186 kw.update({'created_by': created_by})
188 kw.update({'created_by': created_by})
187 kw.update(kwargs)
189 kw.update(kwargs)
188 return callback(**kw)
190 return callback(**kw)
189
191
190 return 0
192 return 0
General Comments 0
You need to be logged in to leave comments. Login now