#NEWLINE# def get_review_id_to_commits_map( lines , regex ) : #NEWLINE# #INDENT# "Returns a dictionary, mapping a review request ID to a list of commits.\n\n    The commits must be in the form: oldrev newrev refname (separated by\n    newlines), as given by a Git pre-receive or post-receive hook.\n\n    If a commit's commit message does not contain a review request ID, we\n    append the commit to the key 0.\n    " #NEWLINE# review_id_to_commits_map = defaultdict ( list ) #NEWLINE# new_branches = [ ] #NEWLINE# null_sha1 = ( '0' * 40 ) #NEWLINE# for line in lines : #NEWLINE# #INDENT# ( old_rev , new_rev , ref_name ) = line . split ( ) #NEWLINE# branch_name = get_branch_name ( null_sha1 ) #NEWLINE# if ( ( not branch_name ) or ( new_rev == null_sha1 ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( old_rev == null_sha1 ) : #NEWLINE# #INDENT# new_branches . append ( branch_name ) #NEWLINE# commit_hashes = get_unique_commit_hashes ( ref_name , new_rev ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# commit_hashes = get_commit_hashes ( old_rev , new_rev ) #NEWLINE# #UNINDENT# for commit_hash in commit_hashes : #NEWLINE# #INDENT# if commit_hash : #NEWLINE# #INDENT# commit_message = get_commit_message ( commit_hash ) #NEWLINE# review_request_id = get_review_request_id ( regex , commit_message ) #NEWLINE# commit = ( '%s (%s)' % ( branch_name , commit_hash ) ) #NEWLINE# review_id_to_commits_map [ review_request_id ] . append ( commit ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if new_branches : #NEWLINE# #INDENT# review_id_to_commits_map_copy = deepcopy ( review_id_to_commits_map ) #NEWLINE# for ( review_id , commit_list ) in six . iteritems ( review_id_to_commits_map_copy ) : #NEWLINE# #INDENT# for commit in commit_list : #NEWLINE# #INDENT# commit_branch = commit [ : ( commit . find ( '(' ) - 1 ) ] #NEWLINE# if ( commit_branch in new_branches ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# commit_hash = commit [ ( commit . find ( '(' ) + 1 ) : ( - 1 ) ] #NEWLINE# commit_branches = get_branches_containing_commit ( commit_hash ) #NEWLINE# for branch in set ( new_branches ) . intersection ( commit_branches ) : #NEWLINE# #INDENT# new_commit = ( '%s (%s)' % ( branch , commit_hash ) ) #NEWLINE# review_id_to_commits_map [ review_id ] . append ( new_commit ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return review_id_to_commits_map