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