<s> from __future__ import print_function <EOL> import os <EOL> from colorama import Fore , Style <EOL> from git import RemoteReference as RemoteRef , Repo , exc <EOL> from git . util import RemoteProgress <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> BOLD = Style . BRIGHT <EOL> BLUE = Fore . BLUE + BOLD <EOL> GREEN = Fore . GREEN + BOLD <EOL> RED = Fore . RED + BOLD <EOL> YELLOW = Fore . YELLOW + BOLD <EOL> RESET = Style . RESET_ALL <EOL> INDENT1 = "<STR_LIT:U+0020>" * <NUM_LIT:3> <EOL> INDENT2 = "<STR_LIT:U+0020>" * <NUM_LIT:7> <EOL> ERROR = RED + "<STR_LIT>" + RESET <EOL> class _ProgressMonitor ( RemoteProgress ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( _ProgressMonitor , self ) . __init__ ( ) <EOL> self . _started = False <EOL> def update ( self , op_code , cur_count , max_count = None , message = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if op_code & ( self . COMPRESSING | self . RECEIVING ) : <EOL> cur_count = str ( int ( cur_count ) ) <EOL> if max_count : <EOL> max_count = str ( int ( max_count ) ) <EOL> if op_code & self . BEGIN : <EOL> print ( "<STR_LIT>" if self . _started else "<STR_LIT>" , end = "<STR_LIT>" ) <EOL> if not self . _started : <EOL> self . _started = True <EOL> if op_code & self . END : <EOL> end = "<STR_LIT:)>" <EOL> elif max_count : <EOL> end = "<STR_LIT>" * ( <NUM_LIT:1> + len ( cur_count ) + len ( max_count ) ) <EOL> else : <EOL> end = "<STR_LIT>" * len ( cur_count ) <EOL> if max_count : <EOL> print ( "<STR_LIT>" . format ( cur_count , max_count ) , end = end ) <EOL> else : <EOL> print ( str ( cur_count ) , end = end ) <EOL> def _fetch_remotes ( remotes , prune ) : <EOL> """<STR_LIT>""" <EOL> def _get_name ( ref ) : <EOL> """<STR_LIT>""" <EOL> return ref . remote_head if isinstance ( ref , RemoteRef ) else ref . name <EOL> info = [ ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) ] <EOL> up_to_date = BLUE + "<STR_LIT>" + RESET <EOL> for remote in remotes : <EOL> print ( INDENT2 , "<STR_LIT>" , BOLD + remote . name , end = "<STR_LIT>" ) <EOL> if not remote . config_reader . has_option ( "<STR_LIT>" ) : <EOL> print ( "<STR_LIT::>" , YELLOW + "<STR_LIT>" , "<STR_LIT>" ) <EOL> continue <EOL> try : <EOL> results = remote . fetch ( progress = _ProgressMonitor ( ) , prune = prune ) <EOL> except exc . GitCommandError as err : <EOL> msg = err . command [ <NUM_LIT:0> ] . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if not msg . endswith ( "<STR_LIT:.>" ) : <EOL> msg += "<STR_LIT:.>" <EOL> print ( "<STR_LIT::>" , RED + "<STR_LIT>" , msg ) <EOL> return <EOL> except AssertionError : <EOL> print ( "<STR_LIT::>" , RED + "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> return <EOL> rlist = [ ] <EOL> for attr , singular , plural in info : <EOL> names = [ _get_name ( res . ref ) <EOL> for res in results if res . flags & getattr ( res , attr ) ] <EOL> if names : <EOL> desc = singular if len ( names ) == <NUM_LIT:1> else plural <EOL> colored = GREEN + desc + RESET <EOL> rlist . append ( "<STR_LIT>" . format ( colored , "<STR_LIT:U+002CU+0020>" . join ( names ) ) ) <EOL> print ( "<STR_LIT::>" , ( "<STR_LIT:U+002CU+0020>" . join ( rlist ) if rlist else up_to_date ) + "<STR_LIT:.>" ) <EOL> def _update_branch ( repo , branch , is_active = False ) : <EOL> """<STR_LIT>""" <EOL> print ( INDENT2 , "<STR_LIT>" , BOLD + branch . name , end = "<STR_LIT>" ) <EOL> upstream = branch . tracking_branch ( ) <EOL> if not upstream : <EOL> print ( YELLOW + "<STR_LIT>" , "<STR_LIT>" ) <EOL> return <EOL> try : <EOL> branch . commit <EOL> except ValueError : <EOL> print ( YELLOW + "<STR_LIT>" , "<STR_LIT>" ) <EOL> return <EOL> try : <EOL> upstream . commit <EOL> except ValueError : <EOL> print ( YELLOW + "<STR_LIT>" , "<STR_LIT>" ) <EOL> return <EOL> base = repo . git . merge_base ( branch . commit , upstream . commit ) <EOL> if repo . commit ( base ) == upstream . commit : <EOL> print ( BLUE + "<STR_LIT>" , end = "<STR_LIT>" ) <EOL> return <EOL> if is_active : <EOL> try : <EOL> repo . git . merge ( upstream . name , ff_only = True ) <EOL> print ( GREEN + "<STR_LIT>" , end = "<STR_LIT>" ) <EOL> except exc . GitCommandError as err : <EOL> msg = err . stderr <EOL> if "<STR_LIT>" in msg and "<STR_LIT>" in msg : <EOL> print ( YELLOW + "<STR_LIT>" , "<STR_LIT>" ) <EOL> else : <EOL> print ( YELLOW + "<STR_LIT>" , "<STR_LIT>" ) <EOL> else : <EOL> status = repo . git . merge_base ( <EOL> branch . commit , upstream . commit , is_ancestor = True , <EOL> with_extended_output = True , with_exceptions = False ) [ <NUM_LIT:0> ] <EOL> if status != <NUM_LIT:0> : <EOL> print ( YELLOW + "<STR_LIT>" , "<STR_LIT>" ) <EOL> else : <EOL> repo . git . branch ( branch . name , upstream . name , force = True ) <EOL> print ( GREEN + "<STR_LIT>" , end = "<STR_LIT>" ) <EOL> def _update_repository ( repo , current_only , fetch_only , prune ) : <EOL> """<STR_LIT>""" <EOL> print ( INDENT1 , BOLD + os . path . split ( repo . working_dir ) [ <NUM_LIT:1> ] + "<STR_LIT::>" ) <EOL> try : <EOL> active = repo . active_branch <EOL> except TypeError : <EOL> active = None <EOL> if current_only : <EOL> if not active : <EOL> print ( INDENT2 , ERROR , <EOL> "<STR_LIT>" ) <EOL> return <EOL> ref = active . tracking_branch ( ) <EOL> if not ref : <EOL> print ( INDENT2 , ERROR , "<STR_LIT>" ) <EOL> return <EOL> remotes = [ repo . remotes [ ref . remote_name ] ] <EOL> else : <EOL> remotes = repo . remotes <EOL> if not remotes : <EOL> print ( INDENT2 , ERROR , "<STR_LIT>" ) <EOL> return <EOL> _fetch_remotes ( remotes , prune ) <EOL> if not fetch_only : <EOL> for branch in sorted ( repo . heads , key = lambda b : b . name ) : <EOL> _update_branch ( repo , branch , branch == active ) <EOL> def _update_subdirectories ( path , update_args ) : <EOL> """<STR_LIT>""" <EOL> repos = [ ] <EOL> for item in os . listdir ( path ) : <EOL> try : <EOL> repo = Repo ( os . path . join ( path , item ) ) <EOL> except ( exc . InvalidGitRepositoryError , exc . NoSuchPathError ) : <EOL> continue <EOL> repos . append ( repo ) <EOL> suffix = "<STR_LIT>" if len ( repos ) == <NUM_LIT:1> else "<STR_LIT:s>" <EOL> print ( BOLD + path , "<STR_LIT>" . format ( len ( repos ) , suffix ) ) <EOL> for repo in sorted ( repos , key = lambda r : os . path . split ( r . working_dir ) [ <NUM_LIT:1> ] ) : <EOL> _update_repository ( repo , * update_args ) <EOL> def _update_directory ( path , update_args ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> repo = Repo ( path ) <EOL> except exc . NoSuchPathError : <EOL> print ( ERROR , BOLD + path , "<STR_LIT>" ) <EOL> except exc . InvalidGitRepositoryError : <EOL> if os . path . isdir ( path ) : <EOL> _update_subdirectories ( path , update_args ) <EOL> else : <EOL> print ( ERROR , BOLD + path , "<STR_LIT>" ) <EOL> else : <EOL> print ( BOLD + repo . working_dir , "<STR_LIT>" ) <EOL> _update_repository ( repo , * update_args ) <EOL> def update_bookmarks ( bookmarks , update_args ) : <EOL> """<STR_LIT>""" <EOL> if bookmarks : <EOL> for path in bookmarks : <EOL> _update_directory ( path , update_args ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> def update_directories ( paths , update_args ) : <EOL> """<STR_LIT>""" <EOL> for path in paths : <EOL> full_path = os . path . abspath ( path ) <EOL> _update_directory ( full_path , update_args ) </s>
