Show More
@@ -1,193 +1,193 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | import os |
|
17 | import os | |
18 | import sys |
|
18 | import sys | |
19 | import tempfile |
|
19 | import tempfile | |
20 | import logging |
|
20 | import logging | |
21 | import urllib.parse |
|
21 | import urllib.parse | |
22 |
|
22 | |||
23 | from vcsserver.lib.rc_cache.archive_cache import get_archival_cache_store |
|
23 | from vcsserver.lib.rc_cache.archive_cache import get_archival_cache_store | |
24 |
|
24 | |||
25 | from vcsserver import exceptions |
|
25 | from vcsserver import exceptions | |
26 | from vcsserver.exceptions import NoContentException |
|
26 | from vcsserver.exceptions import NoContentException | |
27 | from vcsserver.hgcompat import archival |
|
27 | from vcsserver.hgcompat import archival | |
28 | from vcsserver.str_utils import safe_bytes |
|
28 | from vcsserver.str_utils import safe_bytes | |
29 | from vcsserver.lib.exc_tracking import format_exc |
|
29 | from vcsserver.lib.exc_tracking import format_exc | |
30 | log = logging.getLogger(__name__) |
|
30 | log = logging.getLogger(__name__) | |
31 |
|
31 | |||
32 |
|
32 | |||
33 |
class RepoFactory |
|
33 | class RepoFactory: | |
34 | """ |
|
34 | """ | |
35 | Utility to create instances of repository |
|
35 | Utility to create instances of repository | |
36 |
|
36 | |||
37 | It provides internal caching of the `repo` object based on |
|
37 | It provides internal caching of the `repo` object based on | |
38 | the :term:`call context`. |
|
38 | the :term:`call context`. | |
39 | """ |
|
39 | """ | |
40 | repo_type = None |
|
40 | repo_type = None | |
41 |
|
41 | |||
42 | def __init__(self): |
|
42 | def __init__(self): | |
43 | pass |
|
43 | pass | |
44 |
|
44 | |||
45 | def _create_config(self, path, config): |
|
45 | def _create_config(self, path, config): | |
46 | config = {} |
|
46 | config = {} | |
47 | return config |
|
47 | return config | |
48 |
|
48 | |||
49 | def _create_repo(self, wire, create): |
|
49 | def _create_repo(self, wire, create): | |
50 | raise NotImplementedError() |
|
50 | raise NotImplementedError() | |
51 |
|
51 | |||
52 | def repo(self, wire, create=False): |
|
52 | def repo(self, wire, create=False): | |
53 | raise NotImplementedError() |
|
53 | raise NotImplementedError() | |
54 |
|
54 | |||
55 |
|
55 | |||
56 | def obfuscate_qs(query_string): |
|
56 | def obfuscate_qs(query_string): | |
57 | if query_string is None: |
|
57 | if query_string is None: | |
58 | return None |
|
58 | return None | |
59 |
|
59 | |||
60 | parsed = [] |
|
60 | parsed = [] | |
61 | for k, v in urllib.parse.parse_qsl(query_string, keep_blank_values=True): |
|
61 | for k, v in urllib.parse.parse_qsl(query_string, keep_blank_values=True): | |
62 | if k in ['auth_token', 'api_key']: |
|
62 | if k in ['auth_token', 'api_key']: | |
63 | v = "*****" |
|
63 | v = "*****" | |
64 | parsed.append((k, v)) |
|
64 | parsed.append((k, v)) | |
65 |
|
65 | |||
66 | return '&'.join('{}{}'.format( |
|
66 | return '&'.join('{}{}'.format( | |
67 | k, f'={v}' if v else '') for k, v in parsed) |
|
67 | k, f'={v}' if v else '') for k, v in parsed) | |
68 |
|
68 | |||
69 |
|
69 | |||
70 | def raise_from_original(new_type, org_exc: Exception): |
|
70 | def raise_from_original(new_type, org_exc: Exception): | |
71 | """ |
|
71 | """ | |
72 | Raise a new exception type with original args and traceback. |
|
72 | Raise a new exception type with original args and traceback. | |
73 | """ |
|
73 | """ | |
74 | exc_info = sys.exc_info() |
|
74 | exc_info = sys.exc_info() | |
75 | exc_type, exc_value, exc_traceback = exc_info |
|
75 | exc_type, exc_value, exc_traceback = exc_info | |
76 | new_exc = new_type(*exc_value.args) |
|
76 | new_exc = new_type(*exc_value.args) | |
77 |
|
77 | |||
78 | # store the original traceback into the new exc |
|
78 | # store the original traceback into the new exc | |
79 | new_exc._org_exc_tb = format_exc(exc_info) |
|
79 | new_exc._org_exc_tb = format_exc(exc_info) | |
80 |
|
80 | |||
81 | try: |
|
81 | try: | |
82 | raise new_exc.with_traceback(exc_traceback) |
|
82 | raise new_exc.with_traceback(exc_traceback) | |
83 | finally: |
|
83 | finally: | |
84 | del exc_traceback |
|
84 | del exc_traceback | |
85 |
|
85 | |||
86 |
|
86 | |||
87 |
class ArchiveNode |
|
87 | class ArchiveNode: | |
88 | def __init__(self, path, mode, is_link, raw_bytes): |
|
88 | def __init__(self, path, mode, is_link, raw_bytes): | |
89 | self.path = path |
|
89 | self.path = path | |
90 | self.mode = mode |
|
90 | self.mode = mode | |
91 | self.is_link = is_link |
|
91 | self.is_link = is_link | |
92 | self.raw_bytes = raw_bytes |
|
92 | self.raw_bytes = raw_bytes | |
93 |
|
93 | |||
94 |
|
94 | |||
95 | def store_archive_in_cache(node_walker, archive_key, kind, mtime, archive_at_path, archive_dir_name, |
|
95 | def store_archive_in_cache(node_walker, archive_key, kind, mtime, archive_at_path, archive_dir_name, | |
96 | commit_id, write_metadata=True, extra_metadata=None, cache_config=None): |
|
96 | commit_id, write_metadata=True, extra_metadata=None, cache_config=None): | |
97 | """ |
|
97 | """ | |
98 | Function that would store generate archive and send it to a dedicated backend store |
|
98 | Function that would store generate archive and send it to a dedicated backend store | |
99 | In here we use diskcache |
|
99 | In here we use diskcache | |
100 |
|
100 | |||
101 | :param node_walker: a generator returning nodes to add to archive |
|
101 | :param node_walker: a generator returning nodes to add to archive | |
102 | :param archive_key: key used to store the path |
|
102 | :param archive_key: key used to store the path | |
103 | :param kind: archive kind |
|
103 | :param kind: archive kind | |
104 | :param mtime: time of creation |
|
104 | :param mtime: time of creation | |
105 | :param archive_at_path: default '/' the path at archive was started. |
|
105 | :param archive_at_path: default '/' the path at archive was started. | |
106 | If this is not '/' it means it's a partial archive |
|
106 | If this is not '/' it means it's a partial archive | |
107 | :param archive_dir_name: inside dir name when creating an archive |
|
107 | :param archive_dir_name: inside dir name when creating an archive | |
108 | :param commit_id: commit sha of revision archive was created at |
|
108 | :param commit_id: commit sha of revision archive was created at | |
109 | :param write_metadata: |
|
109 | :param write_metadata: | |
110 | :param extra_metadata: |
|
110 | :param extra_metadata: | |
111 | :param cache_config: |
|
111 | :param cache_config: | |
112 |
|
112 | |||
113 | walker should be a file walker, for example, |
|
113 | walker should be a file walker, for example, | |
114 | def node_walker(): |
|
114 | def node_walker(): | |
115 | for file_info in files: |
|
115 | for file_info in files: | |
116 | yield ArchiveNode(fn, mode, is_link, ctx[fn].data) |
|
116 | yield ArchiveNode(fn, mode, is_link, ctx[fn].data) | |
117 | """ |
|
117 | """ | |
118 | extra_metadata = extra_metadata or {} |
|
118 | extra_metadata = extra_metadata or {} | |
119 |
|
119 | |||
120 | d_cache = get_archival_cache_store(config=cache_config) |
|
120 | d_cache = get_archival_cache_store(config=cache_config) | |
121 |
|
121 | |||
122 | if archive_key in d_cache: |
|
122 | if archive_key in d_cache: | |
123 | with d_cache as d_cache_reader: |
|
123 | with d_cache as d_cache_reader: | |
124 | reader, tag = d_cache_reader.get(archive_key, read=True, tag=True, retry=True) |
|
124 | reader, tag = d_cache_reader.get(archive_key, read=True, tag=True, retry=True) | |
125 | return reader.name |
|
125 | return reader.name | |
126 |
|
126 | |||
127 | archive_tmp_path = safe_bytes(tempfile.mkstemp()[1]) |
|
127 | archive_tmp_path = safe_bytes(tempfile.mkstemp()[1]) | |
128 | log.debug('Creating new temp archive in %s', archive_tmp_path) |
|
128 | log.debug('Creating new temp archive in %s', archive_tmp_path) | |
129 |
|
129 | |||
130 | if kind == "tgz": |
|
130 | if kind == "tgz": | |
131 | archiver = archival.tarit(archive_tmp_path, mtime, b"gz") |
|
131 | archiver = archival.tarit(archive_tmp_path, mtime, b"gz") | |
132 | elif kind == "tbz2": |
|
132 | elif kind == "tbz2": | |
133 | archiver = archival.tarit(archive_tmp_path, mtime, b"bz2") |
|
133 | archiver = archival.tarit(archive_tmp_path, mtime, b"bz2") | |
134 | elif kind == 'zip': |
|
134 | elif kind == 'zip': | |
135 | archiver = archival.zipit(archive_tmp_path, mtime) |
|
135 | archiver = archival.zipit(archive_tmp_path, mtime) | |
136 | else: |
|
136 | else: | |
137 | raise exceptions.ArchiveException()( |
|
137 | raise exceptions.ArchiveException()( | |
138 | f'Remote does not support: "{kind}" archive type.') |
|
138 | f'Remote does not support: "{kind}" archive type.') | |
139 |
|
139 | |||
140 | for f in node_walker(commit_id, archive_at_path): |
|
140 | for f in node_walker(commit_id, archive_at_path): | |
141 | f_path = os.path.join(safe_bytes(archive_dir_name), safe_bytes(f.path).lstrip(b'/')) |
|
141 | f_path = os.path.join(safe_bytes(archive_dir_name), safe_bytes(f.path).lstrip(b'/')) | |
142 | try: |
|
142 | try: | |
143 | archiver.addfile(f_path, f.mode, f.is_link, f.raw_bytes()) |
|
143 | archiver.addfile(f_path, f.mode, f.is_link, f.raw_bytes()) | |
144 | except NoContentException: |
|
144 | except NoContentException: | |
145 | # NOTE(marcink): this is a special case for SVN so we can create "empty" |
|
145 | # NOTE(marcink): this is a special case for SVN so we can create "empty" | |
146 | # directories which are not supported by archiver |
|
146 | # directories which are not supported by archiver | |
147 | archiver.addfile(os.path.join(f_path, b'.dir'), f.mode, f.is_link, b'') |
|
147 | archiver.addfile(os.path.join(f_path, b'.dir'), f.mode, f.is_link, b'') | |
148 |
|
148 | |||
149 | if write_metadata: |
|
149 | if write_metadata: | |
150 | metadata = dict([ |
|
150 | metadata = dict([ | |
151 | ('commit_id', commit_id), |
|
151 | ('commit_id', commit_id), | |
152 | ('mtime', mtime), |
|
152 | ('mtime', mtime), | |
153 | ]) |
|
153 | ]) | |
154 | metadata.update(extra_metadata) |
|
154 | metadata.update(extra_metadata) | |
155 |
|
155 | |||
156 | meta = [safe_bytes(f"{f_name}:{value}") for f_name, value in metadata.items()] |
|
156 | meta = [safe_bytes(f"{f_name}:{value}") for f_name, value in metadata.items()] | |
157 | f_path = os.path.join(safe_bytes(archive_dir_name), b'.archival.txt') |
|
157 | f_path = os.path.join(safe_bytes(archive_dir_name), b'.archival.txt') | |
158 | archiver.addfile(f_path, 0o644, False, b'\n'.join(meta)) |
|
158 | archiver.addfile(f_path, 0o644, False, b'\n'.join(meta)) | |
159 |
|
159 | |||
160 | archiver.done() |
|
160 | archiver.done() | |
161 |
|
161 | |||
162 | # ensure set & get are atomic |
|
162 | # ensure set & get are atomic | |
163 | with d_cache.transact(): |
|
163 | with d_cache.transact(): | |
164 |
|
164 | |||
165 | with open(archive_tmp_path, 'rb') as archive_file: |
|
165 | with open(archive_tmp_path, 'rb') as archive_file: | |
166 | add_result = d_cache.set(archive_key, archive_file, read=True, tag='db-name', retry=True) |
|
166 | add_result = d_cache.set(archive_key, archive_file, read=True, tag='db-name', retry=True) | |
167 | if not add_result: |
|
167 | if not add_result: | |
168 | log.error('Failed to store cache for key=%s', archive_key) |
|
168 | log.error('Failed to store cache for key=%s', archive_key) | |
169 |
|
169 | |||
170 | os.remove(archive_tmp_path) |
|
170 | os.remove(archive_tmp_path) | |
171 |
|
171 | |||
172 | reader, tag = d_cache.get(archive_key, read=True, tag=True, retry=True) |
|
172 | reader, tag = d_cache.get(archive_key, read=True, tag=True, retry=True) | |
173 | if not reader: |
|
173 | if not reader: | |
174 | raise AssertionError(f'empty reader on key={archive_key} added={add_result}') |
|
174 | raise AssertionError(f'empty reader on key={archive_key} added={add_result}') | |
175 |
|
175 | |||
176 | return reader.name |
|
176 | return reader.name | |
177 |
|
177 | |||
178 |
|
178 | |||
179 |
class BinaryEnvelope |
|
179 | class BinaryEnvelope: | |
180 | def __init__(self, val): |
|
180 | def __init__(self, val): | |
181 | self.val = val |
|
181 | self.val = val | |
182 |
|
182 | |||
183 |
|
183 | |||
184 | class BytesEnvelope(bytes): |
|
184 | class BytesEnvelope(bytes): | |
185 | def __new__(cls, content): |
|
185 | def __new__(cls, content): | |
186 | if isinstance(content, bytes): |
|
186 | if isinstance(content, bytes): | |
187 | return super().__new__(cls, content) |
|
187 | return super().__new__(cls, content) | |
188 | else: |
|
188 | else: | |
189 | raise TypeError('BytesEnvelope content= param must be bytes. Use BinaryEnvelope to wrap other types') |
|
189 | raise TypeError('BytesEnvelope content= param must be bytes. Use BinaryEnvelope to wrap other types') | |
190 |
|
190 | |||
191 |
|
191 | |||
192 | class BinaryBytesEnvelope(BytesEnvelope): |
|
192 | class BinaryBytesEnvelope(BytesEnvelope): | |
193 | pass |
|
193 | pass |
@@ -1,168 +1,168 b'' | |||||
1 | # Copyright (C) 2010-2023 RhodeCode GmbH |
|
1 | # Copyright (C) 2010-2023 RhodeCode GmbH | |
2 | # |
|
2 | # | |
3 | # This program is free software: you can redistribute it and/or modify |
|
3 | # This program is free software: you can redistribute it and/or modify | |
4 | # it under the terms of the GNU Affero General Public License, version 3 |
|
4 | # it under the terms of the GNU Affero General Public License, version 3 | |
5 | # (only), as published by the Free Software Foundation. |
|
5 | # (only), as published by the Free Software Foundation. | |
6 | # |
|
6 | # | |
7 | # This program is distributed in the hope that it will be useful, |
|
7 | # This program is distributed in the hope that it will be useful, | |
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | # GNU General Public License for more details. |
|
10 | # GNU General Public License for more details. | |
11 | # |
|
11 | # | |
12 | # You should have received a copy of the GNU Affero General Public License |
|
12 | # You should have received a copy of the GNU Affero General Public License | |
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
14 | # |
|
14 | # | |
15 | # This program is dual-licensed. If you wish to learn more about the |
|
15 | # This program is dual-licensed. If you wish to learn more about the | |
16 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
16 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
17 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
17 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
18 |
|
18 | |||
19 | import os |
|
19 | import os | |
20 | import textwrap |
|
20 | import textwrap | |
21 | import string |
|
21 | import string | |
22 | import functools |
|
22 | import functools | |
23 | import logging |
|
23 | import logging | |
24 | import tempfile |
|
24 | import tempfile | |
25 | import logging.config |
|
25 | import logging.config | |
26 |
|
26 | |||
27 | from vcsserver.type_utils import str2bool, aslist |
|
27 | from vcsserver.type_utils import str2bool, aslist | |
28 |
|
28 | |||
29 | log = logging.getLogger(__name__) |
|
29 | log = logging.getLogger(__name__) | |
30 |
|
30 | |||
31 | # skip keys, that are set here, so we don't double process those |
|
31 | # skip keys, that are set here, so we don't double process those | |
32 | set_keys = { |
|
32 | set_keys = { | |
33 | '__file__': '' |
|
33 | '__file__': '' | |
34 | } |
|
34 | } | |
35 |
|
35 | |||
36 |
|
36 | |||
37 |
class SettingsMaker |
|
37 | class SettingsMaker: | |
38 |
|
38 | |||
39 | def __init__(self, app_settings): |
|
39 | def __init__(self, app_settings): | |
40 | self.settings = app_settings |
|
40 | self.settings = app_settings | |
41 |
|
41 | |||
42 | @classmethod |
|
42 | @classmethod | |
43 | def _bool_func(cls, input_val): |
|
43 | def _bool_func(cls, input_val): | |
44 | if isinstance(input_val, bytes): |
|
44 | if isinstance(input_val, bytes): | |
45 | # decode to str |
|
45 | # decode to str | |
46 | input_val = input_val.decode('utf8') |
|
46 | input_val = input_val.decode('utf8') | |
47 | return str2bool(input_val) |
|
47 | return str2bool(input_val) | |
48 |
|
48 | |||
49 | @classmethod |
|
49 | @classmethod | |
50 | def _int_func(cls, input_val): |
|
50 | def _int_func(cls, input_val): | |
51 | return int(input_val) |
|
51 | return int(input_val) | |
52 |
|
52 | |||
53 | @classmethod |
|
53 | @classmethod | |
54 | def _list_func(cls, input_val, sep=','): |
|
54 | def _list_func(cls, input_val, sep=','): | |
55 | return aslist(input_val, sep=sep) |
|
55 | return aslist(input_val, sep=sep) | |
56 |
|
56 | |||
57 | @classmethod |
|
57 | @classmethod | |
58 | def _string_func(cls, input_val, lower=True): |
|
58 | def _string_func(cls, input_val, lower=True): | |
59 | if lower: |
|
59 | if lower: | |
60 | input_val = input_val.lower() |
|
60 | input_val = input_val.lower() | |
61 | return input_val |
|
61 | return input_val | |
62 |
|
62 | |||
63 | @classmethod |
|
63 | @classmethod | |
64 | def _float_func(cls, input_val): |
|
64 | def _float_func(cls, input_val): | |
65 | return float(input_val) |
|
65 | return float(input_val) | |
66 |
|
66 | |||
67 | @classmethod |
|
67 | @classmethod | |
68 | def _dir_func(cls, input_val, ensure_dir=False, mode=0o755): |
|
68 | def _dir_func(cls, input_val, ensure_dir=False, mode=0o755): | |
69 |
|
69 | |||
70 | # ensure we have our dir created |
|
70 | # ensure we have our dir created | |
71 | if not os.path.isdir(input_val) and ensure_dir: |
|
71 | if not os.path.isdir(input_val) and ensure_dir: | |
72 | os.makedirs(input_val, mode=mode, exist_ok=True) |
|
72 | os.makedirs(input_val, mode=mode, exist_ok=True) | |
73 |
|
73 | |||
74 | if not os.path.isdir(input_val): |
|
74 | if not os.path.isdir(input_val): | |
75 | raise Exception(f'Dir at {input_val} does not exist') |
|
75 | raise Exception(f'Dir at {input_val} does not exist') | |
76 | return input_val |
|
76 | return input_val | |
77 |
|
77 | |||
78 | @classmethod |
|
78 | @classmethod | |
79 | def _file_path_func(cls, input_val, ensure_dir=False, mode=0o755): |
|
79 | def _file_path_func(cls, input_val, ensure_dir=False, mode=0o755): | |
80 | dirname = os.path.dirname(input_val) |
|
80 | dirname = os.path.dirname(input_val) | |
81 | cls._dir_func(dirname, ensure_dir=ensure_dir) |
|
81 | cls._dir_func(dirname, ensure_dir=ensure_dir) | |
82 | return input_val |
|
82 | return input_val | |
83 |
|
83 | |||
84 | @classmethod |
|
84 | @classmethod | |
85 | def _key_transformator(cls, key): |
|
85 | def _key_transformator(cls, key): | |
86 | return "{}_{}".format('RC'.upper(), key.upper().replace('.', '_').replace('-', '_')) |
|
86 | return "{}_{}".format('RC'.upper(), key.upper().replace('.', '_').replace('-', '_')) | |
87 |
|
87 | |||
88 | def maybe_env_key(self, key): |
|
88 | def maybe_env_key(self, key): | |
89 | # now maybe we have this KEY in env, search and use the value with higher priority. |
|
89 | # now maybe we have this KEY in env, search and use the value with higher priority. | |
90 | transformed_key = self._key_transformator(key) |
|
90 | transformed_key = self._key_transformator(key) | |
91 | envvar_value = os.environ.get(transformed_key) |
|
91 | envvar_value = os.environ.get(transformed_key) | |
92 | if envvar_value: |
|
92 | if envvar_value: | |
93 | log.debug('using `%s` key instead of `%s` key for config', transformed_key, key) |
|
93 | log.debug('using `%s` key instead of `%s` key for config', transformed_key, key) | |
94 |
|
94 | |||
95 | return envvar_value |
|
95 | return envvar_value | |
96 |
|
96 | |||
97 | def env_expand(self): |
|
97 | def env_expand(self): | |
98 | replaced = {} |
|
98 | replaced = {} | |
99 | for k, v in self.settings.items(): |
|
99 | for k, v in self.settings.items(): | |
100 | if k not in set_keys: |
|
100 | if k not in set_keys: | |
101 | envvar_value = self.maybe_env_key(k) |
|
101 | envvar_value = self.maybe_env_key(k) | |
102 | if envvar_value: |
|
102 | if envvar_value: | |
103 | replaced[k] = envvar_value |
|
103 | replaced[k] = envvar_value | |
104 | set_keys[k] = envvar_value |
|
104 | set_keys[k] = envvar_value | |
105 |
|
105 | |||
106 | # replace ALL keys updated |
|
106 | # replace ALL keys updated | |
107 | self.settings.update(replaced) |
|
107 | self.settings.update(replaced) | |
108 |
|
108 | |||
109 | def enable_logging(self, logging_conf=None, level='INFO', formatter='generic'): |
|
109 | def enable_logging(self, logging_conf=None, level='INFO', formatter='generic'): | |
110 | """ |
|
110 | """ | |
111 | Helper to enable debug on running instance |
|
111 | Helper to enable debug on running instance | |
112 | :return: |
|
112 | :return: | |
113 | """ |
|
113 | """ | |
114 |
|
114 | |||
115 | if not str2bool(self.settings.get('logging.autoconfigure')): |
|
115 | if not str2bool(self.settings.get('logging.autoconfigure')): | |
116 | log.info('logging configuration based on main .ini file') |
|
116 | log.info('logging configuration based on main .ini file') | |
117 | return |
|
117 | return | |
118 |
|
118 | |||
119 | if logging_conf is None: |
|
119 | if logging_conf is None: | |
120 | logging_conf = self.settings.get('logging.logging_conf_file') or '' |
|
120 | logging_conf = self.settings.get('logging.logging_conf_file') or '' | |
121 |
|
121 | |||
122 | if not os.path.isfile(logging_conf): |
|
122 | if not os.path.isfile(logging_conf): | |
123 | log.error('Unable to setup logging based on %s, ' |
|
123 | log.error('Unable to setup logging based on %s, ' | |
124 | 'file does not exist.... specify path using logging.logging_conf_file= config setting. ', logging_conf) |
|
124 | 'file does not exist.... specify path using logging.logging_conf_file= config setting. ', logging_conf) | |
125 | return |
|
125 | return | |
126 |
|
126 | |||
127 | with open(logging_conf, 'rt') as f: |
|
127 | with open(logging_conf, 'rt') as f: | |
128 | ini_template = textwrap.dedent(f.read()) |
|
128 | ini_template = textwrap.dedent(f.read()) | |
129 | ini_template = string.Template(ini_template).safe_substitute( |
|
129 | ini_template = string.Template(ini_template).safe_substitute( | |
130 | RC_LOGGING_LEVEL=os.environ.get('RC_LOGGING_LEVEL', '') or level, |
|
130 | RC_LOGGING_LEVEL=os.environ.get('RC_LOGGING_LEVEL', '') or level, | |
131 | RC_LOGGING_FORMATTER=os.environ.get('RC_LOGGING_FORMATTER', '') or formatter |
|
131 | RC_LOGGING_FORMATTER=os.environ.get('RC_LOGGING_FORMATTER', '') or formatter | |
132 | ) |
|
132 | ) | |
133 |
|
133 | |||
134 | with tempfile.NamedTemporaryFile(prefix='rc_logging_', suffix='.ini', delete=False) as f: |
|
134 | with tempfile.NamedTemporaryFile(prefix='rc_logging_', suffix='.ini', delete=False) as f: | |
135 | log.info('Saved Temporary LOGGING config at %s', f.name) |
|
135 | log.info('Saved Temporary LOGGING config at %s', f.name) | |
136 | f.write(ini_template) |
|
136 | f.write(ini_template) | |
137 |
|
137 | |||
138 | logging.config.fileConfig(f.name) |
|
138 | logging.config.fileConfig(f.name) | |
139 | os.remove(f.name) |
|
139 | os.remove(f.name) | |
140 |
|
140 | |||
141 | def make_setting(self, key, default, lower=False, default_when_empty=False, parser=None): |
|
141 | def make_setting(self, key, default, lower=False, default_when_empty=False, parser=None): | |
142 | input_val = self.settings.get(key, default) |
|
142 | input_val = self.settings.get(key, default) | |
143 |
|
143 | |||
144 | if default_when_empty and not input_val: |
|
144 | if default_when_empty and not input_val: | |
145 | # use default value when value is set in the config but it is empty |
|
145 | # use default value when value is set in the config but it is empty | |
146 | input_val = default |
|
146 | input_val = default | |
147 |
|
147 | |||
148 | parser_func = { |
|
148 | parser_func = { | |
149 | 'bool': self._bool_func, |
|
149 | 'bool': self._bool_func, | |
150 | 'int': self._int_func, |
|
150 | 'int': self._int_func, | |
151 | 'list': self._list_func, |
|
151 | 'list': self._list_func, | |
152 | 'list:newline': functools.partial(self._list_func, sep='/n'), |
|
152 | 'list:newline': functools.partial(self._list_func, sep='/n'), | |
153 | 'list:spacesep': functools.partial(self._list_func, sep=' '), |
|
153 | 'list:spacesep': functools.partial(self._list_func, sep=' '), | |
154 | 'string': functools.partial(self._string_func, lower=lower), |
|
154 | 'string': functools.partial(self._string_func, lower=lower), | |
155 | 'dir': self._dir_func, |
|
155 | 'dir': self._dir_func, | |
156 | 'dir:ensured': functools.partial(self._dir_func, ensure_dir=True), |
|
156 | 'dir:ensured': functools.partial(self._dir_func, ensure_dir=True), | |
157 | 'file': self._file_path_func, |
|
157 | 'file': self._file_path_func, | |
158 | 'file:ensured': functools.partial(self._file_path_func, ensure_dir=True), |
|
158 | 'file:ensured': functools.partial(self._file_path_func, ensure_dir=True), | |
159 | None: lambda i: i |
|
159 | None: lambda i: i | |
160 | }[parser] |
|
160 | }[parser] | |
161 |
|
161 | |||
162 | envvar_value = self.maybe_env_key(key) |
|
162 | envvar_value = self.maybe_env_key(key) | |
163 | if envvar_value: |
|
163 | if envvar_value: | |
164 | input_val = envvar_value |
|
164 | input_val = envvar_value | |
165 | set_keys[key] = input_val |
|
165 | set_keys[key] = input_val | |
166 |
|
166 | |||
167 | self.settings[key] = parser_func(input_val) |
|
167 | self.settings[key] = parser_func(input_val) | |
168 | return self.settings[key] |
|
168 | return self.settings[key] |
@@ -1,56 +1,56 b'' | |||||
1 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
1 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
2 |
|
2 | |||
3 | """ |
|
3 | """ | |
4 | Implementation of :class:`EchoApp`. |
|
4 | Implementation of :class:`EchoApp`. | |
5 |
|
5 | |||
6 | This WSGI application will just echo back the data which it recieves. |
|
6 | This WSGI application will just echo back the data which it recieves. | |
7 | """ |
|
7 | """ | |
8 |
|
8 | |||
9 | import logging |
|
9 | import logging | |
10 |
|
10 | |||
11 |
|
11 | |||
12 | log = logging.getLogger(__name__) |
|
12 | log = logging.getLogger(__name__) | |
13 |
|
13 | |||
14 |
|
14 | |||
15 |
class EchoApp |
|
15 | class EchoApp: | |
16 |
|
16 | |||
17 | def __init__(self, repo_path, repo_name, config): |
|
17 | def __init__(self, repo_path, repo_name, config): | |
18 | self._repo_path = repo_path |
|
18 | self._repo_path = repo_path | |
19 | log.info("EchoApp initialized for %s", repo_path) |
|
19 | log.info("EchoApp initialized for %s", repo_path) | |
20 |
|
20 | |||
21 | def __call__(self, environ, start_response): |
|
21 | def __call__(self, environ, start_response): | |
22 | log.debug("EchoApp called for %s", self._repo_path) |
|
22 | log.debug("EchoApp called for %s", self._repo_path) | |
23 | log.debug("Content-Length: %s", environ.get('CONTENT_LENGTH')) |
|
23 | log.debug("Content-Length: %s", environ.get('CONTENT_LENGTH')) | |
24 | environ['wsgi.input'].read() |
|
24 | environ['wsgi.input'].read() | |
25 | status = '200 OK' |
|
25 | status = '200 OK' | |
26 | headers = [('Content-Type', 'text/plain')] |
|
26 | headers = [('Content-Type', 'text/plain')] | |
27 | start_response(status, headers) |
|
27 | start_response(status, headers) | |
28 | return [b"ECHO"] |
|
28 | return [b"ECHO"] | |
29 |
|
29 | |||
30 |
|
30 | |||
31 |
class EchoAppStream |
|
31 | class EchoAppStream: | |
32 |
|
32 | |||
33 | def __init__(self, repo_path, repo_name, config): |
|
33 | def __init__(self, repo_path, repo_name, config): | |
34 | self._repo_path = repo_path |
|
34 | self._repo_path = repo_path | |
35 | log.info("EchoApp initialized for %s", repo_path) |
|
35 | log.info("EchoApp initialized for %s", repo_path) | |
36 |
|
36 | |||
37 | def __call__(self, environ, start_response): |
|
37 | def __call__(self, environ, start_response): | |
38 | log.debug("EchoApp called for %s", self._repo_path) |
|
38 | log.debug("EchoApp called for %s", self._repo_path) | |
39 | log.debug("Content-Length: %s", environ.get('CONTENT_LENGTH')) |
|
39 | log.debug("Content-Length: %s", environ.get('CONTENT_LENGTH')) | |
40 | environ['wsgi.input'].read() |
|
40 | environ['wsgi.input'].read() | |
41 | status = '200 OK' |
|
41 | status = '200 OK' | |
42 | headers = [('Content-Type', 'text/plain')] |
|
42 | headers = [('Content-Type', 'text/plain')] | |
43 | start_response(status, headers) |
|
43 | start_response(status, headers) | |
44 |
|
44 | |||
45 | def generator(): |
|
45 | def generator(): | |
46 | for _ in range(1000000): |
|
46 | for _ in range(1000000): | |
47 | yield b"ECHO_STREAM" |
|
47 | yield b"ECHO_STREAM" | |
48 | return generator() |
|
48 | return generator() | |
49 |
|
49 | |||
50 |
|
50 | |||
51 | def create_app(): |
|
51 | def create_app(): | |
52 | """ |
|
52 | """ | |
53 | Allows to run this app directly in a WSGI server. |
|
53 | Allows to run this app directly in a WSGI server. | |
54 | """ |
|
54 | """ | |
55 | stub_config = {} |
|
55 | stub_config = {} | |
56 | return EchoApp('stub_path', 'stub_name', stub_config) |
|
56 | return EchoApp('stub_path', 'stub_name', stub_config) |
@@ -1,47 +1,47 b'' | |||||
1 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
1 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
2 |
|
2 | |||
3 | """ |
|
3 | """ | |
4 | Provides the same API as :mod:`remote_wsgi`. |
|
4 | Provides the same API as :mod:`remote_wsgi`. | |
5 |
|
5 | |||
6 | Uses the `EchoApp` instead of real implementations. |
|
6 | Uses the `EchoApp` instead of real implementations. | |
7 | """ |
|
7 | """ | |
8 |
|
8 | |||
9 | import logging |
|
9 | import logging | |
10 |
|
10 | |||
11 | from .echo_app import EchoApp |
|
11 | from .echo_app import EchoApp | |
12 | from vcsserver import wsgi_app_caller |
|
12 | from vcsserver import wsgi_app_caller | |
13 |
|
13 | |||
14 |
|
14 | |||
15 | log = logging.getLogger(__name__) |
|
15 | log = logging.getLogger(__name__) | |
16 |
|
16 | |||
17 |
|
17 | |||
18 |
class GitRemoteWsgi |
|
18 | class GitRemoteWsgi: | |
19 | def handle(self, environ, input_data, *args, **kwargs): |
|
19 | def handle(self, environ, input_data, *args, **kwargs): | |
20 | app = wsgi_app_caller.WSGIAppCaller( |
|
20 | app = wsgi_app_caller.WSGIAppCaller( | |
21 | create_echo_wsgi_app(*args, **kwargs)) |
|
21 | create_echo_wsgi_app(*args, **kwargs)) | |
22 |
|
22 | |||
23 | return app.handle(environ, input_data) |
|
23 | return app.handle(environ, input_data) | |
24 |
|
24 | |||
25 |
|
25 | |||
26 |
class HgRemoteWsgi |
|
26 | class HgRemoteWsgi: | |
27 | def handle(self, environ, input_data, *args, **kwargs): |
|
27 | def handle(self, environ, input_data, *args, **kwargs): | |
28 | app = wsgi_app_caller.WSGIAppCaller( |
|
28 | app = wsgi_app_caller.WSGIAppCaller( | |
29 | create_echo_wsgi_app(*args, **kwargs)) |
|
29 | create_echo_wsgi_app(*args, **kwargs)) | |
30 |
|
30 | |||
31 | return app.handle(environ, input_data) |
|
31 | return app.handle(environ, input_data) | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | def create_echo_wsgi_app(repo_path, repo_name, config): |
|
34 | def create_echo_wsgi_app(repo_path, repo_name, config): | |
35 | log.debug("Creating EchoApp WSGI application") |
|
35 | log.debug("Creating EchoApp WSGI application") | |
36 |
|
36 | |||
37 | _assert_valid_config(config) |
|
37 | _assert_valid_config(config) | |
38 |
|
38 | |||
39 | # Remaining items are forwarded to have the extras available |
|
39 | # Remaining items are forwarded to have the extras available | |
40 | return EchoApp(repo_path, repo_name, config=config) |
|
40 | return EchoApp(repo_path, repo_name, config=config) | |
41 |
|
41 | |||
42 |
|
42 | |||
43 | def _assert_valid_config(config): |
|
43 | def _assert_valid_config(config): | |
44 | config = config.copy() |
|
44 | config = config.copy() | |
45 |
|
45 | |||
46 | # This is what git needs from config at this stage |
|
46 | # This is what git needs from config at this stage | |
47 | config.pop(b'git_update_server_info') |
|
47 | config.pop(b'git_update_server_info') |
@@ -1,291 +1,291 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import re |
|
18 | import re | |
19 | import logging |
|
19 | import logging | |
20 |
|
20 | |||
21 | from pyramid.config import Configurator |
|
21 | from pyramid.config import Configurator | |
22 | from pyramid.response import Response, FileIter |
|
22 | from pyramid.response import Response, FileIter | |
23 | from pyramid.httpexceptions import ( |
|
23 | from pyramid.httpexceptions import ( | |
24 | HTTPBadRequest, HTTPNotImplemented, HTTPNotFound, HTTPForbidden, |
|
24 | HTTPBadRequest, HTTPNotImplemented, HTTPNotFound, HTTPForbidden, | |
25 | HTTPUnprocessableEntity) |
|
25 | HTTPUnprocessableEntity) | |
26 |
|
26 | |||
27 | from vcsserver.lib.rc_json import json |
|
27 | from vcsserver.lib.rc_json import json | |
28 | from vcsserver.git_lfs.lib import OidHandler, LFSOidStore |
|
28 | from vcsserver.git_lfs.lib import OidHandler, LFSOidStore | |
29 | from vcsserver.git_lfs.utils import safe_result, get_cython_compat_decorator |
|
29 | from vcsserver.git_lfs.utils import safe_result, get_cython_compat_decorator | |
30 | from vcsserver.str_utils import safe_int |
|
30 | from vcsserver.str_utils import safe_int | |
31 |
|
31 | |||
32 | log = logging.getLogger(__name__) |
|
32 | log = logging.getLogger(__name__) | |
33 |
|
33 | |||
34 |
|
34 | |||
35 | GIT_LFS_CONTENT_TYPE = 'application/vnd.git-lfs' # +json ? |
|
35 | GIT_LFS_CONTENT_TYPE = 'application/vnd.git-lfs' # +json ? | |
36 | GIT_LFS_PROTO_PAT = re.compile(r'^/(.+)/(info/lfs/(.+))') |
|
36 | GIT_LFS_PROTO_PAT = re.compile(r'^/(.+)/(info/lfs/(.+))') | |
37 |
|
37 | |||
38 |
|
38 | |||
39 | def write_response_error(http_exception, text=None): |
|
39 | def write_response_error(http_exception, text=None): | |
40 | content_type = GIT_LFS_CONTENT_TYPE + '+json' |
|
40 | content_type = GIT_LFS_CONTENT_TYPE + '+json' | |
41 | _exception = http_exception(content_type=content_type) |
|
41 | _exception = http_exception(content_type=content_type) | |
42 | _exception.content_type = content_type |
|
42 | _exception.content_type = content_type | |
43 | if text: |
|
43 | if text: | |
44 | _exception.body = json.dumps({'message': text}) |
|
44 | _exception.body = json.dumps({'message': text}) | |
45 | log.debug('LFS: writing response of type %s to client with text:%s', |
|
45 | log.debug('LFS: writing response of type %s to client with text:%s', | |
46 | http_exception, text) |
|
46 | http_exception, text) | |
47 | return _exception |
|
47 | return _exception | |
48 |
|
48 | |||
49 |
|
49 | |||
50 |
class AuthHeaderRequired |
|
50 | class AuthHeaderRequired: | |
51 | """ |
|
51 | """ | |
52 | Decorator to check if request has proper auth-header |
|
52 | Decorator to check if request has proper auth-header | |
53 | """ |
|
53 | """ | |
54 |
|
54 | |||
55 | def __call__(self, func): |
|
55 | def __call__(self, func): | |
56 | return get_cython_compat_decorator(self.__wrapper, func) |
|
56 | return get_cython_compat_decorator(self.__wrapper, func) | |
57 |
|
57 | |||
58 | def __wrapper(self, func, *fargs, **fkwargs): |
|
58 | def __wrapper(self, func, *fargs, **fkwargs): | |
59 | request = fargs[1] |
|
59 | request = fargs[1] | |
60 | auth = request.authorization |
|
60 | auth = request.authorization | |
61 | if not auth: |
|
61 | if not auth: | |
62 | return write_response_error(HTTPForbidden) |
|
62 | return write_response_error(HTTPForbidden) | |
63 | return func(*fargs[1:], **fkwargs) |
|
63 | return func(*fargs[1:], **fkwargs) | |
64 |
|
64 | |||
65 |
|
65 | |||
66 | # views |
|
66 | # views | |
67 |
|
67 | |||
68 | def lfs_objects(request): |
|
68 | def lfs_objects(request): | |
69 | # indicate not supported, V1 API |
|
69 | # indicate not supported, V1 API | |
70 | log.warning('LFS: v1 api not supported, reporting it back to client') |
|
70 | log.warning('LFS: v1 api not supported, reporting it back to client') | |
71 | return write_response_error(HTTPNotImplemented, 'LFS: v1 api not supported') |
|
71 | return write_response_error(HTTPNotImplemented, 'LFS: v1 api not supported') | |
72 |
|
72 | |||
73 |
|
73 | |||
74 | @AuthHeaderRequired() |
|
74 | @AuthHeaderRequired() | |
75 | def lfs_objects_batch(request): |
|
75 | def lfs_objects_batch(request): | |
76 | """ |
|
76 | """ | |
77 | The client sends the following information to the Batch endpoint to transfer some objects: |
|
77 | The client sends the following information to the Batch endpoint to transfer some objects: | |
78 |
|
78 | |||
79 | operation - Should be download or upload. |
|
79 | operation - Should be download or upload. | |
80 | transfers - An optional Array of String identifiers for transfer |
|
80 | transfers - An optional Array of String identifiers for transfer | |
81 | adapters that the client has configured. If omitted, the basic |
|
81 | adapters that the client has configured. If omitted, the basic | |
82 | transfer adapter MUST be assumed by the server. |
|
82 | transfer adapter MUST be assumed by the server. | |
83 | objects - An Array of objects to download. |
|
83 | objects - An Array of objects to download. | |
84 | oid - String OID of the LFS object. |
|
84 | oid - String OID of the LFS object. | |
85 | size - Integer byte size of the LFS object. Must be at least zero. |
|
85 | size - Integer byte size of the LFS object. Must be at least zero. | |
86 | """ |
|
86 | """ | |
87 | request.response.content_type = GIT_LFS_CONTENT_TYPE + '+json' |
|
87 | request.response.content_type = GIT_LFS_CONTENT_TYPE + '+json' | |
88 | auth = request.authorization |
|
88 | auth = request.authorization | |
89 | repo = request.matchdict.get('repo') |
|
89 | repo = request.matchdict.get('repo') | |
90 | data = request.json |
|
90 | data = request.json | |
91 | operation = data.get('operation') |
|
91 | operation = data.get('operation') | |
92 | http_scheme = request.registry.git_lfs_http_scheme |
|
92 | http_scheme = request.registry.git_lfs_http_scheme | |
93 |
|
93 | |||
94 | if operation not in ('download', 'upload'): |
|
94 | if operation not in ('download', 'upload'): | |
95 | log.debug('LFS: unsupported operation:%s', operation) |
|
95 | log.debug('LFS: unsupported operation:%s', operation) | |
96 | return write_response_error( |
|
96 | return write_response_error( | |
97 |
HTTPBadRequest, 'unsupported operation mode: ` |
|
97 | HTTPBadRequest, f'unsupported operation mode: `{operation}`') | |
98 |
|
98 | |||
99 | if 'objects' not in data: |
|
99 | if 'objects' not in data: | |
100 | log.debug('LFS: missing objects data') |
|
100 | log.debug('LFS: missing objects data') | |
101 | return write_response_error( |
|
101 | return write_response_error( | |
102 | HTTPBadRequest, 'missing objects data') |
|
102 | HTTPBadRequest, 'missing objects data') | |
103 |
|
103 | |||
104 | log.debug('LFS: handling operation of type: %s', operation) |
|
104 | log.debug('LFS: handling operation of type: %s', operation) | |
105 |
|
105 | |||
106 | objects = [] |
|
106 | objects = [] | |
107 | for o in data['objects']: |
|
107 | for o in data['objects']: | |
108 | try: |
|
108 | try: | |
109 | oid = o['oid'] |
|
109 | oid = o['oid'] | |
110 | obj_size = o['size'] |
|
110 | obj_size = o['size'] | |
111 | except KeyError: |
|
111 | except KeyError: | |
112 | log.exception('LFS, failed to extract data') |
|
112 | log.exception('LFS, failed to extract data') | |
113 | return write_response_error( |
|
113 | return write_response_error( | |
114 | HTTPBadRequest, 'unsupported data in objects') |
|
114 | HTTPBadRequest, 'unsupported data in objects') | |
115 |
|
115 | |||
116 | obj_data = {'oid': oid} |
|
116 | obj_data = {'oid': oid} | |
117 |
|
117 | |||
118 | obj_href = request.route_url('lfs_objects_oid', repo=repo, oid=oid, |
|
118 | obj_href = request.route_url('lfs_objects_oid', repo=repo, oid=oid, | |
119 | _scheme=http_scheme) |
|
119 | _scheme=http_scheme) | |
120 | obj_verify_href = request.route_url('lfs_objects_verify', repo=repo, |
|
120 | obj_verify_href = request.route_url('lfs_objects_verify', repo=repo, | |
121 | _scheme=http_scheme) |
|
121 | _scheme=http_scheme) | |
122 | store = LFSOidStore( |
|
122 | store = LFSOidStore( | |
123 | oid, repo, store_location=request.registry.git_lfs_store_path) |
|
123 | oid, repo, store_location=request.registry.git_lfs_store_path) | |
124 | handler = OidHandler( |
|
124 | handler = OidHandler( | |
125 | store, repo, auth, oid, obj_size, obj_data, |
|
125 | store, repo, auth, oid, obj_size, obj_data, | |
126 | obj_href, obj_verify_href) |
|
126 | obj_href, obj_verify_href) | |
127 |
|
127 | |||
128 | # this verifies also OIDs |
|
128 | # this verifies also OIDs | |
129 | actions, errors = handler.exec_operation(operation) |
|
129 | actions, errors = handler.exec_operation(operation) | |
130 | if errors: |
|
130 | if errors: | |
131 | log.warning('LFS: got following errors: %s', errors) |
|
131 | log.warning('LFS: got following errors: %s', errors) | |
132 | obj_data['errors'] = errors |
|
132 | obj_data['errors'] = errors | |
133 |
|
133 | |||
134 | if actions: |
|
134 | if actions: | |
135 | obj_data['actions'] = actions |
|
135 | obj_data['actions'] = actions | |
136 |
|
136 | |||
137 | obj_data['size'] = obj_size |
|
137 | obj_data['size'] = obj_size | |
138 | obj_data['authenticated'] = True |
|
138 | obj_data['authenticated'] = True | |
139 | objects.append(obj_data) |
|
139 | objects.append(obj_data) | |
140 |
|
140 | |||
141 | result = {'objects': objects, 'transfer': 'basic'} |
|
141 | result = {'objects': objects, 'transfer': 'basic'} | |
142 | log.debug('LFS Response %s', safe_result(result)) |
|
142 | log.debug('LFS Response %s', safe_result(result)) | |
143 |
|
143 | |||
144 | return result |
|
144 | return result | |
145 |
|
145 | |||
146 |
|
146 | |||
147 | def lfs_objects_oid_upload(request): |
|
147 | def lfs_objects_oid_upload(request): | |
148 | request.response.content_type = GIT_LFS_CONTENT_TYPE + '+json' |
|
148 | request.response.content_type = GIT_LFS_CONTENT_TYPE + '+json' | |
149 | repo = request.matchdict.get('repo') |
|
149 | repo = request.matchdict.get('repo') | |
150 | oid = request.matchdict.get('oid') |
|
150 | oid = request.matchdict.get('oid') | |
151 | store = LFSOidStore( |
|
151 | store = LFSOidStore( | |
152 | oid, repo, store_location=request.registry.git_lfs_store_path) |
|
152 | oid, repo, store_location=request.registry.git_lfs_store_path) | |
153 | engine = store.get_engine(mode='wb') |
|
153 | engine = store.get_engine(mode='wb') | |
154 | log.debug('LFS: starting chunked write of LFS oid: %s to storage', oid) |
|
154 | log.debug('LFS: starting chunked write of LFS oid: %s to storage', oid) | |
155 |
|
155 | |||
156 | body = request.environ['wsgi.input'] |
|
156 | body = request.environ['wsgi.input'] | |
157 |
|
157 | |||
158 | with engine as f: |
|
158 | with engine as f: | |
159 | blksize = 64 * 1024 # 64kb |
|
159 | blksize = 64 * 1024 # 64kb | |
160 | while True: |
|
160 | while True: | |
161 | # read in chunks as stream comes in from Gunicorn |
|
161 | # read in chunks as stream comes in from Gunicorn | |
162 | # this is a specific Gunicorn support function. |
|
162 | # this is a specific Gunicorn support function. | |
163 | # might work differently on waitress |
|
163 | # might work differently on waitress | |
164 | chunk = body.read(blksize) |
|
164 | chunk = body.read(blksize) | |
165 | if not chunk: |
|
165 | if not chunk: | |
166 | break |
|
166 | break | |
167 | f.write(chunk) |
|
167 | f.write(chunk) | |
168 |
|
168 | |||
169 | return {'upload': 'ok'} |
|
169 | return {'upload': 'ok'} | |
170 |
|
170 | |||
171 |
|
171 | |||
172 | def lfs_objects_oid_download(request): |
|
172 | def lfs_objects_oid_download(request): | |
173 | repo = request.matchdict.get('repo') |
|
173 | repo = request.matchdict.get('repo') | |
174 | oid = request.matchdict.get('oid') |
|
174 | oid = request.matchdict.get('oid') | |
175 |
|
175 | |||
176 | store = LFSOidStore( |
|
176 | store = LFSOidStore( | |
177 | oid, repo, store_location=request.registry.git_lfs_store_path) |
|
177 | oid, repo, store_location=request.registry.git_lfs_store_path) | |
178 | if not store.has_oid(): |
|
178 | if not store.has_oid(): | |
179 | log.debug('LFS: oid %s does not exists in store', oid) |
|
179 | log.debug('LFS: oid %s does not exists in store', oid) | |
180 | return write_response_error( |
|
180 | return write_response_error( | |
181 |
HTTPNotFound, 'requested file with oid ` |
|
181 | HTTPNotFound, f'requested file with oid `{oid}` not found in store') | |
182 |
|
182 | |||
183 | # TODO(marcink): support range header ? |
|
183 | # TODO(marcink): support range header ? | |
184 | # Range: bytes=0-, `bytes=(\d+)\-.*` |
|
184 | # Range: bytes=0-, `bytes=(\d+)\-.*` | |
185 |
|
185 | |||
186 | f = open(store.oid_path, 'rb') |
|
186 | f = open(store.oid_path, 'rb') | |
187 | response = Response( |
|
187 | response = Response( | |
188 | content_type='application/octet-stream', app_iter=FileIter(f)) |
|
188 | content_type='application/octet-stream', app_iter=FileIter(f)) | |
189 | response.headers.add('X-RC-LFS-Response-Oid', str(oid)) |
|
189 | response.headers.add('X-RC-LFS-Response-Oid', str(oid)) | |
190 | return response |
|
190 | return response | |
191 |
|
191 | |||
192 |
|
192 | |||
193 | def lfs_objects_verify(request): |
|
193 | def lfs_objects_verify(request): | |
194 | request.response.content_type = GIT_LFS_CONTENT_TYPE + '+json' |
|
194 | request.response.content_type = GIT_LFS_CONTENT_TYPE + '+json' | |
195 | repo = request.matchdict.get('repo') |
|
195 | repo = request.matchdict.get('repo') | |
196 |
|
196 | |||
197 | data = request.json |
|
197 | data = request.json | |
198 | oid = data.get('oid') |
|
198 | oid = data.get('oid') | |
199 | size = safe_int(data.get('size')) |
|
199 | size = safe_int(data.get('size')) | |
200 |
|
200 | |||
201 | if not (oid and size): |
|
201 | if not (oid and size): | |
202 | return write_response_error( |
|
202 | return write_response_error( | |
203 | HTTPBadRequest, 'missing oid and size in request data') |
|
203 | HTTPBadRequest, 'missing oid and size in request data') | |
204 |
|
204 | |||
205 | store = LFSOidStore( |
|
205 | store = LFSOidStore( | |
206 | oid, repo, store_location=request.registry.git_lfs_store_path) |
|
206 | oid, repo, store_location=request.registry.git_lfs_store_path) | |
207 | if not store.has_oid(): |
|
207 | if not store.has_oid(): | |
208 | log.debug('LFS: oid %s does not exists in store', oid) |
|
208 | log.debug('LFS: oid %s does not exists in store', oid) | |
209 | return write_response_error( |
|
209 | return write_response_error( | |
210 |
HTTPNotFound, 'oid ` |
|
210 | HTTPNotFound, f'oid `{oid}` does not exists in store') | |
211 |
|
211 | |||
212 | store_size = store.size_oid() |
|
212 | store_size = store.size_oid() | |
213 | if store_size != size: |
|
213 | if store_size != size: | |
214 | msg = 'requested file size mismatch store size:{} requested:{}'.format( |
|
214 | msg = 'requested file size mismatch store size:{} requested:{}'.format( | |
215 | store_size, size) |
|
215 | store_size, size) | |
216 | return write_response_error( |
|
216 | return write_response_error( | |
217 | HTTPUnprocessableEntity, msg) |
|
217 | HTTPUnprocessableEntity, msg) | |
218 |
|
218 | |||
219 | return {'message': {'size': 'ok', 'in_store': 'ok'}} |
|
219 | return {'message': {'size': 'ok', 'in_store': 'ok'}} | |
220 |
|
220 | |||
221 |
|
221 | |||
222 | def lfs_objects_lock(request): |
|
222 | def lfs_objects_lock(request): | |
223 | return write_response_error( |
|
223 | return write_response_error( | |
224 | HTTPNotImplemented, 'GIT LFS locking api not supported') |
|
224 | HTTPNotImplemented, 'GIT LFS locking api not supported') | |
225 |
|
225 | |||
226 |
|
226 | |||
227 | def not_found(request): |
|
227 | def not_found(request): | |
228 | return write_response_error( |
|
228 | return write_response_error( | |
229 | HTTPNotFound, 'request path not found') |
|
229 | HTTPNotFound, 'request path not found') | |
230 |
|
230 | |||
231 |
|
231 | |||
232 | def lfs_disabled(request): |
|
232 | def lfs_disabled(request): | |
233 | return write_response_error( |
|
233 | return write_response_error( | |
234 | HTTPNotImplemented, 'GIT LFS disabled for this repo') |
|
234 | HTTPNotImplemented, 'GIT LFS disabled for this repo') | |
235 |
|
235 | |||
236 |
|
236 | |||
237 | def git_lfs_app(config): |
|
237 | def git_lfs_app(config): | |
238 |
|
238 | |||
239 | # v1 API deprecation endpoint |
|
239 | # v1 API deprecation endpoint | |
240 | config.add_route('lfs_objects', |
|
240 | config.add_route('lfs_objects', | |
241 | '/{repo:.*?[^/]}/info/lfs/objects') |
|
241 | '/{repo:.*?[^/]}/info/lfs/objects') | |
242 | config.add_view(lfs_objects, route_name='lfs_objects', |
|
242 | config.add_view(lfs_objects, route_name='lfs_objects', | |
243 | request_method='POST', renderer='json') |
|
243 | request_method='POST', renderer='json') | |
244 |
|
244 | |||
245 | # locking API |
|
245 | # locking API | |
246 | config.add_route('lfs_objects_lock', |
|
246 | config.add_route('lfs_objects_lock', | |
247 | '/{repo:.*?[^/]}/info/lfs/locks') |
|
247 | '/{repo:.*?[^/]}/info/lfs/locks') | |
248 | config.add_view(lfs_objects_lock, route_name='lfs_objects_lock', |
|
248 | config.add_view(lfs_objects_lock, route_name='lfs_objects_lock', | |
249 | request_method=('POST', 'GET'), renderer='json') |
|
249 | request_method=('POST', 'GET'), renderer='json') | |
250 |
|
250 | |||
251 | config.add_route('lfs_objects_lock_verify', |
|
251 | config.add_route('lfs_objects_lock_verify', | |
252 | '/{repo:.*?[^/]}/info/lfs/locks/verify') |
|
252 | '/{repo:.*?[^/]}/info/lfs/locks/verify') | |
253 | config.add_view(lfs_objects_lock, route_name='lfs_objects_lock_verify', |
|
253 | config.add_view(lfs_objects_lock, route_name='lfs_objects_lock_verify', | |
254 | request_method=('POST', 'GET'), renderer='json') |
|
254 | request_method=('POST', 'GET'), renderer='json') | |
255 |
|
255 | |||
256 | # batch API |
|
256 | # batch API | |
257 | config.add_route('lfs_objects_batch', |
|
257 | config.add_route('lfs_objects_batch', | |
258 | '/{repo:.*?[^/]}/info/lfs/objects/batch') |
|
258 | '/{repo:.*?[^/]}/info/lfs/objects/batch') | |
259 | config.add_view(lfs_objects_batch, route_name='lfs_objects_batch', |
|
259 | config.add_view(lfs_objects_batch, route_name='lfs_objects_batch', | |
260 | request_method='POST', renderer='json') |
|
260 | request_method='POST', renderer='json') | |
261 |
|
261 | |||
262 | # oid upload/download API |
|
262 | # oid upload/download API | |
263 | config.add_route('lfs_objects_oid', |
|
263 | config.add_route('lfs_objects_oid', | |
264 | '/{repo:.*?[^/]}/info/lfs/objects/{oid}') |
|
264 | '/{repo:.*?[^/]}/info/lfs/objects/{oid}') | |
265 | config.add_view(lfs_objects_oid_upload, route_name='lfs_objects_oid', |
|
265 | config.add_view(lfs_objects_oid_upload, route_name='lfs_objects_oid', | |
266 | request_method='PUT', renderer='json') |
|
266 | request_method='PUT', renderer='json') | |
267 | config.add_view(lfs_objects_oid_download, route_name='lfs_objects_oid', |
|
267 | config.add_view(lfs_objects_oid_download, route_name='lfs_objects_oid', | |
268 | request_method='GET', renderer='json') |
|
268 | request_method='GET', renderer='json') | |
269 |
|
269 | |||
270 | # verification API |
|
270 | # verification API | |
271 | config.add_route('lfs_objects_verify', |
|
271 | config.add_route('lfs_objects_verify', | |
272 | '/{repo:.*?[^/]}/info/lfs/verify') |
|
272 | '/{repo:.*?[^/]}/info/lfs/verify') | |
273 | config.add_view(lfs_objects_verify, route_name='lfs_objects_verify', |
|
273 | config.add_view(lfs_objects_verify, route_name='lfs_objects_verify', | |
274 | request_method='POST', renderer='json') |
|
274 | request_method='POST', renderer='json') | |
275 |
|
275 | |||
276 | # not found handler for API |
|
276 | # not found handler for API | |
277 | config.add_notfound_view(not_found, renderer='json') |
|
277 | config.add_notfound_view(not_found, renderer='json') | |
278 |
|
278 | |||
279 |
|
279 | |||
280 | def create_app(git_lfs_enabled, git_lfs_store_path, git_lfs_http_scheme): |
|
280 | def create_app(git_lfs_enabled, git_lfs_store_path, git_lfs_http_scheme): | |
281 | config = Configurator() |
|
281 | config = Configurator() | |
282 | if git_lfs_enabled: |
|
282 | if git_lfs_enabled: | |
283 | config.include(git_lfs_app) |
|
283 | config.include(git_lfs_app) | |
284 | config.registry.git_lfs_store_path = git_lfs_store_path |
|
284 | config.registry.git_lfs_store_path = git_lfs_store_path | |
285 | config.registry.git_lfs_http_scheme = git_lfs_http_scheme |
|
285 | config.registry.git_lfs_http_scheme = git_lfs_http_scheme | |
286 | else: |
|
286 | else: | |
287 | # not found handler for API, reporting disabled LFS support |
|
287 | # not found handler for API, reporting disabled LFS support | |
288 | config.add_notfound_view(lfs_disabled, renderer='json') |
|
288 | config.add_notfound_view(lfs_disabled, renderer='json') | |
289 |
|
289 | |||
290 | app = config.make_wsgi_app() |
|
290 | app = config.make_wsgi_app() | |
291 | return app |
|
291 | return app |
@@ -1,175 +1,175 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import os |
|
18 | import os | |
19 | import shutil |
|
19 | import shutil | |
20 | import logging |
|
20 | import logging | |
21 | from collections import OrderedDict |
|
21 | from collections import OrderedDict | |
22 |
|
22 | |||
23 | log = logging.getLogger(__name__) |
|
23 | log = logging.getLogger(__name__) | |
24 |
|
24 | |||
25 |
|
25 | |||
26 |
class OidHandler |
|
26 | class OidHandler: | |
27 |
|
27 | |||
28 | def __init__(self, store, repo_name, auth, oid, obj_size, obj_data, obj_href, |
|
28 | def __init__(self, store, repo_name, auth, oid, obj_size, obj_data, obj_href, | |
29 | obj_verify_href=None): |
|
29 | obj_verify_href=None): | |
30 | self.current_store = store |
|
30 | self.current_store = store | |
31 | self.repo_name = repo_name |
|
31 | self.repo_name = repo_name | |
32 | self.auth = auth |
|
32 | self.auth = auth | |
33 | self.oid = oid |
|
33 | self.oid = oid | |
34 | self.obj_size = obj_size |
|
34 | self.obj_size = obj_size | |
35 | self.obj_data = obj_data |
|
35 | self.obj_data = obj_data | |
36 | self.obj_href = obj_href |
|
36 | self.obj_href = obj_href | |
37 | self.obj_verify_href = obj_verify_href |
|
37 | self.obj_verify_href = obj_verify_href | |
38 |
|
38 | |||
39 | def get_store(self, mode=None): |
|
39 | def get_store(self, mode=None): | |
40 | return self.current_store |
|
40 | return self.current_store | |
41 |
|
41 | |||
42 | def get_auth(self): |
|
42 | def get_auth(self): | |
43 | """returns auth header for re-use in upload/download""" |
|
43 | """returns auth header for re-use in upload/download""" | |
44 | return " ".join(self.auth) |
|
44 | return " ".join(self.auth) | |
45 |
|
45 | |||
46 | def download(self): |
|
46 | def download(self): | |
47 |
|
47 | |||
48 | store = self.get_store() |
|
48 | store = self.get_store() | |
49 | response = None |
|
49 | response = None | |
50 | has_errors = None |
|
50 | has_errors = None | |
51 |
|
51 | |||
52 | if not store.has_oid(): |
|
52 | if not store.has_oid(): | |
53 | # error reply back to client that something is wrong with dl |
|
53 | # error reply back to client that something is wrong with dl | |
54 | err_msg = f'object: {store.oid} does not exist in store' |
|
54 | err_msg = f'object: {store.oid} does not exist in store' | |
55 | has_errors = OrderedDict( |
|
55 | has_errors = OrderedDict( | |
56 | error=OrderedDict( |
|
56 | error=OrderedDict( | |
57 | code=404, |
|
57 | code=404, | |
58 | message=err_msg |
|
58 | message=err_msg | |
59 | ) |
|
59 | ) | |
60 | ) |
|
60 | ) | |
61 |
|
61 | |||
62 | download_action = OrderedDict( |
|
62 | download_action = OrderedDict( | |
63 | href=self.obj_href, |
|
63 | href=self.obj_href, | |
64 | header=OrderedDict([("Authorization", self.get_auth())]) |
|
64 | header=OrderedDict([("Authorization", self.get_auth())]) | |
65 | ) |
|
65 | ) | |
66 | if not has_errors: |
|
66 | if not has_errors: | |
67 | response = OrderedDict(download=download_action) |
|
67 | response = OrderedDict(download=download_action) | |
68 | return response, has_errors |
|
68 | return response, has_errors | |
69 |
|
69 | |||
70 | def upload(self, skip_existing=True): |
|
70 | def upload(self, skip_existing=True): | |
71 | """ |
|
71 | """ | |
72 | Write upload action for git-lfs server |
|
72 | Write upload action for git-lfs server | |
73 | """ |
|
73 | """ | |
74 |
|
74 | |||
75 | store = self.get_store() |
|
75 | store = self.get_store() | |
76 | response = None |
|
76 | response = None | |
77 | has_errors = None |
|
77 | has_errors = None | |
78 |
|
78 | |||
79 | # verify if we have the OID before, if we do, reply with empty |
|
79 | # verify if we have the OID before, if we do, reply with empty | |
80 | if store.has_oid(): |
|
80 | if store.has_oid(): | |
81 | log.debug('LFS: store already has oid %s', store.oid) |
|
81 | log.debug('LFS: store already has oid %s', store.oid) | |
82 |
|
82 | |||
83 | # validate size |
|
83 | # validate size | |
84 | store_size = store.size_oid() |
|
84 | store_size = store.size_oid() | |
85 | size_match = store_size == self.obj_size |
|
85 | size_match = store_size == self.obj_size | |
86 | if not size_match: |
|
86 | if not size_match: | |
87 | log.warning( |
|
87 | log.warning( | |
88 | 'LFS: size mismatch for oid:%s, in store:%s expected: %s', |
|
88 | 'LFS: size mismatch for oid:%s, in store:%s expected: %s', | |
89 | self.oid, store_size, self.obj_size) |
|
89 | self.oid, store_size, self.obj_size) | |
90 | elif skip_existing: |
|
90 | elif skip_existing: | |
91 | log.debug('LFS: skipping further action as oid is existing') |
|
91 | log.debug('LFS: skipping further action as oid is existing') | |
92 | return response, has_errors |
|
92 | return response, has_errors | |
93 |
|
93 | |||
94 | chunked = ("Transfer-Encoding", "chunked") |
|
94 | chunked = ("Transfer-Encoding", "chunked") | |
95 | upload_action = OrderedDict( |
|
95 | upload_action = OrderedDict( | |
96 | href=self.obj_href, |
|
96 | href=self.obj_href, | |
97 | header=OrderedDict([("Authorization", self.get_auth()), chunked]) |
|
97 | header=OrderedDict([("Authorization", self.get_auth()), chunked]) | |
98 | ) |
|
98 | ) | |
99 | if not has_errors: |
|
99 | if not has_errors: | |
100 | response = OrderedDict(upload=upload_action) |
|
100 | response = OrderedDict(upload=upload_action) | |
101 | # if specified in handler, return the verification endpoint |
|
101 | # if specified in handler, return the verification endpoint | |
102 | if self.obj_verify_href: |
|
102 | if self.obj_verify_href: | |
103 | verify_action = OrderedDict( |
|
103 | verify_action = OrderedDict( | |
104 | href=self.obj_verify_href, |
|
104 | href=self.obj_verify_href, | |
105 | header=OrderedDict([("Authorization", self.get_auth())]) |
|
105 | header=OrderedDict([("Authorization", self.get_auth())]) | |
106 | ) |
|
106 | ) | |
107 | response['verify'] = verify_action |
|
107 | response['verify'] = verify_action | |
108 | return response, has_errors |
|
108 | return response, has_errors | |
109 |
|
109 | |||
110 | def exec_operation(self, operation, *args, **kwargs): |
|
110 | def exec_operation(self, operation, *args, **kwargs): | |
111 | handler = getattr(self, operation) |
|
111 | handler = getattr(self, operation) | |
112 | log.debug('LFS: handling request using %s handler', handler) |
|
112 | log.debug('LFS: handling request using %s handler', handler) | |
113 | return handler(*args, **kwargs) |
|
113 | return handler(*args, **kwargs) | |
114 |
|
114 | |||
115 |
|
115 | |||
116 |
class LFSOidStore |
|
116 | class LFSOidStore: | |
117 |
|
117 | |||
118 | def __init__(self, oid, repo, store_location=None): |
|
118 | def __init__(self, oid, repo, store_location=None): | |
119 | self.oid = oid |
|
119 | self.oid = oid | |
120 | self.repo = repo |
|
120 | self.repo = repo | |
121 | self.store_path = store_location or self.get_default_store() |
|
121 | self.store_path = store_location or self.get_default_store() | |
122 | self.tmp_oid_path = os.path.join(self.store_path, oid + '.tmp') |
|
122 | self.tmp_oid_path = os.path.join(self.store_path, oid + '.tmp') | |
123 | self.oid_path = os.path.join(self.store_path, oid) |
|
123 | self.oid_path = os.path.join(self.store_path, oid) | |
124 | self.fd = None |
|
124 | self.fd = None | |
125 |
|
125 | |||
126 | def get_engine(self, mode): |
|
126 | def get_engine(self, mode): | |
127 | """ |
|
127 | """ | |
128 | engine = .get_engine(mode='wb') |
|
128 | engine = .get_engine(mode='wb') | |
129 | with engine as f: |
|
129 | with engine as f: | |
130 | f.write('...') |
|
130 | f.write('...') | |
131 | """ |
|
131 | """ | |
132 |
|
132 | |||
133 | class StoreEngine(object): |
|
133 | class StoreEngine(object): | |
134 | def __init__(self, mode, store_path, oid_path, tmp_oid_path): |
|
134 | def __init__(self, mode, store_path, oid_path, tmp_oid_path): | |
135 | self.mode = mode |
|
135 | self.mode = mode | |
136 | self.store_path = store_path |
|
136 | self.store_path = store_path | |
137 | self.oid_path = oid_path |
|
137 | self.oid_path = oid_path | |
138 | self.tmp_oid_path = tmp_oid_path |
|
138 | self.tmp_oid_path = tmp_oid_path | |
139 |
|
139 | |||
140 | def __enter__(self): |
|
140 | def __enter__(self): | |
141 | if not os.path.isdir(self.store_path): |
|
141 | if not os.path.isdir(self.store_path): | |
142 | os.makedirs(self.store_path) |
|
142 | os.makedirs(self.store_path) | |
143 |
|
143 | |||
144 | # TODO(marcink): maybe write metadata here with size/oid ? |
|
144 | # TODO(marcink): maybe write metadata here with size/oid ? | |
145 | fd = open(self.tmp_oid_path, self.mode) |
|
145 | fd = open(self.tmp_oid_path, self.mode) | |
146 | self.fd = fd |
|
146 | self.fd = fd | |
147 | return fd |
|
147 | return fd | |
148 |
|
148 | |||
149 | def __exit__(self, exc_type, exc_value, traceback): |
|
149 | def __exit__(self, exc_type, exc_value, traceback): | |
150 | # close tmp file, and rename to final destination |
|
150 | # close tmp file, and rename to final destination | |
151 | self.fd.close() |
|
151 | self.fd.close() | |
152 | shutil.move(self.tmp_oid_path, self.oid_path) |
|
152 | shutil.move(self.tmp_oid_path, self.oid_path) | |
153 |
|
153 | |||
154 | return StoreEngine( |
|
154 | return StoreEngine( | |
155 | mode, self.store_path, self.oid_path, self.tmp_oid_path) |
|
155 | mode, self.store_path, self.oid_path, self.tmp_oid_path) | |
156 |
|
156 | |||
157 | def get_default_store(self): |
|
157 | def get_default_store(self): | |
158 | """ |
|
158 | """ | |
159 | Default store, consistent with defaults of Mercurial large files store |
|
159 | Default store, consistent with defaults of Mercurial large files store | |
160 | which is /home/username/.cache/largefiles |
|
160 | which is /home/username/.cache/largefiles | |
161 | """ |
|
161 | """ | |
162 | user_home = os.path.expanduser("~") |
|
162 | user_home = os.path.expanduser("~") | |
163 | return os.path.join(user_home, '.cache', 'lfs-store') |
|
163 | return os.path.join(user_home, '.cache', 'lfs-store') | |
164 |
|
164 | |||
165 | def has_oid(self): |
|
165 | def has_oid(self): | |
166 | return os.path.exists(os.path.join(self.store_path, self.oid)) |
|
166 | return os.path.exists(os.path.join(self.store_path, self.oid)) | |
167 |
|
167 | |||
168 | def size_oid(self): |
|
168 | def size_oid(self): | |
169 | size = -1 |
|
169 | size = -1 | |
170 |
|
170 | |||
171 | if self.has_oid(): |
|
171 | if self.has_oid(): | |
172 | oid = os.path.join(self.store_path, self.oid) |
|
172 | oid = os.path.join(self.store_path, self.oid) | |
173 | size = os.stat(oid).st_size |
|
173 | size = os.stat(oid).st_size | |
174 |
|
174 | |||
175 | return size |
|
175 | return size |
@@ -1,273 +1,273 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import os |
|
18 | import os | |
19 | import pytest |
|
19 | import pytest | |
20 | from webtest.app import TestApp as WebObTestApp |
|
20 | from webtest.app import TestApp as WebObTestApp | |
21 |
|
21 | |||
22 | from vcsserver.lib.rc_json import json |
|
22 | from vcsserver.lib.rc_json import json | |
23 | from vcsserver.str_utils import safe_bytes |
|
23 | from vcsserver.str_utils import safe_bytes | |
24 | from vcsserver.git_lfs.app import create_app |
|
24 | from vcsserver.git_lfs.app import create_app | |
25 |
|
25 | |||
26 |
|
26 | |||
27 | @pytest.fixture(scope='function') |
|
27 | @pytest.fixture(scope='function') | |
28 | def git_lfs_app(tmpdir): |
|
28 | def git_lfs_app(tmpdir): | |
29 | custom_app = WebObTestApp(create_app( |
|
29 | custom_app = WebObTestApp(create_app( | |
30 | git_lfs_enabled=True, git_lfs_store_path=str(tmpdir), |
|
30 | git_lfs_enabled=True, git_lfs_store_path=str(tmpdir), | |
31 | git_lfs_http_scheme='http')) |
|
31 | git_lfs_http_scheme='http')) | |
32 | custom_app._store = str(tmpdir) |
|
32 | custom_app._store = str(tmpdir) | |
33 | return custom_app |
|
33 | return custom_app | |
34 |
|
34 | |||
35 |
|
35 | |||
36 | @pytest.fixture(scope='function') |
|
36 | @pytest.fixture(scope='function') | |
37 | def git_lfs_https_app(tmpdir): |
|
37 | def git_lfs_https_app(tmpdir): | |
38 | custom_app = WebObTestApp(create_app( |
|
38 | custom_app = WebObTestApp(create_app( | |
39 | git_lfs_enabled=True, git_lfs_store_path=str(tmpdir), |
|
39 | git_lfs_enabled=True, git_lfs_store_path=str(tmpdir), | |
40 | git_lfs_http_scheme='https')) |
|
40 | git_lfs_http_scheme='https')) | |
41 | custom_app._store = str(tmpdir) |
|
41 | custom_app._store = str(tmpdir) | |
42 | return custom_app |
|
42 | return custom_app | |
43 |
|
43 | |||
44 |
|
44 | |||
45 | @pytest.fixture() |
|
45 | @pytest.fixture() | |
46 | def http_auth(): |
|
46 | def http_auth(): | |
47 | return {'HTTP_AUTHORIZATION': "Basic XXXXX"} |
|
47 | return {'HTTP_AUTHORIZATION': "Basic XXXXX"} | |
48 |
|
48 | |||
49 |
|
49 | |||
50 |
class TestLFSApplication |
|
50 | class TestLFSApplication: | |
51 |
|
51 | |||
52 | def test_app_wrong_path(self, git_lfs_app): |
|
52 | def test_app_wrong_path(self, git_lfs_app): | |
53 | git_lfs_app.get('/repo/info/lfs/xxx', status=404) |
|
53 | git_lfs_app.get('/repo/info/lfs/xxx', status=404) | |
54 |
|
54 | |||
55 | def test_app_deprecated_endpoint(self, git_lfs_app): |
|
55 | def test_app_deprecated_endpoint(self, git_lfs_app): | |
56 | response = git_lfs_app.post('/repo/info/lfs/objects', status=501) |
|
56 | response = git_lfs_app.post('/repo/info/lfs/objects', status=501) | |
57 | assert response.status_code == 501 |
|
57 | assert response.status_code == 501 | |
58 | assert json.loads(response.text) == {'message': 'LFS: v1 api not supported'} |
|
58 | assert json.loads(response.text) == {'message': 'LFS: v1 api not supported'} | |
59 |
|
59 | |||
60 | def test_app_lock_verify_api_not_available(self, git_lfs_app): |
|
60 | def test_app_lock_verify_api_not_available(self, git_lfs_app): | |
61 | response = git_lfs_app.post('/repo/info/lfs/locks/verify', status=501) |
|
61 | response = git_lfs_app.post('/repo/info/lfs/locks/verify', status=501) | |
62 | assert response.status_code == 501 |
|
62 | assert response.status_code == 501 | |
63 | assert json.loads(response.text) == { |
|
63 | assert json.loads(response.text) == { | |
64 | 'message': 'GIT LFS locking api not supported'} |
|
64 | 'message': 'GIT LFS locking api not supported'} | |
65 |
|
65 | |||
66 | def test_app_lock_api_not_available(self, git_lfs_app): |
|
66 | def test_app_lock_api_not_available(self, git_lfs_app): | |
67 | response = git_lfs_app.post('/repo/info/lfs/locks', status=501) |
|
67 | response = git_lfs_app.post('/repo/info/lfs/locks', status=501) | |
68 | assert response.status_code == 501 |
|
68 | assert response.status_code == 501 | |
69 | assert json.loads(response.text) == { |
|
69 | assert json.loads(response.text) == { | |
70 | 'message': 'GIT LFS locking api not supported'} |
|
70 | 'message': 'GIT LFS locking api not supported'} | |
71 |
|
71 | |||
72 | def test_app_batch_api_missing_auth(self, git_lfs_app): |
|
72 | def test_app_batch_api_missing_auth(self, git_lfs_app): | |
73 | git_lfs_app.post_json( |
|
73 | git_lfs_app.post_json( | |
74 | '/repo/info/lfs/objects/batch', params={}, status=403) |
|
74 | '/repo/info/lfs/objects/batch', params={}, status=403) | |
75 |
|
75 | |||
76 | def test_app_batch_api_unsupported_operation(self, git_lfs_app, http_auth): |
|
76 | def test_app_batch_api_unsupported_operation(self, git_lfs_app, http_auth): | |
77 | response = git_lfs_app.post_json( |
|
77 | response = git_lfs_app.post_json( | |
78 | '/repo/info/lfs/objects/batch', params={}, status=400, |
|
78 | '/repo/info/lfs/objects/batch', params={}, status=400, | |
79 | extra_environ=http_auth) |
|
79 | extra_environ=http_auth) | |
80 | assert json.loads(response.text) == { |
|
80 | assert json.loads(response.text) == { | |
81 | 'message': 'unsupported operation mode: `None`'} |
|
81 | 'message': 'unsupported operation mode: `None`'} | |
82 |
|
82 | |||
83 | def test_app_batch_api_missing_objects(self, git_lfs_app, http_auth): |
|
83 | def test_app_batch_api_missing_objects(self, git_lfs_app, http_auth): | |
84 | response = git_lfs_app.post_json( |
|
84 | response = git_lfs_app.post_json( | |
85 | '/repo/info/lfs/objects/batch', params={'operation': 'download'}, |
|
85 | '/repo/info/lfs/objects/batch', params={'operation': 'download'}, | |
86 | status=400, extra_environ=http_auth) |
|
86 | status=400, extra_environ=http_auth) | |
87 | assert json.loads(response.text) == { |
|
87 | assert json.loads(response.text) == { | |
88 | 'message': 'missing objects data'} |
|
88 | 'message': 'missing objects data'} | |
89 |
|
89 | |||
90 | def test_app_batch_api_unsupported_data_in_objects( |
|
90 | def test_app_batch_api_unsupported_data_in_objects( | |
91 | self, git_lfs_app, http_auth): |
|
91 | self, git_lfs_app, http_auth): | |
92 | params = {'operation': 'download', |
|
92 | params = {'operation': 'download', | |
93 | 'objects': [{}]} |
|
93 | 'objects': [{}]} | |
94 | response = git_lfs_app.post_json( |
|
94 | response = git_lfs_app.post_json( | |
95 | '/repo/info/lfs/objects/batch', params=params, status=400, |
|
95 | '/repo/info/lfs/objects/batch', params=params, status=400, | |
96 | extra_environ=http_auth) |
|
96 | extra_environ=http_auth) | |
97 | assert json.loads(response.text) == { |
|
97 | assert json.loads(response.text) == { | |
98 | 'message': 'unsupported data in objects'} |
|
98 | 'message': 'unsupported data in objects'} | |
99 |
|
99 | |||
100 | def test_app_batch_api_download_missing_object( |
|
100 | def test_app_batch_api_download_missing_object( | |
101 | self, git_lfs_app, http_auth): |
|
101 | self, git_lfs_app, http_auth): | |
102 | params = {'operation': 'download', |
|
102 | params = {'operation': 'download', | |
103 | 'objects': [{'oid': '123', 'size': '1024'}]} |
|
103 | 'objects': [{'oid': '123', 'size': '1024'}]} | |
104 | response = git_lfs_app.post_json( |
|
104 | response = git_lfs_app.post_json( | |
105 | '/repo/info/lfs/objects/batch', params=params, |
|
105 | '/repo/info/lfs/objects/batch', params=params, | |
106 | extra_environ=http_auth) |
|
106 | extra_environ=http_auth) | |
107 |
|
107 | |||
108 | expected_objects = [ |
|
108 | expected_objects = [ | |
109 | {'authenticated': True, |
|
109 | {'authenticated': True, | |
110 | 'errors': {'error': { |
|
110 | 'errors': {'error': { | |
111 | 'code': 404, |
|
111 | 'code': 404, | |
112 | 'message': 'object: 123 does not exist in store'}}, |
|
112 | 'message': 'object: 123 does not exist in store'}}, | |
113 | 'oid': '123', |
|
113 | 'oid': '123', | |
114 | 'size': '1024'} |
|
114 | 'size': '1024'} | |
115 | ] |
|
115 | ] | |
116 | assert json.loads(response.text) == { |
|
116 | assert json.loads(response.text) == { | |
117 | 'objects': expected_objects, 'transfer': 'basic'} |
|
117 | 'objects': expected_objects, 'transfer': 'basic'} | |
118 |
|
118 | |||
119 | def test_app_batch_api_download(self, git_lfs_app, http_auth): |
|
119 | def test_app_batch_api_download(self, git_lfs_app, http_auth): | |
120 | oid = '456' |
|
120 | oid = '456' | |
121 | oid_path = os.path.join(git_lfs_app._store, oid) |
|
121 | oid_path = os.path.join(git_lfs_app._store, oid) | |
122 | if not os.path.isdir(os.path.dirname(oid_path)): |
|
122 | if not os.path.isdir(os.path.dirname(oid_path)): | |
123 | os.makedirs(os.path.dirname(oid_path)) |
|
123 | os.makedirs(os.path.dirname(oid_path)) | |
124 | with open(oid_path, 'wb') as f: |
|
124 | with open(oid_path, 'wb') as f: | |
125 | f.write(safe_bytes('OID_CONTENT')) |
|
125 | f.write(safe_bytes('OID_CONTENT')) | |
126 |
|
126 | |||
127 | params = {'operation': 'download', |
|
127 | params = {'operation': 'download', | |
128 | 'objects': [{'oid': oid, 'size': '1024'}]} |
|
128 | 'objects': [{'oid': oid, 'size': '1024'}]} | |
129 | response = git_lfs_app.post_json( |
|
129 | response = git_lfs_app.post_json( | |
130 | '/repo/info/lfs/objects/batch', params=params, |
|
130 | '/repo/info/lfs/objects/batch', params=params, | |
131 | extra_environ=http_auth) |
|
131 | extra_environ=http_auth) | |
132 |
|
132 | |||
133 | expected_objects = [ |
|
133 | expected_objects = [ | |
134 | {'authenticated': True, |
|
134 | {'authenticated': True, | |
135 | 'actions': { |
|
135 | 'actions': { | |
136 | 'download': { |
|
136 | 'download': { | |
137 | 'header': {'Authorization': 'Basic XXXXX'}, |
|
137 | 'header': {'Authorization': 'Basic XXXXX'}, | |
138 | 'href': 'http://localhost/repo/info/lfs/objects/456'}, |
|
138 | 'href': 'http://localhost/repo/info/lfs/objects/456'}, | |
139 | }, |
|
139 | }, | |
140 | 'oid': '456', |
|
140 | 'oid': '456', | |
141 | 'size': '1024'} |
|
141 | 'size': '1024'} | |
142 | ] |
|
142 | ] | |
143 | assert json.loads(response.text) == { |
|
143 | assert json.loads(response.text) == { | |
144 | 'objects': expected_objects, 'transfer': 'basic'} |
|
144 | 'objects': expected_objects, 'transfer': 'basic'} | |
145 |
|
145 | |||
146 | def test_app_batch_api_upload(self, git_lfs_app, http_auth): |
|
146 | def test_app_batch_api_upload(self, git_lfs_app, http_auth): | |
147 | params = {'operation': 'upload', |
|
147 | params = {'operation': 'upload', | |
148 | 'objects': [{'oid': '123', 'size': '1024'}]} |
|
148 | 'objects': [{'oid': '123', 'size': '1024'}]} | |
149 | response = git_lfs_app.post_json( |
|
149 | response = git_lfs_app.post_json( | |
150 | '/repo/info/lfs/objects/batch', params=params, |
|
150 | '/repo/info/lfs/objects/batch', params=params, | |
151 | extra_environ=http_auth) |
|
151 | extra_environ=http_auth) | |
152 | expected_objects = [ |
|
152 | expected_objects = [ | |
153 | {'authenticated': True, |
|
153 | {'authenticated': True, | |
154 | 'actions': { |
|
154 | 'actions': { | |
155 | 'upload': { |
|
155 | 'upload': { | |
156 | 'header': {'Authorization': 'Basic XXXXX', |
|
156 | 'header': {'Authorization': 'Basic XXXXX', | |
157 | 'Transfer-Encoding': 'chunked'}, |
|
157 | 'Transfer-Encoding': 'chunked'}, | |
158 | 'href': 'http://localhost/repo/info/lfs/objects/123'}, |
|
158 | 'href': 'http://localhost/repo/info/lfs/objects/123'}, | |
159 | 'verify': { |
|
159 | 'verify': { | |
160 | 'header': {'Authorization': 'Basic XXXXX'}, |
|
160 | 'header': {'Authorization': 'Basic XXXXX'}, | |
161 | 'href': 'http://localhost/repo/info/lfs/verify'} |
|
161 | 'href': 'http://localhost/repo/info/lfs/verify'} | |
162 | }, |
|
162 | }, | |
163 | 'oid': '123', |
|
163 | 'oid': '123', | |
164 | 'size': '1024'} |
|
164 | 'size': '1024'} | |
165 | ] |
|
165 | ] | |
166 | assert json.loads(response.text) == { |
|
166 | assert json.loads(response.text) == { | |
167 | 'objects': expected_objects, 'transfer': 'basic'} |
|
167 | 'objects': expected_objects, 'transfer': 'basic'} | |
168 |
|
168 | |||
169 | def test_app_batch_api_upload_for_https(self, git_lfs_https_app, http_auth): |
|
169 | def test_app_batch_api_upload_for_https(self, git_lfs_https_app, http_auth): | |
170 | params = {'operation': 'upload', |
|
170 | params = {'operation': 'upload', | |
171 | 'objects': [{'oid': '123', 'size': '1024'}]} |
|
171 | 'objects': [{'oid': '123', 'size': '1024'}]} | |
172 | response = git_lfs_https_app.post_json( |
|
172 | response = git_lfs_https_app.post_json( | |
173 | '/repo/info/lfs/objects/batch', params=params, |
|
173 | '/repo/info/lfs/objects/batch', params=params, | |
174 | extra_environ=http_auth) |
|
174 | extra_environ=http_auth) | |
175 | expected_objects = [ |
|
175 | expected_objects = [ | |
176 | {'authenticated': True, |
|
176 | {'authenticated': True, | |
177 | 'actions': { |
|
177 | 'actions': { | |
178 | 'upload': { |
|
178 | 'upload': { | |
179 | 'header': {'Authorization': 'Basic XXXXX', |
|
179 | 'header': {'Authorization': 'Basic XXXXX', | |
180 | 'Transfer-Encoding': 'chunked'}, |
|
180 | 'Transfer-Encoding': 'chunked'}, | |
181 | 'href': 'https://localhost/repo/info/lfs/objects/123'}, |
|
181 | 'href': 'https://localhost/repo/info/lfs/objects/123'}, | |
182 | 'verify': { |
|
182 | 'verify': { | |
183 | 'header': {'Authorization': 'Basic XXXXX'}, |
|
183 | 'header': {'Authorization': 'Basic XXXXX'}, | |
184 | 'href': 'https://localhost/repo/info/lfs/verify'} |
|
184 | 'href': 'https://localhost/repo/info/lfs/verify'} | |
185 | }, |
|
185 | }, | |
186 | 'oid': '123', |
|
186 | 'oid': '123', | |
187 | 'size': '1024'} |
|
187 | 'size': '1024'} | |
188 | ] |
|
188 | ] | |
189 | assert json.loads(response.text) == { |
|
189 | assert json.loads(response.text) == { | |
190 | 'objects': expected_objects, 'transfer': 'basic'} |
|
190 | 'objects': expected_objects, 'transfer': 'basic'} | |
191 |
|
191 | |||
192 | def test_app_verify_api_missing_data(self, git_lfs_app): |
|
192 | def test_app_verify_api_missing_data(self, git_lfs_app): | |
193 | params = {'oid': 'missing'} |
|
193 | params = {'oid': 'missing'} | |
194 | response = git_lfs_app.post_json( |
|
194 | response = git_lfs_app.post_json( | |
195 | '/repo/info/lfs/verify', params=params, |
|
195 | '/repo/info/lfs/verify', params=params, | |
196 | status=400) |
|
196 | status=400) | |
197 |
|
197 | |||
198 | assert json.loads(response.text) == { |
|
198 | assert json.loads(response.text) == { | |
199 | 'message': 'missing oid and size in request data'} |
|
199 | 'message': 'missing oid and size in request data'} | |
200 |
|
200 | |||
201 | def test_app_verify_api_missing_obj(self, git_lfs_app): |
|
201 | def test_app_verify_api_missing_obj(self, git_lfs_app): | |
202 | params = {'oid': 'missing', 'size': '1024'} |
|
202 | params = {'oid': 'missing', 'size': '1024'} | |
203 | response = git_lfs_app.post_json( |
|
203 | response = git_lfs_app.post_json( | |
204 | '/repo/info/lfs/verify', params=params, |
|
204 | '/repo/info/lfs/verify', params=params, | |
205 | status=404) |
|
205 | status=404) | |
206 |
|
206 | |||
207 | assert json.loads(response.text) == { |
|
207 | assert json.loads(response.text) == { | |
208 | 'message': 'oid `missing` does not exists in store'} |
|
208 | 'message': 'oid `missing` does not exists in store'} | |
209 |
|
209 | |||
210 | def test_app_verify_api_size_mismatch(self, git_lfs_app): |
|
210 | def test_app_verify_api_size_mismatch(self, git_lfs_app): | |
211 | oid = 'existing' |
|
211 | oid = 'existing' | |
212 | oid_path = os.path.join(git_lfs_app._store, oid) |
|
212 | oid_path = os.path.join(git_lfs_app._store, oid) | |
213 | if not os.path.isdir(os.path.dirname(oid_path)): |
|
213 | if not os.path.isdir(os.path.dirname(oid_path)): | |
214 | os.makedirs(os.path.dirname(oid_path)) |
|
214 | os.makedirs(os.path.dirname(oid_path)) | |
215 | with open(oid_path, 'wb') as f: |
|
215 | with open(oid_path, 'wb') as f: | |
216 | f.write(safe_bytes('OID_CONTENT')) |
|
216 | f.write(safe_bytes('OID_CONTENT')) | |
217 |
|
217 | |||
218 | params = {'oid': oid, 'size': '1024'} |
|
218 | params = {'oid': oid, 'size': '1024'} | |
219 | response = git_lfs_app.post_json( |
|
219 | response = git_lfs_app.post_json( | |
220 | '/repo/info/lfs/verify', params=params, status=422) |
|
220 | '/repo/info/lfs/verify', params=params, status=422) | |
221 |
|
221 | |||
222 | assert json.loads(response.text) == { |
|
222 | assert json.loads(response.text) == { | |
223 | 'message': 'requested file size mismatch ' |
|
223 | 'message': 'requested file size mismatch ' | |
224 | 'store size:11 requested:1024'} |
|
224 | 'store size:11 requested:1024'} | |
225 |
|
225 | |||
226 | def test_app_verify_api(self, git_lfs_app): |
|
226 | def test_app_verify_api(self, git_lfs_app): | |
227 | oid = 'existing' |
|
227 | oid = 'existing' | |
228 | oid_path = os.path.join(git_lfs_app._store, oid) |
|
228 | oid_path = os.path.join(git_lfs_app._store, oid) | |
229 | if not os.path.isdir(os.path.dirname(oid_path)): |
|
229 | if not os.path.isdir(os.path.dirname(oid_path)): | |
230 | os.makedirs(os.path.dirname(oid_path)) |
|
230 | os.makedirs(os.path.dirname(oid_path)) | |
231 | with open(oid_path, 'wb') as f: |
|
231 | with open(oid_path, 'wb') as f: | |
232 | f.write(safe_bytes('OID_CONTENT')) |
|
232 | f.write(safe_bytes('OID_CONTENT')) | |
233 |
|
233 | |||
234 | params = {'oid': oid, 'size': 11} |
|
234 | params = {'oid': oid, 'size': 11} | |
235 | response = git_lfs_app.post_json( |
|
235 | response = git_lfs_app.post_json( | |
236 | '/repo/info/lfs/verify', params=params) |
|
236 | '/repo/info/lfs/verify', params=params) | |
237 |
|
237 | |||
238 | assert json.loads(response.text) == { |
|
238 | assert json.loads(response.text) == { | |
239 | 'message': {'size': 'ok', 'in_store': 'ok'}} |
|
239 | 'message': {'size': 'ok', 'in_store': 'ok'}} | |
240 |
|
240 | |||
241 | def test_app_download_api_oid_not_existing(self, git_lfs_app): |
|
241 | def test_app_download_api_oid_not_existing(self, git_lfs_app): | |
242 | oid = 'missing' |
|
242 | oid = 'missing' | |
243 |
|
243 | |||
244 | response = git_lfs_app.get( |
|
244 | response = git_lfs_app.get( | |
245 | '/repo/info/lfs/objects/{oid}'.format(oid=oid), status=404) |
|
245 | '/repo/info/lfs/objects/{oid}'.format(oid=oid), status=404) | |
246 |
|
246 | |||
247 | assert json.loads(response.text) == { |
|
247 | assert json.loads(response.text) == { | |
248 | 'message': 'requested file with oid `missing` not found in store'} |
|
248 | 'message': 'requested file with oid `missing` not found in store'} | |
249 |
|
249 | |||
250 | def test_app_download_api(self, git_lfs_app): |
|
250 | def test_app_download_api(self, git_lfs_app): | |
251 | oid = 'existing' |
|
251 | oid = 'existing' | |
252 | oid_path = os.path.join(git_lfs_app._store, oid) |
|
252 | oid_path = os.path.join(git_lfs_app._store, oid) | |
253 | if not os.path.isdir(os.path.dirname(oid_path)): |
|
253 | if not os.path.isdir(os.path.dirname(oid_path)): | |
254 | os.makedirs(os.path.dirname(oid_path)) |
|
254 | os.makedirs(os.path.dirname(oid_path)) | |
255 | with open(oid_path, 'wb') as f: |
|
255 | with open(oid_path, 'wb') as f: | |
256 | f.write(safe_bytes('OID_CONTENT')) |
|
256 | f.write(safe_bytes('OID_CONTENT')) | |
257 |
|
257 | |||
258 | response = git_lfs_app.get( |
|
258 | response = git_lfs_app.get( | |
259 | '/repo/info/lfs/objects/{oid}'.format(oid=oid)) |
|
259 | '/repo/info/lfs/objects/{oid}'.format(oid=oid)) | |
260 | assert response |
|
260 | assert response | |
261 |
|
261 | |||
262 | def test_app_upload(self, git_lfs_app): |
|
262 | def test_app_upload(self, git_lfs_app): | |
263 | oid = 'uploaded' |
|
263 | oid = 'uploaded' | |
264 |
|
264 | |||
265 | response = git_lfs_app.put( |
|
265 | response = git_lfs_app.put( | |
266 | '/repo/info/lfs/objects/{oid}'.format(oid=oid), params='CONTENT') |
|
266 | '/repo/info/lfs/objects/{oid}'.format(oid=oid), params='CONTENT') | |
267 |
|
267 | |||
268 | assert json.loads(response.text) == {'upload': 'ok'} |
|
268 | assert json.loads(response.text) == {'upload': 'ok'} | |
269 |
|
269 | |||
270 | # verify that we actually wrote that OID |
|
270 | # verify that we actually wrote that OID | |
271 | oid_path = os.path.join(git_lfs_app._store, oid) |
|
271 | oid_path = os.path.join(git_lfs_app._store, oid) | |
272 | assert os.path.isfile(oid_path) |
|
272 | assert os.path.isfile(oid_path) | |
273 | assert 'CONTENT' == open(oid_path).read() |
|
273 | assert 'CONTENT' == open(oid_path).read() |
@@ -1,142 +1,142 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import os |
|
18 | import os | |
19 | import pytest |
|
19 | import pytest | |
20 | from vcsserver.str_utils import safe_bytes |
|
20 | from vcsserver.str_utils import safe_bytes | |
21 | from vcsserver.git_lfs.lib import OidHandler, LFSOidStore |
|
21 | from vcsserver.git_lfs.lib import OidHandler, LFSOidStore | |
22 |
|
22 | |||
23 |
|
23 | |||
24 | @pytest.fixture() |
|
24 | @pytest.fixture() | |
25 | def lfs_store(tmpdir): |
|
25 | def lfs_store(tmpdir): | |
26 | repo = 'test' |
|
26 | repo = 'test' | |
27 | oid = '123456789' |
|
27 | oid = '123456789' | |
28 | store = LFSOidStore(oid=oid, repo=repo, store_location=str(tmpdir)) |
|
28 | store = LFSOidStore(oid=oid, repo=repo, store_location=str(tmpdir)) | |
29 | return store |
|
29 | return store | |
30 |
|
30 | |||
31 |
|
31 | |||
32 | @pytest.fixture() |
|
32 | @pytest.fixture() | |
33 | def oid_handler(lfs_store): |
|
33 | def oid_handler(lfs_store): | |
34 | store = lfs_store |
|
34 | store = lfs_store | |
35 | repo = store.repo |
|
35 | repo = store.repo | |
36 | oid = store.oid |
|
36 | oid = store.oid | |
37 |
|
37 | |||
38 | oid_handler = OidHandler( |
|
38 | oid_handler = OidHandler( | |
39 | store=store, repo_name=repo, auth=('basic', 'xxxx'), |
|
39 | store=store, repo_name=repo, auth=('basic', 'xxxx'), | |
40 | oid=oid, |
|
40 | oid=oid, | |
41 | obj_size='1024', obj_data={}, obj_href='http://localhost/handle_oid', |
|
41 | obj_size='1024', obj_data={}, obj_href='http://localhost/handle_oid', | |
42 | obj_verify_href='http://localhost/verify') |
|
42 | obj_verify_href='http://localhost/verify') | |
43 | return oid_handler |
|
43 | return oid_handler | |
44 |
|
44 | |||
45 |
|
45 | |||
46 |
class TestOidHandler |
|
46 | class TestOidHandler: | |
47 |
|
47 | |||
48 | @pytest.mark.parametrize('exec_action', [ |
|
48 | @pytest.mark.parametrize('exec_action', [ | |
49 | 'download', |
|
49 | 'download', | |
50 | 'upload', |
|
50 | 'upload', | |
51 | ]) |
|
51 | ]) | |
52 | def test_exec_action(self, exec_action, oid_handler): |
|
52 | def test_exec_action(self, exec_action, oid_handler): | |
53 | handler = oid_handler.exec_operation(exec_action) |
|
53 | handler = oid_handler.exec_operation(exec_action) | |
54 | assert handler |
|
54 | assert handler | |
55 |
|
55 | |||
56 | def test_exec_action_undefined(self, oid_handler): |
|
56 | def test_exec_action_undefined(self, oid_handler): | |
57 | with pytest.raises(AttributeError): |
|
57 | with pytest.raises(AttributeError): | |
58 | oid_handler.exec_operation('wrong') |
|
58 | oid_handler.exec_operation('wrong') | |
59 |
|
59 | |||
60 | def test_download_oid_not_existing(self, oid_handler): |
|
60 | def test_download_oid_not_existing(self, oid_handler): | |
61 | response, has_errors = oid_handler.exec_operation('download') |
|
61 | response, has_errors = oid_handler.exec_operation('download') | |
62 |
|
62 | |||
63 | assert response is None |
|
63 | assert response is None | |
64 | assert has_errors['error'] == { |
|
64 | assert has_errors['error'] == { | |
65 | 'code': 404, |
|
65 | 'code': 404, | |
66 | 'message': 'object: 123456789 does not exist in store'} |
|
66 | 'message': 'object: 123456789 does not exist in store'} | |
67 |
|
67 | |||
68 | def test_download_oid(self, oid_handler): |
|
68 | def test_download_oid(self, oid_handler): | |
69 | store = oid_handler.get_store() |
|
69 | store = oid_handler.get_store() | |
70 | if not os.path.isdir(os.path.dirname(store.oid_path)): |
|
70 | if not os.path.isdir(os.path.dirname(store.oid_path)): | |
71 | os.makedirs(os.path.dirname(store.oid_path)) |
|
71 | os.makedirs(os.path.dirname(store.oid_path)) | |
72 |
|
72 | |||
73 | with open(store.oid_path, 'wb') as f: |
|
73 | with open(store.oid_path, 'wb') as f: | |
74 | f.write(safe_bytes('CONTENT')) |
|
74 | f.write(safe_bytes('CONTENT')) | |
75 |
|
75 | |||
76 | response, has_errors = oid_handler.exec_operation('download') |
|
76 | response, has_errors = oid_handler.exec_operation('download') | |
77 |
|
77 | |||
78 | assert has_errors is None |
|
78 | assert has_errors is None | |
79 | assert response['download'] == { |
|
79 | assert response['download'] == { | |
80 | 'header': {'Authorization': 'basic xxxx'}, |
|
80 | 'header': {'Authorization': 'basic xxxx'}, | |
81 | 'href': 'http://localhost/handle_oid' |
|
81 | 'href': 'http://localhost/handle_oid' | |
82 | } |
|
82 | } | |
83 |
|
83 | |||
84 | def test_upload_oid_that_exists(self, oid_handler): |
|
84 | def test_upload_oid_that_exists(self, oid_handler): | |
85 | store = oid_handler.get_store() |
|
85 | store = oid_handler.get_store() | |
86 | if not os.path.isdir(os.path.dirname(store.oid_path)): |
|
86 | if not os.path.isdir(os.path.dirname(store.oid_path)): | |
87 | os.makedirs(os.path.dirname(store.oid_path)) |
|
87 | os.makedirs(os.path.dirname(store.oid_path)) | |
88 |
|
88 | |||
89 | with open(store.oid_path, 'wb') as f: |
|
89 | with open(store.oid_path, 'wb') as f: | |
90 | f.write(safe_bytes('CONTENT')) |
|
90 | f.write(safe_bytes('CONTENT')) | |
91 | oid_handler.obj_size = 7 |
|
91 | oid_handler.obj_size = 7 | |
92 | response, has_errors = oid_handler.exec_operation('upload') |
|
92 | response, has_errors = oid_handler.exec_operation('upload') | |
93 | assert has_errors is None |
|
93 | assert has_errors is None | |
94 | assert response is None |
|
94 | assert response is None | |
95 |
|
95 | |||
96 | def test_upload_oid_that_exists_but_has_wrong_size(self, oid_handler): |
|
96 | def test_upload_oid_that_exists_but_has_wrong_size(self, oid_handler): | |
97 | store = oid_handler.get_store() |
|
97 | store = oid_handler.get_store() | |
98 | if not os.path.isdir(os.path.dirname(store.oid_path)): |
|
98 | if not os.path.isdir(os.path.dirname(store.oid_path)): | |
99 | os.makedirs(os.path.dirname(store.oid_path)) |
|
99 | os.makedirs(os.path.dirname(store.oid_path)) | |
100 |
|
100 | |||
101 | with open(store.oid_path, 'wb') as f: |
|
101 | with open(store.oid_path, 'wb') as f: | |
102 | f.write(safe_bytes('CONTENT')) |
|
102 | f.write(safe_bytes('CONTENT')) | |
103 |
|
103 | |||
104 | oid_handler.obj_size = 10240 |
|
104 | oid_handler.obj_size = 10240 | |
105 | response, has_errors = oid_handler.exec_operation('upload') |
|
105 | response, has_errors = oid_handler.exec_operation('upload') | |
106 | assert has_errors is None |
|
106 | assert has_errors is None | |
107 | assert response['upload'] == { |
|
107 | assert response['upload'] == { | |
108 | 'header': {'Authorization': 'basic xxxx', |
|
108 | 'header': {'Authorization': 'basic xxxx', | |
109 | 'Transfer-Encoding': 'chunked'}, |
|
109 | 'Transfer-Encoding': 'chunked'}, | |
110 | 'href': 'http://localhost/handle_oid', |
|
110 | 'href': 'http://localhost/handle_oid', | |
111 | } |
|
111 | } | |
112 |
|
112 | |||
113 | def test_upload_oid(self, oid_handler): |
|
113 | def test_upload_oid(self, oid_handler): | |
114 | response, has_errors = oid_handler.exec_operation('upload') |
|
114 | response, has_errors = oid_handler.exec_operation('upload') | |
115 | assert has_errors is None |
|
115 | assert has_errors is None | |
116 | assert response['upload'] == { |
|
116 | assert response['upload'] == { | |
117 | 'header': {'Authorization': 'basic xxxx', |
|
117 | 'header': {'Authorization': 'basic xxxx', | |
118 | 'Transfer-Encoding': 'chunked'}, |
|
118 | 'Transfer-Encoding': 'chunked'}, | |
119 | 'href': 'http://localhost/handle_oid' |
|
119 | 'href': 'http://localhost/handle_oid' | |
120 | } |
|
120 | } | |
121 |
|
121 | |||
122 |
|
122 | |||
123 |
class TestLFSStore |
|
123 | class TestLFSStore: | |
124 | def test_write_oid(self, lfs_store): |
|
124 | def test_write_oid(self, lfs_store): | |
125 | oid_location = lfs_store.oid_path |
|
125 | oid_location = lfs_store.oid_path | |
126 |
|
126 | |||
127 | assert not os.path.isfile(oid_location) |
|
127 | assert not os.path.isfile(oid_location) | |
128 |
|
128 | |||
129 | engine = lfs_store.get_engine(mode='wb') |
|
129 | engine = lfs_store.get_engine(mode='wb') | |
130 | with engine as f: |
|
130 | with engine as f: | |
131 | f.write(safe_bytes('CONTENT')) |
|
131 | f.write(safe_bytes('CONTENT')) | |
132 |
|
132 | |||
133 | assert os.path.isfile(oid_location) |
|
133 | assert os.path.isfile(oid_location) | |
134 |
|
134 | |||
135 | def test_detect_has_oid(self, lfs_store): |
|
135 | def test_detect_has_oid(self, lfs_store): | |
136 |
|
136 | |||
137 | assert lfs_store.has_oid() is False |
|
137 | assert lfs_store.has_oid() is False | |
138 | engine = lfs_store.get_engine(mode='wb') |
|
138 | engine = lfs_store.get_engine(mode='wb') | |
139 | with engine as f: |
|
139 | with engine as f: | |
140 | f.write(safe_bytes('CONTENT')) |
|
140 | f.write(safe_bytes('CONTENT')) | |
141 |
|
141 | |||
142 | assert lfs_store.has_oid() is True |
|
142 | assert lfs_store.has_oid() is True |
@@ -1,202 +1,202 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import re |
|
18 | import re | |
19 | import os |
|
19 | import os | |
20 | import sys |
|
20 | import sys | |
21 | import datetime |
|
21 | import datetime | |
22 | import logging |
|
22 | import logging | |
23 | import pkg_resources |
|
23 | import pkg_resources | |
24 |
|
24 | |||
25 | import vcsserver |
|
25 | import vcsserver | |
26 | from vcsserver.str_utils import safe_bytes |
|
26 | from vcsserver.str_utils import safe_bytes | |
27 |
|
27 | |||
28 | log = logging.getLogger(__name__) |
|
28 | log = logging.getLogger(__name__) | |
29 |
|
29 | |||
30 |
|
30 | |||
31 | def get_git_hooks_path(repo_path, bare): |
|
31 | def get_git_hooks_path(repo_path, bare): | |
32 | hooks_path = os.path.join(repo_path, 'hooks') |
|
32 | hooks_path = os.path.join(repo_path, 'hooks') | |
33 | if not bare: |
|
33 | if not bare: | |
34 | hooks_path = os.path.join(repo_path, '.git', 'hooks') |
|
34 | hooks_path = os.path.join(repo_path, '.git', 'hooks') | |
35 |
|
35 | |||
36 | return hooks_path |
|
36 | return hooks_path | |
37 |
|
37 | |||
38 |
|
38 | |||
39 | def install_git_hooks(repo_path, bare, executable=None, force_create=False): |
|
39 | def install_git_hooks(repo_path, bare, executable=None, force_create=False): | |
40 | """ |
|
40 | """ | |
41 | Creates a RhodeCode hook inside a git repository |
|
41 | Creates a RhodeCode hook inside a git repository | |
42 |
|
42 | |||
43 | :param repo_path: path to repository |
|
43 | :param repo_path: path to repository | |
44 | :param executable: binary executable to put in the hooks |
|
44 | :param executable: binary executable to put in the hooks | |
45 | :param force_create: Create even if same name hook exists |
|
45 | :param force_create: Create even if same name hook exists | |
46 | """ |
|
46 | """ | |
47 | executable = executable or sys.executable |
|
47 | executable = executable or sys.executable | |
48 | hooks_path = get_git_hooks_path(repo_path, bare) |
|
48 | hooks_path = get_git_hooks_path(repo_path, bare) | |
49 |
|
49 | |||
50 | if not os.path.isdir(hooks_path): |
|
50 | if not os.path.isdir(hooks_path): | |
51 | os.makedirs(hooks_path, mode=0o777, exist_ok=True) |
|
51 | os.makedirs(hooks_path, mode=0o777, exist_ok=True) | |
52 |
|
52 | |||
53 | tmpl_post = pkg_resources.resource_string( |
|
53 | tmpl_post = pkg_resources.resource_string( | |
54 | 'vcsserver', '/'.join( |
|
54 | 'vcsserver', '/'.join( | |
55 | ('hook_utils', 'hook_templates', 'git_post_receive.py.tmpl'))) |
|
55 | ('hook_utils', 'hook_templates', 'git_post_receive.py.tmpl'))) | |
56 | tmpl_pre = pkg_resources.resource_string( |
|
56 | tmpl_pre = pkg_resources.resource_string( | |
57 | 'vcsserver', '/'.join( |
|
57 | 'vcsserver', '/'.join( | |
58 | ('hook_utils', 'hook_templates', 'git_pre_receive.py.tmpl'))) |
|
58 | ('hook_utils', 'hook_templates', 'git_pre_receive.py.tmpl'))) | |
59 |
|
59 | |||
60 | path = '' # not used for now |
|
60 | path = '' # not used for now | |
61 | timestamp = datetime.datetime.utcnow().isoformat() |
|
61 | timestamp = datetime.datetime.utcnow().isoformat() | |
62 |
|
62 | |||
63 | for h_type, template in [('pre', tmpl_pre), ('post', tmpl_post)]: |
|
63 | for h_type, template in [('pre', tmpl_pre), ('post', tmpl_post)]: | |
64 | log.debug('Installing git hook in repo %s', repo_path) |
|
64 | log.debug('Installing git hook in repo %s', repo_path) | |
65 |
_hook_file = os.path.join(hooks_path, ' |
|
65 | _hook_file = os.path.join(hooks_path, f'{h_type}-receive') | |
66 | _rhodecode_hook = check_rhodecode_hook(_hook_file) |
|
66 | _rhodecode_hook = check_rhodecode_hook(_hook_file) | |
67 |
|
67 | |||
68 | if _rhodecode_hook or force_create: |
|
68 | if _rhodecode_hook or force_create: | |
69 | log.debug('writing git %s hook file at %s !', h_type, _hook_file) |
|
69 | log.debug('writing git %s hook file at %s !', h_type, _hook_file) | |
70 | try: |
|
70 | try: | |
71 | with open(_hook_file, 'wb') as f: |
|
71 | with open(_hook_file, 'wb') as f: | |
72 | template = template.replace(b'_TMPL_', safe_bytes(vcsserver.__version__)) |
|
72 | template = template.replace(b'_TMPL_', safe_bytes(vcsserver.__version__)) | |
73 | template = template.replace(b'_DATE_', safe_bytes(timestamp)) |
|
73 | template = template.replace(b'_DATE_', safe_bytes(timestamp)) | |
74 | template = template.replace(b'_ENV_', safe_bytes(executable)) |
|
74 | template = template.replace(b'_ENV_', safe_bytes(executable)) | |
75 | template = template.replace(b'_PATH_', safe_bytes(path)) |
|
75 | template = template.replace(b'_PATH_', safe_bytes(path)) | |
76 | f.write(template) |
|
76 | f.write(template) | |
77 | os.chmod(_hook_file, 0o755) |
|
77 | os.chmod(_hook_file, 0o755) | |
78 | except OSError: |
|
78 | except OSError: | |
79 | log.exception('error writing hook file %s', _hook_file) |
|
79 | log.exception('error writing hook file %s', _hook_file) | |
80 | else: |
|
80 | else: | |
81 | log.debug('skipping writing hook file') |
|
81 | log.debug('skipping writing hook file') | |
82 |
|
82 | |||
83 | return True |
|
83 | return True | |
84 |
|
84 | |||
85 |
|
85 | |||
86 | def get_svn_hooks_path(repo_path): |
|
86 | def get_svn_hooks_path(repo_path): | |
87 | hooks_path = os.path.join(repo_path, 'hooks') |
|
87 | hooks_path = os.path.join(repo_path, 'hooks') | |
88 |
|
88 | |||
89 | return hooks_path |
|
89 | return hooks_path | |
90 |
|
90 | |||
91 |
|
91 | |||
92 | def install_svn_hooks(repo_path, executable=None, force_create=False): |
|
92 | def install_svn_hooks(repo_path, executable=None, force_create=False): | |
93 | """ |
|
93 | """ | |
94 | Creates RhodeCode hooks inside a svn repository |
|
94 | Creates RhodeCode hooks inside a svn repository | |
95 |
|
95 | |||
96 | :param repo_path: path to repository |
|
96 | :param repo_path: path to repository | |
97 | :param executable: binary executable to put in the hooks |
|
97 | :param executable: binary executable to put in the hooks | |
98 | :param force_create: Create even if same name hook exists |
|
98 | :param force_create: Create even if same name hook exists | |
99 | """ |
|
99 | """ | |
100 | executable = executable or sys.executable |
|
100 | executable = executable or sys.executable | |
101 | hooks_path = get_svn_hooks_path(repo_path) |
|
101 | hooks_path = get_svn_hooks_path(repo_path) | |
102 | if not os.path.isdir(hooks_path): |
|
102 | if not os.path.isdir(hooks_path): | |
103 | os.makedirs(hooks_path, mode=0o777, exist_ok=True) |
|
103 | os.makedirs(hooks_path, mode=0o777, exist_ok=True) | |
104 |
|
104 | |||
105 | tmpl_post = pkg_resources.resource_string( |
|
105 | tmpl_post = pkg_resources.resource_string( | |
106 | 'vcsserver', '/'.join( |
|
106 | 'vcsserver', '/'.join( | |
107 | ('hook_utils', 'hook_templates', 'svn_post_commit_hook.py.tmpl'))) |
|
107 | ('hook_utils', 'hook_templates', 'svn_post_commit_hook.py.tmpl'))) | |
108 | tmpl_pre = pkg_resources.resource_string( |
|
108 | tmpl_pre = pkg_resources.resource_string( | |
109 | 'vcsserver', '/'.join( |
|
109 | 'vcsserver', '/'.join( | |
110 | ('hook_utils', 'hook_templates', 'svn_pre_commit_hook.py.tmpl'))) |
|
110 | ('hook_utils', 'hook_templates', 'svn_pre_commit_hook.py.tmpl'))) | |
111 |
|
111 | |||
112 | path = '' # not used for now |
|
112 | path = '' # not used for now | |
113 | timestamp = datetime.datetime.utcnow().isoformat() |
|
113 | timestamp = datetime.datetime.utcnow().isoformat() | |
114 |
|
114 | |||
115 | for h_type, template in [('pre', tmpl_pre), ('post', tmpl_post)]: |
|
115 | for h_type, template in [('pre', tmpl_pre), ('post', tmpl_post)]: | |
116 | log.debug('Installing svn hook in repo %s', repo_path) |
|
116 | log.debug('Installing svn hook in repo %s', repo_path) | |
117 |
_hook_file = os.path.join(hooks_path, ' |
|
117 | _hook_file = os.path.join(hooks_path, f'{h_type}-commit') | |
118 | _rhodecode_hook = check_rhodecode_hook(_hook_file) |
|
118 | _rhodecode_hook = check_rhodecode_hook(_hook_file) | |
119 |
|
119 | |||
120 | if _rhodecode_hook or force_create: |
|
120 | if _rhodecode_hook or force_create: | |
121 | log.debug('writing svn %s hook file at %s !', h_type, _hook_file) |
|
121 | log.debug('writing svn %s hook file at %s !', h_type, _hook_file) | |
122 |
|
122 | |||
123 | try: |
|
123 | try: | |
124 | with open(_hook_file, 'wb') as f: |
|
124 | with open(_hook_file, 'wb') as f: | |
125 | template = template.replace(b'_TMPL_', safe_bytes(vcsserver.__version__)) |
|
125 | template = template.replace(b'_TMPL_', safe_bytes(vcsserver.__version__)) | |
126 | template = template.replace(b'_DATE_', safe_bytes(timestamp)) |
|
126 | template = template.replace(b'_DATE_', safe_bytes(timestamp)) | |
127 | template = template.replace(b'_ENV_', safe_bytes(executable)) |
|
127 | template = template.replace(b'_ENV_', safe_bytes(executable)) | |
128 | template = template.replace(b'_PATH_', safe_bytes(path)) |
|
128 | template = template.replace(b'_PATH_', safe_bytes(path)) | |
129 |
|
129 | |||
130 | f.write(template) |
|
130 | f.write(template) | |
131 | os.chmod(_hook_file, 0o755) |
|
131 | os.chmod(_hook_file, 0o755) | |
132 | except OSError: |
|
132 | except OSError: | |
133 | log.exception('error writing hook file %s', _hook_file) |
|
133 | log.exception('error writing hook file %s', _hook_file) | |
134 | else: |
|
134 | else: | |
135 | log.debug('skipping writing hook file') |
|
135 | log.debug('skipping writing hook file') | |
136 |
|
136 | |||
137 | return True |
|
137 | return True | |
138 |
|
138 | |||
139 |
|
139 | |||
140 | def get_version_from_hook(hook_path): |
|
140 | def get_version_from_hook(hook_path): | |
141 | version = b'' |
|
141 | version = b'' | |
142 | hook_content = read_hook_content(hook_path) |
|
142 | hook_content = read_hook_content(hook_path) | |
143 | matches = re.search(rb'RC_HOOK_VER\s*=\s*(.*)', hook_content) |
|
143 | matches = re.search(rb'RC_HOOK_VER\s*=\s*(.*)', hook_content) | |
144 | if matches: |
|
144 | if matches: | |
145 | try: |
|
145 | try: | |
146 | version = matches.groups()[0] |
|
146 | version = matches.groups()[0] | |
147 | log.debug('got version %s from hooks.', version) |
|
147 | log.debug('got version %s from hooks.', version) | |
148 | except Exception: |
|
148 | except Exception: | |
149 | log.exception("Exception while reading the hook version.") |
|
149 | log.exception("Exception while reading the hook version.") | |
150 | return version.replace(b"'", b"") |
|
150 | return version.replace(b"'", b"") | |
151 |
|
151 | |||
152 |
|
152 | |||
153 | def check_rhodecode_hook(hook_path): |
|
153 | def check_rhodecode_hook(hook_path): | |
154 | """ |
|
154 | """ | |
155 | Check if the hook was created by RhodeCode |
|
155 | Check if the hook was created by RhodeCode | |
156 | """ |
|
156 | """ | |
157 | if not os.path.exists(hook_path): |
|
157 | if not os.path.exists(hook_path): | |
158 | return True |
|
158 | return True | |
159 |
|
159 | |||
160 | log.debug('hook exists, checking if it is from RhodeCode') |
|
160 | log.debug('hook exists, checking if it is from RhodeCode') | |
161 |
|
161 | |||
162 | version = get_version_from_hook(hook_path) |
|
162 | version = get_version_from_hook(hook_path) | |
163 | if version: |
|
163 | if version: | |
164 | return True |
|
164 | return True | |
165 |
|
165 | |||
166 | return False |
|
166 | return False | |
167 |
|
167 | |||
168 |
|
168 | |||
169 | def read_hook_content(hook_path) -> bytes: |
|
169 | def read_hook_content(hook_path) -> bytes: | |
170 | content = b'' |
|
170 | content = b'' | |
171 | if os.path.isfile(hook_path): |
|
171 | if os.path.isfile(hook_path): | |
172 | with open(hook_path, 'rb') as f: |
|
172 | with open(hook_path, 'rb') as f: | |
173 | content = f.read() |
|
173 | content = f.read() | |
174 | return content |
|
174 | return content | |
175 |
|
175 | |||
176 |
|
176 | |||
177 | def get_git_pre_hook_version(repo_path, bare): |
|
177 | def get_git_pre_hook_version(repo_path, bare): | |
178 | hooks_path = get_git_hooks_path(repo_path, bare) |
|
178 | hooks_path = get_git_hooks_path(repo_path, bare) | |
179 | _hook_file = os.path.join(hooks_path, 'pre-receive') |
|
179 | _hook_file = os.path.join(hooks_path, 'pre-receive') | |
180 | version = get_version_from_hook(_hook_file) |
|
180 | version = get_version_from_hook(_hook_file) | |
181 | return version |
|
181 | return version | |
182 |
|
182 | |||
183 |
|
183 | |||
184 | def get_git_post_hook_version(repo_path, bare): |
|
184 | def get_git_post_hook_version(repo_path, bare): | |
185 | hooks_path = get_git_hooks_path(repo_path, bare) |
|
185 | hooks_path = get_git_hooks_path(repo_path, bare) | |
186 | _hook_file = os.path.join(hooks_path, 'post-receive') |
|
186 | _hook_file = os.path.join(hooks_path, 'post-receive') | |
187 | version = get_version_from_hook(_hook_file) |
|
187 | version = get_version_from_hook(_hook_file) | |
188 | return version |
|
188 | return version | |
189 |
|
189 | |||
190 |
|
190 | |||
191 | def get_svn_pre_hook_version(repo_path): |
|
191 | def get_svn_pre_hook_version(repo_path): | |
192 | hooks_path = get_svn_hooks_path(repo_path) |
|
192 | hooks_path = get_svn_hooks_path(repo_path) | |
193 | _hook_file = os.path.join(hooks_path, 'pre-commit') |
|
193 | _hook_file = os.path.join(hooks_path, 'pre-commit') | |
194 | version = get_version_from_hook(_hook_file) |
|
194 | version = get_version_from_hook(_hook_file) | |
195 | return version |
|
195 | return version | |
196 |
|
196 | |||
197 |
|
197 | |||
198 | def get_svn_post_hook_version(repo_path): |
|
198 | def get_svn_post_hook_version(repo_path): | |
199 | hooks_path = get_svn_hooks_path(repo_path) |
|
199 | hooks_path = get_svn_hooks_path(repo_path) | |
200 | _hook_file = os.path.join(hooks_path, 'post-commit') |
|
200 | _hook_file = os.path.join(hooks_path, 'post-commit') | |
201 | version = get_version_from_hook(_hook_file) |
|
201 | version = get_version_from_hook(_hook_file) | |
202 | return version |
|
202 | return version |
@@ -1,779 +1,779 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import io |
|
18 | import io | |
19 | import os |
|
19 | import os | |
20 | import sys |
|
20 | import sys | |
21 | import logging |
|
21 | import logging | |
22 | import collections |
|
22 | import collections | |
23 | import importlib |
|
23 | import importlib | |
24 | import base64 |
|
24 | import base64 | |
25 | import msgpack |
|
25 | import msgpack | |
26 | import dataclasses |
|
26 | import dataclasses | |
27 | import pygit2 |
|
27 | import pygit2 | |
28 |
|
28 | |||
29 | import http.client |
|
29 | import http.client | |
30 |
|
30 | |||
31 |
|
31 | |||
32 | import mercurial.scmutil |
|
32 | import mercurial.scmutil | |
33 | import mercurial.node |
|
33 | import mercurial.node | |
34 |
|
34 | |||
35 | from vcsserver.lib.rc_json import json |
|
35 | from vcsserver.lib.rc_json import json | |
36 | from vcsserver import exceptions, subprocessio, settings |
|
36 | from vcsserver import exceptions, subprocessio, settings | |
37 | from vcsserver.str_utils import ascii_str, safe_str |
|
37 | from vcsserver.str_utils import ascii_str, safe_str | |
38 | from vcsserver.remote.git_remote import Repository |
|
38 | from vcsserver.remote.git_remote import Repository | |
39 |
|
39 | |||
40 | log = logging.getLogger(__name__) |
|
40 | log = logging.getLogger(__name__) | |
41 |
|
41 | |||
42 |
|
42 | |||
43 |
class HooksHttpClient |
|
43 | class HooksHttpClient: | |
44 | proto = 'msgpack.v1' |
|
44 | proto = 'msgpack.v1' | |
45 | connection = None |
|
45 | connection = None | |
46 |
|
46 | |||
47 | def __init__(self, hooks_uri): |
|
47 | def __init__(self, hooks_uri): | |
48 | self.hooks_uri = hooks_uri |
|
48 | self.hooks_uri = hooks_uri | |
49 |
|
49 | |||
50 | def __repr__(self): |
|
50 | def __repr__(self): | |
51 | return f'{self.__class__}(hook_uri={self.hooks_uri}, proto={self.proto})' |
|
51 | return f'{self.__class__}(hook_uri={self.hooks_uri}, proto={self.proto})' | |
52 |
|
52 | |||
53 | def __call__(self, method, extras): |
|
53 | def __call__(self, method, extras): | |
54 | connection = http.client.HTTPConnection(self.hooks_uri) |
|
54 | connection = http.client.HTTPConnection(self.hooks_uri) | |
55 | # binary msgpack body |
|
55 | # binary msgpack body | |
56 | headers, body = self._serialize(method, extras) |
|
56 | headers, body = self._serialize(method, extras) | |
57 | log.debug('Doing a new hooks call using HTTPConnection to %s', self.hooks_uri) |
|
57 | log.debug('Doing a new hooks call using HTTPConnection to %s', self.hooks_uri) | |
58 |
|
58 | |||
59 | try: |
|
59 | try: | |
60 | try: |
|
60 | try: | |
61 | connection.request('POST', '/', body, headers) |
|
61 | connection.request('POST', '/', body, headers) | |
62 | except Exception as error: |
|
62 | except Exception as error: | |
63 | log.error('Hooks calling Connection failed on %s, org error: %s', connection.__dict__, error) |
|
63 | log.error('Hooks calling Connection failed on %s, org error: %s', connection.__dict__, error) | |
64 | raise |
|
64 | raise | |
65 |
|
65 | |||
66 | response = connection.getresponse() |
|
66 | response = connection.getresponse() | |
67 | try: |
|
67 | try: | |
68 | return msgpack.load(response) |
|
68 | return msgpack.load(response) | |
69 | except Exception: |
|
69 | except Exception: | |
70 | response_data = response.read() |
|
70 | response_data = response.read() | |
71 | log.exception('Failed to decode hook response json data. ' |
|
71 | log.exception('Failed to decode hook response json data. ' | |
72 | 'response_code:%s, raw_data:%s', |
|
72 | 'response_code:%s, raw_data:%s', | |
73 | response.status, response_data) |
|
73 | response.status, response_data) | |
74 | raise |
|
74 | raise | |
75 | finally: |
|
75 | finally: | |
76 | connection.close() |
|
76 | connection.close() | |
77 |
|
77 | |||
78 | @classmethod |
|
78 | @classmethod | |
79 | def _serialize(cls, hook_name, extras): |
|
79 | def _serialize(cls, hook_name, extras): | |
80 | data = { |
|
80 | data = { | |
81 | 'method': hook_name, |
|
81 | 'method': hook_name, | |
82 | 'extras': extras |
|
82 | 'extras': extras | |
83 | } |
|
83 | } | |
84 | headers = { |
|
84 | headers = { | |
85 | "rc-hooks-protocol": cls.proto, |
|
85 | "rc-hooks-protocol": cls.proto, | |
86 | "Connection": "keep-alive" |
|
86 | "Connection": "keep-alive" | |
87 | } |
|
87 | } | |
88 | return headers, msgpack.packb(data) |
|
88 | return headers, msgpack.packb(data) | |
89 |
|
89 | |||
90 |
|
90 | |||
91 |
class HooksDummyClient |
|
91 | class HooksDummyClient: | |
92 | def __init__(self, hooks_module): |
|
92 | def __init__(self, hooks_module): | |
93 | self._hooks_module = importlib.import_module(hooks_module) |
|
93 | self._hooks_module = importlib.import_module(hooks_module) | |
94 |
|
94 | |||
95 | def __call__(self, hook_name, extras): |
|
95 | def __call__(self, hook_name, extras): | |
96 | with self._hooks_module.Hooks() as hooks: |
|
96 | with self._hooks_module.Hooks() as hooks: | |
97 | return getattr(hooks, hook_name)(extras) |
|
97 | return getattr(hooks, hook_name)(extras) | |
98 |
|
98 | |||
99 |
|
99 | |||
100 |
class HooksShadowRepoClient |
|
100 | class HooksShadowRepoClient: | |
101 |
|
101 | |||
102 | def __call__(self, hook_name, extras): |
|
102 | def __call__(self, hook_name, extras): | |
103 | return {'output': '', 'status': 0} |
|
103 | return {'output': '', 'status': 0} | |
104 |
|
104 | |||
105 |
|
105 | |||
106 |
class RemoteMessageWriter |
|
106 | class RemoteMessageWriter: | |
107 | """Writer base class.""" |
|
107 | """Writer base class.""" | |
108 | def write(self, message): |
|
108 | def write(self, message): | |
109 | raise NotImplementedError() |
|
109 | raise NotImplementedError() | |
110 |
|
110 | |||
111 |
|
111 | |||
112 | class HgMessageWriter(RemoteMessageWriter): |
|
112 | class HgMessageWriter(RemoteMessageWriter): | |
113 | """Writer that knows how to send messages to mercurial clients.""" |
|
113 | """Writer that knows how to send messages to mercurial clients.""" | |
114 |
|
114 | |||
115 | def __init__(self, ui): |
|
115 | def __init__(self, ui): | |
116 | self.ui = ui |
|
116 | self.ui = ui | |
117 |
|
117 | |||
118 | def write(self, message: str): |
|
118 | def write(self, message: str): | |
119 | # TODO: Check why the quiet flag is set by default. |
|
119 | # TODO: Check why the quiet flag is set by default. | |
120 | old = self.ui.quiet |
|
120 | old = self.ui.quiet | |
121 | self.ui.quiet = False |
|
121 | self.ui.quiet = False | |
122 | self.ui.status(message.encode('utf-8')) |
|
122 | self.ui.status(message.encode('utf-8')) | |
123 | self.ui.quiet = old |
|
123 | self.ui.quiet = old | |
124 |
|
124 | |||
125 |
|
125 | |||
126 | class GitMessageWriter(RemoteMessageWriter): |
|
126 | class GitMessageWriter(RemoteMessageWriter): | |
127 | """Writer that knows how to send messages to git clients.""" |
|
127 | """Writer that knows how to send messages to git clients.""" | |
128 |
|
128 | |||
129 | def __init__(self, stdout=None): |
|
129 | def __init__(self, stdout=None): | |
130 | self.stdout = stdout or sys.stdout |
|
130 | self.stdout = stdout or sys.stdout | |
131 |
|
131 | |||
132 | def write(self, message: str): |
|
132 | def write(self, message: str): | |
133 | self.stdout.write(message) |
|
133 | self.stdout.write(message) | |
134 |
|
134 | |||
135 |
|
135 | |||
136 | class SvnMessageWriter(RemoteMessageWriter): |
|
136 | class SvnMessageWriter(RemoteMessageWriter): | |
137 | """Writer that knows how to send messages to svn clients.""" |
|
137 | """Writer that knows how to send messages to svn clients.""" | |
138 |
|
138 | |||
139 | def __init__(self, stderr=None): |
|
139 | def __init__(self, stderr=None): | |
140 | # SVN needs data sent to stderr for back-to-client messaging |
|
140 | # SVN needs data sent to stderr for back-to-client messaging | |
141 | self.stderr = stderr or sys.stderr |
|
141 | self.stderr = stderr or sys.stderr | |
142 |
|
142 | |||
143 | def write(self, message): |
|
143 | def write(self, message): | |
144 | self.stderr.write(message.encode('utf-8')) |
|
144 | self.stderr.write(message.encode('utf-8')) | |
145 |
|
145 | |||
146 |
|
146 | |||
147 | def _handle_exception(result): |
|
147 | def _handle_exception(result): | |
148 | exception_class = result.get('exception') |
|
148 | exception_class = result.get('exception') | |
149 | exception_traceback = result.get('exception_traceback') |
|
149 | exception_traceback = result.get('exception_traceback') | |
150 | log.debug('Handling hook-call exception: %s', exception_class) |
|
150 | log.debug('Handling hook-call exception: %s', exception_class) | |
151 |
|
151 | |||
152 | if exception_traceback: |
|
152 | if exception_traceback: | |
153 | log.error('Got traceback from remote call:%s', exception_traceback) |
|
153 | log.error('Got traceback from remote call:%s', exception_traceback) | |
154 |
|
154 | |||
155 | if exception_class == 'HTTPLockedRC': |
|
155 | if exception_class == 'HTTPLockedRC': | |
156 | raise exceptions.RepositoryLockedException()(*result['exception_args']) |
|
156 | raise exceptions.RepositoryLockedException()(*result['exception_args']) | |
157 | elif exception_class == 'HTTPBranchProtected': |
|
157 | elif exception_class == 'HTTPBranchProtected': | |
158 | raise exceptions.RepositoryBranchProtectedException()(*result['exception_args']) |
|
158 | raise exceptions.RepositoryBranchProtectedException()(*result['exception_args']) | |
159 | elif exception_class == 'RepositoryError': |
|
159 | elif exception_class == 'RepositoryError': | |
160 | raise exceptions.VcsException()(*result['exception_args']) |
|
160 | raise exceptions.VcsException()(*result['exception_args']) | |
161 | elif exception_class: |
|
161 | elif exception_class: | |
162 | raise Exception( |
|
162 | raise Exception( | |
163 | f"""Got remote exception "{exception_class}" with args "{result['exception_args']}" """ |
|
163 | f"""Got remote exception "{exception_class}" with args "{result['exception_args']}" """ | |
164 | ) |
|
164 | ) | |
165 |
|
165 | |||
166 |
|
166 | |||
167 | def _get_hooks_client(extras): |
|
167 | def _get_hooks_client(extras): | |
168 | hooks_uri = extras.get('hooks_uri') |
|
168 | hooks_uri = extras.get('hooks_uri') | |
169 | is_shadow_repo = extras.get('is_shadow_repo') |
|
169 | is_shadow_repo = extras.get('is_shadow_repo') | |
170 |
|
170 | |||
171 | if hooks_uri: |
|
171 | if hooks_uri: | |
172 | return HooksHttpClient(extras['hooks_uri']) |
|
172 | return HooksHttpClient(extras['hooks_uri']) | |
173 | elif is_shadow_repo: |
|
173 | elif is_shadow_repo: | |
174 | return HooksShadowRepoClient() |
|
174 | return HooksShadowRepoClient() | |
175 | else: |
|
175 | else: | |
176 | return HooksDummyClient(extras['hooks_module']) |
|
176 | return HooksDummyClient(extras['hooks_module']) | |
177 |
|
177 | |||
178 |
|
178 | |||
179 | def _call_hook(hook_name, extras, writer): |
|
179 | def _call_hook(hook_name, extras, writer): | |
180 | hooks_client = _get_hooks_client(extras) |
|
180 | hooks_client = _get_hooks_client(extras) | |
181 | log.debug('Hooks, using client:%s', hooks_client) |
|
181 | log.debug('Hooks, using client:%s', hooks_client) | |
182 | result = hooks_client(hook_name, extras) |
|
182 | result = hooks_client(hook_name, extras) | |
183 | log.debug('Hooks got result: %s', result) |
|
183 | log.debug('Hooks got result: %s', result) | |
184 | _handle_exception(result) |
|
184 | _handle_exception(result) | |
185 | writer.write(result['output']) |
|
185 | writer.write(result['output']) | |
186 |
|
186 | |||
187 | return result['status'] |
|
187 | return result['status'] | |
188 |
|
188 | |||
189 |
|
189 | |||
190 | def _extras_from_ui(ui): |
|
190 | def _extras_from_ui(ui): | |
191 | hook_data = ui.config(b'rhodecode', b'RC_SCM_DATA') |
|
191 | hook_data = ui.config(b'rhodecode', b'RC_SCM_DATA') | |
192 | if not hook_data: |
|
192 | if not hook_data: | |
193 | # maybe it's inside environ ? |
|
193 | # maybe it's inside environ ? | |
194 | env_hook_data = os.environ.get('RC_SCM_DATA') |
|
194 | env_hook_data = os.environ.get('RC_SCM_DATA') | |
195 | if env_hook_data: |
|
195 | if env_hook_data: | |
196 | hook_data = env_hook_data |
|
196 | hook_data = env_hook_data | |
197 |
|
197 | |||
198 | extras = {} |
|
198 | extras = {} | |
199 | if hook_data: |
|
199 | if hook_data: | |
200 | extras = json.loads(hook_data) |
|
200 | extras = json.loads(hook_data) | |
201 | return extras |
|
201 | return extras | |
202 |
|
202 | |||
203 |
|
203 | |||
204 | def _rev_range_hash(repo, node, check_heads=False): |
|
204 | def _rev_range_hash(repo, node, check_heads=False): | |
205 | from vcsserver.hgcompat import get_ctx |
|
205 | from vcsserver.hgcompat import get_ctx | |
206 |
|
206 | |||
207 | commits = [] |
|
207 | commits = [] | |
208 | revs = [] |
|
208 | revs = [] | |
209 | start = get_ctx(repo, node).rev() |
|
209 | start = get_ctx(repo, node).rev() | |
210 | end = len(repo) |
|
210 | end = len(repo) | |
211 | for rev in range(start, end): |
|
211 | for rev in range(start, end): | |
212 | revs.append(rev) |
|
212 | revs.append(rev) | |
213 | ctx = get_ctx(repo, rev) |
|
213 | ctx = get_ctx(repo, rev) | |
214 | commit_id = ascii_str(mercurial.node.hex(ctx.node())) |
|
214 | commit_id = ascii_str(mercurial.node.hex(ctx.node())) | |
215 | branch = safe_str(ctx.branch()) |
|
215 | branch = safe_str(ctx.branch()) | |
216 | commits.append((commit_id, branch)) |
|
216 | commits.append((commit_id, branch)) | |
217 |
|
217 | |||
218 | parent_heads = [] |
|
218 | parent_heads = [] | |
219 | if check_heads: |
|
219 | if check_heads: | |
220 | parent_heads = _check_heads(repo, start, end, revs) |
|
220 | parent_heads = _check_heads(repo, start, end, revs) | |
221 | return commits, parent_heads |
|
221 | return commits, parent_heads | |
222 |
|
222 | |||
223 |
|
223 | |||
224 | def _check_heads(repo, start, end, commits): |
|
224 | def _check_heads(repo, start, end, commits): | |
225 | from vcsserver.hgcompat import get_ctx |
|
225 | from vcsserver.hgcompat import get_ctx | |
226 | changelog = repo.changelog |
|
226 | changelog = repo.changelog | |
227 | parents = set() |
|
227 | parents = set() | |
228 |
|
228 | |||
229 | for new_rev in commits: |
|
229 | for new_rev in commits: | |
230 | for p in changelog.parentrevs(new_rev): |
|
230 | for p in changelog.parentrevs(new_rev): | |
231 | if p == mercurial.node.nullrev: |
|
231 | if p == mercurial.node.nullrev: | |
232 | continue |
|
232 | continue | |
233 | if p < start: |
|
233 | if p < start: | |
234 | parents.add(p) |
|
234 | parents.add(p) | |
235 |
|
235 | |||
236 | for p in parents: |
|
236 | for p in parents: | |
237 | branch = get_ctx(repo, p).branch() |
|
237 | branch = get_ctx(repo, p).branch() | |
238 | # The heads descending from that parent, on the same branch |
|
238 | # The heads descending from that parent, on the same branch | |
239 | parent_heads = {p} |
|
239 | parent_heads = {p} | |
240 | reachable = {p} |
|
240 | reachable = {p} | |
241 | for x in range(p + 1, end): |
|
241 | for x in range(p + 1, end): | |
242 | if get_ctx(repo, x).branch() != branch: |
|
242 | if get_ctx(repo, x).branch() != branch: | |
243 | continue |
|
243 | continue | |
244 | for pp in changelog.parentrevs(x): |
|
244 | for pp in changelog.parentrevs(x): | |
245 | if pp in reachable: |
|
245 | if pp in reachable: | |
246 | reachable.add(x) |
|
246 | reachable.add(x) | |
247 | parent_heads.discard(pp) |
|
247 | parent_heads.discard(pp) | |
248 | parent_heads.add(x) |
|
248 | parent_heads.add(x) | |
249 | # More than one head? Suggest merging |
|
249 | # More than one head? Suggest merging | |
250 | if len(parent_heads) > 1: |
|
250 | if len(parent_heads) > 1: | |
251 | return list(parent_heads) |
|
251 | return list(parent_heads) | |
252 |
|
252 | |||
253 | return [] |
|
253 | return [] | |
254 |
|
254 | |||
255 |
|
255 | |||
256 | def _get_git_env(): |
|
256 | def _get_git_env(): | |
257 | env = {} |
|
257 | env = {} | |
258 | for k, v in os.environ.items(): |
|
258 | for k, v in os.environ.items(): | |
259 | if k.startswith('GIT'): |
|
259 | if k.startswith('GIT'): | |
260 | env[k] = v |
|
260 | env[k] = v | |
261 |
|
261 | |||
262 | # serialized version |
|
262 | # serialized version | |
263 | return [(k, v) for k, v in env.items()] |
|
263 | return [(k, v) for k, v in env.items()] | |
264 |
|
264 | |||
265 |
|
265 | |||
266 | def _get_hg_env(old_rev, new_rev, txnid, repo_path): |
|
266 | def _get_hg_env(old_rev, new_rev, txnid, repo_path): | |
267 | env = {} |
|
267 | env = {} | |
268 | for k, v in os.environ.items(): |
|
268 | for k, v in os.environ.items(): | |
269 | if k.startswith('HG'): |
|
269 | if k.startswith('HG'): | |
270 | env[k] = v |
|
270 | env[k] = v | |
271 |
|
271 | |||
272 | env['HG_NODE'] = old_rev |
|
272 | env['HG_NODE'] = old_rev | |
273 | env['HG_NODE_LAST'] = new_rev |
|
273 | env['HG_NODE_LAST'] = new_rev | |
274 | env['HG_TXNID'] = txnid |
|
274 | env['HG_TXNID'] = txnid | |
275 | env['HG_PENDING'] = repo_path |
|
275 | env['HG_PENDING'] = repo_path | |
276 |
|
276 | |||
277 | return [(k, v) for k, v in env.items()] |
|
277 | return [(k, v) for k, v in env.items()] | |
278 |
|
278 | |||
279 |
|
279 | |||
280 | def repo_size(ui, repo, **kwargs): |
|
280 | def repo_size(ui, repo, **kwargs): | |
281 | extras = _extras_from_ui(ui) |
|
281 | extras = _extras_from_ui(ui) | |
282 | return _call_hook('repo_size', extras, HgMessageWriter(ui)) |
|
282 | return _call_hook('repo_size', extras, HgMessageWriter(ui)) | |
283 |
|
283 | |||
284 |
|
284 | |||
285 | def pre_pull(ui, repo, **kwargs): |
|
285 | def pre_pull(ui, repo, **kwargs): | |
286 | extras = _extras_from_ui(ui) |
|
286 | extras = _extras_from_ui(ui) | |
287 | return _call_hook('pre_pull', extras, HgMessageWriter(ui)) |
|
287 | return _call_hook('pre_pull', extras, HgMessageWriter(ui)) | |
288 |
|
288 | |||
289 |
|
289 | |||
290 | def pre_pull_ssh(ui, repo, **kwargs): |
|
290 | def pre_pull_ssh(ui, repo, **kwargs): | |
291 | extras = _extras_from_ui(ui) |
|
291 | extras = _extras_from_ui(ui) | |
292 | if extras and extras.get('SSH'): |
|
292 | if extras and extras.get('SSH'): | |
293 | return pre_pull(ui, repo, **kwargs) |
|
293 | return pre_pull(ui, repo, **kwargs) | |
294 | return 0 |
|
294 | return 0 | |
295 |
|
295 | |||
296 |
|
296 | |||
297 | def post_pull(ui, repo, **kwargs): |
|
297 | def post_pull(ui, repo, **kwargs): | |
298 | extras = _extras_from_ui(ui) |
|
298 | extras = _extras_from_ui(ui) | |
299 | return _call_hook('post_pull', extras, HgMessageWriter(ui)) |
|
299 | return _call_hook('post_pull', extras, HgMessageWriter(ui)) | |
300 |
|
300 | |||
301 |
|
301 | |||
302 | def post_pull_ssh(ui, repo, **kwargs): |
|
302 | def post_pull_ssh(ui, repo, **kwargs): | |
303 | extras = _extras_from_ui(ui) |
|
303 | extras = _extras_from_ui(ui) | |
304 | if extras and extras.get('SSH'): |
|
304 | if extras and extras.get('SSH'): | |
305 | return post_pull(ui, repo, **kwargs) |
|
305 | return post_pull(ui, repo, **kwargs) | |
306 | return 0 |
|
306 | return 0 | |
307 |
|
307 | |||
308 |
|
308 | |||
309 | def pre_push(ui, repo, node=None, **kwargs): |
|
309 | def pre_push(ui, repo, node=None, **kwargs): | |
310 | """ |
|
310 | """ | |
311 | Mercurial pre_push hook |
|
311 | Mercurial pre_push hook | |
312 | """ |
|
312 | """ | |
313 | extras = _extras_from_ui(ui) |
|
313 | extras = _extras_from_ui(ui) | |
314 | detect_force_push = extras.get('detect_force_push') |
|
314 | detect_force_push = extras.get('detect_force_push') | |
315 |
|
315 | |||
316 | rev_data = [] |
|
316 | rev_data = [] | |
317 | hook_type: str = safe_str(kwargs.get('hooktype')) |
|
317 | hook_type: str = safe_str(kwargs.get('hooktype')) | |
318 |
|
318 | |||
319 | if node and hook_type == 'pretxnchangegroup': |
|
319 | if node and hook_type == 'pretxnchangegroup': | |
320 | branches = collections.defaultdict(list) |
|
320 | branches = collections.defaultdict(list) | |
321 | commits, _heads = _rev_range_hash(repo, node, check_heads=detect_force_push) |
|
321 | commits, _heads = _rev_range_hash(repo, node, check_heads=detect_force_push) | |
322 | for commit_id, branch in commits: |
|
322 | for commit_id, branch in commits: | |
323 | branches[branch].append(commit_id) |
|
323 | branches[branch].append(commit_id) | |
324 |
|
324 | |||
325 | for branch, commits in branches.items(): |
|
325 | for branch, commits in branches.items(): | |
326 | old_rev = ascii_str(kwargs.get('node_last')) or commits[0] |
|
326 | old_rev = ascii_str(kwargs.get('node_last')) or commits[0] | |
327 | rev_data.append({ |
|
327 | rev_data.append({ | |
328 | 'total_commits': len(commits), |
|
328 | 'total_commits': len(commits), | |
329 | 'old_rev': old_rev, |
|
329 | 'old_rev': old_rev, | |
330 | 'new_rev': commits[-1], |
|
330 | 'new_rev': commits[-1], | |
331 | 'ref': '', |
|
331 | 'ref': '', | |
332 | 'type': 'branch', |
|
332 | 'type': 'branch', | |
333 | 'name': branch, |
|
333 | 'name': branch, | |
334 | }) |
|
334 | }) | |
335 |
|
335 | |||
336 | for push_ref in rev_data: |
|
336 | for push_ref in rev_data: | |
337 | push_ref['multiple_heads'] = _heads |
|
337 | push_ref['multiple_heads'] = _heads | |
338 |
|
338 | |||
339 | repo_path = os.path.join( |
|
339 | repo_path = os.path.join( | |
340 | extras.get('repo_store', ''), extras.get('repository', '')) |
|
340 | extras.get('repo_store', ''), extras.get('repository', '')) | |
341 | push_ref['hg_env'] = _get_hg_env( |
|
341 | push_ref['hg_env'] = _get_hg_env( | |
342 | old_rev=push_ref['old_rev'], |
|
342 | old_rev=push_ref['old_rev'], | |
343 | new_rev=push_ref['new_rev'], txnid=ascii_str(kwargs.get('txnid')), |
|
343 | new_rev=push_ref['new_rev'], txnid=ascii_str(kwargs.get('txnid')), | |
344 | repo_path=repo_path) |
|
344 | repo_path=repo_path) | |
345 |
|
345 | |||
346 | extras['hook_type'] = hook_type or 'pre_push' |
|
346 | extras['hook_type'] = hook_type or 'pre_push' | |
347 | extras['commit_ids'] = rev_data |
|
347 | extras['commit_ids'] = rev_data | |
348 |
|
348 | |||
349 | return _call_hook('pre_push', extras, HgMessageWriter(ui)) |
|
349 | return _call_hook('pre_push', extras, HgMessageWriter(ui)) | |
350 |
|
350 | |||
351 |
|
351 | |||
352 | def pre_push_ssh(ui, repo, node=None, **kwargs): |
|
352 | def pre_push_ssh(ui, repo, node=None, **kwargs): | |
353 | extras = _extras_from_ui(ui) |
|
353 | extras = _extras_from_ui(ui) | |
354 | if extras.get('SSH'): |
|
354 | if extras.get('SSH'): | |
355 | return pre_push(ui, repo, node, **kwargs) |
|
355 | return pre_push(ui, repo, node, **kwargs) | |
356 |
|
356 | |||
357 | return 0 |
|
357 | return 0 | |
358 |
|
358 | |||
359 |
|
359 | |||
360 | def pre_push_ssh_auth(ui, repo, node=None, **kwargs): |
|
360 | def pre_push_ssh_auth(ui, repo, node=None, **kwargs): | |
361 | """ |
|
361 | """ | |
362 | Mercurial pre_push hook for SSH |
|
362 | Mercurial pre_push hook for SSH | |
363 | """ |
|
363 | """ | |
364 | extras = _extras_from_ui(ui) |
|
364 | extras = _extras_from_ui(ui) | |
365 | if extras.get('SSH'): |
|
365 | if extras.get('SSH'): | |
366 | permission = extras['SSH_PERMISSIONS'] |
|
366 | permission = extras['SSH_PERMISSIONS'] | |
367 |
|
367 | |||
368 | if 'repository.write' == permission or 'repository.admin' == permission: |
|
368 | if 'repository.write' == permission or 'repository.admin' == permission: | |
369 | return 0 |
|
369 | return 0 | |
370 |
|
370 | |||
371 | # non-zero ret code |
|
371 | # non-zero ret code | |
372 | return 1 |
|
372 | return 1 | |
373 |
|
373 | |||
374 | return 0 |
|
374 | return 0 | |
375 |
|
375 | |||
376 |
|
376 | |||
377 | def post_push(ui, repo, node, **kwargs): |
|
377 | def post_push(ui, repo, node, **kwargs): | |
378 | """ |
|
378 | """ | |
379 | Mercurial post_push hook |
|
379 | Mercurial post_push hook | |
380 | """ |
|
380 | """ | |
381 | extras = _extras_from_ui(ui) |
|
381 | extras = _extras_from_ui(ui) | |
382 |
|
382 | |||
383 | commit_ids = [] |
|
383 | commit_ids = [] | |
384 | branches = [] |
|
384 | branches = [] | |
385 | bookmarks = [] |
|
385 | bookmarks = [] | |
386 | tags = [] |
|
386 | tags = [] | |
387 | hook_type: str = safe_str(kwargs.get('hooktype')) |
|
387 | hook_type: str = safe_str(kwargs.get('hooktype')) | |
388 |
|
388 | |||
389 | commits, _heads = _rev_range_hash(repo, node) |
|
389 | commits, _heads = _rev_range_hash(repo, node) | |
390 | for commit_id, branch in commits: |
|
390 | for commit_id, branch in commits: | |
391 | commit_ids.append(commit_id) |
|
391 | commit_ids.append(commit_id) | |
392 | if branch not in branches: |
|
392 | if branch not in branches: | |
393 | branches.append(branch) |
|
393 | branches.append(branch) | |
394 |
|
394 | |||
395 | if hasattr(ui, '_rc_pushkey_bookmarks'): |
|
395 | if hasattr(ui, '_rc_pushkey_bookmarks'): | |
396 | bookmarks = ui._rc_pushkey_bookmarks |
|
396 | bookmarks = ui._rc_pushkey_bookmarks | |
397 |
|
397 | |||
398 | extras['hook_type'] = hook_type or 'post_push' |
|
398 | extras['hook_type'] = hook_type or 'post_push' | |
399 | extras['commit_ids'] = commit_ids |
|
399 | extras['commit_ids'] = commit_ids | |
400 |
|
400 | |||
401 | extras['new_refs'] = { |
|
401 | extras['new_refs'] = { | |
402 | 'branches': branches, |
|
402 | 'branches': branches, | |
403 | 'bookmarks': bookmarks, |
|
403 | 'bookmarks': bookmarks, | |
404 | 'tags': tags |
|
404 | 'tags': tags | |
405 | } |
|
405 | } | |
406 |
|
406 | |||
407 | return _call_hook('post_push', extras, HgMessageWriter(ui)) |
|
407 | return _call_hook('post_push', extras, HgMessageWriter(ui)) | |
408 |
|
408 | |||
409 |
|
409 | |||
410 | def post_push_ssh(ui, repo, node, **kwargs): |
|
410 | def post_push_ssh(ui, repo, node, **kwargs): | |
411 | """ |
|
411 | """ | |
412 | Mercurial post_push hook for SSH |
|
412 | Mercurial post_push hook for SSH | |
413 | """ |
|
413 | """ | |
414 | if _extras_from_ui(ui).get('SSH'): |
|
414 | if _extras_from_ui(ui).get('SSH'): | |
415 | return post_push(ui, repo, node, **kwargs) |
|
415 | return post_push(ui, repo, node, **kwargs) | |
416 | return 0 |
|
416 | return 0 | |
417 |
|
417 | |||
418 |
|
418 | |||
419 | def key_push(ui, repo, **kwargs): |
|
419 | def key_push(ui, repo, **kwargs): | |
420 | from vcsserver.hgcompat import get_ctx |
|
420 | from vcsserver.hgcompat import get_ctx | |
421 |
|
421 | |||
422 | if kwargs['new'] != b'0' and kwargs['namespace'] == b'bookmarks': |
|
422 | if kwargs['new'] != b'0' and kwargs['namespace'] == b'bookmarks': | |
423 | # store new bookmarks in our UI object propagated later to post_push |
|
423 | # store new bookmarks in our UI object propagated later to post_push | |
424 | ui._rc_pushkey_bookmarks = get_ctx(repo, kwargs['key']).bookmarks() |
|
424 | ui._rc_pushkey_bookmarks = get_ctx(repo, kwargs['key']).bookmarks() | |
425 | return |
|
425 | return | |
426 |
|
426 | |||
427 |
|
427 | |||
428 | # backward compat |
|
428 | # backward compat | |
429 | log_pull_action = post_pull |
|
429 | log_pull_action = post_pull | |
430 |
|
430 | |||
431 | # backward compat |
|
431 | # backward compat | |
432 | log_push_action = post_push |
|
432 | log_push_action = post_push | |
433 |
|
433 | |||
434 |
|
434 | |||
435 | def handle_git_pre_receive(unused_repo_path, unused_revs, unused_env): |
|
435 | def handle_git_pre_receive(unused_repo_path, unused_revs, unused_env): | |
436 | """ |
|
436 | """ | |
437 | Old hook name: keep here for backward compatibility. |
|
437 | Old hook name: keep here for backward compatibility. | |
438 |
|
438 | |||
439 | This is only required when the installed git hooks are not upgraded. |
|
439 | This is only required when the installed git hooks are not upgraded. | |
440 | """ |
|
440 | """ | |
441 | pass |
|
441 | pass | |
442 |
|
442 | |||
443 |
|
443 | |||
444 | def handle_git_post_receive(unused_repo_path, unused_revs, unused_env): |
|
444 | def handle_git_post_receive(unused_repo_path, unused_revs, unused_env): | |
445 | """ |
|
445 | """ | |
446 | Old hook name: keep here for backward compatibility. |
|
446 | Old hook name: keep here for backward compatibility. | |
447 |
|
447 | |||
448 | This is only required when the installed git hooks are not upgraded. |
|
448 | This is only required when the installed git hooks are not upgraded. | |
449 | """ |
|
449 | """ | |
450 | pass |
|
450 | pass | |
451 |
|
451 | |||
452 |
|
452 | |||
453 | @dataclasses.dataclass |
|
453 | @dataclasses.dataclass | |
454 | class HookResponse: |
|
454 | class HookResponse: | |
455 | status: int |
|
455 | status: int | |
456 | output: str |
|
456 | output: str | |
457 |
|
457 | |||
458 |
|
458 | |||
459 | def git_pre_pull(extras) -> HookResponse: |
|
459 | def git_pre_pull(extras) -> HookResponse: | |
460 | """ |
|
460 | """ | |
461 | Pre pull hook. |
|
461 | Pre pull hook. | |
462 |
|
462 | |||
463 | :param extras: dictionary containing the keys defined in simplevcs |
|
463 | :param extras: dictionary containing the keys defined in simplevcs | |
464 | :type extras: dict |
|
464 | :type extras: dict | |
465 |
|
465 | |||
466 | :return: status code of the hook. 0 for success. |
|
466 | :return: status code of the hook. 0 for success. | |
467 | :rtype: int |
|
467 | :rtype: int | |
468 | """ |
|
468 | """ | |
469 |
|
469 | |||
470 | if 'pull' not in extras['hooks']: |
|
470 | if 'pull' not in extras['hooks']: | |
471 | return HookResponse(0, '') |
|
471 | return HookResponse(0, '') | |
472 |
|
472 | |||
473 | stdout = io.StringIO() |
|
473 | stdout = io.StringIO() | |
474 | try: |
|
474 | try: | |
475 | status_code = _call_hook('pre_pull', extras, GitMessageWriter(stdout)) |
|
475 | status_code = _call_hook('pre_pull', extras, GitMessageWriter(stdout)) | |
476 |
|
476 | |||
477 | except Exception as error: |
|
477 | except Exception as error: | |
478 | log.exception('Failed to call pre_pull hook') |
|
478 | log.exception('Failed to call pre_pull hook') | |
479 | status_code = 128 |
|
479 | status_code = 128 | |
480 | stdout.write(f'ERROR: {error}\n') |
|
480 | stdout.write(f'ERROR: {error}\n') | |
481 |
|
481 | |||
482 | return HookResponse(status_code, stdout.getvalue()) |
|
482 | return HookResponse(status_code, stdout.getvalue()) | |
483 |
|
483 | |||
484 |
|
484 | |||
485 | def git_post_pull(extras) -> HookResponse: |
|
485 | def git_post_pull(extras) -> HookResponse: | |
486 | """ |
|
486 | """ | |
487 | Post pull hook. |
|
487 | Post pull hook. | |
488 |
|
488 | |||
489 | :param extras: dictionary containing the keys defined in simplevcs |
|
489 | :param extras: dictionary containing the keys defined in simplevcs | |
490 | :type extras: dict |
|
490 | :type extras: dict | |
491 |
|
491 | |||
492 | :return: status code of the hook. 0 for success. |
|
492 | :return: status code of the hook. 0 for success. | |
493 | :rtype: int |
|
493 | :rtype: int | |
494 | """ |
|
494 | """ | |
495 | if 'pull' not in extras['hooks']: |
|
495 | if 'pull' not in extras['hooks']: | |
496 | return HookResponse(0, '') |
|
496 | return HookResponse(0, '') | |
497 |
|
497 | |||
498 | stdout = io.StringIO() |
|
498 | stdout = io.StringIO() | |
499 | try: |
|
499 | try: | |
500 | status = _call_hook('post_pull', extras, GitMessageWriter(stdout)) |
|
500 | status = _call_hook('post_pull', extras, GitMessageWriter(stdout)) | |
501 | except Exception as error: |
|
501 | except Exception as error: | |
502 | status = 128 |
|
502 | status = 128 | |
503 | stdout.write(f'ERROR: {error}\n') |
|
503 | stdout.write(f'ERROR: {error}\n') | |
504 |
|
504 | |||
505 | return HookResponse(status, stdout.getvalue()) |
|
505 | return HookResponse(status, stdout.getvalue()) | |
506 |
|
506 | |||
507 |
|
507 | |||
508 | def _parse_git_ref_lines(revision_lines): |
|
508 | def _parse_git_ref_lines(revision_lines): | |
509 | rev_data = [] |
|
509 | rev_data = [] | |
510 | for revision_line in revision_lines or []: |
|
510 | for revision_line in revision_lines or []: | |
511 | old_rev, new_rev, ref = revision_line.strip().split(' ') |
|
511 | old_rev, new_rev, ref = revision_line.strip().split(' ') | |
512 | ref_data = ref.split('/', 2) |
|
512 | ref_data = ref.split('/', 2) | |
513 | if ref_data[1] in ('tags', 'heads'): |
|
513 | if ref_data[1] in ('tags', 'heads'): | |
514 | rev_data.append({ |
|
514 | rev_data.append({ | |
515 | # NOTE(marcink): |
|
515 | # NOTE(marcink): | |
516 | # we're unable to tell total_commits for git at this point |
|
516 | # we're unable to tell total_commits for git at this point | |
517 | # but we set the variable for consistency with GIT |
|
517 | # but we set the variable for consistency with GIT | |
518 | 'total_commits': -1, |
|
518 | 'total_commits': -1, | |
519 | 'old_rev': old_rev, |
|
519 | 'old_rev': old_rev, | |
520 | 'new_rev': new_rev, |
|
520 | 'new_rev': new_rev, | |
521 | 'ref': ref, |
|
521 | 'ref': ref, | |
522 | 'type': ref_data[1], |
|
522 | 'type': ref_data[1], | |
523 | 'name': ref_data[2], |
|
523 | 'name': ref_data[2], | |
524 | }) |
|
524 | }) | |
525 | return rev_data |
|
525 | return rev_data | |
526 |
|
526 | |||
527 |
|
527 | |||
528 | def git_pre_receive(unused_repo_path, revision_lines, env) -> int: |
|
528 | def git_pre_receive(unused_repo_path, revision_lines, env) -> int: | |
529 | """ |
|
529 | """ | |
530 | Pre push hook. |
|
530 | Pre push hook. | |
531 |
|
531 | |||
532 | :return: status code of the hook. 0 for success. |
|
532 | :return: status code of the hook. 0 for success. | |
533 | """ |
|
533 | """ | |
534 | extras = json.loads(env['RC_SCM_DATA']) |
|
534 | extras = json.loads(env['RC_SCM_DATA']) | |
535 | rev_data = _parse_git_ref_lines(revision_lines) |
|
535 | rev_data = _parse_git_ref_lines(revision_lines) | |
536 | if 'push' not in extras['hooks']: |
|
536 | if 'push' not in extras['hooks']: | |
537 | return 0 |
|
537 | return 0 | |
538 | empty_commit_id = '0' * 40 |
|
538 | empty_commit_id = '0' * 40 | |
539 |
|
539 | |||
540 | detect_force_push = extras.get('detect_force_push') |
|
540 | detect_force_push = extras.get('detect_force_push') | |
541 |
|
541 | |||
542 | for push_ref in rev_data: |
|
542 | for push_ref in rev_data: | |
543 | # store our git-env which holds the temp store |
|
543 | # store our git-env which holds the temp store | |
544 | push_ref['git_env'] = _get_git_env() |
|
544 | push_ref['git_env'] = _get_git_env() | |
545 | push_ref['pruned_sha'] = '' |
|
545 | push_ref['pruned_sha'] = '' | |
546 | if not detect_force_push: |
|
546 | if not detect_force_push: | |
547 | # don't check for forced-push when we don't need to |
|
547 | # don't check for forced-push when we don't need to | |
548 | continue |
|
548 | continue | |
549 |
|
549 | |||
550 | type_ = push_ref['type'] |
|
550 | type_ = push_ref['type'] | |
551 | new_branch = push_ref['old_rev'] == empty_commit_id |
|
551 | new_branch = push_ref['old_rev'] == empty_commit_id | |
552 | delete_branch = push_ref['new_rev'] == empty_commit_id |
|
552 | delete_branch = push_ref['new_rev'] == empty_commit_id | |
553 | if type_ == 'heads' and not (new_branch or delete_branch): |
|
553 | if type_ == 'heads' and not (new_branch or delete_branch): | |
554 | old_rev = push_ref['old_rev'] |
|
554 | old_rev = push_ref['old_rev'] | |
555 | new_rev = push_ref['new_rev'] |
|
555 | new_rev = push_ref['new_rev'] | |
556 | cmd = [settings.GIT_EXECUTABLE, 'rev-list', old_rev, f'^{new_rev}'] |
|
556 | cmd = [settings.GIT_EXECUTABLE, 'rev-list', old_rev, f'^{new_rev}'] | |
557 | stdout, stderr = subprocessio.run_command( |
|
557 | stdout, stderr = subprocessio.run_command( | |
558 | cmd, env=os.environ.copy()) |
|
558 | cmd, env=os.environ.copy()) | |
559 | # means we're having some non-reachable objects, this forced push was used |
|
559 | # means we're having some non-reachable objects, this forced push was used | |
560 | if stdout: |
|
560 | if stdout: | |
561 | push_ref['pruned_sha'] = stdout.splitlines() |
|
561 | push_ref['pruned_sha'] = stdout.splitlines() | |
562 |
|
562 | |||
563 | extras['hook_type'] = 'pre_receive' |
|
563 | extras['hook_type'] = 'pre_receive' | |
564 | extras['commit_ids'] = rev_data |
|
564 | extras['commit_ids'] = rev_data | |
565 |
|
565 | |||
566 | stdout = sys.stdout |
|
566 | stdout = sys.stdout | |
567 | status_code = _call_hook('pre_push', extras, GitMessageWriter(stdout)) |
|
567 | status_code = _call_hook('pre_push', extras, GitMessageWriter(stdout)) | |
568 |
|
568 | |||
569 | return status_code |
|
569 | return status_code | |
570 |
|
570 | |||
571 |
|
571 | |||
572 | def git_post_receive(unused_repo_path, revision_lines, env) -> int: |
|
572 | def git_post_receive(unused_repo_path, revision_lines, env) -> int: | |
573 | """ |
|
573 | """ | |
574 | Post push hook. |
|
574 | Post push hook. | |
575 |
|
575 | |||
576 | :return: status code of the hook. 0 for success. |
|
576 | :return: status code of the hook. 0 for success. | |
577 | """ |
|
577 | """ | |
578 | extras = json.loads(env['RC_SCM_DATA']) |
|
578 | extras = json.loads(env['RC_SCM_DATA']) | |
579 | if 'push' not in extras['hooks']: |
|
579 | if 'push' not in extras['hooks']: | |
580 | return 0 |
|
580 | return 0 | |
581 |
|
581 | |||
582 | rev_data = _parse_git_ref_lines(revision_lines) |
|
582 | rev_data = _parse_git_ref_lines(revision_lines) | |
583 |
|
583 | |||
584 | git_revs = [] |
|
584 | git_revs = [] | |
585 |
|
585 | |||
586 | # N.B.(skreft): it is ok to just call git, as git before calling a |
|
586 | # N.B.(skreft): it is ok to just call git, as git before calling a | |
587 | # subcommand sets the PATH environment variable so that it point to the |
|
587 | # subcommand sets the PATH environment variable so that it point to the | |
588 | # correct version of the git executable. |
|
588 | # correct version of the git executable. | |
589 | empty_commit_id = '0' * 40 |
|
589 | empty_commit_id = '0' * 40 | |
590 | branches = [] |
|
590 | branches = [] | |
591 | tags = [] |
|
591 | tags = [] | |
592 | for push_ref in rev_data: |
|
592 | for push_ref in rev_data: | |
593 | type_ = push_ref['type'] |
|
593 | type_ = push_ref['type'] | |
594 |
|
594 | |||
595 | if type_ == 'heads': |
|
595 | if type_ == 'heads': | |
596 | # starting new branch case |
|
596 | # starting new branch case | |
597 | if push_ref['old_rev'] == empty_commit_id: |
|
597 | if push_ref['old_rev'] == empty_commit_id: | |
598 | push_ref_name = push_ref['name'] |
|
598 | push_ref_name = push_ref['name'] | |
599 |
|
599 | |||
600 | if push_ref_name not in branches: |
|
600 | if push_ref_name not in branches: | |
601 | branches.append(push_ref_name) |
|
601 | branches.append(push_ref_name) | |
602 |
|
602 | |||
603 | need_head_set = '' |
|
603 | need_head_set = '' | |
604 | with Repository(os.getcwd()) as repo: |
|
604 | with Repository(os.getcwd()) as repo: | |
605 | try: |
|
605 | try: | |
606 | repo.head |
|
606 | repo.head | |
607 | except pygit2.GitError: |
|
607 | except pygit2.GitError: | |
608 | need_head_set = f'refs/heads/{push_ref_name}' |
|
608 | need_head_set = f'refs/heads/{push_ref_name}' | |
609 |
|
609 | |||
610 | if need_head_set: |
|
610 | if need_head_set: | |
611 | repo.set_head(need_head_set) |
|
611 | repo.set_head(need_head_set) | |
612 | print(f"Setting default branch to {push_ref_name}") |
|
612 | print(f"Setting default branch to {push_ref_name}") | |
613 |
|
613 | |||
614 | cmd = [settings.GIT_EXECUTABLE, 'for-each-ref', '--format=%(refname)', 'refs/heads/*'] |
|
614 | cmd = [settings.GIT_EXECUTABLE, 'for-each-ref', '--format=%(refname)', 'refs/heads/*'] | |
615 | stdout, stderr = subprocessio.run_command( |
|
615 | stdout, stderr = subprocessio.run_command( | |
616 | cmd, env=os.environ.copy()) |
|
616 | cmd, env=os.environ.copy()) | |
617 | heads = safe_str(stdout) |
|
617 | heads = safe_str(stdout) | |
618 | heads = heads.replace(push_ref['ref'], '') |
|
618 | heads = heads.replace(push_ref['ref'], '') | |
619 | heads = ' '.join(head for head |
|
619 | heads = ' '.join(head for head | |
620 | in heads.splitlines() if head) or '.' |
|
620 | in heads.splitlines() if head) or '.' | |
621 | cmd = [settings.GIT_EXECUTABLE, 'log', '--reverse', |
|
621 | cmd = [settings.GIT_EXECUTABLE, 'log', '--reverse', | |
622 | '--pretty=format:%H', '--', push_ref['new_rev'], |
|
622 | '--pretty=format:%H', '--', push_ref['new_rev'], | |
623 | '--not', heads] |
|
623 | '--not', heads] | |
624 | stdout, stderr = subprocessio.run_command( |
|
624 | stdout, stderr = subprocessio.run_command( | |
625 | cmd, env=os.environ.copy()) |
|
625 | cmd, env=os.environ.copy()) | |
626 | git_revs.extend(list(map(ascii_str, stdout.splitlines()))) |
|
626 | git_revs.extend(list(map(ascii_str, stdout.splitlines()))) | |
627 |
|
627 | |||
628 | # delete branch case |
|
628 | # delete branch case | |
629 | elif push_ref['new_rev'] == empty_commit_id: |
|
629 | elif push_ref['new_rev'] == empty_commit_id: | |
630 |
git_revs.append('delete_branch=> |
|
630 | git_revs.append(f'delete_branch=>{push_ref["name"]}') | |
631 | else: |
|
631 | else: | |
632 | if push_ref['name'] not in branches: |
|
632 | if push_ref['name'] not in branches: | |
633 | branches.append(push_ref['name']) |
|
633 | branches.append(push_ref['name']) | |
634 |
|
634 | |||
635 | cmd = [settings.GIT_EXECUTABLE, 'log', |
|
635 | cmd = [settings.GIT_EXECUTABLE, 'log', | |
636 | '{old_rev}..{new_rev}'.format(**push_ref), |
|
636 | '{old_rev}..{new_rev}'.format(**push_ref), | |
637 | '--reverse', '--pretty=format:%H'] |
|
637 | '--reverse', '--pretty=format:%H'] | |
638 | stdout, stderr = subprocessio.run_command( |
|
638 | stdout, stderr = subprocessio.run_command( | |
639 | cmd, env=os.environ.copy()) |
|
639 | cmd, env=os.environ.copy()) | |
640 | # we get bytes from stdout, we need str to be consistent |
|
640 | # we get bytes from stdout, we need str to be consistent | |
641 | log_revs = list(map(ascii_str, stdout.splitlines())) |
|
641 | log_revs = list(map(ascii_str, stdout.splitlines())) | |
642 | git_revs.extend(log_revs) |
|
642 | git_revs.extend(log_revs) | |
643 |
|
643 | |||
644 | # Pure pygit2 impl. but still 2-3x slower :/ |
|
644 | # Pure pygit2 impl. but still 2-3x slower :/ | |
645 | # results = [] |
|
645 | # results = [] | |
646 | # |
|
646 | # | |
647 | # with Repository(os.getcwd()) as repo: |
|
647 | # with Repository(os.getcwd()) as repo: | |
648 | # repo_new_rev = repo[push_ref['new_rev']] |
|
648 | # repo_new_rev = repo[push_ref['new_rev']] | |
649 | # repo_old_rev = repo[push_ref['old_rev']] |
|
649 | # repo_old_rev = repo[push_ref['old_rev']] | |
650 | # walker = repo.walk(repo_new_rev.id, pygit2.GIT_SORT_TOPOLOGICAL) |
|
650 | # walker = repo.walk(repo_new_rev.id, pygit2.GIT_SORT_TOPOLOGICAL) | |
651 | # |
|
651 | # | |
652 | # for commit in walker: |
|
652 | # for commit in walker: | |
653 | # if commit.id == repo_old_rev.id: |
|
653 | # if commit.id == repo_old_rev.id: | |
654 | # break |
|
654 | # break | |
655 | # results.append(commit.id.hex) |
|
655 | # results.append(commit.id.hex) | |
656 | # # reverse the order, can't use GIT_SORT_REVERSE |
|
656 | # # reverse the order, can't use GIT_SORT_REVERSE | |
657 | # log_revs = results[::-1] |
|
657 | # log_revs = results[::-1] | |
658 |
|
658 | |||
659 | elif type_ == 'tags': |
|
659 | elif type_ == 'tags': | |
660 | if push_ref['name'] not in tags: |
|
660 | if push_ref['name'] not in tags: | |
661 | tags.append(push_ref['name']) |
|
661 | tags.append(push_ref['name']) | |
662 |
git_revs.append('tag=> |
|
662 | git_revs.append(f'tag=>{push_ref["name"]}') | |
663 |
|
663 | |||
664 | extras['hook_type'] = 'post_receive' |
|
664 | extras['hook_type'] = 'post_receive' | |
665 | extras['commit_ids'] = git_revs |
|
665 | extras['commit_ids'] = git_revs | |
666 | extras['new_refs'] = { |
|
666 | extras['new_refs'] = { | |
667 | 'branches': branches, |
|
667 | 'branches': branches, | |
668 | 'bookmarks': [], |
|
668 | 'bookmarks': [], | |
669 | 'tags': tags, |
|
669 | 'tags': tags, | |
670 | } |
|
670 | } | |
671 |
|
671 | |||
672 | stdout = sys.stdout |
|
672 | stdout = sys.stdout | |
673 |
|
673 | |||
674 | if 'repo_size' in extras['hooks']: |
|
674 | if 'repo_size' in extras['hooks']: | |
675 | try: |
|
675 | try: | |
676 | _call_hook('repo_size', extras, GitMessageWriter(stdout)) |
|
676 | _call_hook('repo_size', extras, GitMessageWriter(stdout)) | |
677 | except Exception: |
|
677 | except Exception: | |
678 | pass |
|
678 | pass | |
679 |
|
679 | |||
680 | status_code = _call_hook('post_push', extras, GitMessageWriter(stdout)) |
|
680 | status_code = _call_hook('post_push', extras, GitMessageWriter(stdout)) | |
681 | return status_code |
|
681 | return status_code | |
682 |
|
682 | |||
683 |
|
683 | |||
684 | def _get_extras_from_txn_id(path, txn_id): |
|
684 | def _get_extras_from_txn_id(path, txn_id): | |
685 | extras = {} |
|
685 | extras = {} | |
686 | try: |
|
686 | try: | |
687 | cmd = [settings.SVNLOOK_EXECUTABLE, 'pget', |
|
687 | cmd = [settings.SVNLOOK_EXECUTABLE, 'pget', | |
688 | '-t', txn_id, |
|
688 | '-t', txn_id, | |
689 | '--revprop', path, 'rc-scm-extras'] |
|
689 | '--revprop', path, 'rc-scm-extras'] | |
690 | stdout, stderr = subprocessio.run_command( |
|
690 | stdout, stderr = subprocessio.run_command( | |
691 | cmd, env=os.environ.copy()) |
|
691 | cmd, env=os.environ.copy()) | |
692 | extras = json.loads(base64.urlsafe_b64decode(stdout)) |
|
692 | extras = json.loads(base64.urlsafe_b64decode(stdout)) | |
693 | except Exception: |
|
693 | except Exception: | |
694 | log.exception('Failed to extract extras info from txn_id') |
|
694 | log.exception('Failed to extract extras info from txn_id') | |
695 |
|
695 | |||
696 | return extras |
|
696 | return extras | |
697 |
|
697 | |||
698 |
|
698 | |||
699 | def _get_extras_from_commit_id(commit_id, path): |
|
699 | def _get_extras_from_commit_id(commit_id, path): | |
700 | extras = {} |
|
700 | extras = {} | |
701 | try: |
|
701 | try: | |
702 | cmd = [settings.SVNLOOK_EXECUTABLE, 'pget', |
|
702 | cmd = [settings.SVNLOOK_EXECUTABLE, 'pget', | |
703 | '-r', commit_id, |
|
703 | '-r', commit_id, | |
704 | '--revprop', path, 'rc-scm-extras'] |
|
704 | '--revprop', path, 'rc-scm-extras'] | |
705 | stdout, stderr = subprocessio.run_command( |
|
705 | stdout, stderr = subprocessio.run_command( | |
706 | cmd, env=os.environ.copy()) |
|
706 | cmd, env=os.environ.copy()) | |
707 | extras = json.loads(base64.urlsafe_b64decode(stdout)) |
|
707 | extras = json.loads(base64.urlsafe_b64decode(stdout)) | |
708 | except Exception: |
|
708 | except Exception: | |
709 | log.exception('Failed to extract extras info from commit_id') |
|
709 | log.exception('Failed to extract extras info from commit_id') | |
710 |
|
710 | |||
711 | return extras |
|
711 | return extras | |
712 |
|
712 | |||
713 |
|
713 | |||
714 | def svn_pre_commit(repo_path, commit_data, env): |
|
714 | def svn_pre_commit(repo_path, commit_data, env): | |
715 | path, txn_id = commit_data |
|
715 | path, txn_id = commit_data | |
716 | branches = [] |
|
716 | branches = [] | |
717 | tags = [] |
|
717 | tags = [] | |
718 |
|
718 | |||
719 | if env.get('RC_SCM_DATA'): |
|
719 | if env.get('RC_SCM_DATA'): | |
720 | extras = json.loads(env['RC_SCM_DATA']) |
|
720 | extras = json.loads(env['RC_SCM_DATA']) | |
721 | else: |
|
721 | else: | |
722 | # fallback method to read from TXN-ID stored data |
|
722 | # fallback method to read from TXN-ID stored data | |
723 | extras = _get_extras_from_txn_id(path, txn_id) |
|
723 | extras = _get_extras_from_txn_id(path, txn_id) | |
724 | if not extras: |
|
724 | if not extras: | |
725 | return 0 |
|
725 | return 0 | |
726 |
|
726 | |||
727 | extras['hook_type'] = 'pre_commit' |
|
727 | extras['hook_type'] = 'pre_commit' | |
728 | extras['commit_ids'] = [txn_id] |
|
728 | extras['commit_ids'] = [txn_id] | |
729 | extras['txn_id'] = txn_id |
|
729 | extras['txn_id'] = txn_id | |
730 | extras['new_refs'] = { |
|
730 | extras['new_refs'] = { | |
731 | 'total_commits': 1, |
|
731 | 'total_commits': 1, | |
732 | 'branches': branches, |
|
732 | 'branches': branches, | |
733 | 'bookmarks': [], |
|
733 | 'bookmarks': [], | |
734 | 'tags': tags, |
|
734 | 'tags': tags, | |
735 | } |
|
735 | } | |
736 |
|
736 | |||
737 | return _call_hook('pre_push', extras, SvnMessageWriter()) |
|
737 | return _call_hook('pre_push', extras, SvnMessageWriter()) | |
738 |
|
738 | |||
739 |
|
739 | |||
740 | def svn_post_commit(repo_path, commit_data, env): |
|
740 | def svn_post_commit(repo_path, commit_data, env): | |
741 | """ |
|
741 | """ | |
742 | commit_data is path, rev, txn_id |
|
742 | commit_data is path, rev, txn_id | |
743 | """ |
|
743 | """ | |
744 | if len(commit_data) == 3: |
|
744 | if len(commit_data) == 3: | |
745 | path, commit_id, txn_id = commit_data |
|
745 | path, commit_id, txn_id = commit_data | |
746 | elif len(commit_data) == 2: |
|
746 | elif len(commit_data) == 2: | |
747 | log.error('Failed to extract txn_id from commit_data using legacy method. ' |
|
747 | log.error('Failed to extract txn_id from commit_data using legacy method. ' | |
748 | 'Some functionality might be limited') |
|
748 | 'Some functionality might be limited') | |
749 | path, commit_id = commit_data |
|
749 | path, commit_id = commit_data | |
750 | txn_id = None |
|
750 | txn_id = None | |
751 |
|
751 | |||
752 | branches = [] |
|
752 | branches = [] | |
753 | tags = [] |
|
753 | tags = [] | |
754 |
|
754 | |||
755 | if env.get('RC_SCM_DATA'): |
|
755 | if env.get('RC_SCM_DATA'): | |
756 | extras = json.loads(env['RC_SCM_DATA']) |
|
756 | extras = json.loads(env['RC_SCM_DATA']) | |
757 | else: |
|
757 | else: | |
758 | # fallback method to read from TXN-ID stored data |
|
758 | # fallback method to read from TXN-ID stored data | |
759 | extras = _get_extras_from_commit_id(commit_id, path) |
|
759 | extras = _get_extras_from_commit_id(commit_id, path) | |
760 | if not extras: |
|
760 | if not extras: | |
761 | return 0 |
|
761 | return 0 | |
762 |
|
762 | |||
763 | extras['hook_type'] = 'post_commit' |
|
763 | extras['hook_type'] = 'post_commit' | |
764 | extras['commit_ids'] = [commit_id] |
|
764 | extras['commit_ids'] = [commit_id] | |
765 | extras['txn_id'] = txn_id |
|
765 | extras['txn_id'] = txn_id | |
766 | extras['new_refs'] = { |
|
766 | extras['new_refs'] = { | |
767 | 'branches': branches, |
|
767 | 'branches': branches, | |
768 | 'bookmarks': [], |
|
768 | 'bookmarks': [], | |
769 | 'tags': tags, |
|
769 | 'tags': tags, | |
770 | 'total_commits': 1, |
|
770 | 'total_commits': 1, | |
771 | } |
|
771 | } | |
772 |
|
772 | |||
773 | if 'repo_size' in extras['hooks']: |
|
773 | if 'repo_size' in extras['hooks']: | |
774 | try: |
|
774 | try: | |
775 | _call_hook('repo_size', extras, SvnMessageWriter()) |
|
775 | _call_hook('repo_size', extras, SvnMessageWriter()) | |
776 | except Exception: |
|
776 | except Exception: | |
777 | pass |
|
777 | pass | |
778 |
|
778 | |||
779 | return _call_hook('post_push', extras, SvnMessageWriter()) |
|
779 | return _call_hook('post_push', extras, SvnMessageWriter()) |
@@ -1,775 +1,775 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import io |
|
18 | import io | |
19 | import os |
|
19 | import os | |
20 | import platform |
|
20 | import platform | |
21 | import sys |
|
21 | import sys | |
22 | import locale |
|
22 | import locale | |
23 | import logging |
|
23 | import logging | |
24 | import uuid |
|
24 | import uuid | |
25 | import time |
|
25 | import time | |
26 | import wsgiref.util |
|
26 | import wsgiref.util | |
27 | import tempfile |
|
27 | import tempfile | |
28 | import psutil |
|
28 | import psutil | |
29 |
|
29 | |||
30 | from itertools import chain |
|
30 | from itertools import chain | |
31 |
|
31 | |||
32 | import msgpack |
|
32 | import msgpack | |
33 | import configparser |
|
33 | import configparser | |
34 |
|
34 | |||
35 | from pyramid.config import Configurator |
|
35 | from pyramid.config import Configurator | |
36 | from pyramid.wsgi import wsgiapp |
|
36 | from pyramid.wsgi import wsgiapp | |
37 | from pyramid.response import Response |
|
37 | from pyramid.response import Response | |
38 |
|
38 | |||
39 | from vcsserver.base import BytesEnvelope, BinaryEnvelope |
|
39 | from vcsserver.base import BytesEnvelope, BinaryEnvelope | |
40 | from vcsserver.lib.rc_json import json |
|
40 | from vcsserver.lib.rc_json import json | |
41 | from vcsserver.config.settings_maker import SettingsMaker |
|
41 | from vcsserver.config.settings_maker import SettingsMaker | |
42 | from vcsserver.str_utils import safe_int |
|
42 | from vcsserver.str_utils import safe_int | |
43 | from vcsserver.lib.statsd_client import StatsdClient |
|
43 | from vcsserver.lib.statsd_client import StatsdClient | |
44 | from vcsserver.tweens.request_wrapper import get_headers_call_context |
|
44 | from vcsserver.tweens.request_wrapper import get_headers_call_context | |
45 |
|
45 | |||
46 | import vcsserver |
|
46 | import vcsserver | |
47 | from vcsserver import remote_wsgi, scm_app, settings, hgpatches |
|
47 | from vcsserver import remote_wsgi, scm_app, settings, hgpatches | |
48 | from vcsserver.git_lfs.app import GIT_LFS_CONTENT_TYPE, GIT_LFS_PROTO_PAT |
|
48 | from vcsserver.git_lfs.app import GIT_LFS_CONTENT_TYPE, GIT_LFS_PROTO_PAT | |
49 | from vcsserver.echo_stub import remote_wsgi as remote_wsgi_stub |
|
49 | from vcsserver.echo_stub import remote_wsgi as remote_wsgi_stub | |
50 | from vcsserver.echo_stub.echo_app import EchoApp |
|
50 | from vcsserver.echo_stub.echo_app import EchoApp | |
51 | from vcsserver.exceptions import HTTPRepoLocked, HTTPRepoBranchProtected |
|
51 | from vcsserver.exceptions import HTTPRepoLocked, HTTPRepoBranchProtected | |
52 | from vcsserver.lib.exc_tracking import store_exception, format_exc |
|
52 | from vcsserver.lib.exc_tracking import store_exception, format_exc | |
53 | from vcsserver.server import VcsServer |
|
53 | from vcsserver.server import VcsServer | |
54 |
|
54 | |||
55 | strict_vcs = True |
|
55 | strict_vcs = True | |
56 |
|
56 | |||
57 | git_import_err = None |
|
57 | git_import_err = None | |
58 | try: |
|
58 | try: | |
59 | from vcsserver.remote.git_remote import GitFactory, GitRemote |
|
59 | from vcsserver.remote.git_remote import GitFactory, GitRemote | |
60 | except ImportError as e: |
|
60 | except ImportError as e: | |
61 | GitFactory = None |
|
61 | GitFactory = None | |
62 | GitRemote = None |
|
62 | GitRemote = None | |
63 | git_import_err = e |
|
63 | git_import_err = e | |
64 | if strict_vcs: |
|
64 | if strict_vcs: | |
65 | raise |
|
65 | raise | |
66 |
|
66 | |||
67 |
|
67 | |||
68 | hg_import_err = None |
|
68 | hg_import_err = None | |
69 | try: |
|
69 | try: | |
70 | from vcsserver.remote.hg_remote import MercurialFactory, HgRemote |
|
70 | from vcsserver.remote.hg_remote import MercurialFactory, HgRemote | |
71 | except ImportError as e: |
|
71 | except ImportError as e: | |
72 | MercurialFactory = None |
|
72 | MercurialFactory = None | |
73 | HgRemote = None |
|
73 | HgRemote = None | |
74 | hg_import_err = e |
|
74 | hg_import_err = e | |
75 | if strict_vcs: |
|
75 | if strict_vcs: | |
76 | raise |
|
76 | raise | |
77 |
|
77 | |||
78 |
|
78 | |||
79 | svn_import_err = None |
|
79 | svn_import_err = None | |
80 | try: |
|
80 | try: | |
81 | from vcsserver.remote.svn_remote import SubversionFactory, SvnRemote |
|
81 | from vcsserver.remote.svn_remote import SubversionFactory, SvnRemote | |
82 | except ImportError as e: |
|
82 | except ImportError as e: | |
83 | SubversionFactory = None |
|
83 | SubversionFactory = None | |
84 | SvnRemote = None |
|
84 | SvnRemote = None | |
85 | svn_import_err = e |
|
85 | svn_import_err = e | |
86 | if strict_vcs: |
|
86 | if strict_vcs: | |
87 | raise |
|
87 | raise | |
88 |
|
88 | |||
89 | log = logging.getLogger(__name__) |
|
89 | log = logging.getLogger(__name__) | |
90 |
|
90 | |||
91 | # due to Mercurial/glibc2.27 problems we need to detect if locale settings are |
|
91 | # due to Mercurial/glibc2.27 problems we need to detect if locale settings are | |
92 | # causing problems and "fix" it in case they do and fallback to LC_ALL = C |
|
92 | # causing problems and "fix" it in case they do and fallback to LC_ALL = C | |
93 |
|
93 | |||
94 | try: |
|
94 | try: | |
95 | locale.setlocale(locale.LC_ALL, '') |
|
95 | locale.setlocale(locale.LC_ALL, '') | |
96 | except locale.Error as e: |
|
96 | except locale.Error as e: | |
97 | log.error( |
|
97 | log.error( | |
98 | 'LOCALE ERROR: failed to set LC_ALL, fallback to LC_ALL=C, org error: %s', e) |
|
98 | 'LOCALE ERROR: failed to set LC_ALL, fallback to LC_ALL=C, org error: %s', e) | |
99 | os.environ['LC_ALL'] = 'C' |
|
99 | os.environ['LC_ALL'] = 'C' | |
100 |
|
100 | |||
101 |
|
101 | |||
102 | def _is_request_chunked(environ): |
|
102 | def _is_request_chunked(environ): | |
103 | stream = environ.get('HTTP_TRANSFER_ENCODING', '') == 'chunked' |
|
103 | stream = environ.get('HTTP_TRANSFER_ENCODING', '') == 'chunked' | |
104 | return stream |
|
104 | return stream | |
105 |
|
105 | |||
106 |
|
106 | |||
107 | def log_max_fd(): |
|
107 | def log_max_fd(): | |
108 | try: |
|
108 | try: | |
109 | maxfd = psutil.Process().rlimit(psutil.RLIMIT_NOFILE)[1] |
|
109 | maxfd = psutil.Process().rlimit(psutil.RLIMIT_NOFILE)[1] | |
110 | log.info('Max file descriptors value: %s', maxfd) |
|
110 | log.info('Max file descriptors value: %s', maxfd) | |
111 | except Exception: |
|
111 | except Exception: | |
112 | pass |
|
112 | pass | |
113 |
|
113 | |||
114 |
|
114 | |||
115 |
class VCS |
|
115 | class VCS: | |
116 | def __init__(self, locale_conf=None, cache_config=None): |
|
116 | def __init__(self, locale_conf=None, cache_config=None): | |
117 | self.locale = locale_conf |
|
117 | self.locale = locale_conf | |
118 | self.cache_config = cache_config |
|
118 | self.cache_config = cache_config | |
119 | self._configure_locale() |
|
119 | self._configure_locale() | |
120 |
|
120 | |||
121 | log_max_fd() |
|
121 | log_max_fd() | |
122 |
|
122 | |||
123 | if GitFactory and GitRemote: |
|
123 | if GitFactory and GitRemote: | |
124 | git_factory = GitFactory() |
|
124 | git_factory = GitFactory() | |
125 | self._git_remote = GitRemote(git_factory) |
|
125 | self._git_remote = GitRemote(git_factory) | |
126 | else: |
|
126 | else: | |
127 | log.error("Git client import failed: %s", git_import_err) |
|
127 | log.error("Git client import failed: %s", git_import_err) | |
128 |
|
128 | |||
129 | if MercurialFactory and HgRemote: |
|
129 | if MercurialFactory and HgRemote: | |
130 | hg_factory = MercurialFactory() |
|
130 | hg_factory = MercurialFactory() | |
131 | self._hg_remote = HgRemote(hg_factory) |
|
131 | self._hg_remote = HgRemote(hg_factory) | |
132 | else: |
|
132 | else: | |
133 | log.error("Mercurial client import failed: %s", hg_import_err) |
|
133 | log.error("Mercurial client import failed: %s", hg_import_err) | |
134 |
|
134 | |||
135 | if SubversionFactory and SvnRemote: |
|
135 | if SubversionFactory and SvnRemote: | |
136 | svn_factory = SubversionFactory() |
|
136 | svn_factory = SubversionFactory() | |
137 |
|
137 | |||
138 | # hg factory is used for svn url validation |
|
138 | # hg factory is used for svn url validation | |
139 | hg_factory = MercurialFactory() |
|
139 | hg_factory = MercurialFactory() | |
140 | self._svn_remote = SvnRemote(svn_factory, hg_factory=hg_factory) |
|
140 | self._svn_remote = SvnRemote(svn_factory, hg_factory=hg_factory) | |
141 | else: |
|
141 | else: | |
142 | log.error("Subversion client import failed: %s", svn_import_err) |
|
142 | log.error("Subversion client import failed: %s", svn_import_err) | |
143 |
|
143 | |||
144 | self._vcsserver = VcsServer() |
|
144 | self._vcsserver = VcsServer() | |
145 |
|
145 | |||
146 | def _configure_locale(self): |
|
146 | def _configure_locale(self): | |
147 | if self.locale: |
|
147 | if self.locale: | |
148 | log.info('Settings locale: `LC_ALL` to %s', self.locale) |
|
148 | log.info('Settings locale: `LC_ALL` to %s', self.locale) | |
149 | else: |
|
149 | else: | |
150 | log.info('Configuring locale subsystem based on environment variables') |
|
150 | log.info('Configuring locale subsystem based on environment variables') | |
151 | try: |
|
151 | try: | |
152 | # If self.locale is the empty string, then the locale |
|
152 | # If self.locale is the empty string, then the locale | |
153 | # module will use the environment variables. See the |
|
153 | # module will use the environment variables. See the | |
154 | # documentation of the package `locale`. |
|
154 | # documentation of the package `locale`. | |
155 | locale.setlocale(locale.LC_ALL, self.locale) |
|
155 | locale.setlocale(locale.LC_ALL, self.locale) | |
156 |
|
156 | |||
157 | language_code, encoding = locale.getlocale() |
|
157 | language_code, encoding = locale.getlocale() | |
158 | log.info( |
|
158 | log.info( | |
159 | 'Locale set to language code "%s" with encoding "%s".', |
|
159 | 'Locale set to language code "%s" with encoding "%s".', | |
160 | language_code, encoding) |
|
160 | language_code, encoding) | |
161 | except locale.Error: |
|
161 | except locale.Error: | |
162 | log.exception('Cannot set locale, not configuring the locale system') |
|
162 | log.exception('Cannot set locale, not configuring the locale system') | |
163 |
|
163 | |||
164 |
|
164 | |||
165 |
class WsgiProxy |
|
165 | class WsgiProxy: | |
166 | def __init__(self, wsgi): |
|
166 | def __init__(self, wsgi): | |
167 | self.wsgi = wsgi |
|
167 | self.wsgi = wsgi | |
168 |
|
168 | |||
169 | def __call__(self, environ, start_response): |
|
169 | def __call__(self, environ, start_response): | |
170 | input_data = environ['wsgi.input'].read() |
|
170 | input_data = environ['wsgi.input'].read() | |
171 | input_data = msgpack.unpackb(input_data) |
|
171 | input_data = msgpack.unpackb(input_data) | |
172 |
|
172 | |||
173 | error = None |
|
173 | error = None | |
174 | try: |
|
174 | try: | |
175 | data, status, headers = self.wsgi.handle( |
|
175 | data, status, headers = self.wsgi.handle( | |
176 | input_data['environment'], input_data['input_data'], |
|
176 | input_data['environment'], input_data['input_data'], | |
177 | *input_data['args'], **input_data['kwargs']) |
|
177 | *input_data['args'], **input_data['kwargs']) | |
178 | except Exception as e: |
|
178 | except Exception as e: | |
179 | data, status, headers = [], None, None |
|
179 | data, status, headers = [], None, None | |
180 | error = { |
|
180 | error = { | |
181 | 'message': str(e), |
|
181 | 'message': str(e), | |
182 | '_vcs_kind': getattr(e, '_vcs_kind', None) |
|
182 | '_vcs_kind': getattr(e, '_vcs_kind', None) | |
183 | } |
|
183 | } | |
184 |
|
184 | |||
185 | start_response(200, {}) |
|
185 | start_response(200, {}) | |
186 | return self._iterator(error, status, headers, data) |
|
186 | return self._iterator(error, status, headers, data) | |
187 |
|
187 | |||
188 | def _iterator(self, error, status, headers, data): |
|
188 | def _iterator(self, error, status, headers, data): | |
189 | initial_data = [ |
|
189 | initial_data = [ | |
190 | error, |
|
190 | error, | |
191 | status, |
|
191 | status, | |
192 | headers, |
|
192 | headers, | |
193 | ] |
|
193 | ] | |
194 |
|
194 | |||
195 | for d in chain(initial_data, data): |
|
195 | for d in chain(initial_data, data): | |
196 | yield msgpack.packb(d) |
|
196 | yield msgpack.packb(d) | |
197 |
|
197 | |||
198 |
|
198 | |||
199 | def not_found(request): |
|
199 | def not_found(request): | |
200 | return {'status': '404 NOT FOUND'} |
|
200 | return {'status': '404 NOT FOUND'} | |
201 |
|
201 | |||
202 |
|
202 | |||
203 |
class VCSViewPredicate |
|
203 | class VCSViewPredicate: | |
204 | def __init__(self, val, config): |
|
204 | def __init__(self, val, config): | |
205 | self.remotes = val |
|
205 | self.remotes = val | |
206 |
|
206 | |||
207 | def text(self): |
|
207 | def text(self): | |
208 | return f'vcs view method = {list(self.remotes.keys())}' |
|
208 | return f'vcs view method = {list(self.remotes.keys())}' | |
209 |
|
209 | |||
210 | phash = text |
|
210 | phash = text | |
211 |
|
211 | |||
212 | def __call__(self, context, request): |
|
212 | def __call__(self, context, request): | |
213 | """ |
|
213 | """ | |
214 | View predicate that returns true if given backend is supported by |
|
214 | View predicate that returns true if given backend is supported by | |
215 | defined remotes. |
|
215 | defined remotes. | |
216 | """ |
|
216 | """ | |
217 | backend = request.matchdict.get('backend') |
|
217 | backend = request.matchdict.get('backend') | |
218 | return backend in self.remotes |
|
218 | return backend in self.remotes | |
219 |
|
219 | |||
220 |
|
220 | |||
221 |
class HTTPApplication |
|
221 | class HTTPApplication: | |
222 | ALLOWED_EXCEPTIONS = ('KeyError', 'URLError') |
|
222 | ALLOWED_EXCEPTIONS = ('KeyError', 'URLError') | |
223 |
|
223 | |||
224 | remote_wsgi = remote_wsgi |
|
224 | remote_wsgi = remote_wsgi | |
225 | _use_echo_app = False |
|
225 | _use_echo_app = False | |
226 |
|
226 | |||
227 | def __init__(self, settings=None, global_config=None): |
|
227 | def __init__(self, settings=None, global_config=None): | |
228 |
|
228 | |||
229 | self.config = Configurator(settings=settings) |
|
229 | self.config = Configurator(settings=settings) | |
230 | # Init our statsd at very start |
|
230 | # Init our statsd at very start | |
231 | self.config.registry.statsd = StatsdClient.statsd |
|
231 | self.config.registry.statsd = StatsdClient.statsd | |
232 | self.config.registry.vcs_call_context = {} |
|
232 | self.config.registry.vcs_call_context = {} | |
233 |
|
233 | |||
234 | self.global_config = global_config |
|
234 | self.global_config = global_config | |
235 | self.config.include('vcsserver.lib.rc_cache') |
|
235 | self.config.include('vcsserver.lib.rc_cache') | |
236 | self.config.include('vcsserver.lib.rc_cache.archive_cache') |
|
236 | self.config.include('vcsserver.lib.rc_cache.archive_cache') | |
237 |
|
237 | |||
238 | settings_locale = settings.get('locale', '') or 'en_US.UTF-8' |
|
238 | settings_locale = settings.get('locale', '') or 'en_US.UTF-8' | |
239 | vcs = VCS(locale_conf=settings_locale, cache_config=settings) |
|
239 | vcs = VCS(locale_conf=settings_locale, cache_config=settings) | |
240 | self._remotes = { |
|
240 | self._remotes = { | |
241 | 'hg': vcs._hg_remote, |
|
241 | 'hg': vcs._hg_remote, | |
242 | 'git': vcs._git_remote, |
|
242 | 'git': vcs._git_remote, | |
243 | 'svn': vcs._svn_remote, |
|
243 | 'svn': vcs._svn_remote, | |
244 | 'server': vcs._vcsserver, |
|
244 | 'server': vcs._vcsserver, | |
245 | } |
|
245 | } | |
246 | if settings.get('dev.use_echo_app', 'false').lower() == 'true': |
|
246 | if settings.get('dev.use_echo_app', 'false').lower() == 'true': | |
247 | self._use_echo_app = True |
|
247 | self._use_echo_app = True | |
248 | log.warning("Using EchoApp for VCS operations.") |
|
248 | log.warning("Using EchoApp for VCS operations.") | |
249 | self.remote_wsgi = remote_wsgi_stub |
|
249 | self.remote_wsgi = remote_wsgi_stub | |
250 |
|
250 | |||
251 | self._configure_settings(global_config, settings) |
|
251 | self._configure_settings(global_config, settings) | |
252 |
|
252 | |||
253 | self._configure() |
|
253 | self._configure() | |
254 |
|
254 | |||
255 | def _configure_settings(self, global_config, app_settings): |
|
255 | def _configure_settings(self, global_config, app_settings): | |
256 | """ |
|
256 | """ | |
257 | Configure the settings module. |
|
257 | Configure the settings module. | |
258 | """ |
|
258 | """ | |
259 | settings_merged = global_config.copy() |
|
259 | settings_merged = global_config.copy() | |
260 | settings_merged.update(app_settings) |
|
260 | settings_merged.update(app_settings) | |
261 |
|
261 | |||
262 | git_path = app_settings.get('git_path', None) |
|
262 | git_path = app_settings.get('git_path', None) | |
263 | if git_path: |
|
263 | if git_path: | |
264 | settings.GIT_EXECUTABLE = git_path |
|
264 | settings.GIT_EXECUTABLE = git_path | |
265 | binary_dir = app_settings.get('core.binary_dir', None) |
|
265 | binary_dir = app_settings.get('core.binary_dir', None) | |
266 | if binary_dir: |
|
266 | if binary_dir: | |
267 | settings.BINARY_DIR = binary_dir |
|
267 | settings.BINARY_DIR = binary_dir | |
268 |
|
268 | |||
269 | # Store the settings to make them available to other modules. |
|
269 | # Store the settings to make them available to other modules. | |
270 | vcsserver.PYRAMID_SETTINGS = settings_merged |
|
270 | vcsserver.PYRAMID_SETTINGS = settings_merged | |
271 | vcsserver.CONFIG = settings_merged |
|
271 | vcsserver.CONFIG = settings_merged | |
272 |
|
272 | |||
273 | def _configure(self): |
|
273 | def _configure(self): | |
274 | self.config.add_renderer(name='msgpack', factory=self._msgpack_renderer_factory) |
|
274 | self.config.add_renderer(name='msgpack', factory=self._msgpack_renderer_factory) | |
275 |
|
275 | |||
276 | self.config.add_route('service', '/_service') |
|
276 | self.config.add_route('service', '/_service') | |
277 | self.config.add_route('status', '/status') |
|
277 | self.config.add_route('status', '/status') | |
278 | self.config.add_route('hg_proxy', '/proxy/hg') |
|
278 | self.config.add_route('hg_proxy', '/proxy/hg') | |
279 | self.config.add_route('git_proxy', '/proxy/git') |
|
279 | self.config.add_route('git_proxy', '/proxy/git') | |
280 |
|
280 | |||
281 | # rpc methods |
|
281 | # rpc methods | |
282 | self.config.add_route('vcs', '/{backend}') |
|
282 | self.config.add_route('vcs', '/{backend}') | |
283 |
|
283 | |||
284 | # streaming rpc remote methods |
|
284 | # streaming rpc remote methods | |
285 | self.config.add_route('vcs_stream', '/{backend}/stream') |
|
285 | self.config.add_route('vcs_stream', '/{backend}/stream') | |
286 |
|
286 | |||
287 | # vcs operations clone/push as streaming |
|
287 | # vcs operations clone/push as streaming | |
288 | self.config.add_route('stream_git', '/stream/git/*repo_name') |
|
288 | self.config.add_route('stream_git', '/stream/git/*repo_name') | |
289 | self.config.add_route('stream_hg', '/stream/hg/*repo_name') |
|
289 | self.config.add_route('stream_hg', '/stream/hg/*repo_name') | |
290 |
|
290 | |||
291 | self.config.add_view(self.status_view, route_name='status', renderer='json') |
|
291 | self.config.add_view(self.status_view, route_name='status', renderer='json') | |
292 | self.config.add_view(self.service_view, route_name='service', renderer='msgpack') |
|
292 | self.config.add_view(self.service_view, route_name='service', renderer='msgpack') | |
293 |
|
293 | |||
294 | self.config.add_view(self.hg_proxy(), route_name='hg_proxy') |
|
294 | self.config.add_view(self.hg_proxy(), route_name='hg_proxy') | |
295 | self.config.add_view(self.git_proxy(), route_name='git_proxy') |
|
295 | self.config.add_view(self.git_proxy(), route_name='git_proxy') | |
296 | self.config.add_view(self.vcs_view, route_name='vcs', renderer='msgpack', |
|
296 | self.config.add_view(self.vcs_view, route_name='vcs', renderer='msgpack', | |
297 | vcs_view=self._remotes) |
|
297 | vcs_view=self._remotes) | |
298 | self.config.add_view(self.vcs_stream_view, route_name='vcs_stream', |
|
298 | self.config.add_view(self.vcs_stream_view, route_name='vcs_stream', | |
299 | vcs_view=self._remotes) |
|
299 | vcs_view=self._remotes) | |
300 |
|
300 | |||
301 | self.config.add_view(self.hg_stream(), route_name='stream_hg') |
|
301 | self.config.add_view(self.hg_stream(), route_name='stream_hg') | |
302 | self.config.add_view(self.git_stream(), route_name='stream_git') |
|
302 | self.config.add_view(self.git_stream(), route_name='stream_git') | |
303 |
|
303 | |||
304 | self.config.add_view_predicate('vcs_view', VCSViewPredicate) |
|
304 | self.config.add_view_predicate('vcs_view', VCSViewPredicate) | |
305 |
|
305 | |||
306 | self.config.add_notfound_view(not_found, renderer='json') |
|
306 | self.config.add_notfound_view(not_found, renderer='json') | |
307 |
|
307 | |||
308 | self.config.add_view(self.handle_vcs_exception, context=Exception) |
|
308 | self.config.add_view(self.handle_vcs_exception, context=Exception) | |
309 |
|
309 | |||
310 | self.config.add_tween( |
|
310 | self.config.add_tween( | |
311 | 'vcsserver.tweens.request_wrapper.RequestWrapperTween', |
|
311 | 'vcsserver.tweens.request_wrapper.RequestWrapperTween', | |
312 | ) |
|
312 | ) | |
313 | self.config.add_request_method( |
|
313 | self.config.add_request_method( | |
314 | 'vcsserver.lib.request_counter.get_request_counter', |
|
314 | 'vcsserver.lib.request_counter.get_request_counter', | |
315 | 'request_count') |
|
315 | 'request_count') | |
316 |
|
316 | |||
317 | def wsgi_app(self): |
|
317 | def wsgi_app(self): | |
318 | return self.config.make_wsgi_app() |
|
318 | return self.config.make_wsgi_app() | |
319 |
|
319 | |||
320 | def _vcs_view_params(self, request): |
|
320 | def _vcs_view_params(self, request): | |
321 | remote = self._remotes[request.matchdict['backend']] |
|
321 | remote = self._remotes[request.matchdict['backend']] | |
322 | payload = msgpack.unpackb(request.body, use_list=True) |
|
322 | payload = msgpack.unpackb(request.body, use_list=True) | |
323 |
|
323 | |||
324 | method = payload.get('method') |
|
324 | method = payload.get('method') | |
325 | params = payload['params'] |
|
325 | params = payload['params'] | |
326 | wire = params.get('wire') |
|
326 | wire = params.get('wire') | |
327 | args = params.get('args') |
|
327 | args = params.get('args') | |
328 | kwargs = params.get('kwargs') |
|
328 | kwargs = params.get('kwargs') | |
329 | context_uid = None |
|
329 | context_uid = None | |
330 |
|
330 | |||
331 | request.registry.vcs_call_context = { |
|
331 | request.registry.vcs_call_context = { | |
332 | 'method': method, |
|
332 | 'method': method, | |
333 | 'repo_name': payload.get('_repo_name'), |
|
333 | 'repo_name': payload.get('_repo_name'), | |
334 | } |
|
334 | } | |
335 |
|
335 | |||
336 | if wire: |
|
336 | if wire: | |
337 | try: |
|
337 | try: | |
338 | wire['context'] = context_uid = uuid.UUID(wire['context']) |
|
338 | wire['context'] = context_uid = uuid.UUID(wire['context']) | |
339 | except KeyError: |
|
339 | except KeyError: | |
340 | pass |
|
340 | pass | |
341 | args.insert(0, wire) |
|
341 | args.insert(0, wire) | |
342 | repo_state_uid = wire.get('repo_state_uid') if wire else None |
|
342 | repo_state_uid = wire.get('repo_state_uid') if wire else None | |
343 |
|
343 | |||
344 | # NOTE(marcink): trading complexity for slight performance |
|
344 | # NOTE(marcink): trading complexity for slight performance | |
345 | if log.isEnabledFor(logging.DEBUG): |
|
345 | if log.isEnabledFor(logging.DEBUG): | |
346 | # also we SKIP printing out any of those methods args since they maybe excessive |
|
346 | # also we SKIP printing out any of those methods args since they maybe excessive | |
347 | just_args_methods = { |
|
347 | just_args_methods = { | |
348 | 'commitctx': ('content', 'removed', 'updated'), |
|
348 | 'commitctx': ('content', 'removed', 'updated'), | |
349 | 'commit': ('content', 'removed', 'updated') |
|
349 | 'commit': ('content', 'removed', 'updated') | |
350 | } |
|
350 | } | |
351 | if method in just_args_methods: |
|
351 | if method in just_args_methods: | |
352 | skip_args = just_args_methods[method] |
|
352 | skip_args = just_args_methods[method] | |
353 | call_args = '' |
|
353 | call_args = '' | |
354 | call_kwargs = {} |
|
354 | call_kwargs = {} | |
355 | for k in kwargs: |
|
355 | for k in kwargs: | |
356 | if k in skip_args: |
|
356 | if k in skip_args: | |
357 | # replace our skip key with dummy |
|
357 | # replace our skip key with dummy | |
358 | call_kwargs[k] = f'RemovedParam({k})' |
|
358 | call_kwargs[k] = f'RemovedParam({k})' | |
359 | else: |
|
359 | else: | |
360 | call_kwargs[k] = kwargs[k] |
|
360 | call_kwargs[k] = kwargs[k] | |
361 | else: |
|
361 | else: | |
362 | call_args = args[1:] |
|
362 | call_args = args[1:] | |
363 | call_kwargs = kwargs |
|
363 | call_kwargs = kwargs | |
364 |
|
364 | |||
365 | log.debug('Method requested:`%s` with args:%s kwargs:%s context_uid: %s, repo_state_uid:%s', |
|
365 | log.debug('Method requested:`%s` with args:%s kwargs:%s context_uid: %s, repo_state_uid:%s', | |
366 | method, call_args, call_kwargs, context_uid, repo_state_uid) |
|
366 | method, call_args, call_kwargs, context_uid, repo_state_uid) | |
367 |
|
367 | |||
368 | statsd = request.registry.statsd |
|
368 | statsd = request.registry.statsd | |
369 | if statsd: |
|
369 | if statsd: | |
370 | statsd.incr( |
|
370 | statsd.incr( | |
371 | 'vcsserver_method_total', tags=[ |
|
371 | 'vcsserver_method_total', tags=[ | |
372 | f"method:{method}", |
|
372 | f"method:{method}", | |
373 | ]) |
|
373 | ]) | |
374 | return payload, remote, method, args, kwargs |
|
374 | return payload, remote, method, args, kwargs | |
375 |
|
375 | |||
376 | def vcs_view(self, request): |
|
376 | def vcs_view(self, request): | |
377 |
|
377 | |||
378 | payload, remote, method, args, kwargs = self._vcs_view_params(request) |
|
378 | payload, remote, method, args, kwargs = self._vcs_view_params(request) | |
379 | payload_id = payload.get('id') |
|
379 | payload_id = payload.get('id') | |
380 |
|
380 | |||
381 | try: |
|
381 | try: | |
382 | resp = getattr(remote, method)(*args, **kwargs) |
|
382 | resp = getattr(remote, method)(*args, **kwargs) | |
383 | except Exception as e: |
|
383 | except Exception as e: | |
384 | exc_info = list(sys.exc_info()) |
|
384 | exc_info = list(sys.exc_info()) | |
385 | exc_type, exc_value, exc_traceback = exc_info |
|
385 | exc_type, exc_value, exc_traceback = exc_info | |
386 |
|
386 | |||
387 | org_exc = getattr(e, '_org_exc', None) |
|
387 | org_exc = getattr(e, '_org_exc', None) | |
388 | org_exc_name = None |
|
388 | org_exc_name = None | |
389 | org_exc_tb = '' |
|
389 | org_exc_tb = '' | |
390 | if org_exc: |
|
390 | if org_exc: | |
391 | org_exc_name = org_exc.__class__.__name__ |
|
391 | org_exc_name = org_exc.__class__.__name__ | |
392 | org_exc_tb = getattr(e, '_org_exc_tb', '') |
|
392 | org_exc_tb = getattr(e, '_org_exc_tb', '') | |
393 | # replace our "faked" exception with our org |
|
393 | # replace our "faked" exception with our org | |
394 | exc_info[0] = org_exc.__class__ |
|
394 | exc_info[0] = org_exc.__class__ | |
395 | exc_info[1] = org_exc |
|
395 | exc_info[1] = org_exc | |
396 |
|
396 | |||
397 | should_store_exc = True |
|
397 | should_store_exc = True | |
398 | if org_exc: |
|
398 | if org_exc: | |
399 | def get_exc_fqn(_exc_obj): |
|
399 | def get_exc_fqn(_exc_obj): | |
400 | module_name = getattr(org_exc.__class__, '__module__', 'UNKNOWN') |
|
400 | module_name = getattr(org_exc.__class__, '__module__', 'UNKNOWN') | |
401 | return module_name + '.' + org_exc_name |
|
401 | return module_name + '.' + org_exc_name | |
402 |
|
402 | |||
403 | exc_fqn = get_exc_fqn(org_exc) |
|
403 | exc_fqn = get_exc_fqn(org_exc) | |
404 |
|
404 | |||
405 | if exc_fqn in ['mercurial.error.RepoLookupError', |
|
405 | if exc_fqn in ['mercurial.error.RepoLookupError', | |
406 | 'vcsserver.exceptions.RefNotFoundException']: |
|
406 | 'vcsserver.exceptions.RefNotFoundException']: | |
407 | should_store_exc = False |
|
407 | should_store_exc = False | |
408 |
|
408 | |||
409 | if should_store_exc: |
|
409 | if should_store_exc: | |
410 | store_exception(id(exc_info), exc_info, request_path=request.path) |
|
410 | store_exception(id(exc_info), exc_info, request_path=request.path) | |
411 |
|
411 | |||
412 | tb_info = format_exc(exc_info) |
|
412 | tb_info = format_exc(exc_info) | |
413 |
|
413 | |||
414 | type_ = e.__class__.__name__ |
|
414 | type_ = e.__class__.__name__ | |
415 | if type_ not in self.ALLOWED_EXCEPTIONS: |
|
415 | if type_ not in self.ALLOWED_EXCEPTIONS: | |
416 | type_ = None |
|
416 | type_ = None | |
417 |
|
417 | |||
418 | resp = { |
|
418 | resp = { | |
419 | 'id': payload_id, |
|
419 | 'id': payload_id, | |
420 | 'error': { |
|
420 | 'error': { | |
421 | 'message': str(e), |
|
421 | 'message': str(e), | |
422 | 'traceback': tb_info, |
|
422 | 'traceback': tb_info, | |
423 | 'org_exc': org_exc_name, |
|
423 | 'org_exc': org_exc_name, | |
424 | 'org_exc_tb': org_exc_tb, |
|
424 | 'org_exc_tb': org_exc_tb, | |
425 | 'type': type_ |
|
425 | 'type': type_ | |
426 | } |
|
426 | } | |
427 | } |
|
427 | } | |
428 |
|
428 | |||
429 | try: |
|
429 | try: | |
430 | resp['error']['_vcs_kind'] = getattr(e, '_vcs_kind', None) |
|
430 | resp['error']['_vcs_kind'] = getattr(e, '_vcs_kind', None) | |
431 | except AttributeError: |
|
431 | except AttributeError: | |
432 | pass |
|
432 | pass | |
433 | else: |
|
433 | else: | |
434 | resp = { |
|
434 | resp = { | |
435 | 'id': payload_id, |
|
435 | 'id': payload_id, | |
436 | 'result': resp |
|
436 | 'result': resp | |
437 | } |
|
437 | } | |
438 | log.debug('Serving data for method %s', method) |
|
438 | log.debug('Serving data for method %s', method) | |
439 | return resp |
|
439 | return resp | |
440 |
|
440 | |||
441 | def vcs_stream_view(self, request): |
|
441 | def vcs_stream_view(self, request): | |
442 | payload, remote, method, args, kwargs = self._vcs_view_params(request) |
|
442 | payload, remote, method, args, kwargs = self._vcs_view_params(request) | |
443 | # this method has a stream: marker we remove it here |
|
443 | # this method has a stream: marker we remove it here | |
444 | method = method.split('stream:')[-1] |
|
444 | method = method.split('stream:')[-1] | |
445 | chunk_size = safe_int(payload.get('chunk_size')) or 4096 |
|
445 | chunk_size = safe_int(payload.get('chunk_size')) or 4096 | |
446 |
|
446 | |||
447 | resp = getattr(remote, method)(*args, **kwargs) |
|
447 | resp = getattr(remote, method)(*args, **kwargs) | |
448 |
|
448 | |||
449 | def get_chunked_data(method_resp): |
|
449 | def get_chunked_data(method_resp): | |
450 | stream = io.BytesIO(method_resp) |
|
450 | stream = io.BytesIO(method_resp) | |
451 | while 1: |
|
451 | while 1: | |
452 | chunk = stream.read(chunk_size) |
|
452 | chunk = stream.read(chunk_size) | |
453 | if not chunk: |
|
453 | if not chunk: | |
454 | break |
|
454 | break | |
455 | yield chunk |
|
455 | yield chunk | |
456 |
|
456 | |||
457 | response = Response(app_iter=get_chunked_data(resp)) |
|
457 | response = Response(app_iter=get_chunked_data(resp)) | |
458 | response.content_type = 'application/octet-stream' |
|
458 | response.content_type = 'application/octet-stream' | |
459 |
|
459 | |||
460 | return response |
|
460 | return response | |
461 |
|
461 | |||
462 | def status_view(self, request): |
|
462 | def status_view(self, request): | |
463 | import vcsserver |
|
463 | import vcsserver | |
464 | _platform_id = platform.uname()[1] or 'instance' |
|
464 | _platform_id = platform.uname()[1] or 'instance' | |
465 |
|
465 | |||
466 | return { |
|
466 | return { | |
467 | "status": "OK", |
|
467 | "status": "OK", | |
468 | "vcsserver_version": vcsserver.__version__, |
|
468 | "vcsserver_version": vcsserver.__version__, | |
469 | "platform": _platform_id, |
|
469 | "platform": _platform_id, | |
470 | "pid": os.getpid(), |
|
470 | "pid": os.getpid(), | |
471 | } |
|
471 | } | |
472 |
|
472 | |||
473 | def service_view(self, request): |
|
473 | def service_view(self, request): | |
474 | import vcsserver |
|
474 | import vcsserver | |
475 |
|
475 | |||
476 | payload = msgpack.unpackb(request.body, use_list=True) |
|
476 | payload = msgpack.unpackb(request.body, use_list=True) | |
477 | server_config, app_config = {}, {} |
|
477 | server_config, app_config = {}, {} | |
478 |
|
478 | |||
479 | try: |
|
479 | try: | |
480 | path = self.global_config['__file__'] |
|
480 | path = self.global_config['__file__'] | |
481 | config = configparser.RawConfigParser() |
|
481 | config = configparser.RawConfigParser() | |
482 |
|
482 | |||
483 | config.read(path) |
|
483 | config.read(path) | |
484 |
|
484 | |||
485 | if config.has_section('server:main'): |
|
485 | if config.has_section('server:main'): | |
486 | server_config = dict(config.items('server:main')) |
|
486 | server_config = dict(config.items('server:main')) | |
487 | if config.has_section('app:main'): |
|
487 | if config.has_section('app:main'): | |
488 | app_config = dict(config.items('app:main')) |
|
488 | app_config = dict(config.items('app:main')) | |
489 |
|
489 | |||
490 | except Exception: |
|
490 | except Exception: | |
491 | log.exception('Failed to read .ini file for display') |
|
491 | log.exception('Failed to read .ini file for display') | |
492 |
|
492 | |||
493 | environ = list(os.environ.items()) |
|
493 | environ = list(os.environ.items()) | |
494 |
|
494 | |||
495 | resp = { |
|
495 | resp = { | |
496 | 'id': payload.get('id'), |
|
496 | 'id': payload.get('id'), | |
497 | 'result': dict( |
|
497 | 'result': dict( | |
498 | version=vcsserver.__version__, |
|
498 | version=vcsserver.__version__, | |
499 | config=server_config, |
|
499 | config=server_config, | |
500 | app_config=app_config, |
|
500 | app_config=app_config, | |
501 | environ=environ, |
|
501 | environ=environ, | |
502 | payload=payload, |
|
502 | payload=payload, | |
503 | ) |
|
503 | ) | |
504 | } |
|
504 | } | |
505 | return resp |
|
505 | return resp | |
506 |
|
506 | |||
507 | def _msgpack_renderer_factory(self, info): |
|
507 | def _msgpack_renderer_factory(self, info): | |
508 |
|
508 | |||
509 | def _render(value, system): |
|
509 | def _render(value, system): | |
510 | bin_type = False |
|
510 | bin_type = False | |
511 | res = value.get('result') |
|
511 | res = value.get('result') | |
512 | if isinstance(res, BytesEnvelope): |
|
512 | if isinstance(res, BytesEnvelope): | |
513 | log.debug('Result is wrapped in BytesEnvelope type') |
|
513 | log.debug('Result is wrapped in BytesEnvelope type') | |
514 | bin_type = True |
|
514 | bin_type = True | |
515 | elif isinstance(res, BinaryEnvelope): |
|
515 | elif isinstance(res, BinaryEnvelope): | |
516 | log.debug('Result is wrapped in BinaryEnvelope type') |
|
516 | log.debug('Result is wrapped in BinaryEnvelope type') | |
517 | value['result'] = res.val |
|
517 | value['result'] = res.val | |
518 | bin_type = True |
|
518 | bin_type = True | |
519 |
|
519 | |||
520 | request = system.get('request') |
|
520 | request = system.get('request') | |
521 | if request is not None: |
|
521 | if request is not None: | |
522 | response = request.response |
|
522 | response = request.response | |
523 | ct = response.content_type |
|
523 | ct = response.content_type | |
524 | if ct == response.default_content_type: |
|
524 | if ct == response.default_content_type: | |
525 | response.content_type = 'application/x-msgpack' |
|
525 | response.content_type = 'application/x-msgpack' | |
526 | if bin_type: |
|
526 | if bin_type: | |
527 | response.content_type = 'application/x-msgpack-bin' |
|
527 | response.content_type = 'application/x-msgpack-bin' | |
528 |
|
528 | |||
529 | return msgpack.packb(value, use_bin_type=bin_type) |
|
529 | return msgpack.packb(value, use_bin_type=bin_type) | |
530 | return _render |
|
530 | return _render | |
531 |
|
531 | |||
532 | def set_env_from_config(self, environ, config): |
|
532 | def set_env_from_config(self, environ, config): | |
533 | dict_conf = {} |
|
533 | dict_conf = {} | |
534 | try: |
|
534 | try: | |
535 | for elem in config: |
|
535 | for elem in config: | |
536 | if elem[0] == 'rhodecode': |
|
536 | if elem[0] == 'rhodecode': | |
537 | dict_conf = json.loads(elem[2]) |
|
537 | dict_conf = json.loads(elem[2]) | |
538 | break |
|
538 | break | |
539 | except Exception: |
|
539 | except Exception: | |
540 | log.exception('Failed to fetch SCM CONFIG') |
|
540 | log.exception('Failed to fetch SCM CONFIG') | |
541 | return |
|
541 | return | |
542 |
|
542 | |||
543 | username = dict_conf.get('username') |
|
543 | username = dict_conf.get('username') | |
544 | if username: |
|
544 | if username: | |
545 | environ['REMOTE_USER'] = username |
|
545 | environ['REMOTE_USER'] = username | |
546 | # mercurial specific, some extension api rely on this |
|
546 | # mercurial specific, some extension api rely on this | |
547 | environ['HGUSER'] = username |
|
547 | environ['HGUSER'] = username | |
548 |
|
548 | |||
549 | ip = dict_conf.get('ip') |
|
549 | ip = dict_conf.get('ip') | |
550 | if ip: |
|
550 | if ip: | |
551 | environ['REMOTE_HOST'] = ip |
|
551 | environ['REMOTE_HOST'] = ip | |
552 |
|
552 | |||
553 | if _is_request_chunked(environ): |
|
553 | if _is_request_chunked(environ): | |
554 | # set the compatibility flag for webob |
|
554 | # set the compatibility flag for webob | |
555 | environ['wsgi.input_terminated'] = True |
|
555 | environ['wsgi.input_terminated'] = True | |
556 |
|
556 | |||
557 | def hg_proxy(self): |
|
557 | def hg_proxy(self): | |
558 | @wsgiapp |
|
558 | @wsgiapp | |
559 | def _hg_proxy(environ, start_response): |
|
559 | def _hg_proxy(environ, start_response): | |
560 | app = WsgiProxy(self.remote_wsgi.HgRemoteWsgi()) |
|
560 | app = WsgiProxy(self.remote_wsgi.HgRemoteWsgi()) | |
561 | return app(environ, start_response) |
|
561 | return app(environ, start_response) | |
562 | return _hg_proxy |
|
562 | return _hg_proxy | |
563 |
|
563 | |||
564 | def git_proxy(self): |
|
564 | def git_proxy(self): | |
565 | @wsgiapp |
|
565 | @wsgiapp | |
566 | def _git_proxy(environ, start_response): |
|
566 | def _git_proxy(environ, start_response): | |
567 | app = WsgiProxy(self.remote_wsgi.GitRemoteWsgi()) |
|
567 | app = WsgiProxy(self.remote_wsgi.GitRemoteWsgi()) | |
568 | return app(environ, start_response) |
|
568 | return app(environ, start_response) | |
569 | return _git_proxy |
|
569 | return _git_proxy | |
570 |
|
570 | |||
571 | def hg_stream(self): |
|
571 | def hg_stream(self): | |
572 | if self._use_echo_app: |
|
572 | if self._use_echo_app: | |
573 | @wsgiapp |
|
573 | @wsgiapp | |
574 | def _hg_stream(environ, start_response): |
|
574 | def _hg_stream(environ, start_response): | |
575 | app = EchoApp('fake_path', 'fake_name', None) |
|
575 | app = EchoApp('fake_path', 'fake_name', None) | |
576 | return app(environ, start_response) |
|
576 | return app(environ, start_response) | |
577 | return _hg_stream |
|
577 | return _hg_stream | |
578 | else: |
|
578 | else: | |
579 | @wsgiapp |
|
579 | @wsgiapp | |
580 | def _hg_stream(environ, start_response): |
|
580 | def _hg_stream(environ, start_response): | |
581 | log.debug('http-app: handling hg stream') |
|
581 | log.debug('http-app: handling hg stream') | |
582 | call_context = get_headers_call_context(environ) |
|
582 | call_context = get_headers_call_context(environ) | |
583 |
|
583 | |||
584 | repo_path = call_context['repo_path'] |
|
584 | repo_path = call_context['repo_path'] | |
585 | repo_name = call_context['repo_name'] |
|
585 | repo_name = call_context['repo_name'] | |
586 | config = call_context['repo_config'] |
|
586 | config = call_context['repo_config'] | |
587 |
|
587 | |||
588 | app = scm_app.create_hg_wsgi_app( |
|
588 | app = scm_app.create_hg_wsgi_app( | |
589 | repo_path, repo_name, config) |
|
589 | repo_path, repo_name, config) | |
590 |
|
590 | |||
591 | # Consistent path information for hgweb |
|
591 | # Consistent path information for hgweb | |
592 | environ['PATH_INFO'] = call_context['path_info'] |
|
592 | environ['PATH_INFO'] = call_context['path_info'] | |
593 | environ['REPO_NAME'] = repo_name |
|
593 | environ['REPO_NAME'] = repo_name | |
594 | self.set_env_from_config(environ, config) |
|
594 | self.set_env_from_config(environ, config) | |
595 |
|
595 | |||
596 | log.debug('http-app: starting app handler ' |
|
596 | log.debug('http-app: starting app handler ' | |
597 | 'with %s and process request', app) |
|
597 | 'with %s and process request', app) | |
598 | return app(environ, ResponseFilter(start_response)) |
|
598 | return app(environ, ResponseFilter(start_response)) | |
599 | return _hg_stream |
|
599 | return _hg_stream | |
600 |
|
600 | |||
601 | def git_stream(self): |
|
601 | def git_stream(self): | |
602 | if self._use_echo_app: |
|
602 | if self._use_echo_app: | |
603 | @wsgiapp |
|
603 | @wsgiapp | |
604 | def _git_stream(environ, start_response): |
|
604 | def _git_stream(environ, start_response): | |
605 | app = EchoApp('fake_path', 'fake_name', None) |
|
605 | app = EchoApp('fake_path', 'fake_name', None) | |
606 | return app(environ, start_response) |
|
606 | return app(environ, start_response) | |
607 | return _git_stream |
|
607 | return _git_stream | |
608 | else: |
|
608 | else: | |
609 | @wsgiapp |
|
609 | @wsgiapp | |
610 | def _git_stream(environ, start_response): |
|
610 | def _git_stream(environ, start_response): | |
611 | log.debug('http-app: handling git stream') |
|
611 | log.debug('http-app: handling git stream') | |
612 |
|
612 | |||
613 | call_context = get_headers_call_context(environ) |
|
613 | call_context = get_headers_call_context(environ) | |
614 |
|
614 | |||
615 | repo_path = call_context['repo_path'] |
|
615 | repo_path = call_context['repo_path'] | |
616 | repo_name = call_context['repo_name'] |
|
616 | repo_name = call_context['repo_name'] | |
617 | config = call_context['repo_config'] |
|
617 | config = call_context['repo_config'] | |
618 |
|
618 | |||
619 | environ['PATH_INFO'] = call_context['path_info'] |
|
619 | environ['PATH_INFO'] = call_context['path_info'] | |
620 | self.set_env_from_config(environ, config) |
|
620 | self.set_env_from_config(environ, config) | |
621 |
|
621 | |||
622 | content_type = environ.get('CONTENT_TYPE', '') |
|
622 | content_type = environ.get('CONTENT_TYPE', '') | |
623 |
|
623 | |||
624 | path = environ['PATH_INFO'] |
|
624 | path = environ['PATH_INFO'] | |
625 | is_lfs_request = GIT_LFS_CONTENT_TYPE in content_type |
|
625 | is_lfs_request = GIT_LFS_CONTENT_TYPE in content_type | |
626 | log.debug( |
|
626 | log.debug( | |
627 | 'LFS: Detecting if request `%s` is LFS server path based ' |
|
627 | 'LFS: Detecting if request `%s` is LFS server path based ' | |
628 | 'on content type:`%s`, is_lfs:%s', |
|
628 | 'on content type:`%s`, is_lfs:%s', | |
629 | path, content_type, is_lfs_request) |
|
629 | path, content_type, is_lfs_request) | |
630 |
|
630 | |||
631 | if not is_lfs_request: |
|
631 | if not is_lfs_request: | |
632 | # fallback detection by path |
|
632 | # fallback detection by path | |
633 | if GIT_LFS_PROTO_PAT.match(path): |
|
633 | if GIT_LFS_PROTO_PAT.match(path): | |
634 | is_lfs_request = True |
|
634 | is_lfs_request = True | |
635 | log.debug( |
|
635 | log.debug( | |
636 | 'LFS: fallback detection by path of: `%s`, is_lfs:%s', |
|
636 | 'LFS: fallback detection by path of: `%s`, is_lfs:%s', | |
637 | path, is_lfs_request) |
|
637 | path, is_lfs_request) | |
638 |
|
638 | |||
639 | if is_lfs_request: |
|
639 | if is_lfs_request: | |
640 | app = scm_app.create_git_lfs_wsgi_app( |
|
640 | app = scm_app.create_git_lfs_wsgi_app( | |
641 | repo_path, repo_name, config) |
|
641 | repo_path, repo_name, config) | |
642 | else: |
|
642 | else: | |
643 | app = scm_app.create_git_wsgi_app( |
|
643 | app = scm_app.create_git_wsgi_app( | |
644 | repo_path, repo_name, config) |
|
644 | repo_path, repo_name, config) | |
645 |
|
645 | |||
646 | log.debug('http-app: starting app handler ' |
|
646 | log.debug('http-app: starting app handler ' | |
647 | 'with %s and process request', app) |
|
647 | 'with %s and process request', app) | |
648 |
|
648 | |||
649 | return app(environ, start_response) |
|
649 | return app(environ, start_response) | |
650 |
|
650 | |||
651 | return _git_stream |
|
651 | return _git_stream | |
652 |
|
652 | |||
653 | def handle_vcs_exception(self, exception, request): |
|
653 | def handle_vcs_exception(self, exception, request): | |
654 | _vcs_kind = getattr(exception, '_vcs_kind', '') |
|
654 | _vcs_kind = getattr(exception, '_vcs_kind', '') | |
655 |
|
655 | |||
656 | if _vcs_kind == 'repo_locked': |
|
656 | if _vcs_kind == 'repo_locked': | |
657 | headers_call_context = get_headers_call_context(request.environ) |
|
657 | headers_call_context = get_headers_call_context(request.environ) | |
658 | status_code = safe_int(headers_call_context['locked_status_code']) |
|
658 | status_code = safe_int(headers_call_context['locked_status_code']) | |
659 |
|
659 | |||
660 | return HTTPRepoLocked( |
|
660 | return HTTPRepoLocked( | |
661 | title=str(exception), status_code=status_code, headers=[('X-Rc-Locked', '1')]) |
|
661 | title=str(exception), status_code=status_code, headers=[('X-Rc-Locked', '1')]) | |
662 |
|
662 | |||
663 | elif _vcs_kind == 'repo_branch_protected': |
|
663 | elif _vcs_kind == 'repo_branch_protected': | |
664 | # Get custom repo-branch-protected status code if present. |
|
664 | # Get custom repo-branch-protected status code if present. | |
665 | return HTTPRepoBranchProtected( |
|
665 | return HTTPRepoBranchProtected( | |
666 | title=str(exception), headers=[('X-Rc-Branch-Protection', '1')]) |
|
666 | title=str(exception), headers=[('X-Rc-Branch-Protection', '1')]) | |
667 |
|
667 | |||
668 | exc_info = request.exc_info |
|
668 | exc_info = request.exc_info | |
669 | store_exception(id(exc_info), exc_info) |
|
669 | store_exception(id(exc_info), exc_info) | |
670 |
|
670 | |||
671 | traceback_info = 'unavailable' |
|
671 | traceback_info = 'unavailable' | |
672 | if request.exc_info: |
|
672 | if request.exc_info: | |
673 | traceback_info = format_exc(request.exc_info) |
|
673 | traceback_info = format_exc(request.exc_info) | |
674 |
|
674 | |||
675 | log.error( |
|
675 | log.error( | |
676 | 'error occurred handling this request for path: %s, \n%s', |
|
676 | 'error occurred handling this request for path: %s, \n%s', | |
677 | request.path, traceback_info) |
|
677 | request.path, traceback_info) | |
678 |
|
678 | |||
679 | statsd = request.registry.statsd |
|
679 | statsd = request.registry.statsd | |
680 | if statsd: |
|
680 | if statsd: | |
681 | exc_type = f"{exception.__class__.__module__}.{exception.__class__.__name__}" |
|
681 | exc_type = f"{exception.__class__.__module__}.{exception.__class__.__name__}" | |
682 | statsd.incr('vcsserver_exception_total', |
|
682 | statsd.incr('vcsserver_exception_total', | |
683 | tags=[f"type:{exc_type}"]) |
|
683 | tags=[f"type:{exc_type}"]) | |
684 | raise exception |
|
684 | raise exception | |
685 |
|
685 | |||
686 |
|
686 | |||
687 |
class ResponseFilter |
|
687 | class ResponseFilter: | |
688 |
|
688 | |||
689 | def __init__(self, start_response): |
|
689 | def __init__(self, start_response): | |
690 | self._start_response = start_response |
|
690 | self._start_response = start_response | |
691 |
|
691 | |||
692 | def __call__(self, status, response_headers, exc_info=None): |
|
692 | def __call__(self, status, response_headers, exc_info=None): | |
693 | headers = tuple( |
|
693 | headers = tuple( | |
694 | (h, v) for h, v in response_headers |
|
694 | (h, v) for h, v in response_headers | |
695 | if not wsgiref.util.is_hop_by_hop(h)) |
|
695 | if not wsgiref.util.is_hop_by_hop(h)) | |
696 | return self._start_response(status, headers, exc_info) |
|
696 | return self._start_response(status, headers, exc_info) | |
697 |
|
697 | |||
698 |
|
698 | |||
699 | def sanitize_settings_and_apply_defaults(global_config, settings): |
|
699 | def sanitize_settings_and_apply_defaults(global_config, settings): | |
700 | _global_settings_maker = SettingsMaker(global_config) |
|
700 | _global_settings_maker = SettingsMaker(global_config) | |
701 | settings_maker = SettingsMaker(settings) |
|
701 | settings_maker = SettingsMaker(settings) | |
702 |
|
702 | |||
703 | settings_maker.make_setting('logging.autoconfigure', False, parser='bool') |
|
703 | settings_maker.make_setting('logging.autoconfigure', False, parser='bool') | |
704 |
|
704 | |||
705 | logging_conf = os.path.join(os.path.dirname(global_config.get('__file__')), 'logging.ini') |
|
705 | logging_conf = os.path.join(os.path.dirname(global_config.get('__file__')), 'logging.ini') | |
706 | settings_maker.enable_logging(logging_conf) |
|
706 | settings_maker.enable_logging(logging_conf) | |
707 |
|
707 | |||
708 | # Default includes, possible to change as a user |
|
708 | # Default includes, possible to change as a user | |
709 | pyramid_includes = settings_maker.make_setting('pyramid.includes', [], parser='list:newline') |
|
709 | pyramid_includes = settings_maker.make_setting('pyramid.includes', [], parser='list:newline') | |
710 | log.debug("Using the following pyramid.includes: %s", pyramid_includes) |
|
710 | log.debug("Using the following pyramid.includes: %s", pyramid_includes) | |
711 |
|
711 | |||
712 | settings_maker.make_setting('__file__', global_config.get('__file__')) |
|
712 | settings_maker.make_setting('__file__', global_config.get('__file__')) | |
713 |
|
713 | |||
714 | settings_maker.make_setting('pyramid.default_locale_name', 'en') |
|
714 | settings_maker.make_setting('pyramid.default_locale_name', 'en') | |
715 | settings_maker.make_setting('locale', 'en_US.UTF-8') |
|
715 | settings_maker.make_setting('locale', 'en_US.UTF-8') | |
716 |
|
716 | |||
717 | settings_maker.make_setting('core.binary_dir', '') |
|
717 | settings_maker.make_setting('core.binary_dir', '') | |
718 |
|
718 | |||
719 | temp_store = tempfile.gettempdir() |
|
719 | temp_store = tempfile.gettempdir() | |
720 | default_cache_dir = os.path.join(temp_store, 'rc_cache') |
|
720 | default_cache_dir = os.path.join(temp_store, 'rc_cache') | |
721 | # save default, cache dir, and use it for all backends later. |
|
721 | # save default, cache dir, and use it for all backends later. | |
722 | default_cache_dir = settings_maker.make_setting( |
|
722 | default_cache_dir = settings_maker.make_setting( | |
723 | 'cache_dir', |
|
723 | 'cache_dir', | |
724 | default=default_cache_dir, default_when_empty=True, |
|
724 | default=default_cache_dir, default_when_empty=True, | |
725 | parser='dir:ensured') |
|
725 | parser='dir:ensured') | |
726 |
|
726 | |||
727 | # exception store cache |
|
727 | # exception store cache | |
728 | settings_maker.make_setting( |
|
728 | settings_maker.make_setting( | |
729 | 'exception_tracker.store_path', |
|
729 | 'exception_tracker.store_path', | |
730 | default=os.path.join(default_cache_dir, 'exc_store'), default_when_empty=True, |
|
730 | default=os.path.join(default_cache_dir, 'exc_store'), default_when_empty=True, | |
731 | parser='dir:ensured' |
|
731 | parser='dir:ensured' | |
732 | ) |
|
732 | ) | |
733 |
|
733 | |||
734 | # repo_object cache defaults |
|
734 | # repo_object cache defaults | |
735 | settings_maker.make_setting( |
|
735 | settings_maker.make_setting( | |
736 | 'rc_cache.repo_object.backend', |
|
736 | 'rc_cache.repo_object.backend', | |
737 | default='dogpile.cache.rc.file_namespace', |
|
737 | default='dogpile.cache.rc.file_namespace', | |
738 | parser='string') |
|
738 | parser='string') | |
739 | settings_maker.make_setting( |
|
739 | settings_maker.make_setting( | |
740 | 'rc_cache.repo_object.expiration_time', |
|
740 | 'rc_cache.repo_object.expiration_time', | |
741 | default=30 * 24 * 60 * 60, # 30days |
|
741 | default=30 * 24 * 60 * 60, # 30days | |
742 | parser='int') |
|
742 | parser='int') | |
743 | settings_maker.make_setting( |
|
743 | settings_maker.make_setting( | |
744 | 'rc_cache.repo_object.arguments.filename', |
|
744 | 'rc_cache.repo_object.arguments.filename', | |
745 | default=os.path.join(default_cache_dir, 'vcsserver_cache_repo_object.db'), |
|
745 | default=os.path.join(default_cache_dir, 'vcsserver_cache_repo_object.db'), | |
746 | parser='string') |
|
746 | parser='string') | |
747 |
|
747 | |||
748 | # statsd |
|
748 | # statsd | |
749 | settings_maker.make_setting('statsd.enabled', False, parser='bool') |
|
749 | settings_maker.make_setting('statsd.enabled', False, parser='bool') | |
750 | settings_maker.make_setting('statsd.statsd_host', 'statsd-exporter', parser='string') |
|
750 | settings_maker.make_setting('statsd.statsd_host', 'statsd-exporter', parser='string') | |
751 | settings_maker.make_setting('statsd.statsd_port', 9125, parser='int') |
|
751 | settings_maker.make_setting('statsd.statsd_port', 9125, parser='int') | |
752 | settings_maker.make_setting('statsd.statsd_prefix', '') |
|
752 | settings_maker.make_setting('statsd.statsd_prefix', '') | |
753 | settings_maker.make_setting('statsd.statsd_ipv6', False, parser='bool') |
|
753 | settings_maker.make_setting('statsd.statsd_ipv6', False, parser='bool') | |
754 |
|
754 | |||
755 | settings_maker.env_expand() |
|
755 | settings_maker.env_expand() | |
756 |
|
756 | |||
757 |
|
757 | |||
758 | def main(global_config, **settings): |
|
758 | def main(global_config, **settings): | |
759 | start_time = time.time() |
|
759 | start_time = time.time() | |
760 | log.info('Pyramid app config starting') |
|
760 | log.info('Pyramid app config starting') | |
761 |
|
761 | |||
762 | if MercurialFactory: |
|
762 | if MercurialFactory: | |
763 | hgpatches.patch_largefiles_capabilities() |
|
763 | hgpatches.patch_largefiles_capabilities() | |
764 | hgpatches.patch_subrepo_type_mapping() |
|
764 | hgpatches.patch_subrepo_type_mapping() | |
765 |
|
765 | |||
766 | # Fill in and sanitize the defaults & do ENV expansion |
|
766 | # Fill in and sanitize the defaults & do ENV expansion | |
767 | sanitize_settings_and_apply_defaults(global_config, settings) |
|
767 | sanitize_settings_and_apply_defaults(global_config, settings) | |
768 |
|
768 | |||
769 | # init and bootstrap StatsdClient |
|
769 | # init and bootstrap StatsdClient | |
770 | StatsdClient.setup(settings) |
|
770 | StatsdClient.setup(settings) | |
771 |
|
771 | |||
772 | pyramid_app = HTTPApplication(settings=settings, global_config=global_config).wsgi_app() |
|
772 | pyramid_app = HTTPApplication(settings=settings, global_config=global_config).wsgi_app() | |
773 | total_time = time.time() - start_time |
|
773 | total_time = time.time() - start_time | |
774 | log.info('Pyramid app created and configured in %.2fs', total_time) |
|
774 | log.info('Pyramid app created and configured in %.2fs', total_time) | |
775 | return pyramid_app |
|
775 | return pyramid_app |
@@ -1,394 +1,394 b'' | |||||
1 |
|
1 | |||
2 | import threading |
|
2 | import threading | |
3 | import weakref |
|
3 | import weakref | |
4 | from base64 import b64encode |
|
4 | from base64 import b64encode | |
5 | from logging import getLogger |
|
5 | from logging import getLogger | |
6 | from os import urandom |
|
6 | from os import urandom | |
7 | from typing import Union |
|
7 | from typing import Union | |
8 |
|
8 | |||
9 | from redis import StrictRedis |
|
9 | from redis import StrictRedis | |
10 |
|
10 | |||
11 | __version__ = '4.0.0' |
|
11 | __version__ = '4.0.0' | |
12 |
|
12 | |||
13 | loggers = { |
|
13 | loggers = { | |
14 | k: getLogger("vcsserver." + ".".join((__name__, k))) |
|
14 | k: getLogger("vcsserver." + ".".join((__name__, k))) | |
15 | for k in [ |
|
15 | for k in [ | |
16 | "acquire", |
|
16 | "acquire", | |
17 | "refresh.thread.start", |
|
17 | "refresh.thread.start", | |
18 | "refresh.thread.stop", |
|
18 | "refresh.thread.stop", | |
19 | "refresh.thread.exit", |
|
19 | "refresh.thread.exit", | |
20 | "refresh.start", |
|
20 | "refresh.start", | |
21 | "refresh.shutdown", |
|
21 | "refresh.shutdown", | |
22 | "refresh.exit", |
|
22 | "refresh.exit", | |
23 | "release", |
|
23 | "release", | |
24 | ] |
|
24 | ] | |
25 | } |
|
25 | } | |
26 |
|
26 | |||
27 | text_type = str |
|
27 | text_type = str | |
28 | binary_type = bytes |
|
28 | binary_type = bytes | |
29 |
|
29 | |||
30 |
|
30 | |||
31 | # Check if the id match. If not, return an error code. |
|
31 | # Check if the id match. If not, return an error code. | |
32 | UNLOCK_SCRIPT = b""" |
|
32 | UNLOCK_SCRIPT = b""" | |
33 | if redis.call("get", KEYS[1]) ~= ARGV[1] then |
|
33 | if redis.call("get", KEYS[1]) ~= ARGV[1] then | |
34 | return 1 |
|
34 | return 1 | |
35 | else |
|
35 | else | |
36 | redis.call("del", KEYS[2]) |
|
36 | redis.call("del", KEYS[2]) | |
37 | redis.call("lpush", KEYS[2], 1) |
|
37 | redis.call("lpush", KEYS[2], 1) | |
38 | redis.call("pexpire", KEYS[2], ARGV[2]) |
|
38 | redis.call("pexpire", KEYS[2], ARGV[2]) | |
39 | redis.call("del", KEYS[1]) |
|
39 | redis.call("del", KEYS[1]) | |
40 | return 0 |
|
40 | return 0 | |
41 | end |
|
41 | end | |
42 | """ |
|
42 | """ | |
43 |
|
43 | |||
44 | # Covers both cases when key doesn't exist and doesn't equal to lock's id |
|
44 | # Covers both cases when key doesn't exist and doesn't equal to lock's id | |
45 | EXTEND_SCRIPT = b""" |
|
45 | EXTEND_SCRIPT = b""" | |
46 | if redis.call("get", KEYS[1]) ~= ARGV[1] then |
|
46 | if redis.call("get", KEYS[1]) ~= ARGV[1] then | |
47 | return 1 |
|
47 | return 1 | |
48 | elseif redis.call("ttl", KEYS[1]) < 0 then |
|
48 | elseif redis.call("ttl", KEYS[1]) < 0 then | |
49 | return 2 |
|
49 | return 2 | |
50 | else |
|
50 | else | |
51 | redis.call("expire", KEYS[1], ARGV[2]) |
|
51 | redis.call("expire", KEYS[1], ARGV[2]) | |
52 | return 0 |
|
52 | return 0 | |
53 | end |
|
53 | end | |
54 | """ |
|
54 | """ | |
55 |
|
55 | |||
56 | RESET_SCRIPT = b""" |
|
56 | RESET_SCRIPT = b""" | |
57 | redis.call('del', KEYS[2]) |
|
57 | redis.call('del', KEYS[2]) | |
58 | redis.call('lpush', KEYS[2], 1) |
|
58 | redis.call('lpush', KEYS[2], 1) | |
59 | redis.call('pexpire', KEYS[2], ARGV[2]) |
|
59 | redis.call('pexpire', KEYS[2], ARGV[2]) | |
60 | return redis.call('del', KEYS[1]) |
|
60 | return redis.call('del', KEYS[1]) | |
61 | """ |
|
61 | """ | |
62 |
|
62 | |||
63 | RESET_ALL_SCRIPT = b""" |
|
63 | RESET_ALL_SCRIPT = b""" | |
64 | local locks = redis.call('keys', 'lock:*') |
|
64 | local locks = redis.call('keys', 'lock:*') | |
65 | local signal |
|
65 | local signal | |
66 | for _, lock in pairs(locks) do |
|
66 | for _, lock in pairs(locks) do | |
67 | signal = 'lock-signal:' .. string.sub(lock, 6) |
|
67 | signal = 'lock-signal:' .. string.sub(lock, 6) | |
68 | redis.call('del', signal) |
|
68 | redis.call('del', signal) | |
69 | redis.call('lpush', signal, 1) |
|
69 | redis.call('lpush', signal, 1) | |
70 | redis.call('expire', signal, 1) |
|
70 | redis.call('expire', signal, 1) | |
71 | redis.call('del', lock) |
|
71 | redis.call('del', lock) | |
72 | end |
|
72 | end | |
73 | return #locks |
|
73 | return #locks | |
74 | """ |
|
74 | """ | |
75 |
|
75 | |||
76 |
|
76 | |||
77 | class AlreadyAcquired(RuntimeError): |
|
77 | class AlreadyAcquired(RuntimeError): | |
78 | pass |
|
78 | pass | |
79 |
|
79 | |||
80 |
|
80 | |||
81 | class NotAcquired(RuntimeError): |
|
81 | class NotAcquired(RuntimeError): | |
82 | pass |
|
82 | pass | |
83 |
|
83 | |||
84 |
|
84 | |||
85 | class AlreadyStarted(RuntimeError): |
|
85 | class AlreadyStarted(RuntimeError): | |
86 | pass |
|
86 | pass | |
87 |
|
87 | |||
88 |
|
88 | |||
89 | class TimeoutNotUsable(RuntimeError): |
|
89 | class TimeoutNotUsable(RuntimeError): | |
90 | pass |
|
90 | pass | |
91 |
|
91 | |||
92 |
|
92 | |||
93 | class InvalidTimeout(RuntimeError): |
|
93 | class InvalidTimeout(RuntimeError): | |
94 | pass |
|
94 | pass | |
95 |
|
95 | |||
96 |
|
96 | |||
97 | class TimeoutTooLarge(RuntimeError): |
|
97 | class TimeoutTooLarge(RuntimeError): | |
98 | pass |
|
98 | pass | |
99 |
|
99 | |||
100 |
|
100 | |||
101 | class NotExpirable(RuntimeError): |
|
101 | class NotExpirable(RuntimeError): | |
102 | pass |
|
102 | pass | |
103 |
|
103 | |||
104 |
|
104 | |||
105 |
class Lock |
|
105 | class Lock: | |
106 | """ |
|
106 | """ | |
107 | A Lock context manager implemented via redis SETNX/BLPOP. |
|
107 | A Lock context manager implemented via redis SETNX/BLPOP. | |
108 | """ |
|
108 | """ | |
109 |
|
109 | |||
110 | unlock_script = None |
|
110 | unlock_script = None | |
111 | extend_script = None |
|
111 | extend_script = None | |
112 | reset_script = None |
|
112 | reset_script = None | |
113 | reset_all_script = None |
|
113 | reset_all_script = None | |
114 |
|
114 | |||
115 | _lock_renewal_interval: float |
|
115 | _lock_renewal_interval: float | |
116 | _lock_renewal_thread: Union[threading.Thread, None] |
|
116 | _lock_renewal_thread: Union[threading.Thread, None] | |
117 |
|
117 | |||
118 | def __init__(self, redis_client, name, expire=None, id=None, auto_renewal=False, strict=True, signal_expire=1000): |
|
118 | def __init__(self, redis_client, name, expire=None, id=None, auto_renewal=False, strict=True, signal_expire=1000): | |
119 | """ |
|
119 | """ | |
120 | :param redis_client: |
|
120 | :param redis_client: | |
121 | An instance of :class:`~StrictRedis`. |
|
121 | An instance of :class:`~StrictRedis`. | |
122 | :param name: |
|
122 | :param name: | |
123 | The name (redis key) the lock should have. |
|
123 | The name (redis key) the lock should have. | |
124 | :param expire: |
|
124 | :param expire: | |
125 | The lock expiry time in seconds. If left at the default (None) |
|
125 | The lock expiry time in seconds. If left at the default (None) | |
126 | the lock will not expire. |
|
126 | the lock will not expire. | |
127 | :param id: |
|
127 | :param id: | |
128 | The ID (redis value) the lock should have. A random value is |
|
128 | The ID (redis value) the lock should have. A random value is | |
129 | generated when left at the default. |
|
129 | generated when left at the default. | |
130 |
|
130 | |||
131 | Note that if you specify this then the lock is marked as "held". Acquires |
|
131 | Note that if you specify this then the lock is marked as "held". Acquires | |
132 | won't be possible. |
|
132 | won't be possible. | |
133 | :param auto_renewal: |
|
133 | :param auto_renewal: | |
134 | If set to ``True``, Lock will automatically renew the lock so that it |
|
134 | If set to ``True``, Lock will automatically renew the lock so that it | |
135 | doesn't expire for as long as the lock is held (acquire() called |
|
135 | doesn't expire for as long as the lock is held (acquire() called | |
136 | or running in a context manager). |
|
136 | or running in a context manager). | |
137 |
|
137 | |||
138 | Implementation note: Renewal will happen using a daemon thread with |
|
138 | Implementation note: Renewal will happen using a daemon thread with | |
139 | an interval of ``expire*2/3``. If wishing to use a different renewal |
|
139 | an interval of ``expire*2/3``. If wishing to use a different renewal | |
140 | time, subclass Lock, call ``super().__init__()`` then set |
|
140 | time, subclass Lock, call ``super().__init__()`` then set | |
141 | ``self._lock_renewal_interval`` to your desired interval. |
|
141 | ``self._lock_renewal_interval`` to your desired interval. | |
142 | :param strict: |
|
142 | :param strict: | |
143 | If set ``True`` then the ``redis_client`` needs to be an instance of ``redis.StrictRedis``. |
|
143 | If set ``True`` then the ``redis_client`` needs to be an instance of ``redis.StrictRedis``. | |
144 | :param signal_expire: |
|
144 | :param signal_expire: | |
145 | Advanced option to override signal list expiration in milliseconds. Increase it for very slow clients. Default: ``1000``. |
|
145 | Advanced option to override signal list expiration in milliseconds. Increase it for very slow clients. Default: ``1000``. | |
146 | """ |
|
146 | """ | |
147 | if strict and not isinstance(redis_client, StrictRedis): |
|
147 | if strict and not isinstance(redis_client, StrictRedis): | |
148 | raise ValueError("redis_client must be instance of StrictRedis. " |
|
148 | raise ValueError("redis_client must be instance of StrictRedis. " | |
149 | "Use strict=False if you know what you're doing.") |
|
149 | "Use strict=False if you know what you're doing.") | |
150 | if auto_renewal and expire is None: |
|
150 | if auto_renewal and expire is None: | |
151 | raise ValueError("Expire may not be None when auto_renewal is set") |
|
151 | raise ValueError("Expire may not be None when auto_renewal is set") | |
152 |
|
152 | |||
153 | self._client = redis_client |
|
153 | self._client = redis_client | |
154 |
|
154 | |||
155 | if expire: |
|
155 | if expire: | |
156 | expire = int(expire) |
|
156 | expire = int(expire) | |
157 | if expire < 0: |
|
157 | if expire < 0: | |
158 | raise ValueError("A negative expire is not acceptable.") |
|
158 | raise ValueError("A negative expire is not acceptable.") | |
159 | else: |
|
159 | else: | |
160 | expire = None |
|
160 | expire = None | |
161 | self._expire = expire |
|
161 | self._expire = expire | |
162 |
|
162 | |||
163 | self._signal_expire = signal_expire |
|
163 | self._signal_expire = signal_expire | |
164 | if id is None: |
|
164 | if id is None: | |
165 | self._id = b64encode(urandom(18)).decode('ascii') |
|
165 | self._id = b64encode(urandom(18)).decode('ascii') | |
166 | elif isinstance(id, binary_type): |
|
166 | elif isinstance(id, binary_type): | |
167 | try: |
|
167 | try: | |
168 | self._id = id.decode('ascii') |
|
168 | self._id = id.decode('ascii') | |
169 | except UnicodeDecodeError: |
|
169 | except UnicodeDecodeError: | |
170 | self._id = b64encode(id).decode('ascii') |
|
170 | self._id = b64encode(id).decode('ascii') | |
171 | elif isinstance(id, text_type): |
|
171 | elif isinstance(id, text_type): | |
172 | self._id = id |
|
172 | self._id = id | |
173 | else: |
|
173 | else: | |
174 | raise TypeError(f"Incorrect type for `id`. Must be bytes/str not {type(id)}.") |
|
174 | raise TypeError(f"Incorrect type for `id`. Must be bytes/str not {type(id)}.") | |
175 | self._name = 'lock:' + name |
|
175 | self._name = 'lock:' + name | |
176 | self._signal = 'lock-signal:' + name |
|
176 | self._signal = 'lock-signal:' + name | |
177 | self._lock_renewal_interval = (float(expire) * 2 / 3 |
|
177 | self._lock_renewal_interval = (float(expire) * 2 / 3 | |
178 | if auto_renewal |
|
178 | if auto_renewal | |
179 | else None) |
|
179 | else None) | |
180 | self._lock_renewal_thread = None |
|
180 | self._lock_renewal_thread = None | |
181 |
|
181 | |||
182 | self.register_scripts(redis_client) |
|
182 | self.register_scripts(redis_client) | |
183 |
|
183 | |||
184 | @classmethod |
|
184 | @classmethod | |
185 | def register_scripts(cls, redis_client): |
|
185 | def register_scripts(cls, redis_client): | |
186 | global reset_all_script |
|
186 | global reset_all_script | |
187 | if reset_all_script is None: |
|
187 | if reset_all_script is None: | |
188 | cls.unlock_script = redis_client.register_script(UNLOCK_SCRIPT) |
|
188 | cls.unlock_script = redis_client.register_script(UNLOCK_SCRIPT) | |
189 | cls.extend_script = redis_client.register_script(EXTEND_SCRIPT) |
|
189 | cls.extend_script = redis_client.register_script(EXTEND_SCRIPT) | |
190 | cls.reset_script = redis_client.register_script(RESET_SCRIPT) |
|
190 | cls.reset_script = redis_client.register_script(RESET_SCRIPT) | |
191 | cls.reset_all_script = redis_client.register_script(RESET_ALL_SCRIPT) |
|
191 | cls.reset_all_script = redis_client.register_script(RESET_ALL_SCRIPT) | |
192 | reset_all_script = redis_client.register_script(RESET_ALL_SCRIPT) |
|
192 | reset_all_script = redis_client.register_script(RESET_ALL_SCRIPT) | |
193 |
|
193 | |||
194 | @property |
|
194 | @property | |
195 | def _held(self): |
|
195 | def _held(self): | |
196 | return self.id == self.get_owner_id() |
|
196 | return self.id == self.get_owner_id() | |
197 |
|
197 | |||
198 | def reset(self): |
|
198 | def reset(self): | |
199 | """ |
|
199 | """ | |
200 | Forcibly deletes the lock. Use this with care. |
|
200 | Forcibly deletes the lock. Use this with care. | |
201 | """ |
|
201 | """ | |
202 | self.reset_script(client=self._client, keys=(self._name, self._signal), args=(self.id, self._signal_expire)) |
|
202 | self.reset_script(client=self._client, keys=(self._name, self._signal), args=(self.id, self._signal_expire)) | |
203 |
|
203 | |||
204 | @property |
|
204 | @property | |
205 | def id(self): |
|
205 | def id(self): | |
206 | return self._id |
|
206 | return self._id | |
207 |
|
207 | |||
208 | def get_owner_id(self): |
|
208 | def get_owner_id(self): | |
209 | owner_id = self._client.get(self._name) |
|
209 | owner_id = self._client.get(self._name) | |
210 | if isinstance(owner_id, binary_type): |
|
210 | if isinstance(owner_id, binary_type): | |
211 | owner_id = owner_id.decode('ascii', 'replace') |
|
211 | owner_id = owner_id.decode('ascii', 'replace') | |
212 | return owner_id |
|
212 | return owner_id | |
213 |
|
213 | |||
214 | def acquire(self, blocking=True, timeout=None): |
|
214 | def acquire(self, blocking=True, timeout=None): | |
215 | """ |
|
215 | """ | |
216 | :param blocking: |
|
216 | :param blocking: | |
217 | Boolean value specifying whether lock should be blocking or not. |
|
217 | Boolean value specifying whether lock should be blocking or not. | |
218 | :param timeout: |
|
218 | :param timeout: | |
219 | An integer value specifying the maximum number of seconds to block. |
|
219 | An integer value specifying the maximum number of seconds to block. | |
220 | """ |
|
220 | """ | |
221 | logger = loggers["acquire"] |
|
221 | logger = loggers["acquire"] | |
222 |
|
222 | |||
223 | logger.debug("Getting blocking: %s acquire on %r ...", blocking, self._name) |
|
223 | logger.debug("Getting blocking: %s acquire on %r ...", blocking, self._name) | |
224 |
|
224 | |||
225 | if self._held: |
|
225 | if self._held: | |
226 | owner_id = self.get_owner_id() |
|
226 | owner_id = self.get_owner_id() | |
227 | raise AlreadyAcquired("Already acquired from this Lock instance. Lock id: {}".format(owner_id)) |
|
227 | raise AlreadyAcquired("Already acquired from this Lock instance. Lock id: {}".format(owner_id)) | |
228 |
|
228 | |||
229 | if not blocking and timeout is not None: |
|
229 | if not blocking and timeout is not None: | |
230 | raise TimeoutNotUsable("Timeout cannot be used if blocking=False") |
|
230 | raise TimeoutNotUsable("Timeout cannot be used if blocking=False") | |
231 |
|
231 | |||
232 | if timeout: |
|
232 | if timeout: | |
233 | timeout = int(timeout) |
|
233 | timeout = int(timeout) | |
234 | if timeout < 0: |
|
234 | if timeout < 0: | |
235 | raise InvalidTimeout(f"Timeout ({timeout}) cannot be less than or equal to 0") |
|
235 | raise InvalidTimeout(f"Timeout ({timeout}) cannot be less than or equal to 0") | |
236 |
|
236 | |||
237 | if self._expire and not self._lock_renewal_interval and timeout > self._expire: |
|
237 | if self._expire and not self._lock_renewal_interval and timeout > self._expire: | |
238 | raise TimeoutTooLarge(f"Timeout ({timeout}) cannot be greater than expire ({self._expire})") |
|
238 | raise TimeoutTooLarge(f"Timeout ({timeout}) cannot be greater than expire ({self._expire})") | |
239 |
|
239 | |||
240 | busy = True |
|
240 | busy = True | |
241 | blpop_timeout = timeout or self._expire or 0 |
|
241 | blpop_timeout = timeout or self._expire or 0 | |
242 | timed_out = False |
|
242 | timed_out = False | |
243 | while busy: |
|
243 | while busy: | |
244 | busy = not self._client.set(self._name, self._id, nx=True, ex=self._expire) |
|
244 | busy = not self._client.set(self._name, self._id, nx=True, ex=self._expire) | |
245 | if busy: |
|
245 | if busy: | |
246 | if timed_out: |
|
246 | if timed_out: | |
247 | return False |
|
247 | return False | |
248 | elif blocking: |
|
248 | elif blocking: | |
249 | timed_out = not self._client.blpop(self._signal, blpop_timeout) and timeout |
|
249 | timed_out = not self._client.blpop(self._signal, blpop_timeout) and timeout | |
250 | else: |
|
250 | else: | |
251 | logger.warning("Failed to acquire Lock(%r).", self._name) |
|
251 | logger.warning("Failed to acquire Lock(%r).", self._name) | |
252 | return False |
|
252 | return False | |
253 |
|
253 | |||
254 | logger.debug("Acquired Lock(%r).", self._name) |
|
254 | logger.debug("Acquired Lock(%r).", self._name) | |
255 | if self._lock_renewal_interval is not None: |
|
255 | if self._lock_renewal_interval is not None: | |
256 | self._start_lock_renewer() |
|
256 | self._start_lock_renewer() | |
257 | return True |
|
257 | return True | |
258 |
|
258 | |||
259 | def extend(self, expire=None): |
|
259 | def extend(self, expire=None): | |
260 | """ |
|
260 | """ | |
261 | Extends expiration time of the lock. |
|
261 | Extends expiration time of the lock. | |
262 |
|
262 | |||
263 | :param expire: |
|
263 | :param expire: | |
264 | New expiration time. If ``None`` - `expire` provided during |
|
264 | New expiration time. If ``None`` - `expire` provided during | |
265 | lock initialization will be taken. |
|
265 | lock initialization will be taken. | |
266 | """ |
|
266 | """ | |
267 | if expire: |
|
267 | if expire: | |
268 | expire = int(expire) |
|
268 | expire = int(expire) | |
269 | if expire < 0: |
|
269 | if expire < 0: | |
270 | raise ValueError("A negative expire is not acceptable.") |
|
270 | raise ValueError("A negative expire is not acceptable.") | |
271 | elif self._expire is not None: |
|
271 | elif self._expire is not None: | |
272 | expire = self._expire |
|
272 | expire = self._expire | |
273 | else: |
|
273 | else: | |
274 | raise TypeError( |
|
274 | raise TypeError( | |
275 | "To extend a lock 'expire' must be provided as an " |
|
275 | "To extend a lock 'expire' must be provided as an " | |
276 | "argument to extend() method or at initialization time." |
|
276 | "argument to extend() method or at initialization time." | |
277 | ) |
|
277 | ) | |
278 |
|
278 | |||
279 | error = self.extend_script(client=self._client, keys=(self._name, self._signal), args=(self._id, expire)) |
|
279 | error = self.extend_script(client=self._client, keys=(self._name, self._signal), args=(self._id, expire)) | |
280 | if error == 1: |
|
280 | if error == 1: | |
281 | raise NotAcquired(f"Lock {self._name} is not acquired or it already expired.") |
|
281 | raise NotAcquired(f"Lock {self._name} is not acquired or it already expired.") | |
282 | elif error == 2: |
|
282 | elif error == 2: | |
283 | raise NotExpirable(f"Lock {self._name} has no assigned expiration time") |
|
283 | raise NotExpirable(f"Lock {self._name} has no assigned expiration time") | |
284 | elif error: |
|
284 | elif error: | |
285 | raise RuntimeError(f"Unsupported error code {error} from EXTEND script") |
|
285 | raise RuntimeError(f"Unsupported error code {error} from EXTEND script") | |
286 |
|
286 | |||
287 | @staticmethod |
|
287 | @staticmethod | |
288 | def _lock_renewer(name, lockref, interval, stop): |
|
288 | def _lock_renewer(name, lockref, interval, stop): | |
289 | """ |
|
289 | """ | |
290 | Renew the lock key in redis every `interval` seconds for as long |
|
290 | Renew the lock key in redis every `interval` seconds for as long | |
291 | as `self._lock_renewal_thread.should_exit` is False. |
|
291 | as `self._lock_renewal_thread.should_exit` is False. | |
292 | """ |
|
292 | """ | |
293 | while not stop.wait(timeout=interval): |
|
293 | while not stop.wait(timeout=interval): | |
294 | loggers["refresh.thread.start"].debug("Refreshing Lock(%r).", name) |
|
294 | loggers["refresh.thread.start"].debug("Refreshing Lock(%r).", name) | |
295 | lock: "Lock" = lockref() |
|
295 | lock: "Lock" = lockref() | |
296 | if lock is None: |
|
296 | if lock is None: | |
297 | loggers["refresh.thread.stop"].debug( |
|
297 | loggers["refresh.thread.stop"].debug( | |
298 | "Stopping loop because Lock(%r) was garbage collected.", name |
|
298 | "Stopping loop because Lock(%r) was garbage collected.", name | |
299 | ) |
|
299 | ) | |
300 | break |
|
300 | break | |
301 | lock.extend(expire=lock._expire) |
|
301 | lock.extend(expire=lock._expire) | |
302 | del lock |
|
302 | del lock | |
303 | loggers["refresh.thread.exit"].debug("Exiting renewal thread for Lock(%r).", name) |
|
303 | loggers["refresh.thread.exit"].debug("Exiting renewal thread for Lock(%r).", name) | |
304 |
|
304 | |||
305 | def _start_lock_renewer(self): |
|
305 | def _start_lock_renewer(self): | |
306 | """ |
|
306 | """ | |
307 | Starts the lock refresher thread. |
|
307 | Starts the lock refresher thread. | |
308 | """ |
|
308 | """ | |
309 | if self._lock_renewal_thread is not None: |
|
309 | if self._lock_renewal_thread is not None: | |
310 | raise AlreadyStarted("Lock refresh thread already started") |
|
310 | raise AlreadyStarted("Lock refresh thread already started") | |
311 |
|
311 | |||
312 | loggers["refresh.start"].debug( |
|
312 | loggers["refresh.start"].debug( | |
313 | "Starting renewal thread for Lock(%r). Refresh interval: %s seconds.", |
|
313 | "Starting renewal thread for Lock(%r). Refresh interval: %s seconds.", | |
314 | self._name, self._lock_renewal_interval |
|
314 | self._name, self._lock_renewal_interval | |
315 | ) |
|
315 | ) | |
316 | self._lock_renewal_stop = threading.Event() |
|
316 | self._lock_renewal_stop = threading.Event() | |
317 | self._lock_renewal_thread = threading.Thread( |
|
317 | self._lock_renewal_thread = threading.Thread( | |
318 | group=None, |
|
318 | group=None, | |
319 | target=self._lock_renewer, |
|
319 | target=self._lock_renewer, | |
320 | kwargs={ |
|
320 | kwargs={ | |
321 | 'name': self._name, |
|
321 | 'name': self._name, | |
322 | 'lockref': weakref.ref(self), |
|
322 | 'lockref': weakref.ref(self), | |
323 | 'interval': self._lock_renewal_interval, |
|
323 | 'interval': self._lock_renewal_interval, | |
324 | 'stop': self._lock_renewal_stop, |
|
324 | 'stop': self._lock_renewal_stop, | |
325 | }, |
|
325 | }, | |
326 | ) |
|
326 | ) | |
327 | self._lock_renewal_thread.daemon = True |
|
327 | self._lock_renewal_thread.daemon = True | |
328 | self._lock_renewal_thread.start() |
|
328 | self._lock_renewal_thread.start() | |
329 |
|
329 | |||
330 | def _stop_lock_renewer(self): |
|
330 | def _stop_lock_renewer(self): | |
331 | """ |
|
331 | """ | |
332 | Stop the lock renewer. |
|
332 | Stop the lock renewer. | |
333 |
|
333 | |||
334 | This signals the renewal thread and waits for its exit. |
|
334 | This signals the renewal thread and waits for its exit. | |
335 | """ |
|
335 | """ | |
336 | if self._lock_renewal_thread is None or not self._lock_renewal_thread.is_alive(): |
|
336 | if self._lock_renewal_thread is None or not self._lock_renewal_thread.is_alive(): | |
337 | return |
|
337 | return | |
338 | loggers["refresh.shutdown"].debug("Signaling renewal thread for Lock(%r) to exit.", self._name) |
|
338 | loggers["refresh.shutdown"].debug("Signaling renewal thread for Lock(%r) to exit.", self._name) | |
339 | self._lock_renewal_stop.set() |
|
339 | self._lock_renewal_stop.set() | |
340 | self._lock_renewal_thread.join() |
|
340 | self._lock_renewal_thread.join() | |
341 | self._lock_renewal_thread = None |
|
341 | self._lock_renewal_thread = None | |
342 | loggers["refresh.exit"].debug("Renewal thread for Lock(%r) exited.", self._name) |
|
342 | loggers["refresh.exit"].debug("Renewal thread for Lock(%r) exited.", self._name) | |
343 |
|
343 | |||
344 | def __enter__(self): |
|
344 | def __enter__(self): | |
345 | acquired = self.acquire(blocking=True) |
|
345 | acquired = self.acquire(blocking=True) | |
346 | if not acquired: |
|
346 | if not acquired: | |
347 | raise AssertionError(f"Lock({self._name}) wasn't acquired, but blocking=True was used!") |
|
347 | raise AssertionError(f"Lock({self._name}) wasn't acquired, but blocking=True was used!") | |
348 | return self |
|
348 | return self | |
349 |
|
349 | |||
350 | def __exit__(self, exc_type=None, exc_value=None, traceback=None): |
|
350 | def __exit__(self, exc_type=None, exc_value=None, traceback=None): | |
351 | self.release() |
|
351 | self.release() | |
352 |
|
352 | |||
353 | def release(self): |
|
353 | def release(self): | |
354 | """Releases the lock, that was acquired with the same object. |
|
354 | """Releases the lock, that was acquired with the same object. | |
355 |
|
355 | |||
356 | .. note:: |
|
356 | .. note:: | |
357 |
|
357 | |||
358 | If you want to release a lock that you acquired in a different place you have two choices: |
|
358 | If you want to release a lock that you acquired in a different place you have two choices: | |
359 |
|
359 | |||
360 | * Use ``Lock("name", id=id_from_other_place).release()`` |
|
360 | * Use ``Lock("name", id=id_from_other_place).release()`` | |
361 | * Use ``Lock("name").reset()`` |
|
361 | * Use ``Lock("name").reset()`` | |
362 | """ |
|
362 | """ | |
363 | if self._lock_renewal_thread is not None: |
|
363 | if self._lock_renewal_thread is not None: | |
364 | self._stop_lock_renewer() |
|
364 | self._stop_lock_renewer() | |
365 | loggers["release"].debug("Releasing Lock(%r).", self._name) |
|
365 | loggers["release"].debug("Releasing Lock(%r).", self._name) | |
366 | error = self.unlock_script(client=self._client, keys=(self._name, self._signal), args=(self._id, self._signal_expire)) |
|
366 | error = self.unlock_script(client=self._client, keys=(self._name, self._signal), args=(self._id, self._signal_expire)) | |
367 | if error == 1: |
|
367 | if error == 1: | |
368 | raise NotAcquired(f"Lock({self._name}) is not acquired or it already expired.") |
|
368 | raise NotAcquired(f"Lock({self._name}) is not acquired or it already expired.") | |
369 | elif error: |
|
369 | elif error: | |
370 | raise RuntimeError(f"Unsupported error code {error} from EXTEND script.") |
|
370 | raise RuntimeError(f"Unsupported error code {error} from EXTEND script.") | |
371 |
|
371 | |||
372 | def locked(self): |
|
372 | def locked(self): | |
373 | """ |
|
373 | """ | |
374 | Return true if the lock is acquired. |
|
374 | Return true if the lock is acquired. | |
375 |
|
375 | |||
376 | Checks that lock with same name already exists. This method returns true, even if |
|
376 | Checks that lock with same name already exists. This method returns true, even if | |
377 | lock have another id. |
|
377 | lock have another id. | |
378 | """ |
|
378 | """ | |
379 | return self._client.exists(self._name) == 1 |
|
379 | return self._client.exists(self._name) == 1 | |
380 |
|
380 | |||
381 |
|
381 | |||
382 | reset_all_script = None |
|
382 | reset_all_script = None | |
383 |
|
383 | |||
384 |
|
384 | |||
385 | def reset_all(redis_client): |
|
385 | def reset_all(redis_client): | |
386 | """ |
|
386 | """ | |
387 | Forcibly deletes all locks if its remains (like a crash reason). Use this with care. |
|
387 | Forcibly deletes all locks if its remains (like a crash reason). Use this with care. | |
388 |
|
388 | |||
389 | :param redis_client: |
|
389 | :param redis_client: | |
390 | An instance of :class:`~StrictRedis`. |
|
390 | An instance of :class:`~StrictRedis`. | |
391 | """ |
|
391 | """ | |
392 | Lock.register_scripts(redis_client) |
|
392 | Lock.register_scripts(redis_client) | |
393 |
|
393 | |||
394 | reset_all_script(client=redis_client) # noqa |
|
394 | reset_all_script(client=redis_client) # noqa |
@@ -1,154 +1,154 b'' | |||||
1 | import re |
|
1 | import re | |
2 | import random |
|
2 | import random | |
3 | from collections import deque |
|
3 | from collections import deque | |
4 | from datetime import timedelta |
|
4 | from datetime import timedelta | |
5 | from repoze.lru import lru_cache |
|
5 | from repoze.lru import lru_cache | |
6 |
|
6 | |||
7 | from .timer import Timer |
|
7 | from .timer import Timer | |
8 |
|
8 | |||
9 | TAG_INVALID_CHARS_RE = re.compile( |
|
9 | TAG_INVALID_CHARS_RE = re.compile( | |
10 | r"[^\w\d_\-:/\.]", |
|
10 | r"[^\w\d_\-:/\.]", | |
11 | #re.UNICODE |
|
11 | #re.UNICODE | |
12 | ) |
|
12 | ) | |
13 | TAG_INVALID_CHARS_SUBS = "_" |
|
13 | TAG_INVALID_CHARS_SUBS = "_" | |
14 |
|
14 | |||
15 | # we save and expose methods called by statsd for discovery |
|
15 | # we save and expose methods called by statsd for discovery | |
16 | buckets_dict = { |
|
16 | buckets_dict = { | |
17 |
|
17 | |||
18 | } |
|
18 | } | |
19 |
|
19 | |||
20 |
|
20 | |||
21 | @lru_cache(maxsize=500) |
|
21 | @lru_cache(maxsize=500) | |
22 | def _normalize_tags_with_cache(tag_list): |
|
22 | def _normalize_tags_with_cache(tag_list): | |
23 | return [TAG_INVALID_CHARS_RE.sub(TAG_INVALID_CHARS_SUBS, tag) for tag in tag_list] |
|
23 | return [TAG_INVALID_CHARS_RE.sub(TAG_INVALID_CHARS_SUBS, tag) for tag in tag_list] | |
24 |
|
24 | |||
25 |
|
25 | |||
26 | def normalize_tags(tag_list): |
|
26 | def normalize_tags(tag_list): | |
27 | # We have to turn our input tag list into a non-mutable tuple for it to |
|
27 | # We have to turn our input tag list into a non-mutable tuple for it to | |
28 | # be hashable (and thus usable) by the @lru_cache decorator. |
|
28 | # be hashable (and thus usable) by the @lru_cache decorator. | |
29 | return _normalize_tags_with_cache(tuple(tag_list)) |
|
29 | return _normalize_tags_with_cache(tuple(tag_list)) | |
30 |
|
30 | |||
31 |
|
31 | |||
32 |
class StatsClientBase |
|
32 | class StatsClientBase: | |
33 | """A Base class for various statsd clients.""" |
|
33 | """A Base class for various statsd clients.""" | |
34 |
|
34 | |||
35 | def close(self): |
|
35 | def close(self): | |
36 | """Used to close and clean up any underlying resources.""" |
|
36 | """Used to close and clean up any underlying resources.""" | |
37 | raise NotImplementedError() |
|
37 | raise NotImplementedError() | |
38 |
|
38 | |||
39 | def _send(self): |
|
39 | def _send(self): | |
40 | raise NotImplementedError() |
|
40 | raise NotImplementedError() | |
41 |
|
41 | |||
42 | def pipeline(self): |
|
42 | def pipeline(self): | |
43 | raise NotImplementedError() |
|
43 | raise NotImplementedError() | |
44 |
|
44 | |||
45 | def timer(self, stat, rate=1, tags=None, auto_send=True): |
|
45 | def timer(self, stat, rate=1, tags=None, auto_send=True): | |
46 | """ |
|
46 | """ | |
47 | statsd = StatsdClient.statsd |
|
47 | statsd = StatsdClient.statsd | |
48 | with statsd.timer('bucket_name', auto_send=True) as tmr: |
|
48 | with statsd.timer('bucket_name', auto_send=True) as tmr: | |
49 | # This block will be timed. |
|
49 | # This block will be timed. | |
50 | for i in range(0, 100000): |
|
50 | for i in range(0, 100000): | |
51 | i ** 2 |
|
51 | i ** 2 | |
52 | # you can access time here... |
|
52 | # you can access time here... | |
53 | elapsed_ms = tmr.ms |
|
53 | elapsed_ms = tmr.ms | |
54 | """ |
|
54 | """ | |
55 | return Timer(self, stat, rate, tags, auto_send=auto_send) |
|
55 | return Timer(self, stat, rate, tags, auto_send=auto_send) | |
56 |
|
56 | |||
57 | def timing(self, stat, delta, rate=1, tags=None, use_decimals=True): |
|
57 | def timing(self, stat, delta, rate=1, tags=None, use_decimals=True): | |
58 | """ |
|
58 | """ | |
59 | Send new timing information. |
|
59 | Send new timing information. | |
60 |
|
60 | |||
61 | `delta` can be either a number of milliseconds or a timedelta. |
|
61 | `delta` can be either a number of milliseconds or a timedelta. | |
62 | """ |
|
62 | """ | |
63 | if isinstance(delta, timedelta): |
|
63 | if isinstance(delta, timedelta): | |
64 | # Convert timedelta to number of milliseconds. |
|
64 | # Convert timedelta to number of milliseconds. | |
65 | delta = delta.total_seconds() * 1000. |
|
65 | delta = delta.total_seconds() * 1000. | |
66 | if use_decimals: |
|
66 | if use_decimals: | |
67 | fmt = '%0.6f|ms' |
|
67 | fmt = '%0.6f|ms' | |
68 | else: |
|
68 | else: | |
69 | fmt = '%s|ms' |
|
69 | fmt = '%s|ms' | |
70 | self._send_stat(stat, fmt % delta, rate, tags) |
|
70 | self._send_stat(stat, fmt % delta, rate, tags) | |
71 |
|
71 | |||
72 | def incr(self, stat, count=1, rate=1, tags=None): |
|
72 | def incr(self, stat, count=1, rate=1, tags=None): | |
73 | """Increment a stat by `count`.""" |
|
73 | """Increment a stat by `count`.""" | |
74 | self._send_stat(stat, '%s|c' % count, rate, tags) |
|
74 | self._send_stat(stat, '%s|c' % count, rate, tags) | |
75 |
|
75 | |||
76 | def decr(self, stat, count=1, rate=1, tags=None): |
|
76 | def decr(self, stat, count=1, rate=1, tags=None): | |
77 | """Decrement a stat by `count`.""" |
|
77 | """Decrement a stat by `count`.""" | |
78 | self.incr(stat, -count, rate, tags) |
|
78 | self.incr(stat, -count, rate, tags) | |
79 |
|
79 | |||
80 | def gauge(self, stat, value, rate=1, delta=False, tags=None): |
|
80 | def gauge(self, stat, value, rate=1, delta=False, tags=None): | |
81 | """Set a gauge value.""" |
|
81 | """Set a gauge value.""" | |
82 | if value < 0 and not delta: |
|
82 | if value < 0 and not delta: | |
83 | if rate < 1: |
|
83 | if rate < 1: | |
84 | if random.random() > rate: |
|
84 | if random.random() > rate: | |
85 | return |
|
85 | return | |
86 | with self.pipeline() as pipe: |
|
86 | with self.pipeline() as pipe: | |
87 | pipe._send_stat(stat, '0|g', 1) |
|
87 | pipe._send_stat(stat, '0|g', 1) | |
88 | pipe._send_stat(stat, '%s|g' % value, 1) |
|
88 | pipe._send_stat(stat, '%s|g' % value, 1) | |
89 | else: |
|
89 | else: | |
90 | prefix = '+' if delta and value >= 0 else '' |
|
90 | prefix = '+' if delta and value >= 0 else '' | |
91 | self._send_stat(stat, '%s%s|g' % (prefix, value), rate, tags) |
|
91 | self._send_stat(stat, '%s%s|g' % (prefix, value), rate, tags) | |
92 |
|
92 | |||
93 | def set(self, stat, value, rate=1): |
|
93 | def set(self, stat, value, rate=1): | |
94 | """Set a set value.""" |
|
94 | """Set a set value.""" | |
95 | self._send_stat(stat, '%s|s' % value, rate) |
|
95 | self._send_stat(stat, '%s|s' % value, rate) | |
96 |
|
96 | |||
97 | def histogram(self, stat, value, rate=1, tags=None): |
|
97 | def histogram(self, stat, value, rate=1, tags=None): | |
98 | """Set a histogram""" |
|
98 | """Set a histogram""" | |
99 | self._send_stat(stat, '%s|h' % value, rate, tags) |
|
99 | self._send_stat(stat, '%s|h' % value, rate, tags) | |
100 |
|
100 | |||
101 | def _send_stat(self, stat, value, rate, tags=None): |
|
101 | def _send_stat(self, stat, value, rate, tags=None): | |
102 | self._after(self._prepare(stat, value, rate, tags)) |
|
102 | self._after(self._prepare(stat, value, rate, tags)) | |
103 |
|
103 | |||
104 | def _prepare(self, stat, value, rate, tags=None): |
|
104 | def _prepare(self, stat, value, rate, tags=None): | |
105 | global buckets_dict |
|
105 | global buckets_dict | |
106 | buckets_dict[stat] = 1 |
|
106 | buckets_dict[stat] = 1 | |
107 |
|
107 | |||
108 | if rate < 1: |
|
108 | if rate < 1: | |
109 | if random.random() > rate: |
|
109 | if random.random() > rate: | |
110 | return |
|
110 | return | |
111 | value = '%s|@%s' % (value, rate) |
|
111 | value = '%s|@%s' % (value, rate) | |
112 |
|
112 | |||
113 | if self._prefix: |
|
113 | if self._prefix: | |
114 | stat = '%s.%s' % (self._prefix, stat) |
|
114 | stat = '%s.%s' % (self._prefix, stat) | |
115 |
|
115 | |||
116 | res = '%s:%s%s' % ( |
|
116 | res = '%s:%s%s' % ( | |
117 | stat, |
|
117 | stat, | |
118 | value, |
|
118 | value, | |
119 | ("|#" + ",".join(normalize_tags(tags))) if tags else "", |
|
119 | ("|#" + ",".join(normalize_tags(tags))) if tags else "", | |
120 | ) |
|
120 | ) | |
121 | return res |
|
121 | return res | |
122 |
|
122 | |||
123 | def _after(self, data): |
|
123 | def _after(self, data): | |
124 | if data: |
|
124 | if data: | |
125 | self._send(data) |
|
125 | self._send(data) | |
126 |
|
126 | |||
127 |
|
127 | |||
128 | class PipelineBase(StatsClientBase): |
|
128 | class PipelineBase(StatsClientBase): | |
129 |
|
129 | |||
130 | def __init__(self, client): |
|
130 | def __init__(self, client): | |
131 | self._client = client |
|
131 | self._client = client | |
132 | self._prefix = client._prefix |
|
132 | self._prefix = client._prefix | |
133 | self._stats = deque() |
|
133 | self._stats = deque() | |
134 |
|
134 | |||
135 | def _send(self): |
|
135 | def _send(self): | |
136 | raise NotImplementedError() |
|
136 | raise NotImplementedError() | |
137 |
|
137 | |||
138 | def _after(self, data): |
|
138 | def _after(self, data): | |
139 | if data is not None: |
|
139 | if data is not None: | |
140 | self._stats.append(data) |
|
140 | self._stats.append(data) | |
141 |
|
141 | |||
142 | def __enter__(self): |
|
142 | def __enter__(self): | |
143 | return self |
|
143 | return self | |
144 |
|
144 | |||
145 | def __exit__(self, typ, value, tb): |
|
145 | def __exit__(self, typ, value, tb): | |
146 | self.send() |
|
146 | self.send() | |
147 |
|
147 | |||
148 | def send(self): |
|
148 | def send(self): | |
149 | if not self._stats: |
|
149 | if not self._stats: | |
150 | return |
|
150 | return | |
151 | self._send() |
|
151 | self._send() | |
152 |
|
152 | |||
153 | def pipeline(self): |
|
153 | def pipeline(self): | |
154 | return self.__class__(self) |
|
154 | return self.__class__(self) |
@@ -1,66 +1,66 b'' | |||||
1 | import functools |
|
1 | import functools | |
2 | from time import perf_counter as time_now |
|
2 | from time import perf_counter as time_now | |
3 |
|
3 | |||
4 |
|
4 | |||
5 | def safe_wraps(wrapper, *args, **kwargs): |
|
5 | def safe_wraps(wrapper, *args, **kwargs): | |
6 | """Safely wraps partial functions.""" |
|
6 | """Safely wraps partial functions.""" | |
7 | while isinstance(wrapper, functools.partial): |
|
7 | while isinstance(wrapper, functools.partial): | |
8 | wrapper = wrapper.func |
|
8 | wrapper = wrapper.func | |
9 | return functools.wraps(wrapper, *args, **kwargs) |
|
9 | return functools.wraps(wrapper, *args, **kwargs) | |
10 |
|
10 | |||
11 |
|
11 | |||
12 |
class Timer |
|
12 | class Timer: | |
13 | """A context manager/decorator for statsd.timing().""" |
|
13 | """A context manager/decorator for statsd.timing().""" | |
14 |
|
14 | |||
15 | def __init__(self, client, stat, rate=1, tags=None, use_decimals=True, auto_send=True): |
|
15 | def __init__(self, client, stat, rate=1, tags=None, use_decimals=True, auto_send=True): | |
16 | self.client = client |
|
16 | self.client = client | |
17 | self.stat = stat |
|
17 | self.stat = stat | |
18 | self.rate = rate |
|
18 | self.rate = rate | |
19 | self.tags = tags |
|
19 | self.tags = tags | |
20 | self.ms = None |
|
20 | self.ms = None | |
21 | self._sent = False |
|
21 | self._sent = False | |
22 | self._start_time = None |
|
22 | self._start_time = None | |
23 | self.use_decimals = use_decimals |
|
23 | self.use_decimals = use_decimals | |
24 | self.auto_send = auto_send |
|
24 | self.auto_send = auto_send | |
25 |
|
25 | |||
26 | def __call__(self, f): |
|
26 | def __call__(self, f): | |
27 | """Thread-safe timing function decorator.""" |
|
27 | """Thread-safe timing function decorator.""" | |
28 | @safe_wraps(f) |
|
28 | @safe_wraps(f) | |
29 | def _wrapped(*args, **kwargs): |
|
29 | def _wrapped(*args, **kwargs): | |
30 | start_time = time_now() |
|
30 | start_time = time_now() | |
31 | try: |
|
31 | try: | |
32 | return f(*args, **kwargs) |
|
32 | return f(*args, **kwargs) | |
33 | finally: |
|
33 | finally: | |
34 | elapsed_time_ms = 1000.0 * (time_now() - start_time) |
|
34 | elapsed_time_ms = 1000.0 * (time_now() - start_time) | |
35 | self.client.timing(self.stat, elapsed_time_ms, self.rate, self.tags, self.use_decimals) |
|
35 | self.client.timing(self.stat, elapsed_time_ms, self.rate, self.tags, self.use_decimals) | |
36 | self._sent = True |
|
36 | self._sent = True | |
37 | return _wrapped |
|
37 | return _wrapped | |
38 |
|
38 | |||
39 | def __enter__(self): |
|
39 | def __enter__(self): | |
40 | return self.start() |
|
40 | return self.start() | |
41 |
|
41 | |||
42 | def __exit__(self, typ, value, tb): |
|
42 | def __exit__(self, typ, value, tb): | |
43 | self.stop(send=self.auto_send) |
|
43 | self.stop(send=self.auto_send) | |
44 |
|
44 | |||
45 | def start(self): |
|
45 | def start(self): | |
46 | self.ms = None |
|
46 | self.ms = None | |
47 | self._sent = False |
|
47 | self._sent = False | |
48 | self._start_time = time_now() |
|
48 | self._start_time = time_now() | |
49 | return self |
|
49 | return self | |
50 |
|
50 | |||
51 | def stop(self, send=True): |
|
51 | def stop(self, send=True): | |
52 | if self._start_time is None: |
|
52 | if self._start_time is None: | |
53 | raise RuntimeError('Timer has not started.') |
|
53 | raise RuntimeError('Timer has not started.') | |
54 | dt = time_now() - self._start_time |
|
54 | dt = time_now() - self._start_time | |
55 | self.ms = 1000.0 * dt # Convert to milliseconds. |
|
55 | self.ms = 1000.0 * dt # Convert to milliseconds. | |
56 | if send: |
|
56 | if send: | |
57 | self.send() |
|
57 | self.send() | |
58 | return self |
|
58 | return self | |
59 |
|
59 | |||
60 | def send(self): |
|
60 | def send(self): | |
61 | if self.ms is None: |
|
61 | if self.ms is None: | |
62 | raise RuntimeError('No data recorded.') |
|
62 | raise RuntimeError('No data recorded.') | |
63 | if self._sent: |
|
63 | if self._sent: | |
64 | raise RuntimeError('Already sent data.') |
|
64 | raise RuntimeError('Already sent data.') | |
65 | self._sent = True |
|
65 | self._sent = True | |
66 | self.client.timing(self.stat, self.ms, self.rate, self.tags, self.use_decimals) |
|
66 | self.client.timing(self.stat, self.ms, self.rate, self.tags, self.use_decimals) |
@@ -1,267 +1,267 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | #import errno |
|
18 | #import errno | |
19 | import fcntl |
|
19 | import fcntl | |
20 | import functools |
|
20 | import functools | |
21 | import logging |
|
21 | import logging | |
22 | import os |
|
22 | import os | |
23 | import pickle |
|
23 | import pickle | |
24 | #import time |
|
24 | #import time | |
25 |
|
25 | |||
26 | #import gevent |
|
26 | #import gevent | |
27 | import msgpack |
|
27 | import msgpack | |
28 | import redis |
|
28 | import redis | |
29 |
|
29 | |||
30 | flock_org = fcntl.flock |
|
30 | flock_org = fcntl.flock | |
31 | from typing import Union |
|
31 | from typing import Union | |
32 |
|
32 | |||
33 | from dogpile.cache.api import Deserializer, Serializer |
|
33 | from dogpile.cache.api import Deserializer, Serializer | |
34 | from dogpile.cache.backends import file as file_backend |
|
34 | from dogpile.cache.backends import file as file_backend | |
35 | from dogpile.cache.backends import memory as memory_backend |
|
35 | from dogpile.cache.backends import memory as memory_backend | |
36 | from dogpile.cache.backends import redis as redis_backend |
|
36 | from dogpile.cache.backends import redis as redis_backend | |
37 | from dogpile.cache.backends.file import FileLock |
|
37 | from dogpile.cache.backends.file import FileLock | |
38 | from dogpile.cache.util import memoized_property |
|
38 | from dogpile.cache.util import memoized_property | |
39 |
|
39 | |||
40 | from vcsserver.lib.memory_lru_dict import LRUDict, LRUDictDebug |
|
40 | from vcsserver.lib.memory_lru_dict import LRUDict, LRUDictDebug | |
41 | from vcsserver.str_utils import safe_bytes, safe_str |
|
41 | from vcsserver.str_utils import safe_bytes, safe_str | |
42 | from vcsserver.type_utils import str2bool |
|
42 | from vcsserver.type_utils import str2bool | |
43 |
|
43 | |||
44 | _default_max_size = 1024 |
|
44 | _default_max_size = 1024 | |
45 |
|
45 | |||
46 | log = logging.getLogger(__name__) |
|
46 | log = logging.getLogger(__name__) | |
47 |
|
47 | |||
48 |
|
48 | |||
49 | class LRUMemoryBackend(memory_backend.MemoryBackend): |
|
49 | class LRUMemoryBackend(memory_backend.MemoryBackend): | |
50 | key_prefix = 'lru_mem_backend' |
|
50 | key_prefix = 'lru_mem_backend' | |
51 | pickle_values = False |
|
51 | pickle_values = False | |
52 |
|
52 | |||
53 | def __init__(self, arguments): |
|
53 | def __init__(self, arguments): | |
54 | self.max_size = arguments.pop('max_size', _default_max_size) |
|
54 | self.max_size = arguments.pop('max_size', _default_max_size) | |
55 |
|
55 | |||
56 | LRUDictClass = LRUDict |
|
56 | LRUDictClass = LRUDict | |
57 | if arguments.pop('log_key_count', None): |
|
57 | if arguments.pop('log_key_count', None): | |
58 | LRUDictClass = LRUDictDebug |
|
58 | LRUDictClass = LRUDictDebug | |
59 |
|
59 | |||
60 | arguments['cache_dict'] = LRUDictClass(self.max_size) |
|
60 | arguments['cache_dict'] = LRUDictClass(self.max_size) | |
61 | super().__init__(arguments) |
|
61 | super().__init__(arguments) | |
62 |
|
62 | |||
63 | def __repr__(self): |
|
63 | def __repr__(self): | |
64 | return f'{self.__class__}(maxsize=`{self.max_size}`)' |
|
64 | return f'{self.__class__}(maxsize=`{self.max_size}`)' | |
65 |
|
65 | |||
66 | def __str__(self): |
|
66 | def __str__(self): | |
67 | return self.__repr__() |
|
67 | return self.__repr__() | |
68 |
|
68 | |||
69 | def delete(self, key): |
|
69 | def delete(self, key): | |
70 | try: |
|
70 | try: | |
71 | del self._cache[key] |
|
71 | del self._cache[key] | |
72 | except KeyError: |
|
72 | except KeyError: | |
73 | # we don't care if key isn't there at deletion |
|
73 | # we don't care if key isn't there at deletion | |
74 | pass |
|
74 | pass | |
75 |
|
75 | |||
76 | def delete_multi(self, keys): |
|
76 | def delete_multi(self, keys): | |
77 | for key in keys: |
|
77 | for key in keys: | |
78 | self.delete(key) |
|
78 | self.delete(key) | |
79 |
|
79 | |||
80 |
|
80 | |||
81 | class PickleSerializer: |
|
81 | class PickleSerializer: | |
82 | serializer: None | Serializer = staticmethod( # type: ignore |
|
82 | serializer: None | Serializer = staticmethod( # type: ignore | |
83 | functools.partial(pickle.dumps, protocol=pickle.HIGHEST_PROTOCOL) |
|
83 | functools.partial(pickle.dumps, protocol=pickle.HIGHEST_PROTOCOL) | |
84 | ) |
|
84 | ) | |
85 | deserializer: None | Deserializer = staticmethod( # type: ignore |
|
85 | deserializer: None | Deserializer = staticmethod( # type: ignore | |
86 | functools.partial(pickle.loads) |
|
86 | functools.partial(pickle.loads) | |
87 | ) |
|
87 | ) | |
88 |
|
88 | |||
89 |
|
89 | |||
90 |
class MsgPackSerializer |
|
90 | class MsgPackSerializer: | |
91 | serializer: None | Serializer = staticmethod( # type: ignore |
|
91 | serializer: None | Serializer = staticmethod( # type: ignore | |
92 | msgpack.packb |
|
92 | msgpack.packb | |
93 | ) |
|
93 | ) | |
94 | deserializer: None | Deserializer = staticmethod( # type: ignore |
|
94 | deserializer: None | Deserializer = staticmethod( # type: ignore | |
95 | functools.partial(msgpack.unpackb, use_list=False) |
|
95 | functools.partial(msgpack.unpackb, use_list=False) | |
96 | ) |
|
96 | ) | |
97 |
|
97 | |||
98 |
|
98 | |||
99 | class CustomLockFactory(FileLock): |
|
99 | class CustomLockFactory(FileLock): | |
100 |
|
100 | |||
101 | pass |
|
101 | pass | |
102 |
|
102 | |||
103 |
|
103 | |||
104 | class FileNamespaceBackend(PickleSerializer, file_backend.DBMBackend): |
|
104 | class FileNamespaceBackend(PickleSerializer, file_backend.DBMBackend): | |
105 | key_prefix = 'file_backend' |
|
105 | key_prefix = 'file_backend' | |
106 |
|
106 | |||
107 | def __init__(self, arguments): |
|
107 | def __init__(self, arguments): | |
108 | arguments['lock_factory'] = CustomLockFactory |
|
108 | arguments['lock_factory'] = CustomLockFactory | |
109 | db_file = arguments.get('filename') |
|
109 | db_file = arguments.get('filename') | |
110 |
|
110 | |||
111 | log.debug('initialing cache-backend=%s db in %s', self.__class__.__name__, db_file) |
|
111 | log.debug('initialing cache-backend=%s db in %s', self.__class__.__name__, db_file) | |
112 | db_file_dir = os.path.dirname(db_file) |
|
112 | db_file_dir = os.path.dirname(db_file) | |
113 | if not os.path.isdir(db_file_dir): |
|
113 | if not os.path.isdir(db_file_dir): | |
114 | os.makedirs(db_file_dir) |
|
114 | os.makedirs(db_file_dir) | |
115 |
|
115 | |||
116 | try: |
|
116 | try: | |
117 | super().__init__(arguments) |
|
117 | super().__init__(arguments) | |
118 | except Exception: |
|
118 | except Exception: | |
119 | log.exception('Failed to initialize db at: %s', db_file) |
|
119 | log.exception('Failed to initialize db at: %s', db_file) | |
120 | raise |
|
120 | raise | |
121 |
|
121 | |||
122 | def __repr__(self): |
|
122 | def __repr__(self): | |
123 | return f'{self.__class__}(file=`{self.filename}`)' |
|
123 | return f'{self.__class__}(file=`{self.filename}`)' | |
124 |
|
124 | |||
125 | def __str__(self): |
|
125 | def __str__(self): | |
126 | return self.__repr__() |
|
126 | return self.__repr__() | |
127 |
|
127 | |||
128 | def _get_keys_pattern(self, prefix: bytes = b''): |
|
128 | def _get_keys_pattern(self, prefix: bytes = b''): | |
129 | return b'%b:%b' % (safe_bytes(self.key_prefix), safe_bytes(prefix)) |
|
129 | return b'%b:%b' % (safe_bytes(self.key_prefix), safe_bytes(prefix)) | |
130 |
|
130 | |||
131 | def list_keys(self, prefix: bytes = b''): |
|
131 | def list_keys(self, prefix: bytes = b''): | |
132 | prefix = self._get_keys_pattern(prefix) |
|
132 | prefix = self._get_keys_pattern(prefix) | |
133 |
|
133 | |||
134 | def cond(dbm_key: bytes): |
|
134 | def cond(dbm_key: bytes): | |
135 | if not prefix: |
|
135 | if not prefix: | |
136 | return True |
|
136 | return True | |
137 |
|
137 | |||
138 | if dbm_key.startswith(prefix): |
|
138 | if dbm_key.startswith(prefix): | |
139 | return True |
|
139 | return True | |
140 | return False |
|
140 | return False | |
141 |
|
141 | |||
142 | with self._dbm_file(True) as dbm: |
|
142 | with self._dbm_file(True) as dbm: | |
143 | try: |
|
143 | try: | |
144 | return list(filter(cond, dbm.keys())) |
|
144 | return list(filter(cond, dbm.keys())) | |
145 | except Exception: |
|
145 | except Exception: | |
146 | log.error('Failed to fetch DBM keys from DB: %s', self.get_store()) |
|
146 | log.error('Failed to fetch DBM keys from DB: %s', self.get_store()) | |
147 | raise |
|
147 | raise | |
148 |
|
148 | |||
149 | def get_store(self): |
|
149 | def get_store(self): | |
150 | return self.filename |
|
150 | return self.filename | |
151 |
|
151 | |||
152 |
|
152 | |||
153 | class BaseRedisBackend(redis_backend.RedisBackend): |
|
153 | class BaseRedisBackend(redis_backend.RedisBackend): | |
154 | key_prefix = '' |
|
154 | key_prefix = '' | |
155 |
|
155 | |||
156 | def __init__(self, arguments): |
|
156 | def __init__(self, arguments): | |
157 | self.db_conn = arguments.get('host', '') or arguments.get('url', '') or 'redis-host' |
|
157 | self.db_conn = arguments.get('host', '') or arguments.get('url', '') or 'redis-host' | |
158 | super().__init__(arguments) |
|
158 | super().__init__(arguments) | |
159 |
|
159 | |||
160 | self._lock_timeout = self.lock_timeout |
|
160 | self._lock_timeout = self.lock_timeout | |
161 | self._lock_auto_renewal = str2bool(arguments.pop("lock_auto_renewal", True)) |
|
161 | self._lock_auto_renewal = str2bool(arguments.pop("lock_auto_renewal", True)) | |
162 |
|
162 | |||
163 | if self._lock_auto_renewal and not self._lock_timeout: |
|
163 | if self._lock_auto_renewal and not self._lock_timeout: | |
164 | # set default timeout for auto_renewal |
|
164 | # set default timeout for auto_renewal | |
165 | self._lock_timeout = 30 |
|
165 | self._lock_timeout = 30 | |
166 |
|
166 | |||
167 | def __repr__(self): |
|
167 | def __repr__(self): | |
168 | return f'{self.__class__}(conn=`{self.db_conn}`)' |
|
168 | return f'{self.__class__}(conn=`{self.db_conn}`)' | |
169 |
|
169 | |||
170 | def __str__(self): |
|
170 | def __str__(self): | |
171 | return self.__repr__() |
|
171 | return self.__repr__() | |
172 |
|
172 | |||
173 | def _create_client(self): |
|
173 | def _create_client(self): | |
174 | args = {} |
|
174 | args = {} | |
175 |
|
175 | |||
176 | if self.url is not None: |
|
176 | if self.url is not None: | |
177 | args.update(url=self.url) |
|
177 | args.update(url=self.url) | |
178 |
|
178 | |||
179 | else: |
|
179 | else: | |
180 | args.update( |
|
180 | args.update( | |
181 | host=self.host, password=self.password, |
|
181 | host=self.host, password=self.password, | |
182 | port=self.port, db=self.db |
|
182 | port=self.port, db=self.db | |
183 | ) |
|
183 | ) | |
184 |
|
184 | |||
185 | connection_pool = redis.ConnectionPool(**args) |
|
185 | connection_pool = redis.ConnectionPool(**args) | |
186 | self.writer_client = redis.StrictRedis( |
|
186 | self.writer_client = redis.StrictRedis( | |
187 | connection_pool=connection_pool |
|
187 | connection_pool=connection_pool | |
188 | ) |
|
188 | ) | |
189 | self.reader_client = self.writer_client |
|
189 | self.reader_client = self.writer_client | |
190 |
|
190 | |||
191 | def _get_keys_pattern(self, prefix: bytes = b''): |
|
191 | def _get_keys_pattern(self, prefix: bytes = b''): | |
192 | return b'%b:%b*' % (safe_bytes(self.key_prefix), safe_bytes(prefix)) |
|
192 | return b'%b:%b*' % (safe_bytes(self.key_prefix), safe_bytes(prefix)) | |
193 |
|
193 | |||
194 | def list_keys(self, prefix: bytes = b''): |
|
194 | def list_keys(self, prefix: bytes = b''): | |
195 | prefix = self._get_keys_pattern(prefix) |
|
195 | prefix = self._get_keys_pattern(prefix) | |
196 | return self.reader_client.keys(prefix) |
|
196 | return self.reader_client.keys(prefix) | |
197 |
|
197 | |||
198 | def get_store(self): |
|
198 | def get_store(self): | |
199 | return self.reader_client.connection_pool |
|
199 | return self.reader_client.connection_pool | |
200 |
|
200 | |||
201 | def get_mutex(self, key): |
|
201 | def get_mutex(self, key): | |
202 | if self.distributed_lock: |
|
202 | if self.distributed_lock: | |
203 | lock_key = f'_lock_{safe_str(key)}' |
|
203 | lock_key = f'_lock_{safe_str(key)}' | |
204 | return get_mutex_lock( |
|
204 | return get_mutex_lock( | |
205 | self.writer_client, lock_key, |
|
205 | self.writer_client, lock_key, | |
206 | self._lock_timeout, |
|
206 | self._lock_timeout, | |
207 | auto_renewal=self._lock_auto_renewal |
|
207 | auto_renewal=self._lock_auto_renewal | |
208 | ) |
|
208 | ) | |
209 | else: |
|
209 | else: | |
210 | return None |
|
210 | return None | |
211 |
|
211 | |||
212 |
|
212 | |||
213 | class RedisPickleBackend(PickleSerializer, BaseRedisBackend): |
|
213 | class RedisPickleBackend(PickleSerializer, BaseRedisBackend): | |
214 | key_prefix = 'redis_pickle_backend' |
|
214 | key_prefix = 'redis_pickle_backend' | |
215 | pass |
|
215 | pass | |
216 |
|
216 | |||
217 |
|
217 | |||
218 | class RedisMsgPackBackend(MsgPackSerializer, BaseRedisBackend): |
|
218 | class RedisMsgPackBackend(MsgPackSerializer, BaseRedisBackend): | |
219 | key_prefix = 'redis_msgpack_backend' |
|
219 | key_prefix = 'redis_msgpack_backend' | |
220 | pass |
|
220 | pass | |
221 |
|
221 | |||
222 |
|
222 | |||
223 | def get_mutex_lock(client, lock_key, lock_timeout, auto_renewal=False): |
|
223 | def get_mutex_lock(client, lock_key, lock_timeout, auto_renewal=False): | |
224 | from vcsserver.lib._vendor import redis_lock |
|
224 | from vcsserver.lib._vendor import redis_lock | |
225 |
|
225 | |||
226 |
class _RedisLockWrapper |
|
226 | class _RedisLockWrapper: | |
227 | """LockWrapper for redis_lock""" |
|
227 | """LockWrapper for redis_lock""" | |
228 |
|
228 | |||
229 | @classmethod |
|
229 | @classmethod | |
230 | def get_lock(cls): |
|
230 | def get_lock(cls): | |
231 | return redis_lock.Lock( |
|
231 | return redis_lock.Lock( | |
232 | redis_client=client, |
|
232 | redis_client=client, | |
233 | name=lock_key, |
|
233 | name=lock_key, | |
234 | expire=lock_timeout, |
|
234 | expire=lock_timeout, | |
235 | auto_renewal=auto_renewal, |
|
235 | auto_renewal=auto_renewal, | |
236 | strict=True, |
|
236 | strict=True, | |
237 | ) |
|
237 | ) | |
238 |
|
238 | |||
239 | def __repr__(self): |
|
239 | def __repr__(self): | |
240 | return f"{self.__class__.__name__}:{lock_key}" |
|
240 | return f"{self.__class__.__name__}:{lock_key}" | |
241 |
|
241 | |||
242 | def __str__(self): |
|
242 | def __str__(self): | |
243 | return f"{self.__class__.__name__}:{lock_key}" |
|
243 | return f"{self.__class__.__name__}:{lock_key}" | |
244 |
|
244 | |||
245 | def __init__(self): |
|
245 | def __init__(self): | |
246 | self.lock = self.get_lock() |
|
246 | self.lock = self.get_lock() | |
247 | self.lock_key = lock_key |
|
247 | self.lock_key = lock_key | |
248 |
|
248 | |||
249 | def acquire(self, wait=True): |
|
249 | def acquire(self, wait=True): | |
250 | log.debug('Trying to acquire Redis lock for key %s', self.lock_key) |
|
250 | log.debug('Trying to acquire Redis lock for key %s', self.lock_key) | |
251 | try: |
|
251 | try: | |
252 | acquired = self.lock.acquire(wait) |
|
252 | acquired = self.lock.acquire(wait) | |
253 | log.debug('Got lock for key %s, %s', self.lock_key, acquired) |
|
253 | log.debug('Got lock for key %s, %s', self.lock_key, acquired) | |
254 | return acquired |
|
254 | return acquired | |
255 | except redis_lock.AlreadyAcquired: |
|
255 | except redis_lock.AlreadyAcquired: | |
256 | return False |
|
256 | return False | |
257 | except redis_lock.AlreadyStarted: |
|
257 | except redis_lock.AlreadyStarted: | |
258 | # refresh thread exists, but it also means we acquired the lock |
|
258 | # refresh thread exists, but it also means we acquired the lock | |
259 | return True |
|
259 | return True | |
260 |
|
260 | |||
261 | def release(self): |
|
261 | def release(self): | |
262 | try: |
|
262 | try: | |
263 | self.lock.release() |
|
263 | self.lock.release() | |
264 | except redis_lock.NotAcquired: |
|
264 | except redis_lock.NotAcquired: | |
265 | pass |
|
265 | pass | |
266 |
|
266 | |||
267 | return _RedisLockWrapper() |
|
267 | return _RedisLockWrapper() |
@@ -1,160 +1,160 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import os |
|
18 | import os | |
19 | import tempfile |
|
19 | import tempfile | |
20 |
|
20 | |||
21 | from svn import client |
|
21 | from svn import client | |
22 | from svn import core |
|
22 | from svn import core | |
23 | from svn import ra |
|
23 | from svn import ra | |
24 |
|
24 | |||
25 | from mercurial import error |
|
25 | from mercurial import error | |
26 |
|
26 | |||
27 | from vcsserver.str_utils import safe_bytes |
|
27 | from vcsserver.str_utils import safe_bytes | |
28 |
|
28 | |||
29 | core.svn_config_ensure(None) |
|
29 | core.svn_config_ensure(None) | |
30 | svn_config = core.svn_config_get_config(None) |
|
30 | svn_config = core.svn_config_get_config(None) | |
31 |
|
31 | |||
32 |
|
32 | |||
33 | class RaCallbacks(ra.Callbacks): |
|
33 | class RaCallbacks(ra.Callbacks): | |
34 | @staticmethod |
|
34 | @staticmethod | |
35 | def open_tmp_file(pool): # pragma: no cover |
|
35 | def open_tmp_file(pool): # pragma: no cover | |
36 | (fd, fn) = tempfile.mkstemp() |
|
36 | (fd, fn) = tempfile.mkstemp() | |
37 | os.close(fd) |
|
37 | os.close(fd) | |
38 | return fn |
|
38 | return fn | |
39 |
|
39 | |||
40 | @staticmethod |
|
40 | @staticmethod | |
41 | def get_client_string(pool): |
|
41 | def get_client_string(pool): | |
42 | return b'RhodeCode-subversion-url-checker' |
|
42 | return b'RhodeCode-subversion-url-checker' | |
43 |
|
43 | |||
44 |
|
44 | |||
45 | class SubversionException(Exception): |
|
45 | class SubversionException(Exception): | |
46 | pass |
|
46 | pass | |
47 |
|
47 | |||
48 |
|
48 | |||
49 | class SubversionConnectionException(SubversionException): |
|
49 | class SubversionConnectionException(SubversionException): | |
50 | """Exception raised when a generic error occurs when connecting to a repository.""" |
|
50 | """Exception raised when a generic error occurs when connecting to a repository.""" | |
51 |
|
51 | |||
52 |
|
52 | |||
53 | def normalize_url(url): |
|
53 | def normalize_url(url): | |
54 | if not url: |
|
54 | if not url: | |
55 | return url |
|
55 | return url | |
56 | if url.startswith(b'svn+http://') or url.startswith(b'svn+https://'): |
|
56 | if url.startswith(b'svn+http://') or url.startswith(b'svn+https://'): | |
57 | url = url[4:] |
|
57 | url = url[4:] | |
58 | url = url.rstrip(b'/') |
|
58 | url = url.rstrip(b'/') | |
59 | return url |
|
59 | return url | |
60 |
|
60 | |||
61 |
|
61 | |||
62 | def _create_auth_baton(pool): |
|
62 | def _create_auth_baton(pool): | |
63 | """Create a Subversion authentication baton. """ |
|
63 | """Create a Subversion authentication baton. """ | |
64 | # Give the client context baton a suite of authentication |
|
64 | # Give the client context baton a suite of authentication | |
65 | # providers.h |
|
65 | # providers.h | |
66 | platform_specific = [ |
|
66 | platform_specific = [ | |
67 | 'svn_auth_get_gnome_keyring_simple_provider', |
|
67 | 'svn_auth_get_gnome_keyring_simple_provider', | |
68 | 'svn_auth_get_gnome_keyring_ssl_client_cert_pw_provider', |
|
68 | 'svn_auth_get_gnome_keyring_ssl_client_cert_pw_provider', | |
69 | 'svn_auth_get_keychain_simple_provider', |
|
69 | 'svn_auth_get_keychain_simple_provider', | |
70 | 'svn_auth_get_keychain_ssl_client_cert_pw_provider', |
|
70 | 'svn_auth_get_keychain_ssl_client_cert_pw_provider', | |
71 | 'svn_auth_get_kwallet_simple_provider', |
|
71 | 'svn_auth_get_kwallet_simple_provider', | |
72 | 'svn_auth_get_kwallet_ssl_client_cert_pw_provider', |
|
72 | 'svn_auth_get_kwallet_ssl_client_cert_pw_provider', | |
73 | 'svn_auth_get_ssl_client_cert_file_provider', |
|
73 | 'svn_auth_get_ssl_client_cert_file_provider', | |
74 | 'svn_auth_get_windows_simple_provider', |
|
74 | 'svn_auth_get_windows_simple_provider', | |
75 | 'svn_auth_get_windows_ssl_server_trust_provider', |
|
75 | 'svn_auth_get_windows_ssl_server_trust_provider', | |
76 | ] |
|
76 | ] | |
77 |
|
77 | |||
78 | providers = [] |
|
78 | providers = [] | |
79 |
|
79 | |||
80 | for p in platform_specific: |
|
80 | for p in platform_specific: | |
81 | if getattr(core, p, None) is not None: |
|
81 | if getattr(core, p, None) is not None: | |
82 | try: |
|
82 | try: | |
83 | providers.append(getattr(core, p)()) |
|
83 | providers.append(getattr(core, p)()) | |
84 | except RuntimeError: |
|
84 | except RuntimeError: | |
85 | pass |
|
85 | pass | |
86 |
|
86 | |||
87 | providers += [ |
|
87 | providers += [ | |
88 | client.get_simple_provider(), |
|
88 | client.get_simple_provider(), | |
89 | client.get_username_provider(), |
|
89 | client.get_username_provider(), | |
90 | client.get_ssl_client_cert_file_provider(), |
|
90 | client.get_ssl_client_cert_file_provider(), | |
91 | client.get_ssl_client_cert_pw_file_provider(), |
|
91 | client.get_ssl_client_cert_pw_file_provider(), | |
92 | client.get_ssl_server_trust_file_provider(), |
|
92 | client.get_ssl_server_trust_file_provider(), | |
93 | ] |
|
93 | ] | |
94 |
|
94 | |||
95 | return core.svn_auth_open(providers, pool) |
|
95 | return core.svn_auth_open(providers, pool) | |
96 |
|
96 | |||
97 |
|
97 | |||
98 |
class SubversionRepo |
|
98 | class SubversionRepo: | |
99 | """Wrapper for a Subversion repository. |
|
99 | """Wrapper for a Subversion repository. | |
100 |
|
100 | |||
101 | It uses the SWIG Python bindings, see above for requirements. |
|
101 | It uses the SWIG Python bindings, see above for requirements. | |
102 | """ |
|
102 | """ | |
103 | def __init__(self, svn_url: bytes = b'', username: bytes = b'', password: bytes = b''): |
|
103 | def __init__(self, svn_url: bytes = b'', username: bytes = b'', password: bytes = b''): | |
104 |
|
104 | |||
105 | self.username = username |
|
105 | self.username = username | |
106 | self.password = password |
|
106 | self.password = password | |
107 | self.svn_url = core.svn_path_canonicalize(svn_url) |
|
107 | self.svn_url = core.svn_path_canonicalize(svn_url) | |
108 |
|
108 | |||
109 | self.auth_baton_pool = core.Pool() |
|
109 | self.auth_baton_pool = core.Pool() | |
110 | self.auth_baton = _create_auth_baton(self.auth_baton_pool) |
|
110 | self.auth_baton = _create_auth_baton(self.auth_baton_pool) | |
111 | # self.init_ra_and_client() assumes that a pool already exists |
|
111 | # self.init_ra_and_client() assumes that a pool already exists | |
112 | self.pool = core.Pool() |
|
112 | self.pool = core.Pool() | |
113 |
|
113 | |||
114 | self.ra = self.init_ra_and_client() |
|
114 | self.ra = self.init_ra_and_client() | |
115 | self.uuid = ra.get_uuid(self.ra, self.pool) |
|
115 | self.uuid = ra.get_uuid(self.ra, self.pool) | |
116 |
|
116 | |||
117 | def init_ra_and_client(self): |
|
117 | def init_ra_and_client(self): | |
118 | """Initializes the RA and client layers, because sometimes getting |
|
118 | """Initializes the RA and client layers, because sometimes getting | |
119 | unified diffs runs the remote server out of open files. |
|
119 | unified diffs runs the remote server out of open files. | |
120 | """ |
|
120 | """ | |
121 |
|
121 | |||
122 | if self.username: |
|
122 | if self.username: | |
123 | core.svn_auth_set_parameter(self.auth_baton, |
|
123 | core.svn_auth_set_parameter(self.auth_baton, | |
124 | core.SVN_AUTH_PARAM_DEFAULT_USERNAME, |
|
124 | core.SVN_AUTH_PARAM_DEFAULT_USERNAME, | |
125 | self.username) |
|
125 | self.username) | |
126 | if self.password: |
|
126 | if self.password: | |
127 | core.svn_auth_set_parameter(self.auth_baton, |
|
127 | core.svn_auth_set_parameter(self.auth_baton, | |
128 | core.SVN_AUTH_PARAM_DEFAULT_PASSWORD, |
|
128 | core.SVN_AUTH_PARAM_DEFAULT_PASSWORD, | |
129 | self.password) |
|
129 | self.password) | |
130 |
|
130 | |||
131 | callbacks = RaCallbacks() |
|
131 | callbacks = RaCallbacks() | |
132 | callbacks.auth_baton = self.auth_baton |
|
132 | callbacks.auth_baton = self.auth_baton | |
133 |
|
133 | |||
134 | try: |
|
134 | try: | |
135 | return ra.open2(self.svn_url, callbacks, svn_config, self.pool) |
|
135 | return ra.open2(self.svn_url, callbacks, svn_config, self.pool) | |
136 | except SubversionException as e: |
|
136 | except SubversionException as e: | |
137 | # e.child contains a detailed error messages |
|
137 | # e.child contains a detailed error messages | |
138 | msglist = [] |
|
138 | msglist = [] | |
139 | svn_exc = e |
|
139 | svn_exc = e | |
140 | while svn_exc: |
|
140 | while svn_exc: | |
141 | if svn_exc.args[0]: |
|
141 | if svn_exc.args[0]: | |
142 | msglist.append(svn_exc.args[0]) |
|
142 | msglist.append(svn_exc.args[0]) | |
143 | svn_exc = svn_exc.child |
|
143 | svn_exc = svn_exc.child | |
144 | msg = '\n'.join(msglist) |
|
144 | msg = '\n'.join(msglist) | |
145 | raise SubversionConnectionException(msg) |
|
145 | raise SubversionConnectionException(msg) | |
146 |
|
146 | |||
147 |
|
147 | |||
148 |
class svnremoterepo |
|
148 | class svnremoterepo: | |
149 | """ the dumb wrapper for actual Subversion repositories """ |
|
149 | """ the dumb wrapper for actual Subversion repositories """ | |
150 |
|
150 | |||
151 | def __init__(self, username: bytes = b'', password: bytes = b'', svn_url: bytes = b''): |
|
151 | def __init__(self, username: bytes = b'', password: bytes = b'', svn_url: bytes = b''): | |
152 | self.username = username or b'' |
|
152 | self.username = username or b'' | |
153 | self.password = password or b'' |
|
153 | self.password = password or b'' | |
154 | self.path = normalize_url(svn_url) |
|
154 | self.path = normalize_url(svn_url) | |
155 |
|
155 | |||
156 | def svn(self): |
|
156 | def svn(self): | |
157 | try: |
|
157 | try: | |
158 | return SubversionRepo(self.path, self.username, self.password) |
|
158 | return SubversionRepo(self.path, self.username, self.password) | |
159 | except SubversionConnectionException as e: |
|
159 | except SubversionConnectionException as e: | |
160 | raise error.Abort(safe_bytes(e)) |
|
160 | raise error.Abort(safe_bytes(e)) |
@@ -1,417 +1,417 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | """Handles the Git smart protocol.""" |
|
18 | """Handles the Git smart protocol.""" | |
19 |
|
19 | |||
20 | import os |
|
20 | import os | |
21 | import socket |
|
21 | import socket | |
22 | import logging |
|
22 | import logging | |
23 |
|
23 | |||
24 | import dulwich.protocol |
|
24 | import dulwich.protocol | |
25 | from dulwich.protocol import CAPABILITY_SIDE_BAND, CAPABILITY_SIDE_BAND_64K |
|
25 | from dulwich.protocol import CAPABILITY_SIDE_BAND, CAPABILITY_SIDE_BAND_64K | |
26 | from webob import Request, Response, exc |
|
26 | from webob import Request, Response, exc | |
27 |
|
27 | |||
28 | from vcsserver.lib.rc_json import json |
|
28 | from vcsserver.lib.rc_json import json | |
29 | from vcsserver import hooks, subprocessio |
|
29 | from vcsserver import hooks, subprocessio | |
30 | from vcsserver.str_utils import ascii_bytes |
|
30 | from vcsserver.str_utils import ascii_bytes | |
31 |
|
31 | |||
32 |
|
32 | |||
33 | log = logging.getLogger(__name__) |
|
33 | log = logging.getLogger(__name__) | |
34 |
|
34 | |||
35 |
|
35 | |||
36 |
class FileWrapper |
|
36 | class FileWrapper: | |
37 | """File wrapper that ensures how much data is read from it.""" |
|
37 | """File wrapper that ensures how much data is read from it.""" | |
38 |
|
38 | |||
39 | def __init__(self, fd, content_length): |
|
39 | def __init__(self, fd, content_length): | |
40 | self.fd = fd |
|
40 | self.fd = fd | |
41 | self.content_length = content_length |
|
41 | self.content_length = content_length | |
42 | self.remain = content_length |
|
42 | self.remain = content_length | |
43 |
|
43 | |||
44 | def read(self, size): |
|
44 | def read(self, size): | |
45 | if size <= self.remain: |
|
45 | if size <= self.remain: | |
46 | try: |
|
46 | try: | |
47 | data = self.fd.read(size) |
|
47 | data = self.fd.read(size) | |
48 | except socket.error: |
|
48 | except socket.error: | |
49 | raise IOError(self) |
|
49 | raise IOError(self) | |
50 | self.remain -= size |
|
50 | self.remain -= size | |
51 | elif self.remain: |
|
51 | elif self.remain: | |
52 | data = self.fd.read(self.remain) |
|
52 | data = self.fd.read(self.remain) | |
53 | self.remain = 0 |
|
53 | self.remain = 0 | |
54 | else: |
|
54 | else: | |
55 | data = None |
|
55 | data = None | |
56 | return data |
|
56 | return data | |
57 |
|
57 | |||
58 | def __repr__(self): |
|
58 | def __repr__(self): | |
59 | return '<FileWrapper {} len: {}, read: {}>'.format( |
|
59 | return '<FileWrapper {} len: {}, read: {}>'.format( | |
60 | self.fd, self.content_length, self.content_length - self.remain |
|
60 | self.fd, self.content_length, self.content_length - self.remain | |
61 | ) |
|
61 | ) | |
62 |
|
62 | |||
63 |
|
63 | |||
64 |
class GitRepository |
|
64 | class GitRepository: | |
65 | """WSGI app for handling Git smart protocol endpoints.""" |
|
65 | """WSGI app for handling Git smart protocol endpoints.""" | |
66 |
|
66 | |||
67 | git_folder_signature = frozenset(('config', 'head', 'info', 'objects', 'refs')) |
|
67 | git_folder_signature = frozenset(('config', 'head', 'info', 'objects', 'refs')) | |
68 | commands = frozenset(('git-upload-pack', 'git-receive-pack')) |
|
68 | commands = frozenset(('git-upload-pack', 'git-receive-pack')) | |
69 | valid_accepts = frozenset(f'application/x-{c}-result' for c in commands) |
|
69 | valid_accepts = frozenset(f'application/x-{c}-result' for c in commands) | |
70 |
|
70 | |||
71 | # The last bytes are the SHA1 of the first 12 bytes. |
|
71 | # The last bytes are the SHA1 of the first 12 bytes. | |
72 | EMPTY_PACK = ( |
|
72 | EMPTY_PACK = ( | |
73 | b'PACK\x00\x00\x00\x02\x00\x00\x00\x00\x02\x9d\x08' + |
|
73 | b'PACK\x00\x00\x00\x02\x00\x00\x00\x00\x02\x9d\x08' + | |
74 | b'\x82;\xd8\xa8\xea\xb5\x10\xadj\xc7\\\x82<\xfd>\xd3\x1e' |
|
74 | b'\x82;\xd8\xa8\xea\xb5\x10\xadj\xc7\\\x82<\xfd>\xd3\x1e' | |
75 | ) |
|
75 | ) | |
76 | FLUSH_PACKET = b"0000" |
|
76 | FLUSH_PACKET = b"0000" | |
77 |
|
77 | |||
78 | SIDE_BAND_CAPS = frozenset((CAPABILITY_SIDE_BAND, CAPABILITY_SIDE_BAND_64K)) |
|
78 | SIDE_BAND_CAPS = frozenset((CAPABILITY_SIDE_BAND, CAPABILITY_SIDE_BAND_64K)) | |
79 |
|
79 | |||
80 | def __init__(self, repo_name, content_path, git_path, update_server_info, extras): |
|
80 | def __init__(self, repo_name, content_path, git_path, update_server_info, extras): | |
81 | files = frozenset(f.lower() for f in os.listdir(content_path)) |
|
81 | files = frozenset(f.lower() for f in os.listdir(content_path)) | |
82 | valid_dir_signature = self.git_folder_signature.issubset(files) |
|
82 | valid_dir_signature = self.git_folder_signature.issubset(files) | |
83 |
|
83 | |||
84 | if not valid_dir_signature: |
|
84 | if not valid_dir_signature: | |
85 | raise OSError(f'{content_path} missing git signature') |
|
85 | raise OSError(f'{content_path} missing git signature') | |
86 |
|
86 | |||
87 | self.content_path = content_path |
|
87 | self.content_path = content_path | |
88 | self.repo_name = repo_name |
|
88 | self.repo_name = repo_name | |
89 | self.extras = extras |
|
89 | self.extras = extras | |
90 | self.git_path = git_path |
|
90 | self.git_path = git_path | |
91 | self.update_server_info = update_server_info |
|
91 | self.update_server_info = update_server_info | |
92 |
|
92 | |||
93 | def _get_fixedpath(self, path): |
|
93 | def _get_fixedpath(self, path): | |
94 | """ |
|
94 | """ | |
95 | Small fix for repo_path |
|
95 | Small fix for repo_path | |
96 |
|
96 | |||
97 | :param path: |
|
97 | :param path: | |
98 | """ |
|
98 | """ | |
99 | path = path.split(self.repo_name, 1)[-1] |
|
99 | path = path.split(self.repo_name, 1)[-1] | |
100 | if path.startswith('.git'): |
|
100 | if path.startswith('.git'): | |
101 | # for bare repos we still get the .git prefix inside, we skip it |
|
101 | # for bare repos we still get the .git prefix inside, we skip it | |
102 | # here, and remove from the service command |
|
102 | # here, and remove from the service command | |
103 | path = path[4:] |
|
103 | path = path[4:] | |
104 |
|
104 | |||
105 | return path.strip('/') |
|
105 | return path.strip('/') | |
106 |
|
106 | |||
107 | def inforefs(self, request, unused_environ): |
|
107 | def inforefs(self, request, unused_environ): | |
108 | """ |
|
108 | """ | |
109 | WSGI Response producer for HTTP GET Git Smart |
|
109 | WSGI Response producer for HTTP GET Git Smart | |
110 | HTTP /info/refs request. |
|
110 | HTTP /info/refs request. | |
111 | """ |
|
111 | """ | |
112 |
|
112 | |||
113 | git_command = request.GET.get('service') |
|
113 | git_command = request.GET.get('service') | |
114 | if git_command not in self.commands: |
|
114 | if git_command not in self.commands: | |
115 | log.debug('command %s not allowed', git_command) |
|
115 | log.debug('command %s not allowed', git_command) | |
116 | return exc.HTTPForbidden() |
|
116 | return exc.HTTPForbidden() | |
117 |
|
117 | |||
118 | # please, resist the urge to add '\n' to git capture and increment |
|
118 | # please, resist the urge to add '\n' to git capture and increment | |
119 | # line count by 1. |
|
119 | # line count by 1. | |
120 | # by git docs: Documentation/technical/http-protocol.txt#L214 \n is |
|
120 | # by git docs: Documentation/technical/http-protocol.txt#L214 \n is | |
121 | # a part of protocol. |
|
121 | # a part of protocol. | |
122 | # The code in Git client not only does NOT need '\n', but actually |
|
122 | # The code in Git client not only does NOT need '\n', but actually | |
123 | # blows up if you sprinkle "flush" (0000) as "0001\n". |
|
123 | # blows up if you sprinkle "flush" (0000) as "0001\n". | |
124 | # It reads binary, per number of bytes specified. |
|
124 | # It reads binary, per number of bytes specified. | |
125 | # if you do add '\n' as part of data, count it. |
|
125 | # if you do add '\n' as part of data, count it. | |
126 | server_advert = '# service=%s\n' % git_command |
|
126 | server_advert = '# service=%s\n' % git_command | |
127 | packet_len = hex(len(server_advert) + 4)[2:].rjust(4, '0').lower() |
|
127 | packet_len = hex(len(server_advert) + 4)[2:].rjust(4, '0').lower() | |
128 | try: |
|
128 | try: | |
129 | gitenv = dict(os.environ) |
|
129 | gitenv = dict(os.environ) | |
130 | # forget all configs |
|
130 | # forget all configs | |
131 | gitenv['RC_SCM_DATA'] = json.dumps(self.extras) |
|
131 | gitenv['RC_SCM_DATA'] = json.dumps(self.extras) | |
132 | command = [self.git_path, git_command[4:], '--stateless-rpc', |
|
132 | command = [self.git_path, git_command[4:], '--stateless-rpc', | |
133 | '--advertise-refs', self.content_path] |
|
133 | '--advertise-refs', self.content_path] | |
134 | out = subprocessio.SubprocessIOChunker( |
|
134 | out = subprocessio.SubprocessIOChunker( | |
135 | command, |
|
135 | command, | |
136 | env=gitenv, |
|
136 | env=gitenv, | |
137 | starting_values=[ascii_bytes(packet_len + server_advert) + self.FLUSH_PACKET], |
|
137 | starting_values=[ascii_bytes(packet_len + server_advert) + self.FLUSH_PACKET], | |
138 | shell=False |
|
138 | shell=False | |
139 | ) |
|
139 | ) | |
140 | except OSError: |
|
140 | except OSError: | |
141 | log.exception('Error processing command') |
|
141 | log.exception('Error processing command') | |
142 | raise exc.HTTPExpectationFailed() |
|
142 | raise exc.HTTPExpectationFailed() | |
143 |
|
143 | |||
144 | resp = Response() |
|
144 | resp = Response() | |
145 | resp.content_type = f'application/x-{git_command}-advertisement' |
|
145 | resp.content_type = f'application/x-{git_command}-advertisement' | |
146 | resp.charset = None |
|
146 | resp.charset = None | |
147 | resp.app_iter = out |
|
147 | resp.app_iter = out | |
148 |
|
148 | |||
149 | return resp |
|
149 | return resp | |
150 |
|
150 | |||
151 | def _get_want_capabilities(self, request): |
|
151 | def _get_want_capabilities(self, request): | |
152 | """Read the capabilities found in the first want line of the request.""" |
|
152 | """Read the capabilities found in the first want line of the request.""" | |
153 | pos = request.body_file_seekable.tell() |
|
153 | pos = request.body_file_seekable.tell() | |
154 | first_line = request.body_file_seekable.readline() |
|
154 | first_line = request.body_file_seekable.readline() | |
155 | request.body_file_seekable.seek(pos) |
|
155 | request.body_file_seekable.seek(pos) | |
156 |
|
156 | |||
157 | return frozenset( |
|
157 | return frozenset( | |
158 | dulwich.protocol.extract_want_line_capabilities(first_line)[1]) |
|
158 | dulwich.protocol.extract_want_line_capabilities(first_line)[1]) | |
159 |
|
159 | |||
160 | def _build_failed_pre_pull_response(self, capabilities, pre_pull_messages): |
|
160 | def _build_failed_pre_pull_response(self, capabilities, pre_pull_messages): | |
161 | """ |
|
161 | """ | |
162 | Construct a response with an empty PACK file. |
|
162 | Construct a response with an empty PACK file. | |
163 |
|
163 | |||
164 | We use an empty PACK file, as that would trigger the failure of the pull |
|
164 | We use an empty PACK file, as that would trigger the failure of the pull | |
165 | or clone command. |
|
165 | or clone command. | |
166 |
|
166 | |||
167 | We also print in the error output a message explaining why the command |
|
167 | We also print in the error output a message explaining why the command | |
168 | was aborted. |
|
168 | was aborted. | |
169 |
|
169 | |||
170 | If additionally, the user is accepting messages we send them the output |
|
170 | If additionally, the user is accepting messages we send them the output | |
171 | of the pre-pull hook. |
|
171 | of the pre-pull hook. | |
172 |
|
172 | |||
173 | Note that for clients not supporting side-band we just send them the |
|
173 | Note that for clients not supporting side-band we just send them the | |
174 | emtpy PACK file. |
|
174 | emtpy PACK file. | |
175 | """ |
|
175 | """ | |
176 |
|
176 | |||
177 | if self.SIDE_BAND_CAPS.intersection(capabilities): |
|
177 | if self.SIDE_BAND_CAPS.intersection(capabilities): | |
178 | response = [] |
|
178 | response = [] | |
179 | proto = dulwich.protocol.Protocol(None, response.append) |
|
179 | proto = dulwich.protocol.Protocol(None, response.append) | |
180 | proto.write_pkt_line(dulwich.protocol.NAK_LINE) |
|
180 | proto.write_pkt_line(dulwich.protocol.NAK_LINE) | |
181 |
|
181 | |||
182 | self._write_sideband_to_proto(proto, ascii_bytes(pre_pull_messages, allow_bytes=True), capabilities) |
|
182 | self._write_sideband_to_proto(proto, ascii_bytes(pre_pull_messages, allow_bytes=True), capabilities) | |
183 | # N.B.(skreft): Do not change the sideband channel to 3, as that |
|
183 | # N.B.(skreft): Do not change the sideband channel to 3, as that | |
184 | # produces a fatal error in the client: |
|
184 | # produces a fatal error in the client: | |
185 | # fatal: error in sideband demultiplexer |
|
185 | # fatal: error in sideband demultiplexer | |
186 | proto.write_sideband( |
|
186 | proto.write_sideband( | |
187 | dulwich.protocol.SIDE_BAND_CHANNEL_PROGRESS, |
|
187 | dulwich.protocol.SIDE_BAND_CHANNEL_PROGRESS, | |
188 | ascii_bytes('Pre pull hook failed: aborting\n', allow_bytes=True)) |
|
188 | ascii_bytes('Pre pull hook failed: aborting\n', allow_bytes=True)) | |
189 | proto.write_sideband( |
|
189 | proto.write_sideband( | |
190 | dulwich.protocol.SIDE_BAND_CHANNEL_DATA, |
|
190 | dulwich.protocol.SIDE_BAND_CHANNEL_DATA, | |
191 | ascii_bytes(self.EMPTY_PACK, allow_bytes=True)) |
|
191 | ascii_bytes(self.EMPTY_PACK, allow_bytes=True)) | |
192 |
|
192 | |||
193 | # writes b"0000" as default |
|
193 | # writes b"0000" as default | |
194 | proto.write_pkt_line(None) |
|
194 | proto.write_pkt_line(None) | |
195 |
|
195 | |||
196 | return response |
|
196 | return response | |
197 | else: |
|
197 | else: | |
198 | return [ascii_bytes(self.EMPTY_PACK, allow_bytes=True)] |
|
198 | return [ascii_bytes(self.EMPTY_PACK, allow_bytes=True)] | |
199 |
|
199 | |||
200 | def _build_post_pull_response(self, response, capabilities, start_message, end_message): |
|
200 | def _build_post_pull_response(self, response, capabilities, start_message, end_message): | |
201 | """ |
|
201 | """ | |
202 | Given a list response we inject the post-pull messages. |
|
202 | Given a list response we inject the post-pull messages. | |
203 |
|
203 | |||
204 | We only inject the messages if the client supports sideband, and the |
|
204 | We only inject the messages if the client supports sideband, and the | |
205 | response has the format: |
|
205 | response has the format: | |
206 | 0008NAK\n...0000 |
|
206 | 0008NAK\n...0000 | |
207 |
|
207 | |||
208 | Note that we do not check the no-progress capability as by default, git |
|
208 | Note that we do not check the no-progress capability as by default, git | |
209 | sends it, which effectively would block all messages. |
|
209 | sends it, which effectively would block all messages. | |
210 | """ |
|
210 | """ | |
211 |
|
211 | |||
212 | if not self.SIDE_BAND_CAPS.intersection(capabilities): |
|
212 | if not self.SIDE_BAND_CAPS.intersection(capabilities): | |
213 | return response |
|
213 | return response | |
214 |
|
214 | |||
215 | if not start_message and not end_message: |
|
215 | if not start_message and not end_message: | |
216 | return response |
|
216 | return response | |
217 |
|
217 | |||
218 | try: |
|
218 | try: | |
219 | iter(response) |
|
219 | iter(response) | |
220 | # iterator probably will work, we continue |
|
220 | # iterator probably will work, we continue | |
221 | except TypeError: |
|
221 | except TypeError: | |
222 | raise TypeError(f'response must be an iterator: got {type(response)}') |
|
222 | raise TypeError(f'response must be an iterator: got {type(response)}') | |
223 | if isinstance(response, (list, tuple)): |
|
223 | if isinstance(response, (list, tuple)): | |
224 | raise TypeError(f'response must be an iterator: got {type(response)}') |
|
224 | raise TypeError(f'response must be an iterator: got {type(response)}') | |
225 |
|
225 | |||
226 | def injected_response(): |
|
226 | def injected_response(): | |
227 |
|
227 | |||
228 | do_loop = 1 |
|
228 | do_loop = 1 | |
229 | header_injected = 0 |
|
229 | header_injected = 0 | |
230 | next_item = None |
|
230 | next_item = None | |
231 | has_item = False |
|
231 | has_item = False | |
232 | item = b'' |
|
232 | item = b'' | |
233 |
|
233 | |||
234 | while do_loop: |
|
234 | while do_loop: | |
235 |
|
235 | |||
236 | try: |
|
236 | try: | |
237 | next_item = next(response) |
|
237 | next_item = next(response) | |
238 | except StopIteration: |
|
238 | except StopIteration: | |
239 | do_loop = 0 |
|
239 | do_loop = 0 | |
240 |
|
240 | |||
241 | if has_item: |
|
241 | if has_item: | |
242 | # last item ! alter it now |
|
242 | # last item ! alter it now | |
243 | if do_loop == 0 and item.endswith(self.FLUSH_PACKET): |
|
243 | if do_loop == 0 and item.endswith(self.FLUSH_PACKET): | |
244 | new_response = [item[:-4]] |
|
244 | new_response = [item[:-4]] | |
245 | new_response.extend(self._get_messages(end_message, capabilities)) |
|
245 | new_response.extend(self._get_messages(end_message, capabilities)) | |
246 | new_response.append(self.FLUSH_PACKET) |
|
246 | new_response.append(self.FLUSH_PACKET) | |
247 | item = b''.join(new_response) |
|
247 | item = b''.join(new_response) | |
248 |
|
248 | |||
249 | yield item |
|
249 | yield item | |
250 |
|
250 | |||
251 | has_item = True |
|
251 | has_item = True | |
252 | item = next_item |
|
252 | item = next_item | |
253 |
|
253 | |||
254 | # alter item if it's the initial chunk |
|
254 | # alter item if it's the initial chunk | |
255 | if not header_injected and item.startswith(b'0008NAK\n'): |
|
255 | if not header_injected and item.startswith(b'0008NAK\n'): | |
256 | new_response = [b'0008NAK\n'] |
|
256 | new_response = [b'0008NAK\n'] | |
257 | new_response.extend(self._get_messages(start_message, capabilities)) |
|
257 | new_response.extend(self._get_messages(start_message, capabilities)) | |
258 | new_response.append(item[8:]) |
|
258 | new_response.append(item[8:]) | |
259 | item = b''.join(new_response) |
|
259 | item = b''.join(new_response) | |
260 | header_injected = 1 |
|
260 | header_injected = 1 | |
261 |
|
261 | |||
262 | return injected_response() |
|
262 | return injected_response() | |
263 |
|
263 | |||
264 | def _write_sideband_to_proto(self, proto, data, capabilities): |
|
264 | def _write_sideband_to_proto(self, proto, data, capabilities): | |
265 | """ |
|
265 | """ | |
266 | Write the data to the proto's sideband number 2 == SIDE_BAND_CHANNEL_PROGRESS |
|
266 | Write the data to the proto's sideband number 2 == SIDE_BAND_CHANNEL_PROGRESS | |
267 |
|
267 | |||
268 | We do not use dulwich's write_sideband directly as it only supports |
|
268 | We do not use dulwich's write_sideband directly as it only supports | |
269 | side-band-64k. |
|
269 | side-band-64k. | |
270 | """ |
|
270 | """ | |
271 | if not data: |
|
271 | if not data: | |
272 | return |
|
272 | return | |
273 |
|
273 | |||
274 | # N.B.(skreft): The values below are explained in the pack protocol |
|
274 | # N.B.(skreft): The values below are explained in the pack protocol | |
275 | # documentation, section Packfile Data. |
|
275 | # documentation, section Packfile Data. | |
276 | # https://github.com/git/git/blob/master/Documentation/technical/pack-protocol.txt |
|
276 | # https://github.com/git/git/blob/master/Documentation/technical/pack-protocol.txt | |
277 | if CAPABILITY_SIDE_BAND_64K in capabilities: |
|
277 | if CAPABILITY_SIDE_BAND_64K in capabilities: | |
278 | chunk_size = 65515 |
|
278 | chunk_size = 65515 | |
279 | elif CAPABILITY_SIDE_BAND in capabilities: |
|
279 | elif CAPABILITY_SIDE_BAND in capabilities: | |
280 | chunk_size = 995 |
|
280 | chunk_size = 995 | |
281 | else: |
|
281 | else: | |
282 | return |
|
282 | return | |
283 |
|
283 | |||
284 | chunker = (data[i:i + chunk_size] for i in range(0, len(data), chunk_size)) |
|
284 | chunker = (data[i:i + chunk_size] for i in range(0, len(data), chunk_size)) | |
285 |
|
285 | |||
286 | for chunk in chunker: |
|
286 | for chunk in chunker: | |
287 | proto.write_sideband(dulwich.protocol.SIDE_BAND_CHANNEL_PROGRESS, ascii_bytes(chunk, allow_bytes=True)) |
|
287 | proto.write_sideband(dulwich.protocol.SIDE_BAND_CHANNEL_PROGRESS, ascii_bytes(chunk, allow_bytes=True)) | |
288 |
|
288 | |||
289 | def _get_messages(self, data, capabilities): |
|
289 | def _get_messages(self, data, capabilities): | |
290 | """Return a list with packets for sending data in sideband number 2.""" |
|
290 | """Return a list with packets for sending data in sideband number 2.""" | |
291 | response = [] |
|
291 | response = [] | |
292 | proto = dulwich.protocol.Protocol(None, response.append) |
|
292 | proto = dulwich.protocol.Protocol(None, response.append) | |
293 |
|
293 | |||
294 | self._write_sideband_to_proto(proto, data, capabilities) |
|
294 | self._write_sideband_to_proto(proto, data, capabilities) | |
295 |
|
295 | |||
296 | return response |
|
296 | return response | |
297 |
|
297 | |||
298 | def backend(self, request, environ): |
|
298 | def backend(self, request, environ): | |
299 | """ |
|
299 | """ | |
300 | WSGI Response producer for HTTP POST Git Smart HTTP requests. |
|
300 | WSGI Response producer for HTTP POST Git Smart HTTP requests. | |
301 | Reads commands and data from HTTP POST's body. |
|
301 | Reads commands and data from HTTP POST's body. | |
302 | returns an iterator obj with contents of git command's |
|
302 | returns an iterator obj with contents of git command's | |
303 | response to stdout |
|
303 | response to stdout | |
304 | """ |
|
304 | """ | |
305 | # TODO(skreft): think how we could detect an HTTPLockedException, as |
|
305 | # TODO(skreft): think how we could detect an HTTPLockedException, as | |
306 | # we probably want to have the same mechanism used by mercurial and |
|
306 | # we probably want to have the same mechanism used by mercurial and | |
307 | # simplevcs. |
|
307 | # simplevcs. | |
308 | # For that we would need to parse the output of the command looking for |
|
308 | # For that we would need to parse the output of the command looking for | |
309 | # some signs of the HTTPLockedError, parse the data and reraise it in |
|
309 | # some signs of the HTTPLockedError, parse the data and reraise it in | |
310 | # pygrack. However, that would interfere with the streaming. |
|
310 | # pygrack. However, that would interfere with the streaming. | |
311 | # |
|
311 | # | |
312 | # Now the output of a blocked push is: |
|
312 | # Now the output of a blocked push is: | |
313 | # Pushing to http://test_regular:test12@127.0.0.1:5001/vcs_test_git |
|
313 | # Pushing to http://test_regular:test12@127.0.0.1:5001/vcs_test_git | |
314 | # POST git-receive-pack (1047 bytes) |
|
314 | # POST git-receive-pack (1047 bytes) | |
315 | # remote: ERROR: Repository `vcs_test_git` locked by user `test_admin`. Reason:`lock_auto` |
|
315 | # remote: ERROR: Repository `vcs_test_git` locked by user `test_admin`. Reason:`lock_auto` | |
316 | # To http://test_regular:test12@127.0.0.1:5001/vcs_test_git |
|
316 | # To http://test_regular:test12@127.0.0.1:5001/vcs_test_git | |
317 | # ! [remote rejected] master -> master (pre-receive hook declined) |
|
317 | # ! [remote rejected] master -> master (pre-receive hook declined) | |
318 | # error: failed to push some refs to 'http://test_regular:test12@127.0.0.1:5001/vcs_test_git' |
|
318 | # error: failed to push some refs to 'http://test_regular:test12@127.0.0.1:5001/vcs_test_git' | |
319 |
|
319 | |||
320 | git_command = self._get_fixedpath(request.path_info) |
|
320 | git_command = self._get_fixedpath(request.path_info) | |
321 | if git_command not in self.commands: |
|
321 | if git_command not in self.commands: | |
322 | log.debug('command %s not allowed', git_command) |
|
322 | log.debug('command %s not allowed', git_command) | |
323 | return exc.HTTPForbidden() |
|
323 | return exc.HTTPForbidden() | |
324 |
|
324 | |||
325 | capabilities = None |
|
325 | capabilities = None | |
326 | if git_command == 'git-upload-pack': |
|
326 | if git_command == 'git-upload-pack': | |
327 | capabilities = self._get_want_capabilities(request) |
|
327 | capabilities = self._get_want_capabilities(request) | |
328 |
|
328 | |||
329 | if 'CONTENT_LENGTH' in environ: |
|
329 | if 'CONTENT_LENGTH' in environ: | |
330 | inputstream = FileWrapper(request.body_file_seekable, |
|
330 | inputstream = FileWrapper(request.body_file_seekable, | |
331 | request.content_length) |
|
331 | request.content_length) | |
332 | else: |
|
332 | else: | |
333 | inputstream = request.body_file_seekable |
|
333 | inputstream = request.body_file_seekable | |
334 |
|
334 | |||
335 | resp = Response() |
|
335 | resp = Response() | |
336 | resp.content_type = f'application/x-{git_command}-result' |
|
336 | resp.content_type = f'application/x-{git_command}-result' | |
337 | resp.charset = None |
|
337 | resp.charset = None | |
338 |
|
338 | |||
339 | pre_pull_messages = '' |
|
339 | pre_pull_messages = '' | |
340 | # Upload-pack == clone |
|
340 | # Upload-pack == clone | |
341 | if git_command == 'git-upload-pack': |
|
341 | if git_command == 'git-upload-pack': | |
342 | hook_response = hooks.git_pre_pull(self.extras) |
|
342 | hook_response = hooks.git_pre_pull(self.extras) | |
343 | if hook_response.status != 0: |
|
343 | if hook_response.status != 0: | |
344 | pre_pull_messages = hook_response.output |
|
344 | pre_pull_messages = hook_response.output | |
345 | resp.app_iter = self._build_failed_pre_pull_response( |
|
345 | resp.app_iter = self._build_failed_pre_pull_response( | |
346 | capabilities, pre_pull_messages) |
|
346 | capabilities, pre_pull_messages) | |
347 | return resp |
|
347 | return resp | |
348 |
|
348 | |||
349 | gitenv = dict(os.environ) |
|
349 | gitenv = dict(os.environ) | |
350 | # forget all configs |
|
350 | # forget all configs | |
351 | gitenv['GIT_CONFIG_NOGLOBAL'] = '1' |
|
351 | gitenv['GIT_CONFIG_NOGLOBAL'] = '1' | |
352 | gitenv['RC_SCM_DATA'] = json.dumps(self.extras) |
|
352 | gitenv['RC_SCM_DATA'] = json.dumps(self.extras) | |
353 | cmd = [self.git_path, git_command[4:], '--stateless-rpc', |
|
353 | cmd = [self.git_path, git_command[4:], '--stateless-rpc', | |
354 | self.content_path] |
|
354 | self.content_path] | |
355 | log.debug('handling cmd %s', cmd) |
|
355 | log.debug('handling cmd %s', cmd) | |
356 |
|
356 | |||
357 | out = subprocessio.SubprocessIOChunker( |
|
357 | out = subprocessio.SubprocessIOChunker( | |
358 | cmd, |
|
358 | cmd, | |
359 | input_stream=inputstream, |
|
359 | input_stream=inputstream, | |
360 | env=gitenv, |
|
360 | env=gitenv, | |
361 | cwd=self.content_path, |
|
361 | cwd=self.content_path, | |
362 | shell=False, |
|
362 | shell=False, | |
363 | fail_on_stderr=False, |
|
363 | fail_on_stderr=False, | |
364 | fail_on_return_code=False |
|
364 | fail_on_return_code=False | |
365 | ) |
|
365 | ) | |
366 |
|
366 | |||
367 | if self.update_server_info and git_command == 'git-receive-pack': |
|
367 | if self.update_server_info and git_command == 'git-receive-pack': | |
368 | # We need to fully consume the iterator here, as the |
|
368 | # We need to fully consume the iterator here, as the | |
369 | # update-server-info command needs to be run after the push. |
|
369 | # update-server-info command needs to be run after the push. | |
370 | out = list(out) |
|
370 | out = list(out) | |
371 |
|
371 | |||
372 | # Updating refs manually after each push. |
|
372 | # Updating refs manually after each push. | |
373 | # This is required as some clients are exposing Git repos internally |
|
373 | # This is required as some clients are exposing Git repos internally | |
374 | # with the dumb protocol. |
|
374 | # with the dumb protocol. | |
375 | cmd = [self.git_path, 'update-server-info'] |
|
375 | cmd = [self.git_path, 'update-server-info'] | |
376 | log.debug('handling cmd %s', cmd) |
|
376 | log.debug('handling cmd %s', cmd) | |
377 | output = subprocessio.SubprocessIOChunker( |
|
377 | output = subprocessio.SubprocessIOChunker( | |
378 | cmd, |
|
378 | cmd, | |
379 | input_stream=inputstream, |
|
379 | input_stream=inputstream, | |
380 | env=gitenv, |
|
380 | env=gitenv, | |
381 | cwd=self.content_path, |
|
381 | cwd=self.content_path, | |
382 | shell=False, |
|
382 | shell=False, | |
383 | fail_on_stderr=False, |
|
383 | fail_on_stderr=False, | |
384 | fail_on_return_code=False |
|
384 | fail_on_return_code=False | |
385 | ) |
|
385 | ) | |
386 | # Consume all the output so the subprocess finishes |
|
386 | # Consume all the output so the subprocess finishes | |
387 | for _ in output: |
|
387 | for _ in output: | |
388 | pass |
|
388 | pass | |
389 |
|
389 | |||
390 | # Upload-pack == clone |
|
390 | # Upload-pack == clone | |
391 | if git_command == 'git-upload-pack': |
|
391 | if git_command == 'git-upload-pack': | |
392 | hook_response = hooks.git_post_pull(self.extras) |
|
392 | hook_response = hooks.git_post_pull(self.extras) | |
393 | post_pull_messages = hook_response.output |
|
393 | post_pull_messages = hook_response.output | |
394 | resp.app_iter = self._build_post_pull_response(out, capabilities, pre_pull_messages, post_pull_messages) |
|
394 | resp.app_iter = self._build_post_pull_response(out, capabilities, pre_pull_messages, post_pull_messages) | |
395 | else: |
|
395 | else: | |
396 | resp.app_iter = out |
|
396 | resp.app_iter = out | |
397 |
|
397 | |||
398 | return resp |
|
398 | return resp | |
399 |
|
399 | |||
400 | def __call__(self, environ, start_response): |
|
400 | def __call__(self, environ, start_response): | |
401 | request = Request(environ) |
|
401 | request = Request(environ) | |
402 | _path = self._get_fixedpath(request.path_info) |
|
402 | _path = self._get_fixedpath(request.path_info) | |
403 | if _path.startswith('info/refs'): |
|
403 | if _path.startswith('info/refs'): | |
404 | app = self.inforefs |
|
404 | app = self.inforefs | |
405 | else: |
|
405 | else: | |
406 | app = self.backend |
|
406 | app = self.backend | |
407 |
|
407 | |||
408 | try: |
|
408 | try: | |
409 | resp = app(request, environ) |
|
409 | resp = app(request, environ) | |
410 | except exc.HTTPException as error: |
|
410 | except exc.HTTPException as error: | |
411 | log.exception('HTTP Error') |
|
411 | log.exception('HTTP Error') | |
412 | resp = error |
|
412 | resp = error | |
413 | except Exception: |
|
413 | except Exception: | |
414 | log.exception('Unknown error') |
|
414 | log.exception('Unknown error') | |
415 | resp = exc.HTTPInternalServerError() |
|
415 | resp = exc.HTTPInternalServerError() | |
416 |
|
416 | |||
417 | return resp(environ, start_response) |
|
417 | return resp(environ, start_response) |
@@ -1,1463 +1,1462 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import collections |
|
18 | import collections | |
19 | import logging |
|
19 | import logging | |
20 | import os |
|
20 | import os | |
21 | import re |
|
21 | import re | |
22 | import stat |
|
22 | import stat | |
23 | import traceback |
|
23 | import traceback | |
24 | import urllib.request |
|
24 | import urllib.request | |
25 | import urllib.parse |
|
25 | import urllib.parse | |
26 | import urllib.error |
|
26 | import urllib.error | |
27 | from functools import wraps |
|
27 | from functools import wraps | |
28 |
|
28 | |||
29 | import more_itertools |
|
29 | import more_itertools | |
30 | import pygit2 |
|
30 | import pygit2 | |
31 | from pygit2 import Repository as LibGit2Repo |
|
31 | from pygit2 import Repository as LibGit2Repo | |
32 | from pygit2 import index as LibGit2Index |
|
32 | from pygit2 import index as LibGit2Index | |
33 | from dulwich import index, objects |
|
33 | from dulwich import index, objects | |
34 | from dulwich.client import HttpGitClient, LocalGitClient, FetchPackResult |
|
34 | from dulwich.client import HttpGitClient, LocalGitClient, FetchPackResult | |
35 | from dulwich.errors import ( |
|
35 | from dulwich.errors import ( | |
36 | NotGitRepository, ChecksumMismatch, WrongObjectException, |
|
36 | NotGitRepository, ChecksumMismatch, WrongObjectException, | |
37 | MissingCommitError, ObjectMissing, HangupException, |
|
37 | MissingCommitError, ObjectMissing, HangupException, | |
38 | UnexpectedCommandError) |
|
38 | UnexpectedCommandError) | |
39 | from dulwich.repo import Repo as DulwichRepo |
|
39 | from dulwich.repo import Repo as DulwichRepo | |
40 | from dulwich.server import update_server_info |
|
40 | from dulwich.server import update_server_info | |
41 |
|
41 | |||
42 | from vcsserver import exceptions, settings, subprocessio |
|
42 | from vcsserver import exceptions, settings, subprocessio | |
43 | from vcsserver.str_utils import safe_str, safe_int, safe_bytes, ascii_bytes |
|
43 | from vcsserver.str_utils import safe_str, safe_int, safe_bytes, ascii_bytes | |
44 | from vcsserver.base import RepoFactory, obfuscate_qs, ArchiveNode, store_archive_in_cache, BytesEnvelope, BinaryEnvelope |
|
44 | from vcsserver.base import RepoFactory, obfuscate_qs, ArchiveNode, store_archive_in_cache, BytesEnvelope, BinaryEnvelope | |
45 | from vcsserver.hgcompat import ( |
|
45 | from vcsserver.hgcompat import ( | |
46 | hg_url as url_parser, httpbasicauthhandler, httpdigestauthhandler) |
|
46 | hg_url as url_parser, httpbasicauthhandler, httpdigestauthhandler) | |
47 | from vcsserver.git_lfs.lib import LFSOidStore |
|
47 | from vcsserver.git_lfs.lib import LFSOidStore | |
48 | from vcsserver.vcs_base import RemoteBase |
|
48 | from vcsserver.vcs_base import RemoteBase | |
49 |
|
49 | |||
50 | DIR_STAT = stat.S_IFDIR |
|
50 | DIR_STAT = stat.S_IFDIR | |
51 | FILE_MODE = stat.S_IFMT |
|
51 | FILE_MODE = stat.S_IFMT | |
52 | GIT_LINK = objects.S_IFGITLINK |
|
52 | GIT_LINK = objects.S_IFGITLINK | |
53 | PEELED_REF_MARKER = b'^{}' |
|
53 | PEELED_REF_MARKER = b'^{}' | |
54 | HEAD_MARKER = b'HEAD' |
|
54 | HEAD_MARKER = b'HEAD' | |
55 |
|
55 | |||
56 | log = logging.getLogger(__name__) |
|
56 | log = logging.getLogger(__name__) | |
57 |
|
57 | |||
58 |
|
58 | |||
59 | def reraise_safe_exceptions(func): |
|
59 | def reraise_safe_exceptions(func): | |
60 | """Converts Dulwich exceptions to something neutral.""" |
|
60 | """Converts Dulwich exceptions to something neutral.""" | |
61 |
|
61 | |||
62 | @wraps(func) |
|
62 | @wraps(func) | |
63 | def wrapper(*args, **kwargs): |
|
63 | def wrapper(*args, **kwargs): | |
64 | try: |
|
64 | try: | |
65 | return func(*args, **kwargs) |
|
65 | return func(*args, **kwargs) | |
66 | except (ChecksumMismatch, WrongObjectException, MissingCommitError, ObjectMissing,) as e: |
|
66 | except (ChecksumMismatch, WrongObjectException, MissingCommitError, ObjectMissing,) as e: | |
67 | exc = exceptions.LookupException(org_exc=e) |
|
67 | exc = exceptions.LookupException(org_exc=e) | |
68 | raise exc(safe_str(e)) |
|
68 | raise exc(safe_str(e)) | |
69 | except (HangupException, UnexpectedCommandError) as e: |
|
69 | except (HangupException, UnexpectedCommandError) as e: | |
70 | exc = exceptions.VcsException(org_exc=e) |
|
70 | exc = exceptions.VcsException(org_exc=e) | |
71 | raise exc(safe_str(e)) |
|
71 | raise exc(safe_str(e)) | |
72 | except Exception: |
|
72 | except Exception: | |
73 | # NOTE(marcink): because of how dulwich handles some exceptions |
|
73 | # NOTE(marcink): because of how dulwich handles some exceptions | |
74 | # (KeyError on empty repos), we cannot track this and catch all |
|
74 | # (KeyError on empty repos), we cannot track this and catch all | |
75 | # exceptions, it's an exceptions from other handlers |
|
75 | # exceptions, it's an exceptions from other handlers | |
76 | #if not hasattr(e, '_vcs_kind'): |
|
76 | #if not hasattr(e, '_vcs_kind'): | |
77 | #log.exception("Unhandled exception in git remote call") |
|
77 | #log.exception("Unhandled exception in git remote call") | |
78 | #raise_from_original(exceptions.UnhandledException) |
|
78 | #raise_from_original(exceptions.UnhandledException) | |
79 | raise |
|
79 | raise | |
80 | return wrapper |
|
80 | return wrapper | |
81 |
|
81 | |||
82 |
|
82 | |||
83 | class Repo(DulwichRepo): |
|
83 | class Repo(DulwichRepo): | |
84 | """ |
|
84 | """ | |
85 | A wrapper for dulwich Repo class. |
|
85 | A wrapper for dulwich Repo class. | |
86 |
|
86 | |||
87 | Since dulwich is sometimes keeping .idx file descriptors open, it leads to |
|
87 | Since dulwich is sometimes keeping .idx file descriptors open, it leads to | |
88 | "Too many open files" error. We need to close all opened file descriptors |
|
88 | "Too many open files" error. We need to close all opened file descriptors | |
89 | once the repo object is destroyed. |
|
89 | once the repo object is destroyed. | |
90 | """ |
|
90 | """ | |
91 | def __del__(self): |
|
91 | def __del__(self): | |
92 | if hasattr(self, 'object_store'): |
|
92 | if hasattr(self, 'object_store'): | |
93 | self.close() |
|
93 | self.close() | |
94 |
|
94 | |||
95 |
|
95 | |||
96 | class Repository(LibGit2Repo): |
|
96 | class Repository(LibGit2Repo): | |
97 |
|
97 | |||
98 | def __enter__(self): |
|
98 | def __enter__(self): | |
99 | return self |
|
99 | return self | |
100 |
|
100 | |||
101 | def __exit__(self, exc_type, exc_val, exc_tb): |
|
101 | def __exit__(self, exc_type, exc_val, exc_tb): | |
102 | self.free() |
|
102 | self.free() | |
103 |
|
103 | |||
104 |
|
104 | |||
105 | class GitFactory(RepoFactory): |
|
105 | class GitFactory(RepoFactory): | |
106 | repo_type = 'git' |
|
106 | repo_type = 'git' | |
107 |
|
107 | |||
108 | def _create_repo(self, wire, create, use_libgit2=False): |
|
108 | def _create_repo(self, wire, create, use_libgit2=False): | |
109 | if use_libgit2: |
|
109 | if use_libgit2: | |
110 | repo = Repository(safe_bytes(wire['path'])) |
|
110 | repo = Repository(safe_bytes(wire['path'])) | |
111 | else: |
|
111 | else: | |
112 | # dulwich mode |
|
112 | # dulwich mode | |
113 | repo_path = safe_str(wire['path'], to_encoding=settings.WIRE_ENCODING) |
|
113 | repo_path = safe_str(wire['path'], to_encoding=settings.WIRE_ENCODING) | |
114 | repo = Repo(repo_path) |
|
114 | repo = Repo(repo_path) | |
115 |
|
115 | |||
116 | log.debug('repository created: got GIT object: %s', repo) |
|
116 | log.debug('repository created: got GIT object: %s', repo) | |
117 | return repo |
|
117 | return repo | |
118 |
|
118 | |||
119 | def repo(self, wire, create=False, use_libgit2=False): |
|
119 | def repo(self, wire, create=False, use_libgit2=False): | |
120 | """ |
|
120 | """ | |
121 | Get a repository instance for the given path. |
|
121 | Get a repository instance for the given path. | |
122 | """ |
|
122 | """ | |
123 | return self._create_repo(wire, create, use_libgit2) |
|
123 | return self._create_repo(wire, create, use_libgit2) | |
124 |
|
124 | |||
125 | def repo_libgit2(self, wire): |
|
125 | def repo_libgit2(self, wire): | |
126 | return self.repo(wire, use_libgit2=True) |
|
126 | return self.repo(wire, use_libgit2=True) | |
127 |
|
127 | |||
128 |
|
128 | |||
129 | def create_signature_from_string(author_str, **kwargs): |
|
129 | def create_signature_from_string(author_str, **kwargs): | |
130 | """ |
|
130 | """ | |
131 | Creates a pygit2.Signature object from a string of the format 'Name <email>'. |
|
131 | Creates a pygit2.Signature object from a string of the format 'Name <email>'. | |
132 |
|
132 | |||
133 | :param author_str: String of the format 'Name <email>' |
|
133 | :param author_str: String of the format 'Name <email>' | |
134 | :return: pygit2.Signature object |
|
134 | :return: pygit2.Signature object | |
135 | """ |
|
135 | """ | |
136 | match = re.match(r'^(.+) <(.+)>$', author_str) |
|
136 | match = re.match(r'^(.+) <(.+)>$', author_str) | |
137 | if match is None: |
|
137 | if match is None: | |
138 | raise ValueError(f"Invalid format: {author_str}") |
|
138 | raise ValueError(f"Invalid format: {author_str}") | |
139 |
|
139 | |||
140 | name, email = match.groups() |
|
140 | name, email = match.groups() | |
141 | return pygit2.Signature(name, email, **kwargs) |
|
141 | return pygit2.Signature(name, email, **kwargs) | |
142 |
|
142 | |||
143 |
|
143 | |||
144 | def get_obfuscated_url(url_obj): |
|
144 | def get_obfuscated_url(url_obj): | |
145 | url_obj.passwd = b'*****' if url_obj.passwd else url_obj.passwd |
|
145 | url_obj.passwd = b'*****' if url_obj.passwd else url_obj.passwd | |
146 | url_obj.query = obfuscate_qs(url_obj.query) |
|
146 | url_obj.query = obfuscate_qs(url_obj.query) | |
147 | obfuscated_uri = str(url_obj) |
|
147 | obfuscated_uri = str(url_obj) | |
148 | return obfuscated_uri |
|
148 | return obfuscated_uri | |
149 |
|
149 | |||
150 |
|
150 | |||
151 | class GitRemote(RemoteBase): |
|
151 | class GitRemote(RemoteBase): | |
152 |
|
152 | |||
153 | def __init__(self, factory): |
|
153 | def __init__(self, factory): | |
154 | self._factory = factory |
|
154 | self._factory = factory | |
155 | self._bulk_methods = { |
|
155 | self._bulk_methods = { | |
156 | "date": self.date, |
|
156 | "date": self.date, | |
157 | "author": self.author, |
|
157 | "author": self.author, | |
158 | "branch": self.branch, |
|
158 | "branch": self.branch, | |
159 | "message": self.message, |
|
159 | "message": self.message, | |
160 | "parents": self.parents, |
|
160 | "parents": self.parents, | |
161 | "_commit": self.revision, |
|
161 | "_commit": self.revision, | |
162 | } |
|
162 | } | |
163 | self._bulk_file_methods = { |
|
163 | self._bulk_file_methods = { | |
164 | "size": self.get_node_size, |
|
164 | "size": self.get_node_size, | |
165 | "data": self.get_node_data, |
|
165 | "data": self.get_node_data, | |
166 | "flags": self.get_node_flags, |
|
166 | "flags": self.get_node_flags, | |
167 | "is_binary": self.get_node_is_binary, |
|
167 | "is_binary": self.get_node_is_binary, | |
168 | "md5": self.md5_hash |
|
168 | "md5": self.md5_hash | |
169 | } |
|
169 | } | |
170 |
|
170 | |||
171 | def _wire_to_config(self, wire): |
|
171 | def _wire_to_config(self, wire): | |
172 | if 'config' in wire: |
|
172 | if 'config' in wire: | |
173 | return {x[0] + '_' + x[1]: x[2] for x in wire['config']} |
|
173 | return {x[0] + '_' + x[1]: x[2] for x in wire['config']} | |
174 | return {} |
|
174 | return {} | |
175 |
|
175 | |||
176 | def _remote_conf(self, config): |
|
176 | def _remote_conf(self, config): | |
177 | params = [ |
|
177 | params = [ | |
178 | '-c', 'core.askpass=""', |
|
178 | '-c', 'core.askpass=""', | |
179 | ] |
|
179 | ] | |
180 | ssl_cert_dir = config.get('vcs_ssl_dir') |
|
180 | ssl_cert_dir = config.get('vcs_ssl_dir') | |
181 | if ssl_cert_dir: |
|
181 | if ssl_cert_dir: | |
182 | params.extend(['-c', f'http.sslCAinfo={ssl_cert_dir}']) |
|
182 | params.extend(['-c', f'http.sslCAinfo={ssl_cert_dir}']) | |
183 | return params |
|
183 | return params | |
184 |
|
184 | |||
185 | @reraise_safe_exceptions |
|
185 | @reraise_safe_exceptions | |
186 | def discover_git_version(self): |
|
186 | def discover_git_version(self): | |
187 | stdout, _ = self.run_git_command( |
|
187 | stdout, _ = self.run_git_command( | |
188 | {}, ['--version'], _bare=True, _safe=True) |
|
188 | {}, ['--version'], _bare=True, _safe=True) | |
189 | prefix = b'git version' |
|
189 | prefix = b'git version' | |
190 | if stdout.startswith(prefix): |
|
190 | if stdout.startswith(prefix): | |
191 | stdout = stdout[len(prefix):] |
|
191 | stdout = stdout[len(prefix):] | |
192 | return safe_str(stdout.strip()) |
|
192 | return safe_str(stdout.strip()) | |
193 |
|
193 | |||
194 | @reraise_safe_exceptions |
|
194 | @reraise_safe_exceptions | |
195 | def is_empty(self, wire): |
|
195 | def is_empty(self, wire): | |
196 | repo_init = self._factory.repo_libgit2(wire) |
|
196 | repo_init = self._factory.repo_libgit2(wire) | |
197 | with repo_init as repo: |
|
197 | with repo_init as repo: | |
198 |
|
198 | |||
199 | try: |
|
199 | try: | |
200 | has_head = repo.head.name |
|
200 | has_head = repo.head.name | |
201 | if has_head: |
|
201 | if has_head: | |
202 | return False |
|
202 | return False | |
203 |
|
203 | |||
204 | # NOTE(marcink): check again using more expensive method |
|
204 | # NOTE(marcink): check again using more expensive method | |
205 | return repo.is_empty |
|
205 | return repo.is_empty | |
206 | except Exception: |
|
206 | except Exception: | |
207 | pass |
|
207 | pass | |
208 |
|
208 | |||
209 | return True |
|
209 | return True | |
210 |
|
210 | |||
211 | @reraise_safe_exceptions |
|
211 | @reraise_safe_exceptions | |
212 | def assert_correct_path(self, wire): |
|
212 | def assert_correct_path(self, wire): | |
213 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
213 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
214 | region = self._region(wire) |
|
214 | region = self._region(wire) | |
215 |
|
215 | |||
216 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
216 | @region.conditional_cache_on_arguments(condition=cache_on) | |
217 | def _assert_correct_path(_context_uid, _repo_id, fast_check): |
|
217 | def _assert_correct_path(_context_uid, _repo_id, fast_check): | |
218 | if fast_check: |
|
218 | if fast_check: | |
219 | path = safe_str(wire['path']) |
|
219 | path = safe_str(wire['path']) | |
220 | if pygit2.discover_repository(path): |
|
220 | if pygit2.discover_repository(path): | |
221 | return True |
|
221 | return True | |
222 | return False |
|
222 | return False | |
223 | else: |
|
223 | else: | |
224 | try: |
|
224 | try: | |
225 | repo_init = self._factory.repo_libgit2(wire) |
|
225 | repo_init = self._factory.repo_libgit2(wire) | |
226 | with repo_init: |
|
226 | with repo_init: | |
227 | pass |
|
227 | pass | |
228 | except pygit2.GitError: |
|
228 | except pygit2.GitError: | |
229 | path = wire.get('path') |
|
229 | path = wire.get('path') | |
230 | tb = traceback.format_exc() |
|
230 | tb = traceback.format_exc() | |
231 | log.debug("Invalid Git path `%s`, tb: %s", path, tb) |
|
231 | log.debug("Invalid Git path `%s`, tb: %s", path, tb) | |
232 | return False |
|
232 | return False | |
233 | return True |
|
233 | return True | |
234 |
|
234 | |||
235 | return _assert_correct_path(context_uid, repo_id, True) |
|
235 | return _assert_correct_path(context_uid, repo_id, True) | |
236 |
|
236 | |||
237 | @reraise_safe_exceptions |
|
237 | @reraise_safe_exceptions | |
238 | def bare(self, wire): |
|
238 | def bare(self, wire): | |
239 | repo_init = self._factory.repo_libgit2(wire) |
|
239 | repo_init = self._factory.repo_libgit2(wire) | |
240 | with repo_init as repo: |
|
240 | with repo_init as repo: | |
241 | return repo.is_bare |
|
241 | return repo.is_bare | |
242 |
|
242 | |||
243 | @reraise_safe_exceptions |
|
243 | @reraise_safe_exceptions | |
244 | def get_node_data(self, wire, commit_id, path): |
|
244 | def get_node_data(self, wire, commit_id, path): | |
245 | repo_init = self._factory.repo_libgit2(wire) |
|
245 | repo_init = self._factory.repo_libgit2(wire) | |
246 | with repo_init as repo: |
|
246 | with repo_init as repo: | |
247 | commit = repo[commit_id] |
|
247 | commit = repo[commit_id] | |
248 | blob_obj = commit.tree[path] |
|
248 | blob_obj = commit.tree[path] | |
249 |
|
249 | |||
250 | if blob_obj.type != pygit2.GIT_OBJ_BLOB: |
|
250 | if blob_obj.type != pygit2.GIT_OBJ_BLOB: | |
251 | raise exceptions.LookupException()( |
|
251 | raise exceptions.LookupException()( | |
252 | f'Tree for commit_id:{commit_id} is not a blob: {blob_obj.type_str}') |
|
252 | f'Tree for commit_id:{commit_id} is not a blob: {blob_obj.type_str}') | |
253 |
|
253 | |||
254 | return BytesEnvelope(blob_obj.data) |
|
254 | return BytesEnvelope(blob_obj.data) | |
255 |
|
255 | |||
256 | @reraise_safe_exceptions |
|
256 | @reraise_safe_exceptions | |
257 | def get_node_size(self, wire, commit_id, path): |
|
257 | def get_node_size(self, wire, commit_id, path): | |
258 | repo_init = self._factory.repo_libgit2(wire) |
|
258 | repo_init = self._factory.repo_libgit2(wire) | |
259 | with repo_init as repo: |
|
259 | with repo_init as repo: | |
260 | commit = repo[commit_id] |
|
260 | commit = repo[commit_id] | |
261 | blob_obj = commit.tree[path] |
|
261 | blob_obj = commit.tree[path] | |
262 |
|
262 | |||
263 | if blob_obj.type != pygit2.GIT_OBJ_BLOB: |
|
263 | if blob_obj.type != pygit2.GIT_OBJ_BLOB: | |
264 | raise exceptions.LookupException()( |
|
264 | raise exceptions.LookupException()( | |
265 | f'Tree for commit_id:{commit_id} is not a blob: {blob_obj.type_str}') |
|
265 | f'Tree for commit_id:{commit_id} is not a blob: {blob_obj.type_str}') | |
266 |
|
266 | |||
267 | return blob_obj.size |
|
267 | return blob_obj.size | |
268 |
|
268 | |||
269 | @reraise_safe_exceptions |
|
269 | @reraise_safe_exceptions | |
270 | def get_node_flags(self, wire, commit_id, path): |
|
270 | def get_node_flags(self, wire, commit_id, path): | |
271 | repo_init = self._factory.repo_libgit2(wire) |
|
271 | repo_init = self._factory.repo_libgit2(wire) | |
272 | with repo_init as repo: |
|
272 | with repo_init as repo: | |
273 | commit = repo[commit_id] |
|
273 | commit = repo[commit_id] | |
274 | blob_obj = commit.tree[path] |
|
274 | blob_obj = commit.tree[path] | |
275 |
|
275 | |||
276 | if blob_obj.type != pygit2.GIT_OBJ_BLOB: |
|
276 | if blob_obj.type != pygit2.GIT_OBJ_BLOB: | |
277 | raise exceptions.LookupException()( |
|
277 | raise exceptions.LookupException()( | |
278 | f'Tree for commit_id:{commit_id} is not a blob: {blob_obj.type_str}') |
|
278 | f'Tree for commit_id:{commit_id} is not a blob: {blob_obj.type_str}') | |
279 |
|
279 | |||
280 | return blob_obj.filemode |
|
280 | return blob_obj.filemode | |
281 |
|
281 | |||
282 | @reraise_safe_exceptions |
|
282 | @reraise_safe_exceptions | |
283 | def get_node_is_binary(self, wire, commit_id, path): |
|
283 | def get_node_is_binary(self, wire, commit_id, path): | |
284 | repo_init = self._factory.repo_libgit2(wire) |
|
284 | repo_init = self._factory.repo_libgit2(wire) | |
285 | with repo_init as repo: |
|
285 | with repo_init as repo: | |
286 | commit = repo[commit_id] |
|
286 | commit = repo[commit_id] | |
287 | blob_obj = commit.tree[path] |
|
287 | blob_obj = commit.tree[path] | |
288 |
|
288 | |||
289 | if blob_obj.type != pygit2.GIT_OBJ_BLOB: |
|
289 | if blob_obj.type != pygit2.GIT_OBJ_BLOB: | |
290 | raise exceptions.LookupException()( |
|
290 | raise exceptions.LookupException()( | |
291 | f'Tree for commit_id:{commit_id} is not a blob: {blob_obj.type_str}') |
|
291 | f'Tree for commit_id:{commit_id} is not a blob: {blob_obj.type_str}') | |
292 |
|
292 | |||
293 | return blob_obj.is_binary |
|
293 | return blob_obj.is_binary | |
294 |
|
294 | |||
295 | @reraise_safe_exceptions |
|
295 | @reraise_safe_exceptions | |
296 | def blob_as_pretty_string(self, wire, sha): |
|
296 | def blob_as_pretty_string(self, wire, sha): | |
297 | repo_init = self._factory.repo_libgit2(wire) |
|
297 | repo_init = self._factory.repo_libgit2(wire) | |
298 | with repo_init as repo: |
|
298 | with repo_init as repo: | |
299 | blob_obj = repo[sha] |
|
299 | blob_obj = repo[sha] | |
300 | return BytesEnvelope(blob_obj.data) |
|
300 | return BytesEnvelope(blob_obj.data) | |
301 |
|
301 | |||
302 | @reraise_safe_exceptions |
|
302 | @reraise_safe_exceptions | |
303 | def blob_raw_length(self, wire, sha): |
|
303 | def blob_raw_length(self, wire, sha): | |
304 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
304 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
305 | region = self._region(wire) |
|
305 | region = self._region(wire) | |
306 |
|
306 | |||
307 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
307 | @region.conditional_cache_on_arguments(condition=cache_on) | |
308 | def _blob_raw_length(_repo_id, _sha): |
|
308 | def _blob_raw_length(_repo_id, _sha): | |
309 |
|
309 | |||
310 | repo_init = self._factory.repo_libgit2(wire) |
|
310 | repo_init = self._factory.repo_libgit2(wire) | |
311 | with repo_init as repo: |
|
311 | with repo_init as repo: | |
312 | blob = repo[sha] |
|
312 | blob = repo[sha] | |
313 | return blob.size |
|
313 | return blob.size | |
314 |
|
314 | |||
315 | return _blob_raw_length(repo_id, sha) |
|
315 | return _blob_raw_length(repo_id, sha) | |
316 |
|
316 | |||
317 | def _parse_lfs_pointer(self, raw_content): |
|
317 | def _parse_lfs_pointer(self, raw_content): | |
318 | spec_string = b'version https://git-lfs.github.com/spec' |
|
318 | spec_string = b'version https://git-lfs.github.com/spec' | |
319 | if raw_content and raw_content.startswith(spec_string): |
|
319 | if raw_content and raw_content.startswith(spec_string): | |
320 |
|
320 | |||
321 | pattern = re.compile(rb""" |
|
321 | pattern = re.compile(rb""" | |
322 | (?:\n)? |
|
322 | (?:\n)? | |
323 | ^version[ ]https://git-lfs\.github\.com/spec/(?P<spec_ver>v\d+)\n |
|
323 | ^version[ ]https://git-lfs\.github\.com/spec/(?P<spec_ver>v\d+)\n | |
324 | ^oid[ ] sha256:(?P<oid_hash>[0-9a-f]{64})\n |
|
324 | ^oid[ ] sha256:(?P<oid_hash>[0-9a-f]{64})\n | |
325 | ^size[ ](?P<oid_size>[0-9]+)\n |
|
325 | ^size[ ](?P<oid_size>[0-9]+)\n | |
326 | (?:\n)? |
|
326 | (?:\n)? | |
327 | """, re.VERBOSE | re.MULTILINE) |
|
327 | """, re.VERBOSE | re.MULTILINE) | |
328 | match = pattern.match(raw_content) |
|
328 | match = pattern.match(raw_content) | |
329 | if match: |
|
329 | if match: | |
330 | return match.groupdict() |
|
330 | return match.groupdict() | |
331 |
|
331 | |||
332 | return {} |
|
332 | return {} | |
333 |
|
333 | |||
334 | @reraise_safe_exceptions |
|
334 | @reraise_safe_exceptions | |
335 | def is_large_file(self, wire, commit_id): |
|
335 | def is_large_file(self, wire, commit_id): | |
336 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
336 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
337 | region = self._region(wire) |
|
337 | region = self._region(wire) | |
338 |
|
338 | |||
339 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
339 | @region.conditional_cache_on_arguments(condition=cache_on) | |
340 | def _is_large_file(_repo_id, _sha): |
|
340 | def _is_large_file(_repo_id, _sha): | |
341 | repo_init = self._factory.repo_libgit2(wire) |
|
341 | repo_init = self._factory.repo_libgit2(wire) | |
342 | with repo_init as repo: |
|
342 | with repo_init as repo: | |
343 | blob = repo[commit_id] |
|
343 | blob = repo[commit_id] | |
344 | if blob.is_binary: |
|
344 | if blob.is_binary: | |
345 | return {} |
|
345 | return {} | |
346 |
|
346 | |||
347 | return self._parse_lfs_pointer(blob.data) |
|
347 | return self._parse_lfs_pointer(blob.data) | |
348 |
|
348 | |||
349 | return _is_large_file(repo_id, commit_id) |
|
349 | return _is_large_file(repo_id, commit_id) | |
350 |
|
350 | |||
351 | @reraise_safe_exceptions |
|
351 | @reraise_safe_exceptions | |
352 | def is_binary(self, wire, tree_id): |
|
352 | def is_binary(self, wire, tree_id): | |
353 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
353 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
354 | region = self._region(wire) |
|
354 | region = self._region(wire) | |
355 |
|
355 | |||
356 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
356 | @region.conditional_cache_on_arguments(condition=cache_on) | |
357 | def _is_binary(_repo_id, _tree_id): |
|
357 | def _is_binary(_repo_id, _tree_id): | |
358 | repo_init = self._factory.repo_libgit2(wire) |
|
358 | repo_init = self._factory.repo_libgit2(wire) | |
359 | with repo_init as repo: |
|
359 | with repo_init as repo: | |
360 | blob_obj = repo[tree_id] |
|
360 | blob_obj = repo[tree_id] | |
361 | return blob_obj.is_binary |
|
361 | return blob_obj.is_binary | |
362 |
|
362 | |||
363 | return _is_binary(repo_id, tree_id) |
|
363 | return _is_binary(repo_id, tree_id) | |
364 |
|
364 | |||
365 | @reraise_safe_exceptions |
|
365 | @reraise_safe_exceptions | |
366 | def md5_hash(self, wire, commit_id, path): |
|
366 | def md5_hash(self, wire, commit_id, path): | |
367 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
367 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
368 | region = self._region(wire) |
|
368 | region = self._region(wire) | |
369 |
|
369 | |||
370 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
370 | @region.conditional_cache_on_arguments(condition=cache_on) | |
371 | def _md5_hash(_repo_id, _commit_id, _path): |
|
371 | def _md5_hash(_repo_id, _commit_id, _path): | |
372 | repo_init = self._factory.repo_libgit2(wire) |
|
372 | repo_init = self._factory.repo_libgit2(wire) | |
373 | with repo_init as repo: |
|
373 | with repo_init as repo: | |
374 | commit = repo[_commit_id] |
|
374 | commit = repo[_commit_id] | |
375 | blob_obj = commit.tree[_path] |
|
375 | blob_obj = commit.tree[_path] | |
376 |
|
376 | |||
377 | if blob_obj.type != pygit2.GIT_OBJ_BLOB: |
|
377 | if blob_obj.type != pygit2.GIT_OBJ_BLOB: | |
378 | raise exceptions.LookupException()( |
|
378 | raise exceptions.LookupException()( | |
379 | f'Tree for commit_id:{_commit_id} is not a blob: {blob_obj.type_str}') |
|
379 | f'Tree for commit_id:{_commit_id} is not a blob: {blob_obj.type_str}') | |
380 |
|
380 | |||
381 | return '' |
|
381 | return '' | |
382 |
|
382 | |||
383 | return _md5_hash(repo_id, commit_id, path) |
|
383 | return _md5_hash(repo_id, commit_id, path) | |
384 |
|
384 | |||
385 | @reraise_safe_exceptions |
|
385 | @reraise_safe_exceptions | |
386 | def in_largefiles_store(self, wire, oid): |
|
386 | def in_largefiles_store(self, wire, oid): | |
387 | conf = self._wire_to_config(wire) |
|
387 | conf = self._wire_to_config(wire) | |
388 | repo_init = self._factory.repo_libgit2(wire) |
|
388 | repo_init = self._factory.repo_libgit2(wire) | |
389 | with repo_init as repo: |
|
389 | with repo_init as repo: | |
390 | repo_name = repo.path |
|
390 | repo_name = repo.path | |
391 |
|
391 | |||
392 | store_location = conf.get('vcs_git_lfs_store_location') |
|
392 | store_location = conf.get('vcs_git_lfs_store_location') | |
393 | if store_location: |
|
393 | if store_location: | |
394 |
|
394 | |||
395 | store = LFSOidStore( |
|
395 | store = LFSOidStore( | |
396 | oid=oid, repo=repo_name, store_location=store_location) |
|
396 | oid=oid, repo=repo_name, store_location=store_location) | |
397 | return store.has_oid() |
|
397 | return store.has_oid() | |
398 |
|
398 | |||
399 | return False |
|
399 | return False | |
400 |
|
400 | |||
401 | @reraise_safe_exceptions |
|
401 | @reraise_safe_exceptions | |
402 | def store_path(self, wire, oid): |
|
402 | def store_path(self, wire, oid): | |
403 | conf = self._wire_to_config(wire) |
|
403 | conf = self._wire_to_config(wire) | |
404 | repo_init = self._factory.repo_libgit2(wire) |
|
404 | repo_init = self._factory.repo_libgit2(wire) | |
405 | with repo_init as repo: |
|
405 | with repo_init as repo: | |
406 | repo_name = repo.path |
|
406 | repo_name = repo.path | |
407 |
|
407 | |||
408 | store_location = conf.get('vcs_git_lfs_store_location') |
|
408 | store_location = conf.get('vcs_git_lfs_store_location') | |
409 | if store_location: |
|
409 | if store_location: | |
410 | store = LFSOidStore( |
|
410 | store = LFSOidStore( | |
411 | oid=oid, repo=repo_name, store_location=store_location) |
|
411 | oid=oid, repo=repo_name, store_location=store_location) | |
412 | return store.oid_path |
|
412 | return store.oid_path | |
413 | raise ValueError(f'Unable to fetch oid with path {oid}') |
|
413 | raise ValueError(f'Unable to fetch oid with path {oid}') | |
414 |
|
414 | |||
415 | @reraise_safe_exceptions |
|
415 | @reraise_safe_exceptions | |
416 | def bulk_request(self, wire, rev, pre_load): |
|
416 | def bulk_request(self, wire, rev, pre_load): | |
417 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
417 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
418 | region = self._region(wire) |
|
418 | region = self._region(wire) | |
419 |
|
419 | |||
420 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
420 | @region.conditional_cache_on_arguments(condition=cache_on) | |
421 | def _bulk_request(_repo_id, _rev, _pre_load): |
|
421 | def _bulk_request(_repo_id, _rev, _pre_load): | |
422 | result = {} |
|
422 | result = {} | |
423 | for attr in pre_load: |
|
423 | for attr in pre_load: | |
424 | try: |
|
424 | try: | |
425 | method = self._bulk_methods[attr] |
|
425 | method = self._bulk_methods[attr] | |
426 | wire.update({'cache': False}) # disable cache for bulk calls so we don't double cache |
|
426 | wire.update({'cache': False}) # disable cache for bulk calls so we don't double cache | |
427 | args = [wire, rev] |
|
427 | args = [wire, rev] | |
428 | result[attr] = method(*args) |
|
428 | result[attr] = method(*args) | |
429 | except KeyError as e: |
|
429 | except KeyError as e: | |
430 | raise exceptions.VcsException(e)(f"Unknown bulk attribute: {attr}") |
|
430 | raise exceptions.VcsException(e)(f"Unknown bulk attribute: {attr}") | |
431 | return result |
|
431 | return result | |
432 |
|
432 | |||
433 | return _bulk_request(repo_id, rev, sorted(pre_load)) |
|
433 | return _bulk_request(repo_id, rev, sorted(pre_load)) | |
434 |
|
434 | |||
435 | @reraise_safe_exceptions |
|
435 | @reraise_safe_exceptions | |
436 | def bulk_file_request(self, wire, commit_id, path, pre_load): |
|
436 | def bulk_file_request(self, wire, commit_id, path, pre_load): | |
437 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
437 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
438 | region = self._region(wire) |
|
438 | region = self._region(wire) | |
439 |
|
439 | |||
440 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
440 | @region.conditional_cache_on_arguments(condition=cache_on) | |
441 | def _bulk_file_request(_repo_id, _commit_id, _path, _pre_load): |
|
441 | def _bulk_file_request(_repo_id, _commit_id, _path, _pre_load): | |
442 | result = {} |
|
442 | result = {} | |
443 | for attr in pre_load: |
|
443 | for attr in pre_load: | |
444 | try: |
|
444 | try: | |
445 | method = self._bulk_file_methods[attr] |
|
445 | method = self._bulk_file_methods[attr] | |
446 | wire.update({'cache': False}) # disable cache for bulk calls so we don't double cache |
|
446 | wire.update({'cache': False}) # disable cache for bulk calls so we don't double cache | |
447 | result[attr] = method(wire, _commit_id, _path) |
|
447 | result[attr] = method(wire, _commit_id, _path) | |
448 | except KeyError as e: |
|
448 | except KeyError as e: | |
449 | raise exceptions.VcsException(e)(f'Unknown bulk attribute: "{attr}"') |
|
449 | raise exceptions.VcsException(e)(f'Unknown bulk attribute: "{attr}"') | |
450 | return result |
|
450 | return result | |
451 |
|
451 | |||
452 | return BinaryEnvelope(_bulk_file_request(repo_id, commit_id, path, sorted(pre_load))) |
|
452 | return BinaryEnvelope(_bulk_file_request(repo_id, commit_id, path, sorted(pre_load))) | |
453 |
|
453 | |||
454 | def _build_opener(self, url: str): |
|
454 | def _build_opener(self, url: str): | |
455 | handlers = [] |
|
455 | handlers = [] | |
456 | url_obj = url_parser(safe_bytes(url)) |
|
456 | url_obj = url_parser(safe_bytes(url)) | |
457 | authinfo = url_obj.authinfo()[1] |
|
457 | authinfo = url_obj.authinfo()[1] | |
458 |
|
458 | |||
459 | if authinfo: |
|
459 | if authinfo: | |
460 | # create a password manager |
|
460 | # create a password manager | |
461 | passmgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() |
|
461 | passmgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() | |
462 | passmgr.add_password(*authinfo) |
|
462 | passmgr.add_password(*authinfo) | |
463 |
|
463 | |||
464 | handlers.extend((httpbasicauthhandler(passmgr), |
|
464 | handlers.extend((httpbasicauthhandler(passmgr), | |
465 | httpdigestauthhandler(passmgr))) |
|
465 | httpdigestauthhandler(passmgr))) | |
466 |
|
466 | |||
467 | return urllib.request.build_opener(*handlers) |
|
467 | return urllib.request.build_opener(*handlers) | |
468 |
|
468 | |||
469 | @reraise_safe_exceptions |
|
469 | @reraise_safe_exceptions | |
470 | def check_url(self, url, config): |
|
470 | def check_url(self, url, config): | |
471 | url_obj = url_parser(safe_bytes(url)) |
|
471 | url_obj = url_parser(safe_bytes(url)) | |
472 |
|
472 | |||
473 | test_uri = safe_str(url_obj.authinfo()[0]) |
|
473 | test_uri = safe_str(url_obj.authinfo()[0]) | |
474 | obfuscated_uri = get_obfuscated_url(url_obj) |
|
474 | obfuscated_uri = get_obfuscated_url(url_obj) | |
475 |
|
475 | |||
476 | log.info("Checking URL for remote cloning/import: %s", obfuscated_uri) |
|
476 | log.info("Checking URL for remote cloning/import: %s", obfuscated_uri) | |
477 |
|
477 | |||
478 | if not test_uri.endswith('info/refs'): |
|
478 | if not test_uri.endswith('info/refs'): | |
479 | test_uri = test_uri.rstrip('/') + '/info/refs' |
|
479 | test_uri = test_uri.rstrip('/') + '/info/refs' | |
480 |
|
480 | |||
481 | o = self._build_opener(test_uri) |
|
481 | o = self._build_opener(test_uri) | |
482 | o.addheaders = [('User-Agent', 'git/1.7.8.0')] # fake some git |
|
482 | o.addheaders = [('User-Agent', 'git/1.7.8.0')] # fake some git | |
483 |
|
483 | |||
484 | q = {"service": 'git-upload-pack'} |
|
484 | q = {"service": 'git-upload-pack'} | |
485 |
qs = '? |
|
485 | qs = f'?{urllib.parse.urlencode(q)}' | |
486 | cu = f"{test_uri}{qs}" |
|
486 | cu = f"{test_uri}{qs}" | |
487 | req = urllib.request.Request(cu, None, {}) |
|
487 | req = urllib.request.Request(cu, None, {}) | |
488 |
|
488 | |||
489 | try: |
|
489 | try: | |
490 | log.debug("Trying to open URL %s", obfuscated_uri) |
|
490 | log.debug("Trying to open URL %s", obfuscated_uri) | |
491 | resp = o.open(req) |
|
491 | resp = o.open(req) | |
492 | if resp.code != 200: |
|
492 | if resp.code != 200: | |
493 | raise exceptions.URLError()('Return Code is not 200') |
|
493 | raise exceptions.URLError()('Return Code is not 200') | |
494 | except Exception as e: |
|
494 | except Exception as e: | |
495 | log.warning("URL cannot be opened: %s", obfuscated_uri, exc_info=True) |
|
495 | log.warning("URL cannot be opened: %s", obfuscated_uri, exc_info=True) | |
496 | # means it cannot be cloned |
|
496 | # means it cannot be cloned | |
497 | raise exceptions.URLError(e)(f"[{obfuscated_uri}] org_exc: {e}") |
|
497 | raise exceptions.URLError(e)(f"[{obfuscated_uri}] org_exc: {e}") | |
498 |
|
498 | |||
499 | # now detect if it's proper git repo |
|
499 | # now detect if it's proper git repo | |
500 | gitdata: bytes = resp.read() |
|
500 | gitdata: bytes = resp.read() | |
501 |
|
501 | |||
502 | if b'service=git-upload-pack' in gitdata: |
|
502 | if b'service=git-upload-pack' in gitdata: | |
503 | pass |
|
503 | pass | |
504 | elif re.findall(br'[0-9a-fA-F]{40}\s+refs', gitdata): |
|
504 | elif re.findall(br'[0-9a-fA-F]{40}\s+refs', gitdata): | |
505 | # old style git can return some other format ! |
|
505 | # old style git can return some other format ! | |
506 | pass |
|
506 | pass | |
507 | else: |
|
507 | else: | |
508 | e = None |
|
508 | e = None | |
509 | raise exceptions.URLError(e)( |
|
509 | raise exceptions.URLError(e)( | |
510 |
"url [ |
|
510 | f"url [{obfuscated_uri}] does not look like an hg repo org_exc: {e}") | |
511 | % (obfuscated_uri, e)) |
|
|||
512 |
|
511 | |||
513 | return True |
|
512 | return True | |
514 |
|
513 | |||
515 | @reraise_safe_exceptions |
|
514 | @reraise_safe_exceptions | |
516 | def clone(self, wire, url, deferred, valid_refs, update_after_clone): |
|
515 | def clone(self, wire, url, deferred, valid_refs, update_after_clone): | |
517 | # TODO(marcink): deprecate this method. Last i checked we don't use it anymore |
|
516 | # TODO(marcink): deprecate this method. Last i checked we don't use it anymore | |
518 | remote_refs = self.pull(wire, url, apply_refs=False) |
|
517 | remote_refs = self.pull(wire, url, apply_refs=False) | |
519 | repo = self._factory.repo(wire) |
|
518 | repo = self._factory.repo(wire) | |
520 | if isinstance(valid_refs, list): |
|
519 | if isinstance(valid_refs, list): | |
521 | valid_refs = tuple(valid_refs) |
|
520 | valid_refs = tuple(valid_refs) | |
522 |
|
521 | |||
523 | for k in remote_refs: |
|
522 | for k in remote_refs: | |
524 | # only parse heads/tags and skip so called deferred tags |
|
523 | # only parse heads/tags and skip so called deferred tags | |
525 | if k.startswith(valid_refs) and not k.endswith(deferred): |
|
524 | if k.startswith(valid_refs) and not k.endswith(deferred): | |
526 | repo[k] = remote_refs[k] |
|
525 | repo[k] = remote_refs[k] | |
527 |
|
526 | |||
528 | if update_after_clone: |
|
527 | if update_after_clone: | |
529 | # we want to checkout HEAD |
|
528 | # we want to checkout HEAD | |
530 | repo["HEAD"] = remote_refs["HEAD"] |
|
529 | repo["HEAD"] = remote_refs["HEAD"] | |
531 | index.build_index_from_tree(repo.path, repo.index_path(), |
|
530 | index.build_index_from_tree(repo.path, repo.index_path(), | |
532 | repo.object_store, repo["HEAD"].tree) |
|
531 | repo.object_store, repo["HEAD"].tree) | |
533 |
|
532 | |||
534 | @reraise_safe_exceptions |
|
533 | @reraise_safe_exceptions | |
535 | def branch(self, wire, commit_id): |
|
534 | def branch(self, wire, commit_id): | |
536 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
535 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
537 | region = self._region(wire) |
|
536 | region = self._region(wire) | |
538 |
|
537 | |||
539 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
538 | @region.conditional_cache_on_arguments(condition=cache_on) | |
540 | def _branch(_context_uid, _repo_id, _commit_id): |
|
539 | def _branch(_context_uid, _repo_id, _commit_id): | |
541 | regex = re.compile('^refs/heads') |
|
540 | regex = re.compile('^refs/heads') | |
542 |
|
541 | |||
543 | def filter_with(ref): |
|
542 | def filter_with(ref): | |
544 | return regex.match(ref[0]) and ref[1] == _commit_id |
|
543 | return regex.match(ref[0]) and ref[1] == _commit_id | |
545 |
|
544 | |||
546 | branches = list(filter(filter_with, list(self.get_refs(wire).items()))) |
|
545 | branches = list(filter(filter_with, list(self.get_refs(wire).items()))) | |
547 | return [x[0].split('refs/heads/')[-1] for x in branches] |
|
546 | return [x[0].split('refs/heads/')[-1] for x in branches] | |
548 |
|
547 | |||
549 | return _branch(context_uid, repo_id, commit_id) |
|
548 | return _branch(context_uid, repo_id, commit_id) | |
550 |
|
549 | |||
551 | @reraise_safe_exceptions |
|
550 | @reraise_safe_exceptions | |
552 | def commit_branches(self, wire, commit_id): |
|
551 | def commit_branches(self, wire, commit_id): | |
553 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
552 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
554 | region = self._region(wire) |
|
553 | region = self._region(wire) | |
555 |
|
554 | |||
556 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
555 | @region.conditional_cache_on_arguments(condition=cache_on) | |
557 | def _commit_branches(_context_uid, _repo_id, _commit_id): |
|
556 | def _commit_branches(_context_uid, _repo_id, _commit_id): | |
558 | repo_init = self._factory.repo_libgit2(wire) |
|
557 | repo_init = self._factory.repo_libgit2(wire) | |
559 | with repo_init as repo: |
|
558 | with repo_init as repo: | |
560 | branches = [x for x in repo.branches.with_commit(_commit_id)] |
|
559 | branches = [x for x in repo.branches.with_commit(_commit_id)] | |
561 | return branches |
|
560 | return branches | |
562 |
|
561 | |||
563 | return _commit_branches(context_uid, repo_id, commit_id) |
|
562 | return _commit_branches(context_uid, repo_id, commit_id) | |
564 |
|
563 | |||
565 | @reraise_safe_exceptions |
|
564 | @reraise_safe_exceptions | |
566 | def add_object(self, wire, content): |
|
565 | def add_object(self, wire, content): | |
567 | repo_init = self._factory.repo_libgit2(wire) |
|
566 | repo_init = self._factory.repo_libgit2(wire) | |
568 | with repo_init as repo: |
|
567 | with repo_init as repo: | |
569 | blob = objects.Blob() |
|
568 | blob = objects.Blob() | |
570 | blob.set_raw_string(content) |
|
569 | blob.set_raw_string(content) | |
571 | repo.object_store.add_object(blob) |
|
570 | repo.object_store.add_object(blob) | |
572 | return blob.id |
|
571 | return blob.id | |
573 |
|
572 | |||
574 | @reraise_safe_exceptions |
|
573 | @reraise_safe_exceptions | |
575 | def create_commit(self, wire, author, committer, message, branch, new_tree_id, date_args: list[int, int] = None): |
|
574 | def create_commit(self, wire, author, committer, message, branch, new_tree_id, date_args: list[int, int] = None): | |
576 | repo_init = self._factory.repo_libgit2(wire) |
|
575 | repo_init = self._factory.repo_libgit2(wire) | |
577 | with repo_init as repo: |
|
576 | with repo_init as repo: | |
578 |
|
577 | |||
579 | if date_args: |
|
578 | if date_args: | |
580 | current_time, offset = date_args |
|
579 | current_time, offset = date_args | |
581 |
|
580 | |||
582 | kw = { |
|
581 | kw = { | |
583 | 'time': current_time, |
|
582 | 'time': current_time, | |
584 | 'offset': offset |
|
583 | 'offset': offset | |
585 | } |
|
584 | } | |
586 | author = create_signature_from_string(author, **kw) |
|
585 | author = create_signature_from_string(author, **kw) | |
587 | committer = create_signature_from_string(committer, **kw) |
|
586 | committer = create_signature_from_string(committer, **kw) | |
588 |
|
587 | |||
589 | tree = new_tree_id |
|
588 | tree = new_tree_id | |
590 | if isinstance(tree, (bytes, str)): |
|
589 | if isinstance(tree, (bytes, str)): | |
591 | # validate this tree is in the repo... |
|
590 | # validate this tree is in the repo... | |
592 | tree = repo[safe_str(tree)].id |
|
591 | tree = repo[safe_str(tree)].id | |
593 |
|
592 | |||
594 | parents = [] |
|
593 | parents = [] | |
595 | # ensure we COMMIT on top of given branch head |
|
594 | # ensure we COMMIT on top of given branch head | |
596 | # check if this repo has ANY branches, otherwise it's a new branch case we need to make |
|
595 | # check if this repo has ANY branches, otherwise it's a new branch case we need to make | |
597 | if branch in repo.branches.local: |
|
596 | if branch in repo.branches.local: | |
598 | parents += [repo.branches[branch].target] |
|
597 | parents += [repo.branches[branch].target] | |
599 | elif [x for x in repo.branches.local]: |
|
598 | elif [x for x in repo.branches.local]: | |
600 | parents += [repo.head.target] |
|
599 | parents += [repo.head.target] | |
601 | #else: |
|
600 | #else: | |
602 | # in case we want to commit on new branch we create it on top of HEAD |
|
601 | # in case we want to commit on new branch we create it on top of HEAD | |
603 | #repo.branches.local.create(branch, repo.revparse_single('HEAD')) |
|
602 | #repo.branches.local.create(branch, repo.revparse_single('HEAD')) | |
604 |
|
603 | |||
605 | # # Create a new commit |
|
604 | # # Create a new commit | |
606 | commit_oid = repo.create_commit( |
|
605 | commit_oid = repo.create_commit( | |
607 | f'refs/heads/{branch}', # the name of the reference to update |
|
606 | f'refs/heads/{branch}', # the name of the reference to update | |
608 | author, # the author of the commit |
|
607 | author, # the author of the commit | |
609 | committer, # the committer of the commit |
|
608 | committer, # the committer of the commit | |
610 | message, # the commit message |
|
609 | message, # the commit message | |
611 | tree, # the tree produced by the index |
|
610 | tree, # the tree produced by the index | |
612 | parents # list of parents for the new commit, usually just one, |
|
611 | parents # list of parents for the new commit, usually just one, | |
613 | ) |
|
612 | ) | |
614 |
|
613 | |||
615 | new_commit_id = safe_str(commit_oid) |
|
614 | new_commit_id = safe_str(commit_oid) | |
616 |
|
615 | |||
617 | return new_commit_id |
|
616 | return new_commit_id | |
618 |
|
617 | |||
619 | @reraise_safe_exceptions |
|
618 | @reraise_safe_exceptions | |
620 | def commit(self, wire, commit_data, branch, commit_tree, updated, removed): |
|
619 | def commit(self, wire, commit_data, branch, commit_tree, updated, removed): | |
621 |
|
620 | |||
622 | def mode2pygit(mode): |
|
621 | def mode2pygit(mode): | |
623 | """ |
|
622 | """ | |
624 | git only supports two filemode 644 and 755 |
|
623 | git only supports two filemode 644 and 755 | |
625 |
|
624 | |||
626 | 0o100755 -> 33261 |
|
625 | 0o100755 -> 33261 | |
627 | 0o100644 -> 33188 |
|
626 | 0o100644 -> 33188 | |
628 | """ |
|
627 | """ | |
629 | return { |
|
628 | return { | |
630 | 0o100644: pygit2.GIT_FILEMODE_BLOB, |
|
629 | 0o100644: pygit2.GIT_FILEMODE_BLOB, | |
631 | 0o100755: pygit2.GIT_FILEMODE_BLOB_EXECUTABLE, |
|
630 | 0o100755: pygit2.GIT_FILEMODE_BLOB_EXECUTABLE, | |
632 | 0o120000: pygit2.GIT_FILEMODE_LINK |
|
631 | 0o120000: pygit2.GIT_FILEMODE_LINK | |
633 | }.get(mode) or pygit2.GIT_FILEMODE_BLOB |
|
632 | }.get(mode) or pygit2.GIT_FILEMODE_BLOB | |
634 |
|
633 | |||
635 | repo_init = self._factory.repo_libgit2(wire) |
|
634 | repo_init = self._factory.repo_libgit2(wire) | |
636 | with repo_init as repo: |
|
635 | with repo_init as repo: | |
637 | repo_index = repo.index |
|
636 | repo_index = repo.index | |
638 |
|
637 | |||
639 | for pathspec in updated: |
|
638 | for pathspec in updated: | |
640 | blob_id = repo.create_blob(pathspec['content']) |
|
639 | blob_id = repo.create_blob(pathspec['content']) | |
641 | ie = pygit2.IndexEntry(pathspec['path'], blob_id, mode2pygit(pathspec['mode'])) |
|
640 | ie = pygit2.IndexEntry(pathspec['path'], blob_id, mode2pygit(pathspec['mode'])) | |
642 | repo_index.add(ie) |
|
641 | repo_index.add(ie) | |
643 |
|
642 | |||
644 | for pathspec in removed: |
|
643 | for pathspec in removed: | |
645 | repo_index.remove(pathspec) |
|
644 | repo_index.remove(pathspec) | |
646 |
|
645 | |||
647 | # Write changes to the index |
|
646 | # Write changes to the index | |
648 | repo_index.write() |
|
647 | repo_index.write() | |
649 |
|
648 | |||
650 | # Create a tree from the updated index |
|
649 | # Create a tree from the updated index | |
651 | commit_tree = repo_index.write_tree() |
|
650 | commit_tree = repo_index.write_tree() | |
652 |
|
651 | |||
653 | new_tree_id = commit_tree |
|
652 | new_tree_id = commit_tree | |
654 |
|
653 | |||
655 | author = commit_data['author'] |
|
654 | author = commit_data['author'] | |
656 | committer = commit_data['committer'] |
|
655 | committer = commit_data['committer'] | |
657 | message = commit_data['message'] |
|
656 | message = commit_data['message'] | |
658 |
|
657 | |||
659 | date_args = [int(commit_data['commit_time']), int(commit_data['commit_timezone'])] |
|
658 | date_args = [int(commit_data['commit_time']), int(commit_data['commit_timezone'])] | |
660 |
|
659 | |||
661 | new_commit_id = self.create_commit(wire, author, committer, message, branch, |
|
660 | new_commit_id = self.create_commit(wire, author, committer, message, branch, | |
662 | new_tree_id, date_args=date_args) |
|
661 | new_tree_id, date_args=date_args) | |
663 |
|
662 | |||
664 | # libgit2, ensure the branch is there and exists |
|
663 | # libgit2, ensure the branch is there and exists | |
665 | self.create_branch(wire, branch, new_commit_id) |
|
664 | self.create_branch(wire, branch, new_commit_id) | |
666 |
|
665 | |||
667 | # libgit2, set new ref to this created commit |
|
666 | # libgit2, set new ref to this created commit | |
668 | self.set_refs(wire, f'refs/heads/{branch}', new_commit_id) |
|
667 | self.set_refs(wire, f'refs/heads/{branch}', new_commit_id) | |
669 |
|
668 | |||
670 | return new_commit_id |
|
669 | return new_commit_id | |
671 |
|
670 | |||
672 | @reraise_safe_exceptions |
|
671 | @reraise_safe_exceptions | |
673 | def pull(self, wire, url, apply_refs=True, refs=None, update_after=False): |
|
672 | def pull(self, wire, url, apply_refs=True, refs=None, update_after=False): | |
674 | if url != 'default' and '://' not in url: |
|
673 | if url != 'default' and '://' not in url: | |
675 | client = LocalGitClient(url) |
|
674 | client = LocalGitClient(url) | |
676 | else: |
|
675 | else: | |
677 | url_obj = url_parser(safe_bytes(url)) |
|
676 | url_obj = url_parser(safe_bytes(url)) | |
678 | o = self._build_opener(url) |
|
677 | o = self._build_opener(url) | |
679 | url = url_obj.authinfo()[0] |
|
678 | url = url_obj.authinfo()[0] | |
680 | client = HttpGitClient(base_url=url, opener=o) |
|
679 | client = HttpGitClient(base_url=url, opener=o) | |
681 | repo = self._factory.repo(wire) |
|
680 | repo = self._factory.repo(wire) | |
682 |
|
681 | |||
683 | determine_wants = repo.object_store.determine_wants_all |
|
682 | determine_wants = repo.object_store.determine_wants_all | |
684 | if refs: |
|
683 | if refs: | |
685 | refs = [ascii_bytes(x) for x in refs] |
|
684 | refs = [ascii_bytes(x) for x in refs] | |
686 |
|
685 | |||
687 | def determine_wants_requested(remote_refs): |
|
686 | def determine_wants_requested(remote_refs): | |
688 | determined = [] |
|
687 | determined = [] | |
689 | for ref_name, ref_hash in remote_refs.items(): |
|
688 | for ref_name, ref_hash in remote_refs.items(): | |
690 | bytes_ref_name = safe_bytes(ref_name) |
|
689 | bytes_ref_name = safe_bytes(ref_name) | |
691 |
|
690 | |||
692 | if bytes_ref_name in refs: |
|
691 | if bytes_ref_name in refs: | |
693 | bytes_ref_hash = safe_bytes(ref_hash) |
|
692 | bytes_ref_hash = safe_bytes(ref_hash) | |
694 | determined.append(bytes_ref_hash) |
|
693 | determined.append(bytes_ref_hash) | |
695 | return determined |
|
694 | return determined | |
696 |
|
695 | |||
697 | # swap with our custom requested wants |
|
696 | # swap with our custom requested wants | |
698 | determine_wants = determine_wants_requested |
|
697 | determine_wants = determine_wants_requested | |
699 |
|
698 | |||
700 | try: |
|
699 | try: | |
701 | remote_refs = client.fetch( |
|
700 | remote_refs = client.fetch( | |
702 | path=url, target=repo, determine_wants=determine_wants) |
|
701 | path=url, target=repo, determine_wants=determine_wants) | |
703 |
|
702 | |||
704 | except NotGitRepository as e: |
|
703 | except NotGitRepository as e: | |
705 | log.warning( |
|
704 | log.warning( | |
706 | 'Trying to fetch from "%s" failed, not a Git repository.', url) |
|
705 | 'Trying to fetch from "%s" failed, not a Git repository.', url) | |
707 | # Exception can contain unicode which we convert |
|
706 | # Exception can contain unicode which we convert | |
708 | raise exceptions.AbortException(e)(repr(e)) |
|
707 | raise exceptions.AbortException(e)(repr(e)) | |
709 |
|
708 | |||
710 | # mikhail: client.fetch() returns all the remote refs, but fetches only |
|
709 | # mikhail: client.fetch() returns all the remote refs, but fetches only | |
711 | # refs filtered by `determine_wants` function. We need to filter result |
|
710 | # refs filtered by `determine_wants` function. We need to filter result | |
712 | # as well |
|
711 | # as well | |
713 | if refs: |
|
712 | if refs: | |
714 | remote_refs = {k: remote_refs[k] for k in remote_refs if k in refs} |
|
713 | remote_refs = {k: remote_refs[k] for k in remote_refs if k in refs} | |
715 |
|
714 | |||
716 | if apply_refs: |
|
715 | if apply_refs: | |
717 | # TODO: johbo: Needs proper test coverage with a git repository |
|
716 | # TODO: johbo: Needs proper test coverage with a git repository | |
718 | # that contains a tag object, so that we would end up with |
|
717 | # that contains a tag object, so that we would end up with | |
719 | # a peeled ref at this point. |
|
718 | # a peeled ref at this point. | |
720 | for k in remote_refs: |
|
719 | for k in remote_refs: | |
721 | if k.endswith(PEELED_REF_MARKER): |
|
720 | if k.endswith(PEELED_REF_MARKER): | |
722 | log.debug("Skipping peeled reference %s", k) |
|
721 | log.debug("Skipping peeled reference %s", k) | |
723 | continue |
|
722 | continue | |
724 | repo[k] = remote_refs[k] |
|
723 | repo[k] = remote_refs[k] | |
725 |
|
724 | |||
726 | if refs and not update_after: |
|
725 | if refs and not update_after: | |
727 | # mikhail: explicitly set the head to the last ref. |
|
726 | # mikhail: explicitly set the head to the last ref. | |
728 | repo[HEAD_MARKER] = remote_refs[refs[-1]] |
|
727 | repo[HEAD_MARKER] = remote_refs[refs[-1]] | |
729 |
|
728 | |||
730 | if update_after: |
|
729 | if update_after: | |
731 | # we want to check out HEAD |
|
730 | # we want to check out HEAD | |
732 | repo[HEAD_MARKER] = remote_refs[HEAD_MARKER] |
|
731 | repo[HEAD_MARKER] = remote_refs[HEAD_MARKER] | |
733 | index.build_index_from_tree(repo.path, repo.index_path(), |
|
732 | index.build_index_from_tree(repo.path, repo.index_path(), | |
734 | repo.object_store, repo[HEAD_MARKER].tree) |
|
733 | repo.object_store, repo[HEAD_MARKER].tree) | |
735 |
|
734 | |||
736 | if isinstance(remote_refs, FetchPackResult): |
|
735 | if isinstance(remote_refs, FetchPackResult): | |
737 | return remote_refs.refs |
|
736 | return remote_refs.refs | |
738 | return remote_refs |
|
737 | return remote_refs | |
739 |
|
738 | |||
740 | @reraise_safe_exceptions |
|
739 | @reraise_safe_exceptions | |
741 | def sync_fetch(self, wire, url, refs=None, all_refs=False): |
|
740 | def sync_fetch(self, wire, url, refs=None, all_refs=False): | |
742 | self._factory.repo(wire) |
|
741 | self._factory.repo(wire) | |
743 | if refs and not isinstance(refs, (list, tuple)): |
|
742 | if refs and not isinstance(refs, (list, tuple)): | |
744 | refs = [refs] |
|
743 | refs = [refs] | |
745 |
|
744 | |||
746 | config = self._wire_to_config(wire) |
|
745 | config = self._wire_to_config(wire) | |
747 | # get all remote refs we'll use to fetch later |
|
746 | # get all remote refs we'll use to fetch later | |
748 | cmd = ['ls-remote'] |
|
747 | cmd = ['ls-remote'] | |
749 | if not all_refs: |
|
748 | if not all_refs: | |
750 | cmd += ['--heads', '--tags'] |
|
749 | cmd += ['--heads', '--tags'] | |
751 | cmd += [url] |
|
750 | cmd += [url] | |
752 | output, __ = self.run_git_command( |
|
751 | output, __ = self.run_git_command( | |
753 | wire, cmd, fail_on_stderr=False, |
|
752 | wire, cmd, fail_on_stderr=False, | |
754 | _copts=self._remote_conf(config), |
|
753 | _copts=self._remote_conf(config), | |
755 | extra_env={'GIT_TERMINAL_PROMPT': '0'}) |
|
754 | extra_env={'GIT_TERMINAL_PROMPT': '0'}) | |
756 |
|
755 | |||
757 | remote_refs = collections.OrderedDict() |
|
756 | remote_refs = collections.OrderedDict() | |
758 | fetch_refs = [] |
|
757 | fetch_refs = [] | |
759 |
|
758 | |||
760 | for ref_line in output.splitlines(): |
|
759 | for ref_line in output.splitlines(): | |
761 | sha, ref = ref_line.split(b'\t') |
|
760 | sha, ref = ref_line.split(b'\t') | |
762 | sha = sha.strip() |
|
761 | sha = sha.strip() | |
763 | if ref in remote_refs: |
|
762 | if ref in remote_refs: | |
764 | # duplicate, skip |
|
763 | # duplicate, skip | |
765 | continue |
|
764 | continue | |
766 | if ref.endswith(PEELED_REF_MARKER): |
|
765 | if ref.endswith(PEELED_REF_MARKER): | |
767 | log.debug("Skipping peeled reference %s", ref) |
|
766 | log.debug("Skipping peeled reference %s", ref) | |
768 | continue |
|
767 | continue | |
769 | # don't sync HEAD |
|
768 | # don't sync HEAD | |
770 | if ref in [HEAD_MARKER]: |
|
769 | if ref in [HEAD_MARKER]: | |
771 | continue |
|
770 | continue | |
772 |
|
771 | |||
773 | remote_refs[ref] = sha |
|
772 | remote_refs[ref] = sha | |
774 |
|
773 | |||
775 | if refs and sha in refs: |
|
774 | if refs and sha in refs: | |
776 | # we filter fetch using our specified refs |
|
775 | # we filter fetch using our specified refs | |
777 | fetch_refs.append(f'{safe_str(ref)}:{safe_str(ref)}') |
|
776 | fetch_refs.append(f'{safe_str(ref)}:{safe_str(ref)}') | |
778 | elif not refs: |
|
777 | elif not refs: | |
779 | fetch_refs.append(f'{safe_str(ref)}:{safe_str(ref)}') |
|
778 | fetch_refs.append(f'{safe_str(ref)}:{safe_str(ref)}') | |
780 | log.debug('Finished obtaining fetch refs, total: %s', len(fetch_refs)) |
|
779 | log.debug('Finished obtaining fetch refs, total: %s', len(fetch_refs)) | |
781 |
|
780 | |||
782 | if fetch_refs: |
|
781 | if fetch_refs: | |
783 | for chunk in more_itertools.chunked(fetch_refs, 1024 * 4): |
|
782 | for chunk in more_itertools.chunked(fetch_refs, 1024 * 4): | |
784 | fetch_refs_chunks = list(chunk) |
|
783 | fetch_refs_chunks = list(chunk) | |
785 | log.debug('Fetching %s refs from import url', len(fetch_refs_chunks)) |
|
784 | log.debug('Fetching %s refs from import url', len(fetch_refs_chunks)) | |
786 | self.run_git_command( |
|
785 | self.run_git_command( | |
787 | wire, ['fetch', url, '--force', '--prune', '--'] + fetch_refs_chunks, |
|
786 | wire, ['fetch', url, '--force', '--prune', '--'] + fetch_refs_chunks, | |
788 | fail_on_stderr=False, |
|
787 | fail_on_stderr=False, | |
789 | _copts=self._remote_conf(config), |
|
788 | _copts=self._remote_conf(config), | |
790 | extra_env={'GIT_TERMINAL_PROMPT': '0'}) |
|
789 | extra_env={'GIT_TERMINAL_PROMPT': '0'}) | |
791 |
|
790 | |||
792 | return remote_refs |
|
791 | return remote_refs | |
793 |
|
792 | |||
794 | @reraise_safe_exceptions |
|
793 | @reraise_safe_exceptions | |
795 | def sync_push(self, wire, url, refs=None): |
|
794 | def sync_push(self, wire, url, refs=None): | |
796 | if not self.check_url(url, wire): |
|
795 | if not self.check_url(url, wire): | |
797 | return |
|
796 | return | |
798 | config = self._wire_to_config(wire) |
|
797 | config = self._wire_to_config(wire) | |
799 | self._factory.repo(wire) |
|
798 | self._factory.repo(wire) | |
800 | self.run_git_command( |
|
799 | self.run_git_command( | |
801 | wire, ['push', url, '--mirror'], fail_on_stderr=False, |
|
800 | wire, ['push', url, '--mirror'], fail_on_stderr=False, | |
802 | _copts=self._remote_conf(config), |
|
801 | _copts=self._remote_conf(config), | |
803 | extra_env={'GIT_TERMINAL_PROMPT': '0'}) |
|
802 | extra_env={'GIT_TERMINAL_PROMPT': '0'}) | |
804 |
|
803 | |||
805 | @reraise_safe_exceptions |
|
804 | @reraise_safe_exceptions | |
806 | def get_remote_refs(self, wire, url): |
|
805 | def get_remote_refs(self, wire, url): | |
807 | repo = Repo(url) |
|
806 | repo = Repo(url) | |
808 | return repo.get_refs() |
|
807 | return repo.get_refs() | |
809 |
|
808 | |||
810 | @reraise_safe_exceptions |
|
809 | @reraise_safe_exceptions | |
811 | def get_description(self, wire): |
|
810 | def get_description(self, wire): | |
812 | repo = self._factory.repo(wire) |
|
811 | repo = self._factory.repo(wire) | |
813 | return repo.get_description() |
|
812 | return repo.get_description() | |
814 |
|
813 | |||
815 | @reraise_safe_exceptions |
|
814 | @reraise_safe_exceptions | |
816 | def get_missing_revs(self, wire, rev1, rev2, path2): |
|
815 | def get_missing_revs(self, wire, rev1, rev2, path2): | |
817 | repo = self._factory.repo(wire) |
|
816 | repo = self._factory.repo(wire) | |
818 | LocalGitClient(thin_packs=False).fetch(path2, repo) |
|
817 | LocalGitClient(thin_packs=False).fetch(path2, repo) | |
819 |
|
818 | |||
820 | wire_remote = wire.copy() |
|
819 | wire_remote = wire.copy() | |
821 | wire_remote['path'] = path2 |
|
820 | wire_remote['path'] = path2 | |
822 | repo_remote = self._factory.repo(wire_remote) |
|
821 | repo_remote = self._factory.repo(wire_remote) | |
823 | LocalGitClient(thin_packs=False).fetch(path2, repo_remote) |
|
822 | LocalGitClient(thin_packs=False).fetch(path2, repo_remote) | |
824 |
|
823 | |||
825 | revs = [ |
|
824 | revs = [ | |
826 | x.commit.id |
|
825 | x.commit.id | |
827 | for x in repo_remote.get_walker(include=[safe_bytes(rev2)], exclude=[safe_bytes(rev1)])] |
|
826 | for x in repo_remote.get_walker(include=[safe_bytes(rev2)], exclude=[safe_bytes(rev1)])] | |
828 | return revs |
|
827 | return revs | |
829 |
|
828 | |||
830 | @reraise_safe_exceptions |
|
829 | @reraise_safe_exceptions | |
831 | def get_object(self, wire, sha, maybe_unreachable=False): |
|
830 | def get_object(self, wire, sha, maybe_unreachable=False): | |
832 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
831 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
833 | region = self._region(wire) |
|
832 | region = self._region(wire) | |
834 |
|
833 | |||
835 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
834 | @region.conditional_cache_on_arguments(condition=cache_on) | |
836 | def _get_object(_context_uid, _repo_id, _sha): |
|
835 | def _get_object(_context_uid, _repo_id, _sha): | |
837 | repo_init = self._factory.repo_libgit2(wire) |
|
836 | repo_init = self._factory.repo_libgit2(wire) | |
838 | with repo_init as repo: |
|
837 | with repo_init as repo: | |
839 |
|
838 | |||
840 | missing_commit_err = 'Commit {} does not exist for `{}`'.format(sha, wire['path']) |
|
839 | missing_commit_err = 'Commit {} does not exist for `{}`'.format(sha, wire['path']) | |
841 | try: |
|
840 | try: | |
842 | commit = repo.revparse_single(sha) |
|
841 | commit = repo.revparse_single(sha) | |
843 | except KeyError: |
|
842 | except KeyError: | |
844 | # NOTE(marcink): KeyError doesn't give us any meaningful information |
|
843 | # NOTE(marcink): KeyError doesn't give us any meaningful information | |
845 | # here, we instead give something more explicit |
|
844 | # here, we instead give something more explicit | |
846 | e = exceptions.RefNotFoundException('SHA: %s not found', sha) |
|
845 | e = exceptions.RefNotFoundException('SHA: %s not found', sha) | |
847 | raise exceptions.LookupException(e)(missing_commit_err) |
|
846 | raise exceptions.LookupException(e)(missing_commit_err) | |
848 | except ValueError as e: |
|
847 | except ValueError as e: | |
849 | raise exceptions.LookupException(e)(missing_commit_err) |
|
848 | raise exceptions.LookupException(e)(missing_commit_err) | |
850 |
|
849 | |||
851 | is_tag = False |
|
850 | is_tag = False | |
852 | if isinstance(commit, pygit2.Tag): |
|
851 | if isinstance(commit, pygit2.Tag): | |
853 | commit = repo.get(commit.target) |
|
852 | commit = repo.get(commit.target) | |
854 | is_tag = True |
|
853 | is_tag = True | |
855 |
|
854 | |||
856 | check_dangling = True |
|
855 | check_dangling = True | |
857 | if is_tag: |
|
856 | if is_tag: | |
858 | check_dangling = False |
|
857 | check_dangling = False | |
859 |
|
858 | |||
860 | if check_dangling and maybe_unreachable: |
|
859 | if check_dangling and maybe_unreachable: | |
861 | check_dangling = False |
|
860 | check_dangling = False | |
862 |
|
861 | |||
863 | # we used a reference and it parsed means we're not having a dangling commit |
|
862 | # we used a reference and it parsed means we're not having a dangling commit | |
864 | if sha != commit.hex: |
|
863 | if sha != commit.hex: | |
865 | check_dangling = False |
|
864 | check_dangling = False | |
866 |
|
865 | |||
867 | if check_dangling: |
|
866 | if check_dangling: | |
868 | # check for dangling commit |
|
867 | # check for dangling commit | |
869 | for branch in repo.branches.with_commit(commit.hex): |
|
868 | for branch in repo.branches.with_commit(commit.hex): | |
870 | if branch: |
|
869 | if branch: | |
871 | break |
|
870 | break | |
872 | else: |
|
871 | else: | |
873 | # NOTE(marcink): Empty error doesn't give us any meaningful information |
|
872 | # NOTE(marcink): Empty error doesn't give us any meaningful information | |
874 | # here, we instead give something more explicit |
|
873 | # here, we instead give something more explicit | |
875 | e = exceptions.RefNotFoundException('SHA: %s not found in branches', sha) |
|
874 | e = exceptions.RefNotFoundException('SHA: %s not found in branches', sha) | |
876 | raise exceptions.LookupException(e)(missing_commit_err) |
|
875 | raise exceptions.LookupException(e)(missing_commit_err) | |
877 |
|
876 | |||
878 | commit_id = commit.hex |
|
877 | commit_id = commit.hex | |
879 | type_str = commit.type_str |
|
878 | type_str = commit.type_str | |
880 |
|
879 | |||
881 | return { |
|
880 | return { | |
882 | 'id': commit_id, |
|
881 | 'id': commit_id, | |
883 | 'type': type_str, |
|
882 | 'type': type_str, | |
884 | 'commit_id': commit_id, |
|
883 | 'commit_id': commit_id, | |
885 | 'idx': 0 |
|
884 | 'idx': 0 | |
886 | } |
|
885 | } | |
887 |
|
886 | |||
888 | return _get_object(context_uid, repo_id, sha) |
|
887 | return _get_object(context_uid, repo_id, sha) | |
889 |
|
888 | |||
890 | @reraise_safe_exceptions |
|
889 | @reraise_safe_exceptions | |
891 | def get_refs(self, wire): |
|
890 | def get_refs(self, wire): | |
892 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
891 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
893 | region = self._region(wire) |
|
892 | region = self._region(wire) | |
894 |
|
893 | |||
895 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
894 | @region.conditional_cache_on_arguments(condition=cache_on) | |
896 | def _get_refs(_context_uid, _repo_id): |
|
895 | def _get_refs(_context_uid, _repo_id): | |
897 |
|
896 | |||
898 | repo_init = self._factory.repo_libgit2(wire) |
|
897 | repo_init = self._factory.repo_libgit2(wire) | |
899 | with repo_init as repo: |
|
898 | with repo_init as repo: | |
900 | regex = re.compile('^refs/(heads|tags)/') |
|
899 | regex = re.compile('^refs/(heads|tags)/') | |
901 | return {x.name: x.target.hex for x in |
|
900 | return {x.name: x.target.hex for x in | |
902 | [ref for ref in repo.listall_reference_objects() if regex.match(ref.name)]} |
|
901 | [ref for ref in repo.listall_reference_objects() if regex.match(ref.name)]} | |
903 |
|
902 | |||
904 | return _get_refs(context_uid, repo_id) |
|
903 | return _get_refs(context_uid, repo_id) | |
905 |
|
904 | |||
906 | @reraise_safe_exceptions |
|
905 | @reraise_safe_exceptions | |
907 | def get_branch_pointers(self, wire): |
|
906 | def get_branch_pointers(self, wire): | |
908 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
907 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
909 | region = self._region(wire) |
|
908 | region = self._region(wire) | |
910 |
|
909 | |||
911 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
910 | @region.conditional_cache_on_arguments(condition=cache_on) | |
912 | def _get_branch_pointers(_context_uid, _repo_id): |
|
911 | def _get_branch_pointers(_context_uid, _repo_id): | |
913 |
|
912 | |||
914 | repo_init = self._factory.repo_libgit2(wire) |
|
913 | repo_init = self._factory.repo_libgit2(wire) | |
915 | regex = re.compile('^refs/heads') |
|
914 | regex = re.compile('^refs/heads') | |
916 | with repo_init as repo: |
|
915 | with repo_init as repo: | |
917 | branches = [ref for ref in repo.listall_reference_objects() if regex.match(ref.name)] |
|
916 | branches = [ref for ref in repo.listall_reference_objects() if regex.match(ref.name)] | |
918 | return {x.target.hex: x.shorthand for x in branches} |
|
917 | return {x.target.hex: x.shorthand for x in branches} | |
919 |
|
918 | |||
920 | return _get_branch_pointers(context_uid, repo_id) |
|
919 | return _get_branch_pointers(context_uid, repo_id) | |
921 |
|
920 | |||
922 | @reraise_safe_exceptions |
|
921 | @reraise_safe_exceptions | |
923 | def head(self, wire, show_exc=True): |
|
922 | def head(self, wire, show_exc=True): | |
924 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
923 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
925 | region = self._region(wire) |
|
924 | region = self._region(wire) | |
926 |
|
925 | |||
927 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
926 | @region.conditional_cache_on_arguments(condition=cache_on) | |
928 | def _head(_context_uid, _repo_id, _show_exc): |
|
927 | def _head(_context_uid, _repo_id, _show_exc): | |
929 | repo_init = self._factory.repo_libgit2(wire) |
|
928 | repo_init = self._factory.repo_libgit2(wire) | |
930 | with repo_init as repo: |
|
929 | with repo_init as repo: | |
931 | try: |
|
930 | try: | |
932 | return repo.head.peel().hex |
|
931 | return repo.head.peel().hex | |
933 | except Exception: |
|
932 | except Exception: | |
934 | if show_exc: |
|
933 | if show_exc: | |
935 | raise |
|
934 | raise | |
936 | return _head(context_uid, repo_id, show_exc) |
|
935 | return _head(context_uid, repo_id, show_exc) | |
937 |
|
936 | |||
938 | @reraise_safe_exceptions |
|
937 | @reraise_safe_exceptions | |
939 | def init(self, wire): |
|
938 | def init(self, wire): | |
940 | repo_path = safe_str(wire['path']) |
|
939 | repo_path = safe_str(wire['path']) | |
941 | pygit2.init_repository(repo_path, bare=False) |
|
940 | pygit2.init_repository(repo_path, bare=False) | |
942 |
|
941 | |||
943 | @reraise_safe_exceptions |
|
942 | @reraise_safe_exceptions | |
944 | def init_bare(self, wire): |
|
943 | def init_bare(self, wire): | |
945 | repo_path = safe_str(wire['path']) |
|
944 | repo_path = safe_str(wire['path']) | |
946 | pygit2.init_repository(repo_path, bare=True) |
|
945 | pygit2.init_repository(repo_path, bare=True) | |
947 |
|
946 | |||
948 | @reraise_safe_exceptions |
|
947 | @reraise_safe_exceptions | |
949 | def revision(self, wire, rev): |
|
948 | def revision(self, wire, rev): | |
950 |
|
949 | |||
951 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
950 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
952 | region = self._region(wire) |
|
951 | region = self._region(wire) | |
953 |
|
952 | |||
954 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
953 | @region.conditional_cache_on_arguments(condition=cache_on) | |
955 | def _revision(_context_uid, _repo_id, _rev): |
|
954 | def _revision(_context_uid, _repo_id, _rev): | |
956 | repo_init = self._factory.repo_libgit2(wire) |
|
955 | repo_init = self._factory.repo_libgit2(wire) | |
957 | with repo_init as repo: |
|
956 | with repo_init as repo: | |
958 | commit = repo[rev] |
|
957 | commit = repo[rev] | |
959 | obj_data = { |
|
958 | obj_data = { | |
960 | 'id': commit.id.hex, |
|
959 | 'id': commit.id.hex, | |
961 | } |
|
960 | } | |
962 | # tree objects itself don't have tree_id attribute |
|
961 | # tree objects itself don't have tree_id attribute | |
963 | if hasattr(commit, 'tree_id'): |
|
962 | if hasattr(commit, 'tree_id'): | |
964 | obj_data['tree'] = commit.tree_id.hex |
|
963 | obj_data['tree'] = commit.tree_id.hex | |
965 |
|
964 | |||
966 | return obj_data |
|
965 | return obj_data | |
967 | return _revision(context_uid, repo_id, rev) |
|
966 | return _revision(context_uid, repo_id, rev) | |
968 |
|
967 | |||
969 | @reraise_safe_exceptions |
|
968 | @reraise_safe_exceptions | |
970 | def date(self, wire, commit_id): |
|
969 | def date(self, wire, commit_id): | |
971 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
970 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
972 | region = self._region(wire) |
|
971 | region = self._region(wire) | |
973 |
|
972 | |||
974 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
973 | @region.conditional_cache_on_arguments(condition=cache_on) | |
975 | def _date(_repo_id, _commit_id): |
|
974 | def _date(_repo_id, _commit_id): | |
976 | repo_init = self._factory.repo_libgit2(wire) |
|
975 | repo_init = self._factory.repo_libgit2(wire) | |
977 | with repo_init as repo: |
|
976 | with repo_init as repo: | |
978 | commit = repo[commit_id] |
|
977 | commit = repo[commit_id] | |
979 |
|
978 | |||
980 | if hasattr(commit, 'commit_time'): |
|
979 | if hasattr(commit, 'commit_time'): | |
981 | commit_time, commit_time_offset = commit.commit_time, commit.commit_time_offset |
|
980 | commit_time, commit_time_offset = commit.commit_time, commit.commit_time_offset | |
982 | else: |
|
981 | else: | |
983 | commit = commit.get_object() |
|
982 | commit = commit.get_object() | |
984 | commit_time, commit_time_offset = commit.commit_time, commit.commit_time_offset |
|
983 | commit_time, commit_time_offset = commit.commit_time, commit.commit_time_offset | |
985 |
|
984 | |||
986 | # TODO(marcink): check dulwich difference of offset vs timezone |
|
985 | # TODO(marcink): check dulwich difference of offset vs timezone | |
987 | return [commit_time, commit_time_offset] |
|
986 | return [commit_time, commit_time_offset] | |
988 | return _date(repo_id, commit_id) |
|
987 | return _date(repo_id, commit_id) | |
989 |
|
988 | |||
990 | @reraise_safe_exceptions |
|
989 | @reraise_safe_exceptions | |
991 | def author(self, wire, commit_id): |
|
990 | def author(self, wire, commit_id): | |
992 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
991 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
993 | region = self._region(wire) |
|
992 | region = self._region(wire) | |
994 |
|
993 | |||
995 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
994 | @region.conditional_cache_on_arguments(condition=cache_on) | |
996 | def _author(_repo_id, _commit_id): |
|
995 | def _author(_repo_id, _commit_id): | |
997 | repo_init = self._factory.repo_libgit2(wire) |
|
996 | repo_init = self._factory.repo_libgit2(wire) | |
998 | with repo_init as repo: |
|
997 | with repo_init as repo: | |
999 | commit = repo[commit_id] |
|
998 | commit = repo[commit_id] | |
1000 |
|
999 | |||
1001 | if hasattr(commit, 'author'): |
|
1000 | if hasattr(commit, 'author'): | |
1002 | author = commit.author |
|
1001 | author = commit.author | |
1003 | else: |
|
1002 | else: | |
1004 | author = commit.get_object().author |
|
1003 | author = commit.get_object().author | |
1005 |
|
1004 | |||
1006 | if author.email: |
|
1005 | if author.email: | |
1007 | return f"{author.name} <{author.email}>" |
|
1006 | return f"{author.name} <{author.email}>" | |
1008 |
|
1007 | |||
1009 | try: |
|
1008 | try: | |
1010 | return f"{author.name}" |
|
1009 | return f"{author.name}" | |
1011 | except Exception: |
|
1010 | except Exception: | |
1012 | return f"{safe_str(author.raw_name)}" |
|
1011 | return f"{safe_str(author.raw_name)}" | |
1013 |
|
1012 | |||
1014 | return _author(repo_id, commit_id) |
|
1013 | return _author(repo_id, commit_id) | |
1015 |
|
1014 | |||
1016 | @reraise_safe_exceptions |
|
1015 | @reraise_safe_exceptions | |
1017 | def message(self, wire, commit_id): |
|
1016 | def message(self, wire, commit_id): | |
1018 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
1017 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
1019 | region = self._region(wire) |
|
1018 | region = self._region(wire) | |
1020 |
|
1019 | |||
1021 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
1020 | @region.conditional_cache_on_arguments(condition=cache_on) | |
1022 | def _message(_repo_id, _commit_id): |
|
1021 | def _message(_repo_id, _commit_id): | |
1023 | repo_init = self._factory.repo_libgit2(wire) |
|
1022 | repo_init = self._factory.repo_libgit2(wire) | |
1024 | with repo_init as repo: |
|
1023 | with repo_init as repo: | |
1025 | commit = repo[commit_id] |
|
1024 | commit = repo[commit_id] | |
1026 | return commit.message |
|
1025 | return commit.message | |
1027 | return _message(repo_id, commit_id) |
|
1026 | return _message(repo_id, commit_id) | |
1028 |
|
1027 | |||
1029 | @reraise_safe_exceptions |
|
1028 | @reraise_safe_exceptions | |
1030 | def parents(self, wire, commit_id): |
|
1029 | def parents(self, wire, commit_id): | |
1031 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
1030 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
1032 | region = self._region(wire) |
|
1031 | region = self._region(wire) | |
1033 |
|
1032 | |||
1034 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
1033 | @region.conditional_cache_on_arguments(condition=cache_on) | |
1035 | def _parents(_repo_id, _commit_id): |
|
1034 | def _parents(_repo_id, _commit_id): | |
1036 | repo_init = self._factory.repo_libgit2(wire) |
|
1035 | repo_init = self._factory.repo_libgit2(wire) | |
1037 | with repo_init as repo: |
|
1036 | with repo_init as repo: | |
1038 | commit = repo[commit_id] |
|
1037 | commit = repo[commit_id] | |
1039 | if hasattr(commit, 'parent_ids'): |
|
1038 | if hasattr(commit, 'parent_ids'): | |
1040 | parent_ids = commit.parent_ids |
|
1039 | parent_ids = commit.parent_ids | |
1041 | else: |
|
1040 | else: | |
1042 | parent_ids = commit.get_object().parent_ids |
|
1041 | parent_ids = commit.get_object().parent_ids | |
1043 |
|
1042 | |||
1044 | return [x.hex for x in parent_ids] |
|
1043 | return [x.hex for x in parent_ids] | |
1045 | return _parents(repo_id, commit_id) |
|
1044 | return _parents(repo_id, commit_id) | |
1046 |
|
1045 | |||
1047 | @reraise_safe_exceptions |
|
1046 | @reraise_safe_exceptions | |
1048 | def children(self, wire, commit_id): |
|
1047 | def children(self, wire, commit_id): | |
1049 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
1048 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
1050 | region = self._region(wire) |
|
1049 | region = self._region(wire) | |
1051 |
|
1050 | |||
1052 | head = self.head(wire) |
|
1051 | head = self.head(wire) | |
1053 |
|
1052 | |||
1054 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
1053 | @region.conditional_cache_on_arguments(condition=cache_on) | |
1055 | def _children(_repo_id, _commit_id): |
|
1054 | def _children(_repo_id, _commit_id): | |
1056 |
|
1055 | |||
1057 | output, __ = self.run_git_command( |
|
1056 | output, __ = self.run_git_command( | |
1058 | wire, ['rev-list', '--all', '--children', f'{commit_id}^..{head}']) |
|
1057 | wire, ['rev-list', '--all', '--children', f'{commit_id}^..{head}']) | |
1059 |
|
1058 | |||
1060 | child_ids = [] |
|
1059 | child_ids = [] | |
1061 | pat = re.compile(fr'^{commit_id}') |
|
1060 | pat = re.compile(fr'^{commit_id}') | |
1062 | for line in output.splitlines(): |
|
1061 | for line in output.splitlines(): | |
1063 | line = safe_str(line) |
|
1062 | line = safe_str(line) | |
1064 | if pat.match(line): |
|
1063 | if pat.match(line): | |
1065 | found_ids = line.split(' ')[1:] |
|
1064 | found_ids = line.split(' ')[1:] | |
1066 | child_ids.extend(found_ids) |
|
1065 | child_ids.extend(found_ids) | |
1067 | break |
|
1066 | break | |
1068 |
|
1067 | |||
1069 | return child_ids |
|
1068 | return child_ids | |
1070 | return _children(repo_id, commit_id) |
|
1069 | return _children(repo_id, commit_id) | |
1071 |
|
1070 | |||
1072 | @reraise_safe_exceptions |
|
1071 | @reraise_safe_exceptions | |
1073 | def set_refs(self, wire, key, value): |
|
1072 | def set_refs(self, wire, key, value): | |
1074 | repo_init = self._factory.repo_libgit2(wire) |
|
1073 | repo_init = self._factory.repo_libgit2(wire) | |
1075 | with repo_init as repo: |
|
1074 | with repo_init as repo: | |
1076 | repo.references.create(key, value, force=True) |
|
1075 | repo.references.create(key, value, force=True) | |
1077 |
|
1076 | |||
1078 | @reraise_safe_exceptions |
|
1077 | @reraise_safe_exceptions | |
1079 | def create_branch(self, wire, branch_name, commit_id, force=False): |
|
1078 | def create_branch(self, wire, branch_name, commit_id, force=False): | |
1080 | repo_init = self._factory.repo_libgit2(wire) |
|
1079 | repo_init = self._factory.repo_libgit2(wire) | |
1081 | with repo_init as repo: |
|
1080 | with repo_init as repo: | |
1082 | if commit_id: |
|
1081 | if commit_id: | |
1083 | commit = repo[commit_id] |
|
1082 | commit = repo[commit_id] | |
1084 | else: |
|
1083 | else: | |
1085 | # if commit is not given just use the HEAD |
|
1084 | # if commit is not given just use the HEAD | |
1086 | commit = repo.head() |
|
1085 | commit = repo.head() | |
1087 |
|
1086 | |||
1088 | if force: |
|
1087 | if force: | |
1089 | repo.branches.local.create(branch_name, commit, force=force) |
|
1088 | repo.branches.local.create(branch_name, commit, force=force) | |
1090 | elif not repo.branches.get(branch_name): |
|
1089 | elif not repo.branches.get(branch_name): | |
1091 | # create only if that branch isn't existing |
|
1090 | # create only if that branch isn't existing | |
1092 | repo.branches.local.create(branch_name, commit, force=force) |
|
1091 | repo.branches.local.create(branch_name, commit, force=force) | |
1093 |
|
1092 | |||
1094 | @reraise_safe_exceptions |
|
1093 | @reraise_safe_exceptions | |
1095 | def remove_ref(self, wire, key): |
|
1094 | def remove_ref(self, wire, key): | |
1096 | repo_init = self._factory.repo_libgit2(wire) |
|
1095 | repo_init = self._factory.repo_libgit2(wire) | |
1097 | with repo_init as repo: |
|
1096 | with repo_init as repo: | |
1098 | repo.references.delete(key) |
|
1097 | repo.references.delete(key) | |
1099 |
|
1098 | |||
1100 | @reraise_safe_exceptions |
|
1099 | @reraise_safe_exceptions | |
1101 | def tag_remove(self, wire, tag_name): |
|
1100 | def tag_remove(self, wire, tag_name): | |
1102 | repo_init = self._factory.repo_libgit2(wire) |
|
1101 | repo_init = self._factory.repo_libgit2(wire) | |
1103 | with repo_init as repo: |
|
1102 | with repo_init as repo: | |
1104 | key = f'refs/tags/{tag_name}' |
|
1103 | key = f'refs/tags/{tag_name}' | |
1105 | repo.references.delete(key) |
|
1104 | repo.references.delete(key) | |
1106 |
|
1105 | |||
1107 | @reraise_safe_exceptions |
|
1106 | @reraise_safe_exceptions | |
1108 | def tree_changes(self, wire, source_id, target_id): |
|
1107 | def tree_changes(self, wire, source_id, target_id): | |
1109 | repo = self._factory.repo(wire) |
|
1108 | repo = self._factory.repo(wire) | |
1110 | # source can be empty |
|
1109 | # source can be empty | |
1111 | source_id = safe_bytes(source_id if source_id else b'') |
|
1110 | source_id = safe_bytes(source_id if source_id else b'') | |
1112 | target_id = safe_bytes(target_id) |
|
1111 | target_id = safe_bytes(target_id) | |
1113 |
|
1112 | |||
1114 | source = repo[source_id].tree if source_id else None |
|
1113 | source = repo[source_id].tree if source_id else None | |
1115 | target = repo[target_id].tree |
|
1114 | target = repo[target_id].tree | |
1116 | result = repo.object_store.tree_changes(source, target) |
|
1115 | result = repo.object_store.tree_changes(source, target) | |
1117 |
|
1116 | |||
1118 | added = set() |
|
1117 | added = set() | |
1119 | modified = set() |
|
1118 | modified = set() | |
1120 | deleted = set() |
|
1119 | deleted = set() | |
1121 | for (old_path, new_path), (_, _), (_, _) in list(result): |
|
1120 | for (old_path, new_path), (_, _), (_, _) in list(result): | |
1122 | if new_path and old_path: |
|
1121 | if new_path and old_path: | |
1123 | modified.add(new_path) |
|
1122 | modified.add(new_path) | |
1124 | elif new_path and not old_path: |
|
1123 | elif new_path and not old_path: | |
1125 | added.add(new_path) |
|
1124 | added.add(new_path) | |
1126 | elif not new_path and old_path: |
|
1125 | elif not new_path and old_path: | |
1127 | deleted.add(old_path) |
|
1126 | deleted.add(old_path) | |
1128 |
|
1127 | |||
1129 | return list(added), list(modified), list(deleted) |
|
1128 | return list(added), list(modified), list(deleted) | |
1130 |
|
1129 | |||
1131 | @reraise_safe_exceptions |
|
1130 | @reraise_safe_exceptions | |
1132 | def tree_and_type_for_path(self, wire, commit_id, path): |
|
1131 | def tree_and_type_for_path(self, wire, commit_id, path): | |
1133 |
|
1132 | |||
1134 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
1133 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
1135 | region = self._region(wire) |
|
1134 | region = self._region(wire) | |
1136 |
|
1135 | |||
1137 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
1136 | @region.conditional_cache_on_arguments(condition=cache_on) | |
1138 | def _tree_and_type_for_path(_context_uid, _repo_id, _commit_id, _path): |
|
1137 | def _tree_and_type_for_path(_context_uid, _repo_id, _commit_id, _path): | |
1139 | repo_init = self._factory.repo_libgit2(wire) |
|
1138 | repo_init = self._factory.repo_libgit2(wire) | |
1140 |
|
1139 | |||
1141 | with repo_init as repo: |
|
1140 | with repo_init as repo: | |
1142 | commit = repo[commit_id] |
|
1141 | commit = repo[commit_id] | |
1143 | try: |
|
1142 | try: | |
1144 | tree = commit.tree[path] |
|
1143 | tree = commit.tree[path] | |
1145 | except KeyError: |
|
1144 | except KeyError: | |
1146 | return None, None, None |
|
1145 | return None, None, None | |
1147 |
|
1146 | |||
1148 | return tree.id.hex, tree.type_str, tree.filemode |
|
1147 | return tree.id.hex, tree.type_str, tree.filemode | |
1149 | return _tree_and_type_for_path(context_uid, repo_id, commit_id, path) |
|
1148 | return _tree_and_type_for_path(context_uid, repo_id, commit_id, path) | |
1150 |
|
1149 | |||
1151 | @reraise_safe_exceptions |
|
1150 | @reraise_safe_exceptions | |
1152 | def tree_items(self, wire, tree_id): |
|
1151 | def tree_items(self, wire, tree_id): | |
1153 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
1152 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
1154 | region = self._region(wire) |
|
1153 | region = self._region(wire) | |
1155 |
|
1154 | |||
1156 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
1155 | @region.conditional_cache_on_arguments(condition=cache_on) | |
1157 | def _tree_items(_repo_id, _tree_id): |
|
1156 | def _tree_items(_repo_id, _tree_id): | |
1158 |
|
1157 | |||
1159 | repo_init = self._factory.repo_libgit2(wire) |
|
1158 | repo_init = self._factory.repo_libgit2(wire) | |
1160 | with repo_init as repo: |
|
1159 | with repo_init as repo: | |
1161 | try: |
|
1160 | try: | |
1162 | tree = repo[tree_id] |
|
1161 | tree = repo[tree_id] | |
1163 | except KeyError: |
|
1162 | except KeyError: | |
1164 | raise ObjectMissing(f'No tree with id: {tree_id}') |
|
1163 | raise ObjectMissing(f'No tree with id: {tree_id}') | |
1165 |
|
1164 | |||
1166 | result = [] |
|
1165 | result = [] | |
1167 | for item in tree: |
|
1166 | for item in tree: | |
1168 | item_sha = item.hex |
|
1167 | item_sha = item.hex | |
1169 | item_mode = item.filemode |
|
1168 | item_mode = item.filemode | |
1170 | item_type = item.type_str |
|
1169 | item_type = item.type_str | |
1171 |
|
1170 | |||
1172 | if item_type == 'commit': |
|
1171 | if item_type == 'commit': | |
1173 | # NOTE(marcink): submodules we translate to 'link' for backward compat |
|
1172 | # NOTE(marcink): submodules we translate to 'link' for backward compat | |
1174 | item_type = 'link' |
|
1173 | item_type = 'link' | |
1175 |
|
1174 | |||
1176 | result.append((item.name, item_mode, item_sha, item_type)) |
|
1175 | result.append((item.name, item_mode, item_sha, item_type)) | |
1177 | return result |
|
1176 | return result | |
1178 | return _tree_items(repo_id, tree_id) |
|
1177 | return _tree_items(repo_id, tree_id) | |
1179 |
|
1178 | |||
1180 | @reraise_safe_exceptions |
|
1179 | @reraise_safe_exceptions | |
1181 | def diff_2(self, wire, commit_id_1, commit_id_2, file_filter, opt_ignorews, context): |
|
1180 | def diff_2(self, wire, commit_id_1, commit_id_2, file_filter, opt_ignorews, context): | |
1182 | """ |
|
1181 | """ | |
1183 | Old version that uses subprocess to call diff |
|
1182 | Old version that uses subprocess to call diff | |
1184 | """ |
|
1183 | """ | |
1185 |
|
1184 | |||
1186 | flags = [ |
|
1185 | flags = [ | |
1187 |
'-U |
|
1186 | f'-U{context}', '--patch', | |
1188 | '--binary', |
|
1187 | '--binary', | |
1189 | '--find-renames', |
|
1188 | '--find-renames', | |
1190 | '--no-indent-heuristic', |
|
1189 | '--no-indent-heuristic', | |
1191 | # '--indent-heuristic', |
|
1190 | # '--indent-heuristic', | |
1192 | #'--full-index', |
|
1191 | #'--full-index', | |
1193 | #'--abbrev=40' |
|
1192 | #'--abbrev=40' | |
1194 | ] |
|
1193 | ] | |
1195 |
|
1194 | |||
1196 | if opt_ignorews: |
|
1195 | if opt_ignorews: | |
1197 | flags.append('--ignore-all-space') |
|
1196 | flags.append('--ignore-all-space') | |
1198 |
|
1197 | |||
1199 | if commit_id_1 == self.EMPTY_COMMIT: |
|
1198 | if commit_id_1 == self.EMPTY_COMMIT: | |
1200 | cmd = ['show'] + flags + [commit_id_2] |
|
1199 | cmd = ['show'] + flags + [commit_id_2] | |
1201 | else: |
|
1200 | else: | |
1202 | cmd = ['diff'] + flags + [commit_id_1, commit_id_2] |
|
1201 | cmd = ['diff'] + flags + [commit_id_1, commit_id_2] | |
1203 |
|
1202 | |||
1204 | if file_filter: |
|
1203 | if file_filter: | |
1205 | cmd.extend(['--', file_filter]) |
|
1204 | cmd.extend(['--', file_filter]) | |
1206 |
|
1205 | |||
1207 | diff, __ = self.run_git_command(wire, cmd) |
|
1206 | diff, __ = self.run_git_command(wire, cmd) | |
1208 | # If we used 'show' command, strip first few lines (until actual diff |
|
1207 | # If we used 'show' command, strip first few lines (until actual diff | |
1209 | # starts) |
|
1208 | # starts) | |
1210 | if commit_id_1 == self.EMPTY_COMMIT: |
|
1209 | if commit_id_1 == self.EMPTY_COMMIT: | |
1211 | lines = diff.splitlines() |
|
1210 | lines = diff.splitlines() | |
1212 | x = 0 |
|
1211 | x = 0 | |
1213 | for line in lines: |
|
1212 | for line in lines: | |
1214 | if line.startswith(b'diff'): |
|
1213 | if line.startswith(b'diff'): | |
1215 | break |
|
1214 | break | |
1216 | x += 1 |
|
1215 | x += 1 | |
1217 | # Append new line just like 'diff' command do |
|
1216 | # Append new line just like 'diff' command do | |
1218 | diff = '\n'.join(lines[x:]) + '\n' |
|
1217 | diff = '\n'.join(lines[x:]) + '\n' | |
1219 | return diff |
|
1218 | return diff | |
1220 |
|
1219 | |||
1221 | @reraise_safe_exceptions |
|
1220 | @reraise_safe_exceptions | |
1222 | def diff(self, wire, commit_id_1, commit_id_2, file_filter, opt_ignorews, context): |
|
1221 | def diff(self, wire, commit_id_1, commit_id_2, file_filter, opt_ignorews, context): | |
1223 | repo_init = self._factory.repo_libgit2(wire) |
|
1222 | repo_init = self._factory.repo_libgit2(wire) | |
1224 |
|
1223 | |||
1225 | with repo_init as repo: |
|
1224 | with repo_init as repo: | |
1226 | swap = True |
|
1225 | swap = True | |
1227 | flags = 0 |
|
1226 | flags = 0 | |
1228 | flags |= pygit2.GIT_DIFF_SHOW_BINARY |
|
1227 | flags |= pygit2.GIT_DIFF_SHOW_BINARY | |
1229 |
|
1228 | |||
1230 | if opt_ignorews: |
|
1229 | if opt_ignorews: | |
1231 | flags |= pygit2.GIT_DIFF_IGNORE_WHITESPACE |
|
1230 | flags |= pygit2.GIT_DIFF_IGNORE_WHITESPACE | |
1232 |
|
1231 | |||
1233 | if commit_id_1 == self.EMPTY_COMMIT: |
|
1232 | if commit_id_1 == self.EMPTY_COMMIT: | |
1234 | comm1 = repo[commit_id_2] |
|
1233 | comm1 = repo[commit_id_2] | |
1235 | diff_obj = comm1.tree.diff_to_tree( |
|
1234 | diff_obj = comm1.tree.diff_to_tree( | |
1236 | flags=flags, context_lines=context, swap=swap) |
|
1235 | flags=flags, context_lines=context, swap=swap) | |
1237 |
|
1236 | |||
1238 | else: |
|
1237 | else: | |
1239 | comm1 = repo[commit_id_2] |
|
1238 | comm1 = repo[commit_id_2] | |
1240 | comm2 = repo[commit_id_1] |
|
1239 | comm2 = repo[commit_id_1] | |
1241 | diff_obj = comm1.tree.diff_to_tree( |
|
1240 | diff_obj = comm1.tree.diff_to_tree( | |
1242 | comm2.tree, flags=flags, context_lines=context, swap=swap) |
|
1241 | comm2.tree, flags=flags, context_lines=context, swap=swap) | |
1243 | similar_flags = 0 |
|
1242 | similar_flags = 0 | |
1244 | similar_flags |= pygit2.GIT_DIFF_FIND_RENAMES |
|
1243 | similar_flags |= pygit2.GIT_DIFF_FIND_RENAMES | |
1245 | diff_obj.find_similar(flags=similar_flags) |
|
1244 | diff_obj.find_similar(flags=similar_flags) | |
1246 |
|
1245 | |||
1247 | if file_filter: |
|
1246 | if file_filter: | |
1248 | for p in diff_obj: |
|
1247 | for p in diff_obj: | |
1249 | if p.delta.old_file.path == file_filter: |
|
1248 | if p.delta.old_file.path == file_filter: | |
1250 | return BytesEnvelope(p.data) or BytesEnvelope(b'') |
|
1249 | return BytesEnvelope(p.data) or BytesEnvelope(b'') | |
1251 | # fo matching path == no diff |
|
1250 | # fo matching path == no diff | |
1252 | return BytesEnvelope(b'') |
|
1251 | return BytesEnvelope(b'') | |
1253 |
|
1252 | |||
1254 | return BytesEnvelope(safe_bytes(diff_obj.patch)) or BytesEnvelope(b'') |
|
1253 | return BytesEnvelope(safe_bytes(diff_obj.patch)) or BytesEnvelope(b'') | |
1255 |
|
1254 | |||
1256 | @reraise_safe_exceptions |
|
1255 | @reraise_safe_exceptions | |
1257 | def node_history(self, wire, commit_id, path, limit): |
|
1256 | def node_history(self, wire, commit_id, path, limit): | |
1258 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
1257 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
1259 | region = self._region(wire) |
|
1258 | region = self._region(wire) | |
1260 |
|
1259 | |||
1261 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
1260 | @region.conditional_cache_on_arguments(condition=cache_on) | |
1262 | def _node_history(_context_uid, _repo_id, _commit_id, _path, _limit): |
|
1261 | def _node_history(_context_uid, _repo_id, _commit_id, _path, _limit): | |
1263 | # optimize for n==1, rev-list is much faster for that use-case |
|
1262 | # optimize for n==1, rev-list is much faster for that use-case | |
1264 | if limit == 1: |
|
1263 | if limit == 1: | |
1265 | cmd = ['rev-list', '-1', commit_id, '--', path] |
|
1264 | cmd = ['rev-list', '-1', commit_id, '--', path] | |
1266 | else: |
|
1265 | else: | |
1267 | cmd = ['log'] |
|
1266 | cmd = ['log'] | |
1268 | if limit: |
|
1267 | if limit: | |
1269 | cmd.extend(['-n', str(safe_int(limit, 0))]) |
|
1268 | cmd.extend(['-n', str(safe_int(limit, 0))]) | |
1270 | cmd.extend(['--pretty=format: %H', '-s', commit_id, '--', path]) |
|
1269 | cmd.extend(['--pretty=format: %H', '-s', commit_id, '--', path]) | |
1271 |
|
1270 | |||
1272 | output, __ = self.run_git_command(wire, cmd) |
|
1271 | output, __ = self.run_git_command(wire, cmd) | |
1273 | commit_ids = re.findall(rb'[0-9a-fA-F]{40}', output) |
|
1272 | commit_ids = re.findall(rb'[0-9a-fA-F]{40}', output) | |
1274 |
|
1273 | |||
1275 | return [x for x in commit_ids] |
|
1274 | return [x for x in commit_ids] | |
1276 | return _node_history(context_uid, repo_id, commit_id, path, limit) |
|
1275 | return _node_history(context_uid, repo_id, commit_id, path, limit) | |
1277 |
|
1276 | |||
1278 | @reraise_safe_exceptions |
|
1277 | @reraise_safe_exceptions | |
1279 | def node_annotate_legacy(self, wire, commit_id, path): |
|
1278 | def node_annotate_legacy(self, wire, commit_id, path): | |
1280 | # note: replaced by pygit2 implementation |
|
1279 | # note: replaced by pygit2 implementation | |
1281 | cmd = ['blame', '-l', '--root', '-r', commit_id, '--', path] |
|
1280 | cmd = ['blame', '-l', '--root', '-r', commit_id, '--', path] | |
1282 | # -l ==> outputs long shas (and we need all 40 characters) |
|
1281 | # -l ==> outputs long shas (and we need all 40 characters) | |
1283 | # --root ==> doesn't put '^' character for boundaries |
|
1282 | # --root ==> doesn't put '^' character for boundaries | |
1284 | # -r commit_id ==> blames for the given commit |
|
1283 | # -r commit_id ==> blames for the given commit | |
1285 | output, __ = self.run_git_command(wire, cmd) |
|
1284 | output, __ = self.run_git_command(wire, cmd) | |
1286 |
|
1285 | |||
1287 | result = [] |
|
1286 | result = [] | |
1288 | for i, blame_line in enumerate(output.splitlines()[:-1]): |
|
1287 | for i, blame_line in enumerate(output.splitlines()[:-1]): | |
1289 | line_no = i + 1 |
|
1288 | line_no = i + 1 | |
1290 | blame_commit_id, line = re.split(rb' ', blame_line, 1) |
|
1289 | blame_commit_id, line = re.split(rb' ', blame_line, 1) | |
1291 | result.append((line_no, blame_commit_id, line)) |
|
1290 | result.append((line_no, blame_commit_id, line)) | |
1292 |
|
1291 | |||
1293 | return result |
|
1292 | return result | |
1294 |
|
1293 | |||
1295 | @reraise_safe_exceptions |
|
1294 | @reraise_safe_exceptions | |
1296 | def node_annotate(self, wire, commit_id, path): |
|
1295 | def node_annotate(self, wire, commit_id, path): | |
1297 |
|
1296 | |||
1298 | result_libgit = [] |
|
1297 | result_libgit = [] | |
1299 | repo_init = self._factory.repo_libgit2(wire) |
|
1298 | repo_init = self._factory.repo_libgit2(wire) | |
1300 | with repo_init as repo: |
|
1299 | with repo_init as repo: | |
1301 | commit = repo[commit_id] |
|
1300 | commit = repo[commit_id] | |
1302 | blame_obj = repo.blame(path, newest_commit=commit_id) |
|
1301 | blame_obj = repo.blame(path, newest_commit=commit_id) | |
1303 | for i, line in enumerate(commit.tree[path].data.splitlines()): |
|
1302 | for i, line in enumerate(commit.tree[path].data.splitlines()): | |
1304 | line_no = i + 1 |
|
1303 | line_no = i + 1 | |
1305 | hunk = blame_obj.for_line(line_no) |
|
1304 | hunk = blame_obj.for_line(line_no) | |
1306 | blame_commit_id = hunk.final_commit_id.hex |
|
1305 | blame_commit_id = hunk.final_commit_id.hex | |
1307 |
|
1306 | |||
1308 | result_libgit.append((line_no, blame_commit_id, line)) |
|
1307 | result_libgit.append((line_no, blame_commit_id, line)) | |
1309 |
|
1308 | |||
1310 | return BinaryEnvelope(result_libgit) |
|
1309 | return BinaryEnvelope(result_libgit) | |
1311 |
|
1310 | |||
1312 | @reraise_safe_exceptions |
|
1311 | @reraise_safe_exceptions | |
1313 | def update_server_info(self, wire): |
|
1312 | def update_server_info(self, wire): | |
1314 | repo = self._factory.repo(wire) |
|
1313 | repo = self._factory.repo(wire) | |
1315 | update_server_info(repo) |
|
1314 | update_server_info(repo) | |
1316 |
|
1315 | |||
1317 | @reraise_safe_exceptions |
|
1316 | @reraise_safe_exceptions | |
1318 | def get_all_commit_ids(self, wire): |
|
1317 | def get_all_commit_ids(self, wire): | |
1319 |
|
1318 | |||
1320 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
1319 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
1321 | region = self._region(wire) |
|
1320 | region = self._region(wire) | |
1322 |
|
1321 | |||
1323 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
1322 | @region.conditional_cache_on_arguments(condition=cache_on) | |
1324 | def _get_all_commit_ids(_context_uid, _repo_id): |
|
1323 | def _get_all_commit_ids(_context_uid, _repo_id): | |
1325 |
|
1324 | |||
1326 | cmd = ['rev-list', '--reverse', '--date-order', '--branches', '--tags'] |
|
1325 | cmd = ['rev-list', '--reverse', '--date-order', '--branches', '--tags'] | |
1327 | try: |
|
1326 | try: | |
1328 | output, __ = self.run_git_command(wire, cmd) |
|
1327 | output, __ = self.run_git_command(wire, cmd) | |
1329 | return output.splitlines() |
|
1328 | return output.splitlines() | |
1330 | except Exception: |
|
1329 | except Exception: | |
1331 | # Can be raised for empty repositories |
|
1330 | # Can be raised for empty repositories | |
1332 | return [] |
|
1331 | return [] | |
1333 |
|
1332 | |||
1334 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
1333 | @region.conditional_cache_on_arguments(condition=cache_on) | |
1335 | def _get_all_commit_ids_pygit2(_context_uid, _repo_id): |
|
1334 | def _get_all_commit_ids_pygit2(_context_uid, _repo_id): | |
1336 | repo_init = self._factory.repo_libgit2(wire) |
|
1335 | repo_init = self._factory.repo_libgit2(wire) | |
1337 | from pygit2 import GIT_SORT_REVERSE, GIT_SORT_TIME, GIT_BRANCH_ALL |
|
1336 | from pygit2 import GIT_SORT_REVERSE, GIT_SORT_TIME, GIT_BRANCH_ALL | |
1338 | results = [] |
|
1337 | results = [] | |
1339 | with repo_init as repo: |
|
1338 | with repo_init as repo: | |
1340 | for commit in repo.walk(repo.head.target, GIT_SORT_TIME | GIT_BRANCH_ALL | GIT_SORT_REVERSE): |
|
1339 | for commit in repo.walk(repo.head.target, GIT_SORT_TIME | GIT_BRANCH_ALL | GIT_SORT_REVERSE): | |
1341 | results.append(commit.id.hex) |
|
1340 | results.append(commit.id.hex) | |
1342 |
|
1341 | |||
1343 | return _get_all_commit_ids(context_uid, repo_id) |
|
1342 | return _get_all_commit_ids(context_uid, repo_id) | |
1344 |
|
1343 | |||
1345 | @reraise_safe_exceptions |
|
1344 | @reraise_safe_exceptions | |
1346 | def run_git_command(self, wire, cmd, **opts): |
|
1345 | def run_git_command(self, wire, cmd, **opts): | |
1347 | path = wire.get('path', None) |
|
1346 | path = wire.get('path', None) | |
1348 |
|
1347 | |||
1349 | if path and os.path.isdir(path): |
|
1348 | if path and os.path.isdir(path): | |
1350 | opts['cwd'] = path |
|
1349 | opts['cwd'] = path | |
1351 |
|
1350 | |||
1352 | if '_bare' in opts: |
|
1351 | if '_bare' in opts: | |
1353 | _copts = [] |
|
1352 | _copts = [] | |
1354 | del opts['_bare'] |
|
1353 | del opts['_bare'] | |
1355 | else: |
|
1354 | else: | |
1356 | _copts = ['-c', 'core.quotepath=false',] |
|
1355 | _copts = ['-c', 'core.quotepath=false',] | |
1357 | safe_call = False |
|
1356 | safe_call = False | |
1358 | if '_safe' in opts: |
|
1357 | if '_safe' in opts: | |
1359 | # no exc on failure |
|
1358 | # no exc on failure | |
1360 | del opts['_safe'] |
|
1359 | del opts['_safe'] | |
1361 | safe_call = True |
|
1360 | safe_call = True | |
1362 |
|
1361 | |||
1363 | if '_copts' in opts: |
|
1362 | if '_copts' in opts: | |
1364 | _copts.extend(opts['_copts'] or []) |
|
1363 | _copts.extend(opts['_copts'] or []) | |
1365 | del opts['_copts'] |
|
1364 | del opts['_copts'] | |
1366 |
|
1365 | |||
1367 | gitenv = os.environ.copy() |
|
1366 | gitenv = os.environ.copy() | |
1368 | gitenv.update(opts.pop('extra_env', {})) |
|
1367 | gitenv.update(opts.pop('extra_env', {})) | |
1369 | # need to clean fix GIT_DIR ! |
|
1368 | # need to clean fix GIT_DIR ! | |
1370 | if 'GIT_DIR' in gitenv: |
|
1369 | if 'GIT_DIR' in gitenv: | |
1371 | del gitenv['GIT_DIR'] |
|
1370 | del gitenv['GIT_DIR'] | |
1372 | gitenv['GIT_CONFIG_NOGLOBAL'] = '1' |
|
1371 | gitenv['GIT_CONFIG_NOGLOBAL'] = '1' | |
1373 | gitenv['GIT_DISCOVERY_ACROSS_FILESYSTEM'] = '1' |
|
1372 | gitenv['GIT_DISCOVERY_ACROSS_FILESYSTEM'] = '1' | |
1374 |
|
1373 | |||
1375 | cmd = [settings.GIT_EXECUTABLE] + _copts + cmd |
|
1374 | cmd = [settings.GIT_EXECUTABLE] + _copts + cmd | |
1376 | _opts = {'env': gitenv, 'shell': False} |
|
1375 | _opts = {'env': gitenv, 'shell': False} | |
1377 |
|
1376 | |||
1378 | proc = None |
|
1377 | proc = None | |
1379 | try: |
|
1378 | try: | |
1380 | _opts.update(opts) |
|
1379 | _opts.update(opts) | |
1381 | proc = subprocessio.SubprocessIOChunker(cmd, **_opts) |
|
1380 | proc = subprocessio.SubprocessIOChunker(cmd, **_opts) | |
1382 |
|
1381 | |||
1383 | return b''.join(proc), b''.join(proc.stderr) |
|
1382 | return b''.join(proc), b''.join(proc.stderr) | |
1384 | except OSError as err: |
|
1383 | except OSError as err: | |
1385 | cmd = ' '.join(map(safe_str, cmd)) # human friendly CMD |
|
1384 | cmd = ' '.join(map(safe_str, cmd)) # human friendly CMD | |
1386 | tb_err = ("Couldn't run git command (%s).\n" |
|
1385 | tb_err = ("Couldn't run git command (%s).\n" | |
1387 | "Original error was:%s\n" |
|
1386 | "Original error was:%s\n" | |
1388 | "Call options:%s\n" |
|
1387 | "Call options:%s\n" | |
1389 | % (cmd, err, _opts)) |
|
1388 | % (cmd, err, _opts)) | |
1390 | log.exception(tb_err) |
|
1389 | log.exception(tb_err) | |
1391 | if safe_call: |
|
1390 | if safe_call: | |
1392 | return '', err |
|
1391 | return '', err | |
1393 | else: |
|
1392 | else: | |
1394 | raise exceptions.VcsException()(tb_err) |
|
1393 | raise exceptions.VcsException()(tb_err) | |
1395 | finally: |
|
1394 | finally: | |
1396 | if proc: |
|
1395 | if proc: | |
1397 | proc.close() |
|
1396 | proc.close() | |
1398 |
|
1397 | |||
1399 | @reraise_safe_exceptions |
|
1398 | @reraise_safe_exceptions | |
1400 | def install_hooks(self, wire, force=False): |
|
1399 | def install_hooks(self, wire, force=False): | |
1401 | from vcsserver.hook_utils import install_git_hooks |
|
1400 | from vcsserver.hook_utils import install_git_hooks | |
1402 | bare = self.bare(wire) |
|
1401 | bare = self.bare(wire) | |
1403 | path = wire['path'] |
|
1402 | path = wire['path'] | |
1404 | binary_dir = settings.BINARY_DIR |
|
1403 | binary_dir = settings.BINARY_DIR | |
1405 | if binary_dir: |
|
1404 | if binary_dir: | |
1406 | os.path.join(binary_dir, 'python3') |
|
1405 | os.path.join(binary_dir, 'python3') | |
1407 | return install_git_hooks(path, bare, force_create=force) |
|
1406 | return install_git_hooks(path, bare, force_create=force) | |
1408 |
|
1407 | |||
1409 | @reraise_safe_exceptions |
|
1408 | @reraise_safe_exceptions | |
1410 | def get_hooks_info(self, wire): |
|
1409 | def get_hooks_info(self, wire): | |
1411 | from vcsserver.hook_utils import ( |
|
1410 | from vcsserver.hook_utils import ( | |
1412 | get_git_pre_hook_version, get_git_post_hook_version) |
|
1411 | get_git_pre_hook_version, get_git_post_hook_version) | |
1413 | bare = self.bare(wire) |
|
1412 | bare = self.bare(wire) | |
1414 | path = wire['path'] |
|
1413 | path = wire['path'] | |
1415 | return { |
|
1414 | return { | |
1416 | 'pre_version': get_git_pre_hook_version(path, bare), |
|
1415 | 'pre_version': get_git_pre_hook_version(path, bare), | |
1417 | 'post_version': get_git_post_hook_version(path, bare), |
|
1416 | 'post_version': get_git_post_hook_version(path, bare), | |
1418 | } |
|
1417 | } | |
1419 |
|
1418 | |||
1420 | @reraise_safe_exceptions |
|
1419 | @reraise_safe_exceptions | |
1421 | def set_head_ref(self, wire, head_name): |
|
1420 | def set_head_ref(self, wire, head_name): | |
1422 | log.debug('Setting refs/head to `%s`', head_name) |
|
1421 | log.debug('Setting refs/head to `%s`', head_name) | |
1423 | repo_init = self._factory.repo_libgit2(wire) |
|
1422 | repo_init = self._factory.repo_libgit2(wire) | |
1424 | with repo_init as repo: |
|
1423 | with repo_init as repo: | |
1425 | repo.set_head(f'refs/heads/{head_name}') |
|
1424 | repo.set_head(f'refs/heads/{head_name}') | |
1426 |
|
1425 | |||
1427 | return [head_name] + [f'set HEAD to refs/heads/{head_name}'] |
|
1426 | return [head_name] + [f'set HEAD to refs/heads/{head_name}'] | |
1428 |
|
1427 | |||
1429 | @reraise_safe_exceptions |
|
1428 | @reraise_safe_exceptions | |
1430 | def archive_repo(self, wire, archive_name_key, kind, mtime, archive_at_path, |
|
1429 | def archive_repo(self, wire, archive_name_key, kind, mtime, archive_at_path, | |
1431 | archive_dir_name, commit_id, cache_config): |
|
1430 | archive_dir_name, commit_id, cache_config): | |
1432 |
|
1431 | |||
1433 | def file_walker(_commit_id, path): |
|
1432 | def file_walker(_commit_id, path): | |
1434 | repo_init = self._factory.repo_libgit2(wire) |
|
1433 | repo_init = self._factory.repo_libgit2(wire) | |
1435 |
|
1434 | |||
1436 | with repo_init as repo: |
|
1435 | with repo_init as repo: | |
1437 | commit = repo[commit_id] |
|
1436 | commit = repo[commit_id] | |
1438 |
|
1437 | |||
1439 | if path in ['', '/']: |
|
1438 | if path in ['', '/']: | |
1440 | tree = commit.tree |
|
1439 | tree = commit.tree | |
1441 | else: |
|
1440 | else: | |
1442 | tree = commit.tree[path.rstrip('/')] |
|
1441 | tree = commit.tree[path.rstrip('/')] | |
1443 | tree_id = tree.id.hex |
|
1442 | tree_id = tree.id.hex | |
1444 | try: |
|
1443 | try: | |
1445 | tree = repo[tree_id] |
|
1444 | tree = repo[tree_id] | |
1446 | except KeyError: |
|
1445 | except KeyError: | |
1447 | raise ObjectMissing(f'No tree with id: {tree_id}') |
|
1446 | raise ObjectMissing(f'No tree with id: {tree_id}') | |
1448 |
|
1447 | |||
1449 | index = LibGit2Index.Index() |
|
1448 | index = LibGit2Index.Index() | |
1450 | index.read_tree(tree) |
|
1449 | index.read_tree(tree) | |
1451 | file_iter = index |
|
1450 | file_iter = index | |
1452 |
|
1451 | |||
1453 | for file_node in file_iter: |
|
1452 | for file_node in file_iter: | |
1454 | file_path = file_node.path |
|
1453 | file_path = file_node.path | |
1455 | mode = file_node.mode |
|
1454 | mode = file_node.mode | |
1456 | is_link = stat.S_ISLNK(mode) |
|
1455 | is_link = stat.S_ISLNK(mode) | |
1457 | if mode == pygit2.GIT_FILEMODE_COMMIT: |
|
1456 | if mode == pygit2.GIT_FILEMODE_COMMIT: | |
1458 | log.debug('Skipping path %s as a commit node', file_path) |
|
1457 | log.debug('Skipping path %s as a commit node', file_path) | |
1459 | continue |
|
1458 | continue | |
1460 | yield ArchiveNode(file_path, mode, is_link, repo[file_node.hex].read_raw) |
|
1459 | yield ArchiveNode(file_path, mode, is_link, repo[file_node.hex].read_raw) | |
1461 |
|
1460 | |||
1462 | return store_archive_in_cache( |
|
1461 | return store_archive_in_cache( | |
1463 | file_walker, archive_name_key, kind, mtime, archive_at_path, archive_dir_name, commit_id, cache_config=cache_config) |
|
1462 | file_walker, archive_name_key, kind, mtime, archive_at_path, archive_dir_name, commit_id, cache_config=cache_config) |
@@ -1,1201 +1,1200 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | import binascii |
|
17 | import binascii | |
18 | import io |
|
18 | import io | |
19 | import logging |
|
19 | import logging | |
20 | import stat |
|
20 | import stat | |
21 | import sys |
|
21 | import sys | |
22 | import urllib.request |
|
22 | import urllib.request | |
23 | import urllib.parse |
|
23 | import urllib.parse | |
24 | import hashlib |
|
24 | import hashlib | |
25 |
|
25 | |||
26 | from hgext import largefiles, rebase, purge |
|
26 | from hgext import largefiles, rebase, purge | |
27 |
|
27 | |||
28 | from mercurial import commands |
|
28 | from mercurial import commands | |
29 | from mercurial import unionrepo |
|
29 | from mercurial import unionrepo | |
30 | from mercurial import verify |
|
30 | from mercurial import verify | |
31 | from mercurial import repair |
|
31 | from mercurial import repair | |
32 | from mercurial.error import AmbiguousPrefixLookupError |
|
32 | from mercurial.error import AmbiguousPrefixLookupError | |
33 |
|
33 | |||
34 | import vcsserver |
|
34 | import vcsserver | |
35 | from vcsserver import exceptions |
|
35 | from vcsserver import exceptions | |
36 | from vcsserver.base import ( |
|
36 | from vcsserver.base import ( | |
37 | RepoFactory, |
|
37 | RepoFactory, | |
38 | obfuscate_qs, |
|
38 | obfuscate_qs, | |
39 | raise_from_original, |
|
39 | raise_from_original, | |
40 | store_archive_in_cache, |
|
40 | store_archive_in_cache, | |
41 | ArchiveNode, |
|
41 | ArchiveNode, | |
42 | BytesEnvelope, |
|
42 | BytesEnvelope, | |
43 | BinaryEnvelope, |
|
43 | BinaryEnvelope, | |
44 | ) |
|
44 | ) | |
45 | from vcsserver.hgcompat import ( |
|
45 | from vcsserver.hgcompat import ( | |
46 | archival, |
|
46 | archival, | |
47 | bin, |
|
47 | bin, | |
48 | clone, |
|
48 | clone, | |
49 | config as hgconfig, |
|
49 | config as hgconfig, | |
50 | diffopts, |
|
50 | diffopts, | |
51 | hex, |
|
51 | hex, | |
52 | get_ctx, |
|
52 | get_ctx, | |
53 | hg_url as url_parser, |
|
53 | hg_url as url_parser, | |
54 | httpbasicauthhandler, |
|
54 | httpbasicauthhandler, | |
55 | httpdigestauthhandler, |
|
55 | httpdigestauthhandler, | |
56 | makepeer, |
|
56 | makepeer, | |
57 | instance, |
|
57 | instance, | |
58 | match, |
|
58 | match, | |
59 | memctx, |
|
59 | memctx, | |
60 | exchange, |
|
60 | exchange, | |
61 | memfilectx, |
|
61 | memfilectx, | |
62 | nullrev, |
|
62 | nullrev, | |
63 | hg_merge, |
|
63 | hg_merge, | |
64 | patch, |
|
64 | patch, | |
65 | peer, |
|
65 | peer, | |
66 | revrange, |
|
66 | revrange, | |
67 | ui, |
|
67 | ui, | |
68 | hg_tag, |
|
68 | hg_tag, | |
69 | Abort, |
|
69 | Abort, | |
70 | LookupError, |
|
70 | LookupError, | |
71 | RepoError, |
|
71 | RepoError, | |
72 | RepoLookupError, |
|
72 | RepoLookupError, | |
73 | InterventionRequired, |
|
73 | InterventionRequired, | |
74 | RequirementError, |
|
74 | RequirementError, | |
75 | alwaysmatcher, |
|
75 | alwaysmatcher, | |
76 | patternmatcher, |
|
76 | patternmatcher, | |
77 | hgutil, |
|
77 | hgutil, | |
78 | hgext_strip, |
|
78 | hgext_strip, | |
79 | ) |
|
79 | ) | |
80 | from vcsserver.str_utils import ascii_bytes, ascii_str, safe_str, safe_bytes |
|
80 | from vcsserver.str_utils import ascii_bytes, ascii_str, safe_str, safe_bytes | |
81 | from vcsserver.vcs_base import RemoteBase |
|
81 | from vcsserver.vcs_base import RemoteBase | |
82 | from vcsserver.config import hooks as hooks_config |
|
82 | from vcsserver.config import hooks as hooks_config | |
83 | from vcsserver.lib.exc_tracking import format_exc |
|
83 | from vcsserver.lib.exc_tracking import format_exc | |
84 |
|
84 | |||
85 | log = logging.getLogger(__name__) |
|
85 | log = logging.getLogger(__name__) | |
86 |
|
86 | |||
87 |
|
87 | |||
88 | def make_ui_from_config(repo_config): |
|
88 | def make_ui_from_config(repo_config): | |
89 |
|
89 | |||
90 | class LoggingUI(ui.ui): |
|
90 | class LoggingUI(ui.ui): | |
91 |
|
91 | |||
92 | def status(self, *msg, **opts): |
|
92 | def status(self, *msg, **opts): | |
93 | str_msg = map(safe_str, msg) |
|
93 | str_msg = map(safe_str, msg) | |
94 | log.info(' '.join(str_msg).rstrip('\n')) |
|
94 | log.info(' '.join(str_msg).rstrip('\n')) | |
95 | #super(LoggingUI, self).status(*msg, **opts) |
|
95 | #super(LoggingUI, self).status(*msg, **opts) | |
96 |
|
96 | |||
97 | def warn(self, *msg, **opts): |
|
97 | def warn(self, *msg, **opts): | |
98 | str_msg = map(safe_str, msg) |
|
98 | str_msg = map(safe_str, msg) | |
99 | log.warning('ui_logger:'+' '.join(str_msg).rstrip('\n')) |
|
99 | log.warning('ui_logger:'+' '.join(str_msg).rstrip('\n')) | |
100 | #super(LoggingUI, self).warn(*msg, **opts) |
|
100 | #super(LoggingUI, self).warn(*msg, **opts) | |
101 |
|
101 | |||
102 | def error(self, *msg, **opts): |
|
102 | def error(self, *msg, **opts): | |
103 | str_msg = map(safe_str, msg) |
|
103 | str_msg = map(safe_str, msg) | |
104 | log.error('ui_logger:'+' '.join(str_msg).rstrip('\n')) |
|
104 | log.error('ui_logger:'+' '.join(str_msg).rstrip('\n')) | |
105 | #super(LoggingUI, self).error(*msg, **opts) |
|
105 | #super(LoggingUI, self).error(*msg, **opts) | |
106 |
|
106 | |||
107 | def note(self, *msg, **opts): |
|
107 | def note(self, *msg, **opts): | |
108 | str_msg = map(safe_str, msg) |
|
108 | str_msg = map(safe_str, msg) | |
109 | log.info('ui_logger:'+' '.join(str_msg).rstrip('\n')) |
|
109 | log.info('ui_logger:'+' '.join(str_msg).rstrip('\n')) | |
110 | #super(LoggingUI, self).note(*msg, **opts) |
|
110 | #super(LoggingUI, self).note(*msg, **opts) | |
111 |
|
111 | |||
112 | def debug(self, *msg, **opts): |
|
112 | def debug(self, *msg, **opts): | |
113 | str_msg = map(safe_str, msg) |
|
113 | str_msg = map(safe_str, msg) | |
114 | log.debug('ui_logger:'+' '.join(str_msg).rstrip('\n')) |
|
114 | log.debug('ui_logger:'+' '.join(str_msg).rstrip('\n')) | |
115 | #super(LoggingUI, self).debug(*msg, **opts) |
|
115 | #super(LoggingUI, self).debug(*msg, **opts) | |
116 |
|
116 | |||
117 | baseui = LoggingUI() |
|
117 | baseui = LoggingUI() | |
118 |
|
118 | |||
119 | # clean the baseui object |
|
119 | # clean the baseui object | |
120 | baseui._ocfg = hgconfig.config() |
|
120 | baseui._ocfg = hgconfig.config() | |
121 | baseui._ucfg = hgconfig.config() |
|
121 | baseui._ucfg = hgconfig.config() | |
122 | baseui._tcfg = hgconfig.config() |
|
122 | baseui._tcfg = hgconfig.config() | |
123 |
|
123 | |||
124 | for section, option, value in repo_config: |
|
124 | for section, option, value in repo_config: | |
125 | baseui.setconfig(ascii_bytes(section), ascii_bytes(option), ascii_bytes(value)) |
|
125 | baseui.setconfig(ascii_bytes(section), ascii_bytes(option), ascii_bytes(value)) | |
126 |
|
126 | |||
127 | # make our hgweb quiet so it doesn't print output |
|
127 | # make our hgweb quiet so it doesn't print output | |
128 | baseui.setconfig(b'ui', b'quiet', b'true') |
|
128 | baseui.setconfig(b'ui', b'quiet', b'true') | |
129 |
|
129 | |||
130 | baseui.setconfig(b'ui', b'paginate', b'never') |
|
130 | baseui.setconfig(b'ui', b'paginate', b'never') | |
131 | # for better Error reporting of Mercurial |
|
131 | # for better Error reporting of Mercurial | |
132 | baseui.setconfig(b'ui', b'message-output', b'stderr') |
|
132 | baseui.setconfig(b'ui', b'message-output', b'stderr') | |
133 |
|
133 | |||
134 | # force mercurial to only use 1 thread, otherwise it may try to set a |
|
134 | # force mercurial to only use 1 thread, otherwise it may try to set a | |
135 | # signal in a non-main thread, thus generating a ValueError. |
|
135 | # signal in a non-main thread, thus generating a ValueError. | |
136 | baseui.setconfig(b'worker', b'numcpus', 1) |
|
136 | baseui.setconfig(b'worker', b'numcpus', 1) | |
137 |
|
137 | |||
138 | # If there is no config for the largefiles extension, we explicitly disable |
|
138 | # If there is no config for the largefiles extension, we explicitly disable | |
139 | # it here. This overrides settings from repositories hgrc file. Recent |
|
139 | # it here. This overrides settings from repositories hgrc file. Recent | |
140 | # mercurial versions enable largefiles in hgrc on clone from largefile |
|
140 | # mercurial versions enable largefiles in hgrc on clone from largefile | |
141 | # repo. |
|
141 | # repo. | |
142 | if not baseui.hasconfig(b'extensions', b'largefiles'): |
|
142 | if not baseui.hasconfig(b'extensions', b'largefiles'): | |
143 | log.debug('Explicitly disable largefiles extension for repo.') |
|
143 | log.debug('Explicitly disable largefiles extension for repo.') | |
144 | baseui.setconfig(b'extensions', b'largefiles', b'!') |
|
144 | baseui.setconfig(b'extensions', b'largefiles', b'!') | |
145 |
|
145 | |||
146 | return baseui |
|
146 | return baseui | |
147 |
|
147 | |||
148 |
|
148 | |||
149 | def reraise_safe_exceptions(func): |
|
149 | def reraise_safe_exceptions(func): | |
150 | """Decorator for converting mercurial exceptions to something neutral.""" |
|
150 | """Decorator for converting mercurial exceptions to something neutral.""" | |
151 |
|
151 | |||
152 | def wrapper(*args, **kwargs): |
|
152 | def wrapper(*args, **kwargs): | |
153 | try: |
|
153 | try: | |
154 | return func(*args, **kwargs) |
|
154 | return func(*args, **kwargs) | |
155 | except (Abort, InterventionRequired) as e: |
|
155 | except (Abort, InterventionRequired) as e: | |
156 | raise_from_original(exceptions.AbortException(e), e) |
|
156 | raise_from_original(exceptions.AbortException(e), e) | |
157 | except RepoLookupError as e: |
|
157 | except RepoLookupError as e: | |
158 | raise_from_original(exceptions.LookupException(e), e) |
|
158 | raise_from_original(exceptions.LookupException(e), e) | |
159 | except RequirementError as e: |
|
159 | except RequirementError as e: | |
160 | raise_from_original(exceptions.RequirementException(e), e) |
|
160 | raise_from_original(exceptions.RequirementException(e), e) | |
161 | except RepoError as e: |
|
161 | except RepoError as e: | |
162 | raise_from_original(exceptions.VcsException(e), e) |
|
162 | raise_from_original(exceptions.VcsException(e), e) | |
163 | except LookupError as e: |
|
163 | except LookupError as e: | |
164 | raise_from_original(exceptions.LookupException(e), e) |
|
164 | raise_from_original(exceptions.LookupException(e), e) | |
165 | except Exception as e: |
|
165 | except Exception as e: | |
166 | if not hasattr(e, '_vcs_kind'): |
|
166 | if not hasattr(e, '_vcs_kind'): | |
167 | log.exception("Unhandled exception in hg remote call") |
|
167 | log.exception("Unhandled exception in hg remote call") | |
168 | raise_from_original(exceptions.UnhandledException(e), e) |
|
168 | raise_from_original(exceptions.UnhandledException(e), e) | |
169 |
|
169 | |||
170 | raise |
|
170 | raise | |
171 | return wrapper |
|
171 | return wrapper | |
172 |
|
172 | |||
173 |
|
173 | |||
174 | class MercurialFactory(RepoFactory): |
|
174 | class MercurialFactory(RepoFactory): | |
175 | repo_type = 'hg' |
|
175 | repo_type = 'hg' | |
176 |
|
176 | |||
177 | def _create_config(self, config, hooks=True): |
|
177 | def _create_config(self, config, hooks=True): | |
178 | if not hooks: |
|
178 | if not hooks: | |
179 |
|
179 | |||
180 | hooks_to_clean = { |
|
180 | hooks_to_clean = { | |
181 |
|
181 | |||
182 | hooks_config.HOOK_REPO_SIZE, |
|
182 | hooks_config.HOOK_REPO_SIZE, | |
183 | hooks_config.HOOK_PRE_PULL, |
|
183 | hooks_config.HOOK_PRE_PULL, | |
184 | hooks_config.HOOK_PULL, |
|
184 | hooks_config.HOOK_PULL, | |
185 |
|
185 | |||
186 | hooks_config.HOOK_PRE_PUSH, |
|
186 | hooks_config.HOOK_PRE_PUSH, | |
187 | # TODO: what about PRETXT, this was disabled in pre 5.0.0 |
|
187 | # TODO: what about PRETXT, this was disabled in pre 5.0.0 | |
188 | hooks_config.HOOK_PRETX_PUSH, |
|
188 | hooks_config.HOOK_PRETX_PUSH, | |
189 |
|
189 | |||
190 | } |
|
190 | } | |
191 | new_config = [] |
|
191 | new_config = [] | |
192 | for section, option, value in config: |
|
192 | for section, option, value in config: | |
193 | if section == 'hooks' and option in hooks_to_clean: |
|
193 | if section == 'hooks' and option in hooks_to_clean: | |
194 | continue |
|
194 | continue | |
195 | new_config.append((section, option, value)) |
|
195 | new_config.append((section, option, value)) | |
196 | config = new_config |
|
196 | config = new_config | |
197 |
|
197 | |||
198 | baseui = make_ui_from_config(config) |
|
198 | baseui = make_ui_from_config(config) | |
199 | return baseui |
|
199 | return baseui | |
200 |
|
200 | |||
201 | def _create_repo(self, wire, create): |
|
201 | def _create_repo(self, wire, create): | |
202 | baseui = self._create_config(wire["config"]) |
|
202 | baseui = self._create_config(wire["config"]) | |
203 | repo = instance(baseui, safe_bytes(wire["path"]), create) |
|
203 | repo = instance(baseui, safe_bytes(wire["path"]), create) | |
204 | log.debug('repository created: got HG object: %s', repo) |
|
204 | log.debug('repository created: got HG object: %s', repo) | |
205 | return repo |
|
205 | return repo | |
206 |
|
206 | |||
207 | def repo(self, wire, create=False): |
|
207 | def repo(self, wire, create=False): | |
208 | """ |
|
208 | """ | |
209 | Get a repository instance for the given path. |
|
209 | Get a repository instance for the given path. | |
210 | """ |
|
210 | """ | |
211 | return self._create_repo(wire, create) |
|
211 | return self._create_repo(wire, create) | |
212 |
|
212 | |||
213 |
|
213 | |||
214 | def patch_ui_message_output(baseui): |
|
214 | def patch_ui_message_output(baseui): | |
215 | baseui.setconfig(b'ui', b'quiet', b'false') |
|
215 | baseui.setconfig(b'ui', b'quiet', b'false') | |
216 | output = io.BytesIO() |
|
216 | output = io.BytesIO() | |
217 |
|
217 | |||
218 | def write(data, **unused_kwargs): |
|
218 | def write(data, **unused_kwargs): | |
219 | output.write(data) |
|
219 | output.write(data) | |
220 |
|
220 | |||
221 | baseui.status = write |
|
221 | baseui.status = write | |
222 | baseui.write = write |
|
222 | baseui.write = write | |
223 | baseui.warn = write |
|
223 | baseui.warn = write | |
224 | baseui.debug = write |
|
224 | baseui.debug = write | |
225 |
|
225 | |||
226 | return baseui, output |
|
226 | return baseui, output | |
227 |
|
227 | |||
228 |
|
228 | |||
229 | def get_obfuscated_url(url_obj): |
|
229 | def get_obfuscated_url(url_obj): | |
230 | url_obj.passwd = b'*****' if url_obj.passwd else url_obj.passwd |
|
230 | url_obj.passwd = b'*****' if url_obj.passwd else url_obj.passwd | |
231 | url_obj.query = obfuscate_qs(url_obj.query) |
|
231 | url_obj.query = obfuscate_qs(url_obj.query) | |
232 | obfuscated_uri = str(url_obj) |
|
232 | obfuscated_uri = str(url_obj) | |
233 | return obfuscated_uri |
|
233 | return obfuscated_uri | |
234 |
|
234 | |||
235 |
|
235 | |||
236 | def normalize_url_for_hg(url: str): |
|
236 | def normalize_url_for_hg(url: str): | |
237 | _proto = None |
|
237 | _proto = None | |
238 |
|
238 | |||
239 | if '+' in url[:url.find('://')]: |
|
239 | if '+' in url[:url.find('://')]: | |
240 | _proto = url[0:url.find('+')] |
|
240 | _proto = url[0:url.find('+')] | |
241 | url = url[url.find('+') + 1:] |
|
241 | url = url[url.find('+') + 1:] | |
242 | return url, _proto |
|
242 | return url, _proto | |
243 |
|
243 | |||
244 |
|
244 | |||
245 | class HgRemote(RemoteBase): |
|
245 | class HgRemote(RemoteBase): | |
246 |
|
246 | |||
247 | def __init__(self, factory): |
|
247 | def __init__(self, factory): | |
248 | self._factory = factory |
|
248 | self._factory = factory | |
249 | self._bulk_methods = { |
|
249 | self._bulk_methods = { | |
250 | "affected_files": self.ctx_files, |
|
250 | "affected_files": self.ctx_files, | |
251 | "author": self.ctx_user, |
|
251 | "author": self.ctx_user, | |
252 | "branch": self.ctx_branch, |
|
252 | "branch": self.ctx_branch, | |
253 | "children": self.ctx_children, |
|
253 | "children": self.ctx_children, | |
254 | "date": self.ctx_date, |
|
254 | "date": self.ctx_date, | |
255 | "message": self.ctx_description, |
|
255 | "message": self.ctx_description, | |
256 | "parents": self.ctx_parents, |
|
256 | "parents": self.ctx_parents, | |
257 | "status": self.ctx_status, |
|
257 | "status": self.ctx_status, | |
258 | "obsolete": self.ctx_obsolete, |
|
258 | "obsolete": self.ctx_obsolete, | |
259 | "phase": self.ctx_phase, |
|
259 | "phase": self.ctx_phase, | |
260 | "hidden": self.ctx_hidden, |
|
260 | "hidden": self.ctx_hidden, | |
261 | "_file_paths": self.ctx_list, |
|
261 | "_file_paths": self.ctx_list, | |
262 | } |
|
262 | } | |
263 | self._bulk_file_methods = { |
|
263 | self._bulk_file_methods = { | |
264 | "size": self.fctx_size, |
|
264 | "size": self.fctx_size, | |
265 | "data": self.fctx_node_data, |
|
265 | "data": self.fctx_node_data, | |
266 | "flags": self.fctx_flags, |
|
266 | "flags": self.fctx_flags, | |
267 | "is_binary": self.is_binary, |
|
267 | "is_binary": self.is_binary, | |
268 | "md5": self.md5_hash, |
|
268 | "md5": self.md5_hash, | |
269 | } |
|
269 | } | |
270 |
|
270 | |||
271 | def _get_ctx(self, repo, ref): |
|
271 | def _get_ctx(self, repo, ref): | |
272 | return get_ctx(repo, ref) |
|
272 | return get_ctx(repo, ref) | |
273 |
|
273 | |||
274 | @reraise_safe_exceptions |
|
274 | @reraise_safe_exceptions | |
275 | def discover_hg_version(self): |
|
275 | def discover_hg_version(self): | |
276 | from mercurial import util |
|
276 | from mercurial import util | |
277 | return safe_str(util.version()) |
|
277 | return safe_str(util.version()) | |
278 |
|
278 | |||
279 | @reraise_safe_exceptions |
|
279 | @reraise_safe_exceptions | |
280 | def is_empty(self, wire): |
|
280 | def is_empty(self, wire): | |
281 | repo = self._factory.repo(wire) |
|
281 | repo = self._factory.repo(wire) | |
282 |
|
282 | |||
283 | try: |
|
283 | try: | |
284 | return len(repo) == 0 |
|
284 | return len(repo) == 0 | |
285 | except Exception: |
|
285 | except Exception: | |
286 | log.exception("failed to read object_store") |
|
286 | log.exception("failed to read object_store") | |
287 | return False |
|
287 | return False | |
288 |
|
288 | |||
289 | @reraise_safe_exceptions |
|
289 | @reraise_safe_exceptions | |
290 | def bookmarks(self, wire): |
|
290 | def bookmarks(self, wire): | |
291 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
291 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
292 | region = self._region(wire) |
|
292 | region = self._region(wire) | |
293 |
|
293 | |||
294 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
294 | @region.conditional_cache_on_arguments(condition=cache_on) | |
295 | def _bookmarks(_context_uid, _repo_id): |
|
295 | def _bookmarks(_context_uid, _repo_id): | |
296 | repo = self._factory.repo(wire) |
|
296 | repo = self._factory.repo(wire) | |
297 | return {safe_str(name): ascii_str(hex(sha)) for name, sha in repo._bookmarks.items()} |
|
297 | return {safe_str(name): ascii_str(hex(sha)) for name, sha in repo._bookmarks.items()} | |
298 |
|
298 | |||
299 | return _bookmarks(context_uid, repo_id) |
|
299 | return _bookmarks(context_uid, repo_id) | |
300 |
|
300 | |||
301 | @reraise_safe_exceptions |
|
301 | @reraise_safe_exceptions | |
302 | def branches(self, wire, normal, closed): |
|
302 | def branches(self, wire, normal, closed): | |
303 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
303 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
304 | region = self._region(wire) |
|
304 | region = self._region(wire) | |
305 |
|
305 | |||
306 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
306 | @region.conditional_cache_on_arguments(condition=cache_on) | |
307 | def _branches(_context_uid, _repo_id, _normal, _closed): |
|
307 | def _branches(_context_uid, _repo_id, _normal, _closed): | |
308 | repo = self._factory.repo(wire) |
|
308 | repo = self._factory.repo(wire) | |
309 | iter_branches = repo.branchmap().iterbranches() |
|
309 | iter_branches = repo.branchmap().iterbranches() | |
310 | bt = {} |
|
310 | bt = {} | |
311 | for branch_name, _heads, tip_node, is_closed in iter_branches: |
|
311 | for branch_name, _heads, tip_node, is_closed in iter_branches: | |
312 | if normal and not is_closed: |
|
312 | if normal and not is_closed: | |
313 | bt[safe_str(branch_name)] = ascii_str(hex(tip_node)) |
|
313 | bt[safe_str(branch_name)] = ascii_str(hex(tip_node)) | |
314 | if closed and is_closed: |
|
314 | if closed and is_closed: | |
315 | bt[safe_str(branch_name)] = ascii_str(hex(tip_node)) |
|
315 | bt[safe_str(branch_name)] = ascii_str(hex(tip_node)) | |
316 |
|
316 | |||
317 | return bt |
|
317 | return bt | |
318 |
|
318 | |||
319 | return _branches(context_uid, repo_id, normal, closed) |
|
319 | return _branches(context_uid, repo_id, normal, closed) | |
320 |
|
320 | |||
321 | @reraise_safe_exceptions |
|
321 | @reraise_safe_exceptions | |
322 | def bulk_request(self, wire, commit_id, pre_load): |
|
322 | def bulk_request(self, wire, commit_id, pre_load): | |
323 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
323 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
324 | region = self._region(wire) |
|
324 | region = self._region(wire) | |
325 |
|
325 | |||
326 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
326 | @region.conditional_cache_on_arguments(condition=cache_on) | |
327 | def _bulk_request(_repo_id, _commit_id, _pre_load): |
|
327 | def _bulk_request(_repo_id, _commit_id, _pre_load): | |
328 | result = {} |
|
328 | result = {} | |
329 | for attr in pre_load: |
|
329 | for attr in pre_load: | |
330 | try: |
|
330 | try: | |
331 | method = self._bulk_methods[attr] |
|
331 | method = self._bulk_methods[attr] | |
332 | wire.update({'cache': False}) # disable cache for bulk calls so we don't double cache |
|
332 | wire.update({'cache': False}) # disable cache for bulk calls so we don't double cache | |
333 | result[attr] = method(wire, commit_id) |
|
333 | result[attr] = method(wire, commit_id) | |
334 | except KeyError as e: |
|
334 | except KeyError as e: | |
335 | raise exceptions.VcsException(e)( |
|
335 | raise exceptions.VcsException(e)( | |
336 |
'Unknown bulk attribute: " |
|
336 | f'Unknown bulk attribute: "{attr}"') | |
337 | return result |
|
337 | return result | |
338 |
|
338 | |||
339 | return _bulk_request(repo_id, commit_id, sorted(pre_load)) |
|
339 | return _bulk_request(repo_id, commit_id, sorted(pre_load)) | |
340 |
|
340 | |||
341 | @reraise_safe_exceptions |
|
341 | @reraise_safe_exceptions | |
342 | def ctx_branch(self, wire, commit_id): |
|
342 | def ctx_branch(self, wire, commit_id): | |
343 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
343 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
344 | region = self._region(wire) |
|
344 | region = self._region(wire) | |
345 |
|
345 | |||
346 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
346 | @region.conditional_cache_on_arguments(condition=cache_on) | |
347 | def _ctx_branch(_repo_id, _commit_id): |
|
347 | def _ctx_branch(_repo_id, _commit_id): | |
348 | repo = self._factory.repo(wire) |
|
348 | repo = self._factory.repo(wire) | |
349 | ctx = self._get_ctx(repo, commit_id) |
|
349 | ctx = self._get_ctx(repo, commit_id) | |
350 | return ctx.branch() |
|
350 | return ctx.branch() | |
351 | return _ctx_branch(repo_id, commit_id) |
|
351 | return _ctx_branch(repo_id, commit_id) | |
352 |
|
352 | |||
353 | @reraise_safe_exceptions |
|
353 | @reraise_safe_exceptions | |
354 | def ctx_date(self, wire, commit_id): |
|
354 | def ctx_date(self, wire, commit_id): | |
355 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
355 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
356 | region = self._region(wire) |
|
356 | region = self._region(wire) | |
357 |
|
357 | |||
358 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
358 | @region.conditional_cache_on_arguments(condition=cache_on) | |
359 | def _ctx_date(_repo_id, _commit_id): |
|
359 | def _ctx_date(_repo_id, _commit_id): | |
360 | repo = self._factory.repo(wire) |
|
360 | repo = self._factory.repo(wire) | |
361 | ctx = self._get_ctx(repo, commit_id) |
|
361 | ctx = self._get_ctx(repo, commit_id) | |
362 | return ctx.date() |
|
362 | return ctx.date() | |
363 | return _ctx_date(repo_id, commit_id) |
|
363 | return _ctx_date(repo_id, commit_id) | |
364 |
|
364 | |||
365 | @reraise_safe_exceptions |
|
365 | @reraise_safe_exceptions | |
366 | def ctx_description(self, wire, revision): |
|
366 | def ctx_description(self, wire, revision): | |
367 | repo = self._factory.repo(wire) |
|
367 | repo = self._factory.repo(wire) | |
368 | ctx = self._get_ctx(repo, revision) |
|
368 | ctx = self._get_ctx(repo, revision) | |
369 | return ctx.description() |
|
369 | return ctx.description() | |
370 |
|
370 | |||
371 | @reraise_safe_exceptions |
|
371 | @reraise_safe_exceptions | |
372 | def ctx_files(self, wire, commit_id): |
|
372 | def ctx_files(self, wire, commit_id): | |
373 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
373 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
374 | region = self._region(wire) |
|
374 | region = self._region(wire) | |
375 |
|
375 | |||
376 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
376 | @region.conditional_cache_on_arguments(condition=cache_on) | |
377 | def _ctx_files(_repo_id, _commit_id): |
|
377 | def _ctx_files(_repo_id, _commit_id): | |
378 | repo = self._factory.repo(wire) |
|
378 | repo = self._factory.repo(wire) | |
379 | ctx = self._get_ctx(repo, commit_id) |
|
379 | ctx = self._get_ctx(repo, commit_id) | |
380 | return ctx.files() |
|
380 | return ctx.files() | |
381 |
|
381 | |||
382 | return _ctx_files(repo_id, commit_id) |
|
382 | return _ctx_files(repo_id, commit_id) | |
383 |
|
383 | |||
384 | @reraise_safe_exceptions |
|
384 | @reraise_safe_exceptions | |
385 | def ctx_list(self, path, revision): |
|
385 | def ctx_list(self, path, revision): | |
386 | repo = self._factory.repo(path) |
|
386 | repo = self._factory.repo(path) | |
387 | ctx = self._get_ctx(repo, revision) |
|
387 | ctx = self._get_ctx(repo, revision) | |
388 | return list(ctx) |
|
388 | return list(ctx) | |
389 |
|
389 | |||
390 | @reraise_safe_exceptions |
|
390 | @reraise_safe_exceptions | |
391 | def ctx_parents(self, wire, commit_id): |
|
391 | def ctx_parents(self, wire, commit_id): | |
392 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
392 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
393 | region = self._region(wire) |
|
393 | region = self._region(wire) | |
394 |
|
394 | |||
395 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
395 | @region.conditional_cache_on_arguments(condition=cache_on) | |
396 | def _ctx_parents(_repo_id, _commit_id): |
|
396 | def _ctx_parents(_repo_id, _commit_id): | |
397 | repo = self._factory.repo(wire) |
|
397 | repo = self._factory.repo(wire) | |
398 | ctx = self._get_ctx(repo, commit_id) |
|
398 | ctx = self._get_ctx(repo, commit_id) | |
399 | return [parent.hex() for parent in ctx.parents() |
|
399 | return [parent.hex() for parent in ctx.parents() | |
400 | if not (parent.hidden() or parent.obsolete())] |
|
400 | if not (parent.hidden() or parent.obsolete())] | |
401 |
|
401 | |||
402 | return _ctx_parents(repo_id, commit_id) |
|
402 | return _ctx_parents(repo_id, commit_id) | |
403 |
|
403 | |||
404 | @reraise_safe_exceptions |
|
404 | @reraise_safe_exceptions | |
405 | def ctx_children(self, wire, commit_id): |
|
405 | def ctx_children(self, wire, commit_id): | |
406 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
406 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
407 | region = self._region(wire) |
|
407 | region = self._region(wire) | |
408 |
|
408 | |||
409 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
409 | @region.conditional_cache_on_arguments(condition=cache_on) | |
410 | def _ctx_children(_repo_id, _commit_id): |
|
410 | def _ctx_children(_repo_id, _commit_id): | |
411 | repo = self._factory.repo(wire) |
|
411 | repo = self._factory.repo(wire) | |
412 | ctx = self._get_ctx(repo, commit_id) |
|
412 | ctx = self._get_ctx(repo, commit_id) | |
413 | return [child.hex() for child in ctx.children() |
|
413 | return [child.hex() for child in ctx.children() | |
414 | if not (child.hidden() or child.obsolete())] |
|
414 | if not (child.hidden() or child.obsolete())] | |
415 |
|
415 | |||
416 | return _ctx_children(repo_id, commit_id) |
|
416 | return _ctx_children(repo_id, commit_id) | |
417 |
|
417 | |||
418 | @reraise_safe_exceptions |
|
418 | @reraise_safe_exceptions | |
419 | def ctx_phase(self, wire, commit_id): |
|
419 | def ctx_phase(self, wire, commit_id): | |
420 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
420 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
421 | region = self._region(wire) |
|
421 | region = self._region(wire) | |
422 |
|
422 | |||
423 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
423 | @region.conditional_cache_on_arguments(condition=cache_on) | |
424 | def _ctx_phase(_context_uid, _repo_id, _commit_id): |
|
424 | def _ctx_phase(_context_uid, _repo_id, _commit_id): | |
425 | repo = self._factory.repo(wire) |
|
425 | repo = self._factory.repo(wire) | |
426 | ctx = self._get_ctx(repo, commit_id) |
|
426 | ctx = self._get_ctx(repo, commit_id) | |
427 | # public=0, draft=1, secret=3 |
|
427 | # public=0, draft=1, secret=3 | |
428 | return ctx.phase() |
|
428 | return ctx.phase() | |
429 | return _ctx_phase(context_uid, repo_id, commit_id) |
|
429 | return _ctx_phase(context_uid, repo_id, commit_id) | |
430 |
|
430 | |||
431 | @reraise_safe_exceptions |
|
431 | @reraise_safe_exceptions | |
432 | def ctx_obsolete(self, wire, commit_id): |
|
432 | def ctx_obsolete(self, wire, commit_id): | |
433 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
433 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
434 | region = self._region(wire) |
|
434 | region = self._region(wire) | |
435 |
|
435 | |||
436 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
436 | @region.conditional_cache_on_arguments(condition=cache_on) | |
437 | def _ctx_obsolete(_context_uid, _repo_id, _commit_id): |
|
437 | def _ctx_obsolete(_context_uid, _repo_id, _commit_id): | |
438 | repo = self._factory.repo(wire) |
|
438 | repo = self._factory.repo(wire) | |
439 | ctx = self._get_ctx(repo, commit_id) |
|
439 | ctx = self._get_ctx(repo, commit_id) | |
440 | return ctx.obsolete() |
|
440 | return ctx.obsolete() | |
441 | return _ctx_obsolete(context_uid, repo_id, commit_id) |
|
441 | return _ctx_obsolete(context_uid, repo_id, commit_id) | |
442 |
|
442 | |||
443 | @reraise_safe_exceptions |
|
443 | @reraise_safe_exceptions | |
444 | def ctx_hidden(self, wire, commit_id): |
|
444 | def ctx_hidden(self, wire, commit_id): | |
445 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
445 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
446 | region = self._region(wire) |
|
446 | region = self._region(wire) | |
447 |
|
447 | |||
448 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
448 | @region.conditional_cache_on_arguments(condition=cache_on) | |
449 | def _ctx_hidden(_context_uid, _repo_id, _commit_id): |
|
449 | def _ctx_hidden(_context_uid, _repo_id, _commit_id): | |
450 | repo = self._factory.repo(wire) |
|
450 | repo = self._factory.repo(wire) | |
451 | ctx = self._get_ctx(repo, commit_id) |
|
451 | ctx = self._get_ctx(repo, commit_id) | |
452 | return ctx.hidden() |
|
452 | return ctx.hidden() | |
453 | return _ctx_hidden(context_uid, repo_id, commit_id) |
|
453 | return _ctx_hidden(context_uid, repo_id, commit_id) | |
454 |
|
454 | |||
455 | @reraise_safe_exceptions |
|
455 | @reraise_safe_exceptions | |
456 | def ctx_substate(self, wire, revision): |
|
456 | def ctx_substate(self, wire, revision): | |
457 | repo = self._factory.repo(wire) |
|
457 | repo = self._factory.repo(wire) | |
458 | ctx = self._get_ctx(repo, revision) |
|
458 | ctx = self._get_ctx(repo, revision) | |
459 | return ctx.substate |
|
459 | return ctx.substate | |
460 |
|
460 | |||
461 | @reraise_safe_exceptions |
|
461 | @reraise_safe_exceptions | |
462 | def ctx_status(self, wire, revision): |
|
462 | def ctx_status(self, wire, revision): | |
463 | repo = self._factory.repo(wire) |
|
463 | repo = self._factory.repo(wire) | |
464 | ctx = self._get_ctx(repo, revision) |
|
464 | ctx = self._get_ctx(repo, revision) | |
465 | status = repo[ctx.p1().node()].status(other=ctx.node()) |
|
465 | status = repo[ctx.p1().node()].status(other=ctx.node()) | |
466 | # object of status (odd, custom named tuple in mercurial) is not |
|
466 | # object of status (odd, custom named tuple in mercurial) is not | |
467 | # correctly serializable, we make it a list, as the underling |
|
467 | # correctly serializable, we make it a list, as the underling | |
468 | # API expects this to be a list |
|
468 | # API expects this to be a list | |
469 | return list(status) |
|
469 | return list(status) | |
470 |
|
470 | |||
471 | @reraise_safe_exceptions |
|
471 | @reraise_safe_exceptions | |
472 | def ctx_user(self, wire, revision): |
|
472 | def ctx_user(self, wire, revision): | |
473 | repo = self._factory.repo(wire) |
|
473 | repo = self._factory.repo(wire) | |
474 | ctx = self._get_ctx(repo, revision) |
|
474 | ctx = self._get_ctx(repo, revision) | |
475 | return ctx.user() |
|
475 | return ctx.user() | |
476 |
|
476 | |||
477 | @reraise_safe_exceptions |
|
477 | @reraise_safe_exceptions | |
478 | def check_url(self, url, config): |
|
478 | def check_url(self, url, config): | |
479 | url, _proto = normalize_url_for_hg(url) |
|
479 | url, _proto = normalize_url_for_hg(url) | |
480 | url_obj = url_parser(safe_bytes(url)) |
|
480 | url_obj = url_parser(safe_bytes(url)) | |
481 |
|
481 | |||
482 | test_uri = safe_str(url_obj.authinfo()[0]) |
|
482 | test_uri = safe_str(url_obj.authinfo()[0]) | |
483 | authinfo = url_obj.authinfo()[1] |
|
483 | authinfo = url_obj.authinfo()[1] | |
484 | obfuscated_uri = get_obfuscated_url(url_obj) |
|
484 | obfuscated_uri = get_obfuscated_url(url_obj) | |
485 | log.info("Checking URL for remote cloning/import: %s", obfuscated_uri) |
|
485 | log.info("Checking URL for remote cloning/import: %s", obfuscated_uri) | |
486 |
|
486 | |||
487 | handlers = [] |
|
487 | handlers = [] | |
488 | if authinfo: |
|
488 | if authinfo: | |
489 | # create a password manager |
|
489 | # create a password manager | |
490 | passmgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() |
|
490 | passmgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() | |
491 | passmgr.add_password(*authinfo) |
|
491 | passmgr.add_password(*authinfo) | |
492 |
|
492 | |||
493 | handlers.extend((httpbasicauthhandler(passmgr), |
|
493 | handlers.extend((httpbasicauthhandler(passmgr), | |
494 | httpdigestauthhandler(passmgr))) |
|
494 | httpdigestauthhandler(passmgr))) | |
495 |
|
495 | |||
496 | o = urllib.request.build_opener(*handlers) |
|
496 | o = urllib.request.build_opener(*handlers) | |
497 | o.addheaders = [('Content-Type', 'application/mercurial-0.1'), |
|
497 | o.addheaders = [('Content-Type', 'application/mercurial-0.1'), | |
498 | ('Accept', 'application/mercurial-0.1')] |
|
498 | ('Accept', 'application/mercurial-0.1')] | |
499 |
|
499 | |||
500 | q = {"cmd": 'between'} |
|
500 | q = {"cmd": 'between'} | |
501 | q.update({'pairs': "{}-{}".format('0' * 40, '0' * 40)}) |
|
501 | q.update({'pairs': "{}-{}".format('0' * 40, '0' * 40)}) | |
502 |
qs = '? |
|
502 | qs = f'?{urllib.parse.urlencode(q)}' | |
503 | cu = f"{test_uri}{qs}" |
|
503 | cu = f"{test_uri}{qs}" | |
504 | req = urllib.request.Request(cu, None, {}) |
|
504 | req = urllib.request.Request(cu, None, {}) | |
505 |
|
505 | |||
506 | try: |
|
506 | try: | |
507 | log.debug("Trying to open URL %s", obfuscated_uri) |
|
507 | log.debug("Trying to open URL %s", obfuscated_uri) | |
508 | resp = o.open(req) |
|
508 | resp = o.open(req) | |
509 | if resp.code != 200: |
|
509 | if resp.code != 200: | |
510 | raise exceptions.URLError()('Return Code is not 200') |
|
510 | raise exceptions.URLError()('Return Code is not 200') | |
511 | except Exception as e: |
|
511 | except Exception as e: | |
512 | log.warning("URL cannot be opened: %s", obfuscated_uri, exc_info=True) |
|
512 | log.warning("URL cannot be opened: %s", obfuscated_uri, exc_info=True) | |
513 | # means it cannot be cloned |
|
513 | # means it cannot be cloned | |
514 | raise exceptions.URLError(e)(f"[{obfuscated_uri}] org_exc: {e}") |
|
514 | raise exceptions.URLError(e)(f"[{obfuscated_uri}] org_exc: {e}") | |
515 |
|
515 | |||
516 | # now check if it's a proper hg repo, but don't do it for svn |
|
516 | # now check if it's a proper hg repo, but don't do it for svn | |
517 | try: |
|
517 | try: | |
518 | if _proto == 'svn': |
|
518 | if _proto == 'svn': | |
519 | pass |
|
519 | pass | |
520 | else: |
|
520 | else: | |
521 | # check for pure hg repos |
|
521 | # check for pure hg repos | |
522 | log.debug( |
|
522 | log.debug( | |
523 | "Verifying if URL is a Mercurial repository: %s", obfuscated_uri) |
|
523 | "Verifying if URL is a Mercurial repository: %s", obfuscated_uri) | |
524 | ui = make_ui_from_config(config) |
|
524 | ui = make_ui_from_config(config) | |
525 | peer_checker = makepeer(ui, safe_bytes(url)) |
|
525 | peer_checker = makepeer(ui, safe_bytes(url)) | |
526 | peer_checker.lookup(b'tip') |
|
526 | peer_checker.lookup(b'tip') | |
527 | except Exception as e: |
|
527 | except Exception as e: | |
528 | log.warning("URL is not a valid Mercurial repository: %s", |
|
528 | log.warning("URL is not a valid Mercurial repository: %s", | |
529 | obfuscated_uri) |
|
529 | obfuscated_uri) | |
530 | raise exceptions.URLError(e)( |
|
530 | raise exceptions.URLError(e)( | |
531 |
"url [ |
|
531 | f"url [{obfuscated_uri}] does not look like an hg repo org_exc: {e}") | |
532 | % (obfuscated_uri, e)) |
|
|||
533 |
|
532 | |||
534 | log.info("URL is a valid Mercurial repository: %s", obfuscated_uri) |
|
533 | log.info("URL is a valid Mercurial repository: %s", obfuscated_uri) | |
535 | return True |
|
534 | return True | |
536 |
|
535 | |||
537 | @reraise_safe_exceptions |
|
536 | @reraise_safe_exceptions | |
538 | def diff(self, wire, commit_id_1, commit_id_2, file_filter, opt_git, opt_ignorews, context): |
|
537 | def diff(self, wire, commit_id_1, commit_id_2, file_filter, opt_git, opt_ignorews, context): | |
539 | repo = self._factory.repo(wire) |
|
538 | repo = self._factory.repo(wire) | |
540 |
|
539 | |||
541 | if file_filter: |
|
540 | if file_filter: | |
542 | # unpack the file-filter |
|
541 | # unpack the file-filter | |
543 | repo_path, node_path = file_filter |
|
542 | repo_path, node_path = file_filter | |
544 | match_filter = match(safe_bytes(repo_path), b'', [safe_bytes(node_path)]) |
|
543 | match_filter = match(safe_bytes(repo_path), b'', [safe_bytes(node_path)]) | |
545 | else: |
|
544 | else: | |
546 | match_filter = file_filter |
|
545 | match_filter = file_filter | |
547 | opts = diffopts(git=opt_git, ignorews=opt_ignorews, context=context, showfunc=1) |
|
546 | opts = diffopts(git=opt_git, ignorews=opt_ignorews, context=context, showfunc=1) | |
548 |
|
547 | |||
549 | try: |
|
548 | try: | |
550 | diff_iter = patch.diff( |
|
549 | diff_iter = patch.diff( | |
551 | repo, node1=commit_id_1, node2=commit_id_2, match=match_filter, opts=opts) |
|
550 | repo, node1=commit_id_1, node2=commit_id_2, match=match_filter, opts=opts) | |
552 | return BytesEnvelope(b"".join(diff_iter)) |
|
551 | return BytesEnvelope(b"".join(diff_iter)) | |
553 | except RepoLookupError as e: |
|
552 | except RepoLookupError as e: | |
554 | raise exceptions.LookupException(e)() |
|
553 | raise exceptions.LookupException(e)() | |
555 |
|
554 | |||
556 | @reraise_safe_exceptions |
|
555 | @reraise_safe_exceptions | |
557 | def node_history(self, wire, revision, path, limit): |
|
556 | def node_history(self, wire, revision, path, limit): | |
558 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
557 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
559 | region = self._region(wire) |
|
558 | region = self._region(wire) | |
560 |
|
559 | |||
561 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
560 | @region.conditional_cache_on_arguments(condition=cache_on) | |
562 | def _node_history(_context_uid, _repo_id, _revision, _path, _limit): |
|
561 | def _node_history(_context_uid, _repo_id, _revision, _path, _limit): | |
563 | repo = self._factory.repo(wire) |
|
562 | repo = self._factory.repo(wire) | |
564 |
|
563 | |||
565 | ctx = self._get_ctx(repo, revision) |
|
564 | ctx = self._get_ctx(repo, revision) | |
566 | fctx = ctx.filectx(safe_bytes(path)) |
|
565 | fctx = ctx.filectx(safe_bytes(path)) | |
567 |
|
566 | |||
568 | def history_iter(): |
|
567 | def history_iter(): | |
569 | limit_rev = fctx.rev() |
|
568 | limit_rev = fctx.rev() | |
570 | for fctx_candidate in reversed(list(fctx.filelog())): |
|
569 | for fctx_candidate in reversed(list(fctx.filelog())): | |
571 | f_obj = fctx.filectx(fctx_candidate) |
|
570 | f_obj = fctx.filectx(fctx_candidate) | |
572 |
|
571 | |||
573 | # NOTE: This can be problematic...we can hide ONLY history node resulting in empty history |
|
572 | # NOTE: This can be problematic...we can hide ONLY history node resulting in empty history | |
574 | _ctx = obj.changectx() |
|
573 | _ctx = obj.changectx() | |
575 | if _ctx.hidden() or _ctx.obsolete(): |
|
574 | if _ctx.hidden() or _ctx.obsolete(): | |
576 | continue |
|
575 | continue | |
577 |
|
576 | |||
578 | if limit_rev >= f_obj.rev(): |
|
577 | if limit_rev >= f_obj.rev(): | |
579 | yield f_obj |
|
578 | yield f_obj | |
580 |
|
579 | |||
581 | history = [] |
|
580 | history = [] | |
582 | for cnt, obj in enumerate(history_iter()): |
|
581 | for cnt, obj in enumerate(history_iter()): | |
583 | if limit and cnt >= limit: |
|
582 | if limit and cnt >= limit: | |
584 | break |
|
583 | break | |
585 | history.append(hex(obj.node())) |
|
584 | history.append(hex(obj.node())) | |
586 |
|
585 | |||
587 | return [x for x in history] |
|
586 | return [x for x in history] | |
588 | return _node_history(context_uid, repo_id, revision, path, limit) |
|
587 | return _node_history(context_uid, repo_id, revision, path, limit) | |
589 |
|
588 | |||
590 | @reraise_safe_exceptions |
|
589 | @reraise_safe_exceptions | |
591 | def node_history_until(self, wire, revision, path, limit): |
|
590 | def node_history_until(self, wire, revision, path, limit): | |
592 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
591 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
593 | region = self._region(wire) |
|
592 | region = self._region(wire) | |
594 |
|
593 | |||
595 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
594 | @region.conditional_cache_on_arguments(condition=cache_on) | |
596 | def _node_history_until(_context_uid, _repo_id): |
|
595 | def _node_history_until(_context_uid, _repo_id): | |
597 | repo = self._factory.repo(wire) |
|
596 | repo = self._factory.repo(wire) | |
598 | ctx = self._get_ctx(repo, revision) |
|
597 | ctx = self._get_ctx(repo, revision) | |
599 | fctx = ctx.filectx(safe_bytes(path)) |
|
598 | fctx = ctx.filectx(safe_bytes(path)) | |
600 |
|
599 | |||
601 | file_log = list(fctx.filelog()) |
|
600 | file_log = list(fctx.filelog()) | |
602 | if limit: |
|
601 | if limit: | |
603 | # Limit to the last n items |
|
602 | # Limit to the last n items | |
604 | file_log = file_log[-limit:] |
|
603 | file_log = file_log[-limit:] | |
605 |
|
604 | |||
606 | return [hex(fctx.filectx(cs).node()) for cs in reversed(file_log)] |
|
605 | return [hex(fctx.filectx(cs).node()) for cs in reversed(file_log)] | |
607 | return _node_history_until(context_uid, repo_id, revision, path, limit) |
|
606 | return _node_history_until(context_uid, repo_id, revision, path, limit) | |
608 |
|
607 | |||
609 | @reraise_safe_exceptions |
|
608 | @reraise_safe_exceptions | |
610 | def bulk_file_request(self, wire, commit_id, path, pre_load): |
|
609 | def bulk_file_request(self, wire, commit_id, path, pre_load): | |
611 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
610 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
612 | region = self._region(wire) |
|
611 | region = self._region(wire) | |
613 |
|
612 | |||
614 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
613 | @region.conditional_cache_on_arguments(condition=cache_on) | |
615 | def _bulk_file_request(_repo_id, _commit_id, _path, _pre_load): |
|
614 | def _bulk_file_request(_repo_id, _commit_id, _path, _pre_load): | |
616 | result = {} |
|
615 | result = {} | |
617 | for attr in pre_load: |
|
616 | for attr in pre_load: | |
618 | try: |
|
617 | try: | |
619 | method = self._bulk_file_methods[attr] |
|
618 | method = self._bulk_file_methods[attr] | |
620 | wire.update({'cache': False}) # disable cache for bulk calls so we don't double cache |
|
619 | wire.update({'cache': False}) # disable cache for bulk calls so we don't double cache | |
621 | result[attr] = method(wire, _commit_id, _path) |
|
620 | result[attr] = method(wire, _commit_id, _path) | |
622 | except KeyError as e: |
|
621 | except KeyError as e: | |
623 | raise exceptions.VcsException(e)(f'Unknown bulk attribute: "{attr}"') |
|
622 | raise exceptions.VcsException(e)(f'Unknown bulk attribute: "{attr}"') | |
624 | return result |
|
623 | return result | |
625 |
|
624 | |||
626 | return BinaryEnvelope(_bulk_file_request(repo_id, commit_id, path, sorted(pre_load))) |
|
625 | return BinaryEnvelope(_bulk_file_request(repo_id, commit_id, path, sorted(pre_load))) | |
627 |
|
626 | |||
628 | @reraise_safe_exceptions |
|
627 | @reraise_safe_exceptions | |
629 | def fctx_annotate(self, wire, revision, path): |
|
628 | def fctx_annotate(self, wire, revision, path): | |
630 | repo = self._factory.repo(wire) |
|
629 | repo = self._factory.repo(wire) | |
631 | ctx = self._get_ctx(repo, revision) |
|
630 | ctx = self._get_ctx(repo, revision) | |
632 | fctx = ctx.filectx(safe_bytes(path)) |
|
631 | fctx = ctx.filectx(safe_bytes(path)) | |
633 |
|
632 | |||
634 | result = [] |
|
633 | result = [] | |
635 | for i, annotate_obj in enumerate(fctx.annotate(), 1): |
|
634 | for i, annotate_obj in enumerate(fctx.annotate(), 1): | |
636 | ln_no = i |
|
635 | ln_no = i | |
637 | sha = hex(annotate_obj.fctx.node()) |
|
636 | sha = hex(annotate_obj.fctx.node()) | |
638 | content = annotate_obj.text |
|
637 | content = annotate_obj.text | |
639 | result.append((ln_no, ascii_str(sha), content)) |
|
638 | result.append((ln_no, ascii_str(sha), content)) | |
640 | return BinaryEnvelope(result) |
|
639 | return BinaryEnvelope(result) | |
641 |
|
640 | |||
642 | @reraise_safe_exceptions |
|
641 | @reraise_safe_exceptions | |
643 | def fctx_node_data(self, wire, revision, path): |
|
642 | def fctx_node_data(self, wire, revision, path): | |
644 | repo = self._factory.repo(wire) |
|
643 | repo = self._factory.repo(wire) | |
645 | ctx = self._get_ctx(repo, revision) |
|
644 | ctx = self._get_ctx(repo, revision) | |
646 | fctx = ctx.filectx(safe_bytes(path)) |
|
645 | fctx = ctx.filectx(safe_bytes(path)) | |
647 | return BytesEnvelope(fctx.data()) |
|
646 | return BytesEnvelope(fctx.data()) | |
648 |
|
647 | |||
649 | @reraise_safe_exceptions |
|
648 | @reraise_safe_exceptions | |
650 | def fctx_flags(self, wire, commit_id, path): |
|
649 | def fctx_flags(self, wire, commit_id, path): | |
651 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
650 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
652 | region = self._region(wire) |
|
651 | region = self._region(wire) | |
653 |
|
652 | |||
654 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
653 | @region.conditional_cache_on_arguments(condition=cache_on) | |
655 | def _fctx_flags(_repo_id, _commit_id, _path): |
|
654 | def _fctx_flags(_repo_id, _commit_id, _path): | |
656 | repo = self._factory.repo(wire) |
|
655 | repo = self._factory.repo(wire) | |
657 | ctx = self._get_ctx(repo, commit_id) |
|
656 | ctx = self._get_ctx(repo, commit_id) | |
658 | fctx = ctx.filectx(safe_bytes(path)) |
|
657 | fctx = ctx.filectx(safe_bytes(path)) | |
659 | return fctx.flags() |
|
658 | return fctx.flags() | |
660 |
|
659 | |||
661 | return _fctx_flags(repo_id, commit_id, path) |
|
660 | return _fctx_flags(repo_id, commit_id, path) | |
662 |
|
661 | |||
663 | @reraise_safe_exceptions |
|
662 | @reraise_safe_exceptions | |
664 | def fctx_size(self, wire, commit_id, path): |
|
663 | def fctx_size(self, wire, commit_id, path): | |
665 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
664 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
666 | region = self._region(wire) |
|
665 | region = self._region(wire) | |
667 |
|
666 | |||
668 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
667 | @region.conditional_cache_on_arguments(condition=cache_on) | |
669 | def _fctx_size(_repo_id, _revision, _path): |
|
668 | def _fctx_size(_repo_id, _revision, _path): | |
670 | repo = self._factory.repo(wire) |
|
669 | repo = self._factory.repo(wire) | |
671 | ctx = self._get_ctx(repo, commit_id) |
|
670 | ctx = self._get_ctx(repo, commit_id) | |
672 | fctx = ctx.filectx(safe_bytes(path)) |
|
671 | fctx = ctx.filectx(safe_bytes(path)) | |
673 | return fctx.size() |
|
672 | return fctx.size() | |
674 | return _fctx_size(repo_id, commit_id, path) |
|
673 | return _fctx_size(repo_id, commit_id, path) | |
675 |
|
674 | |||
676 | @reraise_safe_exceptions |
|
675 | @reraise_safe_exceptions | |
677 | def get_all_commit_ids(self, wire, name): |
|
676 | def get_all_commit_ids(self, wire, name): | |
678 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
677 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
679 | region = self._region(wire) |
|
678 | region = self._region(wire) | |
680 |
|
679 | |||
681 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
680 | @region.conditional_cache_on_arguments(condition=cache_on) | |
682 | def _get_all_commit_ids(_context_uid, _repo_id, _name): |
|
681 | def _get_all_commit_ids(_context_uid, _repo_id, _name): | |
683 | repo = self._factory.repo(wire) |
|
682 | repo = self._factory.repo(wire) | |
684 | revs = [ascii_str(repo[x].hex()) for x in repo.filtered(b'visible').changelog.revs()] |
|
683 | revs = [ascii_str(repo[x].hex()) for x in repo.filtered(b'visible').changelog.revs()] | |
685 | return revs |
|
684 | return revs | |
686 | return _get_all_commit_ids(context_uid, repo_id, name) |
|
685 | return _get_all_commit_ids(context_uid, repo_id, name) | |
687 |
|
686 | |||
688 | @reraise_safe_exceptions |
|
687 | @reraise_safe_exceptions | |
689 | def get_config_value(self, wire, section, name, untrusted=False): |
|
688 | def get_config_value(self, wire, section, name, untrusted=False): | |
690 | repo = self._factory.repo(wire) |
|
689 | repo = self._factory.repo(wire) | |
691 | return repo.ui.config(ascii_bytes(section), ascii_bytes(name), untrusted=untrusted) |
|
690 | return repo.ui.config(ascii_bytes(section), ascii_bytes(name), untrusted=untrusted) | |
692 |
|
691 | |||
693 | @reraise_safe_exceptions |
|
692 | @reraise_safe_exceptions | |
694 | def is_large_file(self, wire, commit_id, path): |
|
693 | def is_large_file(self, wire, commit_id, path): | |
695 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
694 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
696 | region = self._region(wire) |
|
695 | region = self._region(wire) | |
697 |
|
696 | |||
698 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
697 | @region.conditional_cache_on_arguments(condition=cache_on) | |
699 | def _is_large_file(_context_uid, _repo_id, _commit_id, _path): |
|
698 | def _is_large_file(_context_uid, _repo_id, _commit_id, _path): | |
700 | return largefiles.lfutil.isstandin(safe_bytes(path)) |
|
699 | return largefiles.lfutil.isstandin(safe_bytes(path)) | |
701 |
|
700 | |||
702 | return _is_large_file(context_uid, repo_id, commit_id, path) |
|
701 | return _is_large_file(context_uid, repo_id, commit_id, path) | |
703 |
|
702 | |||
704 | @reraise_safe_exceptions |
|
703 | @reraise_safe_exceptions | |
705 | def is_binary(self, wire, revision, path): |
|
704 | def is_binary(self, wire, revision, path): | |
706 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
705 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
707 | region = self._region(wire) |
|
706 | region = self._region(wire) | |
708 |
|
707 | |||
709 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
708 | @region.conditional_cache_on_arguments(condition=cache_on) | |
710 | def _is_binary(_repo_id, _sha, _path): |
|
709 | def _is_binary(_repo_id, _sha, _path): | |
711 | repo = self._factory.repo(wire) |
|
710 | repo = self._factory.repo(wire) | |
712 | ctx = self._get_ctx(repo, revision) |
|
711 | ctx = self._get_ctx(repo, revision) | |
713 | fctx = ctx.filectx(safe_bytes(path)) |
|
712 | fctx = ctx.filectx(safe_bytes(path)) | |
714 | return fctx.isbinary() |
|
713 | return fctx.isbinary() | |
715 |
|
714 | |||
716 | return _is_binary(repo_id, revision, path) |
|
715 | return _is_binary(repo_id, revision, path) | |
717 |
|
716 | |||
718 | @reraise_safe_exceptions |
|
717 | @reraise_safe_exceptions | |
719 | def md5_hash(self, wire, revision, path): |
|
718 | def md5_hash(self, wire, revision, path): | |
720 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
719 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
721 | region = self._region(wire) |
|
720 | region = self._region(wire) | |
722 |
|
721 | |||
723 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
722 | @region.conditional_cache_on_arguments(condition=cache_on) | |
724 | def _md5_hash(_repo_id, _sha, _path): |
|
723 | def _md5_hash(_repo_id, _sha, _path): | |
725 | repo = self._factory.repo(wire) |
|
724 | repo = self._factory.repo(wire) | |
726 | ctx = self._get_ctx(repo, revision) |
|
725 | ctx = self._get_ctx(repo, revision) | |
727 | fctx = ctx.filectx(safe_bytes(path)) |
|
726 | fctx = ctx.filectx(safe_bytes(path)) | |
728 | return hashlib.md5(fctx.data()).hexdigest() |
|
727 | return hashlib.md5(fctx.data()).hexdigest() | |
729 |
|
728 | |||
730 | return _md5_hash(repo_id, revision, path) |
|
729 | return _md5_hash(repo_id, revision, path) | |
731 |
|
730 | |||
732 | @reraise_safe_exceptions |
|
731 | @reraise_safe_exceptions | |
733 | def in_largefiles_store(self, wire, sha): |
|
732 | def in_largefiles_store(self, wire, sha): | |
734 | repo = self._factory.repo(wire) |
|
733 | repo = self._factory.repo(wire) | |
735 | return largefiles.lfutil.instore(repo, sha) |
|
734 | return largefiles.lfutil.instore(repo, sha) | |
736 |
|
735 | |||
737 | @reraise_safe_exceptions |
|
736 | @reraise_safe_exceptions | |
738 | def in_user_cache(self, wire, sha): |
|
737 | def in_user_cache(self, wire, sha): | |
739 | repo = self._factory.repo(wire) |
|
738 | repo = self._factory.repo(wire) | |
740 | return largefiles.lfutil.inusercache(repo.ui, sha) |
|
739 | return largefiles.lfutil.inusercache(repo.ui, sha) | |
741 |
|
740 | |||
742 | @reraise_safe_exceptions |
|
741 | @reraise_safe_exceptions | |
743 | def store_path(self, wire, sha): |
|
742 | def store_path(self, wire, sha): | |
744 | repo = self._factory.repo(wire) |
|
743 | repo = self._factory.repo(wire) | |
745 | return largefiles.lfutil.storepath(repo, sha) |
|
744 | return largefiles.lfutil.storepath(repo, sha) | |
746 |
|
745 | |||
747 | @reraise_safe_exceptions |
|
746 | @reraise_safe_exceptions | |
748 | def link(self, wire, sha, path): |
|
747 | def link(self, wire, sha, path): | |
749 | repo = self._factory.repo(wire) |
|
748 | repo = self._factory.repo(wire) | |
750 | largefiles.lfutil.link( |
|
749 | largefiles.lfutil.link( | |
751 | largefiles.lfutil.usercachepath(repo.ui, sha), path) |
|
750 | largefiles.lfutil.usercachepath(repo.ui, sha), path) | |
752 |
|
751 | |||
753 | @reraise_safe_exceptions |
|
752 | @reraise_safe_exceptions | |
754 | def localrepository(self, wire, create=False): |
|
753 | def localrepository(self, wire, create=False): | |
755 | self._factory.repo(wire, create=create) |
|
754 | self._factory.repo(wire, create=create) | |
756 |
|
755 | |||
757 | @reraise_safe_exceptions |
|
756 | @reraise_safe_exceptions | |
758 | def lookup(self, wire, revision, both): |
|
757 | def lookup(self, wire, revision, both): | |
759 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
758 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
760 | region = self._region(wire) |
|
759 | region = self._region(wire) | |
761 |
|
760 | |||
762 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
761 | @region.conditional_cache_on_arguments(condition=cache_on) | |
763 | def _lookup(_context_uid, _repo_id, _revision, _both): |
|
762 | def _lookup(_context_uid, _repo_id, _revision, _both): | |
764 | repo = self._factory.repo(wire) |
|
763 | repo = self._factory.repo(wire) | |
765 | rev = _revision |
|
764 | rev = _revision | |
766 | if isinstance(rev, int): |
|
765 | if isinstance(rev, int): | |
767 | # NOTE(marcink): |
|
766 | # NOTE(marcink): | |
768 | # since Mercurial doesn't support negative indexes properly |
|
767 | # since Mercurial doesn't support negative indexes properly | |
769 | # we need to shift accordingly by one to get proper index, e.g |
|
768 | # we need to shift accordingly by one to get proper index, e.g | |
770 | # repo[-1] => repo[-2] |
|
769 | # repo[-1] => repo[-2] | |
771 | # repo[0] => repo[-1] |
|
770 | # repo[0] => repo[-1] | |
772 | if rev <= 0: |
|
771 | if rev <= 0: | |
773 | rev = rev + -1 |
|
772 | rev = rev + -1 | |
774 | try: |
|
773 | try: | |
775 | ctx = self._get_ctx(repo, rev) |
|
774 | ctx = self._get_ctx(repo, rev) | |
776 | except AmbiguousPrefixLookupError: |
|
775 | except AmbiguousPrefixLookupError: | |
777 | e = RepoLookupError(rev) |
|
776 | e = RepoLookupError(rev) | |
778 | e._org_exc_tb = format_exc(sys.exc_info()) |
|
777 | e._org_exc_tb = format_exc(sys.exc_info()) | |
779 | raise exceptions.LookupException(e)(rev) |
|
778 | raise exceptions.LookupException(e)(rev) | |
780 | except (TypeError, RepoLookupError, binascii.Error) as e: |
|
779 | except (TypeError, RepoLookupError, binascii.Error) as e: | |
781 | e._org_exc_tb = format_exc(sys.exc_info()) |
|
780 | e._org_exc_tb = format_exc(sys.exc_info()) | |
782 | raise exceptions.LookupException(e)(rev) |
|
781 | raise exceptions.LookupException(e)(rev) | |
783 | except LookupError as e: |
|
782 | except LookupError as e: | |
784 | e._org_exc_tb = format_exc(sys.exc_info()) |
|
783 | e._org_exc_tb = format_exc(sys.exc_info()) | |
785 | raise exceptions.LookupException(e)(e.name) |
|
784 | raise exceptions.LookupException(e)(e.name) | |
786 |
|
785 | |||
787 | if not both: |
|
786 | if not both: | |
788 | return ctx.hex() |
|
787 | return ctx.hex() | |
789 |
|
788 | |||
790 | ctx = repo[ctx.hex()] |
|
789 | ctx = repo[ctx.hex()] | |
791 | return ctx.hex(), ctx.rev() |
|
790 | return ctx.hex(), ctx.rev() | |
792 |
|
791 | |||
793 | return _lookup(context_uid, repo_id, revision, both) |
|
792 | return _lookup(context_uid, repo_id, revision, both) | |
794 |
|
793 | |||
795 | @reraise_safe_exceptions |
|
794 | @reraise_safe_exceptions | |
796 | def sync_push(self, wire, url): |
|
795 | def sync_push(self, wire, url): | |
797 | if not self.check_url(url, wire['config']): |
|
796 | if not self.check_url(url, wire['config']): | |
798 | return |
|
797 | return | |
799 |
|
798 | |||
800 | repo = self._factory.repo(wire) |
|
799 | repo = self._factory.repo(wire) | |
801 |
|
800 | |||
802 | # Disable any prompts for this repo |
|
801 | # Disable any prompts for this repo | |
803 | repo.ui.setconfig(b'ui', b'interactive', b'off', b'-y') |
|
802 | repo.ui.setconfig(b'ui', b'interactive', b'off', b'-y') | |
804 |
|
803 | |||
805 | bookmarks = list(dict(repo._bookmarks).keys()) |
|
804 | bookmarks = list(dict(repo._bookmarks).keys()) | |
806 | remote = peer(repo, {}, safe_bytes(url)) |
|
805 | remote = peer(repo, {}, safe_bytes(url)) | |
807 | # Disable any prompts for this remote |
|
806 | # Disable any prompts for this remote | |
808 | remote.ui.setconfig(b'ui', b'interactive', b'off', b'-y') |
|
807 | remote.ui.setconfig(b'ui', b'interactive', b'off', b'-y') | |
809 |
|
808 | |||
810 | return exchange.push( |
|
809 | return exchange.push( | |
811 | repo, remote, newbranch=True, bookmarks=bookmarks).cgresult |
|
810 | repo, remote, newbranch=True, bookmarks=bookmarks).cgresult | |
812 |
|
811 | |||
813 | @reraise_safe_exceptions |
|
812 | @reraise_safe_exceptions | |
814 | def revision(self, wire, rev): |
|
813 | def revision(self, wire, rev): | |
815 | repo = self._factory.repo(wire) |
|
814 | repo = self._factory.repo(wire) | |
816 | ctx = self._get_ctx(repo, rev) |
|
815 | ctx = self._get_ctx(repo, rev) | |
817 | return ctx.rev() |
|
816 | return ctx.rev() | |
818 |
|
817 | |||
819 | @reraise_safe_exceptions |
|
818 | @reraise_safe_exceptions | |
820 | def rev_range(self, wire, commit_filter): |
|
819 | def rev_range(self, wire, commit_filter): | |
821 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
820 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
822 | region = self._region(wire) |
|
821 | region = self._region(wire) | |
823 |
|
822 | |||
824 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
823 | @region.conditional_cache_on_arguments(condition=cache_on) | |
825 | def _rev_range(_context_uid, _repo_id, _filter): |
|
824 | def _rev_range(_context_uid, _repo_id, _filter): | |
826 | repo = self._factory.repo(wire) |
|
825 | repo = self._factory.repo(wire) | |
827 | revisions = [ |
|
826 | revisions = [ | |
828 | ascii_str(repo[rev].hex()) |
|
827 | ascii_str(repo[rev].hex()) | |
829 | for rev in revrange(repo, list(map(ascii_bytes, commit_filter))) |
|
828 | for rev in revrange(repo, list(map(ascii_bytes, commit_filter))) | |
830 | ] |
|
829 | ] | |
831 | return revisions |
|
830 | return revisions | |
832 |
|
831 | |||
833 | return _rev_range(context_uid, repo_id, sorted(commit_filter)) |
|
832 | return _rev_range(context_uid, repo_id, sorted(commit_filter)) | |
834 |
|
833 | |||
835 | @reraise_safe_exceptions |
|
834 | @reraise_safe_exceptions | |
836 | def rev_range_hash(self, wire, node): |
|
835 | def rev_range_hash(self, wire, node): | |
837 | repo = self._factory.repo(wire) |
|
836 | repo = self._factory.repo(wire) | |
838 |
|
837 | |||
839 | def get_revs(repo, rev_opt): |
|
838 | def get_revs(repo, rev_opt): | |
840 | if rev_opt: |
|
839 | if rev_opt: | |
841 | revs = revrange(repo, rev_opt) |
|
840 | revs = revrange(repo, rev_opt) | |
842 | if len(revs) == 0: |
|
841 | if len(revs) == 0: | |
843 | return (nullrev, nullrev) |
|
842 | return (nullrev, nullrev) | |
844 | return max(revs), min(revs) |
|
843 | return max(revs), min(revs) | |
845 | else: |
|
844 | else: | |
846 | return len(repo) - 1, 0 |
|
845 | return len(repo) - 1, 0 | |
847 |
|
846 | |||
848 | stop, start = get_revs(repo, [node + ':']) |
|
847 | stop, start = get_revs(repo, [node + ':']) | |
849 | revs = [ascii_str(repo[r].hex()) for r in range(start, stop + 1)] |
|
848 | revs = [ascii_str(repo[r].hex()) for r in range(start, stop + 1)] | |
850 | return revs |
|
849 | return revs | |
851 |
|
850 | |||
852 | @reraise_safe_exceptions |
|
851 | @reraise_safe_exceptions | |
853 | def revs_from_revspec(self, wire, rev_spec, *args, **kwargs): |
|
852 | def revs_from_revspec(self, wire, rev_spec, *args, **kwargs): | |
854 | org_path = safe_bytes(wire["path"]) |
|
853 | org_path = safe_bytes(wire["path"]) | |
855 | other_path = safe_bytes(kwargs.pop('other_path', '')) |
|
854 | other_path = safe_bytes(kwargs.pop('other_path', '')) | |
856 |
|
855 | |||
857 | # case when we want to compare two independent repositories |
|
856 | # case when we want to compare two independent repositories | |
858 | if other_path and other_path != wire["path"]: |
|
857 | if other_path and other_path != wire["path"]: | |
859 | baseui = self._factory._create_config(wire["config"]) |
|
858 | baseui = self._factory._create_config(wire["config"]) | |
860 | repo = unionrepo.makeunionrepository(baseui, other_path, org_path) |
|
859 | repo = unionrepo.makeunionrepository(baseui, other_path, org_path) | |
861 | else: |
|
860 | else: | |
862 | repo = self._factory.repo(wire) |
|
861 | repo = self._factory.repo(wire) | |
863 | return list(repo.revs(rev_spec, *args)) |
|
862 | return list(repo.revs(rev_spec, *args)) | |
864 |
|
863 | |||
865 | @reraise_safe_exceptions |
|
864 | @reraise_safe_exceptions | |
866 | def verify(self, wire,): |
|
865 | def verify(self, wire,): | |
867 | repo = self._factory.repo(wire) |
|
866 | repo = self._factory.repo(wire) | |
868 | baseui = self._factory._create_config(wire['config']) |
|
867 | baseui = self._factory._create_config(wire['config']) | |
869 |
|
868 | |||
870 | baseui, output = patch_ui_message_output(baseui) |
|
869 | baseui, output = patch_ui_message_output(baseui) | |
871 |
|
870 | |||
872 | repo.ui = baseui |
|
871 | repo.ui = baseui | |
873 | verify.verify(repo) |
|
872 | verify.verify(repo) | |
874 | return output.getvalue() |
|
873 | return output.getvalue() | |
875 |
|
874 | |||
876 | @reraise_safe_exceptions |
|
875 | @reraise_safe_exceptions | |
877 | def hg_update_cache(self, wire,): |
|
876 | def hg_update_cache(self, wire,): | |
878 | repo = self._factory.repo(wire) |
|
877 | repo = self._factory.repo(wire) | |
879 | baseui = self._factory._create_config(wire['config']) |
|
878 | baseui = self._factory._create_config(wire['config']) | |
880 | baseui, output = patch_ui_message_output(baseui) |
|
879 | baseui, output = patch_ui_message_output(baseui) | |
881 |
|
880 | |||
882 | repo.ui = baseui |
|
881 | repo.ui = baseui | |
883 | with repo.wlock(), repo.lock(): |
|
882 | with repo.wlock(), repo.lock(): | |
884 | repo.updatecaches(full=True) |
|
883 | repo.updatecaches(full=True) | |
885 |
|
884 | |||
886 | return output.getvalue() |
|
885 | return output.getvalue() | |
887 |
|
886 | |||
888 | @reraise_safe_exceptions |
|
887 | @reraise_safe_exceptions | |
889 | def hg_rebuild_fn_cache(self, wire,): |
|
888 | def hg_rebuild_fn_cache(self, wire,): | |
890 | repo = self._factory.repo(wire) |
|
889 | repo = self._factory.repo(wire) | |
891 | baseui = self._factory._create_config(wire['config']) |
|
890 | baseui = self._factory._create_config(wire['config']) | |
892 | baseui, output = patch_ui_message_output(baseui) |
|
891 | baseui, output = patch_ui_message_output(baseui) | |
893 |
|
892 | |||
894 | repo.ui = baseui |
|
893 | repo.ui = baseui | |
895 |
|
894 | |||
896 | repair.rebuildfncache(baseui, repo) |
|
895 | repair.rebuildfncache(baseui, repo) | |
897 |
|
896 | |||
898 | return output.getvalue() |
|
897 | return output.getvalue() | |
899 |
|
898 | |||
900 | @reraise_safe_exceptions |
|
899 | @reraise_safe_exceptions | |
901 | def tags(self, wire): |
|
900 | def tags(self, wire): | |
902 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
901 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
903 | region = self._region(wire) |
|
902 | region = self._region(wire) | |
904 |
|
903 | |||
905 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
904 | @region.conditional_cache_on_arguments(condition=cache_on) | |
906 | def _tags(_context_uid, _repo_id): |
|
905 | def _tags(_context_uid, _repo_id): | |
907 | repo = self._factory.repo(wire) |
|
906 | repo = self._factory.repo(wire) | |
908 | return {safe_str(name): ascii_str(hex(sha)) for name, sha in repo.tags().items()} |
|
907 | return {safe_str(name): ascii_str(hex(sha)) for name, sha in repo.tags().items()} | |
909 |
|
908 | |||
910 | return _tags(context_uid, repo_id) |
|
909 | return _tags(context_uid, repo_id) | |
911 |
|
910 | |||
912 | @reraise_safe_exceptions |
|
911 | @reraise_safe_exceptions | |
913 | def update(self, wire, node='', clean=False): |
|
912 | def update(self, wire, node='', clean=False): | |
914 | repo = self._factory.repo(wire) |
|
913 | repo = self._factory.repo(wire) | |
915 | baseui = self._factory._create_config(wire['config']) |
|
914 | baseui = self._factory._create_config(wire['config']) | |
916 | node = safe_bytes(node) |
|
915 | node = safe_bytes(node) | |
917 |
|
916 | |||
918 | commands.update(baseui, repo, node=node, clean=clean) |
|
917 | commands.update(baseui, repo, node=node, clean=clean) | |
919 |
|
918 | |||
920 | @reraise_safe_exceptions |
|
919 | @reraise_safe_exceptions | |
921 | def identify(self, wire): |
|
920 | def identify(self, wire): | |
922 | repo = self._factory.repo(wire) |
|
921 | repo = self._factory.repo(wire) | |
923 | baseui = self._factory._create_config(wire['config']) |
|
922 | baseui = self._factory._create_config(wire['config']) | |
924 | output = io.BytesIO() |
|
923 | output = io.BytesIO() | |
925 | baseui.write = output.write |
|
924 | baseui.write = output.write | |
926 | # This is required to get a full node id |
|
925 | # This is required to get a full node id | |
927 | baseui.debugflag = True |
|
926 | baseui.debugflag = True | |
928 | commands.identify(baseui, repo, id=True) |
|
927 | commands.identify(baseui, repo, id=True) | |
929 |
|
928 | |||
930 | return output.getvalue() |
|
929 | return output.getvalue() | |
931 |
|
930 | |||
932 | @reraise_safe_exceptions |
|
931 | @reraise_safe_exceptions | |
933 | def heads(self, wire, branch=None): |
|
932 | def heads(self, wire, branch=None): | |
934 | repo = self._factory.repo(wire) |
|
933 | repo = self._factory.repo(wire) | |
935 | baseui = self._factory._create_config(wire['config']) |
|
934 | baseui = self._factory._create_config(wire['config']) | |
936 | output = io.BytesIO() |
|
935 | output = io.BytesIO() | |
937 |
|
936 | |||
938 | def write(data, **unused_kwargs): |
|
937 | def write(data, **unused_kwargs): | |
939 | output.write(data) |
|
938 | output.write(data) | |
940 |
|
939 | |||
941 | baseui.write = write |
|
940 | baseui.write = write | |
942 | if branch: |
|
941 | if branch: | |
943 | args = [safe_bytes(branch)] |
|
942 | args = [safe_bytes(branch)] | |
944 | else: |
|
943 | else: | |
945 | args = [] |
|
944 | args = [] | |
946 | commands.heads(baseui, repo, template=b'{node} ', *args) |
|
945 | commands.heads(baseui, repo, template=b'{node} ', *args) | |
947 |
|
946 | |||
948 | return output.getvalue() |
|
947 | return output.getvalue() | |
949 |
|
948 | |||
950 | @reraise_safe_exceptions |
|
949 | @reraise_safe_exceptions | |
951 | def ancestor(self, wire, revision1, revision2): |
|
950 | def ancestor(self, wire, revision1, revision2): | |
952 | repo = self._factory.repo(wire) |
|
951 | repo = self._factory.repo(wire) | |
953 | changelog = repo.changelog |
|
952 | changelog = repo.changelog | |
954 | lookup = repo.lookup |
|
953 | lookup = repo.lookup | |
955 | a = changelog.ancestor(lookup(safe_bytes(revision1)), lookup(safe_bytes(revision2))) |
|
954 | a = changelog.ancestor(lookup(safe_bytes(revision1)), lookup(safe_bytes(revision2))) | |
956 | return hex(a) |
|
955 | return hex(a) | |
957 |
|
956 | |||
958 | @reraise_safe_exceptions |
|
957 | @reraise_safe_exceptions | |
959 | def clone(self, wire, source, dest, update_after_clone=False, hooks=True): |
|
958 | def clone(self, wire, source, dest, update_after_clone=False, hooks=True): | |
960 | baseui = self._factory._create_config(wire["config"], hooks=hooks) |
|
959 | baseui = self._factory._create_config(wire["config"], hooks=hooks) | |
961 | clone(baseui, safe_bytes(source), safe_bytes(dest), noupdate=not update_after_clone) |
|
960 | clone(baseui, safe_bytes(source), safe_bytes(dest), noupdate=not update_after_clone) | |
962 |
|
961 | |||
963 | @reraise_safe_exceptions |
|
962 | @reraise_safe_exceptions | |
964 | def commitctx(self, wire, message, parents, commit_time, commit_timezone, user, files, extra, removed, updated): |
|
963 | def commitctx(self, wire, message, parents, commit_time, commit_timezone, user, files, extra, removed, updated): | |
965 |
|
964 | |||
966 | repo = self._factory.repo(wire) |
|
965 | repo = self._factory.repo(wire) | |
967 | baseui = self._factory._create_config(wire['config']) |
|
966 | baseui = self._factory._create_config(wire['config']) | |
968 | publishing = baseui.configbool(b'phases', b'publish') |
|
967 | publishing = baseui.configbool(b'phases', b'publish') | |
969 |
|
968 | |||
970 | def _filectxfn(_repo, ctx, path: bytes): |
|
969 | def _filectxfn(_repo, ctx, path: bytes): | |
971 | """ |
|
970 | """ | |
972 | Marks given path as added/changed/removed in a given _repo. This is |
|
971 | Marks given path as added/changed/removed in a given _repo. This is | |
973 | for internal mercurial commit function. |
|
972 | for internal mercurial commit function. | |
974 | """ |
|
973 | """ | |
975 |
|
974 | |||
976 | # check if this path is removed |
|
975 | # check if this path is removed | |
977 | if safe_str(path) in removed: |
|
976 | if safe_str(path) in removed: | |
978 | # returning None is a way to mark node for removal |
|
977 | # returning None is a way to mark node for removal | |
979 | return None |
|
978 | return None | |
980 |
|
979 | |||
981 | # check if this path is added |
|
980 | # check if this path is added | |
982 | for node in updated: |
|
981 | for node in updated: | |
983 | if safe_bytes(node['path']) == path: |
|
982 | if safe_bytes(node['path']) == path: | |
984 | return memfilectx( |
|
983 | return memfilectx( | |
985 | _repo, |
|
984 | _repo, | |
986 | changectx=ctx, |
|
985 | changectx=ctx, | |
987 | path=safe_bytes(node['path']), |
|
986 | path=safe_bytes(node['path']), | |
988 | data=safe_bytes(node['content']), |
|
987 | data=safe_bytes(node['content']), | |
989 | islink=False, |
|
988 | islink=False, | |
990 | isexec=bool(node['mode'] & stat.S_IXUSR), |
|
989 | isexec=bool(node['mode'] & stat.S_IXUSR), | |
991 | copysource=False) |
|
990 | copysource=False) | |
992 | abort_exc = exceptions.AbortException() |
|
991 | abort_exc = exceptions.AbortException() | |
993 | raise abort_exc(f"Given path haven't been marked as added, changed or removed ({path})") |
|
992 | raise abort_exc(f"Given path haven't been marked as added, changed or removed ({path})") | |
994 |
|
993 | |||
995 | if publishing: |
|
994 | if publishing: | |
996 | new_commit_phase = b'public' |
|
995 | new_commit_phase = b'public' | |
997 | else: |
|
996 | else: | |
998 | new_commit_phase = b'draft' |
|
997 | new_commit_phase = b'draft' | |
999 | with repo.ui.configoverride({(b'phases', b'new-commit'): new_commit_phase}): |
|
998 | with repo.ui.configoverride({(b'phases', b'new-commit'): new_commit_phase}): | |
1000 | kwargs = {safe_bytes(k): safe_bytes(v) for k, v in extra.items()} |
|
999 | kwargs = {safe_bytes(k): safe_bytes(v) for k, v in extra.items()} | |
1001 | commit_ctx = memctx( |
|
1000 | commit_ctx = memctx( | |
1002 | repo=repo, |
|
1001 | repo=repo, | |
1003 | parents=parents, |
|
1002 | parents=parents, | |
1004 | text=safe_bytes(message), |
|
1003 | text=safe_bytes(message), | |
1005 | files=[safe_bytes(x) for x in files], |
|
1004 | files=[safe_bytes(x) for x in files], | |
1006 | filectxfn=_filectxfn, |
|
1005 | filectxfn=_filectxfn, | |
1007 | user=safe_bytes(user), |
|
1006 | user=safe_bytes(user), | |
1008 | date=(commit_time, commit_timezone), |
|
1007 | date=(commit_time, commit_timezone), | |
1009 | extra=kwargs) |
|
1008 | extra=kwargs) | |
1010 |
|
1009 | |||
1011 | n = repo.commitctx(commit_ctx) |
|
1010 | n = repo.commitctx(commit_ctx) | |
1012 | new_id = hex(n) |
|
1011 | new_id = hex(n) | |
1013 |
|
1012 | |||
1014 | return new_id |
|
1013 | return new_id | |
1015 |
|
1014 | |||
1016 | @reraise_safe_exceptions |
|
1015 | @reraise_safe_exceptions | |
1017 | def pull(self, wire, url, commit_ids=None): |
|
1016 | def pull(self, wire, url, commit_ids=None): | |
1018 | repo = self._factory.repo(wire) |
|
1017 | repo = self._factory.repo(wire) | |
1019 | # Disable any prompts for this repo |
|
1018 | # Disable any prompts for this repo | |
1020 | repo.ui.setconfig(b'ui', b'interactive', b'off', b'-y') |
|
1019 | repo.ui.setconfig(b'ui', b'interactive', b'off', b'-y') | |
1021 |
|
1020 | |||
1022 | remote = peer(repo, {}, safe_bytes(url)) |
|
1021 | remote = peer(repo, {}, safe_bytes(url)) | |
1023 | # Disable any prompts for this remote |
|
1022 | # Disable any prompts for this remote | |
1024 | remote.ui.setconfig(b'ui', b'interactive', b'off', b'-y') |
|
1023 | remote.ui.setconfig(b'ui', b'interactive', b'off', b'-y') | |
1025 |
|
1024 | |||
1026 | if commit_ids: |
|
1025 | if commit_ids: | |
1027 | commit_ids = [bin(commit_id) for commit_id in commit_ids] |
|
1026 | commit_ids = [bin(commit_id) for commit_id in commit_ids] | |
1028 |
|
1027 | |||
1029 | return exchange.pull( |
|
1028 | return exchange.pull( | |
1030 | repo, remote, heads=commit_ids, force=None).cgresult |
|
1029 | repo, remote, heads=commit_ids, force=None).cgresult | |
1031 |
|
1030 | |||
1032 | @reraise_safe_exceptions |
|
1031 | @reraise_safe_exceptions | |
1033 | def pull_cmd(self, wire, source, bookmark='', branch='', revision='', hooks=True): |
|
1032 | def pull_cmd(self, wire, source, bookmark='', branch='', revision='', hooks=True): | |
1034 | repo = self._factory.repo(wire) |
|
1033 | repo = self._factory.repo(wire) | |
1035 | baseui = self._factory._create_config(wire['config'], hooks=hooks) |
|
1034 | baseui = self._factory._create_config(wire['config'], hooks=hooks) | |
1036 |
|
1035 | |||
1037 | source = safe_bytes(source) |
|
1036 | source = safe_bytes(source) | |
1038 |
|
1037 | |||
1039 | # Mercurial internally has a lot of logic that checks ONLY if |
|
1038 | # Mercurial internally has a lot of logic that checks ONLY if | |
1040 | # option is defined, we just pass those if they are defined then |
|
1039 | # option is defined, we just pass those if they are defined then | |
1041 | opts = {} |
|
1040 | opts = {} | |
1042 |
|
1041 | |||
1043 | if bookmark: |
|
1042 | if bookmark: | |
1044 | opts['bookmark'] = [safe_bytes(x) for x in bookmark] \ |
|
1043 | opts['bookmark'] = [safe_bytes(x) for x in bookmark] \ | |
1045 | if isinstance(bookmark, list) else safe_bytes(bookmark) |
|
1044 | if isinstance(bookmark, list) else safe_bytes(bookmark) | |
1046 |
|
1045 | |||
1047 | if branch: |
|
1046 | if branch: | |
1048 | opts['branch'] = [safe_bytes(x) for x in branch] \ |
|
1047 | opts['branch'] = [safe_bytes(x) for x in branch] \ | |
1049 | if isinstance(branch, list) else safe_bytes(branch) |
|
1048 | if isinstance(branch, list) else safe_bytes(branch) | |
1050 |
|
1049 | |||
1051 | if revision: |
|
1050 | if revision: | |
1052 | opts['rev'] = [safe_bytes(x) for x in revision] \ |
|
1051 | opts['rev'] = [safe_bytes(x) for x in revision] \ | |
1053 | if isinstance(revision, list) else safe_bytes(revision) |
|
1052 | if isinstance(revision, list) else safe_bytes(revision) | |
1054 |
|
1053 | |||
1055 | commands.pull(baseui, repo, source, **opts) |
|
1054 | commands.pull(baseui, repo, source, **opts) | |
1056 |
|
1055 | |||
1057 | @reraise_safe_exceptions |
|
1056 | @reraise_safe_exceptions | |
1058 | def push(self, wire, revisions, dest_path, hooks: bool = True, push_branches: bool = False): |
|
1057 | def push(self, wire, revisions, dest_path, hooks: bool = True, push_branches: bool = False): | |
1059 | repo = self._factory.repo(wire) |
|
1058 | repo = self._factory.repo(wire) | |
1060 | baseui = self._factory._create_config(wire['config'], hooks=hooks) |
|
1059 | baseui = self._factory._create_config(wire['config'], hooks=hooks) | |
1061 |
|
1060 | |||
1062 | revisions = [safe_bytes(x) for x in revisions] \ |
|
1061 | revisions = [safe_bytes(x) for x in revisions] \ | |
1063 | if isinstance(revisions, list) else safe_bytes(revisions) |
|
1062 | if isinstance(revisions, list) else safe_bytes(revisions) | |
1064 |
|
1063 | |||
1065 | commands.push(baseui, repo, safe_bytes(dest_path), |
|
1064 | commands.push(baseui, repo, safe_bytes(dest_path), | |
1066 | rev=revisions, |
|
1065 | rev=revisions, | |
1067 | new_branch=push_branches) |
|
1066 | new_branch=push_branches) | |
1068 |
|
1067 | |||
1069 | @reraise_safe_exceptions |
|
1068 | @reraise_safe_exceptions | |
1070 | def strip(self, wire, revision, update, backup): |
|
1069 | def strip(self, wire, revision, update, backup): | |
1071 | repo = self._factory.repo(wire) |
|
1070 | repo = self._factory.repo(wire) | |
1072 | ctx = self._get_ctx(repo, revision) |
|
1071 | ctx = self._get_ctx(repo, revision) | |
1073 | hgext_strip.strip( |
|
1072 | hgext_strip.strip( | |
1074 | repo.baseui, repo, ctx.node(), update=update, backup=backup) |
|
1073 | repo.baseui, repo, ctx.node(), update=update, backup=backup) | |
1075 |
|
1074 | |||
1076 | @reraise_safe_exceptions |
|
1075 | @reraise_safe_exceptions | |
1077 | def get_unresolved_files(self, wire): |
|
1076 | def get_unresolved_files(self, wire): | |
1078 | repo = self._factory.repo(wire) |
|
1077 | repo = self._factory.repo(wire) | |
1079 |
|
1078 | |||
1080 | log.debug('Calculating unresolved files for repo: %s', repo) |
|
1079 | log.debug('Calculating unresolved files for repo: %s', repo) | |
1081 | output = io.BytesIO() |
|
1080 | output = io.BytesIO() | |
1082 |
|
1081 | |||
1083 | def write(data, **unused_kwargs): |
|
1082 | def write(data, **unused_kwargs): | |
1084 | output.write(data) |
|
1083 | output.write(data) | |
1085 |
|
1084 | |||
1086 | baseui = self._factory._create_config(wire['config']) |
|
1085 | baseui = self._factory._create_config(wire['config']) | |
1087 | baseui.write = write |
|
1086 | baseui.write = write | |
1088 |
|
1087 | |||
1089 | commands.resolve(baseui, repo, list=True) |
|
1088 | commands.resolve(baseui, repo, list=True) | |
1090 | unresolved = output.getvalue().splitlines(0) |
|
1089 | unresolved = output.getvalue().splitlines(0) | |
1091 | return unresolved |
|
1090 | return unresolved | |
1092 |
|
1091 | |||
1093 | @reraise_safe_exceptions |
|
1092 | @reraise_safe_exceptions | |
1094 | def merge(self, wire, revision): |
|
1093 | def merge(self, wire, revision): | |
1095 | repo = self._factory.repo(wire) |
|
1094 | repo = self._factory.repo(wire) | |
1096 | baseui = self._factory._create_config(wire['config']) |
|
1095 | baseui = self._factory._create_config(wire['config']) | |
1097 | repo.ui.setconfig(b'ui', b'merge', b'internal:dump') |
|
1096 | repo.ui.setconfig(b'ui', b'merge', b'internal:dump') | |
1098 |
|
1097 | |||
1099 | # In case of sub repositories are used mercurial prompts the user in |
|
1098 | # In case of sub repositories are used mercurial prompts the user in | |
1100 | # case of merge conflicts or different sub repository sources. By |
|
1099 | # case of merge conflicts or different sub repository sources. By | |
1101 | # setting the interactive flag to `False` mercurial doesn't prompt the |
|
1100 | # setting the interactive flag to `False` mercurial doesn't prompt the | |
1102 | # used but instead uses a default value. |
|
1101 | # used but instead uses a default value. | |
1103 | repo.ui.setconfig(b'ui', b'interactive', False) |
|
1102 | repo.ui.setconfig(b'ui', b'interactive', False) | |
1104 | commands.merge(baseui, repo, rev=safe_bytes(revision)) |
|
1103 | commands.merge(baseui, repo, rev=safe_bytes(revision)) | |
1105 |
|
1104 | |||
1106 | @reraise_safe_exceptions |
|
1105 | @reraise_safe_exceptions | |
1107 | def merge_state(self, wire): |
|
1106 | def merge_state(self, wire): | |
1108 | repo = self._factory.repo(wire) |
|
1107 | repo = self._factory.repo(wire) | |
1109 | repo.ui.setconfig(b'ui', b'merge', b'internal:dump') |
|
1108 | repo.ui.setconfig(b'ui', b'merge', b'internal:dump') | |
1110 |
|
1109 | |||
1111 | # In case of sub repositories are used mercurial prompts the user in |
|
1110 | # In case of sub repositories are used mercurial prompts the user in | |
1112 | # case of merge conflicts or different sub repository sources. By |
|
1111 | # case of merge conflicts or different sub repository sources. By | |
1113 | # setting the interactive flag to `False` mercurial doesn't prompt the |
|
1112 | # setting the interactive flag to `False` mercurial doesn't prompt the | |
1114 | # used but instead uses a default value. |
|
1113 | # used but instead uses a default value. | |
1115 | repo.ui.setconfig(b'ui', b'interactive', False) |
|
1114 | repo.ui.setconfig(b'ui', b'interactive', False) | |
1116 | ms = hg_merge.mergestate(repo) |
|
1115 | ms = hg_merge.mergestate(repo) | |
1117 | return [x for x in ms.unresolved()] |
|
1116 | return [x for x in ms.unresolved()] | |
1118 |
|
1117 | |||
1119 | @reraise_safe_exceptions |
|
1118 | @reraise_safe_exceptions | |
1120 | def commit(self, wire, message, username, close_branch=False): |
|
1119 | def commit(self, wire, message, username, close_branch=False): | |
1121 | repo = self._factory.repo(wire) |
|
1120 | repo = self._factory.repo(wire) | |
1122 | baseui = self._factory._create_config(wire['config']) |
|
1121 | baseui = self._factory._create_config(wire['config']) | |
1123 | repo.ui.setconfig(b'ui', b'username', safe_bytes(username)) |
|
1122 | repo.ui.setconfig(b'ui', b'username', safe_bytes(username)) | |
1124 | commands.commit(baseui, repo, message=safe_bytes(message), close_branch=close_branch) |
|
1123 | commands.commit(baseui, repo, message=safe_bytes(message), close_branch=close_branch) | |
1125 |
|
1124 | |||
1126 | @reraise_safe_exceptions |
|
1125 | @reraise_safe_exceptions | |
1127 | def rebase(self, wire, source='', dest='', abort=False): |
|
1126 | def rebase(self, wire, source='', dest='', abort=False): | |
1128 | repo = self._factory.repo(wire) |
|
1127 | repo = self._factory.repo(wire) | |
1129 | baseui = self._factory._create_config(wire['config']) |
|
1128 | baseui = self._factory._create_config(wire['config']) | |
1130 | repo.ui.setconfig(b'ui', b'merge', b'internal:dump') |
|
1129 | repo.ui.setconfig(b'ui', b'merge', b'internal:dump') | |
1131 | # In case of sub repositories are used mercurial prompts the user in |
|
1130 | # In case of sub repositories are used mercurial prompts the user in | |
1132 | # case of merge conflicts or different sub repository sources. By |
|
1131 | # case of merge conflicts or different sub repository sources. By | |
1133 | # setting the interactive flag to `False` mercurial doesn't prompt the |
|
1132 | # setting the interactive flag to `False` mercurial doesn't prompt the | |
1134 | # used but instead uses a default value. |
|
1133 | # used but instead uses a default value. | |
1135 | repo.ui.setconfig(b'ui', b'interactive', False) |
|
1134 | repo.ui.setconfig(b'ui', b'interactive', False) | |
1136 |
|
1135 | |||
1137 | rebase.rebase(baseui, repo, base=safe_bytes(source or ''), dest=safe_bytes(dest or ''), |
|
1136 | rebase.rebase(baseui, repo, base=safe_bytes(source or ''), dest=safe_bytes(dest or ''), | |
1138 | abort=abort, keep=not abort) |
|
1137 | abort=abort, keep=not abort) | |
1139 |
|
1138 | |||
1140 | @reraise_safe_exceptions |
|
1139 | @reraise_safe_exceptions | |
1141 | def tag(self, wire, name, revision, message, local, user, tag_time, tag_timezone): |
|
1140 | def tag(self, wire, name, revision, message, local, user, tag_time, tag_timezone): | |
1142 | repo = self._factory.repo(wire) |
|
1141 | repo = self._factory.repo(wire) | |
1143 | ctx = self._get_ctx(repo, revision) |
|
1142 | ctx = self._get_ctx(repo, revision) | |
1144 | node = ctx.node() |
|
1143 | node = ctx.node() | |
1145 |
|
1144 | |||
1146 | date = (tag_time, tag_timezone) |
|
1145 | date = (tag_time, tag_timezone) | |
1147 | try: |
|
1146 | try: | |
1148 | hg_tag.tag(repo, safe_bytes(name), node, safe_bytes(message), local, safe_bytes(user), date) |
|
1147 | hg_tag.tag(repo, safe_bytes(name), node, safe_bytes(message), local, safe_bytes(user), date) | |
1149 | except Abort as e: |
|
1148 | except Abort as e: | |
1150 | log.exception("Tag operation aborted") |
|
1149 | log.exception("Tag operation aborted") | |
1151 | # Exception can contain unicode which we convert |
|
1150 | # Exception can contain unicode which we convert | |
1152 | raise exceptions.AbortException(e)(repr(e)) |
|
1151 | raise exceptions.AbortException(e)(repr(e)) | |
1153 |
|
1152 | |||
1154 | @reraise_safe_exceptions |
|
1153 | @reraise_safe_exceptions | |
1155 | def bookmark(self, wire, bookmark, revision=''): |
|
1154 | def bookmark(self, wire, bookmark, revision=''): | |
1156 | repo = self._factory.repo(wire) |
|
1155 | repo = self._factory.repo(wire) | |
1157 | baseui = self._factory._create_config(wire['config']) |
|
1156 | baseui = self._factory._create_config(wire['config']) | |
1158 | revision = revision or '' |
|
1157 | revision = revision or '' | |
1159 | commands.bookmark(baseui, repo, safe_bytes(bookmark), rev=safe_bytes(revision), force=True) |
|
1158 | commands.bookmark(baseui, repo, safe_bytes(bookmark), rev=safe_bytes(revision), force=True) | |
1160 |
|
1159 | |||
1161 | @reraise_safe_exceptions |
|
1160 | @reraise_safe_exceptions | |
1162 | def install_hooks(self, wire, force=False): |
|
1161 | def install_hooks(self, wire, force=False): | |
1163 | # we don't need any special hooks for Mercurial |
|
1162 | # we don't need any special hooks for Mercurial | |
1164 | pass |
|
1163 | pass | |
1165 |
|
1164 | |||
1166 | @reraise_safe_exceptions |
|
1165 | @reraise_safe_exceptions | |
1167 | def get_hooks_info(self, wire): |
|
1166 | def get_hooks_info(self, wire): | |
1168 | return { |
|
1167 | return { | |
1169 | 'pre_version': vcsserver.__version__, |
|
1168 | 'pre_version': vcsserver.__version__, | |
1170 | 'post_version': vcsserver.__version__, |
|
1169 | 'post_version': vcsserver.__version__, | |
1171 | } |
|
1170 | } | |
1172 |
|
1171 | |||
1173 | @reraise_safe_exceptions |
|
1172 | @reraise_safe_exceptions | |
1174 | def set_head_ref(self, wire, head_name): |
|
1173 | def set_head_ref(self, wire, head_name): | |
1175 | pass |
|
1174 | pass | |
1176 |
|
1175 | |||
1177 | @reraise_safe_exceptions |
|
1176 | @reraise_safe_exceptions | |
1178 | def archive_repo(self, wire, archive_name_key, kind, mtime, archive_at_path, |
|
1177 | def archive_repo(self, wire, archive_name_key, kind, mtime, archive_at_path, | |
1179 | archive_dir_name, commit_id, cache_config): |
|
1178 | archive_dir_name, commit_id, cache_config): | |
1180 |
|
1179 | |||
1181 | def file_walker(_commit_id, path): |
|
1180 | def file_walker(_commit_id, path): | |
1182 | repo = self._factory.repo(wire) |
|
1181 | repo = self._factory.repo(wire) | |
1183 | ctx = repo[_commit_id] |
|
1182 | ctx = repo[_commit_id] | |
1184 | is_root = path in ['', '/'] |
|
1183 | is_root = path in ['', '/'] | |
1185 | if is_root: |
|
1184 | if is_root: | |
1186 | matcher = alwaysmatcher(badfn=None) |
|
1185 | matcher = alwaysmatcher(badfn=None) | |
1187 | else: |
|
1186 | else: | |
1188 | matcher = patternmatcher('', [(b'glob', safe_bytes(path)+b'/**', b'')], badfn=None) |
|
1187 | matcher = patternmatcher('', [(b'glob', safe_bytes(path)+b'/**', b'')], badfn=None) | |
1189 | file_iter = ctx.manifest().walk(matcher) |
|
1188 | file_iter = ctx.manifest().walk(matcher) | |
1190 |
|
1189 | |||
1191 | for fn in file_iter: |
|
1190 | for fn in file_iter: | |
1192 | file_path = fn |
|
1191 | file_path = fn | |
1193 | flags = ctx.flags(fn) |
|
1192 | flags = ctx.flags(fn) | |
1194 | mode = b'x' in flags and 0o755 or 0o644 |
|
1193 | mode = b'x' in flags and 0o755 or 0o644 | |
1195 | is_link = b'l' in flags |
|
1194 | is_link = b'l' in flags | |
1196 |
|
1195 | |||
1197 | yield ArchiveNode(file_path, mode, is_link, ctx[fn].data) |
|
1196 | yield ArchiveNode(file_path, mode, is_link, ctx[fn].data) | |
1198 |
|
1197 | |||
1199 | return store_archive_in_cache( |
|
1198 | return store_archive_in_cache( | |
1200 | file_walker, archive_name_key, kind, mtime, archive_at_path, archive_dir_name, commit_id, cache_config=cache_config) |
|
1199 | file_walker, archive_name_key, kind, mtime, archive_at_path, archive_dir_name, commit_id, cache_config=cache_config) | |
1201 |
|
1200 |
@@ -1,943 +1,942 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 |
|
18 | |||
19 | import os |
|
19 | import os | |
20 | import subprocess |
|
20 | import subprocess | |
21 | from urllib.error import URLError |
|
21 | from urllib.error import URLError | |
22 | import urllib.parse |
|
22 | import urllib.parse | |
23 | import logging |
|
23 | import logging | |
24 | import posixpath as vcspath |
|
24 | import posixpath as vcspath | |
25 | import io |
|
25 | import io | |
26 | import urllib.request |
|
26 | import urllib.request | |
27 | import urllib.parse |
|
27 | import urllib.parse | |
28 | import urllib.error |
|
28 | import urllib.error | |
29 | import traceback |
|
29 | import traceback | |
30 |
|
30 | |||
31 |
|
31 | |||
32 | import svn.client # noqa |
|
32 | import svn.client # noqa | |
33 | import svn.core # noqa |
|
33 | import svn.core # noqa | |
34 | import svn.delta # noqa |
|
34 | import svn.delta # noqa | |
35 | import svn.diff # noqa |
|
35 | import svn.diff # noqa | |
36 | import svn.fs # noqa |
|
36 | import svn.fs # noqa | |
37 | import svn.repos # noqa |
|
37 | import svn.repos # noqa | |
38 |
|
38 | |||
39 | from vcsserver import svn_diff, exceptions, subprocessio, settings |
|
39 | from vcsserver import svn_diff, exceptions, subprocessio, settings | |
40 | from vcsserver.base import ( |
|
40 | from vcsserver.base import ( | |
41 | RepoFactory, |
|
41 | RepoFactory, | |
42 | raise_from_original, |
|
42 | raise_from_original, | |
43 | ArchiveNode, |
|
43 | ArchiveNode, | |
44 | store_archive_in_cache, |
|
44 | store_archive_in_cache, | |
45 | BytesEnvelope, |
|
45 | BytesEnvelope, | |
46 | BinaryEnvelope, |
|
46 | BinaryEnvelope, | |
47 | ) |
|
47 | ) | |
48 | from vcsserver.exceptions import NoContentException |
|
48 | from vcsserver.exceptions import NoContentException | |
49 | from vcsserver.str_utils import safe_str, safe_bytes |
|
49 | from vcsserver.str_utils import safe_str, safe_bytes | |
50 | from vcsserver.type_utils import assert_bytes |
|
50 | from vcsserver.type_utils import assert_bytes | |
51 | from vcsserver.vcs_base import RemoteBase |
|
51 | from vcsserver.vcs_base import RemoteBase | |
52 | from vcsserver.lib.svnremoterepo import svnremoterepo |
|
52 | from vcsserver.lib.svnremoterepo import svnremoterepo | |
53 |
|
53 | |||
54 | log = logging.getLogger(__name__) |
|
54 | log = logging.getLogger(__name__) | |
55 |
|
55 | |||
56 |
|
56 | |||
57 | svn_compatible_versions_map = { |
|
57 | svn_compatible_versions_map = { | |
58 | 'pre-1.4-compatible': '1.3', |
|
58 | 'pre-1.4-compatible': '1.3', | |
59 | 'pre-1.5-compatible': '1.4', |
|
59 | 'pre-1.5-compatible': '1.4', | |
60 | 'pre-1.6-compatible': '1.5', |
|
60 | 'pre-1.6-compatible': '1.5', | |
61 | 'pre-1.8-compatible': '1.7', |
|
61 | 'pre-1.8-compatible': '1.7', | |
62 | 'pre-1.9-compatible': '1.8', |
|
62 | 'pre-1.9-compatible': '1.8', | |
63 | } |
|
63 | } | |
64 |
|
64 | |||
65 | current_compatible_version = '1.14' |
|
65 | current_compatible_version = '1.14' | |
66 |
|
66 | |||
67 |
|
67 | |||
68 | def reraise_safe_exceptions(func): |
|
68 | def reraise_safe_exceptions(func): | |
69 | """Decorator for converting svn exceptions to something neutral.""" |
|
69 | """Decorator for converting svn exceptions to something neutral.""" | |
70 | def wrapper(*args, **kwargs): |
|
70 | def wrapper(*args, **kwargs): | |
71 | try: |
|
71 | try: | |
72 | return func(*args, **kwargs) |
|
72 | return func(*args, **kwargs) | |
73 | except Exception as e: |
|
73 | except Exception as e: | |
74 | if not hasattr(e, '_vcs_kind'): |
|
74 | if not hasattr(e, '_vcs_kind'): | |
75 | log.exception("Unhandled exception in svn remote call") |
|
75 | log.exception("Unhandled exception in svn remote call") | |
76 | raise_from_original(exceptions.UnhandledException(e), e) |
|
76 | raise_from_original(exceptions.UnhandledException(e), e) | |
77 | raise |
|
77 | raise | |
78 | return wrapper |
|
78 | return wrapper | |
79 |
|
79 | |||
80 |
|
80 | |||
81 | class SubversionFactory(RepoFactory): |
|
81 | class SubversionFactory(RepoFactory): | |
82 | repo_type = 'svn' |
|
82 | repo_type = 'svn' | |
83 |
|
83 | |||
84 | def _create_repo(self, wire, create, compatible_version): |
|
84 | def _create_repo(self, wire, create, compatible_version): | |
85 | path = svn.core.svn_path_canonicalize(wire['path']) |
|
85 | path = svn.core.svn_path_canonicalize(wire['path']) | |
86 | if create: |
|
86 | if create: | |
87 | fs_config = {'compatible-version': current_compatible_version} |
|
87 | fs_config = {'compatible-version': current_compatible_version} | |
88 | if compatible_version: |
|
88 | if compatible_version: | |
89 |
|
89 | |||
90 | compatible_version_string = \ |
|
90 | compatible_version_string = \ | |
91 | svn_compatible_versions_map.get(compatible_version) \ |
|
91 | svn_compatible_versions_map.get(compatible_version) \ | |
92 | or compatible_version |
|
92 | or compatible_version | |
93 | fs_config['compatible-version'] = compatible_version_string |
|
93 | fs_config['compatible-version'] = compatible_version_string | |
94 |
|
94 | |||
95 | log.debug('Create SVN repo with config `%s`', fs_config) |
|
95 | log.debug('Create SVN repo with config `%s`', fs_config) | |
96 | repo = svn.repos.create(path, "", "", None, fs_config) |
|
96 | repo = svn.repos.create(path, "", "", None, fs_config) | |
97 | else: |
|
97 | else: | |
98 | repo = svn.repos.open(path) |
|
98 | repo = svn.repos.open(path) | |
99 |
|
99 | |||
100 | log.debug('repository created: got SVN object: %s', repo) |
|
100 | log.debug('repository created: got SVN object: %s', repo) | |
101 | return repo |
|
101 | return repo | |
102 |
|
102 | |||
103 | def repo(self, wire, create=False, compatible_version=None): |
|
103 | def repo(self, wire, create=False, compatible_version=None): | |
104 | """ |
|
104 | """ | |
105 | Get a repository instance for the given path. |
|
105 | Get a repository instance for the given path. | |
106 | """ |
|
106 | """ | |
107 | return self._create_repo(wire, create, compatible_version) |
|
107 | return self._create_repo(wire, create, compatible_version) | |
108 |
|
108 | |||
109 |
|
109 | |||
110 | NODE_TYPE_MAPPING = { |
|
110 | NODE_TYPE_MAPPING = { | |
111 | svn.core.svn_node_file: 'file', |
|
111 | svn.core.svn_node_file: 'file', | |
112 | svn.core.svn_node_dir: 'dir', |
|
112 | svn.core.svn_node_dir: 'dir', | |
113 | } |
|
113 | } | |
114 |
|
114 | |||
115 |
|
115 | |||
116 | class SvnRemote(RemoteBase): |
|
116 | class SvnRemote(RemoteBase): | |
117 |
|
117 | |||
118 | def __init__(self, factory, hg_factory=None): |
|
118 | def __init__(self, factory, hg_factory=None): | |
119 | self._factory = factory |
|
119 | self._factory = factory | |
120 |
|
120 | |||
121 | self._bulk_methods = { |
|
121 | self._bulk_methods = { | |
122 | # NOT supported in SVN ATM... |
|
122 | # NOT supported in SVN ATM... | |
123 | } |
|
123 | } | |
124 | self._bulk_file_methods = { |
|
124 | self._bulk_file_methods = { | |
125 | "size": self.get_file_size, |
|
125 | "size": self.get_file_size, | |
126 | "data": self.get_file_content, |
|
126 | "data": self.get_file_content, | |
127 | "flags": self.get_node_type, |
|
127 | "flags": self.get_node_type, | |
128 | "is_binary": self.is_binary, |
|
128 | "is_binary": self.is_binary, | |
129 | "md5": self.md5_hash |
|
129 | "md5": self.md5_hash | |
130 | } |
|
130 | } | |
131 |
|
131 | |||
132 | @reraise_safe_exceptions |
|
132 | @reraise_safe_exceptions | |
133 | def bulk_file_request(self, wire, commit_id, path, pre_load): |
|
133 | def bulk_file_request(self, wire, commit_id, path, pre_load): | |
134 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
134 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
135 | region = self._region(wire) |
|
135 | region = self._region(wire) | |
136 |
|
136 | |||
137 | # since we use unified API, we need to cast from str to in for SVN |
|
137 | # since we use unified API, we need to cast from str to in for SVN | |
138 | commit_id = int(commit_id) |
|
138 | commit_id = int(commit_id) | |
139 |
|
139 | |||
140 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
140 | @region.conditional_cache_on_arguments(condition=cache_on) | |
141 | def _bulk_file_request(_repo_id, _commit_id, _path, _pre_load): |
|
141 | def _bulk_file_request(_repo_id, _commit_id, _path, _pre_load): | |
142 | result = {} |
|
142 | result = {} | |
143 | for attr in pre_load: |
|
143 | for attr in pre_load: | |
144 | try: |
|
144 | try: | |
145 | method = self._bulk_file_methods[attr] |
|
145 | method = self._bulk_file_methods[attr] | |
146 | wire.update({'cache': False}) # disable cache for bulk calls so we don't double cache |
|
146 | wire.update({'cache': False}) # disable cache for bulk calls so we don't double cache | |
147 | result[attr] = method(wire, _commit_id, _path) |
|
147 | result[attr] = method(wire, _commit_id, _path) | |
148 | except KeyError as e: |
|
148 | except KeyError as e: | |
149 | raise exceptions.VcsException(e)(f'Unknown bulk attribute: "{attr}"') |
|
149 | raise exceptions.VcsException(e)(f'Unknown bulk attribute: "{attr}"') | |
150 | return result |
|
150 | return result | |
151 |
|
151 | |||
152 | return BinaryEnvelope(_bulk_file_request(repo_id, commit_id, path, sorted(pre_load))) |
|
152 | return BinaryEnvelope(_bulk_file_request(repo_id, commit_id, path, sorted(pre_load))) | |
153 |
|
153 | |||
154 | @reraise_safe_exceptions |
|
154 | @reraise_safe_exceptions | |
155 | def discover_svn_version(self): |
|
155 | def discover_svn_version(self): | |
156 | try: |
|
156 | try: | |
157 | import svn.core |
|
157 | import svn.core | |
158 | svn_ver = svn.core.SVN_VERSION |
|
158 | svn_ver = svn.core.SVN_VERSION | |
159 | except ImportError: |
|
159 | except ImportError: | |
160 | svn_ver = None |
|
160 | svn_ver = None | |
161 | return safe_str(svn_ver) |
|
161 | return safe_str(svn_ver) | |
162 |
|
162 | |||
163 | @reraise_safe_exceptions |
|
163 | @reraise_safe_exceptions | |
164 | def is_empty(self, wire): |
|
164 | def is_empty(self, wire): | |
165 | try: |
|
165 | try: | |
166 | return self.lookup(wire, -1) == 0 |
|
166 | return self.lookup(wire, -1) == 0 | |
167 | except Exception: |
|
167 | except Exception: | |
168 | log.exception("failed to read object_store") |
|
168 | log.exception("failed to read object_store") | |
169 | return False |
|
169 | return False | |
170 |
|
170 | |||
171 | def check_url(self, url, config): |
|
171 | def check_url(self, url, config): | |
172 |
|
172 | |||
173 | # uuid function gets only valid UUID from proper repo, else |
|
173 | # uuid function gets only valid UUID from proper repo, else | |
174 | # throws exception |
|
174 | # throws exception | |
175 | username, password, src_url = self.get_url_and_credentials(url) |
|
175 | username, password, src_url = self.get_url_and_credentials(url) | |
176 | try: |
|
176 | try: | |
177 | svnremoterepo(safe_bytes(username), safe_bytes(password), safe_bytes(src_url)).svn().uuid |
|
177 | svnremoterepo(safe_bytes(username), safe_bytes(password), safe_bytes(src_url)).svn().uuid | |
178 | except Exception: |
|
178 | except Exception: | |
179 | tb = traceback.format_exc() |
|
179 | tb = traceback.format_exc() | |
180 | log.debug("Invalid Subversion url: `%s`, tb: %s", url, tb) |
|
180 | log.debug("Invalid Subversion url: `%s`, tb: %s", url, tb) | |
181 | raise URLError(f'"{url}" is not a valid Subversion source url.') |
|
181 | raise URLError(f'"{url}" is not a valid Subversion source url.') | |
182 | return True |
|
182 | return True | |
183 |
|
183 | |||
184 | def is_path_valid_repository(self, wire, path): |
|
184 | def is_path_valid_repository(self, wire, path): | |
185 |
|
185 | |||
186 | # NOTE(marcink): short circuit the check for SVN repo |
|
186 | # NOTE(marcink): short circuit the check for SVN repo | |
187 | # the repos.open might be expensive to check, but we have one cheap |
|
187 | # the repos.open might be expensive to check, but we have one cheap | |
188 | # pre condition that we can use, to check for 'format' file |
|
188 | # pre condition that we can use, to check for 'format' file | |
189 |
|
189 | |||
190 | if not os.path.isfile(os.path.join(path, 'format')): |
|
190 | if not os.path.isfile(os.path.join(path, 'format')): | |
191 | return False |
|
191 | return False | |
192 |
|
192 | |||
193 | try: |
|
193 | try: | |
194 | svn.repos.open(path) |
|
194 | svn.repos.open(path) | |
195 | except svn.core.SubversionException: |
|
195 | except svn.core.SubversionException: | |
196 | tb = traceback.format_exc() |
|
196 | tb = traceback.format_exc() | |
197 | log.debug("Invalid Subversion path `%s`, tb: %s", path, tb) |
|
197 | log.debug("Invalid Subversion path `%s`, tb: %s", path, tb) | |
198 | return False |
|
198 | return False | |
199 | return True |
|
199 | return True | |
200 |
|
200 | |||
201 | @reraise_safe_exceptions |
|
201 | @reraise_safe_exceptions | |
202 | def verify(self, wire,): |
|
202 | def verify(self, wire,): | |
203 | repo_path = wire['path'] |
|
203 | repo_path = wire['path'] | |
204 | if not self.is_path_valid_repository(wire, repo_path): |
|
204 | if not self.is_path_valid_repository(wire, repo_path): | |
205 | raise Exception( |
|
205 | raise Exception( | |
206 |
"Path |
|
206 | f"Path {repo_path} is not a valid Subversion repository.") | |
207 |
|
207 | |||
208 | cmd = ['svnadmin', 'info', repo_path] |
|
208 | cmd = ['svnadmin', 'info', repo_path] | |
209 | stdout, stderr = subprocessio.run_command(cmd) |
|
209 | stdout, stderr = subprocessio.run_command(cmd) | |
210 | return stdout |
|
210 | return stdout | |
211 |
|
211 | |||
212 | @reraise_safe_exceptions |
|
212 | @reraise_safe_exceptions | |
213 | def lookup(self, wire, revision): |
|
213 | def lookup(self, wire, revision): | |
214 | if revision not in [-1, None, 'HEAD']: |
|
214 | if revision not in [-1, None, 'HEAD']: | |
215 | raise NotImplementedError |
|
215 | raise NotImplementedError | |
216 | repo = self._factory.repo(wire) |
|
216 | repo = self._factory.repo(wire) | |
217 | fs_ptr = svn.repos.fs(repo) |
|
217 | fs_ptr = svn.repos.fs(repo) | |
218 | head = svn.fs.youngest_rev(fs_ptr) |
|
218 | head = svn.fs.youngest_rev(fs_ptr) | |
219 | return head |
|
219 | return head | |
220 |
|
220 | |||
221 | @reraise_safe_exceptions |
|
221 | @reraise_safe_exceptions | |
222 | def lookup_interval(self, wire, start_ts, end_ts): |
|
222 | def lookup_interval(self, wire, start_ts, end_ts): | |
223 | repo = self._factory.repo(wire) |
|
223 | repo = self._factory.repo(wire) | |
224 | fsobj = svn.repos.fs(repo) |
|
224 | fsobj = svn.repos.fs(repo) | |
225 | start_rev = None |
|
225 | start_rev = None | |
226 | end_rev = None |
|
226 | end_rev = None | |
227 | if start_ts: |
|
227 | if start_ts: | |
228 | start_ts_svn = apr_time_t(start_ts) |
|
228 | start_ts_svn = apr_time_t(start_ts) | |
229 | start_rev = svn.repos.dated_revision(repo, start_ts_svn) + 1 |
|
229 | start_rev = svn.repos.dated_revision(repo, start_ts_svn) + 1 | |
230 | else: |
|
230 | else: | |
231 | start_rev = 1 |
|
231 | start_rev = 1 | |
232 | if end_ts: |
|
232 | if end_ts: | |
233 | end_ts_svn = apr_time_t(end_ts) |
|
233 | end_ts_svn = apr_time_t(end_ts) | |
234 | end_rev = svn.repos.dated_revision(repo, end_ts_svn) |
|
234 | end_rev = svn.repos.dated_revision(repo, end_ts_svn) | |
235 | else: |
|
235 | else: | |
236 | end_rev = svn.fs.youngest_rev(fsobj) |
|
236 | end_rev = svn.fs.youngest_rev(fsobj) | |
237 | return start_rev, end_rev |
|
237 | return start_rev, end_rev | |
238 |
|
238 | |||
239 | @reraise_safe_exceptions |
|
239 | @reraise_safe_exceptions | |
240 | def revision_properties(self, wire, revision): |
|
240 | def revision_properties(self, wire, revision): | |
241 |
|
241 | |||
242 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
242 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
243 | region = self._region(wire) |
|
243 | region = self._region(wire) | |
244 |
|
244 | |||
245 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
245 | @region.conditional_cache_on_arguments(condition=cache_on) | |
246 | def _revision_properties(_repo_id, _revision): |
|
246 | def _revision_properties(_repo_id, _revision): | |
247 | repo = self._factory.repo(wire) |
|
247 | repo = self._factory.repo(wire) | |
248 | fs_ptr = svn.repos.fs(repo) |
|
248 | fs_ptr = svn.repos.fs(repo) | |
249 | return svn.fs.revision_proplist(fs_ptr, revision) |
|
249 | return svn.fs.revision_proplist(fs_ptr, revision) | |
250 | return _revision_properties(repo_id, revision) |
|
250 | return _revision_properties(repo_id, revision) | |
251 |
|
251 | |||
252 | def revision_changes(self, wire, revision): |
|
252 | def revision_changes(self, wire, revision): | |
253 |
|
253 | |||
254 | repo = self._factory.repo(wire) |
|
254 | repo = self._factory.repo(wire) | |
255 | fsobj = svn.repos.fs(repo) |
|
255 | fsobj = svn.repos.fs(repo) | |
256 | rev_root = svn.fs.revision_root(fsobj, revision) |
|
256 | rev_root = svn.fs.revision_root(fsobj, revision) | |
257 |
|
257 | |||
258 | editor = svn.repos.ChangeCollector(fsobj, rev_root) |
|
258 | editor = svn.repos.ChangeCollector(fsobj, rev_root) | |
259 | editor_ptr, editor_baton = svn.delta.make_editor(editor) |
|
259 | editor_ptr, editor_baton = svn.delta.make_editor(editor) | |
260 | base_dir = "" |
|
260 | base_dir = "" | |
261 | send_deltas = False |
|
261 | send_deltas = False | |
262 | svn.repos.replay2( |
|
262 | svn.repos.replay2( | |
263 | rev_root, base_dir, svn.core.SVN_INVALID_REVNUM, send_deltas, |
|
263 | rev_root, base_dir, svn.core.SVN_INVALID_REVNUM, send_deltas, | |
264 | editor_ptr, editor_baton, None) |
|
264 | editor_ptr, editor_baton, None) | |
265 |
|
265 | |||
266 | added = [] |
|
266 | added = [] | |
267 | changed = [] |
|
267 | changed = [] | |
268 | removed = [] |
|
268 | removed = [] | |
269 |
|
269 | |||
270 | # TODO: CHANGE_ACTION_REPLACE: Figure out where it belongs |
|
270 | # TODO: CHANGE_ACTION_REPLACE: Figure out where it belongs | |
271 | for path, change in editor.changes.items(): |
|
271 | for path, change in editor.changes.items(): | |
272 | # TODO: Decide what to do with directory nodes. Subversion can add |
|
272 | # TODO: Decide what to do with directory nodes. Subversion can add | |
273 | # empty directories. |
|
273 | # empty directories. | |
274 |
|
274 | |||
275 | if change.item_kind == svn.core.svn_node_dir: |
|
275 | if change.item_kind == svn.core.svn_node_dir: | |
276 | continue |
|
276 | continue | |
277 | if change.action in [svn.repos.CHANGE_ACTION_ADD]: |
|
277 | if change.action in [svn.repos.CHANGE_ACTION_ADD]: | |
278 | added.append(path) |
|
278 | added.append(path) | |
279 | elif change.action in [svn.repos.CHANGE_ACTION_MODIFY, |
|
279 | elif change.action in [svn.repos.CHANGE_ACTION_MODIFY, | |
280 | svn.repos.CHANGE_ACTION_REPLACE]: |
|
280 | svn.repos.CHANGE_ACTION_REPLACE]: | |
281 | changed.append(path) |
|
281 | changed.append(path) | |
282 | elif change.action in [svn.repos.CHANGE_ACTION_DELETE]: |
|
282 | elif change.action in [svn.repos.CHANGE_ACTION_DELETE]: | |
283 | removed.append(path) |
|
283 | removed.append(path) | |
284 | else: |
|
284 | else: | |
285 | raise NotImplementedError( |
|
285 | raise NotImplementedError( | |
286 | "Action {} not supported on path {}".format( |
|
286 | "Action {} not supported on path {}".format( | |
287 | change.action, path)) |
|
287 | change.action, path)) | |
288 |
|
288 | |||
289 | changes = { |
|
289 | changes = { | |
290 | 'added': added, |
|
290 | 'added': added, | |
291 | 'changed': changed, |
|
291 | 'changed': changed, | |
292 | 'removed': removed, |
|
292 | 'removed': removed, | |
293 | } |
|
293 | } | |
294 | return changes |
|
294 | return changes | |
295 |
|
295 | |||
296 | @reraise_safe_exceptions |
|
296 | @reraise_safe_exceptions | |
297 | def node_history(self, wire, path, revision, limit): |
|
297 | def node_history(self, wire, path, revision, limit): | |
298 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
298 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
299 | region = self._region(wire) |
|
299 | region = self._region(wire) | |
300 |
|
300 | |||
301 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
301 | @region.conditional_cache_on_arguments(condition=cache_on) | |
302 | def _assert_correct_path(_context_uid, _repo_id, _path, _revision, _limit): |
|
302 | def _assert_correct_path(_context_uid, _repo_id, _path, _revision, _limit): | |
303 | cross_copies = False |
|
303 | cross_copies = False | |
304 | repo = self._factory.repo(wire) |
|
304 | repo = self._factory.repo(wire) | |
305 | fsobj = svn.repos.fs(repo) |
|
305 | fsobj = svn.repos.fs(repo) | |
306 | rev_root = svn.fs.revision_root(fsobj, revision) |
|
306 | rev_root = svn.fs.revision_root(fsobj, revision) | |
307 |
|
307 | |||
308 | history_revisions = [] |
|
308 | history_revisions = [] | |
309 | history = svn.fs.node_history(rev_root, path) |
|
309 | history = svn.fs.node_history(rev_root, path) | |
310 | history = svn.fs.history_prev(history, cross_copies) |
|
310 | history = svn.fs.history_prev(history, cross_copies) | |
311 | while history: |
|
311 | while history: | |
312 | __, node_revision = svn.fs.history_location(history) |
|
312 | __, node_revision = svn.fs.history_location(history) | |
313 | history_revisions.append(node_revision) |
|
313 | history_revisions.append(node_revision) | |
314 | if limit and len(history_revisions) >= limit: |
|
314 | if limit and len(history_revisions) >= limit: | |
315 | break |
|
315 | break | |
316 | history = svn.fs.history_prev(history, cross_copies) |
|
316 | history = svn.fs.history_prev(history, cross_copies) | |
317 | return history_revisions |
|
317 | return history_revisions | |
318 | return _assert_correct_path(context_uid, repo_id, path, revision, limit) |
|
318 | return _assert_correct_path(context_uid, repo_id, path, revision, limit) | |
319 |
|
319 | |||
320 | @reraise_safe_exceptions |
|
320 | @reraise_safe_exceptions | |
321 | def node_properties(self, wire, path, revision): |
|
321 | def node_properties(self, wire, path, revision): | |
322 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
322 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
323 | region = self._region(wire) |
|
323 | region = self._region(wire) | |
324 |
|
324 | |||
325 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
325 | @region.conditional_cache_on_arguments(condition=cache_on) | |
326 | def _node_properties(_repo_id, _path, _revision): |
|
326 | def _node_properties(_repo_id, _path, _revision): | |
327 | repo = self._factory.repo(wire) |
|
327 | repo = self._factory.repo(wire) | |
328 | fsobj = svn.repos.fs(repo) |
|
328 | fsobj = svn.repos.fs(repo) | |
329 | rev_root = svn.fs.revision_root(fsobj, revision) |
|
329 | rev_root = svn.fs.revision_root(fsobj, revision) | |
330 | return svn.fs.node_proplist(rev_root, path) |
|
330 | return svn.fs.node_proplist(rev_root, path) | |
331 | return _node_properties(repo_id, path, revision) |
|
331 | return _node_properties(repo_id, path, revision) | |
332 |
|
332 | |||
333 | def file_annotate(self, wire, path, revision): |
|
333 | def file_annotate(self, wire, path, revision): | |
334 | abs_path = 'file://' + urllib.request.pathname2url( |
|
334 | abs_path = 'file://' + urllib.request.pathname2url( | |
335 | vcspath.join(wire['path'], path)) |
|
335 | vcspath.join(wire['path'], path)) | |
336 | file_uri = svn.core.svn_path_canonicalize(abs_path) |
|
336 | file_uri = svn.core.svn_path_canonicalize(abs_path) | |
337 |
|
337 | |||
338 | start_rev = svn_opt_revision_value_t(0) |
|
338 | start_rev = svn_opt_revision_value_t(0) | |
339 | peg_rev = svn_opt_revision_value_t(revision) |
|
339 | peg_rev = svn_opt_revision_value_t(revision) | |
340 | end_rev = peg_rev |
|
340 | end_rev = peg_rev | |
341 |
|
341 | |||
342 | annotations = [] |
|
342 | annotations = [] | |
343 |
|
343 | |||
344 | def receiver(line_no, revision, author, date, line, pool): |
|
344 | def receiver(line_no, revision, author, date, line, pool): | |
345 | annotations.append((line_no, revision, line)) |
|
345 | annotations.append((line_no, revision, line)) | |
346 |
|
346 | |||
347 | # TODO: Cannot use blame5, missing typemap function in the swig code |
|
347 | # TODO: Cannot use blame5, missing typemap function in the swig code | |
348 | try: |
|
348 | try: | |
349 | svn.client.blame2( |
|
349 | svn.client.blame2( | |
350 | file_uri, peg_rev, start_rev, end_rev, |
|
350 | file_uri, peg_rev, start_rev, end_rev, | |
351 | receiver, svn.client.create_context()) |
|
351 | receiver, svn.client.create_context()) | |
352 | except svn.core.SubversionException as exc: |
|
352 | except svn.core.SubversionException as exc: | |
353 | log.exception("Error during blame operation.") |
|
353 | log.exception("Error during blame operation.") | |
354 | raise Exception( |
|
354 | raise Exception( | |
355 | f"Blame not supported or file does not exist at path {path}. " |
|
355 | f"Blame not supported or file does not exist at path {path}. " | |
356 | f"Error {exc}.") |
|
356 | f"Error {exc}.") | |
357 |
|
357 | |||
358 | return BinaryEnvelope(annotations) |
|
358 | return BinaryEnvelope(annotations) | |
359 |
|
359 | |||
360 | @reraise_safe_exceptions |
|
360 | @reraise_safe_exceptions | |
361 | def get_node_type(self, wire, revision=None, path=''): |
|
361 | def get_node_type(self, wire, revision=None, path=''): | |
362 |
|
362 | |||
363 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
363 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
364 | region = self._region(wire) |
|
364 | region = self._region(wire) | |
365 |
|
365 | |||
366 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
366 | @region.conditional_cache_on_arguments(condition=cache_on) | |
367 | def _get_node_type(_repo_id, _revision, _path): |
|
367 | def _get_node_type(_repo_id, _revision, _path): | |
368 | repo = self._factory.repo(wire) |
|
368 | repo = self._factory.repo(wire) | |
369 | fs_ptr = svn.repos.fs(repo) |
|
369 | fs_ptr = svn.repos.fs(repo) | |
370 | if _revision is None: |
|
370 | if _revision is None: | |
371 | _revision = svn.fs.youngest_rev(fs_ptr) |
|
371 | _revision = svn.fs.youngest_rev(fs_ptr) | |
372 | root = svn.fs.revision_root(fs_ptr, _revision) |
|
372 | root = svn.fs.revision_root(fs_ptr, _revision) | |
373 | node = svn.fs.check_path(root, path) |
|
373 | node = svn.fs.check_path(root, path) | |
374 | return NODE_TYPE_MAPPING.get(node, None) |
|
374 | return NODE_TYPE_MAPPING.get(node, None) | |
375 | return _get_node_type(repo_id, revision, path) |
|
375 | return _get_node_type(repo_id, revision, path) | |
376 |
|
376 | |||
377 | @reraise_safe_exceptions |
|
377 | @reraise_safe_exceptions | |
378 | def get_nodes(self, wire, revision=None, path=''): |
|
378 | def get_nodes(self, wire, revision=None, path=''): | |
379 |
|
379 | |||
380 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
380 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
381 | region = self._region(wire) |
|
381 | region = self._region(wire) | |
382 |
|
382 | |||
383 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
383 | @region.conditional_cache_on_arguments(condition=cache_on) | |
384 | def _get_nodes(_repo_id, _path, _revision): |
|
384 | def _get_nodes(_repo_id, _path, _revision): | |
385 | repo = self._factory.repo(wire) |
|
385 | repo = self._factory.repo(wire) | |
386 | fsobj = svn.repos.fs(repo) |
|
386 | fsobj = svn.repos.fs(repo) | |
387 | if _revision is None: |
|
387 | if _revision is None: | |
388 | _revision = svn.fs.youngest_rev(fsobj) |
|
388 | _revision = svn.fs.youngest_rev(fsobj) | |
389 | root = svn.fs.revision_root(fsobj, _revision) |
|
389 | root = svn.fs.revision_root(fsobj, _revision) | |
390 | entries = svn.fs.dir_entries(root, path) |
|
390 | entries = svn.fs.dir_entries(root, path) | |
391 | result = [] |
|
391 | result = [] | |
392 | for entry_path, entry_info in entries.items(): |
|
392 | for entry_path, entry_info in entries.items(): | |
393 | result.append( |
|
393 | result.append( | |
394 | (entry_path, NODE_TYPE_MAPPING.get(entry_info.kind, None))) |
|
394 | (entry_path, NODE_TYPE_MAPPING.get(entry_info.kind, None))) | |
395 | return result |
|
395 | return result | |
396 | return _get_nodes(repo_id, path, revision) |
|
396 | return _get_nodes(repo_id, path, revision) | |
397 |
|
397 | |||
398 | @reraise_safe_exceptions |
|
398 | @reraise_safe_exceptions | |
399 | def get_file_content(self, wire, rev=None, path=''): |
|
399 | def get_file_content(self, wire, rev=None, path=''): | |
400 | repo = self._factory.repo(wire) |
|
400 | repo = self._factory.repo(wire) | |
401 | fsobj = svn.repos.fs(repo) |
|
401 | fsobj = svn.repos.fs(repo) | |
402 |
|
402 | |||
403 | if rev is None: |
|
403 | if rev is None: | |
404 | rev = svn.fs.youngest_rev(fsobj) |
|
404 | rev = svn.fs.youngest_rev(fsobj) | |
405 |
|
405 | |||
406 | root = svn.fs.revision_root(fsobj, rev) |
|
406 | root = svn.fs.revision_root(fsobj, rev) | |
407 | content = svn.core.Stream(svn.fs.file_contents(root, path)) |
|
407 | content = svn.core.Stream(svn.fs.file_contents(root, path)) | |
408 | return BytesEnvelope(content.read()) |
|
408 | return BytesEnvelope(content.read()) | |
409 |
|
409 | |||
410 | @reraise_safe_exceptions |
|
410 | @reraise_safe_exceptions | |
411 | def get_file_size(self, wire, revision=None, path=''): |
|
411 | def get_file_size(self, wire, revision=None, path=''): | |
412 |
|
412 | |||
413 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
413 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
414 | region = self._region(wire) |
|
414 | region = self._region(wire) | |
415 |
|
415 | |||
416 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
416 | @region.conditional_cache_on_arguments(condition=cache_on) | |
417 | def _get_file_size(_repo_id, _revision, _path): |
|
417 | def _get_file_size(_repo_id, _revision, _path): | |
418 | repo = self._factory.repo(wire) |
|
418 | repo = self._factory.repo(wire) | |
419 | fsobj = svn.repos.fs(repo) |
|
419 | fsobj = svn.repos.fs(repo) | |
420 | if _revision is None: |
|
420 | if _revision is None: | |
421 | _revision = svn.fs.youngest_revision(fsobj) |
|
421 | _revision = svn.fs.youngest_revision(fsobj) | |
422 | root = svn.fs.revision_root(fsobj, _revision) |
|
422 | root = svn.fs.revision_root(fsobj, _revision) | |
423 | size = svn.fs.file_length(root, path) |
|
423 | size = svn.fs.file_length(root, path) | |
424 | return size |
|
424 | return size | |
425 | return _get_file_size(repo_id, revision, path) |
|
425 | return _get_file_size(repo_id, revision, path) | |
426 |
|
426 | |||
427 | def create_repository(self, wire, compatible_version=None): |
|
427 | def create_repository(self, wire, compatible_version=None): | |
428 | log.info('Creating Subversion repository in path "%s"', wire['path']) |
|
428 | log.info('Creating Subversion repository in path "%s"', wire['path']) | |
429 | self._factory.repo(wire, create=True, |
|
429 | self._factory.repo(wire, create=True, | |
430 | compatible_version=compatible_version) |
|
430 | compatible_version=compatible_version) | |
431 |
|
431 | |||
432 | def get_url_and_credentials(self, src_url) -> tuple[str, str, str]: |
|
432 | def get_url_and_credentials(self, src_url) -> tuple[str, str, str]: | |
433 | obj = urllib.parse.urlparse(src_url) |
|
433 | obj = urllib.parse.urlparse(src_url) | |
434 | username = obj.username or '' |
|
434 | username = obj.username or '' | |
435 | password = obj.password or '' |
|
435 | password = obj.password or '' | |
436 | return username, password, src_url |
|
436 | return username, password, src_url | |
437 |
|
437 | |||
438 | def import_remote_repository(self, wire, src_url): |
|
438 | def import_remote_repository(self, wire, src_url): | |
439 | repo_path = wire['path'] |
|
439 | repo_path = wire['path'] | |
440 | if not self.is_path_valid_repository(wire, repo_path): |
|
440 | if not self.is_path_valid_repository(wire, repo_path): | |
441 | raise Exception( |
|
441 | raise Exception( | |
442 |
"Path |
|
442 | f"Path {repo_path} is not a valid Subversion repository.") | |
443 |
|
443 | |||
444 | username, password, src_url = self.get_url_and_credentials(src_url) |
|
444 | username, password, src_url = self.get_url_and_credentials(src_url) | |
445 | rdump_cmd = ['svnrdump', 'dump', '--non-interactive', |
|
445 | rdump_cmd = ['svnrdump', 'dump', '--non-interactive', | |
446 | '--trust-server-cert-failures=unknown-ca'] |
|
446 | '--trust-server-cert-failures=unknown-ca'] | |
447 | if username and password: |
|
447 | if username and password: | |
448 | rdump_cmd += ['--username', username, '--password', password] |
|
448 | rdump_cmd += ['--username', username, '--password', password] | |
449 | rdump_cmd += [src_url] |
|
449 | rdump_cmd += [src_url] | |
450 |
|
450 | |||
451 | rdump = subprocess.Popen( |
|
451 | rdump = subprocess.Popen( | |
452 | rdump_cmd, |
|
452 | rdump_cmd, | |
453 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
|
453 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
454 | load = subprocess.Popen( |
|
454 | load = subprocess.Popen( | |
455 | ['svnadmin', 'load', repo_path], stdin=rdump.stdout) |
|
455 | ['svnadmin', 'load', repo_path], stdin=rdump.stdout) | |
456 |
|
456 | |||
457 | # TODO: johbo: This can be a very long operation, might be better |
|
457 | # TODO: johbo: This can be a very long operation, might be better | |
458 | # to track some kind of status and provide an api to check if the |
|
458 | # to track some kind of status and provide an api to check if the | |
459 | # import is done. |
|
459 | # import is done. | |
460 | rdump.wait() |
|
460 | rdump.wait() | |
461 | load.wait() |
|
461 | load.wait() | |
462 |
|
462 | |||
463 | log.debug('Return process ended with code: %s', rdump.returncode) |
|
463 | log.debug('Return process ended with code: %s', rdump.returncode) | |
464 | if rdump.returncode != 0: |
|
464 | if rdump.returncode != 0: | |
465 | errors = rdump.stderr.read() |
|
465 | errors = rdump.stderr.read() | |
466 | log.error('svnrdump dump failed: statuscode %s: message: %s', rdump.returncode, errors) |
|
466 | log.error('svnrdump dump failed: statuscode %s: message: %s', rdump.returncode, errors) | |
467 |
|
467 | |||
468 | reason = 'UNKNOWN' |
|
468 | reason = 'UNKNOWN' | |
469 | if b'svnrdump: E230001:' in errors: |
|
469 | if b'svnrdump: E230001:' in errors: | |
470 | reason = 'INVALID_CERTIFICATE' |
|
470 | reason = 'INVALID_CERTIFICATE' | |
471 |
|
471 | |||
472 | if reason == 'UNKNOWN': |
|
472 | if reason == 'UNKNOWN': | |
473 | reason = f'UNKNOWN:{safe_str(errors)}' |
|
473 | reason = f'UNKNOWN:{safe_str(errors)}' | |
474 |
|
474 | |||
475 | raise Exception( |
|
475 | raise Exception( | |
476 | 'Failed to dump the remote repository from {}. Reason:{}'.format( |
|
476 | 'Failed to dump the remote repository from {}. Reason:{}'.format( | |
477 | src_url, reason)) |
|
477 | src_url, reason)) | |
478 | if load.returncode != 0: |
|
478 | if load.returncode != 0: | |
479 | raise Exception( |
|
479 | raise Exception( | |
480 |
'Failed to load the dump of remote repository from |
|
480 | f'Failed to load the dump of remote repository from {src_url}.') | |
481 | (src_url, )) |
|
|||
482 |
|
481 | |||
483 | def commit(self, wire, message, author, timestamp, updated, removed): |
|
482 | def commit(self, wire, message, author, timestamp, updated, removed): | |
484 |
|
483 | |||
485 | message = safe_bytes(message) |
|
484 | message = safe_bytes(message) | |
486 | author = safe_bytes(author) |
|
485 | author = safe_bytes(author) | |
487 |
|
486 | |||
488 | repo = self._factory.repo(wire) |
|
487 | repo = self._factory.repo(wire) | |
489 | fsobj = svn.repos.fs(repo) |
|
488 | fsobj = svn.repos.fs(repo) | |
490 |
|
489 | |||
491 | rev = svn.fs.youngest_rev(fsobj) |
|
490 | rev = svn.fs.youngest_rev(fsobj) | |
492 | txn = svn.repos.fs_begin_txn_for_commit(repo, rev, author, message) |
|
491 | txn = svn.repos.fs_begin_txn_for_commit(repo, rev, author, message) | |
493 | txn_root = svn.fs.txn_root(txn) |
|
492 | txn_root = svn.fs.txn_root(txn) | |
494 |
|
493 | |||
495 | for node in updated: |
|
494 | for node in updated: | |
496 | TxnNodeProcessor(node, txn_root).update() |
|
495 | TxnNodeProcessor(node, txn_root).update() | |
497 | for node in removed: |
|
496 | for node in removed: | |
498 | TxnNodeProcessor(node, txn_root).remove() |
|
497 | TxnNodeProcessor(node, txn_root).remove() | |
499 |
|
498 | |||
500 | commit_id = svn.repos.fs_commit_txn(repo, txn) |
|
499 | commit_id = svn.repos.fs_commit_txn(repo, txn) | |
501 |
|
500 | |||
502 | if timestamp: |
|
501 | if timestamp: | |
503 | apr_time = apr_time_t(timestamp) |
|
502 | apr_time = apr_time_t(timestamp) | |
504 | ts_formatted = svn.core.svn_time_to_cstring(apr_time) |
|
503 | ts_formatted = svn.core.svn_time_to_cstring(apr_time) | |
505 | svn.fs.change_rev_prop(fsobj, commit_id, 'svn:date', ts_formatted) |
|
504 | svn.fs.change_rev_prop(fsobj, commit_id, 'svn:date', ts_formatted) | |
506 |
|
505 | |||
507 | log.debug('Committed revision "%s" to "%s".', commit_id, wire['path']) |
|
506 | log.debug('Committed revision "%s" to "%s".', commit_id, wire['path']) | |
508 | return commit_id |
|
507 | return commit_id | |
509 |
|
508 | |||
510 | @reraise_safe_exceptions |
|
509 | @reraise_safe_exceptions | |
511 | def diff(self, wire, rev1, rev2, path1=None, path2=None, |
|
510 | def diff(self, wire, rev1, rev2, path1=None, path2=None, | |
512 | ignore_whitespace=False, context=3): |
|
511 | ignore_whitespace=False, context=3): | |
513 |
|
512 | |||
514 | wire.update(cache=False) |
|
513 | wire.update(cache=False) | |
515 | repo = self._factory.repo(wire) |
|
514 | repo = self._factory.repo(wire) | |
516 | diff_creator = SvnDiffer( |
|
515 | diff_creator = SvnDiffer( | |
517 | repo, rev1, path1, rev2, path2, ignore_whitespace, context) |
|
516 | repo, rev1, path1, rev2, path2, ignore_whitespace, context) | |
518 | try: |
|
517 | try: | |
519 | return BytesEnvelope(diff_creator.generate_diff()) |
|
518 | return BytesEnvelope(diff_creator.generate_diff()) | |
520 | except svn.core.SubversionException as e: |
|
519 | except svn.core.SubversionException as e: | |
521 | log.exception( |
|
520 | log.exception( | |
522 | "Error during diff operation operation. " |
|
521 | "Error during diff operation operation. " | |
523 | "Path might not exist %s, %s", path1, path2) |
|
522 | "Path might not exist %s, %s", path1, path2) | |
524 | return BytesEnvelope(b'') |
|
523 | return BytesEnvelope(b'') | |
525 |
|
524 | |||
526 | @reraise_safe_exceptions |
|
525 | @reraise_safe_exceptions | |
527 | def is_large_file(self, wire, path): |
|
526 | def is_large_file(self, wire, path): | |
528 | return False |
|
527 | return False | |
529 |
|
528 | |||
530 | @reraise_safe_exceptions |
|
529 | @reraise_safe_exceptions | |
531 | def is_binary(self, wire, rev, path): |
|
530 | def is_binary(self, wire, rev, path): | |
532 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
531 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
533 | region = self._region(wire) |
|
532 | region = self._region(wire) | |
534 |
|
533 | |||
535 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
534 | @region.conditional_cache_on_arguments(condition=cache_on) | |
536 | def _is_binary(_repo_id, _rev, _path): |
|
535 | def _is_binary(_repo_id, _rev, _path): | |
537 | raw_bytes = self.get_file_content(wire, rev, path) |
|
536 | raw_bytes = self.get_file_content(wire, rev, path) | |
538 | if not raw_bytes: |
|
537 | if not raw_bytes: | |
539 | return False |
|
538 | return False | |
540 | return b'\0' in raw_bytes |
|
539 | return b'\0' in raw_bytes | |
541 |
|
540 | |||
542 | return _is_binary(repo_id, rev, path) |
|
541 | return _is_binary(repo_id, rev, path) | |
543 |
|
542 | |||
544 | @reraise_safe_exceptions |
|
543 | @reraise_safe_exceptions | |
545 | def md5_hash(self, wire, rev, path): |
|
544 | def md5_hash(self, wire, rev, path): | |
546 | cache_on, context_uid, repo_id = self._cache_on(wire) |
|
545 | cache_on, context_uid, repo_id = self._cache_on(wire) | |
547 | region = self._region(wire) |
|
546 | region = self._region(wire) | |
548 |
|
547 | |||
549 | @region.conditional_cache_on_arguments(condition=cache_on) |
|
548 | @region.conditional_cache_on_arguments(condition=cache_on) | |
550 | def _md5_hash(_repo_id, _rev, _path): |
|
549 | def _md5_hash(_repo_id, _rev, _path): | |
551 | return '' |
|
550 | return '' | |
552 |
|
551 | |||
553 | return _md5_hash(repo_id, rev, path) |
|
552 | return _md5_hash(repo_id, rev, path) | |
554 |
|
553 | |||
555 | @reraise_safe_exceptions |
|
554 | @reraise_safe_exceptions | |
556 | def run_svn_command(self, wire, cmd, **opts): |
|
555 | def run_svn_command(self, wire, cmd, **opts): | |
557 | path = wire.get('path', None) |
|
556 | path = wire.get('path', None) | |
558 |
|
557 | |||
559 | if path and os.path.isdir(path): |
|
558 | if path and os.path.isdir(path): | |
560 | opts['cwd'] = path |
|
559 | opts['cwd'] = path | |
561 |
|
560 | |||
562 | safe_call = opts.pop('_safe', False) |
|
561 | safe_call = opts.pop('_safe', False) | |
563 |
|
562 | |||
564 | svnenv = os.environ.copy() |
|
563 | svnenv = os.environ.copy() | |
565 | svnenv.update(opts.pop('extra_env', {})) |
|
564 | svnenv.update(opts.pop('extra_env', {})) | |
566 |
|
565 | |||
567 | _opts = {'env': svnenv, 'shell': False} |
|
566 | _opts = {'env': svnenv, 'shell': False} | |
568 |
|
567 | |||
569 | try: |
|
568 | try: | |
570 | _opts.update(opts) |
|
569 | _opts.update(opts) | |
571 | proc = subprocessio.SubprocessIOChunker(cmd, **_opts) |
|
570 | proc = subprocessio.SubprocessIOChunker(cmd, **_opts) | |
572 |
|
571 | |||
573 | return b''.join(proc), b''.join(proc.stderr) |
|
572 | return b''.join(proc), b''.join(proc.stderr) | |
574 | except OSError as err: |
|
573 | except OSError as err: | |
575 | if safe_call: |
|
574 | if safe_call: | |
576 | return '', safe_str(err).strip() |
|
575 | return '', safe_str(err).strip() | |
577 | else: |
|
576 | else: | |
578 | cmd = ' '.join(map(safe_str, cmd)) # human friendly CMD |
|
577 | cmd = ' '.join(map(safe_str, cmd)) # human friendly CMD | |
579 | tb_err = ("Couldn't run svn command (%s).\n" |
|
578 | tb_err = ("Couldn't run svn command (%s).\n" | |
580 | "Original error was:%s\n" |
|
579 | "Original error was:%s\n" | |
581 | "Call options:%s\n" |
|
580 | "Call options:%s\n" | |
582 | % (cmd, err, _opts)) |
|
581 | % (cmd, err, _opts)) | |
583 | log.exception(tb_err) |
|
582 | log.exception(tb_err) | |
584 | raise exceptions.VcsException()(tb_err) |
|
583 | raise exceptions.VcsException()(tb_err) | |
585 |
|
584 | |||
586 | @reraise_safe_exceptions |
|
585 | @reraise_safe_exceptions | |
587 | def install_hooks(self, wire, force=False): |
|
586 | def install_hooks(self, wire, force=False): | |
588 | from vcsserver.hook_utils import install_svn_hooks |
|
587 | from vcsserver.hook_utils import install_svn_hooks | |
589 | repo_path = wire['path'] |
|
588 | repo_path = wire['path'] | |
590 | binary_dir = settings.BINARY_DIR |
|
589 | binary_dir = settings.BINARY_DIR | |
591 | executable = None |
|
590 | executable = None | |
592 | if binary_dir: |
|
591 | if binary_dir: | |
593 | executable = os.path.join(binary_dir, 'python3') |
|
592 | executable = os.path.join(binary_dir, 'python3') | |
594 | return install_svn_hooks(repo_path, force_create=force) |
|
593 | return install_svn_hooks(repo_path, force_create=force) | |
595 |
|
594 | |||
596 | @reraise_safe_exceptions |
|
595 | @reraise_safe_exceptions | |
597 | def get_hooks_info(self, wire): |
|
596 | def get_hooks_info(self, wire): | |
598 | from vcsserver.hook_utils import ( |
|
597 | from vcsserver.hook_utils import ( | |
599 | get_svn_pre_hook_version, get_svn_post_hook_version) |
|
598 | get_svn_pre_hook_version, get_svn_post_hook_version) | |
600 | repo_path = wire['path'] |
|
599 | repo_path = wire['path'] | |
601 | return { |
|
600 | return { | |
602 | 'pre_version': get_svn_pre_hook_version(repo_path), |
|
601 | 'pre_version': get_svn_pre_hook_version(repo_path), | |
603 | 'post_version': get_svn_post_hook_version(repo_path), |
|
602 | 'post_version': get_svn_post_hook_version(repo_path), | |
604 | } |
|
603 | } | |
605 |
|
604 | |||
606 | @reraise_safe_exceptions |
|
605 | @reraise_safe_exceptions | |
607 | def set_head_ref(self, wire, head_name): |
|
606 | def set_head_ref(self, wire, head_name): | |
608 | pass |
|
607 | pass | |
609 |
|
608 | |||
610 | @reraise_safe_exceptions |
|
609 | @reraise_safe_exceptions | |
611 | def archive_repo(self, wire, archive_name_key, kind, mtime, archive_at_path, |
|
610 | def archive_repo(self, wire, archive_name_key, kind, mtime, archive_at_path, | |
612 | archive_dir_name, commit_id, cache_config): |
|
611 | archive_dir_name, commit_id, cache_config): | |
613 |
|
612 | |||
614 | def walk_tree(root, root_dir, _commit_id): |
|
613 | def walk_tree(root, root_dir, _commit_id): | |
615 | """ |
|
614 | """ | |
616 | Special recursive svn repo walker |
|
615 | Special recursive svn repo walker | |
617 | """ |
|
616 | """ | |
618 | root_dir = safe_bytes(root_dir) |
|
617 | root_dir = safe_bytes(root_dir) | |
619 |
|
618 | |||
620 | filemode_default = 0o100644 |
|
619 | filemode_default = 0o100644 | |
621 | filemode_executable = 0o100755 |
|
620 | filemode_executable = 0o100755 | |
622 |
|
621 | |||
623 | file_iter = svn.fs.dir_entries(root, root_dir) |
|
622 | file_iter = svn.fs.dir_entries(root, root_dir) | |
624 | for f_name in file_iter: |
|
623 | for f_name in file_iter: | |
625 | f_type = NODE_TYPE_MAPPING.get(file_iter[f_name].kind, None) |
|
624 | f_type = NODE_TYPE_MAPPING.get(file_iter[f_name].kind, None) | |
626 |
|
625 | |||
627 | if f_type == 'dir': |
|
626 | if f_type == 'dir': | |
628 | # return only DIR, and then all entries in that dir |
|
627 | # return only DIR, and then all entries in that dir | |
629 | yield os.path.join(root_dir, f_name), {'mode': filemode_default}, f_type |
|
628 | yield os.path.join(root_dir, f_name), {'mode': filemode_default}, f_type | |
630 | new_root = os.path.join(root_dir, f_name) |
|
629 | new_root = os.path.join(root_dir, f_name) | |
631 | yield from walk_tree(root, new_root, _commit_id) |
|
630 | yield from walk_tree(root, new_root, _commit_id) | |
632 | else: |
|
631 | else: | |
633 |
|
632 | |||
634 | f_path = os.path.join(root_dir, f_name).rstrip(b'/') |
|
633 | f_path = os.path.join(root_dir, f_name).rstrip(b'/') | |
635 | prop_list = svn.fs.node_proplist(root, f_path) |
|
634 | prop_list = svn.fs.node_proplist(root, f_path) | |
636 |
|
635 | |||
637 | f_mode = filemode_default |
|
636 | f_mode = filemode_default | |
638 | if prop_list.get('svn:executable'): |
|
637 | if prop_list.get('svn:executable'): | |
639 | f_mode = filemode_executable |
|
638 | f_mode = filemode_executable | |
640 |
|
639 | |||
641 | f_is_link = False |
|
640 | f_is_link = False | |
642 | if prop_list.get('svn:special'): |
|
641 | if prop_list.get('svn:special'): | |
643 | f_is_link = True |
|
642 | f_is_link = True | |
644 |
|
643 | |||
645 | data = { |
|
644 | data = { | |
646 | 'is_link': f_is_link, |
|
645 | 'is_link': f_is_link, | |
647 | 'mode': f_mode, |
|
646 | 'mode': f_mode, | |
648 | 'content_stream': svn.core.Stream(svn.fs.file_contents(root, f_path)).read |
|
647 | 'content_stream': svn.core.Stream(svn.fs.file_contents(root, f_path)).read | |
649 | } |
|
648 | } | |
650 |
|
649 | |||
651 | yield f_path, data, f_type |
|
650 | yield f_path, data, f_type | |
652 |
|
651 | |||
653 | def file_walker(_commit_id, path): |
|
652 | def file_walker(_commit_id, path): | |
654 | repo = self._factory.repo(wire) |
|
653 | repo = self._factory.repo(wire) | |
655 | root = svn.fs.revision_root(svn.repos.fs(repo), int(commit_id)) |
|
654 | root = svn.fs.revision_root(svn.repos.fs(repo), int(commit_id)) | |
656 |
|
655 | |||
657 | def no_content(): |
|
656 | def no_content(): | |
658 | raise NoContentException() |
|
657 | raise NoContentException() | |
659 |
|
658 | |||
660 | for f_name, f_data, f_type in walk_tree(root, path, _commit_id): |
|
659 | for f_name, f_data, f_type in walk_tree(root, path, _commit_id): | |
661 | file_path = f_name |
|
660 | file_path = f_name | |
662 |
|
661 | |||
663 | if f_type == 'dir': |
|
662 | if f_type == 'dir': | |
664 | mode = f_data['mode'] |
|
663 | mode = f_data['mode'] | |
665 | yield ArchiveNode(file_path, mode, False, no_content) |
|
664 | yield ArchiveNode(file_path, mode, False, no_content) | |
666 | else: |
|
665 | else: | |
667 | mode = f_data['mode'] |
|
666 | mode = f_data['mode'] | |
668 | is_link = f_data['is_link'] |
|
667 | is_link = f_data['is_link'] | |
669 | data_stream = f_data['content_stream'] |
|
668 | data_stream = f_data['content_stream'] | |
670 | yield ArchiveNode(file_path, mode, is_link, data_stream) |
|
669 | yield ArchiveNode(file_path, mode, is_link, data_stream) | |
671 |
|
670 | |||
672 | return store_archive_in_cache( |
|
671 | return store_archive_in_cache( | |
673 | file_walker, archive_name_key, kind, mtime, archive_at_path, archive_dir_name, commit_id, cache_config=cache_config) |
|
672 | file_walker, archive_name_key, kind, mtime, archive_at_path, archive_dir_name, commit_id, cache_config=cache_config) | |
674 |
|
673 | |||
675 |
|
674 | |||
676 |
class SvnDiffer |
|
675 | class SvnDiffer: | |
677 | """ |
|
676 | """ | |
678 | Utility to create diffs based on difflib and the Subversion api |
|
677 | Utility to create diffs based on difflib and the Subversion api | |
679 | """ |
|
678 | """ | |
680 |
|
679 | |||
681 | binary_content = False |
|
680 | binary_content = False | |
682 |
|
681 | |||
683 | def __init__( |
|
682 | def __init__( | |
684 | self, repo, src_rev, src_path, tgt_rev, tgt_path, |
|
683 | self, repo, src_rev, src_path, tgt_rev, tgt_path, | |
685 | ignore_whitespace, context): |
|
684 | ignore_whitespace, context): | |
686 | self.repo = repo |
|
685 | self.repo = repo | |
687 | self.ignore_whitespace = ignore_whitespace |
|
686 | self.ignore_whitespace = ignore_whitespace | |
688 | self.context = context |
|
687 | self.context = context | |
689 |
|
688 | |||
690 | fsobj = svn.repos.fs(repo) |
|
689 | fsobj = svn.repos.fs(repo) | |
691 |
|
690 | |||
692 | self.tgt_rev = tgt_rev |
|
691 | self.tgt_rev = tgt_rev | |
693 | self.tgt_path = tgt_path or '' |
|
692 | self.tgt_path = tgt_path or '' | |
694 | self.tgt_root = svn.fs.revision_root(fsobj, tgt_rev) |
|
693 | self.tgt_root = svn.fs.revision_root(fsobj, tgt_rev) | |
695 | self.tgt_kind = svn.fs.check_path(self.tgt_root, self.tgt_path) |
|
694 | self.tgt_kind = svn.fs.check_path(self.tgt_root, self.tgt_path) | |
696 |
|
695 | |||
697 | self.src_rev = src_rev |
|
696 | self.src_rev = src_rev | |
698 | self.src_path = src_path or self.tgt_path |
|
697 | self.src_path = src_path or self.tgt_path | |
699 | self.src_root = svn.fs.revision_root(fsobj, src_rev) |
|
698 | self.src_root = svn.fs.revision_root(fsobj, src_rev) | |
700 | self.src_kind = svn.fs.check_path(self.src_root, self.src_path) |
|
699 | self.src_kind = svn.fs.check_path(self.src_root, self.src_path) | |
701 |
|
700 | |||
702 | self._validate() |
|
701 | self._validate() | |
703 |
|
702 | |||
704 | def _validate(self): |
|
703 | def _validate(self): | |
705 | if (self.tgt_kind != svn.core.svn_node_none and |
|
704 | if (self.tgt_kind != svn.core.svn_node_none and | |
706 | self.src_kind != svn.core.svn_node_none and |
|
705 | self.src_kind != svn.core.svn_node_none and | |
707 | self.src_kind != self.tgt_kind): |
|
706 | self.src_kind != self.tgt_kind): | |
708 | # TODO: johbo: proper error handling |
|
707 | # TODO: johbo: proper error handling | |
709 | raise Exception( |
|
708 | raise Exception( | |
710 | "Source and target are not compatible for diff generation. " |
|
709 | "Source and target are not compatible for diff generation. " | |
711 | "Source type: %s, target type: %s" % |
|
710 | "Source type: %s, target type: %s" % | |
712 | (self.src_kind, self.tgt_kind)) |
|
711 | (self.src_kind, self.tgt_kind)) | |
713 |
|
712 | |||
714 | def generate_diff(self) -> bytes: |
|
713 | def generate_diff(self) -> bytes: | |
715 | buf = io.BytesIO() |
|
714 | buf = io.BytesIO() | |
716 | if self.tgt_kind == svn.core.svn_node_dir: |
|
715 | if self.tgt_kind == svn.core.svn_node_dir: | |
717 | self._generate_dir_diff(buf) |
|
716 | self._generate_dir_diff(buf) | |
718 | else: |
|
717 | else: | |
719 | self._generate_file_diff(buf) |
|
718 | self._generate_file_diff(buf) | |
720 | return buf.getvalue() |
|
719 | return buf.getvalue() | |
721 |
|
720 | |||
722 | def _generate_dir_diff(self, buf: io.BytesIO): |
|
721 | def _generate_dir_diff(self, buf: io.BytesIO): | |
723 | editor = DiffChangeEditor() |
|
722 | editor = DiffChangeEditor() | |
724 | editor_ptr, editor_baton = svn.delta.make_editor(editor) |
|
723 | editor_ptr, editor_baton = svn.delta.make_editor(editor) | |
725 | svn.repos.dir_delta2( |
|
724 | svn.repos.dir_delta2( | |
726 | self.src_root, |
|
725 | self.src_root, | |
727 | self.src_path, |
|
726 | self.src_path, | |
728 | '', # src_entry |
|
727 | '', # src_entry | |
729 | self.tgt_root, |
|
728 | self.tgt_root, | |
730 | self.tgt_path, |
|
729 | self.tgt_path, | |
731 | editor_ptr, editor_baton, |
|
730 | editor_ptr, editor_baton, | |
732 | authorization_callback_allow_all, |
|
731 | authorization_callback_allow_all, | |
733 | False, # text_deltas |
|
732 | False, # text_deltas | |
734 | svn.core.svn_depth_infinity, # depth |
|
733 | svn.core.svn_depth_infinity, # depth | |
735 | False, # entry_props |
|
734 | False, # entry_props | |
736 | False, # ignore_ancestry |
|
735 | False, # ignore_ancestry | |
737 | ) |
|
736 | ) | |
738 |
|
737 | |||
739 | for path, __, change in sorted(editor.changes): |
|
738 | for path, __, change in sorted(editor.changes): | |
740 | self._generate_node_diff( |
|
739 | self._generate_node_diff( | |
741 | buf, change, path, self.tgt_path, path, self.src_path) |
|
740 | buf, change, path, self.tgt_path, path, self.src_path) | |
742 |
|
741 | |||
743 | def _generate_file_diff(self, buf: io.BytesIO): |
|
742 | def _generate_file_diff(self, buf: io.BytesIO): | |
744 | change = None |
|
743 | change = None | |
745 | if self.src_kind == svn.core.svn_node_none: |
|
744 | if self.src_kind == svn.core.svn_node_none: | |
746 | change = "add" |
|
745 | change = "add" | |
747 | elif self.tgt_kind == svn.core.svn_node_none: |
|
746 | elif self.tgt_kind == svn.core.svn_node_none: | |
748 | change = "delete" |
|
747 | change = "delete" | |
749 | tgt_base, tgt_path = vcspath.split(self.tgt_path) |
|
748 | tgt_base, tgt_path = vcspath.split(self.tgt_path) | |
750 | src_base, src_path = vcspath.split(self.src_path) |
|
749 | src_base, src_path = vcspath.split(self.src_path) | |
751 | self._generate_node_diff( |
|
750 | self._generate_node_diff( | |
752 | buf, change, tgt_path, tgt_base, src_path, src_base) |
|
751 | buf, change, tgt_path, tgt_base, src_path, src_base) | |
753 |
|
752 | |||
754 | def _generate_node_diff( |
|
753 | def _generate_node_diff( | |
755 | self, buf: io.BytesIO, change, tgt_path, tgt_base, src_path, src_base): |
|
754 | self, buf: io.BytesIO, change, tgt_path, tgt_base, src_path, src_base): | |
756 |
|
755 | |||
757 | tgt_path_bytes = safe_bytes(tgt_path) |
|
756 | tgt_path_bytes = safe_bytes(tgt_path) | |
758 | tgt_path = safe_str(tgt_path) |
|
757 | tgt_path = safe_str(tgt_path) | |
759 |
|
758 | |||
760 | src_path_bytes = safe_bytes(src_path) |
|
759 | src_path_bytes = safe_bytes(src_path) | |
761 | src_path = safe_str(src_path) |
|
760 | src_path = safe_str(src_path) | |
762 |
|
761 | |||
763 | if self.src_rev == self.tgt_rev and tgt_base == src_base: |
|
762 | if self.src_rev == self.tgt_rev and tgt_base == src_base: | |
764 | # makes consistent behaviour with git/hg to return empty diff if |
|
763 | # makes consistent behaviour with git/hg to return empty diff if | |
765 | # we compare same revisions |
|
764 | # we compare same revisions | |
766 | return |
|
765 | return | |
767 |
|
766 | |||
768 | tgt_full_path = vcspath.join(tgt_base, tgt_path) |
|
767 | tgt_full_path = vcspath.join(tgt_base, tgt_path) | |
769 | src_full_path = vcspath.join(src_base, src_path) |
|
768 | src_full_path = vcspath.join(src_base, src_path) | |
770 |
|
769 | |||
771 | self.binary_content = False |
|
770 | self.binary_content = False | |
772 | mime_type = self._get_mime_type(tgt_full_path) |
|
771 | mime_type = self._get_mime_type(tgt_full_path) | |
773 |
|
772 | |||
774 | if mime_type and not mime_type.startswith(b'text'): |
|
773 | if mime_type and not mime_type.startswith(b'text'): | |
775 | self.binary_content = True |
|
774 | self.binary_content = True | |
776 | buf.write(b"=" * 67 + b'\n') |
|
775 | buf.write(b"=" * 67 + b'\n') | |
777 | buf.write(b"Cannot display: file marked as a binary type.\n") |
|
776 | buf.write(b"Cannot display: file marked as a binary type.\n") | |
778 | buf.write(b"svn:mime-type = %s\n" % mime_type) |
|
777 | buf.write(b"svn:mime-type = %s\n" % mime_type) | |
779 | buf.write(b"Index: %b\n" % tgt_path_bytes) |
|
778 | buf.write(b"Index: %b\n" % tgt_path_bytes) | |
780 | buf.write(b"=" * 67 + b'\n') |
|
779 | buf.write(b"=" * 67 + b'\n') | |
781 | buf.write(b"diff --git a/%b b/%b\n" % (tgt_path_bytes, tgt_path_bytes)) |
|
780 | buf.write(b"diff --git a/%b b/%b\n" % (tgt_path_bytes, tgt_path_bytes)) | |
782 |
|
781 | |||
783 | if change == 'add': |
|
782 | if change == 'add': | |
784 | # TODO: johbo: SVN is missing a zero here compared to git |
|
783 | # TODO: johbo: SVN is missing a zero here compared to git | |
785 | buf.write(b"new file mode 10644\n") |
|
784 | buf.write(b"new file mode 10644\n") | |
786 |
|
785 | |||
787 | # TODO(marcink): intro to binary detection of svn patches |
|
786 | # TODO(marcink): intro to binary detection of svn patches | |
788 | # if self.binary_content: |
|
787 | # if self.binary_content: | |
789 | # buf.write(b'GIT binary patch\n') |
|
788 | # buf.write(b'GIT binary patch\n') | |
790 |
|
789 | |||
791 | buf.write(b"--- /dev/null\t(revision 0)\n") |
|
790 | buf.write(b"--- /dev/null\t(revision 0)\n") | |
792 | src_lines = [] |
|
791 | src_lines = [] | |
793 | else: |
|
792 | else: | |
794 | if change == 'delete': |
|
793 | if change == 'delete': | |
795 | buf.write(b"deleted file mode 10644\n") |
|
794 | buf.write(b"deleted file mode 10644\n") | |
796 |
|
795 | |||
797 | # TODO(marcink): intro to binary detection of svn patches |
|
796 | # TODO(marcink): intro to binary detection of svn patches | |
798 | # if self.binary_content: |
|
797 | # if self.binary_content: | |
799 | # buf.write('GIT binary patch\n') |
|
798 | # buf.write('GIT binary patch\n') | |
800 |
|
799 | |||
801 | buf.write(b"--- a/%b\t(revision %d)\n" % (src_path_bytes, self.src_rev)) |
|
800 | buf.write(b"--- a/%b\t(revision %d)\n" % (src_path_bytes, self.src_rev)) | |
802 | src_lines = self._svn_readlines(self.src_root, src_full_path) |
|
801 | src_lines = self._svn_readlines(self.src_root, src_full_path) | |
803 |
|
802 | |||
804 | if change == 'delete': |
|
803 | if change == 'delete': | |
805 | buf.write(b"+++ /dev/null\t(revision %d)\n" % self.tgt_rev) |
|
804 | buf.write(b"+++ /dev/null\t(revision %d)\n" % self.tgt_rev) | |
806 | tgt_lines = [] |
|
805 | tgt_lines = [] | |
807 | else: |
|
806 | else: | |
808 | buf.write(b"+++ b/%b\t(revision %d)\n" % (tgt_path_bytes, self.tgt_rev)) |
|
807 | buf.write(b"+++ b/%b\t(revision %d)\n" % (tgt_path_bytes, self.tgt_rev)) | |
809 | tgt_lines = self._svn_readlines(self.tgt_root, tgt_full_path) |
|
808 | tgt_lines = self._svn_readlines(self.tgt_root, tgt_full_path) | |
810 |
|
809 | |||
811 | # we made our diff header, time to generate the diff content into our buffer |
|
810 | # we made our diff header, time to generate the diff content into our buffer | |
812 |
|
811 | |||
813 | if not self.binary_content: |
|
812 | if not self.binary_content: | |
814 | udiff = svn_diff.unified_diff( |
|
813 | udiff = svn_diff.unified_diff( | |
815 | src_lines, tgt_lines, context=self.context, |
|
814 | src_lines, tgt_lines, context=self.context, | |
816 | ignore_blank_lines=self.ignore_whitespace, |
|
815 | ignore_blank_lines=self.ignore_whitespace, | |
817 | ignore_case=False, |
|
816 | ignore_case=False, | |
818 | ignore_space_changes=self.ignore_whitespace) |
|
817 | ignore_space_changes=self.ignore_whitespace) | |
819 |
|
818 | |||
820 | buf.writelines(udiff) |
|
819 | buf.writelines(udiff) | |
821 |
|
820 | |||
822 | def _get_mime_type(self, path) -> bytes: |
|
821 | def _get_mime_type(self, path) -> bytes: | |
823 | try: |
|
822 | try: | |
824 | mime_type = svn.fs.node_prop( |
|
823 | mime_type = svn.fs.node_prop( | |
825 | self.tgt_root, path, svn.core.SVN_PROP_MIME_TYPE) |
|
824 | self.tgt_root, path, svn.core.SVN_PROP_MIME_TYPE) | |
826 | except svn.core.SubversionException: |
|
825 | except svn.core.SubversionException: | |
827 | mime_type = svn.fs.node_prop( |
|
826 | mime_type = svn.fs.node_prop( | |
828 | self.src_root, path, svn.core.SVN_PROP_MIME_TYPE) |
|
827 | self.src_root, path, svn.core.SVN_PROP_MIME_TYPE) | |
829 | return mime_type |
|
828 | return mime_type | |
830 |
|
829 | |||
831 | def _svn_readlines(self, fs_root, node_path): |
|
830 | def _svn_readlines(self, fs_root, node_path): | |
832 | if self.binary_content: |
|
831 | if self.binary_content: | |
833 | return [] |
|
832 | return [] | |
834 | node_kind = svn.fs.check_path(fs_root, node_path) |
|
833 | node_kind = svn.fs.check_path(fs_root, node_path) | |
835 | if node_kind not in ( |
|
834 | if node_kind not in ( | |
836 | svn.core.svn_node_file, svn.core.svn_node_symlink): |
|
835 | svn.core.svn_node_file, svn.core.svn_node_symlink): | |
837 | return [] |
|
836 | return [] | |
838 | content = svn.core.Stream( |
|
837 | content = svn.core.Stream( | |
839 | svn.fs.file_contents(fs_root, node_path)).read() |
|
838 | svn.fs.file_contents(fs_root, node_path)).read() | |
840 |
|
839 | |||
841 | return content.splitlines(True) |
|
840 | return content.splitlines(True) | |
842 |
|
841 | |||
843 |
|
842 | |||
844 | class DiffChangeEditor(svn.delta.Editor): |
|
843 | class DiffChangeEditor(svn.delta.Editor): | |
845 | """ |
|
844 | """ | |
846 | Records changes between two given revisions |
|
845 | Records changes between two given revisions | |
847 | """ |
|
846 | """ | |
848 |
|
847 | |||
849 | def __init__(self): |
|
848 | def __init__(self): | |
850 | self.changes = [] |
|
849 | self.changes = [] | |
851 |
|
850 | |||
852 | def delete_entry(self, path, revision, parent_baton, pool=None): |
|
851 | def delete_entry(self, path, revision, parent_baton, pool=None): | |
853 | self.changes.append((path, None, 'delete')) |
|
852 | self.changes.append((path, None, 'delete')) | |
854 |
|
853 | |||
855 | def add_file( |
|
854 | def add_file( | |
856 | self, path, parent_baton, copyfrom_path, copyfrom_revision, |
|
855 | self, path, parent_baton, copyfrom_path, copyfrom_revision, | |
857 | file_pool=None): |
|
856 | file_pool=None): | |
858 | self.changes.append((path, 'file', 'add')) |
|
857 | self.changes.append((path, 'file', 'add')) | |
859 |
|
858 | |||
860 | def open_file(self, path, parent_baton, base_revision, file_pool=None): |
|
859 | def open_file(self, path, parent_baton, base_revision, file_pool=None): | |
861 | self.changes.append((path, 'file', 'change')) |
|
860 | self.changes.append((path, 'file', 'change')) | |
862 |
|
861 | |||
863 |
|
862 | |||
864 | def authorization_callback_allow_all(root, path, pool): |
|
863 | def authorization_callback_allow_all(root, path, pool): | |
865 | return True |
|
864 | return True | |
866 |
|
865 | |||
867 |
|
866 | |||
868 |
class TxnNodeProcessor |
|
867 | class TxnNodeProcessor: | |
869 | """ |
|
868 | """ | |
870 | Utility to process the change of one node within a transaction root. |
|
869 | Utility to process the change of one node within a transaction root. | |
871 |
|
870 | |||
872 | It encapsulates the knowledge of how to add, update or remove |
|
871 | It encapsulates the knowledge of how to add, update or remove | |
873 | a node for a given transaction root. The purpose is to support the method |
|
872 | a node for a given transaction root. The purpose is to support the method | |
874 | `SvnRemote.commit`. |
|
873 | `SvnRemote.commit`. | |
875 | """ |
|
874 | """ | |
876 |
|
875 | |||
877 | def __init__(self, node, txn_root): |
|
876 | def __init__(self, node, txn_root): | |
878 | assert_bytes(node['path']) |
|
877 | assert_bytes(node['path']) | |
879 |
|
878 | |||
880 | self.node = node |
|
879 | self.node = node | |
881 | self.txn_root = txn_root |
|
880 | self.txn_root = txn_root | |
882 |
|
881 | |||
883 | def update(self): |
|
882 | def update(self): | |
884 | self._ensure_parent_dirs() |
|
883 | self._ensure_parent_dirs() | |
885 | self._add_file_if_node_does_not_exist() |
|
884 | self._add_file_if_node_does_not_exist() | |
886 | self._update_file_content() |
|
885 | self._update_file_content() | |
887 | self._update_file_properties() |
|
886 | self._update_file_properties() | |
888 |
|
887 | |||
889 | def remove(self): |
|
888 | def remove(self): | |
890 | svn.fs.delete(self.txn_root, self.node['path']) |
|
889 | svn.fs.delete(self.txn_root, self.node['path']) | |
891 | # TODO: Clean up directory if empty |
|
890 | # TODO: Clean up directory if empty | |
892 |
|
891 | |||
893 | def _ensure_parent_dirs(self): |
|
892 | def _ensure_parent_dirs(self): | |
894 | curdir = vcspath.dirname(self.node['path']) |
|
893 | curdir = vcspath.dirname(self.node['path']) | |
895 | dirs_to_create = [] |
|
894 | dirs_to_create = [] | |
896 | while not self._svn_path_exists(curdir): |
|
895 | while not self._svn_path_exists(curdir): | |
897 | dirs_to_create.append(curdir) |
|
896 | dirs_to_create.append(curdir) | |
898 | curdir = vcspath.dirname(curdir) |
|
897 | curdir = vcspath.dirname(curdir) | |
899 |
|
898 | |||
900 | for curdir in reversed(dirs_to_create): |
|
899 | for curdir in reversed(dirs_to_create): | |
901 | log.debug('Creating missing directory "%s"', curdir) |
|
900 | log.debug('Creating missing directory "%s"', curdir) | |
902 | svn.fs.make_dir(self.txn_root, curdir) |
|
901 | svn.fs.make_dir(self.txn_root, curdir) | |
903 |
|
902 | |||
904 | def _svn_path_exists(self, path): |
|
903 | def _svn_path_exists(self, path): | |
905 | path_status = svn.fs.check_path(self.txn_root, path) |
|
904 | path_status = svn.fs.check_path(self.txn_root, path) | |
906 | return path_status != svn.core.svn_node_none |
|
905 | return path_status != svn.core.svn_node_none | |
907 |
|
906 | |||
908 | def _add_file_if_node_does_not_exist(self): |
|
907 | def _add_file_if_node_does_not_exist(self): | |
909 | kind = svn.fs.check_path(self.txn_root, self.node['path']) |
|
908 | kind = svn.fs.check_path(self.txn_root, self.node['path']) | |
910 | if kind == svn.core.svn_node_none: |
|
909 | if kind == svn.core.svn_node_none: | |
911 | svn.fs.make_file(self.txn_root, self.node['path']) |
|
910 | svn.fs.make_file(self.txn_root, self.node['path']) | |
912 |
|
911 | |||
913 | def _update_file_content(self): |
|
912 | def _update_file_content(self): | |
914 | assert_bytes(self.node['content']) |
|
913 | assert_bytes(self.node['content']) | |
915 |
|
914 | |||
916 | handler, baton = svn.fs.apply_textdelta( |
|
915 | handler, baton = svn.fs.apply_textdelta( | |
917 | self.txn_root, self.node['path'], None, None) |
|
916 | self.txn_root, self.node['path'], None, None) | |
918 | svn.delta.svn_txdelta_send_string(self.node['content'], handler, baton) |
|
917 | svn.delta.svn_txdelta_send_string(self.node['content'], handler, baton) | |
919 |
|
918 | |||
920 | def _update_file_properties(self): |
|
919 | def _update_file_properties(self): | |
921 | properties = self.node.get('properties', {}) |
|
920 | properties = self.node.get('properties', {}) | |
922 | for key, value in properties.items(): |
|
921 | for key, value in properties.items(): | |
923 | svn.fs.change_node_prop( |
|
922 | svn.fs.change_node_prop( | |
924 | self.txn_root, self.node['path'], safe_bytes(key), safe_bytes(value)) |
|
923 | self.txn_root, self.node['path'], safe_bytes(key), safe_bytes(value)) | |
925 |
|
924 | |||
926 |
|
925 | |||
927 | def apr_time_t(timestamp): |
|
926 | def apr_time_t(timestamp): | |
928 | """ |
|
927 | """ | |
929 | Convert a Python timestamp into APR timestamp type apr_time_t |
|
928 | Convert a Python timestamp into APR timestamp type apr_time_t | |
930 | """ |
|
929 | """ | |
931 | return int(timestamp * 1E6) |
|
930 | return int(timestamp * 1E6) | |
932 |
|
931 | |||
933 |
|
932 | |||
934 | def svn_opt_revision_value_t(num): |
|
933 | def svn_opt_revision_value_t(num): | |
935 | """ |
|
934 | """ | |
936 | Put `num` into a `svn_opt_revision_value_t` structure. |
|
935 | Put `num` into a `svn_opt_revision_value_t` structure. | |
937 | """ |
|
936 | """ | |
938 | value = svn.core.svn_opt_revision_value_t() |
|
937 | value = svn.core.svn_opt_revision_value_t() | |
939 | value.number = num |
|
938 | value.number = num | |
940 | revision = svn.core.svn_opt_revision_t() |
|
939 | revision = svn.core.svn_opt_revision_t() | |
941 | revision.kind = svn.core.svn_opt_revision_number |
|
940 | revision.kind = svn.core.svn_opt_revision_number | |
942 | revision.value = value |
|
941 | revision.value = value | |
943 | return revision |
|
942 | return revision |
@@ -1,34 +1,34 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | from vcsserver import scm_app, wsgi_app_caller |
|
18 | from vcsserver import scm_app, wsgi_app_caller | |
19 |
|
19 | |||
20 |
|
20 | |||
21 |
class GitRemoteWsgi |
|
21 | class GitRemoteWsgi: | |
22 | def handle(self, environ, input_data, *args, **kwargs): |
|
22 | def handle(self, environ, input_data, *args, **kwargs): | |
23 | app = wsgi_app_caller.WSGIAppCaller( |
|
23 | app = wsgi_app_caller.WSGIAppCaller( | |
24 | scm_app.create_git_wsgi_app(*args, **kwargs)) |
|
24 | scm_app.create_git_wsgi_app(*args, **kwargs)) | |
25 |
|
25 | |||
26 | return app.handle(environ, input_data) |
|
26 | return app.handle(environ, input_data) | |
27 |
|
27 | |||
28 |
|
28 | |||
29 |
class HgRemoteWsgi |
|
29 | class HgRemoteWsgi: | |
30 | def handle(self, environ, input_data, *args, **kwargs): |
|
30 | def handle(self, environ, input_data, *args, **kwargs): | |
31 | app = wsgi_app_caller.WSGIAppCaller( |
|
31 | app = wsgi_app_caller.WSGIAppCaller( | |
32 | scm_app.create_hg_wsgi_app(*args, **kwargs)) |
|
32 | scm_app.create_hg_wsgi_app(*args, **kwargs)) | |
33 |
|
33 | |||
34 | return app.handle(environ, input_data) |
|
34 | return app.handle(environ, input_data) |
@@ -1,242 +1,242 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import os |
|
18 | import os | |
19 | import logging |
|
19 | import logging | |
20 | import itertools |
|
20 | import itertools | |
21 |
|
21 | |||
22 | import mercurial |
|
22 | import mercurial | |
23 | import mercurial.error |
|
23 | import mercurial.error | |
24 | import mercurial.wireprotoserver |
|
24 | import mercurial.wireprotoserver | |
25 | import mercurial.hgweb.common |
|
25 | import mercurial.hgweb.common | |
26 | import mercurial.hgweb.hgweb_mod |
|
26 | import mercurial.hgweb.hgweb_mod | |
27 | import webob.exc |
|
27 | import webob.exc | |
28 |
|
28 | |||
29 | from vcsserver import pygrack, exceptions, settings, git_lfs |
|
29 | from vcsserver import pygrack, exceptions, settings, git_lfs | |
30 | from vcsserver.str_utils import ascii_bytes, safe_bytes |
|
30 | from vcsserver.str_utils import ascii_bytes, safe_bytes | |
31 |
|
31 | |||
32 | log = logging.getLogger(__name__) |
|
32 | log = logging.getLogger(__name__) | |
33 |
|
33 | |||
34 |
|
34 | |||
35 | # propagated from mercurial documentation |
|
35 | # propagated from mercurial documentation | |
36 | HG_UI_SECTIONS = [ |
|
36 | HG_UI_SECTIONS = [ | |
37 | 'alias', 'auth', 'decode/encode', 'defaults', 'diff', 'email', 'extensions', |
|
37 | 'alias', 'auth', 'decode/encode', 'defaults', 'diff', 'email', 'extensions', | |
38 | 'format', 'merge-patterns', 'merge-tools', 'hooks', 'http_proxy', 'smtp', |
|
38 | 'format', 'merge-patterns', 'merge-tools', 'hooks', 'http_proxy', 'smtp', | |
39 | 'patch', 'paths', 'profiling', 'server', 'trusted', 'ui', 'web', |
|
39 | 'patch', 'paths', 'profiling', 'server', 'trusted', 'ui', 'web', | |
40 | ] |
|
40 | ] | |
41 |
|
41 | |||
42 |
|
42 | |||
43 | class HgWeb(mercurial.hgweb.hgweb_mod.hgweb): |
|
43 | class HgWeb(mercurial.hgweb.hgweb_mod.hgweb): | |
44 | """Extension of hgweb that simplifies some functions.""" |
|
44 | """Extension of hgweb that simplifies some functions.""" | |
45 |
|
45 | |||
46 | def _get_view(self, repo): |
|
46 | def _get_view(self, repo): | |
47 | """Views are not supported.""" |
|
47 | """Views are not supported.""" | |
48 | return repo |
|
48 | return repo | |
49 |
|
49 | |||
50 | def loadsubweb(self): |
|
50 | def loadsubweb(self): | |
51 | """The result is only used in the templater method which is not used.""" |
|
51 | """The result is only used in the templater method which is not used.""" | |
52 | return None |
|
52 | return None | |
53 |
|
53 | |||
54 | def run(self): |
|
54 | def run(self): | |
55 | """Unused function so raise an exception if accidentally called.""" |
|
55 | """Unused function so raise an exception if accidentally called.""" | |
56 | raise NotImplementedError |
|
56 | raise NotImplementedError | |
57 |
|
57 | |||
58 | def templater(self, req): |
|
58 | def templater(self, req): | |
59 | """Function used in an unreachable code path. |
|
59 | """Function used in an unreachable code path. | |
60 |
|
60 | |||
61 | This code is unreachable because we guarantee that the HTTP request, |
|
61 | This code is unreachable because we guarantee that the HTTP request, | |
62 | corresponds to a Mercurial command. See the is_hg method. So, we are |
|
62 | corresponds to a Mercurial command. See the is_hg method. So, we are | |
63 | never going to get a user-visible url. |
|
63 | never going to get a user-visible url. | |
64 | """ |
|
64 | """ | |
65 | raise NotImplementedError |
|
65 | raise NotImplementedError | |
66 |
|
66 | |||
67 | def archivelist(self, nodeid): |
|
67 | def archivelist(self, nodeid): | |
68 | """Unused function so raise an exception if accidentally called.""" |
|
68 | """Unused function so raise an exception if accidentally called.""" | |
69 | raise NotImplementedError |
|
69 | raise NotImplementedError | |
70 |
|
70 | |||
71 | def __call__(self, environ, start_response): |
|
71 | def __call__(self, environ, start_response): | |
72 | """Run the WSGI application. |
|
72 | """Run the WSGI application. | |
73 |
|
73 | |||
74 | This may be called by multiple threads. |
|
74 | This may be called by multiple threads. | |
75 | """ |
|
75 | """ | |
76 | from mercurial.hgweb import request as requestmod |
|
76 | from mercurial.hgweb import request as requestmod | |
77 | req = requestmod.parserequestfromenv(environ) |
|
77 | req = requestmod.parserequestfromenv(environ) | |
78 | res = requestmod.wsgiresponse(req, start_response) |
|
78 | res = requestmod.wsgiresponse(req, start_response) | |
79 | gen = self.run_wsgi(req, res) |
|
79 | gen = self.run_wsgi(req, res) | |
80 |
|
80 | |||
81 | first_chunk = None |
|
81 | first_chunk = None | |
82 |
|
82 | |||
83 | try: |
|
83 | try: | |
84 | data = next(gen) |
|
84 | data = next(gen) | |
85 |
|
85 | |||
86 | def first_chunk(): |
|
86 | def first_chunk(): | |
87 | yield data |
|
87 | yield data | |
88 | except StopIteration: |
|
88 | except StopIteration: | |
89 | pass |
|
89 | pass | |
90 |
|
90 | |||
91 | if first_chunk: |
|
91 | if first_chunk: | |
92 | return itertools.chain(first_chunk(), gen) |
|
92 | return itertools.chain(first_chunk(), gen) | |
93 | return gen |
|
93 | return gen | |
94 |
|
94 | |||
95 | def _runwsgi(self, req, res, repo): |
|
95 | def _runwsgi(self, req, res, repo): | |
96 |
|
96 | |||
97 | cmd = req.qsparams.get(b'cmd', '') |
|
97 | cmd = req.qsparams.get(b'cmd', '') | |
98 | if not mercurial.wireprotoserver.iscmd(cmd): |
|
98 | if not mercurial.wireprotoserver.iscmd(cmd): | |
99 | # NOTE(marcink): for unsupported commands, we return bad request |
|
99 | # NOTE(marcink): for unsupported commands, we return bad request | |
100 | # internally from HG |
|
100 | # internally from HG | |
101 | log.warning('cmd: `%s` is not supported by the mercurial wireprotocol v1', cmd) |
|
101 | log.warning('cmd: `%s` is not supported by the mercurial wireprotocol v1', cmd) | |
102 | from mercurial.hgweb.common import statusmessage |
|
102 | from mercurial.hgweb.common import statusmessage | |
103 | res.status = statusmessage(mercurial.hgweb.common.HTTP_BAD_REQUEST) |
|
103 | res.status = statusmessage(mercurial.hgweb.common.HTTP_BAD_REQUEST) | |
104 | res.setbodybytes(b'') |
|
104 | res.setbodybytes(b'') | |
105 | return res.sendresponse() |
|
105 | return res.sendresponse() | |
106 |
|
106 | |||
107 | return super()._runwsgi(req, res, repo) |
|
107 | return super()._runwsgi(req, res, repo) | |
108 |
|
108 | |||
109 |
|
109 | |||
110 | def make_hg_ui_from_config(repo_config): |
|
110 | def make_hg_ui_from_config(repo_config): | |
111 | baseui = mercurial.ui.ui() |
|
111 | baseui = mercurial.ui.ui() | |
112 |
|
112 | |||
113 | # clean the baseui object |
|
113 | # clean the baseui object | |
114 | baseui._ocfg = mercurial.config.config() |
|
114 | baseui._ocfg = mercurial.config.config() | |
115 | baseui._ucfg = mercurial.config.config() |
|
115 | baseui._ucfg = mercurial.config.config() | |
116 | baseui._tcfg = mercurial.config.config() |
|
116 | baseui._tcfg = mercurial.config.config() | |
117 |
|
117 | |||
118 | for section, option, value in repo_config: |
|
118 | for section, option, value in repo_config: | |
119 | baseui.setconfig( |
|
119 | baseui.setconfig( | |
120 | ascii_bytes(section, allow_bytes=True), |
|
120 | ascii_bytes(section, allow_bytes=True), | |
121 | ascii_bytes(option, allow_bytes=True), |
|
121 | ascii_bytes(option, allow_bytes=True), | |
122 | ascii_bytes(value, allow_bytes=True)) |
|
122 | ascii_bytes(value, allow_bytes=True)) | |
123 |
|
123 | |||
124 | # make our hgweb quiet so it doesn't print output |
|
124 | # make our hgweb quiet so it doesn't print output | |
125 | baseui.setconfig(b'ui', b'quiet', b'true') |
|
125 | baseui.setconfig(b'ui', b'quiet', b'true') | |
126 |
|
126 | |||
127 | return baseui |
|
127 | return baseui | |
128 |
|
128 | |||
129 |
|
129 | |||
130 | def update_hg_ui_from_hgrc(baseui, repo_path): |
|
130 | def update_hg_ui_from_hgrc(baseui, repo_path): | |
131 | path = os.path.join(repo_path, '.hg', 'hgrc') |
|
131 | path = os.path.join(repo_path, '.hg', 'hgrc') | |
132 |
|
132 | |||
133 | if not os.path.isfile(path): |
|
133 | if not os.path.isfile(path): | |
134 | log.debug('hgrc file is not present at %s, skipping...', path) |
|
134 | log.debug('hgrc file is not present at %s, skipping...', path) | |
135 | return |
|
135 | return | |
136 | log.debug('reading hgrc from %s', path) |
|
136 | log.debug('reading hgrc from %s', path) | |
137 | cfg = mercurial.config.config() |
|
137 | cfg = mercurial.config.config() | |
138 | cfg.read(ascii_bytes(path)) |
|
138 | cfg.read(ascii_bytes(path)) | |
139 | for section in HG_UI_SECTIONS: |
|
139 | for section in HG_UI_SECTIONS: | |
140 | for k, v in cfg.items(section): |
|
140 | for k, v in cfg.items(section): | |
141 | log.debug('settings ui from file: [%s] %s=%s', section, k, v) |
|
141 | log.debug('settings ui from file: [%s] %s=%s', section, k, v) | |
142 | baseui.setconfig( |
|
142 | baseui.setconfig( | |
143 | ascii_bytes(section, allow_bytes=True), |
|
143 | ascii_bytes(section, allow_bytes=True), | |
144 | ascii_bytes(k, allow_bytes=True), |
|
144 | ascii_bytes(k, allow_bytes=True), | |
145 | ascii_bytes(v, allow_bytes=True)) |
|
145 | ascii_bytes(v, allow_bytes=True)) | |
146 |
|
146 | |||
147 |
|
147 | |||
148 | def create_hg_wsgi_app(repo_path, repo_name, config): |
|
148 | def create_hg_wsgi_app(repo_path, repo_name, config): | |
149 | """ |
|
149 | """ | |
150 | Prepares a WSGI application to handle Mercurial requests. |
|
150 | Prepares a WSGI application to handle Mercurial requests. | |
151 |
|
151 | |||
152 | :param config: is a list of 3-item tuples representing a ConfigObject |
|
152 | :param config: is a list of 3-item tuples representing a ConfigObject | |
153 | (it is the serialized version of the config object). |
|
153 | (it is the serialized version of the config object). | |
154 | """ |
|
154 | """ | |
155 | log.debug("Creating Mercurial WSGI application") |
|
155 | log.debug("Creating Mercurial WSGI application") | |
156 |
|
156 | |||
157 | baseui = make_hg_ui_from_config(config) |
|
157 | baseui = make_hg_ui_from_config(config) | |
158 | update_hg_ui_from_hgrc(baseui, repo_path) |
|
158 | update_hg_ui_from_hgrc(baseui, repo_path) | |
159 |
|
159 | |||
160 | try: |
|
160 | try: | |
161 | return HgWeb(safe_bytes(repo_path), name=safe_bytes(repo_name), baseui=baseui) |
|
161 | return HgWeb(safe_bytes(repo_path), name=safe_bytes(repo_name), baseui=baseui) | |
162 | except mercurial.error.RequirementError as e: |
|
162 | except mercurial.error.RequirementError as e: | |
163 | raise exceptions.RequirementException(e)(e) |
|
163 | raise exceptions.RequirementException(e)(e) | |
164 |
|
164 | |||
165 |
|
165 | |||
166 |
class GitHandler |
|
166 | class GitHandler: | |
167 | """ |
|
167 | """ | |
168 | Handler for Git operations like push/pull etc |
|
168 | Handler for Git operations like push/pull etc | |
169 | """ |
|
169 | """ | |
170 | def __init__(self, repo_location, repo_name, git_path, update_server_info, |
|
170 | def __init__(self, repo_location, repo_name, git_path, update_server_info, | |
171 | extras): |
|
171 | extras): | |
172 | if not os.path.isdir(repo_location): |
|
172 | if not os.path.isdir(repo_location): | |
173 | raise OSError(repo_location) |
|
173 | raise OSError(repo_location) | |
174 | self.content_path = repo_location |
|
174 | self.content_path = repo_location | |
175 | self.repo_name = repo_name |
|
175 | self.repo_name = repo_name | |
176 | self.repo_location = repo_location |
|
176 | self.repo_location = repo_location | |
177 | self.extras = extras |
|
177 | self.extras = extras | |
178 | self.git_path = git_path |
|
178 | self.git_path = git_path | |
179 | self.update_server_info = update_server_info |
|
179 | self.update_server_info = update_server_info | |
180 |
|
180 | |||
181 | def __call__(self, environ, start_response): |
|
181 | def __call__(self, environ, start_response): | |
182 | app = webob.exc.HTTPNotFound() |
|
182 | app = webob.exc.HTTPNotFound() | |
183 | candidate_paths = ( |
|
183 | candidate_paths = ( | |
184 | self.content_path, os.path.join(self.content_path, '.git')) |
|
184 | self.content_path, os.path.join(self.content_path, '.git')) | |
185 |
|
185 | |||
186 | for content_path in candidate_paths: |
|
186 | for content_path in candidate_paths: | |
187 | try: |
|
187 | try: | |
188 | app = pygrack.GitRepository( |
|
188 | app = pygrack.GitRepository( | |
189 | self.repo_name, content_path, self.git_path, |
|
189 | self.repo_name, content_path, self.git_path, | |
190 | self.update_server_info, self.extras) |
|
190 | self.update_server_info, self.extras) | |
191 | break |
|
191 | break | |
192 | except OSError: |
|
192 | except OSError: | |
193 | continue |
|
193 | continue | |
194 |
|
194 | |||
195 | return app(environ, start_response) |
|
195 | return app(environ, start_response) | |
196 |
|
196 | |||
197 |
|
197 | |||
198 | def create_git_wsgi_app(repo_path, repo_name, config): |
|
198 | def create_git_wsgi_app(repo_path, repo_name, config): | |
199 | """ |
|
199 | """ | |
200 | Creates a WSGI application to handle Git requests. |
|
200 | Creates a WSGI application to handle Git requests. | |
201 |
|
201 | |||
202 | :param config: is a dictionary holding the extras. |
|
202 | :param config: is a dictionary holding the extras. | |
203 | """ |
|
203 | """ | |
204 | git_path = settings.GIT_EXECUTABLE |
|
204 | git_path = settings.GIT_EXECUTABLE | |
205 | update_server_info = config.pop('git_update_server_info') |
|
205 | update_server_info = config.pop('git_update_server_info') | |
206 | app = GitHandler( |
|
206 | app = GitHandler( | |
207 | repo_path, repo_name, git_path, update_server_info, config) |
|
207 | repo_path, repo_name, git_path, update_server_info, config) | |
208 |
|
208 | |||
209 | return app |
|
209 | return app | |
210 |
|
210 | |||
211 |
|
211 | |||
212 |
class GitLFSHandler |
|
212 | class GitLFSHandler: | |
213 | """ |
|
213 | """ | |
214 | Handler for Git LFS operations |
|
214 | Handler for Git LFS operations | |
215 | """ |
|
215 | """ | |
216 |
|
216 | |||
217 | def __init__(self, repo_location, repo_name, git_path, update_server_info, |
|
217 | def __init__(self, repo_location, repo_name, git_path, update_server_info, | |
218 | extras): |
|
218 | extras): | |
219 | if not os.path.isdir(repo_location): |
|
219 | if not os.path.isdir(repo_location): | |
220 | raise OSError(repo_location) |
|
220 | raise OSError(repo_location) | |
221 | self.content_path = repo_location |
|
221 | self.content_path = repo_location | |
222 | self.repo_name = repo_name |
|
222 | self.repo_name = repo_name | |
223 | self.repo_location = repo_location |
|
223 | self.repo_location = repo_location | |
224 | self.extras = extras |
|
224 | self.extras = extras | |
225 | self.git_path = git_path |
|
225 | self.git_path = git_path | |
226 | self.update_server_info = update_server_info |
|
226 | self.update_server_info = update_server_info | |
227 |
|
227 | |||
228 | def get_app(self, git_lfs_enabled, git_lfs_store_path, git_lfs_http_scheme): |
|
228 | def get_app(self, git_lfs_enabled, git_lfs_store_path, git_lfs_http_scheme): | |
229 | app = git_lfs.create_app(git_lfs_enabled, git_lfs_store_path, git_lfs_http_scheme) |
|
229 | app = git_lfs.create_app(git_lfs_enabled, git_lfs_store_path, git_lfs_http_scheme) | |
230 | return app |
|
230 | return app | |
231 |
|
231 | |||
232 |
|
232 | |||
233 | def create_git_lfs_wsgi_app(repo_path, repo_name, config): |
|
233 | def create_git_lfs_wsgi_app(repo_path, repo_name, config): | |
234 | git_path = settings.GIT_EXECUTABLE |
|
234 | git_path = settings.GIT_EXECUTABLE | |
235 | update_server_info = config.pop(b'git_update_server_info') |
|
235 | update_server_info = config.pop(b'git_update_server_info') | |
236 | git_lfs_enabled = config.pop(b'git_lfs_enabled') |
|
236 | git_lfs_enabled = config.pop(b'git_lfs_enabled') | |
237 | git_lfs_store_path = config.pop(b'git_lfs_store_path') |
|
237 | git_lfs_store_path = config.pop(b'git_lfs_store_path') | |
238 | git_lfs_http_scheme = config.pop(b'git_lfs_http_scheme', 'http') |
|
238 | git_lfs_http_scheme = config.pop(b'git_lfs_http_scheme', 'http') | |
239 | app = GitLFSHandler( |
|
239 | app = GitLFSHandler( | |
240 | repo_path, repo_name, git_path, update_server_info, config) |
|
240 | repo_path, repo_name, git_path, update_server_info, config) | |
241 |
|
241 | |||
242 | return app.get_app(git_lfs_enabled, git_lfs_store_path, git_lfs_http_scheme) |
|
242 | return app.get_app(git_lfs_enabled, git_lfs_store_path, git_lfs_http_scheme) |
@@ -1,78 +1,78 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import gc |
|
18 | import gc | |
19 | import logging |
|
19 | import logging | |
20 | import os |
|
20 | import os | |
21 | import time |
|
21 | import time | |
22 |
|
22 | |||
23 |
|
23 | |||
24 | log = logging.getLogger(__name__) |
|
24 | log = logging.getLogger(__name__) | |
25 |
|
25 | |||
26 |
|
26 | |||
27 |
class VcsServer |
|
27 | class VcsServer: | |
28 | """ |
|
28 | """ | |
29 | Exposed remote interface of the vcsserver itself. |
|
29 | Exposed remote interface of the vcsserver itself. | |
30 |
|
30 | |||
31 | This object can be used to manage the server remotely. Right now the main |
|
31 | This object can be used to manage the server remotely. Right now the main | |
32 | use case is to allow to shut down the server. |
|
32 | use case is to allow to shut down the server. | |
33 | """ |
|
33 | """ | |
34 |
|
34 | |||
35 | _shutdown = False |
|
35 | _shutdown = False | |
36 |
|
36 | |||
37 | def shutdown(self): |
|
37 | def shutdown(self): | |
38 | self._shutdown = True |
|
38 | self._shutdown = True | |
39 |
|
39 | |||
40 | def ping(self): |
|
40 | def ping(self): | |
41 | """ |
|
41 | """ | |
42 | Utility to probe a server connection. |
|
42 | Utility to probe a server connection. | |
43 | """ |
|
43 | """ | |
44 | log.debug("Received server ping.") |
|
44 | log.debug("Received server ping.") | |
45 |
|
45 | |||
46 | def echo(self, data): |
|
46 | def echo(self, data): | |
47 | """ |
|
47 | """ | |
48 | Utility for performance testing. |
|
48 | Utility for performance testing. | |
49 |
|
49 | |||
50 | Allows to pass in arbitrary data and will return this data. |
|
50 | Allows to pass in arbitrary data and will return this data. | |
51 | """ |
|
51 | """ | |
52 | log.debug("Received server echo.") |
|
52 | log.debug("Received server echo.") | |
53 | return data |
|
53 | return data | |
54 |
|
54 | |||
55 | def sleep(self, seconds): |
|
55 | def sleep(self, seconds): | |
56 | """ |
|
56 | """ | |
57 | Utility to simulate long running server interaction. |
|
57 | Utility to simulate long running server interaction. | |
58 | """ |
|
58 | """ | |
59 | log.debug("Sleeping %s seconds", seconds) |
|
59 | log.debug("Sleeping %s seconds", seconds) | |
60 | time.sleep(seconds) |
|
60 | time.sleep(seconds) | |
61 |
|
61 | |||
62 | def get_pid(self): |
|
62 | def get_pid(self): | |
63 | """ |
|
63 | """ | |
64 | Allows to discover the PID based on a proxy object. |
|
64 | Allows to discover the PID based on a proxy object. | |
65 | """ |
|
65 | """ | |
66 | return os.getpid() |
|
66 | return os.getpid() | |
67 |
|
67 | |||
68 | def run_gc(self): |
|
68 | def run_gc(self): | |
69 | """ |
|
69 | """ | |
70 | Allows to trigger the garbage collector. |
|
70 | Allows to trigger the garbage collector. | |
71 |
|
71 | |||
72 | Main intention is to support statistics gathering during test runs. |
|
72 | Main intention is to support statistics gathering during test runs. | |
73 | """ |
|
73 | """ | |
74 | freed_objects = gc.collect() |
|
74 | freed_objects = gc.collect() | |
75 | return { |
|
75 | return { | |
76 | 'freed_objects': freed_objects, |
|
76 | 'freed_objects': freed_objects, | |
77 | 'garbage': len(gc.garbage), |
|
77 | 'garbage': len(gc.garbage), | |
78 | } |
|
78 | } |
@@ -1,563 +1,563 b'' | |||||
1 | """ |
|
1 | """ | |
2 | Module provides a class allowing to wrap communication over subprocess.Popen |
|
2 | Module provides a class allowing to wrap communication over subprocess.Popen | |
3 | input, output, error streams into a meaningfull, non-blocking, concurrent |
|
3 | input, output, error streams into a meaningfull, non-blocking, concurrent | |
4 | stream processor exposing the output data as an iterator fitting to be a |
|
4 | stream processor exposing the output data as an iterator fitting to be a | |
5 | return value passed by a WSGI applicaiton to a WSGI server per PEP 3333. |
|
5 | return value passed by a WSGI applicaiton to a WSGI server per PEP 3333. | |
6 |
|
6 | |||
7 | Copyright (c) 2011 Daniel Dotsenko <dotsa[at]hotmail.com> |
|
7 | Copyright (c) 2011 Daniel Dotsenko <dotsa[at]hotmail.com> | |
8 |
|
8 | |||
9 | This file is part of git_http_backend.py Project. |
|
9 | This file is part of git_http_backend.py Project. | |
10 |
|
10 | |||
11 | git_http_backend.py Project is free software: you can redistribute it and/or |
|
11 | git_http_backend.py Project is free software: you can redistribute it and/or | |
12 | modify it under the terms of the GNU Lesser General Public License as |
|
12 | modify it under the terms of the GNU Lesser General Public License as | |
13 | published by the Free Software Foundation, either version 2.1 of the License, |
|
13 | published by the Free Software Foundation, either version 2.1 of the License, | |
14 | or (at your option) any later version. |
|
14 | or (at your option) any later version. | |
15 |
|
15 | |||
16 | git_http_backend.py Project is distributed in the hope that it will be useful, |
|
16 | git_http_backend.py Project is distributed in the hope that it will be useful, | |
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | GNU Lesser General Public License for more details. |
|
19 | GNU Lesser General Public License for more details. | |
20 |
|
20 | |||
21 | You should have received a copy of the GNU Lesser General Public License |
|
21 | You should have received a copy of the GNU Lesser General Public License | |
22 | along with git_http_backend.py Project. |
|
22 | along with git_http_backend.py Project. | |
23 | If not, see <http://www.gnu.org/licenses/>. |
|
23 | If not, see <http://www.gnu.org/licenses/>. | |
24 | """ |
|
24 | """ | |
25 | import os |
|
25 | import os | |
26 | import collections |
|
26 | import collections | |
27 | import logging |
|
27 | import logging | |
28 | import subprocess |
|
28 | import subprocess | |
29 | import threading |
|
29 | import threading | |
30 |
|
30 | |||
31 | from vcsserver.str_utils import safe_str |
|
31 | from vcsserver.str_utils import safe_str | |
32 |
|
32 | |||
33 | log = logging.getLogger(__name__) |
|
33 | log = logging.getLogger(__name__) | |
34 |
|
34 | |||
35 |
|
35 | |||
36 | class StreamFeeder(threading.Thread): |
|
36 | class StreamFeeder(threading.Thread): | |
37 | """ |
|
37 | """ | |
38 | Normal writing into pipe-like is blocking once the buffer is filled. |
|
38 | Normal writing into pipe-like is blocking once the buffer is filled. | |
39 | This thread allows a thread to seep data from a file-like into a pipe |
|
39 | This thread allows a thread to seep data from a file-like into a pipe | |
40 | without blocking the main thread. |
|
40 | without blocking the main thread. | |
41 | We close inpipe once the end of the source stream is reached. |
|
41 | We close inpipe once the end of the source stream is reached. | |
42 | """ |
|
42 | """ | |
43 |
|
43 | |||
44 | def __init__(self, source): |
|
44 | def __init__(self, source): | |
45 | super().__init__() |
|
45 | super().__init__() | |
46 | self.daemon = True |
|
46 | self.daemon = True | |
47 | filelike = False |
|
47 | filelike = False | |
48 | self.bytes = b'' |
|
48 | self.bytes = b'' | |
49 | if type(source) in (str, bytes, bytearray): # string-like |
|
49 | if type(source) in (str, bytes, bytearray): # string-like | |
50 | self.bytes = bytes(source) |
|
50 | self.bytes = bytes(source) | |
51 | else: # can be either file pointer or file-like |
|
51 | else: # can be either file pointer or file-like | |
52 | if isinstance(source, int): # file pointer it is |
|
52 | if isinstance(source, int): # file pointer it is | |
53 | # converting file descriptor (int) stdin into file-like |
|
53 | # converting file descriptor (int) stdin into file-like | |
54 | source = os.fdopen(source, 'rb', 16384) |
|
54 | source = os.fdopen(source, 'rb', 16384) | |
55 | # let's see if source is file-like by now |
|
55 | # let's see if source is file-like by now | |
56 | filelike = hasattr(source, 'read') |
|
56 | filelike = hasattr(source, 'read') | |
57 | if not filelike and not self.bytes: |
|
57 | if not filelike and not self.bytes: | |
58 | raise TypeError("StreamFeeder's source object must be a readable " |
|
58 | raise TypeError("StreamFeeder's source object must be a readable " | |
59 | "file-like, a file descriptor, or a string-like.") |
|
59 | "file-like, a file descriptor, or a string-like.") | |
60 | self.source = source |
|
60 | self.source = source | |
61 | self.readiface, self.writeiface = os.pipe() |
|
61 | self.readiface, self.writeiface = os.pipe() | |
62 |
|
62 | |||
63 | def run(self): |
|
63 | def run(self): | |
64 | writer = self.writeiface |
|
64 | writer = self.writeiface | |
65 | try: |
|
65 | try: | |
66 | if self.bytes: |
|
66 | if self.bytes: | |
67 | os.write(writer, self.bytes) |
|
67 | os.write(writer, self.bytes) | |
68 | else: |
|
68 | else: | |
69 | s = self.source |
|
69 | s = self.source | |
70 |
|
70 | |||
71 | while 1: |
|
71 | while 1: | |
72 | _bytes = s.read(4096) |
|
72 | _bytes = s.read(4096) | |
73 | if not _bytes: |
|
73 | if not _bytes: | |
74 | break |
|
74 | break | |
75 | os.write(writer, _bytes) |
|
75 | os.write(writer, _bytes) | |
76 |
|
76 | |||
77 | finally: |
|
77 | finally: | |
78 | os.close(writer) |
|
78 | os.close(writer) | |
79 |
|
79 | |||
80 | @property |
|
80 | @property | |
81 | def output(self): |
|
81 | def output(self): | |
82 | return self.readiface |
|
82 | return self.readiface | |
83 |
|
83 | |||
84 |
|
84 | |||
85 | class InputStreamChunker(threading.Thread): |
|
85 | class InputStreamChunker(threading.Thread): | |
86 | def __init__(self, source, target, buffer_size, chunk_size): |
|
86 | def __init__(self, source, target, buffer_size, chunk_size): | |
87 |
|
87 | |||
88 | super().__init__() |
|
88 | super().__init__() | |
89 |
|
89 | |||
90 | self.daemon = True # die die die. |
|
90 | self.daemon = True # die die die. | |
91 |
|
91 | |||
92 | self.source = source |
|
92 | self.source = source | |
93 | self.target = target |
|
93 | self.target = target | |
94 | self.chunk_count_max = int(buffer_size / chunk_size) + 1 |
|
94 | self.chunk_count_max = int(buffer_size / chunk_size) + 1 | |
95 | self.chunk_size = chunk_size |
|
95 | self.chunk_size = chunk_size | |
96 |
|
96 | |||
97 | self.data_added = threading.Event() |
|
97 | self.data_added = threading.Event() | |
98 | self.data_added.clear() |
|
98 | self.data_added.clear() | |
99 |
|
99 | |||
100 | self.keep_reading = threading.Event() |
|
100 | self.keep_reading = threading.Event() | |
101 | self.keep_reading.set() |
|
101 | self.keep_reading.set() | |
102 |
|
102 | |||
103 | self.EOF = threading.Event() |
|
103 | self.EOF = threading.Event() | |
104 | self.EOF.clear() |
|
104 | self.EOF.clear() | |
105 |
|
105 | |||
106 | self.go = threading.Event() |
|
106 | self.go = threading.Event() | |
107 | self.go.set() |
|
107 | self.go.set() | |
108 |
|
108 | |||
109 | def stop(self): |
|
109 | def stop(self): | |
110 | self.go.clear() |
|
110 | self.go.clear() | |
111 | self.EOF.set() |
|
111 | self.EOF.set() | |
112 | try: |
|
112 | try: | |
113 | # this is not proper, but is done to force the reader thread let |
|
113 | # this is not proper, but is done to force the reader thread let | |
114 | # go of the input because, if successful, .close() will send EOF |
|
114 | # go of the input because, if successful, .close() will send EOF | |
115 | # down the pipe. |
|
115 | # down the pipe. | |
116 | self.source.close() |
|
116 | self.source.close() | |
117 | except Exception: |
|
117 | except Exception: | |
118 | pass |
|
118 | pass | |
119 |
|
119 | |||
120 | def run(self): |
|
120 | def run(self): | |
121 | s = self.source |
|
121 | s = self.source | |
122 | t = self.target |
|
122 | t = self.target | |
123 | cs = self.chunk_size |
|
123 | cs = self.chunk_size | |
124 | chunk_count_max = self.chunk_count_max |
|
124 | chunk_count_max = self.chunk_count_max | |
125 | keep_reading = self.keep_reading |
|
125 | keep_reading = self.keep_reading | |
126 | da = self.data_added |
|
126 | da = self.data_added | |
127 | go = self.go |
|
127 | go = self.go | |
128 |
|
128 | |||
129 | try: |
|
129 | try: | |
130 | b = s.read(cs) |
|
130 | b = s.read(cs) | |
131 | except ValueError: |
|
131 | except ValueError: | |
132 | b = '' |
|
132 | b = '' | |
133 |
|
133 | |||
134 | timeout_input = 20 |
|
134 | timeout_input = 20 | |
135 | while b and go.is_set(): |
|
135 | while b and go.is_set(): | |
136 | if len(t) > chunk_count_max: |
|
136 | if len(t) > chunk_count_max: | |
137 | keep_reading.clear() |
|
137 | keep_reading.clear() | |
138 | keep_reading.wait(timeout_input) |
|
138 | keep_reading.wait(timeout_input) | |
139 | if len(t) > chunk_count_max + timeout_input: |
|
139 | if len(t) > chunk_count_max + timeout_input: | |
140 | log.error("Timed out while waiting for input from subprocess.") |
|
140 | log.error("Timed out while waiting for input from subprocess.") | |
141 | os._exit(-1) # this will cause the worker to recycle itself |
|
141 | os._exit(-1) # this will cause the worker to recycle itself | |
142 |
|
142 | |||
143 | t.append(b) |
|
143 | t.append(b) | |
144 | da.set() |
|
144 | da.set() | |
145 |
|
145 | |||
146 | try: |
|
146 | try: | |
147 | b = s.read(cs) |
|
147 | b = s.read(cs) | |
148 | except ValueError: # probably "I/O operation on closed file" |
|
148 | except ValueError: # probably "I/O operation on closed file" | |
149 | b = '' |
|
149 | b = '' | |
150 |
|
150 | |||
151 | self.EOF.set() |
|
151 | self.EOF.set() | |
152 | da.set() # for cases when done but there was no input. |
|
152 | da.set() # for cases when done but there was no input. | |
153 |
|
153 | |||
154 |
|
154 | |||
155 |
class BufferedGenerator |
|
155 | class BufferedGenerator: | |
156 | """ |
|
156 | """ | |
157 | Class behaves as a non-blocking, buffered pipe reader. |
|
157 | Class behaves as a non-blocking, buffered pipe reader. | |
158 | Reads chunks of data (through a thread) |
|
158 | Reads chunks of data (through a thread) | |
159 | from a blocking pipe, and attaches these to an array (Deque) of chunks. |
|
159 | from a blocking pipe, and attaches these to an array (Deque) of chunks. | |
160 | Reading is halted in the thread when max chunks is internally buffered. |
|
160 | Reading is halted in the thread when max chunks is internally buffered. | |
161 | The .next() may operate in blocking or non-blocking fashion by yielding |
|
161 | The .next() may operate in blocking or non-blocking fashion by yielding | |
162 | '' if no data is ready |
|
162 | '' if no data is ready | |
163 | to be sent or by not returning until there is some data to send |
|
163 | to be sent or by not returning until there is some data to send | |
164 | When we get EOF from underlying source pipe we raise the marker to raise |
|
164 | When we get EOF from underlying source pipe we raise the marker to raise | |
165 | StopIteration after the last chunk of data is yielded. |
|
165 | StopIteration after the last chunk of data is yielded. | |
166 | """ |
|
166 | """ | |
167 |
|
167 | |||
168 | def __init__(self, name, source, buffer_size=65536, chunk_size=4096, |
|
168 | def __init__(self, name, source, buffer_size=65536, chunk_size=4096, | |
169 | starting_values=None, bottomless=False): |
|
169 | starting_values=None, bottomless=False): | |
170 | starting_values = starting_values or [] |
|
170 | starting_values = starting_values or [] | |
171 | self.name = name |
|
171 | self.name = name | |
172 | self.buffer_size = buffer_size |
|
172 | self.buffer_size = buffer_size | |
173 | self.chunk_size = chunk_size |
|
173 | self.chunk_size = chunk_size | |
174 |
|
174 | |||
175 | if bottomless: |
|
175 | if bottomless: | |
176 | maxlen = int(buffer_size / chunk_size) |
|
176 | maxlen = int(buffer_size / chunk_size) | |
177 | else: |
|
177 | else: | |
178 | maxlen = None |
|
178 | maxlen = None | |
179 |
|
179 | |||
180 | self.data_queue = collections.deque(starting_values, maxlen) |
|
180 | self.data_queue = collections.deque(starting_values, maxlen) | |
181 | self.worker = InputStreamChunker(source, self.data_queue, buffer_size, chunk_size) |
|
181 | self.worker = InputStreamChunker(source, self.data_queue, buffer_size, chunk_size) | |
182 | if starting_values: |
|
182 | if starting_values: | |
183 | self.worker.data_added.set() |
|
183 | self.worker.data_added.set() | |
184 | self.worker.start() |
|
184 | self.worker.start() | |
185 |
|
185 | |||
186 | #################### |
|
186 | #################### | |
187 | # Generator's methods |
|
187 | # Generator's methods | |
188 | #################### |
|
188 | #################### | |
189 | def __str__(self): |
|
189 | def __str__(self): | |
190 | return f'BufferedGenerator(name={self.name} chunk: {self.chunk_size} on buffer: {self.buffer_size})' |
|
190 | return f'BufferedGenerator(name={self.name} chunk: {self.chunk_size} on buffer: {self.buffer_size})' | |
191 |
|
191 | |||
192 | def __iter__(self): |
|
192 | def __iter__(self): | |
193 | return self |
|
193 | return self | |
194 |
|
194 | |||
195 | def __next__(self): |
|
195 | def __next__(self): | |
196 |
|
196 | |||
197 | while not self.length and not self.worker.EOF.is_set(): |
|
197 | while not self.length and not self.worker.EOF.is_set(): | |
198 | self.worker.data_added.clear() |
|
198 | self.worker.data_added.clear() | |
199 | self.worker.data_added.wait(0.2) |
|
199 | self.worker.data_added.wait(0.2) | |
200 |
|
200 | |||
201 | if self.length: |
|
201 | if self.length: | |
202 | self.worker.keep_reading.set() |
|
202 | self.worker.keep_reading.set() | |
203 | return bytes(self.data_queue.popleft()) |
|
203 | return bytes(self.data_queue.popleft()) | |
204 | elif self.worker.EOF.is_set(): |
|
204 | elif self.worker.EOF.is_set(): | |
205 | raise StopIteration |
|
205 | raise StopIteration | |
206 |
|
206 | |||
207 | def throw(self, exc_type, value=None, traceback=None): |
|
207 | def throw(self, exc_type, value=None, traceback=None): | |
208 | if not self.worker.EOF.is_set(): |
|
208 | if not self.worker.EOF.is_set(): | |
209 | raise exc_type(value) |
|
209 | raise exc_type(value) | |
210 |
|
210 | |||
211 | def start(self): |
|
211 | def start(self): | |
212 | self.worker.start() |
|
212 | self.worker.start() | |
213 |
|
213 | |||
214 | def stop(self): |
|
214 | def stop(self): | |
215 | self.worker.stop() |
|
215 | self.worker.stop() | |
216 |
|
216 | |||
217 | def close(self): |
|
217 | def close(self): | |
218 | try: |
|
218 | try: | |
219 | self.worker.stop() |
|
219 | self.worker.stop() | |
220 | self.throw(GeneratorExit) |
|
220 | self.throw(GeneratorExit) | |
221 | except (GeneratorExit, StopIteration): |
|
221 | except (GeneratorExit, StopIteration): | |
222 | pass |
|
222 | pass | |
223 |
|
223 | |||
224 | #################### |
|
224 | #################### | |
225 | # Threaded reader's infrastructure. |
|
225 | # Threaded reader's infrastructure. | |
226 | #################### |
|
226 | #################### | |
227 | @property |
|
227 | @property | |
228 | def input(self): |
|
228 | def input(self): | |
229 | return self.worker.w |
|
229 | return self.worker.w | |
230 |
|
230 | |||
231 | @property |
|
231 | @property | |
232 | def data_added_event(self): |
|
232 | def data_added_event(self): | |
233 | return self.worker.data_added |
|
233 | return self.worker.data_added | |
234 |
|
234 | |||
235 | @property |
|
235 | @property | |
236 | def data_added(self): |
|
236 | def data_added(self): | |
237 | return self.worker.data_added.is_set() |
|
237 | return self.worker.data_added.is_set() | |
238 |
|
238 | |||
239 | @property |
|
239 | @property | |
240 | def reading_paused(self): |
|
240 | def reading_paused(self): | |
241 | return not self.worker.keep_reading.is_set() |
|
241 | return not self.worker.keep_reading.is_set() | |
242 |
|
242 | |||
243 | @property |
|
243 | @property | |
244 | def done_reading_event(self): |
|
244 | def done_reading_event(self): | |
245 | """ |
|
245 | """ | |
246 | Done_reding does not mean that the iterator's buffer is empty. |
|
246 | Done_reding does not mean that the iterator's buffer is empty. | |
247 | Iterator might have done reading from underlying source, but the read |
|
247 | Iterator might have done reading from underlying source, but the read | |
248 | chunks might still be available for serving through .next() method. |
|
248 | chunks might still be available for serving through .next() method. | |
249 |
|
249 | |||
250 | :returns: An Event class instance. |
|
250 | :returns: An Event class instance. | |
251 | """ |
|
251 | """ | |
252 | return self.worker.EOF |
|
252 | return self.worker.EOF | |
253 |
|
253 | |||
254 | @property |
|
254 | @property | |
255 | def done_reading(self): |
|
255 | def done_reading(self): | |
256 | """ |
|
256 | """ | |
257 | Done_reading does not mean that the iterator's buffer is empty. |
|
257 | Done_reading does not mean that the iterator's buffer is empty. | |
258 | Iterator might have done reading from underlying source, but the read |
|
258 | Iterator might have done reading from underlying source, but the read | |
259 | chunks might still be available for serving through .next() method. |
|
259 | chunks might still be available for serving through .next() method. | |
260 |
|
260 | |||
261 | :returns: An Bool value. |
|
261 | :returns: An Bool value. | |
262 | """ |
|
262 | """ | |
263 | return self.worker.EOF.is_set() |
|
263 | return self.worker.EOF.is_set() | |
264 |
|
264 | |||
265 | @property |
|
265 | @property | |
266 | def length(self): |
|
266 | def length(self): | |
267 | """ |
|
267 | """ | |
268 | returns int. |
|
268 | returns int. | |
269 |
|
269 | |||
270 | This is the length of the queue of chunks, not the length of |
|
270 | This is the length of the queue of chunks, not the length of | |
271 | the combined contents in those chunks. |
|
271 | the combined contents in those chunks. | |
272 |
|
272 | |||
273 | __len__() cannot be meaningfully implemented because this |
|
273 | __len__() cannot be meaningfully implemented because this | |
274 | reader is just flying through a bottomless pit content and |
|
274 | reader is just flying through a bottomless pit content and | |
275 | can only know the length of what it already saw. |
|
275 | can only know the length of what it already saw. | |
276 |
|
276 | |||
277 | If __len__() on WSGI server per PEP 3333 returns a value, |
|
277 | If __len__() on WSGI server per PEP 3333 returns a value, | |
278 | the response's length will be set to that. In order not to |
|
278 | the response's length will be set to that. In order not to | |
279 | confuse WSGI PEP3333 servers, we will not implement __len__ |
|
279 | confuse WSGI PEP3333 servers, we will not implement __len__ | |
280 | at all. |
|
280 | at all. | |
281 | """ |
|
281 | """ | |
282 | return len(self.data_queue) |
|
282 | return len(self.data_queue) | |
283 |
|
283 | |||
284 | def prepend(self, x): |
|
284 | def prepend(self, x): | |
285 | self.data_queue.appendleft(x) |
|
285 | self.data_queue.appendleft(x) | |
286 |
|
286 | |||
287 | def append(self, x): |
|
287 | def append(self, x): | |
288 | self.data_queue.append(x) |
|
288 | self.data_queue.append(x) | |
289 |
|
289 | |||
290 | def extend(self, o): |
|
290 | def extend(self, o): | |
291 | self.data_queue.extend(o) |
|
291 | self.data_queue.extend(o) | |
292 |
|
292 | |||
293 | def __getitem__(self, i): |
|
293 | def __getitem__(self, i): | |
294 | return self.data_queue[i] |
|
294 | return self.data_queue[i] | |
295 |
|
295 | |||
296 |
|
296 | |||
297 |
class SubprocessIOChunker |
|
297 | class SubprocessIOChunker: | |
298 | """ |
|
298 | """ | |
299 | Processor class wrapping handling of subprocess IO. |
|
299 | Processor class wrapping handling of subprocess IO. | |
300 |
|
300 | |||
301 | .. important:: |
|
301 | .. important:: | |
302 |
|
302 | |||
303 | Watch out for the method `__del__` on this class. If this object |
|
303 | Watch out for the method `__del__` on this class. If this object | |
304 | is deleted, it will kill the subprocess, so avoid to |
|
304 | is deleted, it will kill the subprocess, so avoid to | |
305 | return the `output` attribute or usage of it like in the following |
|
305 | return the `output` attribute or usage of it like in the following | |
306 | example:: |
|
306 | example:: | |
307 |
|
307 | |||
308 | # `args` expected to run a program that produces a lot of output |
|
308 | # `args` expected to run a program that produces a lot of output | |
309 | output = ''.join(SubprocessIOChunker( |
|
309 | output = ''.join(SubprocessIOChunker( | |
310 | args, shell=False, inputstream=inputstream, env=environ).output) |
|
310 | args, shell=False, inputstream=inputstream, env=environ).output) | |
311 |
|
311 | |||
312 | # `output` will not contain all the data, because the __del__ method |
|
312 | # `output` will not contain all the data, because the __del__ method | |
313 | # has already killed the subprocess in this case before all output |
|
313 | # has already killed the subprocess in this case before all output | |
314 | # has been consumed. |
|
314 | # has been consumed. | |
315 |
|
315 | |||
316 |
|
316 | |||
317 |
|
317 | |||
318 | In a way, this is a "communicate()" replacement with a twist. |
|
318 | In a way, this is a "communicate()" replacement with a twist. | |
319 |
|
319 | |||
320 | - We are multithreaded. Writing in and reading out, err are all sep threads. |
|
320 | - We are multithreaded. Writing in and reading out, err are all sep threads. | |
321 | - We support concurrent (in and out) stream processing. |
|
321 | - We support concurrent (in and out) stream processing. | |
322 | - The output is not a stream. It's a queue of read string (bytes, not str) |
|
322 | - The output is not a stream. It's a queue of read string (bytes, not str) | |
323 | chunks. The object behaves as an iterable. You can "for chunk in obj:" us. |
|
323 | chunks. The object behaves as an iterable. You can "for chunk in obj:" us. | |
324 | - We are non-blocking in more respects than communicate() |
|
324 | - We are non-blocking in more respects than communicate() | |
325 | (reading from subprocess out pauses when internal buffer is full, but |
|
325 | (reading from subprocess out pauses when internal buffer is full, but | |
326 | does not block the parent calling code. On the flip side, reading from |
|
326 | does not block the parent calling code. On the flip side, reading from | |
327 | slow-yielding subprocess may block the iteration until data shows up. This |
|
327 | slow-yielding subprocess may block the iteration until data shows up. This | |
328 | does not block the parallel inpipe reading occurring parallel thread.) |
|
328 | does not block the parallel inpipe reading occurring parallel thread.) | |
329 |
|
329 | |||
330 | The purpose of the object is to allow us to wrap subprocess interactions into |
|
330 | The purpose of the object is to allow us to wrap subprocess interactions into | |
331 | an iterable that can be passed to a WSGI server as the application's return |
|
331 | an iterable that can be passed to a WSGI server as the application's return | |
332 | value. Because of stream-processing-ability, WSGI does not have to read ALL |
|
332 | value. Because of stream-processing-ability, WSGI does not have to read ALL | |
333 | of the subprocess's output and buffer it, before handing it to WSGI server for |
|
333 | of the subprocess's output and buffer it, before handing it to WSGI server for | |
334 | HTTP response. Instead, the class initializer reads just a bit of the stream |
|
334 | HTTP response. Instead, the class initializer reads just a bit of the stream | |
335 | to figure out if error occurred or likely to occur and if not, just hands the |
|
335 | to figure out if error occurred or likely to occur and if not, just hands the | |
336 | further iteration over subprocess output to the server for completion of HTTP |
|
336 | further iteration over subprocess output to the server for completion of HTTP | |
337 | response. |
|
337 | response. | |
338 |
|
338 | |||
339 | The real or perceived subprocess error is trapped and raised as one of |
|
339 | The real or perceived subprocess error is trapped and raised as one of | |
340 | OSError family of exceptions |
|
340 | OSError family of exceptions | |
341 |
|
341 | |||
342 | Example usage: |
|
342 | Example usage: | |
343 | # try: |
|
343 | # try: | |
344 | # answer = SubprocessIOChunker( |
|
344 | # answer = SubprocessIOChunker( | |
345 | # cmd, |
|
345 | # cmd, | |
346 | # input, |
|
346 | # input, | |
347 | # buffer_size = 65536, |
|
347 | # buffer_size = 65536, | |
348 | # chunk_size = 4096 |
|
348 | # chunk_size = 4096 | |
349 | # ) |
|
349 | # ) | |
350 | # except (OSError) as e: |
|
350 | # except (OSError) as e: | |
351 | # print str(e) |
|
351 | # print str(e) | |
352 | # raise e |
|
352 | # raise e | |
353 | # |
|
353 | # | |
354 | # return answer |
|
354 | # return answer | |
355 |
|
355 | |||
356 |
|
356 | |||
357 | """ |
|
357 | """ | |
358 |
|
358 | |||
359 | # TODO: johbo: This is used to make sure that the open end of the PIPE |
|
359 | # TODO: johbo: This is used to make sure that the open end of the PIPE | |
360 | # is closed in the end. It would be way better to wrap this into an |
|
360 | # is closed in the end. It would be way better to wrap this into an | |
361 | # object, so that it is closed automatically once it is consumed or |
|
361 | # object, so that it is closed automatically once it is consumed or | |
362 | # something similar. |
|
362 | # something similar. | |
363 | _close_input_fd = None |
|
363 | _close_input_fd = None | |
364 |
|
364 | |||
365 | _closed = False |
|
365 | _closed = False | |
366 | _stdout = None |
|
366 | _stdout = None | |
367 | _stderr = None |
|
367 | _stderr = None | |
368 |
|
368 | |||
369 | def __init__(self, cmd, input_stream=None, buffer_size=65536, |
|
369 | def __init__(self, cmd, input_stream=None, buffer_size=65536, | |
370 | chunk_size=4096, starting_values=None, fail_on_stderr=True, |
|
370 | chunk_size=4096, starting_values=None, fail_on_stderr=True, | |
371 | fail_on_return_code=True, **kwargs): |
|
371 | fail_on_return_code=True, **kwargs): | |
372 | """ |
|
372 | """ | |
373 | Initializes SubprocessIOChunker |
|
373 | Initializes SubprocessIOChunker | |
374 |
|
374 | |||
375 | :param cmd: A Subprocess.Popen style "cmd". Can be string or array of strings |
|
375 | :param cmd: A Subprocess.Popen style "cmd". Can be string or array of strings | |
376 | :param input_stream: (Default: None) A file-like, string, or file pointer. |
|
376 | :param input_stream: (Default: None) A file-like, string, or file pointer. | |
377 | :param buffer_size: (Default: 65536) A size of total buffer per stream in bytes. |
|
377 | :param buffer_size: (Default: 65536) A size of total buffer per stream in bytes. | |
378 | :param chunk_size: (Default: 4096) A max size of a chunk. Actual chunk may be smaller. |
|
378 | :param chunk_size: (Default: 4096) A max size of a chunk. Actual chunk may be smaller. | |
379 | :param starting_values: (Default: []) An array of strings to put in front of output que. |
|
379 | :param starting_values: (Default: []) An array of strings to put in front of output que. | |
380 | :param fail_on_stderr: (Default: True) Whether to raise an exception in |
|
380 | :param fail_on_stderr: (Default: True) Whether to raise an exception in | |
381 | case something is written to stderr. |
|
381 | case something is written to stderr. | |
382 | :param fail_on_return_code: (Default: True) Whether to raise an |
|
382 | :param fail_on_return_code: (Default: True) Whether to raise an | |
383 | exception if the return code is not 0. |
|
383 | exception if the return code is not 0. | |
384 | """ |
|
384 | """ | |
385 |
|
385 | |||
386 | kwargs['shell'] = kwargs.get('shell', True) |
|
386 | kwargs['shell'] = kwargs.get('shell', True) | |
387 |
|
387 | |||
388 | starting_values = starting_values or [] |
|
388 | starting_values = starting_values or [] | |
389 | if input_stream: |
|
389 | if input_stream: | |
390 | input_streamer = StreamFeeder(input_stream) |
|
390 | input_streamer = StreamFeeder(input_stream) | |
391 | input_streamer.start() |
|
391 | input_streamer.start() | |
392 | input_stream = input_streamer.output |
|
392 | input_stream = input_streamer.output | |
393 | self._close_input_fd = input_stream |
|
393 | self._close_input_fd = input_stream | |
394 |
|
394 | |||
395 | self._fail_on_stderr = fail_on_stderr |
|
395 | self._fail_on_stderr = fail_on_stderr | |
396 | self._fail_on_return_code = fail_on_return_code |
|
396 | self._fail_on_return_code = fail_on_return_code | |
397 | self.cmd = cmd |
|
397 | self.cmd = cmd | |
398 |
|
398 | |||
399 | _p = subprocess.Popen(cmd, bufsize=-1, stdin=input_stream, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
|
399 | _p = subprocess.Popen(cmd, bufsize=-1, stdin=input_stream, stdout=subprocess.PIPE, stderr=subprocess.PIPE, | |
400 | **kwargs) |
|
400 | **kwargs) | |
401 | self.process = _p |
|
401 | self.process = _p | |
402 |
|
402 | |||
403 | bg_out = BufferedGenerator('stdout', _p.stdout, buffer_size, chunk_size, starting_values) |
|
403 | bg_out = BufferedGenerator('stdout', _p.stdout, buffer_size, chunk_size, starting_values) | |
404 | bg_err = BufferedGenerator('stderr', _p.stderr, 10240, 1, bottomless=True) |
|
404 | bg_err = BufferedGenerator('stderr', _p.stderr, 10240, 1, bottomless=True) | |
405 |
|
405 | |||
406 | while not bg_out.done_reading and not bg_out.reading_paused and not bg_err.length: |
|
406 | while not bg_out.done_reading and not bg_out.reading_paused and not bg_err.length: | |
407 | # doing this until we reach either end of file, or end of buffer. |
|
407 | # doing this until we reach either end of file, or end of buffer. | |
408 | bg_out.data_added_event.wait(0.2) |
|
408 | bg_out.data_added_event.wait(0.2) | |
409 | bg_out.data_added_event.clear() |
|
409 | bg_out.data_added_event.clear() | |
410 |
|
410 | |||
411 | # at this point it's still ambiguous if we are done reading or just full buffer. |
|
411 | # at this point it's still ambiguous if we are done reading or just full buffer. | |
412 | # Either way, if error (returned by ended process, or implied based on |
|
412 | # Either way, if error (returned by ended process, or implied based on | |
413 | # presence of stuff in stderr output) we error out. |
|
413 | # presence of stuff in stderr output) we error out. | |
414 | # Else, we are happy. |
|
414 | # Else, we are happy. | |
415 | return_code = _p.poll() |
|
415 | return_code = _p.poll() | |
416 | ret_code_ok = return_code in [None, 0] |
|
416 | ret_code_ok = return_code in [None, 0] | |
417 | ret_code_fail = return_code is not None and return_code != 0 |
|
417 | ret_code_fail = return_code is not None and return_code != 0 | |
418 | if ( |
|
418 | if ( | |
419 | (ret_code_fail and fail_on_return_code) or |
|
419 | (ret_code_fail and fail_on_return_code) or | |
420 | (ret_code_ok and fail_on_stderr and bg_err.length) |
|
420 | (ret_code_ok and fail_on_stderr and bg_err.length) | |
421 | ): |
|
421 | ): | |
422 |
|
422 | |||
423 | try: |
|
423 | try: | |
424 | _p.terminate() |
|
424 | _p.terminate() | |
425 | except Exception: |
|
425 | except Exception: | |
426 | pass |
|
426 | pass | |
427 |
|
427 | |||
428 | bg_out.stop() |
|
428 | bg_out.stop() | |
429 | out = b''.join(bg_out) |
|
429 | out = b''.join(bg_out) | |
430 | self._stdout = out |
|
430 | self._stdout = out | |
431 |
|
431 | |||
432 | bg_err.stop() |
|
432 | bg_err.stop() | |
433 | err = b''.join(bg_err) |
|
433 | err = b''.join(bg_err) | |
434 | self._stderr = err |
|
434 | self._stderr = err | |
435 |
|
435 | |||
436 | # code from https://github.com/schacon/grack/pull/7 |
|
436 | # code from https://github.com/schacon/grack/pull/7 | |
437 | if err.strip() == b'fatal: The remote end hung up unexpectedly' and out.startswith(b'0034shallow '): |
|
437 | if err.strip() == b'fatal: The remote end hung up unexpectedly' and out.startswith(b'0034shallow '): | |
438 | bg_out = iter([out]) |
|
438 | bg_out = iter([out]) | |
439 | _p = None |
|
439 | _p = None | |
440 | elif err and fail_on_stderr: |
|
440 | elif err and fail_on_stderr: | |
441 | text_err = err.decode() |
|
441 | text_err = err.decode() | |
442 | raise OSError( |
|
442 | raise OSError( | |
443 | f"Subprocess exited due to an error:\n{text_err}") |
|
443 | f"Subprocess exited due to an error:\n{text_err}") | |
444 |
|
444 | |||
445 | if ret_code_fail and fail_on_return_code: |
|
445 | if ret_code_fail and fail_on_return_code: | |
446 | text_err = err.decode() |
|
446 | text_err = err.decode() | |
447 | if not err: |
|
447 | if not err: | |
448 | # maybe get empty stderr, try stdout instead |
|
448 | # maybe get empty stderr, try stdout instead | |
449 | # in many cases git reports the errors on stdout too |
|
449 | # in many cases git reports the errors on stdout too | |
450 | text_err = out.decode() |
|
450 | text_err = out.decode() | |
451 | raise OSError( |
|
451 | raise OSError( | |
452 | f"Subprocess exited with non 0 ret code:{return_code}: stderr:{text_err}") |
|
452 | f"Subprocess exited with non 0 ret code:{return_code}: stderr:{text_err}") | |
453 |
|
453 | |||
454 | self.stdout = bg_out |
|
454 | self.stdout = bg_out | |
455 | self.stderr = bg_err |
|
455 | self.stderr = bg_err | |
456 | self.inputstream = input_stream |
|
456 | self.inputstream = input_stream | |
457 |
|
457 | |||
458 | def __str__(self): |
|
458 | def __str__(self): | |
459 | proc = getattr(self, 'process', 'NO_PROCESS') |
|
459 | proc = getattr(self, 'process', 'NO_PROCESS') | |
460 | return f'SubprocessIOChunker: {proc}' |
|
460 | return f'SubprocessIOChunker: {proc}' | |
461 |
|
461 | |||
462 | def __iter__(self): |
|
462 | def __iter__(self): | |
463 | return self |
|
463 | return self | |
464 |
|
464 | |||
465 | def __next__(self): |
|
465 | def __next__(self): | |
466 | # Note: mikhail: We need to be sure that we are checking the return |
|
466 | # Note: mikhail: We need to be sure that we are checking the return | |
467 | # code after the stdout stream is closed. Some processes, e.g. git |
|
467 | # code after the stdout stream is closed. Some processes, e.g. git | |
468 | # are doing some magic in between closing stdout and terminating the |
|
468 | # are doing some magic in between closing stdout and terminating the | |
469 | # process and, as a result, we are not getting return code on "slow" |
|
469 | # process and, as a result, we are not getting return code on "slow" | |
470 | # systems. |
|
470 | # systems. | |
471 | result = None |
|
471 | result = None | |
472 | stop_iteration = None |
|
472 | stop_iteration = None | |
473 | try: |
|
473 | try: | |
474 | result = next(self.stdout) |
|
474 | result = next(self.stdout) | |
475 | except StopIteration as e: |
|
475 | except StopIteration as e: | |
476 | stop_iteration = e |
|
476 | stop_iteration = e | |
477 |
|
477 | |||
478 | if self.process: |
|
478 | if self.process: | |
479 | return_code = self.process.poll() |
|
479 | return_code = self.process.poll() | |
480 | ret_code_fail = return_code is not None and return_code != 0 |
|
480 | ret_code_fail = return_code is not None and return_code != 0 | |
481 | if ret_code_fail and self._fail_on_return_code: |
|
481 | if ret_code_fail and self._fail_on_return_code: | |
482 | self.stop_streams() |
|
482 | self.stop_streams() | |
483 | err = self.get_stderr() |
|
483 | err = self.get_stderr() | |
484 | raise OSError( |
|
484 | raise OSError( | |
485 | f"Subprocess exited (exit_code:{return_code}) due to an error during iteration:\n{err}") |
|
485 | f"Subprocess exited (exit_code:{return_code}) due to an error during iteration:\n{err}") | |
486 |
|
486 | |||
487 | if stop_iteration: |
|
487 | if stop_iteration: | |
488 | raise stop_iteration |
|
488 | raise stop_iteration | |
489 | return result |
|
489 | return result | |
490 |
|
490 | |||
491 | def throw(self, exc_type, value=None, traceback=None): |
|
491 | def throw(self, exc_type, value=None, traceback=None): | |
492 | if self.stdout.length or not self.stdout.done_reading: |
|
492 | if self.stdout.length or not self.stdout.done_reading: | |
493 | raise exc_type(value) |
|
493 | raise exc_type(value) | |
494 |
|
494 | |||
495 | def close(self): |
|
495 | def close(self): | |
496 | if self._closed: |
|
496 | if self._closed: | |
497 | return |
|
497 | return | |
498 |
|
498 | |||
499 | try: |
|
499 | try: | |
500 | self.process.terminate() |
|
500 | self.process.terminate() | |
501 | except Exception: |
|
501 | except Exception: | |
502 | pass |
|
502 | pass | |
503 | if self._close_input_fd: |
|
503 | if self._close_input_fd: | |
504 | os.close(self._close_input_fd) |
|
504 | os.close(self._close_input_fd) | |
505 | try: |
|
505 | try: | |
506 | self.stdout.close() |
|
506 | self.stdout.close() | |
507 | except Exception: |
|
507 | except Exception: | |
508 | pass |
|
508 | pass | |
509 | try: |
|
509 | try: | |
510 | self.stderr.close() |
|
510 | self.stderr.close() | |
511 | except Exception: |
|
511 | except Exception: | |
512 | pass |
|
512 | pass | |
513 | try: |
|
513 | try: | |
514 | os.close(self.inputstream) |
|
514 | os.close(self.inputstream) | |
515 | except Exception: |
|
515 | except Exception: | |
516 | pass |
|
516 | pass | |
517 |
|
517 | |||
518 | self._closed = True |
|
518 | self._closed = True | |
519 |
|
519 | |||
520 | def stop_streams(self): |
|
520 | def stop_streams(self): | |
521 | getattr(self.stdout, 'stop', lambda: None)() |
|
521 | getattr(self.stdout, 'stop', lambda: None)() | |
522 | getattr(self.stderr, 'stop', lambda: None)() |
|
522 | getattr(self.stderr, 'stop', lambda: None)() | |
523 |
|
523 | |||
524 | def get_stdout(self): |
|
524 | def get_stdout(self): | |
525 | if self._stdout: |
|
525 | if self._stdout: | |
526 | return self._stdout |
|
526 | return self._stdout | |
527 | else: |
|
527 | else: | |
528 | return b''.join(self.stdout) |
|
528 | return b''.join(self.stdout) | |
529 |
|
529 | |||
530 | def get_stderr(self): |
|
530 | def get_stderr(self): | |
531 | if self._stderr: |
|
531 | if self._stderr: | |
532 | return self._stderr |
|
532 | return self._stderr | |
533 | else: |
|
533 | else: | |
534 | return b''.join(self.stderr) |
|
534 | return b''.join(self.stderr) | |
535 |
|
535 | |||
536 |
|
536 | |||
537 | def run_command(arguments, env=None): |
|
537 | def run_command(arguments, env=None): | |
538 | """ |
|
538 | """ | |
539 | Run the specified command and return the stdout. |
|
539 | Run the specified command and return the stdout. | |
540 |
|
540 | |||
541 | :param arguments: sequence of program arguments (including the program name) |
|
541 | :param arguments: sequence of program arguments (including the program name) | |
542 | :type arguments: list[str] |
|
542 | :type arguments: list[str] | |
543 | """ |
|
543 | """ | |
544 |
|
544 | |||
545 | cmd = arguments |
|
545 | cmd = arguments | |
546 | log.debug('Running subprocessio command %s', cmd) |
|
546 | log.debug('Running subprocessio command %s', cmd) | |
547 | proc = None |
|
547 | proc = None | |
548 | try: |
|
548 | try: | |
549 | _opts = {'shell': False, 'fail_on_stderr': False} |
|
549 | _opts = {'shell': False, 'fail_on_stderr': False} | |
550 | if env: |
|
550 | if env: | |
551 | _opts.update({'env': env}) |
|
551 | _opts.update({'env': env}) | |
552 | proc = SubprocessIOChunker(cmd, **_opts) |
|
552 | proc = SubprocessIOChunker(cmd, **_opts) | |
553 | return b''.join(proc), b''.join(proc.stderr) |
|
553 | return b''.join(proc), b''.join(proc.stderr) | |
554 | except OSError as err: |
|
554 | except OSError as err: | |
555 | cmd = ' '.join(map(safe_str, cmd)) # human friendly CMD |
|
555 | cmd = ' '.join(map(safe_str, cmd)) # human friendly CMD | |
556 | tb_err = ("Couldn't run subprocessio command (%s).\n" |
|
556 | tb_err = ("Couldn't run subprocessio command (%s).\n" | |
557 | "Original error was:%s\n" % (cmd, err)) |
|
557 | "Original error was:%s\n" % (cmd, err)) | |
558 | log.exception(tb_err) |
|
558 | log.exception(tb_err) | |
559 | raise Exception(tb_err) |
|
559 | raise Exception(tb_err) | |
560 | finally: |
|
560 | finally: | |
561 | if proc: |
|
561 | if proc: | |
562 | proc.close() |
|
562 | proc.close() | |
563 |
|
563 |
@@ -1,85 +1,85 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import os |
|
18 | import os | |
19 | import shutil |
|
19 | import shutil | |
20 | import tempfile |
|
20 | import tempfile | |
21 | import configparser |
|
21 | import configparser | |
22 |
|
22 | |||
23 |
|
23 | |||
24 |
class ContextINI |
|
24 | class ContextINI: | |
25 | """ |
|
25 | """ | |
26 | Allows to create a new test.ini file as a copy of existing one with edited |
|
26 | Allows to create a new test.ini file as a copy of existing one with edited | |
27 | data. If existing file is not present, it creates a new one. Example usage:: |
|
27 | data. If existing file is not present, it creates a new one. Example usage:: | |
28 |
|
28 | |||
29 | with TestINI('test.ini', [{'section': {'key': 'val'}}]) as new_test_ini_path: |
|
29 | with TestINI('test.ini', [{'section': {'key': 'val'}}]) as new_test_ini_path: | |
30 | print 'vcsserver --config=%s' % new_test_ini |
|
30 | print 'vcsserver --config=%s' % new_test_ini | |
31 | """ |
|
31 | """ | |
32 |
|
32 | |||
33 | def __init__(self, ini_file_path, ini_params, new_file_prefix=None, |
|
33 | def __init__(self, ini_file_path, ini_params, new_file_prefix=None, | |
34 | destroy=True): |
|
34 | destroy=True): | |
35 | self.ini_file_path = ini_file_path |
|
35 | self.ini_file_path = ini_file_path | |
36 | self.ini_params = ini_params |
|
36 | self.ini_params = ini_params | |
37 | self.new_path = None |
|
37 | self.new_path = None | |
38 | self.new_path_prefix = new_file_prefix or 'test' |
|
38 | self.new_path_prefix = new_file_prefix or 'test' | |
39 | self.destroy = destroy |
|
39 | self.destroy = destroy | |
40 |
|
40 | |||
41 | def __enter__(self): |
|
41 | def __enter__(self): | |
42 | _, pref = tempfile.mkstemp() |
|
42 | _, pref = tempfile.mkstemp() | |
43 | loc = tempfile.gettempdir() |
|
43 | loc = tempfile.gettempdir() | |
44 | self.new_path = os.path.join(loc, '{}_{}_{}'.format( |
|
44 | self.new_path = os.path.join(loc, '{}_{}_{}'.format( | |
45 | pref, self.new_path_prefix, self.ini_file_path)) |
|
45 | pref, self.new_path_prefix, self.ini_file_path)) | |
46 |
|
46 | |||
47 | # copy ini file and modify according to the params, if we re-use a file |
|
47 | # copy ini file and modify according to the params, if we re-use a file | |
48 | if os.path.isfile(self.ini_file_path): |
|
48 | if os.path.isfile(self.ini_file_path): | |
49 | shutil.copy(self.ini_file_path, self.new_path) |
|
49 | shutil.copy(self.ini_file_path, self.new_path) | |
50 | else: |
|
50 | else: | |
51 | # create new dump file for configObj to write to. |
|
51 | # create new dump file for configObj to write to. | |
52 | with open(self.new_path, 'wb'): |
|
52 | with open(self.new_path, 'wb'): | |
53 | pass |
|
53 | pass | |
54 |
|
54 | |||
55 | parser = configparser.ConfigParser() |
|
55 | parser = configparser.ConfigParser() | |
56 | parser.read(self.ini_file_path) |
|
56 | parser.read(self.ini_file_path) | |
57 |
|
57 | |||
58 | for data in self.ini_params: |
|
58 | for data in self.ini_params: | |
59 | section, ini_params = list(data.items())[0] |
|
59 | section, ini_params = list(data.items())[0] | |
60 | key, val = list(ini_params.items())[0] |
|
60 | key, val = list(ini_params.items())[0] | |
61 | if section not in parser: |
|
61 | if section not in parser: | |
62 | parser[section] = {} |
|
62 | parser[section] = {} | |
63 | parser[section][key] = val |
|
63 | parser[section][key] = val | |
64 | with open(self.ini_file_path, 'w') as f: |
|
64 | with open(self.ini_file_path, 'w') as f: | |
65 | parser.write(f) |
|
65 | parser.write(f) | |
66 | return self.new_path |
|
66 | return self.new_path | |
67 |
|
67 | |||
68 | def __exit__(self, exc_type, exc_val, exc_tb): |
|
68 | def __exit__(self, exc_type, exc_val, exc_tb): | |
69 | if self.destroy: |
|
69 | if self.destroy: | |
70 | os.remove(self.new_path) |
|
70 | os.remove(self.new_path) | |
71 |
|
71 | |||
72 |
|
72 | |||
73 | def no_newline_id_generator(test_name): |
|
73 | def no_newline_id_generator(test_name): | |
74 | """ |
|
74 | """ | |
75 | Generates a test name without spaces or newlines characters. Used for |
|
75 | Generates a test name without spaces or newlines characters. Used for | |
76 | nicer output of progress of test |
|
76 | nicer output of progress of test | |
77 | """ |
|
77 | """ | |
78 | org_name = test_name |
|
78 | org_name = test_name | |
79 | test_name = str(test_name)\ |
|
79 | test_name = str(test_name)\ | |
80 | .replace('\n', '_N') \ |
|
80 | .replace('\n', '_N') \ | |
81 | .replace('\r', '_N') \ |
|
81 | .replace('\r', '_N') \ | |
82 | .replace('\t', '_T') \ |
|
82 | .replace('\t', '_T') \ | |
83 | .replace(' ', '_S') |
|
83 | .replace(' ', '_S') | |
84 |
|
84 | |||
85 | return test_name or 'test-with-empty-name' |
|
85 | return test_name or 'test-with-empty-name' |
@@ -1,162 +1,162 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import inspect |
|
18 | import inspect | |
19 |
|
19 | |||
20 | import pytest |
|
20 | import pytest | |
21 | import dulwich.errors |
|
21 | import dulwich.errors | |
22 | from mock import Mock, patch |
|
22 | from mock import Mock, patch | |
23 |
|
23 | |||
24 | from vcsserver.remote import git_remote |
|
24 | from vcsserver.remote import git_remote | |
25 |
|
25 | |||
26 | SAMPLE_REFS = { |
|
26 | SAMPLE_REFS = { | |
27 | 'HEAD': 'fd627b9e0dd80b47be81af07c4a98518244ed2f7', |
|
27 | 'HEAD': 'fd627b9e0dd80b47be81af07c4a98518244ed2f7', | |
28 | 'refs/tags/v0.1.9': '341d28f0eec5ddf0b6b77871e13c2bbd6bec685c', |
|
28 | 'refs/tags/v0.1.9': '341d28f0eec5ddf0b6b77871e13c2bbd6bec685c', | |
29 | 'refs/tags/v0.1.8': '74ebce002c088b8a5ecf40073db09375515ecd68', |
|
29 | 'refs/tags/v0.1.8': '74ebce002c088b8a5ecf40073db09375515ecd68', | |
30 | 'refs/tags/v0.1.1': 'e6ea6d16e2f26250124a1f4b4fe37a912f9d86a0', |
|
30 | 'refs/tags/v0.1.1': 'e6ea6d16e2f26250124a1f4b4fe37a912f9d86a0', | |
31 | 'refs/tags/v0.1.3': '5a3a8fb005554692b16e21dee62bf02667d8dc3e', |
|
31 | 'refs/tags/v0.1.3': '5a3a8fb005554692b16e21dee62bf02667d8dc3e', | |
32 | } |
|
32 | } | |
33 |
|
33 | |||
34 |
|
34 | |||
35 | @pytest.fixture |
|
35 | @pytest.fixture | |
36 | def git_remote_fix(): |
|
36 | def git_remote_fix(): | |
37 | """ |
|
37 | """ | |
38 | A GitRemote instance with a mock factory. |
|
38 | A GitRemote instance with a mock factory. | |
39 | """ |
|
39 | """ | |
40 | factory = Mock() |
|
40 | factory = Mock() | |
41 | remote = git_remote.GitRemote(factory) |
|
41 | remote = git_remote.GitRemote(factory) | |
42 | return remote |
|
42 | return remote | |
43 |
|
43 | |||
44 |
|
44 | |||
45 | def test_discover_git_version(git_remote_fix): |
|
45 | def test_discover_git_version(git_remote_fix): | |
46 | version = git_remote_fix.discover_git_version() |
|
46 | version = git_remote_fix.discover_git_version() | |
47 | assert version |
|
47 | assert version | |
48 |
|
48 | |||
49 |
|
49 | |||
50 |
class TestGitFetch |
|
50 | class TestGitFetch: | |
51 | def setup_method(self): |
|
51 | def setup_method(self): | |
52 | self.mock_repo = Mock() |
|
52 | self.mock_repo = Mock() | |
53 | factory = Mock() |
|
53 | factory = Mock() | |
54 | factory.repo = Mock(return_value=self.mock_repo) |
|
54 | factory.repo = Mock(return_value=self.mock_repo) | |
55 | self.remote_git = git_remote.GitRemote(factory) |
|
55 | self.remote_git = git_remote.GitRemote(factory) | |
56 |
|
56 | |||
57 | def test_fetches_all_when_no_commit_ids_specified(self): |
|
57 | def test_fetches_all_when_no_commit_ids_specified(self): | |
58 | def side_effect(determine_wants, *args, **kwargs): |
|
58 | def side_effect(determine_wants, *args, **kwargs): | |
59 | determine_wants(SAMPLE_REFS) |
|
59 | determine_wants(SAMPLE_REFS) | |
60 |
|
60 | |||
61 | with patch('dulwich.client.LocalGitClient.fetch') as mock_fetch: |
|
61 | with patch('dulwich.client.LocalGitClient.fetch') as mock_fetch: | |
62 | mock_fetch.side_effect = side_effect |
|
62 | mock_fetch.side_effect = side_effect | |
63 | self.remote_git.pull(wire={}, url='/tmp/', apply_refs=False) |
|
63 | self.remote_git.pull(wire={}, url='/tmp/', apply_refs=False) | |
64 | determine_wants = self.mock_repo.object_store.determine_wants_all |
|
64 | determine_wants = self.mock_repo.object_store.determine_wants_all | |
65 | determine_wants.assert_called_once_with(SAMPLE_REFS) |
|
65 | determine_wants.assert_called_once_with(SAMPLE_REFS) | |
66 |
|
66 | |||
67 | def test_fetches_specified_commits(self): |
|
67 | def test_fetches_specified_commits(self): | |
68 | selected_refs = { |
|
68 | selected_refs = { | |
69 | 'refs/tags/v0.1.8': b'74ebce002c088b8a5ecf40073db09375515ecd68', |
|
69 | 'refs/tags/v0.1.8': b'74ebce002c088b8a5ecf40073db09375515ecd68', | |
70 | 'refs/tags/v0.1.3': b'5a3a8fb005554692b16e21dee62bf02667d8dc3e', |
|
70 | 'refs/tags/v0.1.3': b'5a3a8fb005554692b16e21dee62bf02667d8dc3e', | |
71 | } |
|
71 | } | |
72 |
|
72 | |||
73 | def side_effect(determine_wants, *args, **kwargs): |
|
73 | def side_effect(determine_wants, *args, **kwargs): | |
74 | result = determine_wants(SAMPLE_REFS) |
|
74 | result = determine_wants(SAMPLE_REFS) | |
75 | assert sorted(result) == sorted(selected_refs.values()) |
|
75 | assert sorted(result) == sorted(selected_refs.values()) | |
76 | return result |
|
76 | return result | |
77 |
|
77 | |||
78 | with patch('dulwich.client.LocalGitClient.fetch') as mock_fetch: |
|
78 | with patch('dulwich.client.LocalGitClient.fetch') as mock_fetch: | |
79 | mock_fetch.side_effect = side_effect |
|
79 | mock_fetch.side_effect = side_effect | |
80 | self.remote_git.pull( |
|
80 | self.remote_git.pull( | |
81 | wire={}, url='/tmp/', apply_refs=False, |
|
81 | wire={}, url='/tmp/', apply_refs=False, | |
82 | refs=list(selected_refs.keys())) |
|
82 | refs=list(selected_refs.keys())) | |
83 | determine_wants = self.mock_repo.object_store.determine_wants_all |
|
83 | determine_wants = self.mock_repo.object_store.determine_wants_all | |
84 | assert determine_wants.call_count == 0 |
|
84 | assert determine_wants.call_count == 0 | |
85 |
|
85 | |||
86 | def test_get_remote_refs(self): |
|
86 | def test_get_remote_refs(self): | |
87 | factory = Mock() |
|
87 | factory = Mock() | |
88 | remote_git = git_remote.GitRemote(factory) |
|
88 | remote_git = git_remote.GitRemote(factory) | |
89 | url = 'https://example.com/test/test.git' |
|
89 | url = 'https://example.com/test/test.git' | |
90 | sample_refs = { |
|
90 | sample_refs = { | |
91 | 'refs/tags/v0.1.8': '74ebce002c088b8a5ecf40073db09375515ecd68', |
|
91 | 'refs/tags/v0.1.8': '74ebce002c088b8a5ecf40073db09375515ecd68', | |
92 | 'refs/tags/v0.1.3': '5a3a8fb005554692b16e21dee62bf02667d8dc3e', |
|
92 | 'refs/tags/v0.1.3': '5a3a8fb005554692b16e21dee62bf02667d8dc3e', | |
93 | } |
|
93 | } | |
94 |
|
94 | |||
95 | with patch('vcsserver.remote.git_remote.Repo', create=False) as mock_repo: |
|
95 | with patch('vcsserver.remote.git_remote.Repo', create=False) as mock_repo: | |
96 | mock_repo().get_refs.return_value = sample_refs |
|
96 | mock_repo().get_refs.return_value = sample_refs | |
97 | remote_refs = remote_git.get_remote_refs(wire={}, url=url) |
|
97 | remote_refs = remote_git.get_remote_refs(wire={}, url=url) | |
98 | mock_repo().get_refs.assert_called_once_with() |
|
98 | mock_repo().get_refs.assert_called_once_with() | |
99 | assert remote_refs == sample_refs |
|
99 | assert remote_refs == sample_refs | |
100 |
|
100 | |||
101 |
|
101 | |||
102 |
class TestReraiseSafeExceptions |
|
102 | class TestReraiseSafeExceptions: | |
103 |
|
103 | |||
104 | def test_method_decorated_with_reraise_safe_exceptions(self): |
|
104 | def test_method_decorated_with_reraise_safe_exceptions(self): | |
105 | factory = Mock() |
|
105 | factory = Mock() | |
106 | git_remote_instance = git_remote.GitRemote(factory) |
|
106 | git_remote_instance = git_remote.GitRemote(factory) | |
107 |
|
107 | |||
108 | def fake_function(): |
|
108 | def fake_function(): | |
109 | return None |
|
109 | return None | |
110 |
|
110 | |||
111 | decorator = git_remote.reraise_safe_exceptions(fake_function) |
|
111 | decorator = git_remote.reraise_safe_exceptions(fake_function) | |
112 |
|
112 | |||
113 | methods = inspect.getmembers(git_remote_instance, predicate=inspect.ismethod) |
|
113 | methods = inspect.getmembers(git_remote_instance, predicate=inspect.ismethod) | |
114 | for method_name, method in methods: |
|
114 | for method_name, method in methods: | |
115 | if not method_name.startswith('_') and method_name not in ['vcsserver_invalidate_cache']: |
|
115 | if not method_name.startswith('_') and method_name not in ['vcsserver_invalidate_cache']: | |
116 | assert method.__func__.__code__ == decorator.__code__ |
|
116 | assert method.__func__.__code__ == decorator.__code__ | |
117 |
|
117 | |||
118 | @pytest.mark.parametrize('side_effect, expected_type', [ |
|
118 | @pytest.mark.parametrize('side_effect, expected_type', [ | |
119 | (dulwich.errors.ChecksumMismatch('0000000', 'deadbeef'), 'lookup'), |
|
119 | (dulwich.errors.ChecksumMismatch('0000000', 'deadbeef'), 'lookup'), | |
120 | (dulwich.errors.NotCommitError('deadbeef'), 'lookup'), |
|
120 | (dulwich.errors.NotCommitError('deadbeef'), 'lookup'), | |
121 | (dulwich.errors.MissingCommitError('deadbeef'), 'lookup'), |
|
121 | (dulwich.errors.MissingCommitError('deadbeef'), 'lookup'), | |
122 | (dulwich.errors.ObjectMissing('deadbeef'), 'lookup'), |
|
122 | (dulwich.errors.ObjectMissing('deadbeef'), 'lookup'), | |
123 | (dulwich.errors.HangupException(), 'error'), |
|
123 | (dulwich.errors.HangupException(), 'error'), | |
124 | (dulwich.errors.UnexpectedCommandError('test-cmd'), 'error'), |
|
124 | (dulwich.errors.UnexpectedCommandError('test-cmd'), 'error'), | |
125 | ]) |
|
125 | ]) | |
126 | def test_safe_exceptions_reraised(self, side_effect, expected_type): |
|
126 | def test_safe_exceptions_reraised(self, side_effect, expected_type): | |
127 | @git_remote.reraise_safe_exceptions |
|
127 | @git_remote.reraise_safe_exceptions | |
128 | def fake_method(): |
|
128 | def fake_method(): | |
129 | raise side_effect |
|
129 | raise side_effect | |
130 |
|
130 | |||
131 | with pytest.raises(Exception) as exc_info: |
|
131 | with pytest.raises(Exception) as exc_info: | |
132 | fake_method() |
|
132 | fake_method() | |
133 | assert type(exc_info.value) == Exception |
|
133 | assert type(exc_info.value) == Exception | |
134 | assert exc_info.value._vcs_kind == expected_type |
|
134 | assert exc_info.value._vcs_kind == expected_type | |
135 |
|
135 | |||
136 |
|
136 | |||
137 |
class TestDulwichRepoWrapper |
|
137 | class TestDulwichRepoWrapper: | |
138 | def test_calls_close_on_delete(self): |
|
138 | def test_calls_close_on_delete(self): | |
139 | isdir_patcher = patch('dulwich.repo.os.path.isdir', return_value=True) |
|
139 | isdir_patcher = patch('dulwich.repo.os.path.isdir', return_value=True) | |
140 | with patch.object(git_remote.Repo, 'close') as close_mock: |
|
140 | with patch.object(git_remote.Repo, 'close') as close_mock: | |
141 | with isdir_patcher: |
|
141 | with isdir_patcher: | |
142 | repo = git_remote.Repo('/tmp/abcde') |
|
142 | repo = git_remote.Repo('/tmp/abcde') | |
143 | assert repo is not None |
|
143 | assert repo is not None | |
144 | repo.__del__() |
|
144 | repo.__del__() | |
145 | # can't use del repo as in python3 this isn't always calling .__del__() |
|
145 | # can't use del repo as in python3 this isn't always calling .__del__() | |
146 |
|
146 | |||
147 | close_mock.assert_called_once_with() |
|
147 | close_mock.assert_called_once_with() | |
148 |
|
148 | |||
149 |
|
149 | |||
150 |
class TestGitFactory |
|
150 | class TestGitFactory: | |
151 | def test_create_repo_returns_dulwich_wrapper(self): |
|
151 | def test_create_repo_returns_dulwich_wrapper(self): | |
152 |
|
152 | |||
153 | with patch('vcsserver.lib.rc_cache.region_meta.dogpile_cache_regions') as mock: |
|
153 | with patch('vcsserver.lib.rc_cache.region_meta.dogpile_cache_regions') as mock: | |
154 | mock.side_effect = {'repo_objects': ''} |
|
154 | mock.side_effect = {'repo_objects': ''} | |
155 | factory = git_remote.GitFactory() |
|
155 | factory = git_remote.GitFactory() | |
156 | wire = { |
|
156 | wire = { | |
157 | 'path': '/tmp/abcde' |
|
157 | 'path': '/tmp/abcde' | |
158 | } |
|
158 | } | |
159 | isdir_patcher = patch('dulwich.repo.os.path.isdir', return_value=True) |
|
159 | isdir_patcher = patch('dulwich.repo.os.path.isdir', return_value=True) | |
160 | with isdir_patcher: |
|
160 | with isdir_patcher: | |
161 | result = factory._create_repo(wire, True) |
|
161 | result = factory._create_repo(wire, True) | |
162 | assert isinstance(result, git_remote.Repo) |
|
162 | assert isinstance(result, git_remote.Repo) |
@@ -1,112 +1,112 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import inspect |
|
18 | import inspect | |
19 | import sys |
|
19 | import sys | |
20 | import traceback |
|
20 | import traceback | |
21 |
|
21 | |||
22 | import pytest |
|
22 | import pytest | |
23 | from mercurial.error import LookupError |
|
23 | from mercurial.error import LookupError | |
24 | from mock import Mock, patch |
|
24 | from mock import Mock, patch | |
25 |
|
25 | |||
26 | from vcsserver import exceptions, hgcompat |
|
26 | from vcsserver import exceptions, hgcompat | |
27 | from vcsserver.remote import hg_remote |
|
27 | from vcsserver.remote import hg_remote | |
28 |
|
28 | |||
29 |
|
29 | |||
30 |
class TestDiff |
|
30 | class TestDiff: | |
31 | def test_raising_safe_exception_when_lookup_failed(self): |
|
31 | def test_raising_safe_exception_when_lookup_failed(self): | |
32 |
|
32 | |||
33 | factory = Mock() |
|
33 | factory = Mock() | |
34 | hg_remote_instance = hg_remote.HgRemote(factory) |
|
34 | hg_remote_instance = hg_remote.HgRemote(factory) | |
35 | with patch('mercurial.patch.diff') as diff_mock: |
|
35 | with patch('mercurial.patch.diff') as diff_mock: | |
36 | diff_mock.side_effect = LookupError(b'deadbeef', b'index', b'message') |
|
36 | diff_mock.side_effect = LookupError(b'deadbeef', b'index', b'message') | |
37 |
|
37 | |||
38 | with pytest.raises(Exception) as exc_info: |
|
38 | with pytest.raises(Exception) as exc_info: | |
39 | hg_remote_instance.diff( |
|
39 | hg_remote_instance.diff( | |
40 | wire={}, commit_id_1='deadbeef', commit_id_2='deadbee1', |
|
40 | wire={}, commit_id_1='deadbeef', commit_id_2='deadbee1', | |
41 | file_filter=None, opt_git=True, opt_ignorews=True, |
|
41 | file_filter=None, opt_git=True, opt_ignorews=True, | |
42 | context=3) |
|
42 | context=3) | |
43 | assert type(exc_info.value) == Exception |
|
43 | assert type(exc_info.value) == Exception | |
44 | assert exc_info.value._vcs_kind == 'lookup' |
|
44 | assert exc_info.value._vcs_kind == 'lookup' | |
45 |
|
45 | |||
46 |
|
46 | |||
47 |
class TestReraiseSafeExceptions |
|
47 | class TestReraiseSafeExceptions: | |
48 | original_traceback = None |
|
48 | original_traceback = None | |
49 |
|
49 | |||
50 | def test_method_decorated_with_reraise_safe_exceptions(self): |
|
50 | def test_method_decorated_with_reraise_safe_exceptions(self): | |
51 | factory = Mock() |
|
51 | factory = Mock() | |
52 | hg_remote_instance = hg_remote.HgRemote(factory) |
|
52 | hg_remote_instance = hg_remote.HgRemote(factory) | |
53 | methods = inspect.getmembers(hg_remote_instance, predicate=inspect.ismethod) |
|
53 | methods = inspect.getmembers(hg_remote_instance, predicate=inspect.ismethod) | |
54 | decorator = hg_remote.reraise_safe_exceptions(None) |
|
54 | decorator = hg_remote.reraise_safe_exceptions(None) | |
55 | for method_name, method in methods: |
|
55 | for method_name, method in methods: | |
56 | if not method_name.startswith('_') and method_name not in ['vcsserver_invalidate_cache']: |
|
56 | if not method_name.startswith('_') and method_name not in ['vcsserver_invalidate_cache']: | |
57 | assert method.__func__.__code__ == decorator.__code__ |
|
57 | assert method.__func__.__code__ == decorator.__code__ | |
58 |
|
58 | |||
59 | @pytest.mark.parametrize('side_effect, expected_type', [ |
|
59 | @pytest.mark.parametrize('side_effect, expected_type', [ | |
60 | (hgcompat.Abort(b'failed-abort'), 'abort'), |
|
60 | (hgcompat.Abort(b'failed-abort'), 'abort'), | |
61 | (hgcompat.InterventionRequired(b'intervention-required'), 'abort'), |
|
61 | (hgcompat.InterventionRequired(b'intervention-required'), 'abort'), | |
62 | (hgcompat.RepoLookupError(), 'lookup'), |
|
62 | (hgcompat.RepoLookupError(), 'lookup'), | |
63 | (hgcompat.LookupError(b'deadbeef', b'index', b'message'), 'lookup'), |
|
63 | (hgcompat.LookupError(b'deadbeef', b'index', b'message'), 'lookup'), | |
64 | (hgcompat.RepoError(), 'error'), |
|
64 | (hgcompat.RepoError(), 'error'), | |
65 | (hgcompat.RequirementError(), 'requirement'), |
|
65 | (hgcompat.RequirementError(), 'requirement'), | |
66 | ]) |
|
66 | ]) | |
67 | def test_safe_exceptions_reraised(self, side_effect, expected_type): |
|
67 | def test_safe_exceptions_reraised(self, side_effect, expected_type): | |
68 | @hg_remote.reraise_safe_exceptions |
|
68 | @hg_remote.reraise_safe_exceptions | |
69 | def fake_method(): |
|
69 | def fake_method(): | |
70 | raise side_effect |
|
70 | raise side_effect | |
71 |
|
71 | |||
72 | with pytest.raises(Exception) as exc_info: |
|
72 | with pytest.raises(Exception) as exc_info: | |
73 | fake_method() |
|
73 | fake_method() | |
74 | assert type(exc_info.value) == Exception |
|
74 | assert type(exc_info.value) == Exception | |
75 | assert exc_info.value._vcs_kind == expected_type |
|
75 | assert exc_info.value._vcs_kind == expected_type | |
76 |
|
76 | |||
77 | def test_keeps_original_traceback(self): |
|
77 | def test_keeps_original_traceback(self): | |
78 |
|
78 | |||
79 | @hg_remote.reraise_safe_exceptions |
|
79 | @hg_remote.reraise_safe_exceptions | |
80 | def fake_method(): |
|
80 | def fake_method(): | |
81 | try: |
|
81 | try: | |
82 | raise hgcompat.Abort(b'test-abort') |
|
82 | raise hgcompat.Abort(b'test-abort') | |
83 | except: |
|
83 | except: | |
84 | self.original_traceback = traceback.format_tb(sys.exc_info()[2]) |
|
84 | self.original_traceback = traceback.format_tb(sys.exc_info()[2]) | |
85 | raise |
|
85 | raise | |
86 |
|
86 | |||
87 | new_traceback = None |
|
87 | new_traceback = None | |
88 | try: |
|
88 | try: | |
89 | fake_method() |
|
89 | fake_method() | |
90 | except Exception: |
|
90 | except Exception: | |
91 | new_traceback = traceback.format_tb(sys.exc_info()[2]) |
|
91 | new_traceback = traceback.format_tb(sys.exc_info()[2]) | |
92 |
|
92 | |||
93 | new_traceback_tail = new_traceback[-len(self.original_traceback):] |
|
93 | new_traceback_tail = new_traceback[-len(self.original_traceback):] | |
94 | assert new_traceback_tail == self.original_traceback |
|
94 | assert new_traceback_tail == self.original_traceback | |
95 |
|
95 | |||
96 | def test_maps_unknown_exceptions_to_unhandled(self): |
|
96 | def test_maps_unknown_exceptions_to_unhandled(self): | |
97 | @hg_remote.reraise_safe_exceptions |
|
97 | @hg_remote.reraise_safe_exceptions | |
98 | def stub_method(): |
|
98 | def stub_method(): | |
99 | raise ValueError('stub') |
|
99 | raise ValueError('stub') | |
100 |
|
100 | |||
101 | with pytest.raises(Exception) as exc_info: |
|
101 | with pytest.raises(Exception) as exc_info: | |
102 | stub_method() |
|
102 | stub_method() | |
103 | assert exc_info.value._vcs_kind == 'unhandled' |
|
103 | assert exc_info.value._vcs_kind == 'unhandled' | |
104 |
|
104 | |||
105 | def test_does_not_map_known_exceptions(self): |
|
105 | def test_does_not_map_known_exceptions(self): | |
106 | @hg_remote.reraise_safe_exceptions |
|
106 | @hg_remote.reraise_safe_exceptions | |
107 | def stub_method(): |
|
107 | def stub_method(): | |
108 | raise exceptions.LookupException()('stub') |
|
108 | raise exceptions.LookupException()('stub') | |
109 |
|
109 | |||
110 | with pytest.raises(Exception) as exc_info: |
|
110 | with pytest.raises(Exception) as exc_info: | |
111 | stub_method() |
|
111 | stub_method() | |
112 | assert exc_info.value._vcs_kind == 'lookup' |
|
112 | assert exc_info.value._vcs_kind == 'lookup' |
@@ -1,286 +1,286 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import threading |
|
18 | import threading | |
19 | import msgpack |
|
19 | import msgpack | |
20 |
|
20 | |||
21 | from http.server import BaseHTTPRequestHandler |
|
21 | from http.server import BaseHTTPRequestHandler | |
22 | from socketserver import TCPServer |
|
22 | from socketserver import TCPServer | |
23 |
|
23 | |||
24 | import mercurial.ui |
|
24 | import mercurial.ui | |
25 | import mock |
|
25 | import mock | |
26 | import pytest |
|
26 | import pytest | |
27 |
|
27 | |||
28 | from vcsserver.hooks import HooksHttpClient |
|
28 | from vcsserver.hooks import HooksHttpClient | |
29 | from vcsserver.lib.rc_json import json |
|
29 | from vcsserver.lib.rc_json import json | |
30 | from vcsserver import hooks |
|
30 | from vcsserver import hooks | |
31 |
|
31 | |||
32 |
|
32 | |||
33 | def get_hg_ui(extras=None): |
|
33 | def get_hg_ui(extras=None): | |
34 | """Create a Config object with a valid RC_SCM_DATA entry.""" |
|
34 | """Create a Config object with a valid RC_SCM_DATA entry.""" | |
35 | extras = extras or {} |
|
35 | extras = extras or {} | |
36 | required_extras = { |
|
36 | required_extras = { | |
37 | 'username': '', |
|
37 | 'username': '', | |
38 | 'repository': '', |
|
38 | 'repository': '', | |
39 | 'locked_by': '', |
|
39 | 'locked_by': '', | |
40 | 'scm': '', |
|
40 | 'scm': '', | |
41 | 'make_lock': '', |
|
41 | 'make_lock': '', | |
42 | 'action': '', |
|
42 | 'action': '', | |
43 | 'ip': '', |
|
43 | 'ip': '', | |
44 | 'hooks_uri': 'fake_hooks_uri', |
|
44 | 'hooks_uri': 'fake_hooks_uri', | |
45 | } |
|
45 | } | |
46 | required_extras.update(extras) |
|
46 | required_extras.update(extras) | |
47 | hg_ui = mercurial.ui.ui() |
|
47 | hg_ui = mercurial.ui.ui() | |
48 | hg_ui.setconfig(b'rhodecode', b'RC_SCM_DATA', json.dumps(required_extras)) |
|
48 | hg_ui.setconfig(b'rhodecode', b'RC_SCM_DATA', json.dumps(required_extras)) | |
49 |
|
49 | |||
50 | return hg_ui |
|
50 | return hg_ui | |
51 |
|
51 | |||
52 |
|
52 | |||
53 | def test_git_pre_receive_is_disabled(): |
|
53 | def test_git_pre_receive_is_disabled(): | |
54 | extras = {'hooks': ['pull']} |
|
54 | extras = {'hooks': ['pull']} | |
55 | response = hooks.git_pre_receive(None, None, |
|
55 | response = hooks.git_pre_receive(None, None, | |
56 | {'RC_SCM_DATA': json.dumps(extras)}) |
|
56 | {'RC_SCM_DATA': json.dumps(extras)}) | |
57 |
|
57 | |||
58 | assert response == 0 |
|
58 | assert response == 0 | |
59 |
|
59 | |||
60 |
|
60 | |||
61 | def test_git_post_receive_is_disabled(): |
|
61 | def test_git_post_receive_is_disabled(): | |
62 | extras = {'hooks': ['pull']} |
|
62 | extras = {'hooks': ['pull']} | |
63 | response = hooks.git_post_receive(None, '', |
|
63 | response = hooks.git_post_receive(None, '', | |
64 | {'RC_SCM_DATA': json.dumps(extras)}) |
|
64 | {'RC_SCM_DATA': json.dumps(extras)}) | |
65 |
|
65 | |||
66 | assert response == 0 |
|
66 | assert response == 0 | |
67 |
|
67 | |||
68 |
|
68 | |||
69 | def test_git_post_receive_calls_repo_size(): |
|
69 | def test_git_post_receive_calls_repo_size(): | |
70 | extras = {'hooks': ['push', 'repo_size']} |
|
70 | extras = {'hooks': ['push', 'repo_size']} | |
71 |
|
71 | |||
72 | with mock.patch.object(hooks, '_call_hook') as call_hook_mock: |
|
72 | with mock.patch.object(hooks, '_call_hook') as call_hook_mock: | |
73 | hooks.git_post_receive( |
|
73 | hooks.git_post_receive( | |
74 | None, '', {'RC_SCM_DATA': json.dumps(extras)}) |
|
74 | None, '', {'RC_SCM_DATA': json.dumps(extras)}) | |
75 | extras.update({'commit_ids': [], 'hook_type': 'post_receive', |
|
75 | extras.update({'commit_ids': [], 'hook_type': 'post_receive', | |
76 | 'new_refs': {'bookmarks': [], 'branches': [], 'tags': []}}) |
|
76 | 'new_refs': {'bookmarks': [], 'branches': [], 'tags': []}}) | |
77 | expected_calls = [ |
|
77 | expected_calls = [ | |
78 | mock.call('repo_size', extras, mock.ANY), |
|
78 | mock.call('repo_size', extras, mock.ANY), | |
79 | mock.call('post_push', extras, mock.ANY), |
|
79 | mock.call('post_push', extras, mock.ANY), | |
80 | ] |
|
80 | ] | |
81 | assert call_hook_mock.call_args_list == expected_calls |
|
81 | assert call_hook_mock.call_args_list == expected_calls | |
82 |
|
82 | |||
83 |
|
83 | |||
84 | def test_git_post_receive_does_not_call_disabled_repo_size(): |
|
84 | def test_git_post_receive_does_not_call_disabled_repo_size(): | |
85 | extras = {'hooks': ['push']} |
|
85 | extras = {'hooks': ['push']} | |
86 |
|
86 | |||
87 | with mock.patch.object(hooks, '_call_hook') as call_hook_mock: |
|
87 | with mock.patch.object(hooks, '_call_hook') as call_hook_mock: | |
88 | hooks.git_post_receive( |
|
88 | hooks.git_post_receive( | |
89 | None, '', {'RC_SCM_DATA': json.dumps(extras)}) |
|
89 | None, '', {'RC_SCM_DATA': json.dumps(extras)}) | |
90 | extras.update({'commit_ids': [], 'hook_type': 'post_receive', |
|
90 | extras.update({'commit_ids': [], 'hook_type': 'post_receive', | |
91 | 'new_refs': {'bookmarks': [], 'branches': [], 'tags': []}}) |
|
91 | 'new_refs': {'bookmarks': [], 'branches': [], 'tags': []}}) | |
92 | expected_calls = [ |
|
92 | expected_calls = [ | |
93 | mock.call('post_push', extras, mock.ANY) |
|
93 | mock.call('post_push', extras, mock.ANY) | |
94 | ] |
|
94 | ] | |
95 | assert call_hook_mock.call_args_list == expected_calls |
|
95 | assert call_hook_mock.call_args_list == expected_calls | |
96 |
|
96 | |||
97 |
|
97 | |||
98 | def test_repo_size_exception_does_not_affect_git_post_receive(): |
|
98 | def test_repo_size_exception_does_not_affect_git_post_receive(): | |
99 | extras = {'hooks': ['push', 'repo_size']} |
|
99 | extras = {'hooks': ['push', 'repo_size']} | |
100 | status = 0 |
|
100 | status = 0 | |
101 |
|
101 | |||
102 | def side_effect(name, *args, **kwargs): |
|
102 | def side_effect(name, *args, **kwargs): | |
103 | if name == 'repo_size': |
|
103 | if name == 'repo_size': | |
104 | raise Exception('Fake exception') |
|
104 | raise Exception('Fake exception') | |
105 | else: |
|
105 | else: | |
106 | return status |
|
106 | return status | |
107 |
|
107 | |||
108 | with mock.patch.object(hooks, '_call_hook') as call_hook_mock: |
|
108 | with mock.patch.object(hooks, '_call_hook') as call_hook_mock: | |
109 | call_hook_mock.side_effect = side_effect |
|
109 | call_hook_mock.side_effect = side_effect | |
110 | result = hooks.git_post_receive( |
|
110 | result = hooks.git_post_receive( | |
111 | None, '', {'RC_SCM_DATA': json.dumps(extras)}) |
|
111 | None, '', {'RC_SCM_DATA': json.dumps(extras)}) | |
112 | assert result == status |
|
112 | assert result == status | |
113 |
|
113 | |||
114 |
|
114 | |||
115 | def test_git_pre_pull_is_disabled(): |
|
115 | def test_git_pre_pull_is_disabled(): | |
116 | assert hooks.git_pre_pull({'hooks': ['push']}) == hooks.HookResponse(0, '') |
|
116 | assert hooks.git_pre_pull({'hooks': ['push']}) == hooks.HookResponse(0, '') | |
117 |
|
117 | |||
118 |
|
118 | |||
119 | def test_git_post_pull_is_disabled(): |
|
119 | def test_git_post_pull_is_disabled(): | |
120 | assert ( |
|
120 | assert ( | |
121 | hooks.git_post_pull({'hooks': ['push']}) == hooks.HookResponse(0, '')) |
|
121 | hooks.git_post_pull({'hooks': ['push']}) == hooks.HookResponse(0, '')) | |
122 |
|
122 | |||
123 |
|
123 | |||
124 |
class TestGetHooksClient |
|
124 | class TestGetHooksClient: | |
125 |
|
125 | |||
126 | def test_returns_http_client_when_protocol_matches(self): |
|
126 | def test_returns_http_client_when_protocol_matches(self): | |
127 | hooks_uri = 'localhost:8000' |
|
127 | hooks_uri = 'localhost:8000' | |
128 | result = hooks._get_hooks_client({ |
|
128 | result = hooks._get_hooks_client({ | |
129 | 'hooks_uri': hooks_uri, |
|
129 | 'hooks_uri': hooks_uri, | |
130 | 'hooks_protocol': 'http' |
|
130 | 'hooks_protocol': 'http' | |
131 | }) |
|
131 | }) | |
132 | assert isinstance(result, hooks.HooksHttpClient) |
|
132 | assert isinstance(result, hooks.HooksHttpClient) | |
133 | assert result.hooks_uri == hooks_uri |
|
133 | assert result.hooks_uri == hooks_uri | |
134 |
|
134 | |||
135 | def test_returns_dummy_client_when_hooks_uri_not_specified(self): |
|
135 | def test_returns_dummy_client_when_hooks_uri_not_specified(self): | |
136 | fake_module = mock.Mock() |
|
136 | fake_module = mock.Mock() | |
137 | import_patcher = mock.patch.object( |
|
137 | import_patcher = mock.patch.object( | |
138 | hooks.importlib, 'import_module', return_value=fake_module) |
|
138 | hooks.importlib, 'import_module', return_value=fake_module) | |
139 | fake_module_name = 'fake.module' |
|
139 | fake_module_name = 'fake.module' | |
140 | with import_patcher as import_mock: |
|
140 | with import_patcher as import_mock: | |
141 | result = hooks._get_hooks_client( |
|
141 | result = hooks._get_hooks_client( | |
142 | {'hooks_module': fake_module_name}) |
|
142 | {'hooks_module': fake_module_name}) | |
143 |
|
143 | |||
144 | import_mock.assert_called_once_with(fake_module_name) |
|
144 | import_mock.assert_called_once_with(fake_module_name) | |
145 | assert isinstance(result, hooks.HooksDummyClient) |
|
145 | assert isinstance(result, hooks.HooksDummyClient) | |
146 | assert result._hooks_module == fake_module |
|
146 | assert result._hooks_module == fake_module | |
147 |
|
147 | |||
148 |
|
148 | |||
149 |
class TestHooksHttpClient |
|
149 | class TestHooksHttpClient: | |
150 | def test_init_sets_hooks_uri(self): |
|
150 | def test_init_sets_hooks_uri(self): | |
151 | uri = 'localhost:3000' |
|
151 | uri = 'localhost:3000' | |
152 | client = hooks.HooksHttpClient(uri) |
|
152 | client = hooks.HooksHttpClient(uri) | |
153 | assert client.hooks_uri == uri |
|
153 | assert client.hooks_uri == uri | |
154 |
|
154 | |||
155 | def test_serialize_returns_serialized_string(self): |
|
155 | def test_serialize_returns_serialized_string(self): | |
156 | client = hooks.HooksHttpClient('localhost:3000') |
|
156 | client = hooks.HooksHttpClient('localhost:3000') | |
157 | hook_name = 'test' |
|
157 | hook_name = 'test' | |
158 | extras = { |
|
158 | extras = { | |
159 | 'first': 1, |
|
159 | 'first': 1, | |
160 | 'second': 'two' |
|
160 | 'second': 'two' | |
161 | } |
|
161 | } | |
162 | hooks_proto, result = client._serialize(hook_name, extras) |
|
162 | hooks_proto, result = client._serialize(hook_name, extras) | |
163 | expected_result = msgpack.packb({ |
|
163 | expected_result = msgpack.packb({ | |
164 | 'method': hook_name, |
|
164 | 'method': hook_name, | |
165 | 'extras': extras, |
|
165 | 'extras': extras, | |
166 | }) |
|
166 | }) | |
167 | assert hooks_proto == {'rc-hooks-protocol': 'msgpack.v1', 'Connection': 'keep-alive'} |
|
167 | assert hooks_proto == {'rc-hooks-protocol': 'msgpack.v1', 'Connection': 'keep-alive'} | |
168 | assert result == expected_result |
|
168 | assert result == expected_result | |
169 |
|
169 | |||
170 | def test_call_queries_http_server(self, http_mirror): |
|
170 | def test_call_queries_http_server(self, http_mirror): | |
171 | client = hooks.HooksHttpClient(http_mirror.uri) |
|
171 | client = hooks.HooksHttpClient(http_mirror.uri) | |
172 | hook_name = 'test' |
|
172 | hook_name = 'test' | |
173 | extras = { |
|
173 | extras = { | |
174 | 'first': 1, |
|
174 | 'first': 1, | |
175 | 'second': 'two' |
|
175 | 'second': 'two' | |
176 | } |
|
176 | } | |
177 | result = client(hook_name, extras) |
|
177 | result = client(hook_name, extras) | |
178 | expected_result = msgpack.unpackb(msgpack.packb({ |
|
178 | expected_result = msgpack.unpackb(msgpack.packb({ | |
179 | 'method': hook_name, |
|
179 | 'method': hook_name, | |
180 | 'extras': extras |
|
180 | 'extras': extras | |
181 | }), raw=False) |
|
181 | }), raw=False) | |
182 | assert result == expected_result |
|
182 | assert result == expected_result | |
183 |
|
183 | |||
184 |
|
184 | |||
185 |
class TestHooksDummyClient |
|
185 | class TestHooksDummyClient: | |
186 | def test_init_imports_hooks_module(self): |
|
186 | def test_init_imports_hooks_module(self): | |
187 | hooks_module_name = 'rhodecode.fake.module' |
|
187 | hooks_module_name = 'rhodecode.fake.module' | |
188 | hooks_module = mock.MagicMock() |
|
188 | hooks_module = mock.MagicMock() | |
189 |
|
189 | |||
190 | import_patcher = mock.patch.object( |
|
190 | import_patcher = mock.patch.object( | |
191 | hooks.importlib, 'import_module', return_value=hooks_module) |
|
191 | hooks.importlib, 'import_module', return_value=hooks_module) | |
192 | with import_patcher as import_mock: |
|
192 | with import_patcher as import_mock: | |
193 | client = hooks.HooksDummyClient(hooks_module_name) |
|
193 | client = hooks.HooksDummyClient(hooks_module_name) | |
194 | import_mock.assert_called_once_with(hooks_module_name) |
|
194 | import_mock.assert_called_once_with(hooks_module_name) | |
195 | assert client._hooks_module == hooks_module |
|
195 | assert client._hooks_module == hooks_module | |
196 |
|
196 | |||
197 | def test_call_returns_hook_result(self): |
|
197 | def test_call_returns_hook_result(self): | |
198 | hooks_module_name = 'rhodecode.fake.module' |
|
198 | hooks_module_name = 'rhodecode.fake.module' | |
199 | hooks_module = mock.MagicMock() |
|
199 | hooks_module = mock.MagicMock() | |
200 | import_patcher = mock.patch.object( |
|
200 | import_patcher = mock.patch.object( | |
201 | hooks.importlib, 'import_module', return_value=hooks_module) |
|
201 | hooks.importlib, 'import_module', return_value=hooks_module) | |
202 | with import_patcher: |
|
202 | with import_patcher: | |
203 | client = hooks.HooksDummyClient(hooks_module_name) |
|
203 | client = hooks.HooksDummyClient(hooks_module_name) | |
204 |
|
204 | |||
205 | result = client('post_push', {}) |
|
205 | result = client('post_push', {}) | |
206 | hooks_module.Hooks.assert_called_once_with() |
|
206 | hooks_module.Hooks.assert_called_once_with() | |
207 | assert result == hooks_module.Hooks().__enter__().post_push() |
|
207 | assert result == hooks_module.Hooks().__enter__().post_push() | |
208 |
|
208 | |||
209 |
|
209 | |||
210 | @pytest.fixture |
|
210 | @pytest.fixture | |
211 | def http_mirror(request): |
|
211 | def http_mirror(request): | |
212 | server = MirrorHttpServer() |
|
212 | server = MirrorHttpServer() | |
213 | request.addfinalizer(server.stop) |
|
213 | request.addfinalizer(server.stop) | |
214 | return server |
|
214 | return server | |
215 |
|
215 | |||
216 |
|
216 | |||
217 | class MirrorHttpHandler(BaseHTTPRequestHandler): |
|
217 | class MirrorHttpHandler(BaseHTTPRequestHandler): | |
218 |
|
218 | |||
219 | def do_POST(self): |
|
219 | def do_POST(self): | |
220 | length = int(self.headers['Content-Length']) |
|
220 | length = int(self.headers['Content-Length']) | |
221 | body = self.rfile.read(length) |
|
221 | body = self.rfile.read(length) | |
222 | self.send_response(200) |
|
222 | self.send_response(200) | |
223 | self.end_headers() |
|
223 | self.end_headers() | |
224 | self.wfile.write(body) |
|
224 | self.wfile.write(body) | |
225 |
|
225 | |||
226 |
|
226 | |||
227 |
class MirrorHttpServer |
|
227 | class MirrorHttpServer: | |
228 | ip_address = '127.0.0.1' |
|
228 | ip_address = '127.0.0.1' | |
229 | port = 0 |
|
229 | port = 0 | |
230 |
|
230 | |||
231 | def __init__(self): |
|
231 | def __init__(self): | |
232 | self._daemon = TCPServer((self.ip_address, 0), MirrorHttpHandler) |
|
232 | self._daemon = TCPServer((self.ip_address, 0), MirrorHttpHandler) | |
233 | _, self.port = self._daemon.server_address |
|
233 | _, self.port = self._daemon.server_address | |
234 | self._thread = threading.Thread(target=self._daemon.serve_forever) |
|
234 | self._thread = threading.Thread(target=self._daemon.serve_forever) | |
235 | self._thread.daemon = True |
|
235 | self._thread.daemon = True | |
236 | self._thread.start() |
|
236 | self._thread.start() | |
237 |
|
237 | |||
238 | def stop(self): |
|
238 | def stop(self): | |
239 | self._daemon.shutdown() |
|
239 | self._daemon.shutdown() | |
240 | self._thread.join() |
|
240 | self._thread.join() | |
241 | self._daemon = None |
|
241 | self._daemon = None | |
242 | self._thread = None |
|
242 | self._thread = None | |
243 |
|
243 | |||
244 | @property |
|
244 | @property | |
245 | def uri(self): |
|
245 | def uri(self): | |
246 | return '{}:{}'.format(self.ip_address, self.port) |
|
246 | return '{}:{}'.format(self.ip_address, self.port) | |
247 |
|
247 | |||
248 |
|
248 | |||
249 | def test_hooks_http_client_init(): |
|
249 | def test_hooks_http_client_init(): | |
250 | hooks_uri = 'http://localhost:8000' |
|
250 | hooks_uri = 'http://localhost:8000' | |
251 | client = HooksHttpClient(hooks_uri) |
|
251 | client = HooksHttpClient(hooks_uri) | |
252 | assert client.hooks_uri == hooks_uri |
|
252 | assert client.hooks_uri == hooks_uri | |
253 |
|
253 | |||
254 |
|
254 | |||
255 | def test_hooks_http_client_call(): |
|
255 | def test_hooks_http_client_call(): | |
256 | hooks_uri = 'http://localhost:8000' |
|
256 | hooks_uri = 'http://localhost:8000' | |
257 |
|
257 | |||
258 | method = 'test_method' |
|
258 | method = 'test_method' | |
259 | extras = {'key': 'value'} |
|
259 | extras = {'key': 'value'} | |
260 |
|
260 | |||
261 | with \ |
|
261 | with \ | |
262 | mock.patch('http.client.HTTPConnection') as mock_connection,\ |
|
262 | mock.patch('http.client.HTTPConnection') as mock_connection,\ | |
263 | mock.patch('msgpack.load') as mock_load: |
|
263 | mock.patch('msgpack.load') as mock_load: | |
264 |
|
264 | |||
265 | client = HooksHttpClient(hooks_uri) |
|
265 | client = HooksHttpClient(hooks_uri) | |
266 |
|
266 | |||
267 | mock_load.return_value = {'result': 'success'} |
|
267 | mock_load.return_value = {'result': 'success'} | |
268 | response = mock.MagicMock() |
|
268 | response = mock.MagicMock() | |
269 | response.status = 200 |
|
269 | response.status = 200 | |
270 | mock_connection.request.side_effect = None |
|
270 | mock_connection.request.side_effect = None | |
271 | mock_connection.getresponse.return_value = response |
|
271 | mock_connection.getresponse.return_value = response | |
272 |
|
272 | |||
273 | result = client(method, extras) |
|
273 | result = client(method, extras) | |
274 |
|
274 | |||
275 | mock_connection.assert_called_with(hooks_uri) |
|
275 | mock_connection.assert_called_with(hooks_uri) | |
276 | mock_connection.return_value.request.assert_called_once() |
|
276 | mock_connection.return_value.request.assert_called_once() | |
277 | assert result == {'result': 'success'} |
|
277 | assert result == {'result': 'success'} | |
278 |
|
278 | |||
279 |
|
279 | |||
280 | def test_hooks_http_client_serialize(): |
|
280 | def test_hooks_http_client_serialize(): | |
281 | method = 'test_method' |
|
281 | method = 'test_method' | |
282 | extras = {'key': 'value'} |
|
282 | extras = {'key': 'value'} | |
283 | headers, body = HooksHttpClient._serialize(method, extras) |
|
283 | headers, body = HooksHttpClient._serialize(method, extras) | |
284 |
|
284 | |||
285 | assert headers == {'rc-hooks-protocol': HooksHttpClient.proto, 'Connection': 'keep-alive'} |
|
285 | assert headers == {'rc-hooks-protocol': HooksHttpClient.proto, 'Connection': 'keep-alive'} | |
286 | assert msgpack.unpackb(body) == {'method': method, 'extras': extras} |
|
286 | assert msgpack.unpackb(body) == {'method': method, 'extras': extras} |
@@ -1,206 +1,206 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import os |
|
18 | import os | |
19 | import sys |
|
19 | import sys | |
20 | import stat |
|
20 | import stat | |
21 | import pytest |
|
21 | import pytest | |
22 | import vcsserver |
|
22 | import vcsserver | |
23 | import tempfile |
|
23 | import tempfile | |
24 | from vcsserver import hook_utils |
|
24 | from vcsserver import hook_utils | |
25 | from vcsserver.tests.fixture import no_newline_id_generator |
|
25 | from vcsserver.tests.fixture import no_newline_id_generator | |
26 | from vcsserver.str_utils import safe_bytes, safe_str |
|
26 | from vcsserver.str_utils import safe_bytes, safe_str | |
27 | from vcsserver.utils import AttributeDict |
|
27 | from vcsserver.utils import AttributeDict | |
28 |
|
28 | |||
29 |
|
29 | |||
30 |
class TestCheckRhodecodeHook |
|
30 | class TestCheckRhodecodeHook: | |
31 |
|
31 | |||
32 | def test_returns_false_when_hook_file_is_wrong_found(self, tmpdir): |
|
32 | def test_returns_false_when_hook_file_is_wrong_found(self, tmpdir): | |
33 | hook = os.path.join(str(tmpdir), 'fake_hook_file.py') |
|
33 | hook = os.path.join(str(tmpdir), 'fake_hook_file.py') | |
34 | with open(hook, 'wb') as f: |
|
34 | with open(hook, 'wb') as f: | |
35 | f.write(b'dummy test') |
|
35 | f.write(b'dummy test') | |
36 | result = hook_utils.check_rhodecode_hook(hook) |
|
36 | result = hook_utils.check_rhodecode_hook(hook) | |
37 | assert result is False |
|
37 | assert result is False | |
38 |
|
38 | |||
39 | def test_returns_true_when_no_hook_file_found(self, tmpdir): |
|
39 | def test_returns_true_when_no_hook_file_found(self, tmpdir): | |
40 | hook = os.path.join(str(tmpdir), 'fake_hook_file_not_existing.py') |
|
40 | hook = os.path.join(str(tmpdir), 'fake_hook_file_not_existing.py') | |
41 | result = hook_utils.check_rhodecode_hook(hook) |
|
41 | result = hook_utils.check_rhodecode_hook(hook) | |
42 | assert result |
|
42 | assert result | |
43 |
|
43 | |||
44 | @pytest.mark.parametrize("file_content, expected_result", [ |
|
44 | @pytest.mark.parametrize("file_content, expected_result", [ | |
45 | ("RC_HOOK_VER = '3.3.3'\n", True), |
|
45 | ("RC_HOOK_VER = '3.3.3'\n", True), | |
46 | ("RC_HOOK = '3.3.3'\n", False), |
|
46 | ("RC_HOOK = '3.3.3'\n", False), | |
47 | ], ids=no_newline_id_generator) |
|
47 | ], ids=no_newline_id_generator) | |
48 | def test_signatures(self, file_content, expected_result, tmpdir): |
|
48 | def test_signatures(self, file_content, expected_result, tmpdir): | |
49 | hook = os.path.join(str(tmpdir), 'fake_hook_file_1.py') |
|
49 | hook = os.path.join(str(tmpdir), 'fake_hook_file_1.py') | |
50 | with open(hook, 'wb') as f: |
|
50 | with open(hook, 'wb') as f: | |
51 | f.write(safe_bytes(file_content)) |
|
51 | f.write(safe_bytes(file_content)) | |
52 |
|
52 | |||
53 | result = hook_utils.check_rhodecode_hook(hook) |
|
53 | result = hook_utils.check_rhodecode_hook(hook) | |
54 |
|
54 | |||
55 | assert result is expected_result |
|
55 | assert result is expected_result | |
56 |
|
56 | |||
57 |
|
57 | |||
58 |
class BaseInstallHooks |
|
58 | class BaseInstallHooks: | |
59 | HOOK_FILES = () |
|
59 | HOOK_FILES = () | |
60 |
|
60 | |||
61 | def _check_hook_file_mode(self, file_path): |
|
61 | def _check_hook_file_mode(self, file_path): | |
62 | assert os.path.exists(file_path), 'path %s missing' % file_path |
|
62 | assert os.path.exists(file_path), 'path %s missing' % file_path | |
63 | stat_info = os.stat(file_path) |
|
63 | stat_info = os.stat(file_path) | |
64 |
|
64 | |||
65 | file_mode = stat.S_IMODE(stat_info.st_mode) |
|
65 | file_mode = stat.S_IMODE(stat_info.st_mode) | |
66 | expected_mode = int('755', 8) |
|
66 | expected_mode = int('755', 8) | |
67 | assert expected_mode == file_mode |
|
67 | assert expected_mode == file_mode | |
68 |
|
68 | |||
69 | def _check_hook_file_content(self, file_path, executable): |
|
69 | def _check_hook_file_content(self, file_path, executable): | |
70 | executable = executable or sys.executable |
|
70 | executable = executable or sys.executable | |
71 | with open(file_path, 'rt') as hook_file: |
|
71 | with open(file_path, 'rt') as hook_file: | |
72 | content = hook_file.read() |
|
72 | content = hook_file.read() | |
73 |
|
73 | |||
74 | expected_env = '#!{}'.format(executable) |
|
74 | expected_env = '#!{}'.format(executable) | |
75 | expected_rc_version = "\nRC_HOOK_VER = '{}'\n".format(vcsserver.__version__) |
|
75 | expected_rc_version = "\nRC_HOOK_VER = '{}'\n".format(vcsserver.__version__) | |
76 | assert content.strip().startswith(expected_env) |
|
76 | assert content.strip().startswith(expected_env) | |
77 | assert expected_rc_version in content |
|
77 | assert expected_rc_version in content | |
78 |
|
78 | |||
79 | def _create_fake_hook(self, file_path, content): |
|
79 | def _create_fake_hook(self, file_path, content): | |
80 | with open(file_path, 'w') as hook_file: |
|
80 | with open(file_path, 'w') as hook_file: | |
81 | hook_file.write(content) |
|
81 | hook_file.write(content) | |
82 |
|
82 | |||
83 | def create_dummy_repo(self, repo_type): |
|
83 | def create_dummy_repo(self, repo_type): | |
84 | tmpdir = tempfile.mkdtemp() |
|
84 | tmpdir = tempfile.mkdtemp() | |
85 | repo = AttributeDict() |
|
85 | repo = AttributeDict() | |
86 | if repo_type == 'git': |
|
86 | if repo_type == 'git': | |
87 | repo.path = os.path.join(tmpdir, 'test_git_hooks_installation_repo') |
|
87 | repo.path = os.path.join(tmpdir, 'test_git_hooks_installation_repo') | |
88 | os.makedirs(repo.path) |
|
88 | os.makedirs(repo.path) | |
89 | os.makedirs(os.path.join(repo.path, 'hooks')) |
|
89 | os.makedirs(os.path.join(repo.path, 'hooks')) | |
90 | repo.bare = True |
|
90 | repo.bare = True | |
91 |
|
91 | |||
92 | elif repo_type == 'svn': |
|
92 | elif repo_type == 'svn': | |
93 | repo.path = os.path.join(tmpdir, 'test_svn_hooks_installation_repo') |
|
93 | repo.path = os.path.join(tmpdir, 'test_svn_hooks_installation_repo') | |
94 | os.makedirs(repo.path) |
|
94 | os.makedirs(repo.path) | |
95 | os.makedirs(os.path.join(repo.path, 'hooks')) |
|
95 | os.makedirs(os.path.join(repo.path, 'hooks')) | |
96 |
|
96 | |||
97 | return repo |
|
97 | return repo | |
98 |
|
98 | |||
99 | def check_hooks(self, repo_path, repo_bare=True): |
|
99 | def check_hooks(self, repo_path, repo_bare=True): | |
100 | for file_name in self.HOOK_FILES: |
|
100 | for file_name in self.HOOK_FILES: | |
101 | if repo_bare: |
|
101 | if repo_bare: | |
102 | file_path = os.path.join(repo_path, 'hooks', file_name) |
|
102 | file_path = os.path.join(repo_path, 'hooks', file_name) | |
103 | else: |
|
103 | else: | |
104 | file_path = os.path.join(repo_path, '.git', 'hooks', file_name) |
|
104 | file_path = os.path.join(repo_path, '.git', 'hooks', file_name) | |
105 | self._check_hook_file_mode(file_path) |
|
105 | self._check_hook_file_mode(file_path) | |
106 | self._check_hook_file_content(file_path, sys.executable) |
|
106 | self._check_hook_file_content(file_path, sys.executable) | |
107 |
|
107 | |||
108 |
|
108 | |||
109 | class TestInstallGitHooks(BaseInstallHooks): |
|
109 | class TestInstallGitHooks(BaseInstallHooks): | |
110 | HOOK_FILES = ('pre-receive', 'post-receive') |
|
110 | HOOK_FILES = ('pre-receive', 'post-receive') | |
111 |
|
111 | |||
112 | def test_hooks_are_installed(self): |
|
112 | def test_hooks_are_installed(self): | |
113 | repo = self.create_dummy_repo('git') |
|
113 | repo = self.create_dummy_repo('git') | |
114 | result = hook_utils.install_git_hooks(repo.path, repo.bare) |
|
114 | result = hook_utils.install_git_hooks(repo.path, repo.bare) | |
115 | assert result |
|
115 | assert result | |
116 | self.check_hooks(repo.path, repo.bare) |
|
116 | self.check_hooks(repo.path, repo.bare) | |
117 |
|
117 | |||
118 | def test_hooks_are_replaced(self): |
|
118 | def test_hooks_are_replaced(self): | |
119 | repo = self.create_dummy_repo('git') |
|
119 | repo = self.create_dummy_repo('git') | |
120 | hooks_path = os.path.join(repo.path, 'hooks') |
|
120 | hooks_path = os.path.join(repo.path, 'hooks') | |
121 | for file_path in [os.path.join(hooks_path, f) for f in self.HOOK_FILES]: |
|
121 | for file_path in [os.path.join(hooks_path, f) for f in self.HOOK_FILES]: | |
122 | self._create_fake_hook( |
|
122 | self._create_fake_hook( | |
123 | file_path, content="RC_HOOK_VER = 'abcde'\n") |
|
123 | file_path, content="RC_HOOK_VER = 'abcde'\n") | |
124 |
|
124 | |||
125 | result = hook_utils.install_git_hooks(repo.path, repo.bare) |
|
125 | result = hook_utils.install_git_hooks(repo.path, repo.bare) | |
126 | assert result |
|
126 | assert result | |
127 | self.check_hooks(repo.path, repo.bare) |
|
127 | self.check_hooks(repo.path, repo.bare) | |
128 |
|
128 | |||
129 | def test_non_rc_hooks_are_not_replaced(self): |
|
129 | def test_non_rc_hooks_are_not_replaced(self): | |
130 | repo = self.create_dummy_repo('git') |
|
130 | repo = self.create_dummy_repo('git') | |
131 | hooks_path = os.path.join(repo.path, 'hooks') |
|
131 | hooks_path = os.path.join(repo.path, 'hooks') | |
132 | non_rc_content = 'echo "non rc hook"\n' |
|
132 | non_rc_content = 'echo "non rc hook"\n' | |
133 | for file_path in [os.path.join(hooks_path, f) for f in self.HOOK_FILES]: |
|
133 | for file_path in [os.path.join(hooks_path, f) for f in self.HOOK_FILES]: | |
134 | self._create_fake_hook( |
|
134 | self._create_fake_hook( | |
135 | file_path, content=non_rc_content) |
|
135 | file_path, content=non_rc_content) | |
136 |
|
136 | |||
137 | result = hook_utils.install_git_hooks(repo.path, repo.bare) |
|
137 | result = hook_utils.install_git_hooks(repo.path, repo.bare) | |
138 | assert result |
|
138 | assert result | |
139 |
|
139 | |||
140 | for file_path in [os.path.join(hooks_path, f) for f in self.HOOK_FILES]: |
|
140 | for file_path in [os.path.join(hooks_path, f) for f in self.HOOK_FILES]: | |
141 | with open(file_path, 'rt') as hook_file: |
|
141 | with open(file_path, 'rt') as hook_file: | |
142 | content = hook_file.read() |
|
142 | content = hook_file.read() | |
143 | assert content == non_rc_content |
|
143 | assert content == non_rc_content | |
144 |
|
144 | |||
145 | def test_non_rc_hooks_are_replaced_with_force_flag(self): |
|
145 | def test_non_rc_hooks_are_replaced_with_force_flag(self): | |
146 | repo = self.create_dummy_repo('git') |
|
146 | repo = self.create_dummy_repo('git') | |
147 | hooks_path = os.path.join(repo.path, 'hooks') |
|
147 | hooks_path = os.path.join(repo.path, 'hooks') | |
148 | non_rc_content = 'echo "non rc hook"\n' |
|
148 | non_rc_content = 'echo "non rc hook"\n' | |
149 | for file_path in [os.path.join(hooks_path, f) for f in self.HOOK_FILES]: |
|
149 | for file_path in [os.path.join(hooks_path, f) for f in self.HOOK_FILES]: | |
150 | self._create_fake_hook( |
|
150 | self._create_fake_hook( | |
151 | file_path, content=non_rc_content) |
|
151 | file_path, content=non_rc_content) | |
152 |
|
152 | |||
153 | result = hook_utils.install_git_hooks( |
|
153 | result = hook_utils.install_git_hooks( | |
154 | repo.path, repo.bare, force_create=True) |
|
154 | repo.path, repo.bare, force_create=True) | |
155 | assert result |
|
155 | assert result | |
156 | self.check_hooks(repo.path, repo.bare) |
|
156 | self.check_hooks(repo.path, repo.bare) | |
157 |
|
157 | |||
158 |
|
158 | |||
159 | class TestInstallSvnHooks(BaseInstallHooks): |
|
159 | class TestInstallSvnHooks(BaseInstallHooks): | |
160 | HOOK_FILES = ('pre-commit', 'post-commit') |
|
160 | HOOK_FILES = ('pre-commit', 'post-commit') | |
161 |
|
161 | |||
162 | def test_hooks_are_installed(self): |
|
162 | def test_hooks_are_installed(self): | |
163 | repo = self.create_dummy_repo('svn') |
|
163 | repo = self.create_dummy_repo('svn') | |
164 | result = hook_utils.install_svn_hooks(repo.path) |
|
164 | result = hook_utils.install_svn_hooks(repo.path) | |
165 | assert result |
|
165 | assert result | |
166 | self.check_hooks(repo.path) |
|
166 | self.check_hooks(repo.path) | |
167 |
|
167 | |||
168 | def test_hooks_are_replaced(self): |
|
168 | def test_hooks_are_replaced(self): | |
169 | repo = self.create_dummy_repo('svn') |
|
169 | repo = self.create_dummy_repo('svn') | |
170 | hooks_path = os.path.join(repo.path, 'hooks') |
|
170 | hooks_path = os.path.join(repo.path, 'hooks') | |
171 | for file_path in [os.path.join(hooks_path, f) for f in self.HOOK_FILES]: |
|
171 | for file_path in [os.path.join(hooks_path, f) for f in self.HOOK_FILES]: | |
172 | self._create_fake_hook( |
|
172 | self._create_fake_hook( | |
173 | file_path, content="RC_HOOK_VER = 'abcde'\n") |
|
173 | file_path, content="RC_HOOK_VER = 'abcde'\n") | |
174 |
|
174 | |||
175 | result = hook_utils.install_svn_hooks(repo.path) |
|
175 | result = hook_utils.install_svn_hooks(repo.path) | |
176 | assert result |
|
176 | assert result | |
177 | self.check_hooks(repo.path) |
|
177 | self.check_hooks(repo.path) | |
178 |
|
178 | |||
179 | def test_non_rc_hooks_are_not_replaced(self): |
|
179 | def test_non_rc_hooks_are_not_replaced(self): | |
180 | repo = self.create_dummy_repo('svn') |
|
180 | repo = self.create_dummy_repo('svn') | |
181 | hooks_path = os.path.join(repo.path, 'hooks') |
|
181 | hooks_path = os.path.join(repo.path, 'hooks') | |
182 | non_rc_content = 'echo "non rc hook"\n' |
|
182 | non_rc_content = 'echo "non rc hook"\n' | |
183 | for file_path in [os.path.join(hooks_path, f) for f in self.HOOK_FILES]: |
|
183 | for file_path in [os.path.join(hooks_path, f) for f in self.HOOK_FILES]: | |
184 | self._create_fake_hook( |
|
184 | self._create_fake_hook( | |
185 | file_path, content=non_rc_content) |
|
185 | file_path, content=non_rc_content) | |
186 |
|
186 | |||
187 | result = hook_utils.install_svn_hooks(repo.path) |
|
187 | result = hook_utils.install_svn_hooks(repo.path) | |
188 | assert result |
|
188 | assert result | |
189 |
|
189 | |||
190 | for file_path in [os.path.join(hooks_path, f) for f in self.HOOK_FILES]: |
|
190 | for file_path in [os.path.join(hooks_path, f) for f in self.HOOK_FILES]: | |
191 | with open(file_path, 'rt') as hook_file: |
|
191 | with open(file_path, 'rt') as hook_file: | |
192 | content = hook_file.read() |
|
192 | content = hook_file.read() | |
193 | assert content == non_rc_content |
|
193 | assert content == non_rc_content | |
194 |
|
194 | |||
195 | def test_non_rc_hooks_are_replaced_with_force_flag(self): |
|
195 | def test_non_rc_hooks_are_replaced_with_force_flag(self): | |
196 | repo = self.create_dummy_repo('svn') |
|
196 | repo = self.create_dummy_repo('svn') | |
197 | hooks_path = os.path.join(repo.path, 'hooks') |
|
197 | hooks_path = os.path.join(repo.path, 'hooks') | |
198 | non_rc_content = 'echo "non rc hook"\n' |
|
198 | non_rc_content = 'echo "non rc hook"\n' | |
199 | for file_path in [os.path.join(hooks_path, f) for f in self.HOOK_FILES]: |
|
199 | for file_path in [os.path.join(hooks_path, f) for f in self.HOOK_FILES]: | |
200 | self._create_fake_hook( |
|
200 | self._create_fake_hook( | |
201 | file_path, content=non_rc_content) |
|
201 | file_path, content=non_rc_content) | |
202 |
|
202 | |||
203 | result = hook_utils.install_svn_hooks( |
|
203 | result = hook_utils.install_svn_hooks( | |
204 | repo.path, force_create=True) |
|
204 | repo.path, force_create=True) | |
205 | assert result |
|
205 | assert result | |
206 | self.check_hooks(repo.path, ) |
|
206 | self.check_hooks(repo.path, ) |
@@ -1,295 +1,295 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import io |
|
18 | import io | |
19 | import more_itertools |
|
19 | import more_itertools | |
20 |
|
20 | |||
21 | import dulwich.protocol |
|
21 | import dulwich.protocol | |
22 | import mock |
|
22 | import mock | |
23 | import pytest |
|
23 | import pytest | |
24 | import webob |
|
24 | import webob | |
25 | import webtest |
|
25 | import webtest | |
26 |
|
26 | |||
27 | from vcsserver import hooks, pygrack |
|
27 | from vcsserver import hooks, pygrack | |
28 |
|
28 | |||
29 | from vcsserver.str_utils import ascii_bytes |
|
29 | from vcsserver.str_utils import ascii_bytes | |
30 |
|
30 | |||
31 |
|
31 | |||
32 | @pytest.fixture() |
|
32 | @pytest.fixture() | |
33 | def pygrack_instance(tmpdir): |
|
33 | def pygrack_instance(tmpdir): | |
34 | """ |
|
34 | """ | |
35 | Creates a pygrack app instance. |
|
35 | Creates a pygrack app instance. | |
36 |
|
36 | |||
37 | Right now, it does not much helpful regarding the passed directory. |
|
37 | Right now, it does not much helpful regarding the passed directory. | |
38 | It just contains the required folders to pass the signature test. |
|
38 | It just contains the required folders to pass the signature test. | |
39 | """ |
|
39 | """ | |
40 | for dir_name in ('config', 'head', 'info', 'objects', 'refs'): |
|
40 | for dir_name in ('config', 'head', 'info', 'objects', 'refs'): | |
41 | tmpdir.mkdir(dir_name) |
|
41 | tmpdir.mkdir(dir_name) | |
42 |
|
42 | |||
43 | return pygrack.GitRepository('repo_name', str(tmpdir), 'git', False, {}) |
|
43 | return pygrack.GitRepository('repo_name', str(tmpdir), 'git', False, {}) | |
44 |
|
44 | |||
45 |
|
45 | |||
46 | @pytest.fixture() |
|
46 | @pytest.fixture() | |
47 | def pygrack_app(pygrack_instance): |
|
47 | def pygrack_app(pygrack_instance): | |
48 | """ |
|
48 | """ | |
49 | Creates a pygrack app wrapped in webtest.TestApp. |
|
49 | Creates a pygrack app wrapped in webtest.TestApp. | |
50 | """ |
|
50 | """ | |
51 | return webtest.TestApp(pygrack_instance) |
|
51 | return webtest.TestApp(pygrack_instance) | |
52 |
|
52 | |||
53 |
|
53 | |||
54 | def test_invalid_service_info_refs_returns_403(pygrack_app): |
|
54 | def test_invalid_service_info_refs_returns_403(pygrack_app): | |
55 | response = pygrack_app.get('/info/refs?service=git-upload-packs', |
|
55 | response = pygrack_app.get('/info/refs?service=git-upload-packs', | |
56 | expect_errors=True) |
|
56 | expect_errors=True) | |
57 |
|
57 | |||
58 | assert response.status_int == 403 |
|
58 | assert response.status_int == 403 | |
59 |
|
59 | |||
60 |
|
60 | |||
61 | def test_invalid_endpoint_returns_403(pygrack_app): |
|
61 | def test_invalid_endpoint_returns_403(pygrack_app): | |
62 | response = pygrack_app.post('/git-upload-packs', expect_errors=True) |
|
62 | response = pygrack_app.post('/git-upload-packs', expect_errors=True) | |
63 |
|
63 | |||
64 | assert response.status_int == 403 |
|
64 | assert response.status_int == 403 | |
65 |
|
65 | |||
66 |
|
66 | |||
67 | @pytest.mark.parametrize('sideband', [ |
|
67 | @pytest.mark.parametrize('sideband', [ | |
68 | 'side-band-64k', |
|
68 | 'side-band-64k', | |
69 | 'side-band', |
|
69 | 'side-band', | |
70 | 'side-band no-progress', |
|
70 | 'side-band no-progress', | |
71 | ]) |
|
71 | ]) | |
72 | def test_pre_pull_hook_fails_with_sideband(pygrack_app, sideband): |
|
72 | def test_pre_pull_hook_fails_with_sideband(pygrack_app, sideband): | |
73 | request = ''.join([ |
|
73 | request = ''.join([ | |
74 | '0054want 74730d410fcb6603ace96f1dc55ea6196122532d ', |
|
74 | '0054want 74730d410fcb6603ace96f1dc55ea6196122532d ', | |
75 |
'multi_ack |
|
75 | f'multi_ack {sideband} ofs-delta\n', | |
76 | '0000', |
|
76 | '0000', | |
77 | '0009done\n', |
|
77 | '0009done\n', | |
78 | ]) |
|
78 | ]) | |
79 | with mock.patch('vcsserver.hooks.git_pre_pull', return_value=hooks.HookResponse(1, 'foo')): |
|
79 | with mock.patch('vcsserver.hooks.git_pre_pull', return_value=hooks.HookResponse(1, 'foo')): | |
80 | response = pygrack_app.post( |
|
80 | response = pygrack_app.post( | |
81 | '/git-upload-pack', params=request, |
|
81 | '/git-upload-pack', params=request, | |
82 | content_type='application/x-git-upload-pack') |
|
82 | content_type='application/x-git-upload-pack') | |
83 |
|
83 | |||
84 | data = io.BytesIO(response.body) |
|
84 | data = io.BytesIO(response.body) | |
85 | proto = dulwich.protocol.Protocol(data.read, None) |
|
85 | proto = dulwich.protocol.Protocol(data.read, None) | |
86 | packets = list(proto.read_pkt_seq()) |
|
86 | packets = list(proto.read_pkt_seq()) | |
87 |
|
87 | |||
88 | expected_packets = [ |
|
88 | expected_packets = [ | |
89 | b'NAK\n', b'\x02foo', b'\x02Pre pull hook failed: aborting\n', |
|
89 | b'NAK\n', b'\x02foo', b'\x02Pre pull hook failed: aborting\n', | |
90 | b'\x01' + pygrack.GitRepository.EMPTY_PACK, |
|
90 | b'\x01' + pygrack.GitRepository.EMPTY_PACK, | |
91 | ] |
|
91 | ] | |
92 | assert packets == expected_packets |
|
92 | assert packets == expected_packets | |
93 |
|
93 | |||
94 |
|
94 | |||
95 | def test_pre_pull_hook_fails_no_sideband(pygrack_app): |
|
95 | def test_pre_pull_hook_fails_no_sideband(pygrack_app): | |
96 | request = ''.join([ |
|
96 | request = ''.join([ | |
97 | '0054want 74730d410fcb6603ace96f1dc55ea6196122532d ' + |
|
97 | '0054want 74730d410fcb6603ace96f1dc55ea6196122532d ' + | |
98 | 'multi_ack ofs-delta\n' |
|
98 | 'multi_ack ofs-delta\n' | |
99 | '0000', |
|
99 | '0000', | |
100 | '0009done\n', |
|
100 | '0009done\n', | |
101 | ]) |
|
101 | ]) | |
102 | with mock.patch('vcsserver.hooks.git_pre_pull', |
|
102 | with mock.patch('vcsserver.hooks.git_pre_pull', | |
103 | return_value=hooks.HookResponse(1, 'foo')): |
|
103 | return_value=hooks.HookResponse(1, 'foo')): | |
104 | response = pygrack_app.post( |
|
104 | response = pygrack_app.post( | |
105 | '/git-upload-pack', params=request, |
|
105 | '/git-upload-pack', params=request, | |
106 | content_type='application/x-git-upload-pack') |
|
106 | content_type='application/x-git-upload-pack') | |
107 |
|
107 | |||
108 | assert response.body == pygrack.GitRepository.EMPTY_PACK |
|
108 | assert response.body == pygrack.GitRepository.EMPTY_PACK | |
109 |
|
109 | |||
110 |
|
110 | |||
111 | def test_pull_has_hook_messages(pygrack_app): |
|
111 | def test_pull_has_hook_messages(pygrack_app): | |
112 | request = ''.join([ |
|
112 | request = ''.join([ | |
113 | '0054want 74730d410fcb6603ace96f1dc55ea6196122532d ' + |
|
113 | '0054want 74730d410fcb6603ace96f1dc55ea6196122532d ' + | |
114 | 'multi_ack side-band-64k ofs-delta\n' |
|
114 | 'multi_ack side-band-64k ofs-delta\n' | |
115 | '0000', |
|
115 | '0000', | |
116 | '0009done\n', |
|
116 | '0009done\n', | |
117 | ]) |
|
117 | ]) | |
118 |
|
118 | |||
119 | pre_pull = 'pre_pull_output' |
|
119 | pre_pull = 'pre_pull_output' | |
120 | post_pull = 'post_pull_output' |
|
120 | post_pull = 'post_pull_output' | |
121 |
|
121 | |||
122 | with mock.patch('vcsserver.hooks.git_pre_pull', |
|
122 | with mock.patch('vcsserver.hooks.git_pre_pull', | |
123 | return_value=hooks.HookResponse(0, pre_pull)): |
|
123 | return_value=hooks.HookResponse(0, pre_pull)): | |
124 | with mock.patch('vcsserver.hooks.git_post_pull', |
|
124 | with mock.patch('vcsserver.hooks.git_post_pull', | |
125 | return_value=hooks.HookResponse(1, post_pull)): |
|
125 | return_value=hooks.HookResponse(1, post_pull)): | |
126 | with mock.patch('vcsserver.subprocessio.SubprocessIOChunker', |
|
126 | with mock.patch('vcsserver.subprocessio.SubprocessIOChunker', | |
127 | return_value=more_itertools.always_iterable([b'0008NAK\n0009subp\n0000'])): |
|
127 | return_value=more_itertools.always_iterable([b'0008NAK\n0009subp\n0000'])): | |
128 | response = pygrack_app.post( |
|
128 | response = pygrack_app.post( | |
129 | '/git-upload-pack', params=request, |
|
129 | '/git-upload-pack', params=request, | |
130 | content_type='application/x-git-upload-pack') |
|
130 | content_type='application/x-git-upload-pack') | |
131 |
|
131 | |||
132 | data = io.BytesIO(response.body) |
|
132 | data = io.BytesIO(response.body) | |
133 | proto = dulwich.protocol.Protocol(data.read, None) |
|
133 | proto = dulwich.protocol.Protocol(data.read, None) | |
134 | packets = list(proto.read_pkt_seq()) |
|
134 | packets = list(proto.read_pkt_seq()) | |
135 |
|
135 | |||
136 | assert packets == [b'NAK\n', |
|
136 | assert packets == [b'NAK\n', | |
137 | # pre-pull only outputs if IT FAILS as in != 0 ret code |
|
137 | # pre-pull only outputs if IT FAILS as in != 0 ret code | |
138 | #b'\x02pre_pull_output', |
|
138 | #b'\x02pre_pull_output', | |
139 | b'subp\n', |
|
139 | b'subp\n', | |
140 | b'\x02post_pull_output'] |
|
140 | b'\x02post_pull_output'] | |
141 |
|
141 | |||
142 |
|
142 | |||
143 | def test_get_want_capabilities(pygrack_instance): |
|
143 | def test_get_want_capabilities(pygrack_instance): | |
144 | data = io.BytesIO( |
|
144 | data = io.BytesIO( | |
145 | b'0054want 74730d410fcb6603ace96f1dc55ea6196122532d ' + |
|
145 | b'0054want 74730d410fcb6603ace96f1dc55ea6196122532d ' + | |
146 | b'multi_ack side-band-64k ofs-delta\n00000009done\n') |
|
146 | b'multi_ack side-band-64k ofs-delta\n00000009done\n') | |
147 |
|
147 | |||
148 | request = webob.Request({ |
|
148 | request = webob.Request({ | |
149 | 'wsgi.input': data, |
|
149 | 'wsgi.input': data, | |
150 | 'REQUEST_METHOD': 'POST', |
|
150 | 'REQUEST_METHOD': 'POST', | |
151 | 'webob.is_body_seekable': True |
|
151 | 'webob.is_body_seekable': True | |
152 | }) |
|
152 | }) | |
153 |
|
153 | |||
154 | capabilities = pygrack_instance._get_want_capabilities(request) |
|
154 | capabilities = pygrack_instance._get_want_capabilities(request) | |
155 |
|
155 | |||
156 | assert capabilities == frozenset( |
|
156 | assert capabilities == frozenset( | |
157 | (b'ofs-delta', b'multi_ack', b'side-band-64k')) |
|
157 | (b'ofs-delta', b'multi_ack', b'side-band-64k')) | |
158 | assert data.tell() == 0 |
|
158 | assert data.tell() == 0 | |
159 |
|
159 | |||
160 |
|
160 | |||
161 | @pytest.mark.parametrize('data,capabilities,expected', [ |
|
161 | @pytest.mark.parametrize('data,capabilities,expected', [ | |
162 | ('foo', [], []), |
|
162 | ('foo', [], []), | |
163 | ('', [pygrack.CAPABILITY_SIDE_BAND_64K], []), |
|
163 | ('', [pygrack.CAPABILITY_SIDE_BAND_64K], []), | |
164 | ('', [pygrack.CAPABILITY_SIDE_BAND], []), |
|
164 | ('', [pygrack.CAPABILITY_SIDE_BAND], []), | |
165 | ('foo', [pygrack.CAPABILITY_SIDE_BAND_64K], [b'0008\x02foo']), |
|
165 | ('foo', [pygrack.CAPABILITY_SIDE_BAND_64K], [b'0008\x02foo']), | |
166 | ('foo', [pygrack.CAPABILITY_SIDE_BAND], [b'0008\x02foo']), |
|
166 | ('foo', [pygrack.CAPABILITY_SIDE_BAND], [b'0008\x02foo']), | |
167 | ('f'*1000, [pygrack.CAPABILITY_SIDE_BAND_64K], [b'03ed\x02' + b'f' * 1000]), |
|
167 | ('f'*1000, [pygrack.CAPABILITY_SIDE_BAND_64K], [b'03ed\x02' + b'f' * 1000]), | |
168 | ('f'*1000, [pygrack.CAPABILITY_SIDE_BAND], [b'03e8\x02' + b'f' * 995, b'000a\x02fffff']), |
|
168 | ('f'*1000, [pygrack.CAPABILITY_SIDE_BAND], [b'03e8\x02' + b'f' * 995, b'000a\x02fffff']), | |
169 | ('f'*65520, [pygrack.CAPABILITY_SIDE_BAND_64K], [b'fff0\x02' + b'f' * 65515, b'000a\x02fffff']), |
|
169 | ('f'*65520, [pygrack.CAPABILITY_SIDE_BAND_64K], [b'fff0\x02' + b'f' * 65515, b'000a\x02fffff']), | |
170 | ('f'*65520, [pygrack.CAPABILITY_SIDE_BAND], [b'03e8\x02' + b'f' * 995] * 65 + [b'0352\x02' + b'f' * 845]), |
|
170 | ('f'*65520, [pygrack.CAPABILITY_SIDE_BAND], [b'03e8\x02' + b'f' * 995] * 65 + [b'0352\x02' + b'f' * 845]), | |
171 | ], ids=[ |
|
171 | ], ids=[ | |
172 | 'foo-empty', |
|
172 | 'foo-empty', | |
173 | 'empty-64k', 'empty', |
|
173 | 'empty-64k', 'empty', | |
174 | 'foo-64k', 'foo', |
|
174 | 'foo-64k', 'foo', | |
175 | 'f-1000-64k', 'f-1000', |
|
175 | 'f-1000-64k', 'f-1000', | |
176 | 'f-65520-64k', 'f-65520']) |
|
176 | 'f-65520-64k', 'f-65520']) | |
177 | def test_get_messages(pygrack_instance, data, capabilities, expected): |
|
177 | def test_get_messages(pygrack_instance, data, capabilities, expected): | |
178 | messages = pygrack_instance._get_messages(data, capabilities) |
|
178 | messages = pygrack_instance._get_messages(data, capabilities) | |
179 |
|
179 | |||
180 | assert messages == expected |
|
180 | assert messages == expected | |
181 |
|
181 | |||
182 |
|
182 | |||
183 | @pytest.mark.parametrize('response,capabilities,pre_pull_messages,post_pull_messages', [ |
|
183 | @pytest.mark.parametrize('response,capabilities,pre_pull_messages,post_pull_messages', [ | |
184 | # Unexpected response |
|
184 | # Unexpected response | |
185 | ([b'unexpected_response[no_initial_header]'], [pygrack.CAPABILITY_SIDE_BAND_64K], 'foo', 'bar'), |
|
185 | ([b'unexpected_response[no_initial_header]'], [pygrack.CAPABILITY_SIDE_BAND_64K], 'foo', 'bar'), | |
186 | # No sideband |
|
186 | # No sideband | |
187 | ([b'no-sideband'], [], 'foo', 'bar'), |
|
187 | ([b'no-sideband'], [], 'foo', 'bar'), | |
188 | # No messages |
|
188 | # No messages | |
189 | ([b'no-messages'], [pygrack.CAPABILITY_SIDE_BAND_64K], '', ''), |
|
189 | ([b'no-messages'], [pygrack.CAPABILITY_SIDE_BAND_64K], '', ''), | |
190 | ]) |
|
190 | ]) | |
191 | def test_inject_messages_to_response_nothing_to_do( |
|
191 | def test_inject_messages_to_response_nothing_to_do( | |
192 | pygrack_instance, response, capabilities, pre_pull_messages, post_pull_messages): |
|
192 | pygrack_instance, response, capabilities, pre_pull_messages, post_pull_messages): | |
193 |
|
193 | |||
194 | new_response = pygrack_instance._build_post_pull_response( |
|
194 | new_response = pygrack_instance._build_post_pull_response( | |
195 | more_itertools.always_iterable(response), capabilities, pre_pull_messages, post_pull_messages) |
|
195 | more_itertools.always_iterable(response), capabilities, pre_pull_messages, post_pull_messages) | |
196 |
|
196 | |||
197 | assert list(new_response) == response |
|
197 | assert list(new_response) == response | |
198 |
|
198 | |||
199 |
|
199 | |||
200 | @pytest.mark.parametrize('capabilities', [ |
|
200 | @pytest.mark.parametrize('capabilities', [ | |
201 | [pygrack.CAPABILITY_SIDE_BAND], |
|
201 | [pygrack.CAPABILITY_SIDE_BAND], | |
202 | [pygrack.CAPABILITY_SIDE_BAND_64K], |
|
202 | [pygrack.CAPABILITY_SIDE_BAND_64K], | |
203 | ]) |
|
203 | ]) | |
204 | def test_inject_messages_to_response_single_element(pygrack_instance, capabilities): |
|
204 | def test_inject_messages_to_response_single_element(pygrack_instance, capabilities): | |
205 | response = [b'0008NAK\n0009subp\n0000'] |
|
205 | response = [b'0008NAK\n0009subp\n0000'] | |
206 | new_response = pygrack_instance._build_post_pull_response( |
|
206 | new_response = pygrack_instance._build_post_pull_response( | |
207 | more_itertools.always_iterable(response), capabilities, 'foo', 'bar') |
|
207 | more_itertools.always_iterable(response), capabilities, 'foo', 'bar') | |
208 |
|
208 | |||
209 | expected_response = b''.join([ |
|
209 | expected_response = b''.join([ | |
210 | b'0008NAK\n', |
|
210 | b'0008NAK\n', | |
211 | b'0008\x02foo', |
|
211 | b'0008\x02foo', | |
212 | b'0009subp\n', |
|
212 | b'0009subp\n', | |
213 | b'0008\x02bar', |
|
213 | b'0008\x02bar', | |
214 | b'0000']) |
|
214 | b'0000']) | |
215 |
|
215 | |||
216 | assert b''.join(new_response) == expected_response |
|
216 | assert b''.join(new_response) == expected_response | |
217 |
|
217 | |||
218 |
|
218 | |||
219 | @pytest.mark.parametrize('capabilities', [ |
|
219 | @pytest.mark.parametrize('capabilities', [ | |
220 | [pygrack.CAPABILITY_SIDE_BAND], |
|
220 | [pygrack.CAPABILITY_SIDE_BAND], | |
221 | [pygrack.CAPABILITY_SIDE_BAND_64K], |
|
221 | [pygrack.CAPABILITY_SIDE_BAND_64K], | |
222 | ]) |
|
222 | ]) | |
223 | def test_inject_messages_to_response_multi_element(pygrack_instance, capabilities): |
|
223 | def test_inject_messages_to_response_multi_element(pygrack_instance, capabilities): | |
224 | response = more_itertools.always_iterable([ |
|
224 | response = more_itertools.always_iterable([ | |
225 | b'0008NAK\n000asubp1\n', b'000asubp2\n', b'000asubp3\n', b'000asubp4\n0000' |
|
225 | b'0008NAK\n000asubp1\n', b'000asubp2\n', b'000asubp3\n', b'000asubp4\n0000' | |
226 | ]) |
|
226 | ]) | |
227 | new_response = pygrack_instance._build_post_pull_response(response, capabilities, 'foo', 'bar') |
|
227 | new_response = pygrack_instance._build_post_pull_response(response, capabilities, 'foo', 'bar') | |
228 |
|
228 | |||
229 | expected_response = b''.join([ |
|
229 | expected_response = b''.join([ | |
230 | b'0008NAK\n', |
|
230 | b'0008NAK\n', | |
231 | b'0008\x02foo', |
|
231 | b'0008\x02foo', | |
232 | b'000asubp1\n', b'000asubp2\n', b'000asubp3\n', b'000asubp4\n', |
|
232 | b'000asubp1\n', b'000asubp2\n', b'000asubp3\n', b'000asubp4\n', | |
233 | b'0008\x02bar', |
|
233 | b'0008\x02bar', | |
234 | b'0000' |
|
234 | b'0000' | |
235 | ]) |
|
235 | ]) | |
236 |
|
236 | |||
237 | assert b''.join(new_response) == expected_response |
|
237 | assert b''.join(new_response) == expected_response | |
238 |
|
238 | |||
239 |
|
239 | |||
240 | def test_build_failed_pre_pull_response_no_sideband(pygrack_instance): |
|
240 | def test_build_failed_pre_pull_response_no_sideband(pygrack_instance): | |
241 | response = pygrack_instance._build_failed_pre_pull_response([], 'foo') |
|
241 | response = pygrack_instance._build_failed_pre_pull_response([], 'foo') | |
242 |
|
242 | |||
243 | assert response == [pygrack.GitRepository.EMPTY_PACK] |
|
243 | assert response == [pygrack.GitRepository.EMPTY_PACK] | |
244 |
|
244 | |||
245 |
|
245 | |||
246 | @pytest.mark.parametrize('capabilities', [ |
|
246 | @pytest.mark.parametrize('capabilities', [ | |
247 | [pygrack.CAPABILITY_SIDE_BAND], |
|
247 | [pygrack.CAPABILITY_SIDE_BAND], | |
248 | [pygrack.CAPABILITY_SIDE_BAND_64K], |
|
248 | [pygrack.CAPABILITY_SIDE_BAND_64K], | |
249 | [pygrack.CAPABILITY_SIDE_BAND_64K, b'no-progress'], |
|
249 | [pygrack.CAPABILITY_SIDE_BAND_64K, b'no-progress'], | |
250 | ]) |
|
250 | ]) | |
251 | def test_build_failed_pre_pull_response(pygrack_instance, capabilities): |
|
251 | def test_build_failed_pre_pull_response(pygrack_instance, capabilities): | |
252 | response = pygrack_instance._build_failed_pre_pull_response(capabilities, 'foo') |
|
252 | response = pygrack_instance._build_failed_pre_pull_response(capabilities, 'foo') | |
253 |
|
253 | |||
254 | expected_response = [ |
|
254 | expected_response = [ | |
255 | b'0008NAK\n', b'0008\x02foo', b'0024\x02Pre pull hook failed: aborting\n', |
|
255 | b'0008NAK\n', b'0008\x02foo', b'0024\x02Pre pull hook failed: aborting\n', | |
256 | b'%04x\x01%s' % (len(pygrack.GitRepository.EMPTY_PACK) + 5, pygrack.GitRepository.EMPTY_PACK), |
|
256 | b'%04x\x01%s' % (len(pygrack.GitRepository.EMPTY_PACK) + 5, pygrack.GitRepository.EMPTY_PACK), | |
257 | pygrack.GitRepository.FLUSH_PACKET, |
|
257 | pygrack.GitRepository.FLUSH_PACKET, | |
258 | ] |
|
258 | ] | |
259 |
|
259 | |||
260 | assert response == expected_response |
|
260 | assert response == expected_response | |
261 |
|
261 | |||
262 |
|
262 | |||
263 | def test_inject_messages_to_response_generator(pygrack_instance): |
|
263 | def test_inject_messages_to_response_generator(pygrack_instance): | |
264 |
|
264 | |||
265 | def response_generator(): |
|
265 | def response_generator(): | |
266 | response = [ |
|
266 | response = [ | |
267 | # protocol start |
|
267 | # protocol start | |
268 | b'0008NAK\n', |
|
268 | b'0008NAK\n', | |
269 | ] |
|
269 | ] | |
270 | response += [ascii_bytes(f'000asubp{x}\n') for x in range(1000)] |
|
270 | response += [ascii_bytes(f'000asubp{x}\n') for x in range(1000)] | |
271 | response += [ |
|
271 | response += [ | |
272 | # protocol end |
|
272 | # protocol end | |
273 | pygrack.GitRepository.FLUSH_PACKET |
|
273 | pygrack.GitRepository.FLUSH_PACKET | |
274 | ] |
|
274 | ] | |
275 | for elem in response: |
|
275 | for elem in response: | |
276 | yield elem |
|
276 | yield elem | |
277 |
|
277 | |||
278 | new_response = pygrack_instance._build_post_pull_response( |
|
278 | new_response = pygrack_instance._build_post_pull_response( | |
279 | response_generator(), [pygrack.CAPABILITY_SIDE_BAND_64K, b'no-progress'], 'PRE_PULL_MSG\n', 'POST_PULL_MSG\n') |
|
279 | response_generator(), [pygrack.CAPABILITY_SIDE_BAND_64K, b'no-progress'], 'PRE_PULL_MSG\n', 'POST_PULL_MSG\n') | |
280 |
|
280 | |||
281 | assert iter(new_response) |
|
281 | assert iter(new_response) | |
282 |
|
282 | |||
283 | expected_response = b''.join([ |
|
283 | expected_response = b''.join([ | |
284 | # start |
|
284 | # start | |
285 | b'0008NAK\n0012\x02PRE_PULL_MSG\n', |
|
285 | b'0008NAK\n0012\x02PRE_PULL_MSG\n', | |
286 | ] + [ |
|
286 | ] + [ | |
287 | # ... rest |
|
287 | # ... rest | |
288 | ascii_bytes(f'000asubp{x}\n') for x in range(1000) |
|
288 | ascii_bytes(f'000asubp{x}\n') for x in range(1000) | |
289 | ] + [ |
|
289 | ] + [ | |
290 | # final message, |
|
290 | # final message, | |
291 | b'0013\x02POST_PULL_MSG\n0000', |
|
291 | b'0013\x02POST_PULL_MSG\n0000', | |
292 |
|
292 | |||
293 | ]) |
|
293 | ]) | |
294 |
|
294 | |||
295 | assert b''.join(new_response) == expected_response |
|
295 | assert b''.join(new_response) == expected_response |
@@ -1,155 +1,155 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import io |
|
18 | import io | |
19 | import os |
|
19 | import os | |
20 | import sys |
|
20 | import sys | |
21 |
|
21 | |||
22 | import pytest |
|
22 | import pytest | |
23 |
|
23 | |||
24 | from vcsserver import subprocessio |
|
24 | from vcsserver import subprocessio | |
25 | from vcsserver.str_utils import ascii_bytes |
|
25 | from vcsserver.str_utils import ascii_bytes | |
26 |
|
26 | |||
27 |
|
27 | |||
28 |
class FileLikeObj |
|
28 | class FileLikeObj: # pragma: no cover | |
29 |
|
29 | |||
30 | def __init__(self, data: bytes, size): |
|
30 | def __init__(self, data: bytes, size): | |
31 | chunks = size // len(data) |
|
31 | chunks = size // len(data) | |
32 |
|
32 | |||
33 | self.stream = self._get_stream(data, chunks) |
|
33 | self.stream = self._get_stream(data, chunks) | |
34 |
|
34 | |||
35 | def _get_stream(self, data, chunks): |
|
35 | def _get_stream(self, data, chunks): | |
36 | for x in range(chunks): |
|
36 | for x in range(chunks): | |
37 | yield data |
|
37 | yield data | |
38 |
|
38 | |||
39 | def read(self, n): |
|
39 | def read(self, n): | |
40 |
|
40 | |||
41 | buffer_stream = b'' |
|
41 | buffer_stream = b'' | |
42 | for chunk in self.stream: |
|
42 | for chunk in self.stream: | |
43 | buffer_stream += chunk |
|
43 | buffer_stream += chunk | |
44 | if len(buffer_stream) >= n: |
|
44 | if len(buffer_stream) >= n: | |
45 | break |
|
45 | break | |
46 |
|
46 | |||
47 | # self.stream = self.bytes[n:] |
|
47 | # self.stream = self.bytes[n:] | |
48 | return buffer_stream |
|
48 | return buffer_stream | |
49 |
|
49 | |||
50 |
|
50 | |||
51 | @pytest.fixture(scope='module') |
|
51 | @pytest.fixture(scope='module') | |
52 | def environ(): |
|
52 | def environ(): | |
53 | """Delete coverage variables, as they make the tests fail.""" |
|
53 | """Delete coverage variables, as they make the tests fail.""" | |
54 | env = dict(os.environ) |
|
54 | env = dict(os.environ) | |
55 | for key in list(env.keys()): |
|
55 | for key in list(env.keys()): | |
56 | if key.startswith('COV_CORE_'): |
|
56 | if key.startswith('COV_CORE_'): | |
57 | del env[key] |
|
57 | del env[key] | |
58 |
|
58 | |||
59 | return env |
|
59 | return env | |
60 |
|
60 | |||
61 |
|
61 | |||
62 | def _get_python_args(script): |
|
62 | def _get_python_args(script): | |
63 | return [sys.executable, '-c', 'import sys; import time; import shutil; ' + script] |
|
63 | return [sys.executable, '-c', 'import sys; import time; import shutil; ' + script] | |
64 |
|
64 | |||
65 |
|
65 | |||
66 | def test_raise_exception_on_non_zero_return_code(environ): |
|
66 | def test_raise_exception_on_non_zero_return_code(environ): | |
67 | call_args = _get_python_args('raise ValueError("fail")') |
|
67 | call_args = _get_python_args('raise ValueError("fail")') | |
68 | with pytest.raises(OSError): |
|
68 | with pytest.raises(OSError): | |
69 | b''.join(subprocessio.SubprocessIOChunker(call_args, shell=False, env=environ)) |
|
69 | b''.join(subprocessio.SubprocessIOChunker(call_args, shell=False, env=environ)) | |
70 |
|
70 | |||
71 |
|
71 | |||
72 | def test_does_not_fail_on_non_zero_return_code(environ): |
|
72 | def test_does_not_fail_on_non_zero_return_code(environ): | |
73 | call_args = _get_python_args('sys.stdout.write("hello"); sys.exit(1)') |
|
73 | call_args = _get_python_args('sys.stdout.write("hello"); sys.exit(1)') | |
74 | proc = subprocessio.SubprocessIOChunker(call_args, shell=False, fail_on_return_code=False, env=environ) |
|
74 | proc = subprocessio.SubprocessIOChunker(call_args, shell=False, fail_on_return_code=False, env=environ) | |
75 | output = b''.join(proc) |
|
75 | output = b''.join(proc) | |
76 |
|
76 | |||
77 | assert output == b'hello' |
|
77 | assert output == b'hello' | |
78 |
|
78 | |||
79 |
|
79 | |||
80 | def test_raise_exception_on_stderr(environ): |
|
80 | def test_raise_exception_on_stderr(environ): | |
81 | call_args = _get_python_args('sys.stderr.write("WRITE_TO_STDERR"); time.sleep(1);') |
|
81 | call_args = _get_python_args('sys.stderr.write("WRITE_TO_STDERR"); time.sleep(1);') | |
82 |
|
82 | |||
83 | with pytest.raises(OSError) as excinfo: |
|
83 | with pytest.raises(OSError) as excinfo: | |
84 | b''.join(subprocessio.SubprocessIOChunker(call_args, shell=False, env=environ)) |
|
84 | b''.join(subprocessio.SubprocessIOChunker(call_args, shell=False, env=environ)) | |
85 |
|
85 | |||
86 | assert 'exited due to an error:\nWRITE_TO_STDERR' in str(excinfo.value) |
|
86 | assert 'exited due to an error:\nWRITE_TO_STDERR' in str(excinfo.value) | |
87 |
|
87 | |||
88 |
|
88 | |||
89 | def test_does_not_fail_on_stderr(environ): |
|
89 | def test_does_not_fail_on_stderr(environ): | |
90 | call_args = _get_python_args('sys.stderr.write("WRITE_TO_STDERR"); sys.stderr.flush; time.sleep(2);') |
|
90 | call_args = _get_python_args('sys.stderr.write("WRITE_TO_STDERR"); sys.stderr.flush; time.sleep(2);') | |
91 | proc = subprocessio.SubprocessIOChunker(call_args, shell=False, fail_on_stderr=False, env=environ) |
|
91 | proc = subprocessio.SubprocessIOChunker(call_args, shell=False, fail_on_stderr=False, env=environ) | |
92 | output = b''.join(proc) |
|
92 | output = b''.join(proc) | |
93 |
|
93 | |||
94 | assert output == b'' |
|
94 | assert output == b'' | |
95 |
|
95 | |||
96 |
|
96 | |||
97 | @pytest.mark.parametrize('size', [ |
|
97 | @pytest.mark.parametrize('size', [ | |
98 | 1, |
|
98 | 1, | |
99 | 10 ** 5 |
|
99 | 10 ** 5 | |
100 | ]) |
|
100 | ]) | |
101 | def test_output_with_no_input(size, environ): |
|
101 | def test_output_with_no_input(size, environ): | |
102 | call_args = _get_python_args(f'sys.stdout.write("X" * {size});') |
|
102 | call_args = _get_python_args(f'sys.stdout.write("X" * {size});') | |
103 | proc = subprocessio.SubprocessIOChunker(call_args, shell=False, env=environ) |
|
103 | proc = subprocessio.SubprocessIOChunker(call_args, shell=False, env=environ) | |
104 | output = b''.join(proc) |
|
104 | output = b''.join(proc) | |
105 |
|
105 | |||
106 | assert output == ascii_bytes("X" * size) |
|
106 | assert output == ascii_bytes("X" * size) | |
107 |
|
107 | |||
108 |
|
108 | |||
109 | @pytest.mark.parametrize('size', [ |
|
109 | @pytest.mark.parametrize('size', [ | |
110 | 1, |
|
110 | 1, | |
111 | 10 ** 5 |
|
111 | 10 ** 5 | |
112 | ]) |
|
112 | ]) | |
113 | def test_output_with_no_input_does_not_fail(size, environ): |
|
113 | def test_output_with_no_input_does_not_fail(size, environ): | |
114 |
|
114 | |||
115 | call_args = _get_python_args(f'sys.stdout.write("X" * {size}); sys.exit(1)') |
|
115 | call_args = _get_python_args(f'sys.stdout.write("X" * {size}); sys.exit(1)') | |
116 | proc = subprocessio.SubprocessIOChunker(call_args, shell=False, fail_on_return_code=False, env=environ) |
|
116 | proc = subprocessio.SubprocessIOChunker(call_args, shell=False, fail_on_return_code=False, env=environ) | |
117 | output = b''.join(proc) |
|
117 | output = b''.join(proc) | |
118 |
|
118 | |||
119 | assert output == ascii_bytes("X" * size) |
|
119 | assert output == ascii_bytes("X" * size) | |
120 |
|
120 | |||
121 |
|
121 | |||
122 | @pytest.mark.parametrize('size', [ |
|
122 | @pytest.mark.parametrize('size', [ | |
123 | 1, |
|
123 | 1, | |
124 | 10 ** 5 |
|
124 | 10 ** 5 | |
125 | ]) |
|
125 | ]) | |
126 | def test_output_with_input(size, environ): |
|
126 | def test_output_with_input(size, environ): | |
127 | data_len = size |
|
127 | data_len = size | |
128 | inputstream = FileLikeObj(b'X', size) |
|
128 | inputstream = FileLikeObj(b'X', size) | |
129 |
|
129 | |||
130 | # This acts like the cat command. |
|
130 | # This acts like the cat command. | |
131 | call_args = _get_python_args('shutil.copyfileobj(sys.stdin, sys.stdout)') |
|
131 | call_args = _get_python_args('shutil.copyfileobj(sys.stdin, sys.stdout)') | |
132 | # note: in this tests we explicitly don't assign chunker to a variable and let it stream directly |
|
132 | # note: in this tests we explicitly don't assign chunker to a variable and let it stream directly | |
133 | output = b''.join( |
|
133 | output = b''.join( | |
134 | subprocessio.SubprocessIOChunker(call_args, shell=False, input_stream=inputstream, env=environ) |
|
134 | subprocessio.SubprocessIOChunker(call_args, shell=False, input_stream=inputstream, env=environ) | |
135 | ) |
|
135 | ) | |
136 |
|
136 | |||
137 | assert len(output) == data_len |
|
137 | assert len(output) == data_len | |
138 |
|
138 | |||
139 |
|
139 | |||
140 | @pytest.mark.parametrize('size', [ |
|
140 | @pytest.mark.parametrize('size', [ | |
141 | 1, |
|
141 | 1, | |
142 | 10 ** 5 |
|
142 | 10 ** 5 | |
143 | ]) |
|
143 | ]) | |
144 | def test_output_with_input_skipping_iterator(size, environ): |
|
144 | def test_output_with_input_skipping_iterator(size, environ): | |
145 | data_len = size |
|
145 | data_len = size | |
146 | inputstream = FileLikeObj(b'X', size) |
|
146 | inputstream = FileLikeObj(b'X', size) | |
147 |
|
147 | |||
148 | # This acts like the cat command. |
|
148 | # This acts like the cat command. | |
149 | call_args = _get_python_args('shutil.copyfileobj(sys.stdin, sys.stdout)') |
|
149 | call_args = _get_python_args('shutil.copyfileobj(sys.stdin, sys.stdout)') | |
150 |
|
150 | |||
151 | # Note: assigning the chunker makes sure that it is not deleted too early |
|
151 | # Note: assigning the chunker makes sure that it is not deleted too early | |
152 | proc = subprocessio.SubprocessIOChunker(call_args, shell=False, input_stream=inputstream, env=environ) |
|
152 | proc = subprocessio.SubprocessIOChunker(call_args, shell=False, input_stream=inputstream, env=environ) | |
153 | output = b''.join(proc.stdout) |
|
153 | output = b''.join(proc.stdout) | |
154 |
|
154 | |||
155 | assert len(output) == data_len |
|
155 | assert len(output) == data_len |
@@ -1,103 +1,103 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | import io |
|
18 | import io | |
19 | import mock |
|
19 | import mock | |
20 | import pytest |
|
20 | import pytest | |
21 | import sys |
|
21 | import sys | |
22 |
|
22 | |||
23 | from vcsserver.str_utils import ascii_bytes |
|
23 | from vcsserver.str_utils import ascii_bytes | |
24 |
|
24 | |||
25 |
|
25 | |||
26 |
class MockPopen |
|
26 | class MockPopen: | |
27 | def __init__(self, stderr): |
|
27 | def __init__(self, stderr): | |
28 | self.stdout = io.BytesIO(b'') |
|
28 | self.stdout = io.BytesIO(b'') | |
29 | self.stderr = io.BytesIO(stderr) |
|
29 | self.stderr = io.BytesIO(stderr) | |
30 | self.returncode = 1 |
|
30 | self.returncode = 1 | |
31 |
|
31 | |||
32 | def wait(self): |
|
32 | def wait(self): | |
33 | pass |
|
33 | pass | |
34 |
|
34 | |||
35 |
|
35 | |||
36 | INVALID_CERTIFICATE_STDERR = '\n'.join([ |
|
36 | INVALID_CERTIFICATE_STDERR = '\n'.join([ | |
37 | 'svnrdump: E230001: Unable to connect to a repository at URL url', |
|
37 | 'svnrdump: E230001: Unable to connect to a repository at URL url', | |
38 | 'svnrdump: E230001: Server SSL certificate verification failed: issuer is not trusted', |
|
38 | 'svnrdump: E230001: Server SSL certificate verification failed: issuer is not trusted', | |
39 | ]) |
|
39 | ]) | |
40 |
|
40 | |||
41 |
|
41 | |||
42 | @pytest.mark.parametrize('stderr,expected_reason', [ |
|
42 | @pytest.mark.parametrize('stderr,expected_reason', [ | |
43 | (INVALID_CERTIFICATE_STDERR, 'INVALID_CERTIFICATE'), |
|
43 | (INVALID_CERTIFICATE_STDERR, 'INVALID_CERTIFICATE'), | |
44 | ('svnrdump: E123456', 'UNKNOWN:svnrdump: E123456'), |
|
44 | ('svnrdump: E123456', 'UNKNOWN:svnrdump: E123456'), | |
45 | ], ids=['invalid-cert-stderr', 'svnrdump-err-123456']) |
|
45 | ], ids=['invalid-cert-stderr', 'svnrdump-err-123456']) | |
46 | @pytest.mark.xfail(sys.platform == "cygwin", |
|
46 | @pytest.mark.xfail(sys.platform == "cygwin", | |
47 | reason="SVN not packaged for Cygwin") |
|
47 | reason="SVN not packaged for Cygwin") | |
48 | def test_import_remote_repository_certificate_error(stderr, expected_reason): |
|
48 | def test_import_remote_repository_certificate_error(stderr, expected_reason): | |
49 | from vcsserver.remote import svn_remote |
|
49 | from vcsserver.remote import svn_remote | |
50 | factory = mock.Mock() |
|
50 | factory = mock.Mock() | |
51 | factory.repo = mock.Mock(return_value=mock.Mock()) |
|
51 | factory.repo = mock.Mock(return_value=mock.Mock()) | |
52 |
|
52 | |||
53 | remote = svn_remote.SvnRemote(factory) |
|
53 | remote = svn_remote.SvnRemote(factory) | |
54 | remote.is_path_valid_repository = lambda wire, path: True |
|
54 | remote.is_path_valid_repository = lambda wire, path: True | |
55 |
|
55 | |||
56 | with mock.patch('subprocess.Popen', |
|
56 | with mock.patch('subprocess.Popen', | |
57 | return_value=MockPopen(ascii_bytes(stderr))): |
|
57 | return_value=MockPopen(ascii_bytes(stderr))): | |
58 | with pytest.raises(Exception) as excinfo: |
|
58 | with pytest.raises(Exception) as excinfo: | |
59 | remote.import_remote_repository({'path': 'path'}, 'url') |
|
59 | remote.import_remote_repository({'path': 'path'}, 'url') | |
60 |
|
60 | |||
61 | expected_error_args = 'Failed to dump the remote repository from url. Reason:{}'.format(expected_reason) |
|
61 | expected_error_args = 'Failed to dump the remote repository from url. Reason:{}'.format(expected_reason) | |
62 |
|
62 | |||
63 | assert excinfo.value.args[0] == expected_error_args |
|
63 | assert excinfo.value.args[0] == expected_error_args | |
64 |
|
64 | |||
65 |
|
65 | |||
66 | def test_svn_libraries_can_be_imported(): |
|
66 | def test_svn_libraries_can_be_imported(): | |
67 | import svn.client # noqa |
|
67 | import svn.client # noqa | |
68 | assert svn.client is not None |
|
68 | assert svn.client is not None | |
69 |
|
69 | |||
70 |
|
70 | |||
71 | @pytest.mark.parametrize('example_url, parts', [ |
|
71 | @pytest.mark.parametrize('example_url, parts', [ | |
72 | ('http://server.com', ('', '', 'http://server.com')), |
|
72 | ('http://server.com', ('', '', 'http://server.com')), | |
73 | ('http://user@server.com', ('user', '', 'http://user@server.com')), |
|
73 | ('http://user@server.com', ('user', '', 'http://user@server.com')), | |
74 | ('http://user:pass@server.com', ('user', 'pass', 'http://user:pass@server.com')), |
|
74 | ('http://user:pass@server.com', ('user', 'pass', 'http://user:pass@server.com')), | |
75 | ('<script>', ('', '', '<script>')), |
|
75 | ('<script>', ('', '', '<script>')), | |
76 | ('http://', ('', '', 'http://')), |
|
76 | ('http://', ('', '', 'http://')), | |
77 | ]) |
|
77 | ]) | |
78 | def test_username_password_extraction_from_url(example_url, parts): |
|
78 | def test_username_password_extraction_from_url(example_url, parts): | |
79 | from vcsserver.remote import svn_remote |
|
79 | from vcsserver.remote import svn_remote | |
80 |
|
80 | |||
81 | factory = mock.Mock() |
|
81 | factory = mock.Mock() | |
82 | factory.repo = mock.Mock(return_value=mock.Mock()) |
|
82 | factory.repo = mock.Mock(return_value=mock.Mock()) | |
83 |
|
83 | |||
84 | remote = svn_remote.SvnRemote(factory) |
|
84 | remote = svn_remote.SvnRemote(factory) | |
85 | remote.is_path_valid_repository = lambda wire, path: True |
|
85 | remote.is_path_valid_repository = lambda wire, path: True | |
86 |
|
86 | |||
87 | assert remote.get_url_and_credentials(example_url) == parts |
|
87 | assert remote.get_url_and_credentials(example_url) == parts | |
88 |
|
88 | |||
89 |
|
89 | |||
90 | @pytest.mark.parametrize('call_url', [ |
|
90 | @pytest.mark.parametrize('call_url', [ | |
91 | b'https://svn.code.sf.net/p/svnbook/source/trunk/', |
|
91 | b'https://svn.code.sf.net/p/svnbook/source/trunk/', | |
92 | b'https://marcink@svn.code.sf.net/p/svnbook/source/trunk/', |
|
92 | b'https://marcink@svn.code.sf.net/p/svnbook/source/trunk/', | |
93 | b'https://marcink:qweqwe@svn.code.sf.net/p/svnbook/source/trunk/', |
|
93 | b'https://marcink:qweqwe@svn.code.sf.net/p/svnbook/source/trunk/', | |
94 | ]) |
|
94 | ]) | |
95 | def test_check_url(call_url): |
|
95 | def test_check_url(call_url): | |
96 | from vcsserver.remote import svn_remote |
|
96 | from vcsserver.remote import svn_remote | |
97 | factory = mock.Mock() |
|
97 | factory = mock.Mock() | |
98 | factory.repo = mock.Mock(return_value=mock.Mock()) |
|
98 | factory.repo = mock.Mock(return_value=mock.Mock()) | |
99 |
|
99 | |||
100 | remote = svn_remote.SvnRemote(factory) |
|
100 | remote = svn_remote.SvnRemote(factory) | |
101 | remote.is_path_valid_repository = lambda wire, path: True |
|
101 | remote.is_path_valid_repository = lambda wire, path: True | |
102 | assert remote.check_url(call_url, {'dummy': 'config'}) |
|
102 | assert remote.check_url(call_url, {'dummy': 'config'}) | |
103 |
|
103 |
@@ -1,123 +1,123 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | import base64 |
|
17 | import base64 | |
18 | import logging |
|
18 | import logging | |
19 | import time |
|
19 | import time | |
20 |
|
20 | |||
21 | import msgpack |
|
21 | import msgpack | |
22 |
|
22 | |||
23 | import vcsserver |
|
23 | import vcsserver | |
24 | from vcsserver.str_utils import safe_str |
|
24 | from vcsserver.str_utils import safe_str | |
25 |
|
25 | |||
26 | log = logging.getLogger(__name__) |
|
26 | log = logging.getLogger(__name__) | |
27 |
|
27 | |||
28 |
|
28 | |||
29 | def get_access_path(environ): |
|
29 | def get_access_path(environ): | |
30 | path = environ.get('PATH_INFO') |
|
30 | path = environ.get('PATH_INFO') | |
31 | return path |
|
31 | return path | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | def get_user_agent(environ): |
|
34 | def get_user_agent(environ): | |
35 | return environ.get('HTTP_USER_AGENT') |
|
35 | return environ.get('HTTP_USER_AGENT') | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | def get_call_context(request) -> dict: |
|
38 | def get_call_context(request) -> dict: | |
39 | cc = {} |
|
39 | cc = {} | |
40 | registry = request.registry |
|
40 | registry = request.registry | |
41 | if hasattr(registry, 'vcs_call_context'): |
|
41 | if hasattr(registry, 'vcs_call_context'): | |
42 | cc.update({ |
|
42 | cc.update({ | |
43 | 'X-RC-Method': registry.vcs_call_context.get('method'), |
|
43 | 'X-RC-Method': registry.vcs_call_context.get('method'), | |
44 | 'X-RC-Repo-Name': registry.vcs_call_context.get('repo_name') |
|
44 | 'X-RC-Repo-Name': registry.vcs_call_context.get('repo_name') | |
45 | }) |
|
45 | }) | |
46 |
|
46 | |||
47 | return cc |
|
47 | return cc | |
48 |
|
48 | |||
49 |
|
49 | |||
50 | def get_headers_call_context(environ, strict=True): |
|
50 | def get_headers_call_context(environ, strict=True): | |
51 | if 'HTTP_X_RC_VCS_STREAM_CALL_CONTEXT' in environ: |
|
51 | if 'HTTP_X_RC_VCS_STREAM_CALL_CONTEXT' in environ: | |
52 | packed_cc = base64.b64decode(environ['HTTP_X_RC_VCS_STREAM_CALL_CONTEXT']) |
|
52 | packed_cc = base64.b64decode(environ['HTTP_X_RC_VCS_STREAM_CALL_CONTEXT']) | |
53 | return msgpack.unpackb(packed_cc) |
|
53 | return msgpack.unpackb(packed_cc) | |
54 | elif strict: |
|
54 | elif strict: | |
55 | raise ValueError('Expected header HTTP_X_RC_VCS_STREAM_CALL_CONTEXT not found') |
|
55 | raise ValueError('Expected header HTTP_X_RC_VCS_STREAM_CALL_CONTEXT not found') | |
56 |
|
56 | |||
57 |
|
57 | |||
58 |
class RequestWrapperTween |
|
58 | class RequestWrapperTween: | |
59 | def __init__(self, handler, registry): |
|
59 | def __init__(self, handler, registry): | |
60 | self.handler = handler |
|
60 | self.handler = handler | |
61 | self.registry = registry |
|
61 | self.registry = registry | |
62 |
|
62 | |||
63 | # one-time configuration code goes here |
|
63 | # one-time configuration code goes here | |
64 |
|
64 | |||
65 | def __call__(self, request): |
|
65 | def __call__(self, request): | |
66 | start = time.time() |
|
66 | start = time.time() | |
67 | log.debug('Starting request time measurement') |
|
67 | log.debug('Starting request time measurement') | |
68 | response = None |
|
68 | response = None | |
69 |
|
69 | |||
70 | try: |
|
70 | try: | |
71 | response = self.handler(request) |
|
71 | response = self.handler(request) | |
72 | finally: |
|
72 | finally: | |
73 | ua = get_user_agent(request.environ) |
|
73 | ua = get_user_agent(request.environ) | |
74 | call_context = get_call_context(request) |
|
74 | call_context = get_call_context(request) | |
75 | vcs_method = call_context.get('X-RC-Method', '_NO_VCS_METHOD') |
|
75 | vcs_method = call_context.get('X-RC-Method', '_NO_VCS_METHOD') | |
76 | repo_name = call_context.get('X-RC-Repo-Name', '') |
|
76 | repo_name = call_context.get('X-RC-Repo-Name', '') | |
77 |
|
77 | |||
78 | count = request.request_count() |
|
78 | count = request.request_count() | |
79 | _ver_ = vcsserver.__version__ |
|
79 | _ver_ = vcsserver.__version__ | |
80 | _path = safe_str(get_access_path(request.environ)) |
|
80 | _path = safe_str(get_access_path(request.environ)) | |
81 |
|
81 | |||
82 | ip = '127.0.0.1' |
|
82 | ip = '127.0.0.1' | |
83 | match_route = request.matched_route.name if request.matched_route else "NOT_FOUND" |
|
83 | match_route = request.matched_route.name if request.matched_route else "NOT_FOUND" | |
84 | resp_code = getattr(response, 'status_code', 'UNDEFINED') |
|
84 | resp_code = getattr(response, 'status_code', 'UNDEFINED') | |
85 |
|
85 | |||
86 | _view_path = f"{repo_name}@{_path}/{vcs_method}" |
|
86 | _view_path = f"{repo_name}@{_path}/{vcs_method}" | |
87 |
|
87 | |||
88 | total = time.time() - start |
|
88 | total = time.time() - start | |
89 |
|
89 | |||
90 | log.info( |
|
90 | log.info( | |
91 | 'Req[%4s] IP: %s %s Request to %s time: %.4fs [%s], VCSServer %s', |
|
91 | 'Req[%4s] IP: %s %s Request to %s time: %.4fs [%s], VCSServer %s', | |
92 | count, ip, request.environ.get('REQUEST_METHOD'), |
|
92 | count, ip, request.environ.get('REQUEST_METHOD'), | |
93 | _view_path, total, ua, _ver_, |
|
93 | _view_path, total, ua, _ver_, | |
94 | extra={"time": total, "ver": _ver_, "code": resp_code, |
|
94 | extra={"time": total, "ver": _ver_, "code": resp_code, | |
95 | "path": _path, "view_name": match_route, "user_agent": ua, |
|
95 | "path": _path, "view_name": match_route, "user_agent": ua, | |
96 | "vcs_method": vcs_method, "repo_name": repo_name} |
|
96 | "vcs_method": vcs_method, "repo_name": repo_name} | |
97 | ) |
|
97 | ) | |
98 |
|
98 | |||
99 | statsd = request.registry.statsd |
|
99 | statsd = request.registry.statsd | |
100 | if statsd: |
|
100 | if statsd: | |
101 | match_route = request.matched_route.name if request.matched_route else _path |
|
101 | match_route = request.matched_route.name if request.matched_route else _path | |
102 | elapsed_time_ms = round(1000.0 * total) # use ms only |
|
102 | elapsed_time_ms = round(1000.0 * total) # use ms only | |
103 | statsd.timing( |
|
103 | statsd.timing( | |
104 | "vcsserver_req_timing.histogram", elapsed_time_ms, |
|
104 | "vcsserver_req_timing.histogram", elapsed_time_ms, | |
105 | tags=[ |
|
105 | tags=[ | |
106 | f"view_name:{match_route}", |
|
106 | f"view_name:{match_route}", | |
107 | f"code:{resp_code}" |
|
107 | f"code:{resp_code}" | |
108 | ], |
|
108 | ], | |
109 | use_decimals=False |
|
109 | use_decimals=False | |
110 | ) |
|
110 | ) | |
111 | statsd.incr( |
|
111 | statsd.incr( | |
112 | "vcsserver_req_total", tags=[ |
|
112 | "vcsserver_req_total", tags=[ | |
113 | f"view_name:{match_route}", |
|
113 | f"view_name:{match_route}", | |
114 | f"code:{resp_code}" |
|
114 | f"code:{resp_code}" | |
115 | ]) |
|
115 | ]) | |
116 |
|
116 | |||
117 | return response |
|
117 | return response | |
118 |
|
118 | |||
119 |
|
119 | |||
120 | def includeme(config): |
|
120 | def includeme(config): | |
121 | config.add_tween( |
|
121 | config.add_tween( | |
122 | 'vcsserver.tweens.request_wrapper.RequestWrapperTween', |
|
122 | 'vcsserver.tweens.request_wrapper.RequestWrapperTween', | |
123 | ) |
|
123 | ) |
@@ -1,46 +1,46 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | from vcsserver.lib import rc_cache |
|
18 | from vcsserver.lib import rc_cache | |
19 |
|
19 | |||
20 |
|
20 | |||
21 |
class RemoteBase |
|
21 | class RemoteBase: | |
22 | EMPTY_COMMIT = '0' * 40 |
|
22 | EMPTY_COMMIT = '0' * 40 | |
23 |
|
23 | |||
24 | def _region(self, wire): |
|
24 | def _region(self, wire): | |
25 | cache_repo_id = wire.get('cache_repo_id', '') |
|
25 | cache_repo_id = wire.get('cache_repo_id', '') | |
26 | cache_namespace_uid = f'cache_repo.{rc_cache.CACHE_OBJ_CACHE_VER}.{cache_repo_id}' |
|
26 | cache_namespace_uid = f'cache_repo.{rc_cache.CACHE_OBJ_CACHE_VER}.{cache_repo_id}' | |
27 | return rc_cache.get_or_create_region('repo_object', cache_namespace_uid) |
|
27 | return rc_cache.get_or_create_region('repo_object', cache_namespace_uid) | |
28 |
|
28 | |||
29 | def _cache_on(self, wire): |
|
29 | def _cache_on(self, wire): | |
30 | context = wire.get('context', '') |
|
30 | context = wire.get('context', '') | |
31 | context_uid = f'{context}' |
|
31 | context_uid = f'{context}' | |
32 | repo_id = wire.get('repo_id', '') |
|
32 | repo_id = wire.get('repo_id', '') | |
33 | cache = wire.get('cache', True) |
|
33 | cache = wire.get('cache', True) | |
34 | cache_on = context and cache |
|
34 | cache_on = context and cache | |
35 | return cache_on, context_uid, repo_id |
|
35 | return cache_on, context_uid, repo_id | |
36 |
|
36 | |||
37 | def vcsserver_invalidate_cache(self, wire, delete): |
|
37 | def vcsserver_invalidate_cache(self, wire, delete): | |
38 | cache_repo_id = wire.get('cache_repo_id', '') |
|
38 | cache_repo_id = wire.get('cache_repo_id', '') | |
39 | cache_namespace_uid = f'cache_repo.{rc_cache.CACHE_OBJ_CACHE_VER}.{cache_repo_id}' |
|
39 | cache_namespace_uid = f'cache_repo.{rc_cache.CACHE_OBJ_CACHE_VER}.{cache_repo_id}' | |
40 |
|
40 | |||
41 | if delete: |
|
41 | if delete: | |
42 | rc_cache.clear_cache_namespace( |
|
42 | rc_cache.clear_cache_namespace( | |
43 | 'repo_object', cache_namespace_uid, method=rc_cache.CLEAR_DELETE) |
|
43 | 'repo_object', cache_namespace_uid, method=rc_cache.CLEAR_DELETE) | |
44 |
|
44 | |||
45 | repo_id = wire.get('repo_id', '') |
|
45 | repo_id = wire.get('repo_id', '') | |
46 | return {'invalidated': {'repo_id': repo_id, 'delete': delete}} |
|
46 | return {'invalidated': {'repo_id': repo_id, 'delete': delete}} |
@@ -1,116 +1,116 b'' | |||||
1 | # RhodeCode VCSServer provides access to different vcs backends via network. |
|
1 | # RhodeCode VCSServer provides access to different vcs backends via network. | |
2 | # Copyright (C) 2014-2023 RhodeCode GmbH |
|
2 | # Copyright (C) 2014-2023 RhodeCode GmbH | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or modify |
|
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by |
|
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 3 of the License, or |
|
6 | # the Free Software Foundation; either version 3 of the License, or | |
7 | # (at your option) any later version. |
|
7 | # (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software Foundation, |
|
15 | # along with this program; if not, write to the Free Software Foundation, | |
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 |
|
17 | |||
18 | """Extract the responses of a WSGI app.""" |
|
18 | """Extract the responses of a WSGI app.""" | |
19 |
|
19 | |||
20 | __all__ = ('WSGIAppCaller',) |
|
20 | __all__ = ('WSGIAppCaller',) | |
21 |
|
21 | |||
22 | import io |
|
22 | import io | |
23 | import logging |
|
23 | import logging | |
24 | import os |
|
24 | import os | |
25 |
|
25 | |||
26 | from vcsserver.str_utils import ascii_bytes |
|
26 | from vcsserver.str_utils import ascii_bytes | |
27 |
|
27 | |||
28 | log = logging.getLogger(__name__) |
|
28 | log = logging.getLogger(__name__) | |
29 |
|
29 | |||
30 | DEV_NULL = open(os.devnull) |
|
30 | DEV_NULL = open(os.devnull) | |
31 |
|
31 | |||
32 |
|
32 | |||
33 | def _complete_environ(environ, input_data: bytes): |
|
33 | def _complete_environ(environ, input_data: bytes): | |
34 | """Update the missing wsgi.* variables of a WSGI environment. |
|
34 | """Update the missing wsgi.* variables of a WSGI environment. | |
35 |
|
35 | |||
36 | :param environ: WSGI environment to update |
|
36 | :param environ: WSGI environment to update | |
37 | :type environ: dict |
|
37 | :type environ: dict | |
38 | :param input_data: data to be read by the app |
|
38 | :param input_data: data to be read by the app | |
39 | :type input_data: bytes |
|
39 | :type input_data: bytes | |
40 | """ |
|
40 | """ | |
41 | environ.update({ |
|
41 | environ.update({ | |
42 | 'wsgi.version': (1, 0), |
|
42 | 'wsgi.version': (1, 0), | |
43 | 'wsgi.url_scheme': 'http', |
|
43 | 'wsgi.url_scheme': 'http', | |
44 | 'wsgi.multithread': True, |
|
44 | 'wsgi.multithread': True, | |
45 | 'wsgi.multiprocess': True, |
|
45 | 'wsgi.multiprocess': True, | |
46 | 'wsgi.run_once': False, |
|
46 | 'wsgi.run_once': False, | |
47 | 'wsgi.input': io.BytesIO(input_data), |
|
47 | 'wsgi.input': io.BytesIO(input_data), | |
48 | 'wsgi.errors': DEV_NULL, |
|
48 | 'wsgi.errors': DEV_NULL, | |
49 | }) |
|
49 | }) | |
50 |
|
50 | |||
51 |
|
51 | |||
52 | # pylint: disable=too-few-public-methods |
|
52 | # pylint: disable=too-few-public-methods | |
53 |
class _StartResponse |
|
53 | class _StartResponse: | |
54 | """Save the arguments of a start_response call.""" |
|
54 | """Save the arguments of a start_response call.""" | |
55 |
|
55 | |||
56 | __slots__ = ['status', 'headers', 'content'] |
|
56 | __slots__ = ['status', 'headers', 'content'] | |
57 |
|
57 | |||
58 | def __init__(self): |
|
58 | def __init__(self): | |
59 | self.status = None |
|
59 | self.status = None | |
60 | self.headers = None |
|
60 | self.headers = None | |
61 | self.content = [] |
|
61 | self.content = [] | |
62 |
|
62 | |||
63 | def __call__(self, status, headers, exc_info=None): |
|
63 | def __call__(self, status, headers, exc_info=None): | |
64 | # TODO(skreft): do something meaningful with the exc_info |
|
64 | # TODO(skreft): do something meaningful with the exc_info | |
65 | exc_info = None # avoid dangling circular reference |
|
65 | exc_info = None # avoid dangling circular reference | |
66 | self.status = status |
|
66 | self.status = status | |
67 | self.headers = headers |
|
67 | self.headers = headers | |
68 |
|
68 | |||
69 | return self.write |
|
69 | return self.write | |
70 |
|
70 | |||
71 | def write(self, content): |
|
71 | def write(self, content): | |
72 | """Write method returning when calling this object. |
|
72 | """Write method returning when calling this object. | |
73 |
|
73 | |||
74 | All the data written is then available in content. |
|
74 | All the data written is then available in content. | |
75 | """ |
|
75 | """ | |
76 | self.content.append(content) |
|
76 | self.content.append(content) | |
77 |
|
77 | |||
78 |
|
78 | |||
79 |
class WSGIAppCaller |
|
79 | class WSGIAppCaller: | |
80 | """Calls a WSGI app.""" |
|
80 | """Calls a WSGI app.""" | |
81 |
|
81 | |||
82 | def __init__(self, app): |
|
82 | def __init__(self, app): | |
83 | """ |
|
83 | """ | |
84 | :param app: WSGI app to call |
|
84 | :param app: WSGI app to call | |
85 | """ |
|
85 | """ | |
86 | self.app = app |
|
86 | self.app = app | |
87 |
|
87 | |||
88 | def handle(self, environ, input_data): |
|
88 | def handle(self, environ, input_data): | |
89 | """Process a request with the WSGI app. |
|
89 | """Process a request with the WSGI app. | |
90 |
|
90 | |||
91 | The returned data of the app is fully consumed into a list. |
|
91 | The returned data of the app is fully consumed into a list. | |
92 |
|
92 | |||
93 | :param environ: WSGI environment to update |
|
93 | :param environ: WSGI environment to update | |
94 | :type environ: dict |
|
94 | :type environ: dict | |
95 | :param input_data: data to be read by the app |
|
95 | :param input_data: data to be read by the app | |
96 | :type input_data: str/bytes |
|
96 | :type input_data: str/bytes | |
97 |
|
97 | |||
98 | :returns: a tuple with the contents, status and headers |
|
98 | :returns: a tuple with the contents, status and headers | |
99 | :rtype: (list<str>, str, list<(str, str)>) |
|
99 | :rtype: (list<str>, str, list<(str, str)>) | |
100 | """ |
|
100 | """ | |
101 | _complete_environ(environ, ascii_bytes(input_data, allow_bytes=True)) |
|
101 | _complete_environ(environ, ascii_bytes(input_data, allow_bytes=True)) | |
102 | start_response = _StartResponse() |
|
102 | start_response = _StartResponse() | |
103 | log.debug("Calling wrapped WSGI application") |
|
103 | log.debug("Calling wrapped WSGI application") | |
104 | responses = self.app(environ, start_response) |
|
104 | responses = self.app(environ, start_response) | |
105 | responses_list = list(responses) |
|
105 | responses_list = list(responses) | |
106 | existing_responses = start_response.content |
|
106 | existing_responses = start_response.content | |
107 | if existing_responses: |
|
107 | if existing_responses: | |
108 | log.debug("Adding returned response to response written via write()") |
|
108 | log.debug("Adding returned response to response written via write()") | |
109 | existing_responses.extend(responses_list) |
|
109 | existing_responses.extend(responses_list) | |
110 | responses_list = existing_responses |
|
110 | responses_list = existing_responses | |
111 | if hasattr(responses, 'close'): |
|
111 | if hasattr(responses, 'close'): | |
112 | log.debug("Closing iterator from WSGI application") |
|
112 | log.debug("Closing iterator from WSGI application") | |
113 | responses.close() |
|
113 | responses.close() | |
114 |
|
114 | |||
115 | log.debug("Handling of WSGI request done, returning response") |
|
115 | log.debug("Handling of WSGI request done, returning response") | |
116 | return responses_list, start_response.status, start_response.headers |
|
116 | return responses_list, start_response.status, start_response.headers |
General Comments 0
You need to be logged in to leave comments.
Login now