Elffuss BraidHub

kikocis/kleidiai public

main · 2.4 KiBRawPermalink
1#!/usr/bin/env python3
2#
3# SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>
4#
5# SPDX-License-Identifier: Apache-2.0
6#
7from __future__ import annotations
8
9import re
10import shlex
11import subprocess
12from pathlib import Path
13
14
15def repo_root() -> Path:
16 return_code, output = git_run(
17 ["rev-parse", "--show-toplevel"],
18 cwd=Path(__file__).resolve().parent,
19 )
20 if return_code != 0:
21 return Path(__file__).resolve().parent.parent.parent
22 return Path(output)
23
24
25def is_shallow_repo(root_dir: Path | None = None) -> bool:
26 cwd = root_dir or repo_root()
27 return_code, output = git_run(
28 ["rev-parse", "--is-shallow-repository"],
29 cwd=cwd,
30 )
31 if return_code != 0:
32 return False
33 return output == "true"
34
35
36def fetch_tags(root_dir: Path | None = None) -> tuple[int, str]:
37 cwd = root_dir or repo_root()
38 fetch_args = [
39 "fetch",
40 "--force",
41 "--prune",
42 "-q",
43 "origin",
44 "refs/tags/*:refs/tags/*",
45 ]
46 if is_shallow_repo(cwd):
47 fetch_args.insert(1, "--unshallow")
48 return git_run(fetch_args, cwd=cwd)
49
50
51def has_local_changes(
52 paths: list[str] | None = None, *, root_dir: Path | None = None
53) -> bool:
54 cwd = root_dir or repo_root()
55 args = ["status", "--porcelain"]
56 if paths:
57 args.extend(["--", *paths])
58 _, output = git_run(args, cwd=cwd)
59 return bool(output.strip())
60
61
62def get_commits(
63 start_ref: str,
64 end_ref: str = "HEAD",
65 *,
66 root_dir: Path | None = None,
67) -> list[str]:
68 cwd = root_dir or repo_root()
69 _, output = git_run(
70 ["log", "--format=%s", f"{start_ref}..{end_ref}"],
71 cwd=cwd,
72 )
73 if not output:
74 return []
75 return output.splitlines()
76
77
78def latest_tag_matching(pattern: str, *, root_dir: Path | None = None) -> str | None:
79 cwd = root_dir or repo_root()
80 return_code, output = git_run(["tag", "--sort=-version:refname"], cwd=cwd)
81 if return_code != 0:
82 return None
83
84 compiled_pattern = re.compile(pattern)
85 for tag in output.splitlines():
86 if compiled_pattern.match(tag):
87 return tag
88 return None
89
90
91def git_run(args: list[str], *, cwd: Path | None = None) -> tuple[int, str]:
92 cmd = ["git", *args]
93 result = subprocess.run(
94 cmd,
95 cwd=cwd,
96 check=False,
97 capture_output=True,
98 text=True,
99 )
100 return result.returncode, result.stdout.strip()