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