<s> import os <EOL> from datetime import datetime <EOL> import re <EOL> import shouter <EOL> import shell <EOL> import configuration <EOL> class Initializer : <EOL> def __init__ ( self ) : <EOL> config = configuration . get ( ) <EOL> self . repoName = config . gitRepoName <EOL> self . clonedRepoName = config . clonedGitRepoName <EOL> self . author = config . user <EOL> @ staticmethod <EOL> def createignore ( ) : <EOL> newline = os . linesep <EOL> git_ignore = "<STR_LIT>" <EOL> if not os . path . exists ( git_ignore ) : <EOL> with open ( git_ignore , "<STR_LIT:w>" ) as ignore : <EOL> ignore . write ( "<STR_LIT>" + newline ) <EOL> ignore . write ( "<STR_LIT>" + newline ) <EOL> ignore . write ( "<STR_LIT>" + newline ) <EOL> config = configuration . get ( ) <EOL> if len ( config . ignoredirectories ) > <NUM_LIT:0> : <EOL> ignore . write ( newline + "<STR_LIT>" + newline ) <EOL> for directory in config . ignoredirectories : <EOL> ignore . write ( '<STR_LIT:/>' + directory + newline ) <EOL> ignore . write ( newline ) <EOL> shell . execute ( "<STR_LIT>" + git_ignore ) <EOL> shell . execute ( "<STR_LIT>" % shell . quote ( "<STR_LIT>" ) ) <EOL> @ staticmethod <EOL> def createattributes ( ) : <EOL> """<STR_LIT>""" <EOL> config = configuration . get ( ) <EOL> if len ( config . gitattributes ) > <NUM_LIT:0> : <EOL> newline = os . linesep <EOL> gitattribues = "<STR_LIT>" <EOL> if not os . path . exists ( gitattribues ) : <EOL> with open ( gitattribues , "<STR_LIT:w>" ) as attributes : <EOL> for line in config . gitattributes : <EOL> attributes . write ( line + newline ) <EOL> shell . execute ( "<STR_LIT>" + gitattribues ) <EOL> shell . execute ( "<STR_LIT>" % shell . quote ( "<STR_LIT>" ) ) <EOL> def initalize ( self ) : <EOL> self . createrepo ( ) <EOL> self . preparerepo ( ) <EOL> @ staticmethod <EOL> def preparerepo ( ) : <EOL> Initializer . setgitconfigs ( ) <EOL> Initializer . createignore ( ) <EOL> Initializer . createattributes ( ) <EOL> def createrepo ( self ) : <EOL> shell . execute ( "<STR_LIT>" + self . repoName ) <EOL> shouter . shout ( "<STR_LIT>" + os . getcwd ( ) ) <EOL> shell . execute ( "<STR_LIT>" + self . repoName ) <EOL> os . chdir ( self . clonedRepoName ) <EOL> @ staticmethod <EOL> def setgitconfigs ( ) : <EOL> shell . execute ( "<STR_LIT>" ) <EOL> shell . execute ( "<STR_LIT>" ) <EOL> shouter . shout ( "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def initialcommit ( ) : <EOL> shouter . shout ( "<STR_LIT>" ) <EOL> shell . execute ( "<STR_LIT>" , os . devnull ) <EOL> shouter . shout ( "<STR_LIT>" ) <EOL> shell . execute ( "<STR_LIT>" % shell . quote ( "<STR_LIT>" ) ) <EOL> shouter . shout ( "<STR_LIT>" ) <EOL> class Commiter : <EOL> commitcounter = <NUM_LIT:0> <EOL> isattachedtoaworkitemregex = re . compile ( "<STR_LIT>" ) <EOL> findignorepatternregex = re . compile ( "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def addandcommit ( changeentry ) : <EOL> Commiter . handleignore ( ) <EOL> Commiter . replaceauthor ( changeentry . author , changeentry . email ) <EOL> shell . execute ( "<STR_LIT>" ) <EOL> Commiter . handle_captitalization_filename_changes ( ) <EOL> shell . execute ( Commiter . getcommitcommand ( changeentry ) ) <EOL> Commiter . commitcounter += <NUM_LIT:1> <EOL> if Commiter . commitcounter is <NUM_LIT:30> : <EOL> shouter . shout ( "<STR_LIT>" ) <EOL> Commiter . pushbranch ( "<STR_LIT>" ) <EOL> Commiter . commitcounter = <NUM_LIT:0> <EOL> shouter . shout ( "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def handle_captitalization_filename_changes ( ) : <EOL> sandbox = os . path . join ( configuration . get ( ) . workDirectory , configuration . get ( ) . clonedGitRepoName ) <EOL> lines = shell . getoutput ( "<STR_LIT>" , stripped = False ) <EOL> for newfilerelativepath in Commiter . splitoutputofgitstatusz ( lines , "<STR_LIT>" ) : <EOL> directoryofnewfile = os . path . dirname ( os . path . join ( sandbox , newfilerelativepath ) ) <EOL> newfilename = os . path . basename ( newfilerelativepath ) <EOL> cwd = os . getcwd ( ) <EOL> os . chdir ( directoryofnewfile ) <EOL> files = shell . getoutput ( "<STR_LIT>" ) <EOL> for previousFileName in files : <EOL> was_same_file_name = newfilename . lower ( ) == previousFileName . lower ( ) <EOL> file_was_renamed = newfilename != previousFileName <EOL> if was_same_file_name and file_was_renamed : <EOL> shell . execute ( "<STR_LIT>" % previousFileName ) <EOL> os . chdir ( cwd ) <EOL> @ staticmethod <EOL> def getcommitcommand ( changeentry ) : <EOL> comment = Commiter . getcommentwithprefix ( changeentry . comment ) <EOL> return "<STR_LIT>" % ( shell . quote ( comment ) , shell . quote ( changeentry . date ) , changeentry . getgitauthor ( ) ) <EOL> @ staticmethod <EOL> def getcommentwithprefix ( comment ) : <EOL> prefix = configuration . get ( ) . commitmessageprefix <EOL> if prefix and Commiter . isattachedtoaworkitemregex . match ( comment ) : <EOL> return prefix + comment <EOL> return comment <EOL> @ staticmethod <EOL> def replaceauthor ( author , email ) : <EOL> shell . execute ( "<STR_LIT>" + shell . quote ( author ) ) <EOL> shell . execute ( "<STR_LIT>" + email ) <EOL> @ staticmethod <EOL> def checkbranchname ( branchname ) : <EOL> exitcode = shell . execute ( "<STR_LIT>" + branchname ) <EOL> if exitcode is <NUM_LIT:0> : <EOL> return True <EOL> else : <EOL> return False <EOL> @ staticmethod <EOL> def branch ( branchname ) : <EOL> branchexist = shell . execute ( "<STR_LIT>" + branchname ) <EOL> if branchexist is <NUM_LIT:0> : <EOL> Commiter . checkout ( branchname ) <EOL> else : <EOL> shell . execute ( "<STR_LIT>" + branchname ) <EOL> @ staticmethod <EOL> def pushbranch ( branchname , force = False ) : <EOL> if branchname : <EOL> shouter . shout ( "<STR_LIT>" + branchname ) <EOL> if force : <EOL> return shell . execute ( "<STR_LIT>" + branchname ) <EOL> else : <EOL> return shell . execute ( "<STR_LIT>" + branchname ) <EOL> @ staticmethod <EOL> def pushmaster ( ) : <EOL> Commiter . pushbranch ( "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def checkout ( branchname ) : <EOL> shell . execute ( "<STR_LIT>" + branchname ) <EOL> @ staticmethod <EOL> def renamebranch ( oldname , newname ) : <EOL> return shell . execute ( "<STR_LIT>" % ( oldname , newname ) ) <EOL> @ staticmethod <EOL> def copybranch ( existingbranchname , newbranchname ) : <EOL> return shell . execute ( "<STR_LIT>" % ( newbranchname , existingbranchname ) ) <EOL> @ staticmethod <EOL> def promotebranchtomaster ( branchname ) : <EOL> master = "<STR_LIT>" <EOL> masterrename = Commiter . renamebranch ( master , "<STR_LIT>" + datetime . now ( ) . strftime ( '<STR_LIT>' ) ) <EOL> copybranch = Commiter . copybranch ( branchname , master ) <EOL> if masterrename is <NUM_LIT:0> and copybranch is <NUM_LIT:0> : <EOL> return Commiter . pushbranch ( master , True ) <EOL> else : <EOL> shouter . shout ( "<STR_LIT>" % branchname ) <EOL> return <NUM_LIT:1> <EOL> @ staticmethod <EOL> def get_untracked_statuszlines ( ) : <EOL> return shell . getoutput ( "<STR_LIT>" , stripped = False ) <EOL> @ staticmethod <EOL> def handleignore ( ) : <EOL> """<STR_LIT>""" <EOL> repositoryfiles = Commiter . splitoutputofgitstatusz ( Commiter . get_untracked_statuszlines ( ) ) <EOL> Commiter . ignoreextensions ( repositoryfiles ) <EOL> Commiter . ignorejazzignore ( repositoryfiles ) <EOL> @ staticmethod <EOL> def ignoreextensions ( repositoryfiles ) : <EOL> """<STR_LIT>""" <EOL> ignorefileextensions = configuration . get ( ) . ignorefileextensions <EOL> if len ( ignorefileextensions ) > <NUM_LIT:0> : <EOL> Commiter . ignore ( ExtensionFilter . match ( repositoryfiles , ignorefileextensions ) ) <EOL> @ staticmethod <EOL> def ignore ( filelines ) : <EOL> """<STR_LIT>""" <EOL> if len ( filelines ) > <NUM_LIT:0> : <EOL> with open ( "<STR_LIT>" , "<STR_LIT:a>" ) as ignore : <EOL> ignore . writelines ( filelines ) <EOL> @ staticmethod <EOL> def splitoutputofgitstatusz ( lines , filterprefix = None ) : <EOL> """<STR_LIT>""" <EOL> repositoryfiles = [ ] <EOL> for line in lines : <EOL> entries = line . split ( sep = '<STR_LIT:\x00>' ) <EOL> for entry in entries : <EOL> if len ( entry ) > <NUM_LIT:0> : <EOL> if not filterprefix or entry . startswith ( filterprefix ) : <EOL> start = entry . find ( '<STR_LIT:U+0020>' ) <EOL> if <NUM_LIT:0> <= start <= <NUM_LIT:2> : <EOL> repositoryfile = entry [ <NUM_LIT:3> : ] <EOL> else : <EOL> repositoryfile = entry <EOL> repositoryfiles . append ( repositoryfile ) <EOL> return repositoryfiles <EOL> @ staticmethod <EOL> def translatejazzignore ( jazzignorelines ) : <EOL> """<STR_LIT>""" <EOL> recursive = False <EOL> gitignorelines = [ ] <EOL> for line in jazzignorelines : <EOL> if not line . startswith ( "<STR_LIT:#>" ) : <EOL> line = line . strip ( ) <EOL> if line . startswith ( "<STR_LIT>" ) : <EOL> gitignorelines . append ( os . linesep ) <EOL> recursive = line . startswith ( "<STR_LIT>" ) <EOL> for foundpattern in Commiter . findignorepatternregex . findall ( line ) : <EOL> gitignoreline = foundpattern + os . linesep <EOL> if not recursive : <EOL> gitignoreline = '<STR_LIT:/>' + gitignoreline <EOL> gitignorelines . append ( gitignoreline ) <EOL> return gitignorelines <EOL> @ staticmethod <EOL> def restore_shed_gitignore ( statuszlines ) : <EOL> """<STR_LIT>""" <EOL> gitignore = "<STR_LIT>" <EOL> gitignorelen = len ( gitignore ) <EOL> deletedfiles = Commiter . splitoutputofgitstatusz ( statuszlines , "<STR_LIT>" ) <EOL> for deletedfile in deletedfiles : <EOL> if deletedfile [ - gitignorelen : ] == gitignore : <EOL> jazzignorefile = deletedfile [ : - gitignorelen ] + "<STR_LIT>" <EOL> if os . path . exists ( jazzignorefile ) : <EOL> shell . execute ( "<STR_LIT>" % deletedfile ) <EOL> @ staticmethod <EOL> def ignorejazzignore ( repositoryfiles ) : <EOL> """<STR_LIT>""" <EOL> jazzignore = "<STR_LIT>" <EOL> jazzignorelen = len ( jazzignore ) <EOL> for repositoryfile in repositoryfiles : <EOL> if repositoryfile [ - jazzignorelen : ] == jazzignore : <EOL> path = repositoryfile [ <NUM_LIT:0> : len ( repositoryfile ) - jazzignorelen ] <EOL> gitignore = path + "<STR_LIT>" <EOL> if os . path . exists ( repositoryfile ) : <EOL> jazzignorelines = [ ] <EOL> with open ( repositoryfile , '<STR_LIT:r>' ) as jazzignorefile : <EOL> jazzignorelines = jazzignorefile . readlines ( ) <EOL> if len ( jazzignorelines ) > <NUM_LIT:0> : <EOL> with open ( gitignore , '<STR_LIT:w>' ) as gitignorefile : <EOL> gitignorefile . writelines ( Commiter . translatejazzignore ( jazzignorelines ) ) <EOL> else : <EOL> if os . path . exists ( gitignore ) : <EOL> os . remove ( gitignore ) <EOL> class Differ : <EOL> @ staticmethod <EOL> def has_diff ( ) : <EOL> return shell . execute ( "<STR_LIT>" ) is <NUM_LIT:1> <EOL> class ExtensionFilter : <EOL> @ staticmethod <EOL> def match ( repositoryfiles , extensions ) : <EOL> """<STR_LIT>""" <EOL> repositoryfilestoignore = [ ] <EOL> for extension in extensions : <EOL> for repositoryfile in repositoryfiles : <EOL> extlen = len ( extension ) <EOL> if len ( repositoryfile ) >= extlen : <EOL> if repositoryfile [ - extlen : ] == extension : <EOL> repositoryfilestoignore . append ( '<STR_LIT:/>' + repositoryfile . replace ( '<STR_LIT:\\>' , '<STR_LIT>' ) + os . linesep ) <EOL> return repositoryfilestoignore </s>
