Changeset 3202

Show
Ignore:
Timestamp:
02/09/08 05:15:03 (10 months ago)
Author:
hvr
Message:

GitPlugin:

  • factorized git execution
  • implemented GIT version check (requires >= 1.5.2 now)
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • gitplugin/0.11/gitplugin/git_fs.py

    r3197 r3202  
    3939                self._version = None 
    4040 
     41                try: 
     42                        self._version = PyGIT.git_version() 
     43                except PyGIT.GitError, e: 
     44                        self.log.error("GitError: "+e.message) 
     45 
     46                if self._version: 
     47                        self.log.info("detected GIT version %s" % self._version['v_str']) 
     48                        self.env.systeminfo.append(('GIT', self._version['v_str'])) 
     49                        if self._version['v_compatible']: 
     50                                self.log.error("GIT version %s installed not compatible (need >= %s)" % 
     51                                               (self._version['v_str'], self._version['v_min_str'])) 
     52 
    4153        def _format_sha_link(self, formatter, ns, sha, label, fullmatch=None): 
    4254                try: 
     
    92104                """GitRepository factory method""" 
    93105                if not self._version: 
    94                         self._version = PyGIT.git_version() 
    95                         self.env.systeminfo.append(('GIT', self._version)) 
     106                        raise TracError("GIT backend not available") 
     107                elif not self._version['v_compatible']: 
     108                        raise TracError("GIT version %s installed not compatible (need >= %s)" % 
     109                                        (self._version['v_str'], self._version['v_min_str'])) 
    96110 
    97111                options = dict(self.config.options(type)) 
  • gitplugin/0.11/gitplugin/PyGIT.py

    r3200 r3202  
    1717#from traceback import print_stack 
    1818 
    19 _profile_git_calls = False 
    20  
    2119class GitError(Exception): 
    2220    pass 
     
    2523    pass 
    2624 
     25GIT_CMD = "git" 
     26GIT_VERSION_MIN_REQUIRED = (1,5,2) 
     27GIT_PROFILE = False 
     28 
     29def _git_execute(gitcmd, git_dir=None, *args): 
     30    if GIT_PROFILE: 
     31        t = time.time() 
     32 
     33    # construct command tuple 
     34    cmd = [GIT_CMD] 
     35    if git_dir: 
     36        cmd.append('--git-dir=%s' % git_dir) 
     37    cmd.append(gitcmd) 
     38    cmd.extend(args) 
     39 
     40    # fds = (input, output, error) 
     41    fds = os.popen3(cmd) 
     42 
     43    if GIT_PROFILE: 
     44        t = time.time() - t # doesn't work actually, as popen3 runs async 
     45        print >>sys.stderr, "GIT: took %6.2fs for '%s'" % (t, cmd) 
     46        pass 
     47 
     48    return fds 
     49 
    2750def git_version(): 
    2851    try: 
    29         (input, output, error) = os.popen3('git --version') 
     52        output = _git_execute("version")[1] 
    3053        [v] = output.readlines() 
    31         [a,b,c] = v.strip().split() 
    32         return c 
     54        [a,b,version] = v.strip().split() 
     55        split_version = tuple(map(int, version.split('.'))) 
     56 
     57        result = {} 
     58        result['v_str'] = version 
     59        result['v_tuple'] = split_version 
     60        result['v_min_tuple'] = GIT_VERSION_MIN_REQUIRED 
     61        result['v_min_str'] = ".".join(map(str, GIT_VERSION_MIN_REQUIRED)) 
     62        result['v_compatible'] = split_version >= GIT_VERSION_MIN_REQUIRED 
     63        return result 
    3364    except: 
    34         raise GitError 
     65        raise GitError("Could not retrieve GIT version") 
    3566 
    3667class StorageFactory: 
     
    84115    def __del__(self): 
    85116        self.logger.debug("PyGIT.Storage instance %d destructed" % id(self)) 
     117 
     118    def _git_call_f(self, gitcmd, args=[]): 
     119        (input, output, error) = _git_execute(gitcmd, self.repo, *args) 
     120        return output 
     121 
     122    def _git_call(self, cmd, args=[]): 
     123        return self._git_call_f(cmd, args).read() 
    86124 
    87125    def _invalidate_caches(self,youngest_rev=None): 
     
    190228        return self.history_relative_rev(sha, +1) 
    191229 
    192     def _git_call_f(self, gitcmd, args=[]): 
    193         #print "GIT: "+cmd 
    194         if _profile_git_calls: 
    195             t = time.time() 
    196             pass 
    197  
    198         cmd = ["git", '--git-dir=%s' % self.repo, gitcmd] 
    199         cmd.extend(args) 
    200  
    201         (input, output, error) = os.popen3(cmd) 
    202  
    203         if _profile_git_calls: 
    204             t = time.time() - t # doesn't work actually, as popen3 runs async 
    205             print >>sys.stderr, "GIT: took %6.2fs for '%s'" % (t, cmd) 
    206             pass 
    207  
    208         return output 
    209  
    210     def _git_call(self, cmd, args=[]): 
    211         return self._git_call_f(cmd, args).read() 
    212  
    213230    def get_commit_encoding(self): 
    214231        if self.commit_encoding is None: 
     
    258275        for e in self._git_call_f("branch", ["-v", "--no-abbrev"]).readlines(): 
    259276            (bname,bsha)=e[1:].strip().split()[:2] 
    260             if e[0]=='*'
     277            if e.startswith('*')
    261278                result.insert(0,(bname,bsha)) 
    262279            else: 
     
    381398            assert line 
    382399            if in_metadata: 
    383                 in_metadata = line[0] != '\t' 
     400                in_metadata = not line.startswith('\t') 
    384401            else: 
    385402                split_line = line.split() 
     
    430447if __name__ == '__main__': 
    431448    import sys, logging 
     449 
     450    print "git version [%s]" % str(git_version()) 
    432451 
    433452    g = Storage(sys.argv[1], logging)