JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
fix(tui): show untracked files in diff viewer
ldstyle8c0c08712788e
diff --git a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.tsindex 0820692974..325ed0d312 100644--- a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts+++ b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts@@ -1519,11 +1519,9 @@ export class AppScreen implements Component, Focusable {const totalAdded = files.reduce((s, f) => s + f.linesAdded, 0);const totalRemoved = files.reduce((s, f) => s + f.linesRemoved, 0);- const workingCount = gitDiff ? Object.keys(gitDiff.files).length : 0;const turnCount = turns.length;const title = `Diff (git diff HEAD)`;const parts: string[] = [`${files.length} files changed +${totalAdded} -${totalRemoved}`];- if (workingCount > 0) parts.push(`working:${workingCount}`);if (turnCount > 0) parts.push(`turns:${turnCount}`);const subtitle = parts.join(" · ");@@ -1681,12 +1679,6 @@ export class AppScreen implements Component, Focusable {lines.push(`│ ${displayPath} ${label} ${added} ${removed}`);lines.push(`│ ${"─".repeat(Math.max(0, width - 4))}`);- if (file.isUntracked) {- lines.push(palette.text.dim("│ New file not yet staged."));- lines.push(palette.text.dim(`│ Run \`git add ${displayPath}\` to see line counts.`));- return lines;- }-if (file.isBinary) {lines.push(palette.text.dim("│ Binary file - cannot display diff"));return lines;@@ -1803,14 +1795,18 @@ export class AppScreen implements Component, Focusable {}}- const sourceLabel = file.isUntracked- ? "untracked"+ const sourceLabel = file.source === "working" || file.isUntracked+ ? "": file.isNewFile? "(new)": file.source;const line = `${pointer}${displayPath}`;- const padded = padToWidth(line, safeWidth - statsLabel.length - sourceLabel.length - 3);- const fullLine = `${padded}${palette.text.dim(sourceLabel)} ${statsStyled}`;+ const rightLabel = sourceLabel ? `${sourceLabel} ${statsLabel}` : statsLabel;+ const rightStyled = sourceLabel+ ? `${palette.text.dim(sourceLabel)} ${statsStyled}`+ : statsStyled;+ const padded = padToWidth(line, safeWidth - rightLabel.length - 1);+ const fullLine = `${padded}${rightStyled}`;if (isSelected) {lines.push(palette.text.accent(fullLine));} else {diff --git a/jiuwenswarm/server/utils/diff_service.py b/jiuwenswarm/server/utils/diff_service.pyindex fd07fd4504..dcece11aa5 100644--- a/jiuwenswarm/server/utils/diff_service.py+++ b/jiuwenswarm/server/utils/diff_service.py@@ -513,21 +513,24 @@ class DiffService:return None@staticmethod- def _is_git_repo(project_dir: str) -> bool:- """检查 project_dir 是否是一个 git 仓库."""+ def _get_git_toplevel(project_dir: str) -> str | None:+ """返回 git 仓库根目录;project_dir 可以是仓库内任意子目录."""import subprocesstry:result = subprocess.run(- ["git", "rev-parse", "--git-dir"],+ ["git", "rev-parse", "--show-toplevel"],cwd=project_dir,capture_output=True,text=True,timeout=5,)- return result.returncode == 0+ if result.returncode != 0:+ return None+ root = result.stdout.strip()+ return str(Path(root).resolve()) if root else Noneexcept Exception:- return False+ return None@staticmethoddef _is_in_transient_git_state(project_dir: str) -> bool:@@ -734,9 +737,12 @@ class DiffService:self, project_dir: str, max_files: int = MAX_FILES) -> dict[str, dict[str, Any]]:"""获取未跟踪文件列表并生成合成 diff(全部为新增行)."""+ # core.quotepath=false 让 git 直接输出原始 UTF-8 文件名,+ # 而非八进制转义串(如 "aaa_design/Code\346\250\241..."),+ # 否则中文路径无法对应到磁盘真实路径,read_text 会失败、hunk 为空。output = self._run_git_command(project_dir,- ["ls-files", "--others", "--exclude-standard"],+ ["-c", "core.quotepath=false", "ls-files", "--others", "--exclude-standard"],)if not output or not output.strip():return {}@@ -808,9 +814,10 @@ class DiffService:"""if not project_dir:return None- if not self._is_git_repo(project_dir):+ repo_dir = self._get_git_toplevel(project_dir)+ if not repo_dir:return None- if self._is_in_transient_git_state(project_dir):+ if self._is_in_transient_git_state(repo_dir):return Nonefiles: dict[str, dict[str, Any]] = {}@@ -819,7 +826,7 @@ class DiffService:# 1. 已跟踪文件的改动: git diff HEAD# 先用 --shortstat 快速探测规模,避免对超大 diff 加载完整内容- shortstat = self._run_git_command(project_dir, ["diff", "HEAD", "--shortstat"])+ shortstat = self._run_git_command(repo_dir, ["diff", "HEAD", "--shortstat"])has_tracked_changes = shortstat and shortstat.strip() != ""# 解析 shortstat 取得准确的文件/行数总计@@ -836,15 +843,15 @@ class DiffService:}if has_tracked_changes:- numstat_output = self._run_git_command(project_dir, ["diff", "HEAD", "--numstat"])- diff_output = self._run_git_command(project_dir, ["diff", "HEAD"])+ numstat_output = self._run_git_command(repo_dir, ["diff", "HEAD", "--numstat"])+ diff_output = self._run_git_command(repo_dir, ["diff", "HEAD"])if numstat_output and diff_output:per_file_stats = self._parse_git_numstat(numstat_output)filtered_output, large_files = self._split_large_file_diffs(diff_output)all_hunks, truncated_files = self._parse_git_diff_hunks(filtered_output)for rel_path, stats in list(per_file_stats.items())[:MAX_FILES]:- abs_path = str(Path(project_dir) / rel_path)+ abs_path = str(Path(repo_dir) / rel_path)is_binary = bool(stats.get("isBinary", False))is_large = rel_path in large_filesis_truncated = rel_path in truncated_files@@ -872,7 +879,7 @@ class DiffService:# 2. 未跟踪的新文件: 仅填充剩余名额,避免无限加载remaining_slots = max(0, MAX_FILES - len(files))- untracked = self._get_untracked_files(project_dir, max_files=remaining_slots)+ untracked = self._get_untracked_files(repo_dir, max_files=remaining_slots)for abs_path, file_info in untracked.items():if abs_path not in files: # 避免重复(理论上不会)file_info.setdefault("isBinary", False)diff --git a/tests/unit_tests/server/test_diff_service.py b/tests/unit_tests/server/test_diff_service.pynew file mode 100644index 0000000000..c70548571d--- /dev/null+++ b/tests/unit_tests/server/test_diff_service.py@@ -0,0 +1,36 @@+import subprocess+from pathlib import Path++from jiuwenswarm.server.utils.diff_service import DiffService+++def _git(repo: Path, *args: str) -> None:+ subprocess.run(["git", *args], cwd=repo, check=True, capture_output=True, text=True)+++def test_git_diff_from_subdir_includes_repo_root_untracked_files(tmp_path):+ repo = tmp_path / "repo"+ repo.mkdir()+ _git(repo, "init")+ _git(repo, "config", "user.email", "[email protected]")+ _git(repo, "config", "user.name", "Test User")++ tracked = repo / "tracked.txt"+ tracked.write_text("before\n", encoding="utf-8")+ _git(repo, "add", "tracked.txt")+ _git(repo, "commit", "-m", "initial")++ tracked.write_text("after\n", encoding="utf-8")+ subdir = repo / "pkg"+ subdir.mkdir()+ untracked = repo / "未跟踪.txt"+ untracked.write_text("line one\nline two\n", encoding="utf-8")++ diff = DiffService().get_git_diff(str(subdir))++ assert diff is not None+ assert str(tracked) in diff["files"]+ assert str(untracked) in diff["files"]+ assert diff["files"][str(untracked)]["isUntracked"] is True+ assert diff["files"][str(untracked)]["linesAdded"] == 2+ assert diff["stats"]["filesChanged"] == 2