fix: parse porcelain status safely

This commit is contained in:
SHAREit
2026-10-16 15:18:59 +08:00
parent b58dd67366
commit da770da12b
+5 -3
View File
@@ -175,13 +175,15 @@ def git_run(repo: Path, args: list[str]) -> subprocess.CompletedProcess[str]:
def git_publish(repo: Path, start: str, end: str, no_push: bool) -> str:
if not (repo / ".git").exists():
raise RuntimeError("仓库尚未初始化")
status = git_run(repo, ["status", "--porcelain", "--untracked-files=all"]).stdout.strip().splitlines()
status_output = git_run(repo, ["status", "--porcelain", "--untracked-files=all"]).stdout
status = status_output.splitlines()
allowed = re.compile(r"data/(?:index\.json|[0-9]{8}\.json)$")
allowed_paths = []
unexpected = []
for line in status:
path = line[3:] if len(line) >= 4 else ""
if not allowed.fullmatch(path):
prefix = line[:2] if len(line) >= 2 else ""
path = line[3:] if len(line) >= 4 and line[2] == " " else ""
if prefix not in {" M", "M ", "??", "D ", " D"} or not allowed.fullmatch(path):
unexpected.append(line)
else:
allowed_paths.append(path)