<s> """<STR_LIT>""" <EOL> import pygit2 <EOL> import shutil <EOL> import itertools <EOL> from . exceptions import ( <EOL> NotJsonError , InvalidKeyError , DifferentRepoError , StagedDataError ) <EOL> from . wrappers import Commit , Diff , Conflict , Merge <EOL> import constants <EOL> import utils <EOL> class Repository ( object ) : <EOL> def __init__ ( self , repo , dumps , loads ) : <EOL> self . _repo = repo <EOL> self . _global_name = utils . global_config ( '<STR_LIT>' ) <EOL> self . _global_email = utils . global_config ( '<STR_LIT>' ) <EOL> self . _dumps = dumps <EOL> self . _loads = loads <EOL> def __eq__ ( self , other ) : <EOL> return self . _repo . path == other . _repo . path <EOL> def _key2ref ( self , key ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( key , basestring ) : <EOL> raise InvalidKeyError ( "<STR_LIT>" % key ) <EOL> elif key [ - <NUM_LIT:1> ] == '<STR_LIT:.>' or key [ - <NUM_LIT:1> ] == '<STR_LIT:/>' or key [ <NUM_LIT:0> ] == '<STR_LIT:/>' or key [ <NUM_LIT:0> ] == '<STR_LIT:.>' : <EOL> raise InvalidKeyError ( "<STR_LIT>" % key ) <EOL> else : <EOL> return '<STR_LIT>' % key <EOL> def _navigate_tree ( self , oid , path ) : <EOL> """<STR_LIT>""" <EOL> steps = path . split ( '<STR_LIT:/>' ) <EOL> for step in steps : <EOL> oid = self . _repo [ oid ] [ step ] . oid <EOL> return oid <EOL> def _build_commit ( self , pygit2_commit ) : <EOL> key = pygit2_commit . tree [ <NUM_LIT:0> ] . name <EOL> raw = self . _repo [ pygit2_commit . tree [ <NUM_LIT:0> ] . oid ] . data <EOL> value = self . _loads ( raw ) <EOL> return Commit ( self , key , value , pygit2_commit ) <EOL> def _head_target ( self ) : <EOL> return self . _repo . lookup_reference ( '<STR_LIT>' ) . target <EOL> def _repo_head ( self ) : <EOL> try : <EOL> return self . _repo [ self . _repo . lookup_reference ( self . _head_target ( ) ) . oid ] <EOL> except KeyError : <EOL> return None <EOL> def add ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> self . _key2ref ( key ) <EOL> try : <EOL> blob_id = self . _repo . write ( pygit2 . GIT_OBJ_BLOB , self . _dumps ( value ) ) <EOL> except ValueError as e : <EOL> raise NotJsonError ( e ) <EOL> except TypeError as e : <EOL> raise NotJsonError ( e ) <EOL> if key in self . _repo . index : <EOL> del self . _repo . index [ key ] <EOL> working_tree_id = self . _repo . index . write_tree ( ) <EOL> working_tree = self . _repo [ working_tree_id ] <EOL> new_entry = b"<STR_LIT>" % ( key , blob_id ) <EOL> tree_data = working_tree . read_raw ( ) + new_entry <EOL> working_tree_id = self . _repo . write ( pygit2 . GIT_OBJ_TREE , tree_data ) <EOL> self . _repo . index . read_tree ( working_tree_id ) <EOL> self . _repo . index . write ( ) <EOL> def checkout ( self , source , dest , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> message = "<STR_LIT>" % ( dest , source ) <EOL> commit = self . head ( source ) <EOL> self . commit ( dest , commit . data , message = message , parents = [ commit ] ) <EOL> def commit ( self , key = None , value = None , add = True , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> keys = [ key ] if key is not None else [ e . path for e in self . _repo . index ] <EOL> message = kwargs . pop ( '<STR_LIT:message>' , '<STR_LIT>' ) <EOL> parents = kwargs . pop ( '<STR_LIT>' , None ) <EOL> author = kwargs . pop ( '<STR_LIT>' , utils . signature ( self . _global_name , <EOL> self . _global_email ) ) <EOL> committer = kwargs . pop ( '<STR_LIT>' , author ) <EOL> if kwargs : <EOL> raise TypeError ( "<STR_LIT>" % kwargs ) <EOL> if key is None and value is not None : <EOL> raise InvalidKeyError ( ) <EOL> if parents is not None : <EOL> for parent in parents : <EOL> if parent . repo != self : <EOL> raise DifferentRepoError ( ) <EOL> if add is True and key is not None and value is not None : <EOL> self . add ( key , value ) <EOL> repo_head = self . _repo_head ( ) <EOL> tree_id = self . _repo . index . write_tree ( ) <EOL> self . _repo . create_commit ( self . _head_target ( ) , author , committer , <EOL> message , tree_id , <EOL> [ repo_head . oid ] if repo_head else [ ] ) <EOL> for key in keys : <EOL> if parents is None : <EOL> parents = [ self . head ( key ) ] if self . committed ( key ) else [ ] <EOL> try : <EOL> blob_id = self . _navigate_tree ( tree_id , key ) <EOL> key_tree_data = b"<STR_LIT>" % ( key , blob_id ) <EOL> key_tree_id = self . _repo . write ( pygit2 . GIT_OBJ_TREE , key_tree_data ) <EOL> self . _repo . create_commit ( self . _key2ref ( key ) , author , <EOL> committer , message , key_tree_id , <EOL> [ parent . oid for parent in parents ] ) <EOL> except pygit2 . GitError as e : <EOL> if str ( e ) . startswith ( '<STR_LIT>' ) : <EOL> raise InvalidKeyError ( e ) <EOL> else : <EOL> raise e <EOL> def committed ( self , key ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _repo . lookup_reference ( self . _key2ref ( key ) ) <EOL> return True <EOL> except KeyError : <EOL> return False <EOL> def destroy ( self ) : <EOL> """<STR_LIT>""" <EOL> shutil . rmtree ( self . _repo . path ) <EOL> self . _repo = None <EOL> def head ( self , key , back = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return itertools . islice ( self . log ( key ) , back , back + <NUM_LIT:1> ) . next ( ) <EOL> except KeyError : <EOL> raise KeyError ( "<STR_LIT>" % key ) <EOL> except StopIteration : <EOL> raise IndexError ( "<STR_LIT>" % ( key , back ) ) <EOL> def index ( self , key ) : <EOL> """<STR_LIT>""" <EOL> self . _repo . index . read ( ) <EOL> raw = self . _repo [ self . _repo . index [ key ] . oid ] . data <EOL> return self . _loads ( raw ) <EOL> def merge ( self , dest , key = None , commit = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if commit is None : <EOL> commit = self . head ( key ) <EOL> if commit . key == dest : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> dest_head = self . head ( dest ) <EOL> if commit . oid == dest_head . oid : <EOL> return Merge ( True , commit , dest_head , "<STR_LIT>" , result = commit ) <EOL> try : <EOL> shared_commit = ( dc for dc in self . log ( commit = dest_head ) <EOL> if dc . oid in ( sc . oid for sc in self . log ( commit = commit ) ) ) . next ( ) <EOL> except StopIteration : <EOL> return Merge ( False , commit , dest_head , "<STR_LIT>" ) <EOL> source_diff = Diff ( shared_commit . data , commit . data ) <EOL> dest_diff = Diff ( shared_commit . data , dest_head . data ) <EOL> conflict = Conflict ( source_diff , dest_diff ) <EOL> if conflict : <EOL> return Merge ( False , commit , dest_head , "<STR_LIT>" , <EOL> conflict = conflict ) <EOL> else : <EOL> merged_data = dest_diff . apply ( source_diff . apply ( shared_commit . data ) ) <EOL> message = "<STR_LIT>" % ( <EOL> commit . hex [ <NUM_LIT:0> : <NUM_LIT:10> ] , dest_head . hex [ <NUM_LIT:0> : <NUM_LIT:10> ] , shared_commit . hex [ <NUM_LIT:0> : <NUM_LIT:10> ] ) <EOL> parents = [ dest_head , commit ] <EOL> result = self . commit ( dest , merged_data , message = message , parents = parents , ** kwargs ) <EOL> return Merge ( True , commit , dest_head , message , result = result ) <EOL> def log ( self , key = None , commit = None , order = constants . GIT_SORT_TOPOLOGICAL ) : <EOL> """<STR_LIT>""" <EOL> if key is None and commit is None : <EOL> raise TypeError ( ) <EOL> elif commit is None : <EOL> c = self . _repo [ self . _repo . lookup_reference ( self . _key2ref ( key ) ) . oid ] <EOL> commit = self . _build_commit ( c ) <EOL> return ( self . _build_commit ( c ) for c in self . _repo . walk ( commit . oid , order ) ) <EOL> def remove ( self , key , force = False ) : <EOL> """<STR_LIT>""" <EOL> if force is True or self . staged ( key ) is False : <EOL> del self . _repo . index [ key ] <EOL> elif force is False and self . staged ( key ) : <EOL> raise StagedDataError ( "<STR_LIT>" % key ) <EOL> self . _repo . lookup_reference ( self . _key2ref ( key ) ) . delete ( ) <EOL> def reset ( self , key ) : <EOL> """<STR_LIT>""" <EOL> self . add ( key , self . head ( key ) . data ) <EOL> def show ( self , key , back = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . head ( key , back = back ) . data <EOL> def staged ( self , key ) : <EOL> """<STR_LIT>""" <EOL> if key in self . _repo . index : <EOL> if self . committed ( key ) : <EOL> return self . index ( key ) != self . show ( key ) <EOL> else : <EOL> return True <EOL> else : <EOL> return False </s>
