def wrapper(self, *args, **kwargs): oldDir = os.getcwd() os.chdir(self.path) try: fn(self, *args, **kwargs) except Exception as e: try: # Failed CVS operations may leave checkout in inconsistent state. # Remove the checkout to prevent trouble next time around util.removeRecursive(self.path) finally: raise CVSError(e) finally: os.chdir(oldDir)
def importcvs(self, repository, Git, CVS, cvsbranch, gitbranch): gitDir = self.ctx.getGitDir() repoName = self.ctx.getRepositoryName(repository) repoDir = "/".join((gitDir, repoName)) skeleton = self.ctx.getSkeleton(repository) exportDir = self.ctx.getCVSExportDir(repository) if os.path.exists(exportDir): util.removeRecursive(exportDir) os.makedirs(exportDir) os.chdir(os.path.dirname(exportDir)) CVS.export(os.path.basename(exportDir)) cvsignore = ignore.Ignore(Git.log, exportDir + "/.cvsignore") # Awaiting use case requiring partial import into Git before # writing test cases to implement it for import from CVS into Git # bigitrsync = ignore.Ignore(Git.log, gitDir + '/.bigitrsync', regex=True) exportedFiles = util.listFiles(exportDir) if not exportedFiles: raise RuntimeError("CVS branch '%s' for location '%s' contains no files" % (CVS.branch, CVS.location)) # Sync only explicitly requested files # exportedFiles = sorted(list(bigitrsync.include(exportedFiles))) os.chdir(exportDir) Git.initializeGitRepository() os.chdir(repoDir) addSkeleton = False branches = Git.branches() if gitbranch not in branches: if "remotes/origin/" + gitbranch in branches: # check out existing remote branch Git.checkoutTracking(gitbranch) else: # check out a new "orphan" branch Git.checkoutNewImportBranch(gitbranch) addSkeleton = True else: if Git.branch() != gitbranch: Git.checkout(gitbranch) Git.fetch() Git.mergeFastForward("origin/" + gitbranch) # clean up after any garbage left over from previous runs so # that we can change branches Git.pristine() gitFiles = Git.listContentFiles() gitFiles = cvsignore.filter(set(gitFiles)) gitFiles.discard(".bigitrsync") # delete only files matching the sync expressions # gitFiles = sorted(list(bigitrsync.include(gitFiles))) for filename in gitFiles: os.remove(filename) os.chdir(gitDir) util.copyFiles(exportDir, repoDir, exportedFiles) if addSkeleton: if skeleton: skelFiles = util.listFiles(skeleton) util.copyFiles(skeleton, repoDir, skelFiles) os.chdir(repoDir) Git.runImpPreHooks(gitbranch) if Git.status(): # there is some change to commit Git.infoStatus() Git.infoDiff() # store Git.log.lastOutput() to email after successful push Git.addAll() # Git.addAll() will have regularized line ending differences, # and in case that is the only change, we need to check again # on status if Git.status(): # FIXME: try to create a commit message that includes all # the CVS commit messages since the previous commit, de-duplicated Git.commit("import from CVS as of %s" % time.asctime()) Git.push("origin", gitbranch, gitbranch) Git.runImpPostHooks(gitbranch) merger = gitmerge.Merger(self.ctx) merger.mergeFrom(repository, Git, gitbranch)
def test_removeRecursive(self): util.removeRecursive(self.s) self.assertEqual(util.listFiles(self.s), [])
def importcvs(self, repository, Git, CVS, cvsbranch, gitbranch): gitDir = self.ctx.getGitDir() repoName = self.ctx.getRepositoryName(repository) repoDir = '/'.join((gitDir, repoName)) skeleton = self.ctx.getSkeleton(repository) exportDir = self.ctx.getCVSExportDir(repository) if os.path.exists(exportDir): util.removeRecursive(exportDir) os.makedirs(exportDir) os.chdir(os.path.dirname(exportDir)) CVS.export(os.path.basename(exportDir)) cvsignore = ignore.Ignore(Git.log, exportDir + '/.cvsignore') # Awaiting use case requiring partial import into Git before # writing test cases to implement it for import from CVS into Git # bigitrsync = ignore.Ignore(Git.log, gitDir + '/.bigitrsync', regex=True) exportedFiles = util.listFiles(exportDir) if not exportedFiles: raise RuntimeError("CVS branch '%s' for location '%s' contains no files" %(CVS.branch, CVS.location)) # Sync only explicitly requested files # exportedFiles = sorted(list(bigitrsync.include(exportedFiles))) os.chdir(exportDir) Git.initializeGitRepository() os.chdir(repoDir) addSkeleton = False branches = Git.branches() if gitbranch not in branches: if 'remotes/origin/' + gitbranch in branches: # check out existing remote branch Git.checkoutTracking(gitbranch) else: # check out a new "orphan" branch Git.checkoutNewImportBranch(gitbranch) addSkeleton = True else: if Git.branch() != gitbranch: Git.checkout(gitbranch) Git.fetch() Git.mergeFastForward('origin/' + gitbranch) # clean up after any garbage left over from previous runs so # that we can change branches Git.pristine() gitFiles = Git.listContentFiles() gitFiles = cvsignore.filter(set(gitFiles)) gitFiles.discard('.bigitrsync') # delete only files matching the sync expressions # gitFiles = sorted(list(bigitrsync.include(gitFiles))) for filename in gitFiles: os.remove(filename) os.chdir(gitDir) util.copyFiles(exportDir, repoDir, exportedFiles) if addSkeleton: if skeleton: skelFiles = util.listFiles(skeleton) util.copyFiles(skeleton, repoDir, skelFiles) os.chdir(repoDir) Git.runImpPreHooks(gitbranch) if Git.status(): # there is some change to commit Git.infoStatus() Git.infoDiff() # store Git.log.lastOutput() to email after successful push Git.addAll() # Git.addAll() will have regularized line ending differences, # and in case that is the only change, we need to check again # on status if Git.status(): # FIXME: try to create a commit message that includes all # the CVS commit messages since the previous commit, de-duplicated Git.commit('import from CVS as of %s' %time.asctime()) Git.push('origin', gitbranch, gitbranch) Git.runImpPostHooks(gitbranch) merger = gitmerge.Merger(self.ctx) merger.mergeFrom(repository, Git, gitbranch)
def removeRecursive(dir): util.removeRecursive(dir)