diff options
Diffstat (limited to 'git_config.py')
| -rw-r--r-- | git_config.py | 41 |
1 files changed, 31 insertions, 10 deletions
diff --git a/git_config.py b/git_config.py index 45a2d257..4a42c047 100644 --- a/git_config.py +++ b/git_config.py | |||
| @@ -19,6 +19,8 @@ import re | |||
| 19 | import subprocess | 19 | import subprocess |
| 20 | import sys | 20 | import sys |
| 21 | import time | 21 | import time |
| 22 | import urllib2 | ||
| 23 | |||
| 22 | from signal import SIGTERM | 24 | from signal import SIGTERM |
| 23 | from urllib2 import urlopen, HTTPError | 25 | from urllib2 import urlopen, HTTPError |
| 24 | from error import GitError, UploadError | 26 | from error import GitError, UploadError |
| @@ -71,6 +73,14 @@ class GitConfig(object): | |||
| 71 | else: | 73 | else: |
| 72 | self._pickle = pickleFile | 74 | self._pickle = pickleFile |
| 73 | 75 | ||
| 76 | def ClearCache(self): | ||
| 77 | if os.path.exists(self._pickle): | ||
| 78 | os.remove(self._pickle) | ||
| 79 | self._cache_dict = None | ||
| 80 | self._section_dict = None | ||
| 81 | self._remotes = {} | ||
| 82 | self._branches = {} | ||
| 83 | |||
| 74 | def Has(self, name, include_defaults = True): | 84 | def Has(self, name, include_defaults = True): |
| 75 | """Return true if this configuration file has the key. | 85 | """Return true if this configuration file has the key. |
| 76 | """ | 86 | """ |
| @@ -254,9 +264,11 @@ class GitConfig(object): | |||
| 254 | finally: | 264 | finally: |
| 255 | fd.close() | 265 | fd.close() |
| 256 | except IOError: | 266 | except IOError: |
| 257 | os.remove(self._pickle) | 267 | if os.path.exists(self._pickle): |
| 268 | os.remove(self._pickle) | ||
| 258 | except cPickle.PickleError: | 269 | except cPickle.PickleError: |
| 259 | os.remove(self._pickle) | 270 | if os.path.exists(self._pickle): |
| 271 | os.remove(self._pickle) | ||
| 260 | 272 | ||
| 261 | def _ReadGit(self): | 273 | def _ReadGit(self): |
| 262 | """ | 274 | """ |
| @@ -356,10 +368,14 @@ class RefSpec(object): | |||
| 356 | _ssh_cache = {} | 368 | _ssh_cache = {} |
| 357 | _ssh_master = True | 369 | _ssh_master = True |
| 358 | 370 | ||
| 359 | def _open_ssh(host, port): | 371 | def _open_ssh(host, port=None): |
| 360 | global _ssh_master | 372 | global _ssh_master |
| 361 | 373 | ||
| 362 | key = '%s:%s' % (host, port) | 374 | if port is not None: |
| 375 | key = '%s:%s' % (host, port) | ||
| 376 | else: | ||
| 377 | key = host | ||
| 378 | |||
| 363 | if key in _ssh_cache: | 379 | if key in _ssh_cache: |
| 364 | return True | 380 | return True |
| 365 | 381 | ||
| @@ -372,10 +388,13 @@ def _open_ssh(host, port): | |||
| 372 | 388 | ||
| 373 | command = ['ssh', | 389 | command = ['ssh', |
| 374 | '-o','ControlPath %s' % _ssh_sock(), | 390 | '-o','ControlPath %s' % _ssh_sock(), |
| 375 | '-p',str(port), | ||
| 376 | '-M', | 391 | '-M', |
| 377 | '-N', | 392 | '-N', |
| 378 | host] | 393 | host] |
| 394 | |||
| 395 | if port is not None: | ||
| 396 | command[3:3] = ['-p',str(port)] | ||
| 397 | |||
| 379 | try: | 398 | try: |
| 380 | Trace(': %s', ' '.join(command)) | 399 | Trace(': %s', ' '.join(command)) |
| 381 | p = subprocess.Popen(command) | 400 | p = subprocess.Popen(command) |
| @@ -417,7 +436,7 @@ def _preconnect(url): | |||
| 417 | if ':' in host: | 436 | if ':' in host: |
| 418 | host, port = host.split(':') | 437 | host, port = host.split(':') |
| 419 | else: | 438 | else: |
| 420 | port = 22 | 439 | port = None |
| 421 | if scheme in ('ssh', 'git+ssh', 'ssh+git'): | 440 | if scheme in ('ssh', 'git+ssh', 'ssh+git'): |
| 422 | return _open_ssh(host, port) | 441 | return _open_ssh(host, port) |
| 423 | return False | 442 | return False |
| @@ -425,7 +444,7 @@ def _preconnect(url): | |||
| 425 | m = URI_SCP.match(url) | 444 | m = URI_SCP.match(url) |
| 426 | if m: | 445 | if m: |
| 427 | host = m.group(1) | 446 | host = m.group(1) |
| 428 | return _open_ssh(host, 22) | 447 | return _open_ssh(host) |
| 429 | 448 | ||
| 430 | return False | 449 | return False |
| 431 | 450 | ||
| @@ -492,23 +511,25 @@ class Remote(object): | |||
| 492 | try: | 511 | try: |
| 493 | info = urlopen(u).read() | 512 | info = urlopen(u).read() |
| 494 | if info == 'NOT_AVAILABLE': | 513 | if info == 'NOT_AVAILABLE': |
| 495 | raise UploadError('Upload over ssh unavailable') | 514 | raise UploadError('%s: SSH disabled' % self.review) |
| 496 | if '<' in info: | 515 | if '<' in info: |
| 497 | # Assume the server gave us some sort of HTML | 516 | # Assume the server gave us some sort of HTML |
| 498 | # response back, like maybe a login page. | 517 | # response back, like maybe a login page. |
| 499 | # | 518 | # |
| 500 | raise UploadError('Cannot read %s:\n%s' % (u, info)) | 519 | raise UploadError('%s: Cannot parse response' % u) |
| 501 | 520 | ||
| 502 | self._review_protocol = 'ssh' | 521 | self._review_protocol = 'ssh' |
| 503 | self._review_host = info.split(" ")[0] | 522 | self._review_host = info.split(" ")[0] |
| 504 | self._review_port = info.split(" ")[1] | 523 | self._review_port = info.split(" ")[1] |
| 524 | except urllib2.URLError, e: | ||
| 525 | raise UploadError('%s: %s' % (self.review, e.reason[1])) | ||
| 505 | except HTTPError, e: | 526 | except HTTPError, e: |
| 506 | if e.code == 404: | 527 | if e.code == 404: |
| 507 | self._review_protocol = 'http-post' | 528 | self._review_protocol = 'http-post' |
| 508 | self._review_host = None | 529 | self._review_host = None |
| 509 | self._review_port = None | 530 | self._review_port = None |
| 510 | else: | 531 | else: |
| 511 | raise UploadError('Cannot guess Gerrit version') | 532 | raise UploadError('Upload over ssh unavailable') |
| 512 | 533 | ||
| 513 | REVIEW_CACHE[u] = ( | 534 | REVIEW_CACHE[u] = ( |
| 514 | self._review_protocol, | 535 | self._review_protocol, |
