git.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. """Functions for working with the QMK repo.
  2. """
  3. from subprocess import DEVNULL
  4. from pathlib import Path
  5. from milc import cli
  6. from qmk.constants import QMK_FIRMWARE
  7. def git_get_version(repo_dir='.', check_dir='.'):
  8. """Returns the current git version for a repo, or None.
  9. """
  10. git_describe_cmd = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags']
  11. if repo_dir != '.':
  12. repo_dir = Path('lib') / repo_dir
  13. if check_dir != '.':
  14. check_dir = repo_dir / check_dir
  15. if Path(check_dir).exists():
  16. git_describe = cli.run(git_describe_cmd, stdin=DEVNULL, cwd=repo_dir)
  17. if git_describe.returncode == 0:
  18. return git_describe.stdout.strip()
  19. else:
  20. cli.log.warn(f'"{" ".join(git_describe_cmd)}" returned error code {git_describe.returncode}')
  21. print(git_describe.stderr)
  22. return None
  23. return None
  24. def git_get_username():
  25. """Retrieves user's username from Git config, if set.
  26. """
  27. git_username = cli.run(['git', 'config', '--get', 'user.name'])
  28. if git_username.returncode == 0 and git_username.stdout:
  29. return git_username.stdout.strip()
  30. def git_get_branch():
  31. """Returns the current branch for a repo, or None.
  32. """
  33. git_branch = cli.run(['git', 'branch', '--show-current'])
  34. if not git_branch.returncode != 0 or not git_branch.stdout:
  35. # Workaround for Git pre-2.22
  36. git_branch = cli.run(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
  37. if git_branch.returncode == 0:
  38. return git_branch.stdout.strip()
  39. def git_get_tag():
  40. """Returns the current tag for a repo, or None.
  41. """
  42. git_tag = cli.run(['git', 'describe', '--abbrev=0', '--tags'])
  43. if git_tag.returncode == 0:
  44. return git_tag.stdout.strip()
  45. def git_get_remotes():
  46. """Returns the current remotes for a repo.
  47. """
  48. remotes = {}
  49. git_remote_show_cmd = ['git', 'remote', 'show']
  50. git_remote_get_cmd = ['git', 'remote', 'get-url']
  51. git_remote_show = cli.run(git_remote_show_cmd)
  52. if git_remote_show.returncode == 0:
  53. for name in git_remote_show.stdout.splitlines():
  54. git_remote_name = cli.run([*git_remote_get_cmd, name])
  55. remotes[name.strip()] = {"url": git_remote_name.stdout.strip()}
  56. return remotes
  57. def git_is_dirty():
  58. """Returns 1 if repo is dirty, or 0 if clean
  59. """
  60. git_diff_staged_cmd = ['git', 'diff', '--quiet']
  61. git_diff_unstaged_cmd = [*git_diff_staged_cmd, '--cached']
  62. unstaged = cli.run(git_diff_staged_cmd)
  63. staged = cli.run(git_diff_unstaged_cmd)
  64. return unstaged.returncode != 0 or staged.returncode != 0
  65. def git_check_repo():
  66. """Checks that the .git directory exists inside QMK_HOME.
  67. This is a decent enough indicator that the qmk_firmware directory is a
  68. proper Git repository, rather than a .zip download from GitHub.
  69. """
  70. dot_git_dir = QMK_FIRMWARE / '.git'
  71. return dot_git_dir.is_dir()
  72. def git_check_deviation(active_branch):
  73. """Return True if branch has custom commits
  74. """
  75. cli.run(['git', 'fetch', 'upstream', active_branch])
  76. deviations = cli.run(['git', '--no-pager', 'log', f'upstream/{active_branch}...{active_branch}'])
  77. return bool(deviations.returncode)