Skip to content

Commit 2144b37

Browse files
authored
Merge pull request #67 from p0thi/feature/compare-to-override
feat: Add --compare-to option to override commit hash for incremental updates
2 parents 50563c5 + ec40a82 commit 2144b37

2 files changed

Lines changed: 36 additions & 16 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ codewiki generate --create-branch --github-pages --verbose
192192

193193
# Incremental update (only regenerate changed modules since last run)
194194
codewiki generate --update
195+
196+
# Incremental update using a specific commit hash to compare against (useful in CI/CD or squashed PRs)
197+
# This overrides the stored commit hash in metadata.json and implicitly enables --update
198+
codewiki generate --compare-to <commit-hash>
195199
```
196200

197201
### Customization Options

codewiki/cli/commands/generate.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def _detect_changed_files(
4343
repo_path: Path,
4444
output_dir: Path,
4545
logger,
46-
verbose: bool
46+
verbose: bool,
47+
compare_to: Optional[str] = None
4748
) -> Optional[List[str]]:
4849
"""
4950
Detect files changed since the last documentation generation.
@@ -57,21 +58,25 @@ def _detect_changed_files(
5758
"""
5859
import json
5960

60-
metadata_path = output_dir / "metadata.json"
61-
if not metadata_path.exists():
62-
if verbose:
63-
logger.debug("No metadata.json found — cannot detect changes, running full generation.")
64-
return None
65-
66-
try:
67-
metadata = json.loads(metadata_path.read_text())
68-
prev_commit = metadata.get("generation_info", {}).get("commit_id")
69-
if not prev_commit:
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():
7067
if verbose:
71-
logger.debug("No commit_id in metadata — running full generation.")
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):
7279
return None
73-
except (json.JSONDecodeError, OSError):
74-
return None
7580

7681
# Get current HEAD commit
7782
try:
@@ -295,6 +300,12 @@ def _find_affected(tree, parent_names=None):
295300
is_flag=True,
296301
help="Incremental update: only regenerate modules affected by changes since last generation",
297302
)
303+
@click.option(
304+
"--compare-to",
305+
type=str,
306+
default=None,
307+
help="Commit hash to compare against for incremental updates (overrides stored commit in metadata.json)",
308+
)
298309
@click.pass_context
299310
def generate_command(
300311
ctx,
@@ -312,7 +323,8 @@ def generate_command(
312323
max_token_per_module: Optional[int],
313324
max_token_per_leaf_module: Optional[int],
314325
max_depth: Optional[int],
315-
update: bool = False
326+
update: bool = False,
327+
compare_to: Optional[str] = None
316328
):
317329
"""
318330
Generate comprehensive documentation for a code repository.
@@ -416,10 +428,14 @@ def generate_command(
416428

417429
logger.success(f"Output directory: {output_dir}")
418430

431+
# If a base commit is specified to compare against, implicitly enable update
432+
if compare_to:
433+
update = True
434+
419435
# Incremental update: detect changed files and selectively regenerate
420436
changed_files = None
421437
if update and output_dir.exists():
422-
changed_files = _detect_changed_files(repo_path, output_dir, logger, verbose)
438+
changed_files = _detect_changed_files(repo_path, output_dir, logger, verbose, compare_to=compare_to)
423439
if changed_files is not None and len(changed_files) == 0:
424440
logger.success("No changes detected since last generation. Documentation is up to date.")
425441
sys.exit(EXIT_SUCCESS)

0 commit comments

Comments
 (0)