aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk/commands.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python/qmk/commands.py')
-rw-r--r--lib/python/qmk/commands.py71
1 files changed, 70 insertions, 1 deletions
diff --git a/lib/python/qmk/commands.py b/lib/python/qmk/commands.py
index 104c87545..8ff8501bf 100644
--- a/lib/python/qmk/commands.py
+++ b/lib/python/qmk/commands.py
@@ -2,6 +2,7 @@
2""" 2"""
3import json 3import json
4import os 4import os
5import sys
5import shutil 6import shutil
6from pathlib import Path 7from pathlib import Path
7from subprocess import DEVNULL 8from subprocess import DEVNULL
@@ -10,7 +11,7 @@ from time import strftime
10from milc import cli 11from milc import cli
11 12
12import qmk.keymap 13import qmk.keymap
13from qmk.constants import KEYBOARD_OUTPUT_PREFIX 14from qmk.constants import QMK_FIRMWARE, KEYBOARD_OUTPUT_PREFIX
14from qmk.json_schema import json_load 15from qmk.json_schema import json_load
15 16
16time_fmt = '%Y-%m-%d-%H:%M:%S' 17time_fmt = '%Y-%m-%d-%H:%M:%S'
@@ -237,3 +238,71 @@ def parse_configurator_json(configurator_file):
237 user_keymap['layout'] = aliases[orig_keyboard]['layouts'][user_keymap['layout']] 238 user_keymap['layout'] = aliases[orig_keyboard]['layouts'][user_keymap['layout']]
238 239
239 return user_keymap 240 return user_keymap
241
242
243def git_check_repo():
244 """Checks that the .git directory exists inside QMK_HOME.
245
246 This is a decent enough indicator that the qmk_firmware directory is a
247 proper Git repository, rather than a .zip download from GitHub.
248 """
249 dot_git_dir = QMK_FIRMWARE / '.git'
250
251 return dot_git_dir.is_dir()
252
253
254def git_get_branch():
255 """Returns the current branch for a repo, or None.
256 """
257 git_branch = cli.run(['git', 'branch', '--show-current'])
258 if not git_branch.returncode != 0 or not git_branch.stdout:
259 # Workaround for Git pre-2.22
260 git_branch = cli.run(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
261
262 if git_branch.returncode == 0:
263 return git_branch.stdout.strip()
264
265
266def git_is_dirty():
267 """Returns 1 if repo is dirty, or 0 if clean
268 """
269 git_diff_staged_cmd = ['git', 'diff', '--quiet']
270 git_diff_unstaged_cmd = [*git_diff_staged_cmd, '--cached']
271
272 unstaged = cli.run(git_diff_staged_cmd)
273 staged = cli.run(git_diff_unstaged_cmd)
274
275 return unstaged.returncode != 0 or staged.returncode != 0
276
277
278def git_get_remotes():
279 """Returns the current remotes for a repo.
280 """
281 remotes = {}
282
283 git_remote_show_cmd = ['git', 'remote', 'show']
284 git_remote_get_cmd = ['git', 'remote', 'get-url']
285
286 git_remote_show = cli.run(git_remote_show_cmd)
287 if git_remote_show.returncode == 0:
288 for name in git_remote_show.stdout.splitlines():
289 git_remote_name = cli.run([*git_remote_get_cmd, name])
290 remotes[name.strip()] = {"url": git_remote_name.stdout.strip()}
291
292 return remotes
293
294
295def git_check_deviation(active_branch):
296 """Return True if branch has custom commits
297 """
298 cli.run(['git', 'fetch', 'upstream', active_branch])
299 deviations = cli.run(['git', '--no-pager', 'log', f'upstream/{active_branch}...{active_branch}'])
300 return bool(deviations.returncode)
301
302
303def in_virtualenv():
304 """Check if running inside a virtualenv.
305 Based on https://stackoverflow.com/a/1883251
306 """
307 active_prefix = getattr(sys, "base_prefix", None) or getattr(sys, "real_prefix", None) or sys.prefix
308 return active_prefix != sys.prefix