#NEWLINE# def track_branches( branches = None , directory = None ) : #NEWLINE# #INDENT# '\n    Tracks all specified branches.\n\n    :param branches: a list of branches that are to be tracked if not already\n    tracked.  If this is set to None then all remote branches will be tracked.\n    :param directory: directory in which to run all commands\n\n    :raises: subprocess.CalledProcessError if git command fails\n    ' #NEWLINE# if ( type ( branches ) == str ) : #NEWLINE# #INDENT# branches = [ branches ] #NEWLINE# #UNINDENT# debug ( ( ( ( ( 'track_branches(' + str ( branches ) ) + ', ' ) + str ( directory ) ) + ')' ) ) #NEWLINE# if ( branches == [ ] ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# current_branch = get_current_branch ( directory ) #NEWLINE# try : #NEWLINE# #INDENT# local_branches = get_branches ( local_only = True , directory = directory ) #NEWLINE# all_branches = get_branches ( local_only = False , directory = directory ) #NEWLINE# untracked_branches = [ ] #NEWLINE# for branch in all_branches : #NEWLINE# #INDENT# if branch . startswith ( 'remotes/' ) : #NEWLINE# #INDENT# if ( branch . count ( '/' ) >= 2 ) : #NEWLINE# #INDENT# branch = '/' . join ( branch . split ( '/' ) [ 2 : ] ) #NEWLINE# #UNINDENT# #UNINDENT# if ( branch not in local_branches ) : #NEWLINE# #INDENT# untracked_branches . append ( branch ) #NEWLINE# #UNINDENT# #UNINDENT# if ( branches is not None ) : #NEWLINE# #INDENT# branches_to_track = [ ] #NEWLINE# for untracked in untracked_branches : #NEWLINE# #INDENT# if ( untracked in branches ) : #NEWLINE# #INDENT# branches_to_track . append ( untracked ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# branches_to_track = untracked_branches #NEWLINE# #UNINDENT# debug ( ( 'Tracking branches: ' + str ( branches_to_track ) ) ) #NEWLINE# for branch in branches_to_track : #NEWLINE# #INDENT# checkout ( branch , directory = directory ) #NEWLINE# #UNINDENT# #UNINDENT# finally : #NEWLINE# #INDENT# if current_branch : #NEWLINE# #INDENT# checkout ( current_branch , directory = directory )