git.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # This Python file uses the following encoding: utf-8
  2. # copy from alas https://github.com/LmeSzinc/AzurLaneAutoScript
  3. from deploy.config import DeployConfig
  4. from deploy.logger import logger
  5. from deploy.utils import *
  6. class GitManager(DeployConfig):
  7. @cached_property
  8. def git(self):
  9. return self.filepath('GitExecutable')
  10. @staticmethod
  11. def remove(file):
  12. try:
  13. os.remove(file)
  14. logger.info(f'Removed file: {file}')
  15. except FileNotFoundError:
  16. logger.info(f'File not found: {file}')
  17. def git_repository_init(
  18. self, repo, source='origin', branch='master',
  19. proxy='', ssl_verify=True, keep_changes=False
  20. ):
  21. logger.hr('Git Init', 1)
  22. if not self.execute(f'"{self.git}" init', allow_failure=True):
  23. self.remove('./.git/config')
  24. self.remove('./.git/index')
  25. self.remove('./.git/HEAD')
  26. self.execute(f'"{self.git}" init')
  27. logger.hr('Set Git Proxy', 1)
  28. if proxy:
  29. self.execute(f'"{self.git}" config --local http.proxy {proxy}')
  30. self.execute(f'"{self.git}" config --local https.proxy {proxy}')
  31. else:
  32. self.execute(f'"{self.git}" config --local --unset http.proxy', allow_failure=True)
  33. self.execute(f'"{self.git}" config --local --unset https.proxy', allow_failure=True)
  34. if ssl_verify:
  35. self.execute(f'"{self.git}" config --local http.sslVerify true', allow_failure=True)
  36. else:
  37. self.execute(f'"{self.git}" config --local http.sslVerify false', allow_failure=True)
  38. logger.hr('Set Git Repository', 1)
  39. if not self.execute(f'"{self.git}" remote set-url {source} {repo}', allow_failure=True):
  40. self.execute(f'"{self.git}" remote add {source} {repo}')
  41. logger.hr('Fetch Repository Branch', 1)
  42. self.execute(f'"{self.git}" fetch {source} {branch}')
  43. logger.hr('Pull Repository Branch', 1)
  44. # Remove git lock
  45. for lock_file in [
  46. './.git/index.lock',
  47. './.git/HEAD.lock',
  48. './.git/refs/heads/master.lock',
  49. ]:
  50. if os.path.exists(lock_file):
  51. logger.info(f'Lock file {lock_file} exists, removing')
  52. os.remove(lock_file)
  53. if keep_changes:
  54. if self.execute(f'"{self.git}" stash', allow_failure=True):
  55. self.execute(f'"{self.git}" pull --ff-only {source} {branch}')
  56. if self.execute(f'"{self.git}" stash pop', allow_failure=True):
  57. pass
  58. else:
  59. # No local changes to existing files, untracked files not included
  60. logger.info('Stash pop failed, there seems to be no local changes, skip instead')
  61. else:
  62. logger.info('Stash failed, this may be the first installation, drop changes instead')
  63. self.execute(f'"{self.git}" reset --hard {source}/{branch}')
  64. self.execute(f'"{self.git}" pull --ff-only {source} {branch}')
  65. else:
  66. self.execute(f'"{self.git}" reset --hard {source}/{branch}')
  67. self.execute(f'"{self.git}" pull --ff-only {source} {branch}')
  68. logger.hr('Show Version', 1)
  69. self.execute(f'"{self.git}" --no-pager log --no-merges -1')
  70. def git_install(self):
  71. logger.hr('Update Alas', 0)
  72. if not self.AutoUpdate:
  73. logger.info('AutoUpdate is disabled, skip')
  74. return
  75. self.git_repository_init(
  76. repo=self.Repository,
  77. source='origin',
  78. branch=self.Branch,
  79. proxy=self.GitProxy,
  80. ssl_verify=self.SSLVerify,
  81. keep_changes=self.KeepLocalChanges,
  82. )