<s> from __future__ import absolute_import , unicode_literals , print_function , division <EOL> from itertools import ifilter , islice <EOL> from flask import current_app , request , Blueprint <EOL> from werkzeug . exceptions import NotFound , BadRequest <EOL> from pygit2 import GIT_REF_SYMBOLIC , GIT_SORT_TIME <EOL> from restfulgit . plumbing . retrieval import get_repo , get_commit as _get_commit , get_blob as _get_blob , get_tree as _get_tree , lookup_ref , get_tag as _get_tag <EOL> from restfulgit . plumbing . converters import convert_commit , convert_blob , convert_tree , convert_ref , convert_tag <EOL> from restfulgit . utils . json import jsonify <EOL> from restfulgit . utils . cors import corsify <EOL> from restfulgit . utils . json_err_pages import json_error_page , register_general_error_handler <EOL> from restfulgit . utils . url_converters import SHAConverter , register_converter <EOL> plumbing = Blueprint ( '<STR_LIT>' , __name__ ) <EOL> register_converter ( plumbing , '<STR_LIT>' , SHAConverter ) <EOL> register_general_error_handler ( plumbing , json_error_page ) <EOL> @ plumbing . route ( '<STR_LIT>' ) <EOL> @ corsify <EOL> @ jsonify <EOL> def get_commit_list ( repo_key ) : <EOL> ref_name = request . args . get ( '<STR_LIT>' ) or None <EOL> start_sha = request . args . get ( '<STR_LIT>' ) or None <EOL> limit = request . args . get ( '<STR_LIT>' ) or current_app . config [ '<STR_LIT>' ] <EOL> try : <EOL> limit = int ( limit ) <EOL> except ValueError : <EOL> raise BadRequest ( "<STR_LIT>" ) <EOL> if limit < <NUM_LIT:0> : <EOL> raise BadRequest ( "<STR_LIT>" ) <EOL> repo = get_repo ( repo_key ) <EOL> start_commit_id = None <EOL> if start_sha is not None : <EOL> start_commit_id = start_sha <EOL> else : <EOL> if ref_name is None : <EOL> ref_name = "<STR_LIT>" <EOL> ref = lookup_ref ( repo , ref_name ) <EOL> if ref is None : <EOL> raise NotFound ( "<STR_LIT>" ) <EOL> start_ref = lookup_ref ( repo , ref_name ) <EOL> try : <EOL> start_commit_id = start_ref . resolve ( ) . target <EOL> except KeyError : <EOL> if ref_name == "<STR_LIT>" : <EOL> return [ ] <EOL> else : <EOL> raise NotFound ( "<STR_LIT>" ) <EOL> try : <EOL> walker = repo . walk ( start_commit_id , GIT_SORT_TIME ) <EOL> except ValueError : <EOL> raise BadRequest ( "<STR_LIT>" ) <EOL> except KeyError : <EOL> raise NotFound ( "<STR_LIT>" ) <EOL> commits = [ convert_commit ( repo_key , commit ) for commit in islice ( walker , limit ) ] <EOL> return commits <EOL> @ plumbing . route ( '<STR_LIT>' ) <EOL> @ corsify <EOL> @ jsonify <EOL> def get_commit ( repo_key , sha ) : <EOL> repo = get_repo ( repo_key ) <EOL> commit = _get_commit ( repo , sha ) <EOL> return convert_commit ( repo_key , commit ) <EOL> @ plumbing . route ( '<STR_LIT>' ) <EOL> @ corsify <EOL> @ jsonify <EOL> def get_merge_base_for_commits ( repo_key , left_sha , right_sha ) : <EOL> repo = get_repo ( repo_key ) <EOL> left_commit = _get_commit ( repo , left_sha ) <EOL> right_commit = _get_commit ( repo , right_sha ) <EOL> try : <EOL> merge_base_oid = repo . merge_base ( left_commit . id , right_commit . id ) <EOL> merge_base_commit = repo [ merge_base_oid ] <EOL> except KeyError : <EOL> return None <EOL> else : <EOL> return convert_commit ( repo_key , merge_base_commit ) <EOL> @ plumbing . route ( '<STR_LIT>' ) <EOL> @ corsify <EOL> @ jsonify <EOL> def get_tree ( repo_key , sha ) : <EOL> recursive = request . args . get ( '<STR_LIT>' ) == '<STR_LIT:1>' <EOL> repo = get_repo ( repo_key ) <EOL> tree = _get_tree ( repo , sha ) <EOL> return convert_tree ( repo_key , repo , tree , recursive ) <EOL> @ plumbing . route ( '<STR_LIT>' ) <EOL> @ corsify <EOL> @ jsonify <EOL> def get_blob ( repo_key , sha ) : <EOL> repo = get_repo ( repo_key ) <EOL> blob = _get_blob ( repo , sha ) <EOL> return convert_blob ( repo_key , blob ) <EOL> @ plumbing . route ( '<STR_LIT>' ) <EOL> @ corsify <EOL> @ jsonify <EOL> def get_tag ( repo_key , sha ) : <EOL> repo = get_repo ( repo_key ) <EOL> tag = _get_tag ( repo , sha ) <EOL> return convert_tag ( repo_key , repo , tag ) <EOL> @ plumbing . route ( '<STR_LIT>' ) <EOL> @ plumbing . route ( '<STR_LIT>' ) <EOL> @ corsify <EOL> @ jsonify <EOL> def get_refs ( repo_key , ref_path = None ) : <EOL> if ref_path is not None : <EOL> ref_path = "<STR_LIT>" + ref_path <EOL> else : <EOL> ref_path = "<STR_LIT>" <EOL> repo = get_repo ( repo_key ) <EOL> ref_names = ifilter ( lambda x : x . startswith ( ref_path ) , repo . listall_references ( ) ) <EOL> references = ( repo . lookup_reference ( ref_name ) for ref_name in ref_names ) <EOL> nonsymbolic_refs = ifilter ( lambda x : x . type != GIT_REF_SYMBOLIC , references ) <EOL> ref_data = [ <EOL> convert_ref ( repo_key , reference , repo [ reference . target ] ) <EOL> for reference in nonsymbolic_refs <EOL> ] <EOL> if len ( ref_data ) == <NUM_LIT:1> and ref_data [ <NUM_LIT:0> ] [ '<STR_LIT>' ] == ref_path : <EOL> ref_data = ref_data [ <NUM_LIT:0> ] <EOL> return ref_data </s>
