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(diff): normalize rename numstat path so hunks align
ldstyle8eb05ed23e026
diff --git a/jiuwenswarm/server/utils/diff_service.py b/jiuwenswarm/server/utils/diff_service.pyindex dcece11aa5..0296e52abf 100644--- a/jiuwenswarm/server/utils/diff_service.py+++ b/jiuwenswarm/server/utils/diff_service.py@@ -576,6 +576,8 @@ class DiffService:返回:{ "/abs/path/file.py": {"added": 3, "removed": 2, "isBinary": false}, ... }"""+ import re+result: dict[str, dict[str, int | bool]] = {}for line in output.strip().splitlines():if not line.strip():@@ -584,6 +586,19 @@ class DiffService:if len(parts) < 3:continueadded_str, removed_str, file_path = parts[0], parts[1], parts[2]+ # rename 的 numstat 路径需归一化为新路径,否则与 _parse_git_diff_hunks+ # 从 "+++ b/new" 提取的 key 不一致,导致 hunks 丢失。+ # 两种形式:+ # - brace 简写(有共同前/后缀): a/{b => c}/d.txt -> a/c/d.txt+ # (可能嵌套或多段: a/{b => c}/{d => e}.txt)+ # - 裸形式(无共同前/后缀): old => new -> new+ while True:+ m = re.search(r"\{([^{}]*)\s=>\s([^{}]*)\}", file_path)+ if not m:+ break+ file_path = file_path[:m.start()] + m.group(2) + file_path[m.end():]+ if " => " in file_path:+ file_path = file_path.rsplit(" => ", 1)[-1].strip()is_binary = added_str == "-" and removed_str == "-"result[file_path] = {"added": 0 if is_binary else int(added_str),diff --git a/tests/unit_tests/server/test_diff_service.py b/tests/unit_tests/server/test_diff_service.pyindex c70548571d..7c215adba9 100644--- a/tests/unit_tests/server/test_diff_service.py+++ b/tests/unit_tests/server/test_diff_service.py@@ -34,3 +34,66 @@ def test_git_diff_from_subdir_includes_repo_root_untracked_files(tmp_path):assert diff["files"][str(untracked)]["isUntracked"] is Trueassert diff["files"][str(untracked)]["linesAdded"] == 2assert diff["stats"]["filesChanged"] == 2+++def test_git_diff_staged_rename_with_modification_keeps_hunks(tmp_path):+ """staged rename + 内容修改时, hunks 应通过新路径对齐 (非空)."""+ repo = tmp_path / "repo"+ repo.mkdir()+ _git(repo, "init")+ _git(repo, "config", "user.email", "[email protected]")+ _git(repo, "config", "user.name", "Test User")++ old = repo / "old.txt"+ old.write_text("line1\nline2\nline3\n", encoding="utf-8")+ _git(repo, "add", "old.txt")+ _git(repo, "commit", "-m", "initial")++ _git(repo, "mv", "old.txt", "new.txt")+ new = repo / "new.txt"+ new.write_text("line1\nCHANGED\nline3\n", encoding="utf-8")+ _git(repo, "add", "new.txt")++ diff = DiffService().get_git_diff(str(repo))++ assert diff is not None+ assert str(new) in diff["files"]+ file_info = diff["files"][str(new)]+ assert file_info["linesAdded"] == 1+ assert file_info["linesRemoved"] == 1+ # rename 前 numstat key 是 "old => new", 与 hunk key "new" 不一致会导致 hunks 丢失+ assert len(file_info["hunks"]) == 1+ assert file_info["hunks"][0]["lines"] # 非空+++def test_git_diff_staged_rename_brace_form_keeps_hunks(tmp_path):+ """目录级 rename 触发 brace 简写 numstat (a/{b => c}/d.txt) 时, hunks 仍对齐."""+ repo = tmp_path / "repo"+ repo.mkdir()+ _git(repo, "init")+ _git(repo, "config", "user.email", "[email protected]")+ _git(repo, "config", "user.name", "Test User")++ old = repo / "a" / "b" / "d.txt"+ old.parent.mkdir(parents=True)+ old.write_text("line1\nline2\nline3\n", encoding="utf-8")+ _git(repo, "add", "a/b/d.txt")+ _git(repo, "commit", "-m", "initial")++ # a/b/d.txt -> a/c/d.txt: 共同前缀 a/ 和后缀 /d.txt, numstat 输出 a/{b => c}/d.txt+ new = repo / "a" / "c" / "d.txt"+ new.parent.mkdir(parents=True)+ _git(repo, "mv", "a/b/d.txt", "a/c/d.txt")+ new.write_text("line1\nCHANGED\nline3\n", encoding="utf-8")+ _git(repo, "add", "a/c/d.txt")++ diff = DiffService().get_git_diff(str(repo))++ assert diff is not None+ assert str(new) in diff["files"]+ file_info = diff["files"][str(new)]+ assert file_info["linesAdded"] == 1+ assert file_info["linesRemoved"] == 1+ # brace 简写 numstat key "a/{b => c}/d.txt" 需展开为 "a/c/d.txt" 与 hunk key 对齐+ assert len(file_info["hunks"]) == 1+ assert file_info["hunks"][0]["lines"] # 非空