<s> import sys <EOL> import psycopg2 <EOL> import argparse <EOL> import os <EOL> import subprocess <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( "<STR_LIT>" , type = int ) <EOL> parser . add_argument ( "<STR_LIT>" , type = int ) <EOL> arguments = parser . parse_args ( ) <EOL> os . setgid ( arguments . gid ) <EOL> os . setuid ( arguments . uid ) <EOL> import configuration <EOL> db = psycopg2 . connect ( ** configuration . database . PARAMETERS ) <EOL> cursor = db . cursor ( ) <EOL> try : <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> except psycopg2 . ProgrammingError : <EOL> db . rollback ( ) <EOL> else : <EOL> db . close ( ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> def fetch_commit_sha1 ( commit_id ) : <EOL> cursor . execute ( "<STR_LIT>" , ( commit_id , ) ) <EOL> ( sha1 , ) = cursor . fetchone ( ) <EOL> return sha1 <EOL> def get_parent_sha1s ( repository_path , sha1 ) : <EOL> output = subprocess . check_output ( <EOL> [ configuration . executables . GIT , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , sha1 ] , <EOL> cwd = repository_path ) <EOL> return output . strip ( ) . split ( ) <EOL> def is_ancestor_of ( repository_path , ancestor_sha1 , descendant_sha1 ) : <EOL> try : <EOL> merge_base_sha1 = subprocess . check_output ( <EOL> [ configuration . executables . GIT , "<STR_LIT>" , <EOL> ancestor_sha1 , descendant_sha1 ] , <EOL> cwd = repository_path ) . strip ( ) <EOL> except subprocess . CalledProcessError : <EOL> return False <EOL> else : <EOL> return merge_base_sha1 == ancestor_sha1 <EOL> cursor . execute ( """<STR_LIT>""" ) <EOL> cursor . execute ( """<STR_LIT>""" ) <EOL> for repository_path , rebase_id , old_head_id , old_upstream_id , new_upstream_id in cursor . fetchall ( ) : <EOL> old_head_sha1 = fetch_commit_sha1 ( old_head_id ) <EOL> old_head_parent_sha1s = get_parent_sha1s ( repository_path , old_head_sha1 ) <EOL> if len ( old_head_parent_sha1s ) != <NUM_LIT:2> : <EOL> continue <EOL> old_upstream_sha1 = fetch_commit_sha1 ( old_upstream_id ) <EOL> new_upstream_sha1 = fetch_commit_sha1 ( new_upstream_id ) <EOL> if old_head_parent_sha1s [ <NUM_LIT:1> ] != new_upstream_sha1 : <EOL> continue <EOL> if not is_ancestor_of ( repository_path , old_upstream_sha1 , new_upstream_sha1 ) : <EOL> continue <EOL> cursor . execute ( "<STR_LIT>" , <EOL> ( old_head_parent_sha1s [ <NUM_LIT:0> ] , ) ) <EOL> ( real_old_head_id , ) = cursor . fetchone ( ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( real_old_head_id , old_head_id , rebase_id ) ) <EOL> db . commit ( ) <EOL> db . close ( ) </s>
