git.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 check_dir != '.':
  12. check_dir = repo_dir / check_dir
  13. if Path(check_dir).exists():
  14. git_describe = cli.run(git_describe_cmd, stdin=DEVNULL, cwd=repo_dir)
  15. if git_describe.returncode == 0:
  16. return git_describe.stdout.strip()
  17. else:
  18. cli.log.warning(f'"{" ".join(git_describe_cmd)}" returned error code {git_describe.returncode}')
  19. cli.log.warning(git_describe.stderr)
  20. return None
  21. return None
  22. def git_get_username():
  23. """Retrieves user's username from Git config, if set.
  24. """
  25. git_username = cli.run(['git', 'config', '--get', 'user.name'])
  26. if git_username.returncode == 0 and git_username.stdout:
  27. return git_username.stdout.strip()
  28. def git_get_branch():
  29. """Returns the current branch for a repo, or None.
  30. """
  31. git_branch = cli.run(['git', 'branch', '--show-current'])
  32. if not git_branch.returncode != 0 or not git_branch.stdout:
  33. # Workaround for Git pre-2.22
  34. git_branch = cli.run(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
  35. if git_branch.returncode == 0:
  36. return git_branch.stdout.strip()
  37. def git_get_tag():
  38. """Returns the current tag for a repo, or None.
  39. """
  40. git_tag = cli.run(['git', 'describe', '--abbrev=0', '--tags'])
  41. if git_tag.returncode == 0:
  42. return git_tag.stdout.strip()
  43. def git_get_last_log_entry(branch_name):
  44. """Retrieves the last log entry for the branch being worked on.
  45. """
  46. git_lastlog = cli.run(['git', '--no-pager', 'log', '--pretty=format:%ad (%h) -- %s', '--date=iso', '-n1', branch_name])
  47. if git_lastlog.returncode == 0 and git_lastlog.stdout:
  48. return git_lastlog.stdout.strip()
  49. def git_get_common_ancestor(branch_a, branch_b):
  50. """Retrieves the common ancestor between for the two supplied branches.
  51. """
  52. git_merge_base = cli.run(['git', 'merge-base', branch_a, branch_b])
  53. git_branchpoint_log = cli.run(['git', '--no-pager', 'log', '--pretty=format:%ad (%h) -- %s', '--date=iso', '-n1', git_merge_base.stdout.strip()])
  54. if git_branchpoint_log.returncode == 0 and git_branchpoint_log.stdout:
  55. return git_branchpoint_log.stdout.strip()
  56. def git_get_remotes():
  57. """Returns the current remotes for a repo.
  58. """
  59. remotes = {}
  60. git_remote_show_cmd = ['git', 'remote', 'show']
  61. git_remote_get_cmd = ['git', 'remote', 'get-url']
  62. git_remote_show = cli.run(git_remote_show_cmd)
  63. if git_remote_show.returncode == 0:
  64. for name in git_remote_show.stdout.splitlines():
  65. git_remote_name = cli.run([*git_remote_get_cmd, name])
  66. remotes[name.strip()] = {"url": git_remote_name.stdout.strip()}
  67. return remotes
  68. def git_is_dirty():
  69. """Returns 1 if repo is dirty, or 0 if clean
  70. """
  71. git_diff_staged_cmd = ['git', 'diff', '--quiet']
  72. git_diff_unstaged_cmd = [*git_diff_staged_cmd, '--cached']
  73. unstaged = cli.run(git_diff_staged_cmd)
  74. staged = cli.run(git_diff_unstaged_cmd)
  75. return unstaged.returncode != 0 or staged.returncode != 0
  76. def git_check_repo():
  77. """Checks that the .git directory exists inside QMK_HOME.
  78. This is a decent enough indicator that the qmk_firmware directory is a
  79. proper Git repository, rather than a .zip download from GitHub.
  80. """
  81. dot_git_dir = QMK_FIRMWARE / '.git'
  82. return dot_git_dir.is_dir()
  83. def git_check_safe(repo_dir='.'):
  84. """Checks if a directory passes the git safe.directory checks
  85. """
  86. if repo_dir != '.':
  87. git_cmd = ['git', '-C', repo_dir, 'status']
  88. else:
  89. git_cmd = ['git', 'status']
  90. status = cli.run(git_cmd)
  91. return '--add safe.directory' not in status.stderr
  92. def git_check_deviation(active_branch):
  93. """Return True if branch has custom commits
  94. """
  95. cli.run(['git', 'fetch', 'upstream', active_branch])
  96. deviations = cli.run(['git', '--no-pager', 'log', f'upstream/{active_branch}...{active_branch}'])
  97. return bool(deviations.returncode)
  98. def git_get_ignored_files(check_dir='.'):
  99. """Return a list of files that would be captured by the current .gitignore
  100. """
  101. invalid = cli.run(['git', 'ls-files', '-c', '-o', '-i', '--exclude-from=.gitignore', check_dir])
  102. if invalid.returncode != 0:
  103. return []
  104. return invalid.stdout.strip().splitlines()
  105. def git_get_qmk_hash():
  106. output = cli.run(['git', 'rev-parse', '--short', 'HEAD'])
  107. if output.returncode != 0:
  108. return None
  109. return output.stdout.strip()