diff options
author | Ben Hutchings <ben.hutchings@codethink.co.uk> | 2020-09-15 21:06:59 +0100 |
---|---|---|
committer | Ben Hutchings <ben.hutchings@codethink.co.uk> | 2020-09-15 21:12:38 +0100 |
commit | 3aa9bf85c228c5ace76c611af8654c3926934f62 (patch) | |
tree | 11b15111d720c42560a71f1ba0db59c6e50ed967 | |
parent | 1a89f13220d5832e7677362d7933f2fc96ab0f9d (diff) | |
download | lorry-3aa9bf85c228c5ace76c611af8654c3926934f62.tar.gz |
lorry: Use common method to create working git repositories
Add an ensure_gitdir method, that creates a bare git repository if the
given directory doesn't yet exist. It also logs what happened and
returns a flag for whether it created the directory.
Use this to replace the open-coded versions of this in (most) gitify
functions.
Add a comment in prepare_working_repos explaining why we can't just
call it there.
-rwxr-xr-x | lorry | 31 |
1 files changed, 17 insertions, 14 deletions
@@ -471,6 +471,8 @@ class Lorry(cliapp.Application): shutil.rmtree(temp_repo) if active_count == 0: + # We can't create the repo here because "git cvsimport" + # and "git svn init" insist on doing so themselves return temp_repo, None, 1 self.copy_gitdir(active_repo, temp_repo) @@ -481,6 +483,17 @@ class Lorry(cliapp.Application): with open(count_name, 'w') as count_file: count_file.write('%d\n' % count) + def ensure_gitdir(self, gitdir): + # Create git directory if it doesn't exist. Return flag for + # whether we created it. + exists = os.path.exists(gitdir) + if not exists: + self.progress('.. creating git repo') + self.run_program(['git', 'init', '--bare', gitdir]) + else: + self.progress('.. updating existing clone') + return not exists + def copy_gitdir(self, source, dest): if not os.path.exists(source): return None @@ -516,10 +529,7 @@ class Lorry(cliapp.Application): env = dict(os.environ) env['GIT_SSL_NO_VERIFY'] = 'true' - if not os.path.exists(gitdir): - self.progress('.. initialising git dir') - self.run_program(['git', 'init', '--bare', gitdir]) - self.progress('.. updating existing clone') + self.ensure_gitdir(gitdir) argv = ['git', '-c', 'gc.autodetach=false', 'fetch', '--prune', spec['url'], '+refs/heads/*:refs/heads/*', '+refs/tags/*:refs/tags/*'] @@ -538,11 +548,7 @@ class Lorry(cliapp.Application): self.progress('.. creating bzr repository') self.run_program([bzr, 'init-repo', '--no-trees', bzrdir]) - if not os.path.exists(gitdir): - self.progress('.. creating git repo') - os.mkdir(gitdir) - self.run_program(['git', 'init', '--bare', gitdir]) - self.needs_aggressive = True + self.needs_aggressive = self.ensure_gitdir(gitdir) # branches are the listed branches, plus the branch specified in url if 'branches' in spec: @@ -692,9 +698,7 @@ class Lorry(cliapp.Application): self.run_program(['hg', 'clone', '--quiet', *cert_options, spec['url'], hgdir]) - if not os.path.exists(gitdir): - self.needs_aggressive = True - self.run_program(['git', 'init', '--bare', gitdir]) + self.needs_aggressive = self.ensure_gitdir(gitdir) # Since there are marks files in existing deployments that # have broken references, fix up the marks file before rather @@ -737,8 +741,7 @@ class Lorry(cliapp.Application): else: self.progress('.. no need to run, nothing to do') return - if not os.path.exists(gitdir): - self.run_program(['git', 'init', '--bare', gitdir]) + self.ensure_gitdir(gitdir) cmdline = ["%s.%s-importer" % (lorry_path, archive_type), archive_dest] self.run_program(cmdline, cwd=gitdir) |