<s> import os <EOL> from trace import Trace <EOL> HEAD = '<STR_LIT>' <EOL> R_HEADS = '<STR_LIT>' <EOL> R_TAGS = '<STR_LIT>' <EOL> R_PUB = '<STR_LIT>' <EOL> R_M = '<STR_LIT>' <EOL> class GitRefs ( object ) : <EOL> def __init__ ( self , gitdir ) : <EOL> self . _gitdir = gitdir <EOL> self . _phyref = None <EOL> self . _symref = None <EOL> self . _mtime = { } <EOL> @ property <EOL> def all ( self ) : <EOL> self . _EnsureLoaded ( ) <EOL> return self . _phyref <EOL> def get ( self , name ) : <EOL> try : <EOL> return self . all [ name ] <EOL> except KeyError : <EOL> return '<STR_LIT>' <EOL> def deleted ( self , name ) : <EOL> if self . _phyref is not None : <EOL> if name in self . _phyref : <EOL> del self . _phyref [ name ] <EOL> if name in self . _symref : <EOL> del self . _symref [ name ] <EOL> if name in self . _mtime : <EOL> del self . _mtime [ name ] <EOL> def symref ( self , name ) : <EOL> try : <EOL> self . _EnsureLoaded ( ) <EOL> return self . _symref [ name ] <EOL> except KeyError : <EOL> return '<STR_LIT>' <EOL> def _EnsureLoaded ( self ) : <EOL> if self . _phyref is None or self . _NeedUpdate ( ) : <EOL> self . _LoadAll ( ) <EOL> def _NeedUpdate ( self ) : <EOL> Trace ( '<STR_LIT>' , self . _gitdir ) <EOL> for name , mtime in self . _mtime . items ( ) : <EOL> try : <EOL> if mtime != os . path . getmtime ( os . path . join ( self . _gitdir , name ) ) : <EOL> return True <EOL> except OSError : <EOL> return True <EOL> return False <EOL> def _LoadAll ( self ) : <EOL> Trace ( '<STR_LIT>' , self . _gitdir ) <EOL> self . _phyref = { } <EOL> self . _symref = { } <EOL> self . _mtime = { } <EOL> self . _ReadPackedRefs ( ) <EOL> self . _ReadLoose ( '<STR_LIT>' ) <EOL> self . _ReadLoose1 ( os . path . join ( self . _gitdir , HEAD ) , HEAD ) <EOL> scan = self . _symref <EOL> attempts = <NUM_LIT:0> <EOL> while scan and attempts < <NUM_LIT:5> : <EOL> scan_next = { } <EOL> for name , dest in scan . items ( ) : <EOL> if dest in self . _phyref : <EOL> self . _phyref [ name ] = self . _phyref [ dest ] <EOL> else : <EOL> scan_next [ name ] = dest <EOL> scan = scan_next <EOL> attempts += <NUM_LIT:1> <EOL> def _ReadPackedRefs ( self ) : <EOL> path = os . path . join ( self . _gitdir , '<STR_LIT>' ) <EOL> try : <EOL> fd = open ( path , '<STR_LIT:r>' ) <EOL> mtime = os . path . getmtime ( path ) <EOL> except IOError : <EOL> return <EOL> except OSError : <EOL> return <EOL> try : <EOL> for line in fd : <EOL> line = str ( line ) <EOL> if line [ <NUM_LIT:0> ] == '<STR_LIT:#>' : <EOL> continue <EOL> if line [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> continue <EOL> line = line [ : - <NUM_LIT:1> ] <EOL> p = line . split ( '<STR_LIT:U+0020>' ) <EOL> ref_id = p [ <NUM_LIT:0> ] <EOL> name = p [ <NUM_LIT:1> ] <EOL> self . _phyref [ name ] = ref_id <EOL> finally : <EOL> fd . close ( ) <EOL> self . _mtime [ '<STR_LIT>' ] = mtime <EOL> def _ReadLoose ( self , prefix ) : <EOL> base = os . path . join ( self . _gitdir , prefix ) <EOL> for name in os . listdir ( base ) : <EOL> p = os . path . join ( base , name ) <EOL> if os . path . isdir ( p ) : <EOL> self . _mtime [ prefix ] = os . path . getmtime ( base ) <EOL> self . _ReadLoose ( prefix + name + '<STR_LIT:/>' ) <EOL> elif name . endswith ( '<STR_LIT>' ) : <EOL> pass <EOL> else : <EOL> self . _ReadLoose1 ( p , prefix + name ) <EOL> def _ReadLoose1 ( self , path , name ) : <EOL> try : <EOL> fd = open ( path , '<STR_LIT:rb>' ) <EOL> except IOError : <EOL> return <EOL> try : <EOL> try : <EOL> mtime = os . path . getmtime ( path ) <EOL> ref_id = fd . readline ( ) <EOL> except ( IOError , OSError ) : <EOL> return <EOL> finally : <EOL> fd . close ( ) <EOL> try : <EOL> ref_id = ref_id . decode ( ) <EOL> except AttributeError : <EOL> pass <EOL> if not ref_id : <EOL> return <EOL> ref_id = ref_id [ : - <NUM_LIT:1> ] <EOL> if ref_id . startswith ( '<STR_LIT>' ) : <EOL> self . _symref [ name ] = ref_id [ <NUM_LIT:5> : ] <EOL> else : <EOL> self . _phyref [ name ] = ref_id <EOL> self . _mtime [ name ] = mtime </s>
