<s> from subprocess import Popen as process , PIPE <EOL> from sys import argv , stderr , exit <EOL> import re <EOL> from dbutils import find_file , describe_file <EOL> import gitutils <EOL> import syntaxhighlight <EOL> import syntaxhighlight . request <EOL> from diffutils import expandWithContext <EOL> from htmlutils import htmlify , jsify <EOL> from time import strftime <EOL> import diff <EOL> import diff . analyze <EOL> import diff . parse <EOL> import diff . merge <EOL> import load <EOL> import dbutils <EOL> import client <EOL> def createFullMergeChangeset ( db , user , repository , commit , ** kwargs ) : <EOL> assert len ( commit . parents ) > <NUM_LIT:1> <EOL> changesets = createChangeset ( db , user , repository , commit , ** kwargs ) <EOL> assert len ( changesets ) == len ( commit . parents ) <EOL> replayed = createChangeset ( db , user , repository , commit , conflicts = True , ** kwargs ) <EOL> assert len ( replayed ) == <NUM_LIT:1> <EOL> changesets . append ( replayed [ <NUM_LIT:0> ] ) <EOL> return changesets <EOL> def createChangesets ( db , repository , commits ) : <EOL> cursor = db . cursor ( ) <EOL> requests = [ ] <EOL> for commit in commits : <EOL> if len ( commit . parents ) > <NUM_LIT:1> : changeset_type = '<STR_LIT>' <EOL> else : changeset_type = '<STR_LIT>' <EOL> cursor . execute ( "<STR_LIT>" , ( commit . getId ( db ) , changeset_type ) ) <EOL> if not cursor . fetchone ( ) : <EOL> requests . append ( { "<STR_LIT>" : repository . name , <EOL> "<STR_LIT>" : changeset_type , <EOL> "<STR_LIT>" : commit . sha1 } ) <EOL> if requests : <EOL> client . requestChangesets ( requests ) <EOL> def createChangeset ( db , user , repository , commit = None , from_commit = None , to_commit = None , rescan = False , reanalyze = False , conflicts = False , filtered_file_ids = None , review = None , do_highlight = True , load_chunks = True ) : <EOL> cursor = db . cursor ( ) <EOL> if conflicts : <EOL> if commit : <EOL> assert len ( commit . parents ) > <NUM_LIT:1> <EOL> cursor . execute ( "<STR_LIT>" , ( commit . getId ( db ) , ) ) <EOL> row = cursor . fetchone ( ) <EOL> if row : <EOL> replay = gitutils . Commit . fromId ( db , repository , row [ <NUM_LIT:0> ] ) <EOL> else : <EOL> replay = repository . replaymerge ( db , user , commit ) <EOL> if not replay : return None <EOL> cursor . execute ( "<STR_LIT>" , ( commit . getId ( db ) , replay . getId ( db ) ) ) <EOL> from_commit = replay <EOL> to_commit = commit <EOL> parents = [ replay ] <EOL> else : <EOL> parents = [ from_commit ] <EOL> commit = to_commit <EOL> changeset_type = '<STR_LIT>' <EOL> elif commit : <EOL> parents = [ gitutils . Commit . fromSHA1 ( db , repository , sha1 ) for sha1 in commit . parents ] or [ None ] <EOL> changeset_type = '<STR_LIT>' if len ( parents ) > <NUM_LIT:1> else '<STR_LIT>' <EOL> else : <EOL> parents = [ from_commit ] <EOL> commit = to_commit <EOL> changeset_type = '<STR_LIT>' if len ( to_commit . parents ) == <NUM_LIT:1> and from_commit == to_commit . parents [ <NUM_LIT:0> ] else '<STR_LIT>' <EOL> changesets = [ ] <EOL> thin_diff = False <EOL> changeset_ids = [ ] <EOL> for parent in parents : <EOL> if parent : <EOL> cursor . execute ( "<STR_LIT>" , <EOL> ( parent . getId ( db ) , commit . getId ( db ) , changeset_type ) ) <EOL> else : <EOL> cursor . execute ( "<STR_LIT>" , <EOL> ( commit . getId ( db ) , changeset_type ) ) <EOL> row = cursor . fetchone ( ) <EOL> if row : changeset_ids . append ( row [ <NUM_LIT:0> ] ) <EOL> else : break <EOL> assert len ( changeset_ids ) in ( <NUM_LIT:0> , len ( parents ) ) <EOL> if changeset_ids : <EOL> if rescan and user . hasRole ( db , "<STR_LIT>" ) : <EOL> cursor . executemany ( "<STR_LIT>" , [ ( changeset_id , ) for changeset_id in changeset_ids ] ) <EOL> db . commit ( ) <EOL> changeset_ids = [ ] <EOL> else : <EOL> for changeset_id in changeset_ids : <EOL> if changeset_type == '<STR_LIT>' : <EOL> cursor . execute ( "<STR_LIT>" , ( changeset_id , ) ) <EOL> changeset = load . loadChangeset ( db , repository , changeset_id , filtered_file_ids = filtered_file_ids , load_chunks = load_chunks ) <EOL> changeset . conflicts = conflicts <EOL> if reanalyze and user . hasRole ( db , "<STR_LIT>" ) : <EOL> analysis_values = [ ] <EOL> for file in changeset . files : <EOL> if not filtered_file_ids or file . id in filtered_file_ids : <EOL> for index , chunk in enumerate ( file . chunks ) : <EOL> old_analysis = chunk . analysis <EOL> chunk . analyze ( file , index == len ( file . chunks ) - <NUM_LIT:1> , True ) <EOL> if old_analysis != chunk . analysis : <EOL> analysis_values . append ( ( chunk . analysis , chunk . id ) ) <EOL> if reanalyze == "<STR_LIT>" and analysis_values : <EOL> cursor . executemany ( "<STR_LIT>" , analysis_values ) <EOL> changesets . append ( changeset ) <EOL> if not changesets : <EOL> if len ( parents ) == <NUM_LIT:1> and from_commit and to_commit and filtered_file_ids : <EOL> if from_commit . isAncestorOf ( to_commit ) : <EOL> iter_commit = to_commit <EOL> while iter_commit != from_commit : <EOL> if len ( iter_commit . parents ) > <NUM_LIT:1> : <EOL> thin_diff = True <EOL> break <EOL> iter_commit = gitutils . Commit . fromSHA1 ( db , repository , iter_commit . parents [ <NUM_LIT:0> ] ) <EOL> else : <EOL> thin_diff = True <EOL> if not thin_diff : <EOL> if changeset_type == "<STR_LIT>" : <EOL> request = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : commit . sha1 } <EOL> elif changeset_type == "<STR_LIT>" : <EOL> request = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : from_commit . sha1 if from_commit else "<STR_LIT:0>" * <NUM_LIT> , <EOL> "<STR_LIT>" : to_commit . sha1 } <EOL> elif changeset_type == "<STR_LIT>" : <EOL> request = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : commit . sha1 } <EOL> else : <EOL> request = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : from_commit . sha1 , <EOL> "<STR_LIT>" : to_commit . sha1 } <EOL> request [ "<STR_LIT>" ] = repository . name <EOL> db . commit ( ) <EOL> client . requestChangesets ( [ request ] ) <EOL> db . commit ( ) <EOL> for parent in parents : <EOL> if parent : <EOL> cursor . execute ( "<STR_LIT>" , <EOL> ( parent . getId ( db ) , commit . getId ( db ) , changeset_type ) ) <EOL> else : <EOL> cursor . execute ( "<STR_LIT>" , <EOL> ( commit . getId ( db ) , changeset_type ) ) <EOL> changeset_id = cursor . fetchone ( ) [ <NUM_LIT:0> ] <EOL> changeset = load . loadChangeset ( db , repository , changeset_id , filtered_file_ids = filtered_file_ids , load_chunks = load_chunks ) <EOL> changeset . conflicts = conflicts <EOL> changesets . append ( changeset ) <EOL> else : <EOL> changes = diff . parse . parseDifferences ( repository , from_commit = from_commit , to_commit = to_commit , filter_paths = [ describe_file ( db , file_id ) for file_id in filtered_file_ids ] ) [ from_commit . sha1 ] <EOL> dbutils . find_files ( db , changes ) <EOL> for file in changes : <EOL> for index , chunk in enumerate ( file . chunks ) : <EOL> chunk . analyze ( file , index == len ( file . chunks ) - <NUM_LIT:1> ) <EOL> changeset = diff . Changeset ( None , from_commit , to_commit , changeset_type ) <EOL> changeset . conflicts = conflicts <EOL> changeset . files = diff . File . sorted ( changes ) <EOL> changesets . append ( changeset ) <EOL> if do_highlight : <EOL> highlights = { } <EOL> for changeset in changesets : <EOL> for file in changeset . files : <EOL> if file . canHighlight ( ) : <EOL> if file . old_sha1 and file . old_sha1 != '<STR_LIT:0>' * <NUM_LIT> : <EOL> highlights [ file . old_sha1 ] = ( file . path , file . getLanguage ( ) ) <EOL> if file . new_sha1 and file . new_sha1 != '<STR_LIT:0>' * <NUM_LIT> : <EOL> highlights [ file . new_sha1 ] = ( file . path , file . getLanguage ( ) ) <EOL> syntaxhighlight . request . requestHighlights ( repository , highlights ) <EOL> return changesets <EOL> def getCodeContext ( db , sha1 , line , minimized = False ) : <EOL> cursor = db . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" , [ sha1 , line , line ] ) <EOL> row = cursor . fetchone ( ) <EOL> if row : <EOL> context = row [ <NUM_LIT:0> ] <EOL> if minimized : context = re . sub ( "<STR_LIT>" , "<STR_LIT>" , context ) <EOL> return context <EOL> else : return None </s>
