MCPcopy Create free account
hub / github.com/FSoft-AI4Code/CodeWiki / _detect_changed_files

Function _detect_changed_files

codewiki/cli/commands/generate.py:42–145  ·  view source on GitHub ↗

Detect files changed since the last documentation generation. Reads the commit_id from metadata.json and compares with current HEAD using git diff. When running inside a subdirectory of a monorepo, only files under that subdirectory are returned. Returns list of changed file p

(
    repo_path: Path,
    output_dir: Path,
    logger,
    verbose: bool,
    compare_to: Optional[str] = None
)

Source from the content-addressed store, hash-verified

40 return [p.strip() for p in patterns_str.split(",") if p.strip()]
41
42
43def _read_stored_language(output_dir: Path) -> str | None:
44 """Language recorded in metadata.json by the previous generation (None = English)."""
45 import json
46
47 try:
48 metadata = json.loads((output_dir / "metadata.json").read_text(encoding="utf-8"))
49 return (metadata.get("generation_info") or {}).get("language")
50 except (OSError, json.JSONDecodeError, AttributeError):
51 return None
52
53
54def _detect_changed_files(
55 repo_path: Path, output_dir: Path, logger, verbose: bool, compare_to: str | None = None
56) -> list[str] | None:
57 """
58 Detect files changed since the last documentation generation.
59
60 Reads the commit_id from metadata.json and compares with current HEAD
61 using git diff. When running inside a subdirectory of a monorepo,
62 only files under that subdirectory are returned.
63
64 Returns list of changed file paths relative to repo_path, or None if
65 unable to determine (e.g., no metadata, not a git repo).
66 """
67 import json
68
69 prev_commit = None
70 if compare_to:
71 prev_commit = compare_to
72 else:
73 metadata_path = output_dir / "metadata.json"
74 if not metadata_path.exists():
75 if verbose:
76 logger.debug(
77 "No metadata.json found — cannot detect changes, running full generation."
78 )
79 return None
80
81 try:
82 metadata = json.loads(metadata_path.read_text())
83 prev_commit = metadata.get("generation_info", {}).get("commit_id")
84 if not prev_commit:
85 if verbose:
86 logger.debug("No commit_id in metadata — running full generation.")
87 return None
88 except (json.JSONDecodeError, OSError):
89 return None
90
91 # Get current HEAD commit
92 try:
93 import git
94
95 repo = git.Repo(repo_path, search_parent_directories=True)
96 current_commit = repo.head.commit.hexsha
97 except Exception: # noqa: BLE001 — any git failure means "no incremental update"
98 if verbose:
99 logger.debug("Cannot access git repo — running full generation.")

Callers 1

generate_commandFunction · 0.85

Calls 3

debugMethod · 0.80
getMethod · 0.80
resolveMethod · 0.80

Tested by

no test coverage detected