keyboard stuff
1"""Functions for working with QMK's submodules.
2"""
3from milc import cli
4
5
6def status():
7 """Returns a dictionary of submodules.
8
9 Each entry is a dict of the form:
10
11 {
12 'name': 'submodule_name',
13 'status': None/False/True,
14 'githash': '<sha-1 hash for the submodule>'
15 'shorthash': '<short hash for the submodule>'
16 'describe': '<output of `git describe --tags`>'
17 'last_log_message': 'log message'
18 'last_log_timestamp': 'timestamp'
19 }
20
21 status is None when the submodule doesn't exist, False when it's out of date, and True when it's current
22 """
23 submodules = {}
24 gitmodule_config = cli.run(['git', 'config', '-f', '.gitmodules', '-l'], timeout=30)
25 for line in gitmodule_config.stdout.splitlines():
26 key, value = line.split('=', maxsplit=2)
27 if key.endswith('.path'):
28 submodules[value] = {'name': value, 'status': None}
29
30 git_cmd = cli.run(['git', 'submodule', 'status'], timeout=30)
31 for line in git_cmd.stdout.splitlines():
32 status = line[0]
33 githash, submodule = line[1:].split()[:2]
34 submodules[submodule]['githash'] = githash
35
36 if status == '-':
37 submodules[submodule]['status'] = None
38 elif status == '+':
39 submodules[submodule]['status'] = False
40 elif status == ' ':
41 submodules[submodule]['status'] = True
42 else:
43 raise ValueError('Unknown `git submodule status` sha-1 prefix character: "%s"' % status)
44
45 submodule_logs = cli.run(['git', 'submodule', '-q', 'foreach', 'git --no-pager log --no-show-signature --pretty=format:"$sm_path%x01%h%x01%ad%x01%s%x0A" --date=iso -n1'])
46 for log_line in submodule_logs.stdout.splitlines():
47 r = log_line.split('\x01')
48 submodule = r[0]
49 submodules[submodule]['shorthash'] = r[1] if len(r) > 1 else ''
50 submodules[submodule]['last_log_timestamp'] = r[2] if len(r) > 2 else ''
51 submodules[submodule]['last_log_message'] = r[3] if len(r) > 3 else ''
52
53 submodule_tags = cli.run(['git', 'submodule', '-q', 'foreach', '\'echo $sm_path `git describe --tags`\''])
54 for log_line in submodule_tags.stdout.splitlines():
55 r = log_line.split()
56 submodule = r[0]
57 submodules[submodule]['describe'] = r[1] if len(r) > 1 else ''
58
59 return submodules
60
61
62def update(submodules=None):
63 """Update the submodules.
64
65 submodules
66 A string containing a single submodule or a list of submodules.
67 """
68 git_sync_cmd = ['git', 'submodule', 'sync']
69 git_update_cmd = ['git', 'submodule', 'update', '--init']
70
71 if submodules is None:
72 # Update everything
73 git_sync_cmd.append('--recursive')
74 git_update_cmd.append('--recursive')
75 cli.run(git_sync_cmd, check=True)
76 cli.run(git_update_cmd, check=True)
77
78 else:
79 if isinstance(submodules, str):
80 # Update only a single submodule
81 git_sync_cmd.append(submodules)
82 git_update_cmd.append(submodules)
83 cli.run(git_sync_cmd, check=True)
84 cli.run(git_update_cmd, check=True)
85
86 else:
87 # Update submodules in a list
88 for submodule in submodules:
89 cli.run([*git_sync_cmd, submodule], check=True)
90 cli.run([*git_update_cmd, submodule], check=True)