hgext/convert/git.py
changeset 13756 6b7077df4aa5
parent 12144 be9c4131a8f4
child 14177 fc004d16633f
equal deleted inserted replaced
13755:e45780ac8292 13756:6b7077df4aa5
    15 class convert_git(converter_source):
    15 class convert_git(converter_source):
    16     # Windows does not support GIT_DIR= construct while other systems
    16     # Windows does not support GIT_DIR= construct while other systems
    17     # cannot remove environment variable. Just assume none have
    17     # cannot remove environment variable. Just assume none have
    18     # both issues.
    18     # both issues.
    19     if hasattr(os, 'unsetenv'):
    19     if hasattr(os, 'unsetenv'):
    20         def gitopen(self, s):
    20         def gitopen(self, s, noerr=False):
    21             prevgitdir = os.environ.get('GIT_DIR')
    21             prevgitdir = os.environ.get('GIT_DIR')
    22             os.environ['GIT_DIR'] = self.path
    22             os.environ['GIT_DIR'] = self.path
    23             try:
    23             try:
    24                 return util.popen(s, 'rb')
    24                 if noerr:
       
    25                     (stdin, stdout, stderr) = util.popen3(s)
       
    26                     return stdout
       
    27                 else:
       
    28                     return util.popen(s, 'rb')
    25             finally:
    29             finally:
    26                 if prevgitdir is None:
    30                 if prevgitdir is None:
    27                     del os.environ['GIT_DIR']
    31                     del os.environ['GIT_DIR']
    28                 else:
    32                 else:
    29                     os.environ['GIT_DIR'] = prevgitdir
    33                     os.environ['GIT_DIR'] = prevgitdir
    30     else:
    34     else:
    31         def gitopen(self, s):
    35         def gitopen(self, s, noerr=False):
    32             return util.popen('GIT_DIR=%s %s' % (self.path, s), 'rb')
    36             if noerr:
       
    37                 (sin, so, se) = util.popen3('GIT_DIR=%s %s' % (self.path, s))
       
    38                 return stdout
       
    39             else:
       
    40                 util.popen('GIT_DIR=%s %s' % (self.path, s), 'rb')
    33 
    41 
    34     def gitread(self, s):
    42     def gitread(self, s):
    35         fh = self.gitopen(s)
    43         fh = self.gitopen(s)
    36         data = fh.read()
    44         data = fh.read()
    37         return data, fh.close()
    45         return data, fh.close()
   166             changes = [f.rstrip('\n') for f in fh]
   174             changes = [f.rstrip('\n') for f in fh]
   167         if fh.close():
   175         if fh.close():
   168             raise util.Abort(_('cannot read changes in %s') % version)
   176             raise util.Abort(_('cannot read changes in %s') % version)
   169 
   177 
   170         return changes
   178         return changes
       
   179 
       
   180     def getbookmarks(self):
       
   181         bookmarks = {}
       
   182 
       
   183         # Interesting references in git are prefixed
       
   184         prefix = 'refs/heads/'
       
   185         prefixlen = len(prefix)
       
   186 
       
   187         # factor two commands
       
   188         gitcmd = { 'remote/': 'git ls-remote --heads origin',
       
   189                           '': 'git show-ref'}
       
   190 
       
   191         # Origin heads
       
   192         for reftype in gitcmd:
       
   193             try:
       
   194                 fh = self.gitopen(gitcmd[reftype], noerr=True)
       
   195                 for line in fh:
       
   196                     line = line.strip()
       
   197                     rev, name = line.split(None, 1)
       
   198                     if not name.startswith(prefix):
       
   199                         continue
       
   200                     name = '%s%s' % (reftype, name[prefixlen:])
       
   201                     bookmarks[name] = rev
       
   202             except:
       
   203                 pass
       
   204 
       
   205         return bookmarks