at master 4.6 kB view raw
1"""Functions for working with the QMK repo. 2""" 3from subprocess import DEVNULL 4from pathlib import Path 5 6from milc import cli 7 8from qmk.constants import QMK_FIRMWARE 9 10 11def git_get_version(repo_dir='.', check_dir='.'): 12 """Returns the current git version for a repo, or None. 13 """ 14 git_describe_cmd = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags'] 15 16 if repo_dir != '.': 17 repo_dir = Path('lib') / repo_dir 18 19 if check_dir != '.': 20 check_dir = repo_dir / check_dir 21 22 if Path(check_dir).exists(): 23 git_describe = cli.run(git_describe_cmd, stdin=DEVNULL, cwd=repo_dir) 24 25 if git_describe.returncode == 0: 26 return git_describe.stdout.strip() 27 28 else: 29 cli.log.warning(f'"{" ".join(git_describe_cmd)}" returned error code {git_describe.returncode}') 30 print(git_describe.stderr) 31 return None 32 33 return None 34 35 36def git_get_username(): 37 """Retrieves user's username from Git config, if set. 38 """ 39 git_username = cli.run(['git', 'config', '--get', 'user.name']) 40 41 if git_username.returncode == 0 and git_username.stdout: 42 return git_username.stdout.strip() 43 44 45def git_get_branch(): 46 """Returns the current branch for a repo, or None. 47 """ 48 git_branch = cli.run(['git', 'branch', '--show-current']) 49 if not git_branch.returncode != 0 or not git_branch.stdout: 50 # Workaround for Git pre-2.22 51 git_branch = cli.run(['git', 'rev-parse', '--abbrev-ref', 'HEAD']) 52 53 if git_branch.returncode == 0: 54 return git_branch.stdout.strip() 55 56 57def git_get_tag(): 58 """Returns the current tag for a repo, or None. 59 """ 60 git_tag = cli.run(['git', 'describe', '--abbrev=0', '--tags']) 61 if git_tag.returncode == 0: 62 return git_tag.stdout.strip() 63 64 65def git_get_last_log_entry(branch_name): 66 """Retrieves the last log entry for the branch being worked on. 67 """ 68 git_lastlog = cli.run(['git', '--no-pager', 'log', '--pretty=format:%ad (%h) -- %s', '--date=iso', '-n1', branch_name]) 69 70 if git_lastlog.returncode == 0 and git_lastlog.stdout: 71 return git_lastlog.stdout.strip() 72 73 74def git_get_common_ancestor(branch_a, branch_b): 75 """Retrieves the common ancestor between for the two supplied branches. 76 """ 77 git_merge_base = cli.run(['git', 'merge-base', branch_a, branch_b]) 78 git_branchpoint_log = cli.run(['git', '--no-pager', 'log', '--pretty=format:%ad (%h) -- %s', '--date=iso', '-n1', git_merge_base.stdout.strip()]) 79 80 if git_branchpoint_log.returncode == 0 and git_branchpoint_log.stdout: 81 return git_branchpoint_log.stdout.strip() 82 83 84def git_get_remotes(): 85 """Returns the current remotes for a repo. 86 """ 87 remotes = {} 88 89 git_remote_show_cmd = ['git', 'remote', 'show'] 90 git_remote_get_cmd = ['git', 'remote', 'get-url'] 91 92 git_remote_show = cli.run(git_remote_show_cmd) 93 if git_remote_show.returncode == 0: 94 for name in git_remote_show.stdout.splitlines(): 95 git_remote_name = cli.run([*git_remote_get_cmd, name]) 96 remotes[name.strip()] = {"url": git_remote_name.stdout.strip()} 97 98 return remotes 99 100 101def git_is_dirty(): 102 """Returns 1 if repo is dirty, or 0 if clean 103 """ 104 git_diff_staged_cmd = ['git', 'diff', '--quiet'] 105 git_diff_unstaged_cmd = [*git_diff_staged_cmd, '--cached'] 106 107 unstaged = cli.run(git_diff_staged_cmd) 108 staged = cli.run(git_diff_unstaged_cmd) 109 110 return unstaged.returncode != 0 or staged.returncode != 0 111 112 113def git_check_repo(): 114 """Checks that the .git directory exists inside QMK_HOME. 115 116 This is a decent enough indicator that the qmk_firmware directory is a 117 proper Git repository, rather than a .zip download from GitHub. 118 """ 119 dot_git_dir = QMK_FIRMWARE / '.git' 120 121 return dot_git_dir.is_dir() 122 123 124def git_check_deviation(active_branch): 125 """Return True if branch has custom commits 126 """ 127 cli.run(['git', 'fetch', 'upstream', active_branch]) 128 deviations = cli.run(['git', '--no-pager', 'log', f'upstream/{active_branch}...{active_branch}']) 129 return bool(deviations.returncode) 130 131 132def git_get_ignored_files(check_dir='.'): 133 """Return a list of files that would be captured by the current .gitignore 134 """ 135 invalid = cli.run(['git', 'ls-files', '-c', '-o', '-i', '--exclude-from=.gitignore', check_dir]) 136 if invalid.returncode != 0: 137 return [] 138 return invalid.stdout.strip().splitlines() 139 140 141def git_get_qmk_hash(): 142 output = cli.run(['git', 'rev-parse', '--short', 'HEAD']) 143 if output.returncode != 0: 144 return None 145 146 return output.stdout.strip()