Relocate CLI git interactions (#16682)

This commit is contained in:
Joel Challis 2022-03-18 16:02:24 +00:00 committed by GitHub
parent e5823b5650
commit ed773ab73c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 118 additions and 122 deletions

View file

@ -7,7 +7,6 @@ from subprocess import DEVNULL
from milc import cli
from qmk import submodules
from qmk.constants import QMK_FIRMWARE
class CheckStatus(Enum):
@ -150,14 +149,3 @@ def is_executable(command):
cli.log.error("{fg_red}Can't run `%s %s`", command, version_arg)
return False
def check_git_repo():
"""Checks that the .git directory exists inside QMK_HOME.
This is a decent enough indicator that the qmk_firmware directory is a
proper Git repository, rather than a .zip download from GitHub.
"""
dot_git = QMK_FIRMWARE / '.git'
return CheckStatus.OK if dot_git.exists() else CheckStatus.WARNING

View file

@ -11,7 +11,8 @@ from milc.questions import yesno
from qmk import submodules
from qmk.constants import QMK_FIRMWARE, QMK_FIRMWARE_UPSTREAM
from .check import CheckStatus, check_binaries, check_binary_versions, check_submodules
from qmk.commands import git_check_repo, git_get_branch, git_get_tag, git_is_dirty, git_get_remotes, git_check_deviation, in_virtualenv
from qmk.git import git_check_repo, git_get_branch, git_get_tag, git_is_dirty, git_get_remotes, git_check_deviation
from qmk.commands import in_virtualenv
def os_tests():

View file

@ -6,7 +6,7 @@ from milc import cli
from qmk.path import normpath
from qmk.commands import dump_lines
from qmk.commands import get_git_version
from qmk.git import git_get_version
from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
TIME_FMT = '%Y-%m-%d-%H:%M:%S'
@ -33,9 +33,9 @@ def generate_version_h(cli):
chibios_version = "NA"
chibios_contrib_version = "NA"
else:
git_version = get_git_version(current_time)
chibios_version = get_git_version(current_time, "chibios", "os")
chibios_contrib_version = get_git_version(current_time, "chibios-contrib", "os")
git_version = git_get_version() or current_time
chibios_version = git_get_version("chibios", "os") or current_time
chibios_contrib_version = git_get_version("chibios-contrib", "os") or current_time
# Build the version.h file.
version_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once']

View file

@ -10,7 +10,7 @@ from dotty_dict import dotty
from milc import cli
from milc.questions import choice, question
from qmk.commands import git_get_username
from qmk.git import git_get_username
from qmk.json_schema import load_jsonschema
from qmk.path import keyboard
from qmk.json_encoders import InfoJSONEncoder

View file

@ -4,13 +4,12 @@ import os
import sys
import shutil
from pathlib import Path
from subprocess import DEVNULL
from milc import cli
import jsonschema
import qmk.keymap
from qmk.constants import QMK_FIRMWARE, KEYBOARD_OUTPUT_PREFIX
from qmk.constants import KEYBOARD_OUTPUT_PREFIX
from qmk.json_schema import json_load, validate
@ -90,31 +89,6 @@ def create_make_command(keyboard, keymap, target=None, dry_run=False, parallel=1
return create_make_target(':'.join(make_args), dry_run=dry_run, parallel=parallel, **env_vars)
def get_git_version(current_time, repo_dir='.', check_dir='.'):
"""Returns the current git version for a repo, or the current time.
"""
git_describe_cmd = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags']
if repo_dir != '.':
repo_dir = Path('lib') / repo_dir
if check_dir != '.':
check_dir = repo_dir / check_dir
if Path(check_dir).exists():
git_describe = cli.run(git_describe_cmd, stdin=DEVNULL, cwd=repo_dir)
if git_describe.returncode == 0:
return git_describe.stdout.strip()
else:
cli.log.warn(f'"{" ".join(git_describe_cmd)}" returned error code {git_describe.returncode}')
print(git_describe.stderr)
return current_time
return current_time
def get_make_parallel_args(parallel=1):
"""Returns the arguments for running the specified number of parallel jobs.
"""
@ -237,83 +211,6 @@ def parse_configurator_json(configurator_file):
return user_keymap
def git_get_username():
"""Retrieves user's username from Git config, if set.
"""
git_username = cli.run(['git', 'config', '--get', 'user.name'])
if git_username.returncode == 0 and git_username.stdout:
return git_username.stdout.strip()
def git_check_repo():
"""Checks that the .git directory exists inside QMK_HOME.
This is a decent enough indicator that the qmk_firmware directory is a
proper Git repository, rather than a .zip download from GitHub.
"""
dot_git_dir = QMK_FIRMWARE / '.git'
return dot_git_dir.is_dir()
def git_get_branch():
"""Returns the current branch for a repo, or None.
"""
git_branch = cli.run(['git', 'branch', '--show-current'])
if not git_branch.returncode != 0 or not git_branch.stdout:
# Workaround for Git pre-2.22
git_branch = cli.run(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
if git_branch.returncode == 0:
return git_branch.stdout.strip()
def git_get_tag():
"""Returns the current tag for a repo, or None.
"""
git_tag = cli.run(['git', 'describe', '--abbrev=0', '--tags'])
if git_tag.returncode == 0:
return git_tag.stdout.strip()
def git_is_dirty():
"""Returns 1 if repo is dirty, or 0 if clean
"""
git_diff_staged_cmd = ['git', 'diff', '--quiet']
git_diff_unstaged_cmd = [*git_diff_staged_cmd, '--cached']
unstaged = cli.run(git_diff_staged_cmd)
staged = cli.run(git_diff_unstaged_cmd)
return unstaged.returncode != 0 or staged.returncode != 0
def git_get_remotes():
"""Returns the current remotes for a repo.
"""
remotes = {}
git_remote_show_cmd = ['git', 'remote', 'show']
git_remote_get_cmd = ['git', 'remote', 'get-url']
git_remote_show = cli.run(git_remote_show_cmd)
if git_remote_show.returncode == 0:
for name in git_remote_show.stdout.splitlines():
git_remote_name = cli.run([*git_remote_get_cmd, name])
remotes[name.strip()] = {"url": git_remote_name.stdout.strip()}
return remotes
def git_check_deviation(active_branch):
"""Return True if branch has custom commits
"""
cli.run(['git', 'fetch', 'upstream', active_branch])
deviations = cli.run(['git', '--no-pager', 'log', f'upstream/{active_branch}...{active_branch}'])
return bool(deviations.returncode)
def in_virtualenv():
"""Check if running inside a virtualenv.
Based on https://stackoverflow.com/a/1883251

110
lib/python/qmk/git.py Normal file
View file

@ -0,0 +1,110 @@
"""Functions for working with the QMK repo.
"""
from subprocess import DEVNULL
from pathlib import Path
from milc import cli
from qmk.constants import QMK_FIRMWARE
def git_get_version(repo_dir='.', check_dir='.'):
"""Returns the current git version for a repo, or None.
"""
git_describe_cmd = ['git', 'describe', '--abbrev=6', '--dirty', '--always', '--tags']
if repo_dir != '.':
repo_dir = Path('lib') / repo_dir
if check_dir != '.':
check_dir = repo_dir / check_dir
if Path(check_dir).exists():
git_describe = cli.run(git_describe_cmd, stdin=DEVNULL, cwd=repo_dir)
if git_describe.returncode == 0:
return git_describe.stdout.strip()
else:
cli.log.warn(f'"{" ".join(git_describe_cmd)}" returned error code {git_describe.returncode}')
print(git_describe.stderr)
return None
return None
def git_get_username():
"""Retrieves user's username from Git config, if set.
"""
git_username = cli.run(['git', 'config', '--get', 'user.name'])
if git_username.returncode == 0 and git_username.stdout:
return git_username.stdout.strip()
def git_get_branch():
"""Returns the current branch for a repo, or None.
"""
git_branch = cli.run(['git', 'branch', '--show-current'])
if not git_branch.returncode != 0 or not git_branch.stdout:
# Workaround for Git pre-2.22
git_branch = cli.run(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
if git_branch.returncode == 0:
return git_branch.stdout.strip()
def git_get_tag():
"""Returns the current tag for a repo, or None.
"""
git_tag = cli.run(['git', 'describe', '--abbrev=0', '--tags'])
if git_tag.returncode == 0:
return git_tag.stdout.strip()
def git_get_remotes():
"""Returns the current remotes for a repo.
"""
remotes = {}
git_remote_show_cmd = ['git', 'remote', 'show']
git_remote_get_cmd = ['git', 'remote', 'get-url']
git_remote_show = cli.run(git_remote_show_cmd)
if git_remote_show.returncode == 0:
for name in git_remote_show.stdout.splitlines():
git_remote_name = cli.run([*git_remote_get_cmd, name])
remotes[name.strip()] = {"url": git_remote_name.stdout.strip()}
return remotes
def git_is_dirty():
"""Returns 1 if repo is dirty, or 0 if clean
"""
git_diff_staged_cmd = ['git', 'diff', '--quiet']
git_diff_unstaged_cmd = [*git_diff_staged_cmd, '--cached']
unstaged = cli.run(git_diff_staged_cmd)
staged = cli.run(git_diff_unstaged_cmd)
return unstaged.returncode != 0 or staged.returncode != 0
def git_check_repo():
"""Checks that the .git directory exists inside QMK_HOME.
This is a decent enough indicator that the qmk_firmware directory is a
proper Git repository, rather than a .zip download from GitHub.
"""
dot_git_dir = QMK_FIRMWARE / '.git'
return dot_git_dir.is_dir()
def git_check_deviation(active_branch):
"""Return True if branch has custom commits
"""
cli.run(['git', 'fetch', 'upstream', active_branch])
deviations = cli.run(['git', '--no-pager', 'log', f'upstream/{active_branch}...{active_branch}'])
return bool(deviations.returncode)