<s> from __future__ import print_function <EOL> import copy <EOL> import re <EOL> import sys <EOL> from command import InteractiveCommand <EOL> from editor import Editor <EOL> from error import GitError , HookError , UploadError <EOL> from git_command import GitCommand <EOL> from project import RepoHook <EOL> from git_refs import GitRefs , HEAD , R_HEADS , R_TAGS , R_PUB , R_M <EOL> from pyversion import is_python3 <EOL> if not is_python3 ( ) : <EOL> input = raw_input <EOL> else : <EOL> unicode = str <EOL> UNUSUAL_COMMIT_THRESHOLD = <NUM_LIT:5> <EOL> def _ConfirmManyPushs ( multiple_branches = False ) : <EOL> if multiple_branches : <EOL> print ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> answer = input ( "<STR_LIT>" ) . strip ( ) <EOL> return answer == "<STR_LIT:yes>" <EOL> def _die ( fmt , * args ) : <EOL> msg = fmt % args <EOL> print ( '<STR_LIT>' % msg , file = sys . stderr ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> class Push ( InteractiveCommand ) : <EOL> common = True <EOL> helpSummary = "<STR_LIT>" <EOL> helpUsage = """<STR_LIT>""" <EOL> helpDescription = """<STR_LIT>""" <EOL> def _Options ( self , p ) : <EOL> p . add_option ( '<STR_LIT>' , <EOL> type = '<STR_LIT:string>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> p . add_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> p . add_option ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> type = '<STR_LIT:string>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> p . add_option ( '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> p . add_option ( '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> def _SingleBranch ( self , opt , branch ) : <EOL> project = branch . project <EOL> name = branch . name <EOL> remote = project . GetBranch ( name ) . remote <EOL> date = branch . date <EOL> commit_list = branch . commits <EOL> destination = opt . dest_branch or project . dest_branch or project . revisionExpr <EOL> print ( '<STR_LIT>' % ( project . relpath , destination ) ) <EOL> print ( '<STR_LIT>' % ( <EOL> name , <EOL> len ( commit_list ) , <EOL> len ( commit_list ) != <NUM_LIT:1> and '<STR_LIT:s>' or '<STR_LIT>' , <EOL> date ) ) <EOL> for commit in commit_list : <EOL> print ( '<STR_LIT>' % commit ) <EOL> sys . stdout . write ( '<STR_LIT>' % remote . name ) <EOL> answer = sys . stdin . readline ( ) . strip ( ) . lower ( ) <EOL> answer = answer in ( '<STR_LIT:y>' , '<STR_LIT:yes>' , '<STR_LIT:1>' , '<STR_LIT:true>' , '<STR_LIT:t>' ) <EOL> if answer : <EOL> if len ( branch . commits ) > UNUSUAL_COMMIT_THRESHOLD : <EOL> answer = _ConfirmManyPushs ( ) <EOL> if answer : <EOL> self . _Push ( opt , [ branch ] ) <EOL> else : <EOL> _die ( "<STR_LIT>" ) <EOL> def _MultipleBranches ( self , opt , pending ) : <EOL> projects = { } <EOL> branches = { } <EOL> script = [ ] <EOL> script . append ( '<STR_LIT>' ) <EOL> for project , avail in pending : <EOL> script . append ( '<STR_LIT:#>' ) <EOL> script . append ( '<STR_LIT>' % project . relpath ) <EOL> b = { } <EOL> for branch in avail : <EOL> if branch is None : <EOL> continue <EOL> name = branch . name <EOL> date = branch . date <EOL> commit_list = branch . commits <EOL> if b : <EOL> script . append ( '<STR_LIT:#>' ) <EOL> destination = opt . dest_branch or project . dest_branch or project . revisionExpr <EOL> script . append ( '<STR_LIT>' % ( <EOL> name , <EOL> len ( commit_list ) , <EOL> len ( commit_list ) != <NUM_LIT:1> and '<STR_LIT:s>' or '<STR_LIT>' , <EOL> date , <EOL> destination ) ) <EOL> for commit in commit_list : <EOL> script . append ( '<STR_LIT>' % commit ) <EOL> b [ name ] = branch <EOL> projects [ project . relpath ] = project <EOL> branches [ project . name ] = b <EOL> script . append ( '<STR_LIT>' ) <EOL> script = [ x . encode ( '<STR_LIT:utf-8>' ) <EOL> if issubclass ( type ( x ) , unicode ) <EOL> else x <EOL> for x in script ] <EOL> script = Editor . EditString ( "<STR_LIT:\n>" . join ( script ) ) . split ( "<STR_LIT:\n>" ) <EOL> project_re = re . compile ( r'<STR_LIT>' ) <EOL> branch_re = re . compile ( r'<STR_LIT>' ) <EOL> project = None <EOL> todo = [ ] <EOL> for line in script : <EOL> m = project_re . match ( line ) <EOL> if m : <EOL> name = m . group ( <NUM_LIT:1> ) <EOL> project = projects . get ( name ) <EOL> if not project : <EOL> _die ( '<STR_LIT>' , name ) <EOL> continue <EOL> m = branch_re . match ( line ) <EOL> if m : <EOL> name = m . group ( <NUM_LIT:1> ) <EOL> if not project : <EOL> _die ( '<STR_LIT>' , name ) <EOL> branch = branches [ project . name ] . get ( name ) <EOL> if not branch : <EOL> _die ( '<STR_LIT>' , name , project . relpath ) <EOL> todo . append ( branch ) <EOL> if not todo : <EOL> _die ( "<STR_LIT>" ) <EOL> many_commits = False <EOL> for branch in todo : <EOL> if len ( branch . commits ) > UNUSUAL_COMMIT_THRESHOLD : <EOL> many_commits = True <EOL> break <EOL> if many_commits : <EOL> if not _ConfirmManyPushs ( multiple_branches = True ) : <EOL> _die ( "<STR_LIT>" ) <EOL> self . _Push ( opt , todo ) <EOL> def _Push ( self , opt , todo ) : <EOL> have_errors = False <EOL> for branch in todo : <EOL> try : <EOL> changes = branch . project . UncommitedFiles ( ) <EOL> if changes : <EOL> sys . stdout . write ( '<STR_LIT>' + branch . project . name ) <EOL> sys . stdout . write ( '<STR_LIT>' ) <EOL> sys . stdout . write ( '<STR_LIT:\n>' . join ( changes ) + '<STR_LIT:\n>' ) <EOL> sys . stdout . write ( '<STR_LIT>' ) <EOL> a = sys . stdin . readline ( ) . strip ( ) . lower ( ) <EOL> if a not in ( '<STR_LIT:y>' , '<STR_LIT:yes>' , '<STR_LIT:t>' , '<STR_LIT:true>' , '<STR_LIT>' ) : <EOL> print ( "<STR_LIT>" , file = sys . stderr ) <EOL> branch . uploaded = False <EOL> branch . error = '<STR_LIT>' <EOL> continue <EOL> destination = opt . dest_branch or branch . project . dest_branch <EOL> merge_branch = self . _GetMergeBranch ( branch . project ) <EOL> if destination : <EOL> full_dest = '<STR_LIT>' % destination <EOL> if not opt . dest_branch and merge_branch and merge_branch != full_dest : <EOL> print ( '<STR_LIT>' <EOL> % ( merge_branch , full_dest ) ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' <EOL> % destination ) <EOL> branch . uploaded = False <EOL> continue <EOL> self . Push ( branch , dest_branch = destination ) <EOL> branch . uploaded = True <EOL> except UploadError as e : <EOL> branch . error = e <EOL> branch . uploaded = False <EOL> have_errors = True <EOL> print ( file = sys . stderr ) <EOL> print ( '<STR_LIT>' , file = sys . stderr ) <EOL> if have_errors : <EOL> for branch in todo : <EOL> if not branch . uploaded : <EOL> if len ( str ( branch . error ) ) <= <NUM_LIT:30> : <EOL> fmt = '<STR_LIT>' <EOL> else : <EOL> fmt = '<STR_LIT>' <EOL> print ( ( '<STR_LIT>' + fmt ) % ( <EOL> branch . project . relpath + '<STR_LIT:/>' , branch . name , str ( branch . error ) ) , <EOL> file = sys . stderr ) <EOL> print ( ) <EOL> for branch in todo : <EOL> if branch . uploaded : <EOL> print ( '<STR_LIT>' % ( <EOL> branch . project . relpath + '<STR_LIT:/>' , <EOL> branch . name ) , <EOL> file = sys . stderr ) <EOL> if have_errors : <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def Push ( self , branch_base , branch = None , <EOL> dest_branch = None ) : <EOL> """<STR_LIT>""" <EOL> project = branch_base . project <EOL> if branch is None : <EOL> branch = project . CurrentBranch <EOL> if branch is None : <EOL> raise GitError ( '<STR_LIT>' ) <EOL> branch = project . GetBranch ( branch ) <EOL> if not branch . LocalMerge : <EOL> raise GitError ( '<STR_LIT>' % branch . name ) <EOL> if dest_branch is None : <EOL> dest_branch = project . dest_branch <EOL> if dest_branch is None : <EOL> dest_branch = branch . merge <EOL> if not dest_branch . startswith ( R_HEADS ) : <EOL> dest_branch = R_HEADS + dest_branch <EOL> if not branch . remote . projectname : <EOL> branch . remote . projectname = project . name <EOL> branch . remote . Save ( ) <EOL> remote = branch . remote . name <EOL> cmd = [ '<STR_LIT>' ] <EOL> cmd . append ( remote ) <EOL> if dest_branch . startswith ( R_HEADS ) : <EOL> dest_branch = dest_branch [ len ( R_HEADS ) : ] <EOL> push_type = '<STR_LIT>' <EOL> ref_spec = '<STR_LIT>' % ( R_HEADS + branch . name , push_type , <EOL> dest_branch ) <EOL> cmd . append ( ref_spec ) <EOL> if GitCommand ( project , cmd , bare = True ) . Wait ( ) != <NUM_LIT:0> : <EOL> raise UploadError ( '<STR_LIT>' ) <EOL> def _GetMergeBranch ( self , project ) : <EOL> p = GitCommand ( project , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> capture_stdout = True , <EOL> capture_stderr = True ) <EOL> p . Wait ( ) <EOL> local_branch = p . stdout . strip ( ) <EOL> p = GitCommand ( project , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' % local_branch ] , <EOL> capture_stdout = True , <EOL> capture_stderr = True ) <EOL> p . Wait ( ) <EOL> merge_branch = p . stdout . strip ( ) <EOL> return merge_branch <EOL> def Execute ( self , opt , args ) : <EOL> self . opt = opt <EOL> project_list = self . GetProjects ( args ) <EOL> pending = [ ] <EOL> branch = None <EOL> if opt . branch : <EOL> branch = opt . branch <EOL> for project in project_list : <EOL> if opt . current_branch : <EOL> cbr = project . CurrentBranch <EOL> up_branch = project . GetUploadableBranch ( cbr ) <EOL> if up_branch : <EOL> avail = [ up_branch ] <EOL> else : <EOL> avail = None <EOL> print ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % str ( cbr ) , <EOL> file = sys . stderr ) <EOL> else : <EOL> avail = project . GetUploadableBranches ( branch ) <EOL> if avail : <EOL> pending . append ( ( project , avail ) ) <EOL> if pending and ( not opt . bypass_hooks ) : <EOL> hook = RepoHook ( '<STR_LIT>' , self . manifest . repo_hooks_project , <EOL> self . manifest . topdir , abort_if_user_denies = True ) <EOL> pending_proj_names = [ project . name for ( project , avail ) in pending ] <EOL> pending_worktrees = [ project . worktree for ( project , avail ) in pending ] <EOL> try : <EOL> hook . Run ( opt . allow_all_hooks , project_list = pending_proj_names , <EOL> worktree_list = pending_worktrees ) <EOL> except HookError as e : <EOL> print ( "<STR_LIT>" % str ( e ) , file = sys . stderr ) <EOL> return <EOL> if not pending : <EOL> print ( "<STR_LIT>" , file = sys . stderr ) <EOL> elif len ( pending ) == <NUM_LIT:1> and len ( pending [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) == <NUM_LIT:1> : <EOL> self . _SingleBranch ( opt , pending [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> else : <EOL> self . _MultipleBranches ( opt , pending ) </s>
