@@ -24,6 +24,11 @@ class _GitOperation(typing.NamedTuple):
24
24
class GitService :
25
25
_GIT_PROCESS_TIMEOUT = 20
26
26
_GIT_VALID_EXIT_CODE = 0
27
+ _STATELESS_RPC = '--stateless-rpc'
28
+ _ADVERTISE_REFS = '--advertise-refs'
29
+ _UPLOAD_COMMAND = 'git-upload-pack'
30
+ _RECEIVE_COMMAND = 'git-receive-pack'
31
+ _REFS_COMMAND = '/info/refs'
27
32
28
33
def __init__ (
29
34
self ,
@@ -125,55 +130,14 @@ def build_response(
125
130
return res
126
131
127
132
def _extract_git_operation (self ) -> _GitOperation :
128
- stateless_rpc = '--stateless-rpc'
129
- advertise_refs = '--advertise-refs'
130
- upload_pack_command = 'git-upload-pack'
131
- receive_pack_command = 'git-receive-pack'
132
- allowed_commands = [upload_pack_command , receive_pack_command ]
133
-
134
- supported = True
135
- format_response = False
136
- contain_new_commits = False
137
-
138
- if self ._request .path .endswith (upload_pack_command ):
139
- content_type = 'application/x-git-upload-pack-result'
140
- service_command = [
141
- upload_pack_command ,
142
- stateless_rpc ,
143
- self .project_name ,
144
- ]
145
-
146
- elif self ._request .path .endswith (receive_pack_command ):
147
- content_type = 'application/x-git-receive-pack-result'
148
- service_command = [
149
- receive_pack_command ,
150
- stateless_rpc ,
151
- self .project_name ,
152
- ]
153
- contain_new_commits = True
154
-
155
- elif self ._request .path .endswith ('/info/refs' ):
156
- service_name = self ._request .args .get ('service' )
157
- service_command = [
158
- service_name ,
159
- stateless_rpc ,
160
- advertise_refs ,
161
- self .project_name ,
162
- ]
163
- content_type = f'application/x-{ service_name } -advertisement'
133
+ if self ._request .path .endswith (self ._UPLOAD_COMMAND ):
134
+ return self ._build_upload_operation ()
164
135
165
- supported = service_name in allowed_commands
136
+ elif self ._request .path .endswith (self ._RECEIVE_COMMAND ):
137
+ return self ._build_receive_operation ()
166
138
167
- def format_response_callback (response_bytes : bytes ) -> bytes :
168
- packet = f'# service={ service_name } \n '
169
- length = len (packet ) + 4
170
- prefix = '{:04x}' .format (length & 0xFFFF )
171
-
172
- data = (prefix + packet + '0000' ).encode ()
173
- data += response_bytes
174
- return data
175
-
176
- format_response = format_response_callback
139
+ elif self ._request .path .endswith (self ._REFS_COMMAND ):
140
+ return self ._build_refs_operation ()
177
141
178
142
else :
179
143
log .error (
@@ -182,12 +146,58 @@ def format_response_callback(response_bytes: bytes) -> bytes:
182
146
)
183
147
raise NotImplementedError
184
148
149
+ def _build_refs_operation (self ) -> _GitOperation :
150
+ allowed_commands = [self ._UPLOAD_COMMAND , self ._RECEIVE_COMMAND ]
151
+ service_name = self ._request .args .get ('service' )
152
+ content_type = f'application/x-{ service_name } -advertisement'
153
+ supported = service_name in allowed_commands
154
+
155
+ def format_response_callback (response_bytes : bytes ) -> bytes :
156
+ packet = f'# service={ service_name } \n '
157
+ length = len (packet ) + 4
158
+ prefix = '{:04x}' .format (length & 0xFFFF )
159
+
160
+ data = (prefix + packet + '0000' ).encode ()
161
+ data += response_bytes
162
+ return data
163
+
185
164
return _GitOperation (
186
165
response_content_type = content_type ,
187
- service_command = service_command ,
166
+ service_command = [
167
+ service_name ,
168
+ self ._STATELESS_RPC ,
169
+ self ._ADVERTISE_REFS ,
170
+ self .project_name ,
171
+ ],
188
172
supported = supported ,
189
- format_response = format_response ,
190
- contain_new_commits = contain_new_commits ,
173
+ format_response = format_response_callback ,
174
+ contain_new_commits = False ,
175
+ )
176
+
177
+ def _build_receive_operation (self ) -> _GitOperation :
178
+ return _GitOperation (
179
+ response_content_type = 'application/x-git-receive-pack-result' ,
180
+ service_command = [
181
+ self ._RECEIVE_COMMAND ,
182
+ self ._STATELESS_RPC ,
183
+ self .project_name ,
184
+ ],
185
+ supported = True ,
186
+ format_response = None ,
187
+ contain_new_commits = True ,
188
+ )
189
+
190
+ def _build_upload_operation (self ) -> _GitOperation :
191
+ return _GitOperation (
192
+ response_content_type = 'application/x-git-upload-pack-result' ,
193
+ service_command = [
194
+ self ._UPLOAD_COMMAND ,
195
+ self ._STATELESS_RPC ,
196
+ self .project_name ,
197
+ ],
198
+ supported = True ,
199
+ format_response = None ,
200
+ contain_new_commits = False ,
191
201
)
192
202
193
203
def _load_files_from_repository (self ) -> typing .List [upload .File ]:
0 commit comments