diff options
Diffstat (limited to 'git_config.py')
-rw-r--r-- | git_config.py | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/git_config.py b/git_config.py index e2236785..3ba9dbd1 100644 --- a/git_config.py +++ b/git_config.py | |||
@@ -20,6 +20,7 @@ import errno | |||
20 | import json | 20 | import json |
21 | import os | 21 | import os |
22 | import re | 22 | import re |
23 | import ssl | ||
23 | import subprocess | 24 | import subprocess |
24 | import sys | 25 | import sys |
25 | try: | 26 | try: |
@@ -41,6 +42,7 @@ else: | |||
41 | 42 | ||
42 | from signal import SIGTERM | 43 | from signal import SIGTERM |
43 | from error import GitError, UploadError | 44 | from error import GitError, UploadError |
45 | import platform_utils | ||
44 | from trace import Trace | 46 | from trace import Trace |
45 | if is_python3(): | 47 | if is_python3(): |
46 | from http.client import HTTPException | 48 | from http.client import HTTPException |
@@ -50,16 +52,24 @@ else: | |||
50 | from git_command import GitCommand | 52 | from git_command import GitCommand |
51 | from git_command import ssh_sock | 53 | from git_command import ssh_sock |
52 | from git_command import terminate_ssh_clients | 54 | from git_command import terminate_ssh_clients |
55 | from git_refs import R_CHANGES, R_HEADS, R_TAGS | ||
53 | 56 | ||
54 | R_HEADS = 'refs/heads/' | ||
55 | R_TAGS = 'refs/tags/' | ||
56 | ID_RE = re.compile(r'^[0-9a-f]{40}$') | 57 | ID_RE = re.compile(r'^[0-9a-f]{40}$') |
57 | 58 | ||
58 | REVIEW_CACHE = dict() | 59 | REVIEW_CACHE = dict() |
59 | 60 | ||
61 | def IsChange(rev): | ||
62 | return rev.startswith(R_CHANGES) | ||
63 | |||
60 | def IsId(rev): | 64 | def IsId(rev): |
61 | return ID_RE.match(rev) | 65 | return ID_RE.match(rev) |
62 | 66 | ||
67 | def IsTag(rev): | ||
68 | return rev.startswith(R_TAGS) | ||
69 | |||
70 | def IsImmutable(rev): | ||
71 | return IsChange(rev) or IsId(rev) or IsTag(rev) | ||
72 | |||
63 | def _key(name): | 73 | def _key(name): |
64 | parts = name.split('.') | 74 | parts = name.split('.') |
65 | if len(parts) < 2: | 75 | if len(parts) < 2: |
@@ -259,7 +269,7 @@ class GitConfig(object): | |||
259 | try: | 269 | try: |
260 | if os.path.getmtime(self._json) \ | 270 | if os.path.getmtime(self._json) \ |
261 | <= os.path.getmtime(self.file): | 271 | <= os.path.getmtime(self.file): |
262 | os.remove(self._json) | 272 | platform_utils.remove(self._json) |
263 | return None | 273 | return None |
264 | except OSError: | 274 | except OSError: |
265 | return None | 275 | return None |
@@ -271,7 +281,7 @@ class GitConfig(object): | |||
271 | finally: | 281 | finally: |
272 | fd.close() | 282 | fd.close() |
273 | except (IOError, ValueError): | 283 | except (IOError, ValueError): |
274 | os.remove(self._json) | 284 | platform_utils.remove(self._json) |
275 | return None | 285 | return None |
276 | 286 | ||
277 | def _SaveJson(self, cache): | 287 | def _SaveJson(self, cache): |
@@ -283,7 +293,7 @@ class GitConfig(object): | |||
283 | fd.close() | 293 | fd.close() |
284 | except (IOError, TypeError): | 294 | except (IOError, TypeError): |
285 | if os.path.exists(self._json): | 295 | if os.path.exists(self._json): |
286 | os.remove(self._json) | 296 | platform_utils.remove(self._json) |
287 | 297 | ||
288 | def _ReadGit(self): | 298 | def _ReadGit(self): |
289 | """ | 299 | """ |
@@ -604,7 +614,7 @@ class Remote(object): | |||
604 | connectionUrl = self._InsteadOf() | 614 | connectionUrl = self._InsteadOf() |
605 | return _preconnect(connectionUrl) | 615 | return _preconnect(connectionUrl) |
606 | 616 | ||
607 | def ReviewUrl(self, userEmail): | 617 | def ReviewUrl(self, userEmail, validate_certs): |
608 | if self._review_url is None: | 618 | if self._review_url is None: |
609 | if self.review is None: | 619 | if self.review is None: |
610 | return None | 620 | return None |
@@ -612,7 +622,7 @@ class Remote(object): | |||
612 | u = self.review | 622 | u = self.review |
613 | if u.startswith('persistent-'): | 623 | if u.startswith('persistent-'): |
614 | u = u[len('persistent-'):] | 624 | u = u[len('persistent-'):] |
615 | if u.split(':')[0] not in ('http', 'https', 'sso'): | 625 | if u.split(':')[0] not in ('http', 'https', 'sso', 'ssh'): |
616 | u = 'http://%s' % u | 626 | u = 'http://%s' % u |
617 | if u.endswith('/Gerrit'): | 627 | if u.endswith('/Gerrit'): |
618 | u = u[:len(u) - len('/Gerrit')] | 628 | u = u[:len(u) - len('/Gerrit')] |
@@ -628,13 +638,20 @@ class Remote(object): | |||
628 | host, port = os.environ['REPO_HOST_PORT_INFO'].split() | 638 | host, port = os.environ['REPO_HOST_PORT_INFO'].split() |
629 | self._review_url = self._SshReviewUrl(userEmail, host, port) | 639 | self._review_url = self._SshReviewUrl(userEmail, host, port) |
630 | REVIEW_CACHE[u] = self._review_url | 640 | REVIEW_CACHE[u] = self._review_url |
631 | elif u.startswith('sso:'): | 641 | elif u.startswith('sso:') or u.startswith('ssh:'): |
632 | self._review_url = u # Assume it's right | 642 | self._review_url = u # Assume it's right |
633 | REVIEW_CACHE[u] = self._review_url | 643 | REVIEW_CACHE[u] = self._review_url |
644 | elif 'REPO_IGNORE_SSH_INFO' in os.environ: | ||
645 | self._review_url = http_url | ||
646 | REVIEW_CACHE[u] = self._review_url | ||
634 | else: | 647 | else: |
635 | try: | 648 | try: |
636 | info_url = u + 'ssh_info' | 649 | info_url = u + 'ssh_info' |
637 | info = urllib.request.urlopen(info_url).read() | 650 | if not validate_certs: |
651 | context = ssl._create_unverified_context() | ||
652 | info = urllib.request.urlopen(info_url, context=context).read() | ||
653 | else: | ||
654 | info = urllib.request.urlopen(info_url).read() | ||
638 | if info == 'NOT_AVAILABLE' or '<' in info: | 655 | if info == 'NOT_AVAILABLE' or '<' in info: |
639 | # If `info` contains '<', we assume the server gave us some sort | 656 | # If `info` contains '<', we assume the server gave us some sort |
640 | # of HTML response back, like maybe a login page. | 657 | # of HTML response back, like maybe a login page. |