<s> """<STR_LIT>""" <EOL> import qisrc . git <EOL> from qisys import ui <EOL> def stat_tracking_remote ( git , branch , tracking ) : <EOL> """<STR_LIT>""" <EOL> if branch is None or tracking is None : <EOL> return <NUM_LIT:0> , <NUM_LIT:0> <EOL> _ , local_ref = git . call ( "<STR_LIT>" , branch , raises = False ) <EOL> _ , remote_ref = git . call ( "<STR_LIT>" , tracking , raises = False ) <EOL> return stat_ahead_behind ( git , local_ref , remote_ref ) <EOL> def stat_fixed_ref ( git , remote_ref ) : <EOL> """<STR_LIT>""" <EOL> _ , local_ref = git . call ( "<STR_LIT>" , "<STR_LIT>" , raises = False ) <EOL> return stat_ahead_behind ( git , local_ref , remote_ref ) <EOL> def stat_ahead_behind ( git , local_ref , remote_ref ) : <EOL> """<STR_LIT>""" <EOL> behind = <NUM_LIT:0> <EOL> ahead = <NUM_LIT:0> <EOL> ( ret , out ) = git . call ( "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" % ( remote_ref , local_ref ) , raises = False ) <EOL> if ret == <NUM_LIT:0> : <EOL> ahead = len ( out . split ( ) ) <EOL> ( ret , out ) = git . call ( "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" % ( local_ref , remote_ref ) , raises = False ) <EOL> if ret == <NUM_LIT:0> : <EOL> behind = len ( out . split ( ) ) <EOL> return ( ahead , behind ) <EOL> class ProjectState ( ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , project ) : <EOL> self . project = project <EOL> self . fixed_ref = None <EOL> self . not_on_a_branch = False <EOL> self . incorrect_proj = False <EOL> self . clean = True <EOL> self . ahead = <NUM_LIT:0> <EOL> self . behind = <NUM_LIT:0> <EOL> self . ahead_manifest = <NUM_LIT:0> <EOL> self . behind_manifest = <NUM_LIT:0> <EOL> self . current_branch = None <EOL> self . tracking = None <EOL> self . valid = True <EOL> self . status = None <EOL> self . manifest_branch = None <EOL> @ property <EOL> def sync ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . ahead == <NUM_LIT:0> and self . behind == <NUM_LIT:0> <EOL> @ property <EOL> def sync_and_clean ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . clean and self . sync <EOL> def check_state ( project , untracked ) : <EOL> """<STR_LIT>""" <EOL> state_project = ProjectState ( project ) <EOL> git = qisrc . git . Git ( project . path ) <EOL> if not git . is_valid ( ) : <EOL> state_project . valid = False <EOL> return state_project <EOL> state_project . clean = git . is_clean ( untracked = untracked ) <EOL> if project . fixed_ref : <EOL> state_project . ahead , state_project . behind = stat_fixed_ref ( git , project . fixed_ref ) <EOL> state_project . fixed_ref = project . fixed_ref <EOL> _set_status ( git , state_project , untracked = untracked ) <EOL> return state_project <EOL> state_project . current_branch = git . get_current_branch ( ) <EOL> state_project . tracking = git . get_tracking_branch ( ) <EOL> if project . default_remote and project . default_branch : <EOL> state_project . manifest_branch = "<STR_LIT>" % ( project . default_remote . name , project . default_branch . name ) <EOL> if state_project . current_branch is None : <EOL> state_project . not_on_a_branch = True <EOL> return state_project <EOL> if project . default_branch : <EOL> if state_project . current_branch != project . default_branch . name : <EOL> state_project . incorrect_proj = True <EOL> ( state_project . ahead , state_project . behind ) = stat_tracking_remote ( git , <EOL> state_project . current_branch , state_project . tracking ) <EOL> if state_project . incorrect_proj : <EOL> ( state_project . ahead_manifest , state_project . behind_manifest ) = stat_tracking_remote ( <EOL> git , state_project . current_branch , "<STR_LIT>" % ( <EOL> project . default_remote . name , project . default_branch . name ) ) <EOL> _set_status ( git , state_project , untracked = untracked ) <EOL> return state_project <EOL> def _set_status ( git , state_project , untracked = False ) : <EOL> """<STR_LIT>""" <EOL> if not state_project . sync_and_clean : <EOL> out = git . get_status ( untracked ) <EOL> if out is not None : <EOL> state_project . status = [ x for x in out . splitlines ( ) if <EOL> len ( x . strip ( ) ) > <NUM_LIT:0> ] <EOL> def print_behind_ahead ( behind , ahead ) : <EOL> numcommits = "<STR_LIT>" <EOL> if behind : <EOL> numcommits += "<STR_LIT:->" + str ( behind ) <EOL> if behind and ahead : <EOL> numcommits += "<STR_LIT:/>" <EOL> if ahead : <EOL> numcommits += "<STR_LIT:+>" + str ( ahead ) <EOL> return numcommits <EOL> def print_state ( project , max_len ) : <EOL> """<STR_LIT>""" <EOL> if project . valid : <EOL> if project . ahead or project . behind : <EOL> numcommits = print_behind_ahead ( project . behind , project . ahead ) <EOL> if project . fixed_ref : <EOL> ui . info ( ui . green , "<STR_LIT:*>" , ui . reset , <EOL> ui . blue , project . project . src . ljust ( max_len ) , ui . reset , <EOL> ui . green , "<STR_LIT>" , project . fixed_ref , <EOL> ui . reset , ui . bold , ui . red , <EOL> numcommits ) <EOL> else : <EOL> ui . info ( ui . green , "<STR_LIT:*>" , ui . reset , <EOL> ui . blue , project . project . src . ljust ( max_len ) , ui . reset , <EOL> ui . green , "<STR_LIT::>" , project . current_branch , <EOL> "<STR_LIT>" , <EOL> ui . reset , ui . bold , ui . red , <EOL> numcommits , ui . green , project . tracking ) <EOL> else : <EOL> if project . fixed_ref : <EOL> ui . info ( ui . green , "<STR_LIT:*>" , ui . reset , <EOL> ui . blue , project . project . src . ljust ( max_len ) , ui . reset , <EOL> ui . green , "<STR_LIT>" , project . fixed_ref ) <EOL> else : <EOL> ui . info ( ui . green , "<STR_LIT:*>" , ui . reset , <EOL> ui . blue , project . project . src . ljust ( max_len ) , ui . reset , <EOL> ui . green , "<STR_LIT::>" , project . current_branch , <EOL> "<STR_LIT>" , project . tracking ) <EOL> if project . ahead_manifest or project . behind_manifest : <EOL> numcommits = print_behind_ahead ( project . behind_manifest , project . ahead_manifest ) <EOL> ui . info ( ui . bold , "<STR_LIT>" , ui . green , project . current_branch , <EOL> ui . reset , ui . bold , "<STR_LIT>" , numcommits , "<STR_LIT>" , <EOL> ui . blue , project . manifest_branch ) <EOL> if not project . sync_and_clean : <EOL> if project . status is not None : <EOL> nlines = [ x [ : <NUM_LIT:3> ] + project . project . src + "<STR_LIT:/>" <EOL> + x [ <NUM_LIT:3> : ] for x in project . status ] <EOL> if nlines : <EOL> print "<STR_LIT:\n>" . join ( nlines ) <EOL> def print_incorrect_projs ( projects , max_len ) : <EOL> """<STR_LIT>""" <EOL> incorrect_projs = [ x for x in projects if x . incorrect_proj ] <EOL> if incorrect_projs : <EOL> ui . info ( ) <EOL> max_branch_len = max ( len ( x . current_branch ) for x in incorrect_projs ) <EOL> max_branch_len = max ( max_branch_len , len ( "<STR_LIT>" ) ) <EOL> ui . warning ( "<STR_LIT>" ) <EOL> ui . info ( ui . blue , "<STR_LIT:U+0020>" * <NUM_LIT:2> , "<STR_LIT>" . ljust ( max_len + <NUM_LIT:3> ) , ui . reset , <EOL> ui . green , "<STR_LIT>" . ljust ( max_branch_len + <NUM_LIT:3> ) , <EOL> "<STR_LIT>" ) <EOL> for project in incorrect_projs : <EOL> ui . info ( ui . green , "<STR_LIT>" , ui . reset , <EOL> ui . blue , project . project . src . ljust ( max_len + <NUM_LIT:3> ) , <EOL> ui . green , project . current_branch . ljust ( max_branch_len + <NUM_LIT:3> ) , <EOL> ui . green , project . project . default_branch . name ) <EOL> def print_not_on_a_branch ( projects ) : <EOL> """<STR_LIT>""" <EOL> not_on_a_branchs = [ x for x in projects if x . not_on_a_branch ] <EOL> if not_on_a_branchs : <EOL> ui . info ( ) <EOL> ui . warning ( "<STR_LIT>" ) <EOL> for project in not_on_a_branchs : <EOL> ui . info ( ui . green , "<STR_LIT>" , ui . reset , ui . blue , project . project . src ) </s>
