summaryrefslogtreecommitdiffstats
path: root/git_config.py
diff options
context:
space:
mode:
Diffstat (limited to 'git_config.py')
-rw-r--r--git_config.py72
1 files changed, 33 insertions, 39 deletions
diff --git a/git_config.py b/git_config.py
index 32879ec7..8ded7c25 100644
--- a/git_config.py
+++ b/git_config.py
@@ -15,8 +15,8 @@
15 15
16from __future__ import print_function 16from __future__ import print_function
17 17
18import json
18import os 19import os
19import pickle
20import re 20import re
21import subprocess 21import subprocess
22import sys 22import sys
@@ -80,7 +80,7 @@ class GitConfig(object):
80 return cls(configfile = os.path.join(gitdir, 'config'), 80 return cls(configfile = os.path.join(gitdir, 'config'),
81 defaults = defaults) 81 defaults = defaults)
82 82
83 def __init__(self, configfile, defaults=None, pickleFile=None): 83 def __init__(self, configfile, defaults=None, jsonFile=None):
84 self.file = configfile 84 self.file = configfile
85 self.defaults = defaults 85 self.defaults = defaults
86 self._cache_dict = None 86 self._cache_dict = None
@@ -88,12 +88,11 @@ class GitConfig(object):
88 self._remotes = {} 88 self._remotes = {}
89 self._branches = {} 89 self._branches = {}
90 90
91 if pickleFile is None: 91 self._json = jsonFile
92 self._pickle = os.path.join( 92 if self._json is None:
93 self._json = os.path.join(
93 os.path.dirname(self.file), 94 os.path.dirname(self.file),
94 '.repopickle_' + os.path.basename(self.file)) 95 '.repo_' + os.path.basename(self.file) + '.json')
95 else:
96 self._pickle = pickleFile
97 96
98 def Has(self, name, include_defaults = True): 97 def Has(self, name, include_defaults = True):
99 """Return true if this configuration file has the key. 98 """Return true if this configuration file has the key.
@@ -217,9 +216,9 @@ class GitConfig(object):
217 """Resolve any url.*.insteadof references. 216 """Resolve any url.*.insteadof references.
218 """ 217 """
219 for new_url in self.GetSubSections('url'): 218 for new_url in self.GetSubSections('url'):
220 old_url = self.GetString('url.%s.insteadof' % new_url) 219 for old_url in self.GetString('url.%s.insteadof' % new_url, True):
221 if old_url is not None and url.startswith(old_url): 220 if old_url is not None and url.startswith(old_url):
222 return new_url + url[len(old_url):] 221 return new_url + url[len(old_url):]
223 return url 222 return url
224 223
225 @property 224 @property
@@ -248,50 +247,41 @@ class GitConfig(object):
248 return self._cache_dict 247 return self._cache_dict
249 248
250 def _Read(self): 249 def _Read(self):
251 d = self._ReadPickle() 250 d = self._ReadJson()
252 if d is None: 251 if d is None:
253 d = self._ReadGit() 252 d = self._ReadGit()
254 self._SavePickle(d) 253 self._SaveJson(d)
255 return d 254 return d
256 255
257 def _ReadPickle(self): 256 def _ReadJson(self):
258 try: 257 try:
259 if os.path.getmtime(self._pickle) \ 258 if os.path.getmtime(self._json) \
260 <= os.path.getmtime(self.file): 259 <= os.path.getmtime(self.file):
261 os.remove(self._pickle) 260 os.remove(self._json)
262 return None 261 return None
263 except OSError: 262 except OSError:
264 return None 263 return None
265 try: 264 try:
266 Trace(': unpickle %s', self.file) 265 Trace(': parsing %s', self.file)
267 fd = open(self._pickle, 'rb') 266 fd = open(self._json)
268 try: 267 try:
269 return pickle.load(fd) 268 return json.load(fd)
270 finally: 269 finally:
271 fd.close() 270 fd.close()
272 except EOFError: 271 except (IOError, ValueError):
273 os.remove(self._pickle) 272 os.remove(self._json)
274 return None
275 except IOError:
276 os.remove(self._pickle)
277 return None
278 except pickle.PickleError:
279 os.remove(self._pickle)
280 return None 273 return None
281 274
282 def _SavePickle(self, cache): 275 def _SaveJson(self, cache):
283 try: 276 try:
284 fd = open(self._pickle, 'wb') 277 fd = open(self._json, 'w')
285 try: 278 try:
286 pickle.dump(cache, fd, pickle.HIGHEST_PROTOCOL) 279 json.dump(cache, fd, indent=2)
287 finally: 280 finally:
288 fd.close() 281 fd.close()
289 except IOError: 282 except (IOError, TypeError):
290 if os.path.exists(self._pickle): 283 if os.path.exists(self._json):
291 os.remove(self._pickle) 284 os.remove(self._json)
292 except pickle.PickleError:
293 if os.path.exists(self._pickle):
294 os.remove(self._pickle)
295 285
296 def _ReadGit(self): 286 def _ReadGit(self):
297 """ 287 """
@@ -576,6 +566,8 @@ class Remote(object):
576 return None 566 return None
577 567
578 u = self.review 568 u = self.review
569 if u.startswith('persistent-'):
570 u = u[len('persistent-'):]
579 if u.split(':')[0] not in ('http', 'https', 'sso'): 571 if u.split(':')[0] not in ('http', 'https', 'sso'):
580 u = 'http://%s' % u 572 u = 'http://%s' % u
581 if u.endswith('/Gerrit'): 573 if u.endswith('/Gerrit'):
@@ -627,9 +619,7 @@ class Remote(object):
627 def ToLocal(self, rev): 619 def ToLocal(self, rev):
628 """Convert a remote revision string to something we have locally. 620 """Convert a remote revision string to something we have locally.
629 """ 621 """
630 if IsId(rev): 622 if self.name == '.' or IsId(rev):
631 return rev
632 if rev.startswith(R_TAGS):
633 return rev 623 return rev
634 624
635 if not rev.startswith('refs/'): 625 if not rev.startswith('refs/'):
@@ -638,6 +628,10 @@ class Remote(object):
638 for spec in self.fetch: 628 for spec in self.fetch:
639 if spec.SourceMatches(rev): 629 if spec.SourceMatches(rev):
640 return spec.MapSource(rev) 630 return spec.MapSource(rev)
631
632 if not rev.startswith(R_HEADS):
633 return rev
634
641 raise GitError('remote %s does not have %s' % (self.name, rev)) 635 raise GitError('remote %s does not have %s' % (self.name, rev))
642 636
643 def WritesTo(self, ref): 637 def WritesTo(self, ref):
@@ -707,7 +701,7 @@ class Branch(object):
707 self._Set('merge', self.merge) 701 self._Set('merge', self.merge)
708 702
709 else: 703 else:
710 fd = open(self._config.file, 'ab') 704 fd = open(self._config.file, 'a')
711 try: 705 try:
712 fd.write('[branch "%s"]\n' % self.name) 706 fd.write('[branch "%s"]\n' % self.name)
713 if self.remote: 707 if self.remote: