<s> from __future__ import absolute_import , unicode_literals , print_function , division <EOL> from flask import current_app , safe_join <EOL> from werkzeug . exceptions import NotFound <EOL> from pygit2 import Repository , GIT_OBJ_COMMIT , GIT_OBJ_BLOB , GIT_OBJ_TREE , GIT_OBJ_TAG <EOL> def get_repo ( repo_key ) : <EOL> path = safe_join ( current_app . config [ '<STR_LIT>' ] , repo_key ) <EOL> try : <EOL> return Repository ( path ) <EOL> except KeyError : <EOL> raise NotFound ( "<STR_LIT>" ) <EOL> def get_commit ( repo , sha ) : <EOL> try : <EOL> commit = repo [ unicode ( sha ) ] <EOL> except KeyError : <EOL> raise NotFound ( "<STR_LIT>" ) <EOL> if commit . type != GIT_OBJ_COMMIT : <EOL> raise NotFound ( "<STR_LIT>" ) <EOL> return commit <EOL> def get_tree ( repo , sha ) : <EOL> try : <EOL> obj = repo [ unicode ( sha ) ] <EOL> except KeyError : <EOL> raise NotFound ( "<STR_LIT>" ) <EOL> if obj . type == GIT_OBJ_TREE : <EOL> return obj <EOL> elif obj . type == GIT_OBJ_COMMIT : <EOL> return obj . tree <EOL> elif obj . type == GIT_OBJ_TAG : <EOL> obj = repo [ obj . target ] <EOL> if obj . type == GIT_OBJ_TAG : <EOL> return get_tree ( repo , obj . target ) <EOL> else : <EOL> return obj . tree <EOL> else : <EOL> raise NotFound ( "<STR_LIT>" ) <EOL> def get_blob ( repo , sha ) : <EOL> try : <EOL> blob = repo [ unicode ( sha ) ] <EOL> except KeyError : <EOL> raise NotFound ( "<STR_LIT>" ) <EOL> if blob . type != GIT_OBJ_BLOB : <EOL> raise NotFound ( "<STR_LIT>" ) <EOL> return blob <EOL> def get_tag ( repo , sha ) : <EOL> try : <EOL> tag = repo [ unicode ( sha ) ] <EOL> except KeyError : <EOL> raise NotFound ( "<STR_LIT>" ) <EOL> if tag . type != GIT_OBJ_TAG : <EOL> raise NotFound ( "<STR_LIT>" ) <EOL> return tag <EOL> def lookup_ref ( repo , ref_name ) : <EOL> try : <EOL> return repo . lookup_reference ( ref_name ) <EOL> except ( ValueError , KeyError ) : <EOL> if "<STR_LIT:/>" in ref_name and not ref_name . startswith ( "<STR_LIT>" ) : <EOL> ref_name = "<STR_LIT>" + ref_name <EOL> else : <EOL> ref_name = "<STR_LIT>" + ref_name <EOL> try : <EOL> return repo . lookup_reference ( ref_name ) <EOL> except ( ValueError , KeyError ) : <EOL> return None </s>
