2
0
mirror of https://github.com/frappe/bench.git synced 2024-09-24 04:59:01 +00:00

fix: switch branches "better"

This commit is contained in:
Gavin D'souza 2020-03-09 13:53:13 +05:30
parent 189d0c7d38
commit dc307b6880
2 changed files with 50 additions and 40 deletions

View File

@ -1,25 +1,32 @@
# imports - compatibility imports
from __future__ import print_function from __future__ import print_function
import os
from .utils import (exec_cmd, get_frappe, check_git_for_shallow_clone, build_assets,
restart_supervisor_processes, get_cmd_output, run_frappe_cmd, CommandFailedError,
restart_systemd_processes)
from .config.common_site_config import get_config
from six.moves import reload_module
# imports - standard imports
import json
import logging import logging
import os
import re
import shutil
import subprocess
import sys
# imports - third party imports
import click
import git
import requests import requests
import semantic_version import semantic_version
import json from six.moves import reload_module
import re
import subprocess
import bench
import sys
import shutil
import click
logging.basicConfig(level="DEBUG") # imports - module imports
import bench
from bench.config.common_site_config import get_config
from bench.utils import CommandFailedError, build_assets, check_git_for_shallow_clone, exec_cmd, get_cmd_output, get_frappe, restart_supervisor_processes, restart_systemd_processes, run_frappe_cmd
logging.basicConfig(level="INFO")
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class InvalidBranchException(Exception): pass class InvalidBranchException(Exception): pass
class InvalidRemoteException(Exception): pass class InvalidRemoteException(Exception): pass
@ -358,8 +365,7 @@ def get_repo_dir(app, bench_path='.'):
return os.path.join(bench_path, 'apps', app) return os.path.join(bench_path, 'apps', app)
def switch_branch(branch, apps=None, bench_path='.', upgrade=False, check_upgrade=True): def switch_branch(branch, apps=None, bench_path='.', upgrade=False, check_upgrade=True):
from .utils import update_requirements, update_node_packages, backup_all_sites, patch_sites, build_assets, post_upgrade from bench.utils import update_requirements, update_node_packages, backup_all_sites, patch_sites, build_assets, post_upgrade
from . import utils
apps_dir = os.path.join(bench_path, 'apps') apps_dir = os.path.join(bench_path, 'apps')
version_upgrade = (False,) version_upgrade = (False,)
switched_apps = [] switched_apps = []
@ -372,29 +378,35 @@ def switch_branch(branch, apps=None, bench_path='.', upgrade=False, check_upgrad
for app in apps: for app in apps:
app_dir = os.path.join(apps_dir, app) app_dir = os.path.join(apps_dir, app)
if os.path.exists(app_dir):
try: if not os.path.exists(app_dir):
bench.utils.log("{} does not exist!".format(app), level=2)
continue
repo = git.Repo(app_dir)
unshallow_flag = os.path.exists(os.path.join(app_dir, ".git", "shallow"))
bench.utils.log("Fetching upstream {0}for {1}".format("unshallow " if unshallow_flag else "", app))
bench.utils.exec_cmd("git remote set-branches upstream '*'", cwd=app_dir)
bench.utils.exec_cmd("git fetch --all{0}".format(" --unshallow" if unshallow_flag else ""), cwd=app_dir)
if check_upgrade: if check_upgrade:
version_upgrade = is_version_upgrade(app=app, bench_path=bench_path, branch=branch) version_upgrade = is_version_upgrade(app=app, bench_path=bench_path, branch=branch)
if version_upgrade[0] and not upgrade: if version_upgrade[0] and not upgrade:
raise MajorVersionUpgradeException("Switching to {0} will cause upgrade from {1} to {2}. Pass --upgrade to confirm".format(branch, version_upgrade[1], version_upgrade[2]), version_upgrade[1], version_upgrade[2]) bench.utils.log("Switching to {0} will cause upgrade from {1} to {2}. Pass --upgrade to confirm".format(branch, version_upgrade[1], version_upgrade[2]), level=2)
sys.exit(1)
print("Switching for "+app) print("Switching for "+app)
unshallow = "--unshallow" if os.path.exists(os.path.join(app_dir, ".git", "shallow")) else "" bench.utils.exec_cmd("git checkout {0}".format(branch), cwd=app_dir)
exec_cmd("git config --unset-all remote.upstream.fetch", cwd=app_dir)
exec_cmd("git config --add remote.upstream.fetch '+refs/heads/*:refs/remotes/upstream/*'", cwd=app_dir) if str(repo.active_branch) == branch:
exec_cmd("git fetch upstream {unshallow}".format(unshallow=unshallow), cwd=app_dir)
exec_cmd("git checkout {branch}".format(branch=branch), cwd=app_dir)
exec_cmd("git merge upstream/{branch}".format(branch=branch), cwd=app_dir)
switched_apps.append(app) switched_apps.append(app)
except CommandFailedError: else:
print("Error switching to branch {0} for {1}".format(branch, app)) bench.utils.log("Switching branches failed for: {}".format(app), level=2)
except InvalidRemoteException:
print("Remote does not exist for app {0}".format(app))
except InvalidBranchException:
print("Branch {0} does not exist in Upstream for {1}".format(branch, app))
if switched_apps: if switched_apps:
print("Successfully switched branches for:\n" + "\n".join(switched_apps)) bench.utils.log("Successfully switched branches for: " + ", ".join(switched_apps), level=1)
print('Please run `bench update --patch` to be safe from any differences in database schema')
if version_upgrade[0] and upgrade: if version_upgrade[0] and upgrade:
update_requirements() update_requirements()
@ -405,6 +417,7 @@ def switch_branch(branch, apps=None, bench_path='.', upgrade=False, check_upgrad
build_assets() build_assets()
post_upgrade(version_upgrade[1], version_upgrade[2]) post_upgrade(version_upgrade[1], version_upgrade[2])
def switch_to_branch(branch=None, apps=None, bench_path='.', upgrade=False): def switch_to_branch(branch=None, apps=None, bench_path='.', upgrade=False):
switch_branch(branch, apps=apps, bench_path=bench_path, upgrade=upgrade) switch_branch(branch, apps=apps, bench_path=bench_path, upgrade=upgrade)

View File

@ -42,8 +42,6 @@ def retry_upgrade(version):
def switch_to_branch(branch, apps, upgrade=False): def switch_to_branch(branch, apps, upgrade=False):
from bench.app import switch_to_branch from bench.app import switch_to_branch
switch_to_branch(branch=branch, apps=list(apps), upgrade=upgrade) switch_to_branch(branch=branch, apps=list(apps), upgrade=upgrade)
print('Switched to ' + branch)
print('Please run `bench update --patch` to be safe from any differences in database schema')
@click.command('switch-to-master', help="[DEPRECATED]: Switch frappe and erpnext to master branch") @click.command('switch-to-master', help="[DEPRECATED]: Switch frappe and erpnext to master branch")
@ -57,4 +55,3 @@ def switch_to_develop(upgrade=False):
"Switch frappe and erpnext to develop branch" "Switch frappe and erpnext to develop branch"
from bench.app import switch_to_develop from bench.app import switch_to_develop
switch_to_develop(apps=['frappe', 'erpnext']) switch_to_develop(apps=['frappe', 'erpnext'])
print('Switched to develop\nPlease run `bench update --patch` to be safe from any differences in database schema')