2023-05-14 07:48:34 +00:00
|
|
|
import configparser
|
2023-09-09 10:36:14 +00:00
|
|
|
import os
|
2023-05-14 07:48:34 +00:00
|
|
|
|
|
|
|
from deploy.Windows.config import DeployConfig
|
2023-09-09 10:36:14 +00:00
|
|
|
from deploy.Windows.logger import Progress, logger
|
|
|
|
from deploy.Windows.utils import cached_property
|
2023-09-09 19:09:15 +00:00
|
|
|
from deploy.git_over_cdn.client import GitOverCdnClient
|
2023-05-14 07:48:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GitConfigParser(configparser.ConfigParser):
|
|
|
|
def check(self, section, option, value):
|
|
|
|
result = self.get(section, option, fallback=None)
|
|
|
|
if result == value:
|
|
|
|
logger.info(f'Git config {section}.{option} = {value}')
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2023-09-09 19:09:15 +00:00
|
|
|
class GitOverCdnClientWindows(GitOverCdnClient):
|
|
|
|
def update(self, *args, **kwargs):
|
|
|
|
Progress.GitInit()
|
|
|
|
_ = super().update(*args, **kwargs)
|
|
|
|
Progress.GitShowVersion()
|
|
|
|
return _
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def latest_commit(self) -> str:
|
|
|
|
_ = super().latest_commit
|
|
|
|
Progress.GitLatestCommit()
|
|
|
|
return _
|
|
|
|
|
|
|
|
def download_pack(self):
|
|
|
|
_ = super().download_pack()
|
|
|
|
Progress.GitDownloadPack()
|
|
|
|
return _
|
|
|
|
|
|
|
|
|
2023-05-14 07:48:34 +00:00
|
|
|
class GitManager(DeployConfig):
|
|
|
|
@staticmethod
|
|
|
|
def remove(file):
|
|
|
|
try:
|
|
|
|
os.remove(file)
|
|
|
|
logger.info(f'Removed file: {file}')
|
|
|
|
except FileNotFoundError:
|
|
|
|
logger.info(f'File not found: {file}')
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def git_config(self):
|
|
|
|
conf = GitConfigParser()
|
|
|
|
conf.read('./.git/config')
|
|
|
|
return conf
|
|
|
|
|
|
|
|
def git_repository_init(
|
|
|
|
self, repo, source='origin', branch='master',
|
|
|
|
proxy='', ssl_verify=True, keep_changes=False
|
|
|
|
):
|
|
|
|
logger.hr('Git Init', 1)
|
|
|
|
if not self.execute(f'"{self.git}" init', allow_failure=True):
|
|
|
|
self.remove('./.git/config')
|
|
|
|
self.remove('./.git/index')
|
|
|
|
self.remove('./.git/HEAD')
|
|
|
|
self.remove('./.git/ORIG_HEAD')
|
|
|
|
self.execute(f'"{self.git}" init')
|
2023-09-09 10:36:14 +00:00
|
|
|
Progress.GitInit()
|
2023-05-14 07:48:34 +00:00
|
|
|
|
|
|
|
logger.hr('Set Git Proxy', 1)
|
|
|
|
if proxy:
|
|
|
|
if not self.git_config.check('http', 'proxy', value=proxy):
|
|
|
|
self.execute(f'"{self.git}" config --local http.proxy {proxy}')
|
|
|
|
if not self.git_config.check('https', 'proxy', value=proxy):
|
|
|
|
self.execute(f'"{self.git}" config --local https.proxy {proxy}')
|
|
|
|
else:
|
|
|
|
if not self.git_config.check('http', 'proxy', value=None):
|
|
|
|
self.execute(f'"{self.git}" config --local --unset http.proxy', allow_failure=True)
|
|
|
|
if not self.git_config.check('https', 'proxy', value=None):
|
|
|
|
self.execute(f'"{self.git}" config --local --unset https.proxy', allow_failure=True)
|
|
|
|
|
|
|
|
if ssl_verify:
|
|
|
|
if not self.git_config.check('http', 'sslVerify', value='true'):
|
|
|
|
self.execute(f'"{self.git}" config --local http.sslVerify true', allow_failure=True)
|
|
|
|
else:
|
|
|
|
if not self.git_config.check('http', 'sslVerify', value='false'):
|
|
|
|
self.execute(f'"{self.git}" config --local http.sslVerify false', allow_failure=True)
|
2023-09-09 10:36:14 +00:00
|
|
|
Progress.GitSetConfig()
|
2023-05-14 07:48:34 +00:00
|
|
|
|
|
|
|
logger.hr('Set Git Repository', 1)
|
|
|
|
if not self.git_config.check(f'remote "{source}"', 'url', value=repo):
|
|
|
|
if not self.execute(f'"{self.git}" remote set-url {source} {repo}', allow_failure=True):
|
|
|
|
self.execute(f'"{self.git}" remote add {source} {repo}')
|
2023-09-09 10:36:14 +00:00
|
|
|
Progress.GitSetRepo()
|
2023-05-14 07:48:34 +00:00
|
|
|
|
|
|
|
logger.hr('Fetch Repository Branch', 1)
|
|
|
|
self.execute(f'"{self.git}" fetch {source} {branch}')
|
2023-09-09 10:36:14 +00:00
|
|
|
Progress.GitFetch()
|
2023-05-14 07:48:34 +00:00
|
|
|
|
|
|
|
logger.hr('Pull Repository Branch', 1)
|
|
|
|
# Remove git lock
|
|
|
|
for lock_file in [
|
|
|
|
'./.git/index.lock',
|
|
|
|
'./.git/HEAD.lock',
|
|
|
|
'./.git/refs/heads/master.lock',
|
|
|
|
]:
|
|
|
|
if os.path.exists(lock_file):
|
|
|
|
logger.info(f'Lock file {lock_file} exists, removing')
|
|
|
|
os.remove(lock_file)
|
|
|
|
if keep_changes:
|
|
|
|
if self.execute(f'"{self.git}" stash', allow_failure=True):
|
|
|
|
self.execute(f'"{self.git}" pull --ff-only {source} {branch}')
|
|
|
|
if self.execute(f'"{self.git}" stash pop', allow_failure=True):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# No local changes to existing files, untracked files not included
|
|
|
|
logger.info('Stash pop failed, there seems to be no local changes, skip instead')
|
|
|
|
else:
|
|
|
|
logger.info('Stash failed, this may be the first installation, drop changes instead')
|
|
|
|
self.execute(f'"{self.git}" reset --hard {source}/{branch}')
|
|
|
|
self.execute(f'"{self.git}" pull --ff-only {source} {branch}')
|
|
|
|
else:
|
|
|
|
self.execute(f'"{self.git}" reset --hard {source}/{branch}')
|
2023-09-09 10:36:14 +00:00
|
|
|
Progress.GitReset()
|
2023-05-14 07:48:34 +00:00
|
|
|
# Since `git fetch` is already called, checkout is faster
|
|
|
|
if not self.execute(f'"{self.git}" checkout {branch}', allow_failure=True):
|
|
|
|
self.execute(f'"{self.git}" pull --ff-only {source} {branch}')
|
2023-09-09 10:36:14 +00:00
|
|
|
Progress.GitCheckout()
|
2023-05-14 07:48:34 +00:00
|
|
|
|
|
|
|
logger.hr('Show Version', 1)
|
|
|
|
self.execute(f'"{self.git}" --no-pager log --no-merges -1')
|
2023-09-09 10:36:14 +00:00
|
|
|
Progress.GitShowVersion()
|
2023-05-14 07:48:34 +00:00
|
|
|
|
2023-09-10 10:00:47 +00:00
|
|
|
@property
|
|
|
|
def goc_client(self):
|
2023-09-09 19:09:15 +00:00
|
|
|
client = GitOverCdnClient(
|
|
|
|
url='https://vip.123pan.cn/1815343254/pack/LmeSzinc_StarRailCopilot_master',
|
2023-09-10 06:52:37 +00:00
|
|
|
folder=self.root_filepath,
|
2023-09-10 13:56:31 +00:00
|
|
|
source='origin',
|
|
|
|
branch='master',
|
|
|
|
git=self.git,
|
2023-09-09 19:09:15 +00:00
|
|
|
)
|
|
|
|
client.logger = logger
|
2023-09-10 10:00:47 +00:00
|
|
|
return client
|
2023-09-09 19:09:15 +00:00
|
|
|
|
2023-05-14 07:48:34 +00:00
|
|
|
def git_install(self):
|
|
|
|
logger.hr('Update Alas', 0)
|
|
|
|
|
|
|
|
if not self.AutoUpdate:
|
|
|
|
logger.info('AutoUpdate is disabled, skip')
|
2023-09-09 10:36:14 +00:00
|
|
|
Progress.GitShowVersion()
|
2023-05-14 07:48:34 +00:00
|
|
|
return
|
|
|
|
|
2023-09-09 19:09:15 +00:00
|
|
|
if self.GitOverCdn:
|
2023-09-10 10:00:47 +00:00
|
|
|
if self.goc_client.update(keep_changes=self.KeepLocalChanges):
|
2023-09-09 19:09:15 +00:00
|
|
|
return
|
|
|
|
|
2023-05-14 07:48:34 +00:00
|
|
|
self.git_repository_init(
|
|
|
|
repo=self.Repository,
|
|
|
|
source='origin',
|
|
|
|
branch=self.Branch,
|
|
|
|
proxy=self.GitProxy,
|
|
|
|
ssl_verify=self.SSLVerify,
|
|
|
|
keep_changes=self.KeepLocalChanges,
|
|
|
|
)
|