You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.1 KiB

  1. """Functions for working with QMK's submodules.
  2. """
  3. from milc import cli
  4. def status():
  5. """Returns a dictionary of submodules.
  6. Each entry is a dict of the form:
  7. {
  8. 'name': 'submodule_name',
  9. 'status': None/False/True,
  10. 'githash': '<sha-1 hash for the submodule>
  11. }
  12. status is None when the submodule doesn't exist, False when it's out of date, and True when it's current
  13. """
  14. submodules = {}
  15. git_cmd = cli.run(['git', 'submodule', 'status'], timeout=30)
  16. for line in git_cmd.stdout.split('\n'):
  17. if not line:
  18. continue
  19. status = line[0]
  20. githash, submodule = line[1:].split()[:2]
  21. submodules[submodule] = {'name': submodule, 'githash': githash}
  22. if status == '-':
  23. submodules[submodule]['status'] = None
  24. elif status == '+':
  25. submodules[submodule]['status'] = False
  26. elif status == ' ':
  27. submodules[submodule]['status'] = True
  28. else:
  29. raise ValueError('Unknown `git submodule status` sha-1 prefix character: "%s"' % status)
  30. return submodules
  31. def update(submodules=None):
  32. """Update the submodules.
  33. submodules
  34. A string containing a single submodule or a list of submodules.
  35. """
  36. git_sync_cmd = ['git', 'submodule', 'sync']
  37. git_update_cmd = ['git', 'submodule', 'update', '--init']
  38. if submodules is None:
  39. # Update everything
  40. git_sync_cmd.append('--recursive')
  41. git_update_cmd.append('--recursive')
  42. cli.run(git_sync_cmd, check=True)
  43. cli.run(git_update_cmd, check=True)
  44. else:
  45. if isinstance(submodules, str):
  46. # Update only a single submodule
  47. git_sync_cmd.append(submodules)
  48. git_update_cmd.append(submodules)
  49. cli.run(git_sync_cmd, check=True)
  50. cli.run(git_update_cmd, check=True)
  51. else:
  52. # Update submodules in a list
  53. for submodule in submodules:
  54. cli.run([*git_sync_cmd, submodule], check=True)
  55. cli.run([*git_update_cmd, submodule], check=True)