chore(update): 忽略备份文件的脏工作区提示
This commit is contained in:
@@ -16,6 +16,7 @@ ZSGLPT Update-Agent(宿主机运行)
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import fnmatch
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
@@ -122,7 +123,25 @@ def _normalize_prefixes(prefixes: Tuple[str, ...]) -> Tuple[str, ...]:
|
||||
|
||||
def _git_has_untracked_changes(*, cwd: Path, ignore_prefixes: Tuple[str, ...]) -> Tuple[bool, int, list[str]]:
|
||||
"""检查 untracked 文件(尊重 .gitignore),并忽略指定前缀目录。"""
|
||||
return _git_has_untracked_changes_v2(cwd=cwd, ignore_prefixes=ignore_prefixes, ignore_globs=())
|
||||
|
||||
|
||||
def _normalize_globs(globs: Tuple[str, ...]) -> Tuple[str, ...]:
|
||||
normalized = []
|
||||
for g in globs:
|
||||
text = str(g or "").strip()
|
||||
if not text:
|
||||
continue
|
||||
normalized.append(text)
|
||||
return tuple(normalized)
|
||||
|
||||
|
||||
def _git_has_untracked_changes_v2(
|
||||
*, cwd: Path, ignore_prefixes: Tuple[str, ...], ignore_globs: Tuple[str, ...]
|
||||
) -> Tuple[bool, int, list[str]]:
|
||||
"""检查 untracked 文件(尊重 .gitignore),并忽略指定前缀目录/通配符。"""
|
||||
ignore_prefixes = _normalize_prefixes(ignore_prefixes)
|
||||
ignore_globs = _normalize_globs(ignore_globs)
|
||||
out = subprocess.check_output(["git", "ls-files", "--others", "--exclude-standard"], cwd=str(cwd), text=True)
|
||||
paths = [line.strip() for line in out.splitlines() if line.strip()]
|
||||
|
||||
@@ -130,13 +149,20 @@ def _git_has_untracked_changes(*, cwd: Path, ignore_prefixes: Tuple[str, ...]) -
|
||||
for p in paths:
|
||||
if ignore_prefixes and any(p.startswith(prefix) for prefix in ignore_prefixes):
|
||||
continue
|
||||
if ignore_globs and any(fnmatch.fnmatch(p, pattern) for pattern in ignore_globs):
|
||||
continue
|
||||
filtered.append(p)
|
||||
|
||||
samples = filtered[:20]
|
||||
return (len(filtered) > 0), len(filtered), samples
|
||||
|
||||
|
||||
def _git_is_dirty(*, cwd: Path, ignore_untracked_prefixes: Tuple[str, ...] = ("data/",)) -> dict:
|
||||
def _git_is_dirty(
|
||||
*,
|
||||
cwd: Path,
|
||||
ignore_untracked_prefixes: Tuple[str, ...] = ("data/",),
|
||||
ignore_untracked_globs: Tuple[str, ...] = ("*.bak.*", "*.tmp.*", "*.backup.*"),
|
||||
) -> dict:
|
||||
"""
|
||||
判断工作区是否“脏”:
|
||||
- tracked 变更(含暂存区)一律算脏
|
||||
@@ -153,8 +179,8 @@ def _git_is_dirty(*, cwd: Path, ignore_untracked_prefixes: Tuple[str, ...] = ("d
|
||||
tracked_dirty = True
|
||||
|
||||
try:
|
||||
untracked_dirty, untracked_count, untracked_samples = _git_has_untracked_changes(
|
||||
cwd=cwd, ignore_prefixes=ignore_untracked_prefixes
|
||||
untracked_dirty, untracked_count, untracked_samples = _git_has_untracked_changes_v2(
|
||||
cwd=cwd, ignore_prefixes=ignore_untracked_prefixes, ignore_globs=ignore_untracked_globs
|
||||
)
|
||||
except Exception:
|
||||
# 若 untracked 检测异常,回退到不影响更新:不计入 dirty
|
||||
@@ -167,6 +193,7 @@ def _git_is_dirty(*, cwd: Path, ignore_untracked_prefixes: Tuple[str, ...] = ("d
|
||||
"dirty_tracked": bool(tracked_dirty),
|
||||
"dirty_untracked": bool(untracked_dirty),
|
||||
"dirty_ignore_untracked_prefixes": list(_normalize_prefixes(ignore_untracked_prefixes)),
|
||||
"dirty_ignore_untracked_globs": list(_normalize_globs(ignore_untracked_globs)),
|
||||
"untracked_count": int(untracked_count),
|
||||
"untracked_samples": list(untracked_samples),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user