update.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env python3
  2. import pathlib
  3. import sys
  4. import common
  5. if __name__ == "__main__":
  6. common.check_workdir_state()
  7. if len(sys.argv) > 1:
  8. subtrees = sys.argv[1:]
  9. else:
  10. subtrees = sorted(list(common.REPO_ROOT.glob("**/.gitsubtree")))
  11. for subtree in subtrees:
  12. if not isinstance(subtree, pathlib.Path):
  13. if not subtree.endswith("/.gitsubtree"):
  14. subtree += "/.gitsubtree"
  15. subtree = common.REPO_ROOT / subtree
  16. path = str(subtree.parent.relative_to(common.REPO_ROOT))
  17. print(f"\n\nUpdating {path}...")
  18. for remote in subtree.read_text().splitlines():
  19. if remote.startswith("#"):
  20. continue
  21. params = remote.split(" ")
  22. if len(params) == 4:
  23. repo, branch, subdir, cached = params
  24. else:
  25. repo, branch, subdir = params
  26. if subdir == "/":
  27. result, status = common.git(
  28. "subtree",
  29. "pull",
  30. "-P",
  31. path,
  32. repo,
  33. branch,
  34. "-m",
  35. f"Merge {path} from {repo}",
  36. tee=True,
  37. )
  38. common.check_merge_result(path, repo, result, status)
  39. else:
  40. commit = common.subdir_split_helper(
  41. path, repo, branch, subdir, "merge", cached=cached
  42. )
  43. if commit:
  44. lines = subtree.read_text().splitlines()
  45. for i in range(len(lines)):
  46. if lines[i].startswith(repo):
  47. lines[i] = f"{repo} {branch} {subdir} {commit}"
  48. break
  49. subtree.write_text("\n".join(lines) + "\n")
  50. common.git("add", str(subtree.relative_to(common.REPO_ROOT)))
  51. common.git("commit", "--amend", "--no-edit")
  52. common.send_alert("Subtree update finished", "Double check merge commits")