chore(update): 忽略备份文件的脏工作区提示
This commit is contained in:
@@ -16,6 +16,7 @@ ZSGLPT Update-Agent(宿主机运行)
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
import fnmatch
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import shutil
|
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]]:
|
def _git_has_untracked_changes(*, cwd: Path, ignore_prefixes: Tuple[str, ...]) -> Tuple[bool, int, list[str]]:
|
||||||
"""检查 untracked 文件(尊重 .gitignore),并忽略指定前缀目录。"""
|
"""检查 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_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)
|
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()]
|
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:
|
for p in paths:
|
||||||
if ignore_prefixes and any(p.startswith(prefix) for prefix in ignore_prefixes):
|
if ignore_prefixes and any(p.startswith(prefix) for prefix in ignore_prefixes):
|
||||||
continue
|
continue
|
||||||
|
if ignore_globs and any(fnmatch.fnmatch(p, pattern) for pattern in ignore_globs):
|
||||||
|
continue
|
||||||
filtered.append(p)
|
filtered.append(p)
|
||||||
|
|
||||||
samples = filtered[:20]
|
samples = filtered[:20]
|
||||||
return (len(filtered) > 0), len(filtered), samples
|
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 变更(含暂存区)一律算脏
|
- tracked 变更(含暂存区)一律算脏
|
||||||
@@ -153,8 +179,8 @@ def _git_is_dirty(*, cwd: Path, ignore_untracked_prefixes: Tuple[str, ...] = ("d
|
|||||||
tracked_dirty = True
|
tracked_dirty = True
|
||||||
|
|
||||||
try:
|
try:
|
||||||
untracked_dirty, untracked_count, untracked_samples = _git_has_untracked_changes(
|
untracked_dirty, untracked_count, untracked_samples = _git_has_untracked_changes_v2(
|
||||||
cwd=cwd, ignore_prefixes=ignore_untracked_prefixes
|
cwd=cwd, ignore_prefixes=ignore_untracked_prefixes, ignore_globs=ignore_untracked_globs
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
# 若 untracked 检测异常,回退到不影响更新:不计入 dirty
|
# 若 untracked 检测异常,回退到不影响更新:不计入 dirty
|
||||||
@@ -167,6 +193,7 @@ def _git_is_dirty(*, cwd: Path, ignore_untracked_prefixes: Tuple[str, ...] = ("d
|
|||||||
"dirty_tracked": bool(tracked_dirty),
|
"dirty_tracked": bool(tracked_dirty),
|
||||||
"dirty_untracked": bool(untracked_dirty),
|
"dirty_untracked": bool(untracked_dirty),
|
||||||
"dirty_ignore_untracked_prefixes": list(_normalize_prefixes(ignore_untracked_prefixes)),
|
"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_count": int(untracked_count),
|
||||||
"untracked_samples": list(untracked_samples),
|
"untracked_samples": list(untracked_samples),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user