@@ -49,8 +49,11 @@ def _detect_changed_files(
4949 Detect files changed since the last documentation generation.
5050
5151 Reads the commit_id from metadata.json and compares with current HEAD
52- using git diff. Returns list of changed file paths, or None if unable
53- to determine (e.g., no metadata, not a git repo).
52+ using git diff. When running inside a subdirectory of a monorepo,
53+ only files under that subdirectory are returned.
54+
55+ Returns list of changed file paths relative to repo_path, or None if
56+ unable to determine (e.g., no metadata, not a git repo).
5457 """
5558 import json
5659
@@ -85,6 +88,21 @@ def _detect_changed_files(
8588 logger .debug (f"HEAD is still at { current_commit [:8 ]} — no changes." )
8689 return []
8790
91+ # Determine subdirectory prefix relative to the git root
92+ if repo .working_tree_dir is None :
93+ if verbose :
94+ logger .debug ("Bare git repository — running full generation." )
95+ return None
96+ git_root = Path (repo .working_tree_dir ).resolve ()
97+ repo_path_resolved = repo_path .resolve ()
98+ try :
99+ subpath_prefix = repo_path_resolved .relative_to (git_root ).as_posix ()
100+ except ValueError :
101+ # repo_path is outside git root — shouldn't happen, but fall back to full generation
102+ if verbose :
103+ logger .debug ("Repo path is outside git root — running full generation." )
104+ return None
105+
88106 # Get changed files between previous and current commit
89107 try :
90108 diff_index = repo .commit (prev_commit ).diff (current_commit )
@@ -95,14 +113,27 @@ def _detect_changed_files(
95113 if diff .b_path and diff .b_path != diff .a_path :
96114 changed .append (diff .b_path )
97115
116+ # Filter to files under the current subdirectory and strip the prefix
117+ # so paths align with module_tree.json component paths
118+ filtered = []
119+ if subpath_prefix == "." :
120+ filtered = changed
121+ else :
122+ prefix = subpath_prefix + "/"
123+ for path in changed :
124+ if path .startswith (prefix ):
125+ filtered .append (path [len (prefix ):])
126+
98127 if verbose :
99128 logger .debug (f"Changes between { prev_commit [:8 ]} and { current_commit [:8 ]} :" )
100- for f in changed [:10 ]:
129+ if subpath_prefix != "." :
130+ logger .debug (f" Scoped to subdirectory: { subpath_prefix } " )
131+ for f in filtered [:10 ]:
101132 logger .debug (f" { f } " )
102- if len (changed ) > 10 :
103- logger .debug (f" ... and { len (changed ) - 10 } more" )
133+ if len (filtered ) > 10 :
134+ logger .debug (f" ... and { len (filtered ) - 10 } more" )
104135
105- return changed
136+ return filtered
106137 except Exception as e :
107138 if verbose :
108139 logger .debug (f"Git diff failed: { e } — running full generation." )
0 commit comments