1+ """Class for interacting with git repositories
2+ """
3+
4+ from __future__ import absolute_import
5+ from __future__ import unicode_literals
6+ from __future__ import print_function
7+
18import os
29import re
310
@@ -315,23 +322,7 @@ def _git_checkout(self, repo_dir_path):
315322
316323 cmd = []
317324 if self ._branch :
318- (curr_branch , _ ) = self ._git_current_branch ()
319- ref_type = self ._git_ref_type (self ._branch )
320- if ref_type == self .GIT_REF_REMOTE_BRANCH :
321- cmd = ['git' , 'checkout' , '--track' , 'origin/' + self ._branch ]
322- elif ref_type == self .GIT_REF_LOCAL_BRANCH :
323- if curr_branch != self ._branch :
324- if not self ._git_working_dir_clean (repo_dir_path ):
325- msg = ('Working directory "{0}" not clean, '
326- 'aborting' .format (repo_dir_path ))
327- fatal_error (msg )
328- else :
329- cmd = ['git' , 'checkout' , self ._branch ]
330-
331- else :
332- msg = 'Unable to check out branch, "{0}"' .format (self ._branch )
333- fatal_error (msg )
334-
325+ cmd = self ._checkout_branch_command (repo_dir_path )
335326 elif self ._tag :
336327 # For now, do a hail mary and hope tag can be checked out
337328 cmd = ['git' , 'checkout' , self ._tag ]
@@ -344,6 +335,31 @@ def _git_checkout(self, repo_dir_path):
344335
345336 os .chdir (cwd )
346337
338+ def _checkout_branch_command (self , repo_dir_path ):
339+ """Construct the command for checking out the specified branch
340+ """
341+ cmd = []
342+ (curr_branch , _ ) = self ._git_current_branch ()
343+ ref_type = self ._git_ref_type (self ._branch )
344+ if ref_type == self .GIT_REF_REMOTE_BRANCH :
345+ cmd = ['git' , 'checkout' , '--track' , 'origin/' + self ._branch ]
346+ elif ref_type == self .GIT_REF_LOCAL_BRANCH :
347+ if curr_branch != self ._branch :
348+ # FIXME(bja, 2017-11) not sure what this branch logic
349+ # is accomplishing, but it can lead to cmd being
350+ # undefined without an error. Probably not what we
351+ # want!
352+ if not self ._git_working_dir_clean (repo_dir_path ):
353+ msg = ('Working directory "{0}" not clean, '
354+ 'aborting' .format (repo_dir_path ))
355+ fatal_error (msg )
356+ else :
357+ cmd = ['git' , 'checkout' , self ._branch ]
358+ else :
359+ msg = 'Unable to check out branch, "{0}"' .format (self ._branch )
360+ fatal_error (msg )
361+ return cmd
362+
347363 @staticmethod
348364 def git_status_porcelain_v1z ():
349365 """Run the git status command on the cwd and report results in the
0 commit comments