<s> from qisys import ui <EOL> import qisys . error <EOL> import qisys . interact <EOL> import qisrc . manifest <EOL> def rebase_worktree ( git_worktree , git_projects , branch = None , <EOL> push = False , dry_run = False ) : <EOL> if not git_projects : <EOL> return <EOL> upstream_projects = git_worktree . get_projects_on_branch ( branch ) <EOL> to_push , errors = rebase_projects ( git_projects , upstream_projects , branch ) <EOL> if errors : <EOL> mess = "<STR_LIT>" <EOL> for git_project in errors : <EOL> mess += "<STR_LIT>" + git_project . src <EOL> mess += "<STR_LIT:\n>" <EOL> raise qisys . error . Error ( mess ) <EOL> if push : <EOL> push_projects ( to_push , dry_run = dry_run ) <EOL> def push_projects ( git_projects , dry_run = False ) : <EOL> if not git_projects : <EOL> ui . info ( ui . green , "<STR_LIT>" ) <EOL> return <EOL> ui . info ( ui . green , "<STR_LIT>" , len ( git_projects ) , "<STR_LIT>" ) <EOL> for i , git_project in enumerate ( git_projects ) : <EOL> default_branch = git_project . default_branch . name <EOL> remote_branch = git_project . default_branch . remote_branch <EOL> ui . info_count ( i , len ( git_projects ) , git_project . src ) <EOL> git = qisrc . git . Git ( git_project . path ) <EOL> if git_project . review : <EOL> push_remote = git_project . review_remote <EOL> else : <EOL> push_remote = git_project . default_remote <EOL> remote_ref = "<STR_LIT>" % ( push_remote . name , remote_branch ) <EOL> to_push = "<STR_LIT>" % ( default_branch , remote_branch ) <EOL> push_args = [ push_remote . name , to_push ] <EOL> push_args . append ( "<STR_LIT>" ) <EOL> if dry_run : <EOL> push_args . append ( "<STR_LIT>" ) <EOL> rc , out = git . push ( * push_args , raises = False ) <EOL> if rc == <NUM_LIT:0> : <EOL> ui . info ( out ) <EOL> else : <EOL> ui . error ( out ) <EOL> def rebase_projects ( git_projects , upstream_projects , branch ) : <EOL> """<STR_LIT>""" <EOL> ui . info ( ui . green , "<STR_LIT>" ) <EOL> forked_projects = get_forked_projects ( git_projects , upstream_projects , branch ) <EOL> if not forked_projects : <EOL> ui . info ( ui . green , "<STR_LIT>" ) <EOL> return list ( ) , list ( ) <EOL> ui . info ( ui . green , "<STR_LIT>" ) <EOL> errors = list ( ) <EOL> to_push = list ( ) <EOL> max_src = max ( len ( x . src ) for x in forked_projects ) <EOL> for i , git_project in enumerate ( forked_projects ) : <EOL> ui . info_count ( i , len ( forked_projects ) , <EOL> git_project . src . ljust ( max_src + <NUM_LIT:2> ) , end = "<STR_LIT>" ) <EOL> upstream_project = upstream_projects [ git_project . src ] <EOL> ok , rebased = rebase_project ( git_project , upstream_project ) <EOL> if not ok : <EOL> errors . append ( git_project ) <EOL> if rebased : <EOL> to_push . append ( git_project ) <EOL> return to_push , errors <EOL> def get_forked_projects ( git_projects , upstream_projects , branch ) : <EOL> res = list ( ) <EOL> for git_project in git_projects : <EOL> if not git_project . default_remote : <EOL> continue <EOL> if not git_project . default_branch : <EOL> continue <EOL> local_branch = git_project . default_branch . name <EOL> remote_branch = git_project . default_branch . remote_branch <EOL> remote_name = git_project . default_remote . name <EOL> remote_ref = "<STR_LIT>" % ( remote_name , remote_branch ) <EOL> if not git_project . src in upstream_projects : <EOL> ui . info ( ui . brown , git_project . src , "<STR_LIT>" ) <EOL> ui . info ( "<STR_LIT>" % ( git_project . src , branch ) ) <EOL> continue <EOL> upstream_project = upstream_projects [ git_project . src ] <EOL> upstream_branch = upstream_project . default_branch . name <EOL> upstream_ref = "<STR_LIT>" % ( upstream_project . default_remote . name , upstream_branch ) <EOL> if remote_ref != upstream_ref : <EOL> res . append ( git_project ) <EOL> return res <EOL> def rebase_project ( git_project , upstream_project ) : <EOL> """<STR_LIT>""" <EOL> ok = check_local_branch ( git_project ) <EOL> if not ok : <EOL> return False , False <EOL> git = qisrc . git . Git ( git_project . path ) <EOL> local_branch = git_project . default_branch . name <EOL> upstream_branch = upstream_project . default_branch . name <EOL> upstream_ref = "<STR_LIT>" % ( upstream_project . default_remote . name , upstream_branch ) <EOL> status = qisrc . git . get_status ( git , local_branch , upstream_ref ) <EOL> if status == "<STR_LIT>" : <EOL> ui . info ( ui . green , "<STR_LIT>" , ui . reset , "<STR_LIT>" ) <EOL> return True , False <EOL> if status == "<STR_LIT>" : <EOL> ui . info ( ui . green , "<STR_LIT>" , ui . reset , "<STR_LIT>" ) <EOL> return True , False <EOL> if status == "<STR_LIT>" : <EOL> rc , out = git . merge ( upstream_ref , raises = False ) <EOL> if rc != <NUM_LIT:0> : <EOL> ui . info ( ui . red , "<STR_LIT>" ) <EOL> ui . info ( ui . red , "<STR_LIT>" + out ) <EOL> return False , False <EOL> ui . info ( ui . green , "<STR_LIT>" , ui . reset , "<STR_LIT>" ) <EOL> return True , True <EOL> git . call ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , raises = False ) <EOL> rc , out = git . call ( "<STR_LIT>" , upstream_ref , raises = False ) <EOL> if rc == <NUM_LIT:0> : <EOL> ui . info ( ui . green , "<STR_LIT>" , ui . reset , "<STR_LIT>" ) <EOL> return True , True <EOL> else : <EOL> ui . info ( ui . red , "<STR_LIT>" , ui . reset , "<STR_LIT>" ) <EOL> git . call ( "<STR_LIT>" , "<STR_LIT>" , raises = False ) <EOL> git . call ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , raises = False ) <EOL> return False , False <EOL> def check_local_branch ( git_project ) : <EOL> git = qisrc . git . Git ( git_project . path ) <EOL> rc , out = git . fetch ( raises = False ) <EOL> if rc != <NUM_LIT:0> : <EOL> ui . info ( ui . red , "<STR_LIT>" ) <EOL> ui . info ( ui . red , "<STR_LIT>" + out ) <EOL> return False <EOL> current_branch = git . get_current_branch ( ) <EOL> local_branch = git_project . default_branch . name <EOL> if current_branch != local_branch : <EOL> ui . info ( ui . brown , "<STR_LIT>" , end = "<STR_LIT>" ) <EOL> ui . info ( "<STR_LIT>" % ( current_branch , local_branch ) ) <EOL> return False <EOL> return True </s>
