<s> import dbutils <EOL> import htmlutils <EOL> import page . utils <EOL> import gitutils <EOL> import re <EOL> import log . commitset <EOL> def addNote ( req , db , user ) : <EOL> repository_id = req . getParameter ( "<STR_LIT>" , filter = int ) <EOL> branch = req . getParameter ( "<STR_LIT>" ) <EOL> upstream = req . getParameter ( "<STR_LIT>" ) <EOL> sha1 = req . getParameter ( "<STR_LIT>" ) <EOL> review_id = req . getParameter ( "<STR_LIT>" , None ) <EOL> text = req . read ( ) . strip ( ) <EOL> if review_id is not None : <EOL> review = dbutils . Review . fromId ( db , review_id ) <EOL> else : <EOL> review = None <EOL> cursor = db . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" , <EOL> ( repository_id , branch , upstream , sha1 ) ) <EOL> cursor . execute ( "<STR_LIT>" , <EOL> ( repository_id , branch , upstream , sha1 , user . id , review_id , text or None ) ) <EOL> db . commit ( ) <EOL> response = "<STR_LIT>" <EOL> if review and review . repository . id == repository_id : <EOL> repository = gitutils . Repository . fromId ( db , repository_id ) <EOL> commit = gitutils . Commit . fromSHA1 ( db , repository , sha1 ) <EOL> commitset = log . commitset . CommitSet ( review . branch . getCommits ( db ) ) <EOL> upstreams = commitset . getFilteredTails ( repository ) <EOL> if len ( upstreams ) == <NUM_LIT:1> : <EOL> upstream = upstreams . pop ( ) <EOL> if repository . mergebase ( [ commit . sha1 , upstream ] ) == upstream : <EOL> response = "<STR_LIT>" <EOL> return response <EOL> def deleteNote ( req , db , user ) : <EOL> repository_id = req . getParameter ( "<STR_LIT>" , filter = int ) <EOL> branch = req . getParameter ( "<STR_LIT>" ) <EOL> upstream = req . getParameter ( "<STR_LIT>" ) <EOL> sha1 = req . getParameter ( "<STR_LIT>" ) <EOL> cursor = db . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" , <EOL> ( repository_id , branch , upstream , sha1 ) ) <EOL> db . commit ( ) <EOL> return "<STR_LIT>" <EOL> def renderCheckBranch ( req , db , user ) : <EOL> mode = "<STR_LIT:html>" if req . path == "<STR_LIT>" else "<STR_LIT:text>" <EOL> repository_arg = req . getParameter ( "<STR_LIT>" , None ) <EOL> cursor = db . cursor ( ) <EOL> header_right = [ ] <EOL> if mode == "<STR_LIT:html>" : <EOL> document = htmlutils . Document ( req ) <EOL> html = document . html ( ) <EOL> head = html . head ( ) <EOL> body = html . body ( ) <EOL> def generateRight ( target ) : <EOL> header_right . append ( target . div ( "<STR_LIT>" ) ) <EOL> page . utils . generateHeader ( body , db , user , generate_right = generateRight ) <EOL> document . addExternalStylesheet ( "<STR_LIT>" ) <EOL> document . addExternalScript ( "<STR_LIT>" ) <EOL> document . addInternalScript ( user . getJS ( ) ) <EOL> target = body . div ( "<STR_LIT>" ) <EOL> else : <EOL> result = "<STR_LIT>" <EOL> if repository_arg is not None : <EOL> repository = gitutils . Repository . fromParameter ( db , repository_arg ) <EOL> branch_arg = commit = req . getParameter ( "<STR_LIT>" ) <EOL> fetch = req . getParameter ( "<STR_LIT>" , "<STR_LIT>" ) == "<STR_LIT:yes>" <EOL> upstream_arg = req . getParameter ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if mode == "<STR_LIT:html>" : <EOL> header_right [ <NUM_LIT:0> ] . a ( "<STR_LIT>" , href = "<STR_LIT>" ) . text ( "<STR_LIT>" ) <EOL> header_right [ <NUM_LIT:0> ] . a ( "<STR_LIT>" , href = "<STR_LIT>" % ( repository_arg , branch_arg , upstream_arg ) ) . text ( "<STR_LIT>" ) <EOL> header_right [ <NUM_LIT:0> ] . span ( "<STR_LIT>" ) <EOL> target . addInternalScript ( repository . getJS ( ) ) <EOL> target . addInternalScript ( "<STR_LIT>" % ( branch_arg , upstream_arg ) ) <EOL> upstream = repository . revparse ( upstream_arg ) <EOL> if fetch : <EOL> if commit . startswith ( "<STR_LIT>" ) : <EOL> raise page . utils . DisplayMessage ( "<STR_LIT>" ) <EOL> repository . updateBranchFromRemote ( db , repository . getDefaultRemote ( db ) , commit ) <EOL> try : commit = repository . revparse ( commit ) <EOL> except : raise page . utils . DisplayMessage ( "<STR_LIT>" % commit ) <EOL> try : gitutils . Commit . fromSHA1 ( db , repository , commit ) <EOL> except : raise page . utils . DisplayMessage ( "<STR_LIT>" % commit ) <EOL> if mode == "<STR_LIT:html>" : <EOL> document . setTitle ( "<STR_LIT>" % branch_arg ) <EOL> commits = repository . revlist ( [ commit ] , [ upstream ] , "<STR_LIT>" ) <EOL> if not commits : <EOL> try : merge_sha1 = repository . revlist ( [ upstream ] , [ commit ] , "<STR_LIT>" , "<STR_LIT>" ) [ - <NUM_LIT:1> ] <EOL> except : raise page . utils . DisplayMessage ( "<STR_LIT>" ) <EOL> merge = gitutils . Commit . fromSHA1 ( db , repository , merge_sha1 ) <EOL> if len ( merge . parents ) == <NUM_LIT:1> : <EOL> candidate_merges = repository . revlist ( [ commit ] , [ ] , "<STR_LIT>" , "<STR_LIT>" ) <EOL> for merge_sha1 in candidate_merges : <EOL> merge = gitutils . Commit . fromSHA1 ( db , repository , merge_sha1 ) <EOL> if len ( merge . parents ) > <NUM_LIT:1> : <EOL> use_upstream = merge . parents [ <NUM_LIT:1> ] <EOL> break <EOL> else : <EOL> raise page . utils . DisplayMessage ( "<STR_LIT>" ) <EOL> else : <EOL> assert commit in merge . parents <EOL> use_upstream = None <EOL> for parent in merge . parents : <EOL> if parent != commit : <EOL> use_upstream = parent <EOL> break <EOL> commits = repository . revlist ( [ commit ] , [ use_upstream ] , "<STR_LIT>" ) <EOL> title = "<STR_LIT>" % len ( commits ) <EOL> display_upstream = gitutils . Commit . fromSHA1 ( db , repository , use_upstream ) . describe ( db ) <EOL> else : <EOL> title = "<STR_LIT>" % len ( commits ) <EOL> display_upstream = upstream_arg <EOL> commits = [ gitutils . Commit . fromSHA1 ( db , repository , sha1 ) for sha1 in commits ] <EOL> if mode == "<STR_LIT:html>" : <EOL> table = target . table ( "<STR_LIT>" , align = "<STR_LIT>" , cellspacing = <NUM_LIT:0> ) <EOL> table . col ( width = "<STR_LIT>" ) <EOL> table . col ( width = "<STR_LIT>" ) <EOL> table . col ( width = "<STR_LIT>" ) <EOL> table . col ( width = "<STR_LIT>" ) <EOL> thead = table . thead ( ) <EOL> h1_cell = thead . tr ( ) . td ( '<STR_LIT>' , colspan = <NUM_LIT:4> ) <EOL> h1_cell . h1 ( ) . text ( title ) <EOL> p = h1_cell . p ( ) <EOL> p . text ( "<STR_LIT>" ) <EOL> p . span ( "<STR_LIT>" ) . text ( "<STR_LIT>" % ( branch_arg , display_upstream ) ) <EOL> row = thead . tr ( "<STR_LIT>" ) <EOL> row . th ( "<STR_LIT>" ) . text ( "<STR_LIT>" ) <EOL> row . th ( "<STR_LIT:user>" ) . text ( "<STR_LIT>" ) <EOL> row . th ( "<STR_LIT>" ) . text ( "<STR_LIT>" ) <EOL> row . th ( "<STR_LIT>" ) . text ( "<STR_LIT>" ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( repository . id , branch_arg , upstream_arg ) ) <EOL> notes = { } <EOL> reds = False <EOL> for sha1 , user_id , review_id , text in cursor : <EOL> notes [ sha1 ] = dbutils . User . fromId ( db , user_id ) , review_id , text <EOL> if commits : <EOL> merged = set ( commits ) <EOL> handled = set ( ) <EOL> current_tbody = None <EOL> last_tbody = None <EOL> def nameFromEmail ( email ) : <EOL> offset = email . find ( "<STR_LIT:@>" ) <EOL> if offset != - <NUM_LIT:1> : return email [ : offset ] <EOL> else : return email <EOL> review = None <EOL> reviewed_commits = [ ] <EOL> text_items = { } <EOL> text_order = [ ] <EOL> for commit in commits : <EOL> if commit not in handled : <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( commit . sha1 , ) ) <EOL> if commit not in reviewed_commits : <EOL> review = None <EOL> reviewed = set ( ) <EOL> best = <NUM_LIT:0> <EOL> for ( review_id , ) in cursor : <EOL> candidate_review = dbutils . Review . fromId ( db , review_id ) <EOL> candidate_reviewed = filter ( lambda commit : commit in merged , <EOL> candidate_review . branch . getCommits ( db ) ) <EOL> if len ( candidate_reviewed ) > best : <EOL> review = candidate_review <EOL> reviewed = candidate_reviewed <EOL> best = len ( reviewed ) <EOL> reviewed_commits = filter ( lambda commit : commit in reviewed , commits ) <EOL> if mode == "<STR_LIT:html>" : <EOL> if review : <EOL> current_tbody = None <EOL> review_tbody = table . tbody ( "<STR_LIT>" if review . state == '<STR_LIT>' or review . accepted ( db ) else "<STR_LIT>" ) <EOL> first = True <EOL> review_tbody . tr ( "<STR_LIT>" ) . td ( "<STR_LIT>" , colspan = <NUM_LIT:4> ) <EOL> for reviewed_commit in reviewed_commits : <EOL> handled . add ( reviewed_commit ) <EOL> row = review_tbody . tr ( "<STR_LIT>" ) <EOL> row . td ( "<STR_LIT>" , title = commit . sha1 ) . div ( ) . text ( reviewed_commit . sha1 [ : <NUM_LIT:8> ] ) <EOL> row . td ( "<STR_LIT:user>" ) . text ( nameFromEmail ( reviewed_commit . committer . email ) ) <EOL> row . td ( "<STR_LIT>" ) . a ( href = "<STR_LIT>" % ( reviewed_commit . sha1 , review . id ) ) . text ( reviewed_commit . niceSummary ( ) ) <EOL> if first : <EOL> row . td ( "<STR_LIT>" , rowspan = len ( reviewed ) ) . a ( href = "<STR_LIT>" % review . id ) . text ( "<STR_LIT>" % review . id ) <EOL> first = False <EOL> last_tbody = review_tbody <EOL> elif commit . sha1 in notes : <EOL> note_user , review_id , text = notes [ commit . sha1 ] <EOL> try : review = dbutils . Review . fromId ( db , review_id ) if review_id else None <EOL> except : review = None <EOL> current_tbody = None <EOL> note_tbody = table . tbody ( "<STR_LIT>" if ( not review_id or ( review and ( review . state == '<STR_LIT>' or review . accepted ( db ) ) ) ) else "<STR_LIT>" ) <EOL> note_tbody . tr ( "<STR_LIT>" ) . td ( "<STR_LIT>" , colspan = <NUM_LIT:4> ) <EOL> row = note_tbody . tr ( "<STR_LIT>" , id = commit . sha1 ) <EOL> row . td ( "<STR_LIT>" , title = commit . sha1 ) . div ( ) . text ( commit . sha1 [ : <NUM_LIT:8> ] ) <EOL> row . td ( "<STR_LIT:user>" ) . text ( nameFromEmail ( commit . committer . email ) ) <EOL> row . td ( "<STR_LIT>" ) . a ( href = "<STR_LIT>" % ( commit . sha1 , repository . id ) ) . text ( commit . niceSummary ( ) ) <EOL> cell = row . td ( "<STR_LIT>" ) <EOL> if review_id is None : cell . text ( ) <EOL> else : cell . a ( href = "<STR_LIT>" % review_id ) . text ( "<STR_LIT>" % review_id ) <EOL> row = note_tbody . tr ( "<STR_LIT>" ) <EOL> row . td ( "<STR_LIT>" ) . text ( ) <EOL> cell = row . td ( "<STR_LIT>" , colspan = <NUM_LIT:2> ) <EOL> cell . text ( "<STR_LIT>" ) <EOL> cell . span ( "<STR_LIT:user>" ) . text ( note_user . fullname ) <EOL> if text is not None : <EOL> cell . text ( "<STR_LIT>" ) <EOL> cell . span ( "<STR_LIT:text>" ) . text ( text ) <EOL> row . td ( "<STR_LIT>" ) . a ( "<STR_LIT>" , href = "<STR_LIT>" % ( commit . sha1 , commit . getId ( db ) , ( "<STR_LIT>" % review_id ) if review_id is not None else "<STR_LIT>" ) ) . text ( "<STR_LIT>" ) <EOL> last_tbody = note_tbody <EOL> else : <EOL> handled . add ( commit ) <EOL> if not current_tbody : <EOL> current_tbody = table . tbody ( "<STR_LIT>" ) <EOL> current_tbody . tr ( "<STR_LIT>" ) . td ( "<STR_LIT>" , colspan = <NUM_LIT:4> ) <EOL> last_tbody = current_tbody <EOL> row = current_tbody . tr ( "<STR_LIT>" % ( "<STR_LIT>" if commit . author . email == user . email else "<STR_LIT>" ) , id = commit . sha1 ) <EOL> row . td ( "<STR_LIT>" , title = commit . sha1 ) . div ( ) . text ( commit . sha1 [ : <NUM_LIT:8> ] ) <EOL> row . td ( "<STR_LIT:user>" ) . text ( nameFromEmail ( commit . committer . email ) ) <EOL> row . td ( "<STR_LIT>" ) . a ( href = "<STR_LIT>" % ( repository . name , commit . sha1 ) ) . text ( commit . niceSummary ( ) ) <EOL> row . td ( "<STR_LIT>" ) . a ( "<STR_LIT>" , href = "<STR_LIT>" % ( commit . sha1 , commit . getId ( db ) ) ) . text ( "<STR_LIT>" ) <EOL> reds = True <EOL> else : <EOL> match = re . search ( "<STR_LIT>" , commit . summary ( ) ) <EOL> if match : <EOL> title = match . group ( <NUM_LIT:0> ) <EOL> else : <EOL> title = commit . summary ( maxlen = <NUM_LIT:50> ) <EOL> if title . endswith ( "<STR_LIT:.>" ) and not title . endswith ( "<STR_LIT>" ) : <EOL> title = title [ : - <NUM_LIT:1> ] <EOL> if commit . sha1 in notes : <EOL> note_user , review_id , text = notes [ commit . sha1 ] <EOL> if review_id : review = dbutils . Review . fromId ( db , review_id ) <EOL> else : <EOL> text = None <EOL> if review : <EOL> item = review . getURL ( db ) <EOL> if review . state != "<STR_LIT>" and not review . accepted ( db ) : <EOL> item += "<STR_LIT>" <EOL> if text : <EOL> item += "<STR_LIT>" % ( note_user . fullname , text ) <EOL> elif text : <EOL> item = "<STR_LIT>" % ( note_user . fullname , text ) <EOL> else : <EOL> item = "<STR_LIT>" <EOL> if title in text_items : <EOL> if item not in text_items [ title ] : <EOL> text_items [ title ] . append ( item ) <EOL> else : <EOL> text_items [ title ] = [ item ] <EOL> text_order . append ( title ) <EOL> if mode == "<STR_LIT:html>" : <EOL> last_tbody . tr ( "<STR_LIT>" ) . td ( "<STR_LIT>" , colspan = <NUM_LIT:4> ) <EOL> else : <EOL> for title in reversed ( text_order ) : <EOL> result += "<STR_LIT>" % ( title , "<STR_LIT:U+002CU+0020>" . join ( text_items [ title ] ) ) <EOL> if mode == "<STR_LIT:html>" : <EOL> if reds : <EOL> h1_cell . h2 ( ) . text ( "<STR_LIT>" ) <EOL> legend = target . div ( "<STR_LIT>" ) <EOL> legend . text ( "<STR_LIT>" ) <EOL> legend . span ( "<STR_LIT>" ) . text ( "<STR_LIT>" ) <EOL> legend . text ( "<STR_LIT:U+0020>" ) <EOL> legend . span ( "<STR_LIT>" ) . text ( "<STR_LIT>" ) <EOL> legend . text ( "<STR_LIT:U+0020>" ) <EOL> legend . span ( "<STR_LIT>" ) . text ( "<STR_LIT>" ) <EOL> return document <EOL> else : <EOL> return result <EOL> else : <EOL> header_right [ <NUM_LIT:0> ] . a ( "<STR_LIT>" , href = "<STR_LIT>" ) . text ( "<STR_LIT>" ) <EOL> table = page . utils . PaleYellowTable ( target , "<STR_LIT>" ) <EOL> def renderRepository ( target ) : <EOL> page . utils . generateRepositorySelect ( db , user , target , name = "<STR_LIT>" ) <EOL> def renderBranchName ( target ) : <EOL> target . input ( name = "<STR_LIT>" ) <EOL> def renderFetch ( target ) : <EOL> target . input ( name = "<STR_LIT>" , type = "<STR_LIT>" , value = "<STR_LIT:yes>" ) <EOL> def renderUpstream ( target ) : <EOL> target . input ( name = "<STR_LIT>" , value = "<STR_LIT>" ) <EOL> table . addItem ( "<STR_LIT>" , renderRepository , description = "<STR_LIT>" ) <EOL> table . addItem ( "<STR_LIT>" , renderBranchName , <EOL> description = "<STR_LIT>" ) <EOL> table . addItem ( "<STR_LIT>" , renderFetch , <EOL> description = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> table . addItem ( "<STR_LIT>" , renderUpstream , <EOL> description = "<STR_LIT>" ) <EOL> def renderCheck ( target ) : <EOL> target . button ( "<STR_LIT>" ) . text ( "<STR_LIT>" ) <EOL> table . addCentered ( renderCheck ) <EOL> return document </s>
