Skip to content

Commit

Permalink
Merge pull request #472 from acsone/create-branch
Browse files Browse the repository at this point in the history
[ADD] oca-create-branch: create branch from a copier template
  • Loading branch information
sbidoul committed Oct 17, 2021
2 parents 9ee18cd + 6a2e045 commit bde63ee
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 8 deletions.
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -75,6 +75,7 @@
'tools.update_pre_commit_excluded_addons:main',
'oca-fix-manifest-website = tools.fix_manifest_website:main',
'oca-configure-travis= tools.configure_travis:main',
'oca-create-branch = tools.create_branch:main',
],
},
)
78 changes: 78 additions & 0 deletions tools/create_branch.py
@@ -0,0 +1,78 @@
"""Create a branch in all addons project.
TODO
- load copier answers from a previous branch
"""
import subprocess

import click

from .oca_projects import get_repositories, temporary_clone


@click.command("Create an orphan branch from a 'copier' template")
@click.argument("new_branch")
@click.option(
"--copier-template",
default="gh:oca/oca-addons-repo-template",
show_default=True,
)
@click.option(
"--copier-template-vcs-ref",
)
@click.option(
"--repo",
"repos",
multiple=True,
)
def main(new_branch, copier_template, copier_template_vcs_ref, repos):
for repo in repos or get_repositories():
print("=" * 10, repo, "=" * 10)
with temporary_clone(repo):
# check if branch already exists
if subprocess.check_output(
["git", "ls-remote", "--head", "origin", new_branch]
):
print(f"branch {new_branch} already exist in {repo}")
continue
# set git user/email
subprocess.check_call(
["git", "config", "user.name", "oca-git-bot"],
)
subprocess.check_call(
["git", "config", "user.email", "oca-git-bot@odoo-community.org"],
)
# create empty git branch
subprocess.check_call(["git", "checkout", "--orphan", new_branch])
subprocess.check_call(["git", "reset", "--hard"])
# copier
copier_cmd = [
"copier",
"--data",
f"odoo_version={new_branch}",
"--data",
f"repo_slug={repo}",
"--data",
f"repo_name={repo}",
"--data",
"repo_description=TODO: add repo description.",
"--data",
"dependency_installation_mode=PIP",
"--data",
"ci=GitHub",
"--force",
]
if copier_template_vcs_ref:
copier_cmd += ["--vcs-ref", copier_template_vcs_ref]
copier_cmd += [copier_template, "."]
subprocess.check_call(copier_cmd)
# pre-commit run -a
subprocess.check_call(["git", "add", "."])
subprocess.call(["pre-commit", "run", "-a"])
# commit and push
subprocess.check_call(["git", "add", "."])
subprocess.check_call(
["git", "commit", "-m", f"Initialize {new_branch} branch"]
)
subprocess.check_call(["pre-commit", "run", "-a"]) # to be sure
subprocess.check_call(["git", "push", "origin", new_branch])
22 changes: 14 additions & 8 deletions tools/oca_projects.py
Expand Up @@ -263,7 +263,7 @@ class BranchNotFoundError(RuntimeError):


@contextmanager
def temporary_clone(project_name, branch, protocol='git', org_name='OCA'):
def temporary_clone(project_name, branch=None, protocol='git', org_name='OCA'):
""" context manager that clones a git branch and cd to it, with cache """
# init cache directory
cache_dir = appdirs.user_cache_dir('oca-mqt')
Expand All @@ -280,19 +280,25 @@ def temporary_clone(project_name, branch, protocol='git', org_name='OCA'):
'refs/heads/*:refs/heads/*',
]
subprocess.check_call(fetch_cmd, cwd=repo_cache_dir)
# check if branch exist
branches = subprocess.check_output(
['git', 'branch'], universal_newlines=True, cwd=repo_cache_dir)
branches = [b.strip() for b in branches.split()]
if branch not in branches:
raise BranchNotFoundError()
if branch:
# check if branch exist
branches = subprocess.check_output(
['git', 'branch'], universal_newlines=True, cwd=repo_cache_dir)
branches = [b.strip() for b in branches.split()]
if branch not in branches:
raise BranchNotFoundError()
# clone to temp dir, with --reference to cache
tempdir = tempfile.mkdtemp()
try:
clone_cmd = [
'git', 'clone', '--quiet',
'--reference', repo_cache_dir,
'--branch', branch,
]
if branch:
clone_cmd += [
'--branch', branch,
]
clone_cmd += [
'--',
repo_url,
tempdir,
Expand Down

0 comments on commit bde63ee

Please sign in to comment.