<s> import urlparse <EOL> from StringIO import StringIO <EOL> from ConfigParser import RawConfigParser <EOL> from pygit2 import GIT_OBJ_TAG <EOL> from pygit2 import GIT_OBJ_BLOB <EOL> from pygit2 import GIT_OBJ_TREE <EOL> from pygit2 import GIT_OBJ_COMMIT <EOL> from pygit2 import GIT_SORT_TOPOLOGICAL <EOL> from ellen . utils import JagareError <EOL> from ellen . utils . format import format_commit <EOL> TREE_ORDER = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> } <EOL> def ls_tree ( repository , ref , <EOL> recursive = None , size = None , name_only = None , <EOL> req_path = None , with_commit = False ) : <EOL> """<STR_LIT>""" <EOL> if req_path : <EOL> req_path = _remove_slash ( req_path ) <EOL> try : <EOL> obj = repository . revparse_single ( ref ) <EOL> except ( ValueError , KeyError ) : <EOL> raise JagareError ( "<STR_LIT>" ) <EOL> commit_obj = None <EOL> if obj . type == GIT_OBJ_TREE : <EOL> tree_obj = obj <EOL> elif obj . type == GIT_OBJ_TAG : <EOL> commit_obj = repository . revparse_single ( obj . target . hex ) <EOL> tree_obj = commit_obj . tree <EOL> elif obj . type == GIT_OBJ_BLOB : <EOL> raise JagareError ( "<STR_LIT>" ) <EOL> elif obj . type == GIT_OBJ_COMMIT : <EOL> commit_obj = obj <EOL> tree_obj = obj . tree <EOL> if req_path : <EOL> tree_entry = tree_obj [ req_path ] <EOL> tree_obj = repository [ tree_entry . id ] <EOL> walker = _walk_tree ( tree_obj , req_path ) <EOL> else : <EOL> walker = _walk_tree ( tree_obj ) <EOL> ret_tree = { } <EOL> submodule_obj = None <EOL> submodule = None <EOL> try : <EOL> submodule_obj = repository . revparse_single ( "<STR_LIT>" % ref ) <EOL> submodule = _parse_submodule ( repository , submodule_obj ) <EOL> except ( ValueError , KeyError ) : <EOL> pass <EOL> for index , ( entry , path ) in enumerate ( walker ) : <EOL> mode = '<STR_LIT>' % entry . filemode <EOL> if mode == '<STR_LIT>' : <EOL> objtype = '<STR_LIT>' <EOL> elif mode == '<STR_LIT>' : <EOL> objtype = '<STR_LIT>' <EOL> else : <EOL> objtype = '<STR_LIT>' <EOL> path = "<STR_LIT>" % ( path , entry . name ) if path else entry . name <EOL> if recursive and entry . type == GIT_OBJ_TREE : <EOL> _tree = repository [ entry . id ] <EOL> _tree_list = _walk_tree ( _tree , path ) <EOL> for _index , _entry in enumerate ( _tree_list ) : <EOL> walker . insert ( index + _index + <NUM_LIT:1> , _entry ) <EOL> continue <EOL> if name_only : <EOL> ret_tree [ path ] = path <EOL> continue <EOL> item = { <EOL> "<STR_LIT:id>" : entry . hex , <EOL> "<STR_LIT>" : mode , <EOL> "<STR_LIT:type>" : objtype , <EOL> "<STR_LIT>" : entry . hex , <EOL> "<STR_LIT:path>" : path , <EOL> "<STR_LIT:name>" : entry . name <EOL> } <EOL> if entry . type == GIT_OBJ_COMMIT : <EOL> item [ '<STR_LIT>' ] = _format_submodule ( submodule , path ) <EOL> if size : <EOL> if objtype == '<STR_LIT>' : <EOL> blob = repository [ entry . id ] <EOL> item [ '<STR_LIT:size>' ] = blob . size <EOL> else : <EOL> item [ '<STR_LIT:size>' ] = '<STR_LIT:->' <EOL> ret_tree [ path ] = item <EOL> if name_only : <EOL> return ret_tree . values ( ) <EOL> if with_commit and commit_obj : <EOL> _format_with_last_commit ( repository , ret_tree , commit_obj ) <EOL> tree_list = ret_tree . values ( ) <EOL> tree_list . sort ( key = lambda i : ( TREE_ORDER [ i [ '<STR_LIT:type>' ] ] , i [ '<STR_LIT:name>' ] ) ) <EOL> return tree_list <EOL> def _walk_tree ( tree , path = None ) : <EOL> _list = [ ] <EOL> for entry in tree : <EOL> _list . append ( ( entry , path ) ) <EOL> return _list <EOL> def _remove_slash ( path ) : <EOL> if path [ - <NUM_LIT:1> ] == '<STR_LIT:/>' : <EOL> return path [ : - <NUM_LIT:1> ] <EOL> return path <EOL> def _format_submodule_conf ( raw ) : <EOL> if isinstance ( raw , unicode ) : <EOL> lines = raw . splitlines ( ) <EOL> elif isinstance ( raw , str ) : <EOL> lines = raw . decode ( "<STR_LIT>" ) . splitlines ( ) <EOL> else : <EOL> return None <EOL> lines = map ( lambda line : line . strip ( ) , lines ) <EOL> return "<STR_LIT:\n>" . join ( lines ) <EOL> def _read_blob ( repository , sha ) : <EOL> obj = repository . revparse_single ( sha ) <EOL> if obj . type != GIT_OBJ_BLOB : <EOL> return None <EOL> return obj . data <EOL> def _parse_submodule ( repository , submodule_obj ) : <EOL> submodule_conf_raw = submodule_obj . data <EOL> submodule_conf_raw = _format_submodule_conf ( submodule_conf_raw ) <EOL> config = RawConfigParser ( allow_no_value = True ) <EOL> config . readfp ( StringIO ( submodule_conf_raw ) ) <EOL> return config <EOL> def _parse_submodule_url ( url ) : <EOL> parser = urlparse . urlparse ( url ) <EOL> netloc = parser . netloc <EOL> if not netloc : <EOL> if parser . path == url : <EOL> netloc = parser . path . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:@>' ) [ - <NUM_LIT:1> ] <EOL> else : <EOL> return url <EOL> elif netloc == '<STR_LIT>' : <EOL> return '<STR_LIT:code>' <EOL> elif netloc == '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> elif netloc == '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> return netloc <EOL> def _calc_is_changed ( commit , path , ret , type ) : <EOL> if len ( commit . parents ) > <NUM_LIT:1> : <EOL> return <EOL> if commit . is_changed ( [ path ] , no_diff = True ) [ <NUM_LIT:0> ] : <EOL> ret [ path ] = <NUM_LIT:1> <EOL> def _format_with_last_commit ( repository , ret_tree , to_commit ) : <EOL> walker = repository . walk ( to_commit . id , GIT_SORT_TOPOLOGICAL ) <EOL> paths = [ k for k , v in ret_tree . iteritems ( ) ] <EOL> ret = { } <EOL> for commit in walker : <EOL> for path in paths : <EOL> _calc_is_changed ( commit , path , ret , ret_tree [ path ] [ '<STR_LIT:type>' ] ) <EOL> if not ret : <EOL> continue <EOL> fc = format_commit ( commit , None ) <EOL> for path , r in ret . iteritems ( ) : <EOL> ret_tree [ path ] [ '<STR_LIT>' ] = fc <EOL> paths . remove ( path ) <EOL> if not paths : <EOL> break <EOL> ret = { } <EOL> def _ls_tree ( tree , path ) : <EOL> return <EOL> def _format_submodule ( modules , path ) : <EOL> submodule = { } <EOL> submodule [ '<STR_LIT:host>' ] = None <EOL> submodule [ '<STR_LIT:url>' ] = None <EOL> section_name = ( '<STR_LIT>' . format ( submodule_name = path ) ) <EOL> if modules and modules . has_section ( section_name ) : <EOL> submodule . update ( dict ( modules . items ( section_name ) ) ) <EOL> url = submodule . get ( '<STR_LIT:url>' ) <EOL> if url : <EOL> submodule [ '<STR_LIT:host>' ] = _parse_submodule_url ( submodule [ '<STR_LIT:url>' ] ) <EOL> if url . endswith ( '<STR_LIT>' ) : <EOL> submodule [ '<STR_LIT:url>' ] = url [ : - <NUM_LIT:4> ] <EOL> return submodule </s>
