Skip to content

Commit 51be3ac

Browse files
committed
Compare each file against the last time it was indexed
1 parent 3ec692a commit 51be3ac

2 files changed

Lines changed: 10 additions & 52 deletions

File tree

src/wily/__main__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,6 @@ def report(ctx, file, metrics: tuple[str, ...] | None, number, message, format,
327327
default=True,
328328
help=_("Show function/class level metrics where available"),
329329
)
330-
@click.option("-r", "--revision", help=_("Compare against specific revision"), type=click.STRING)
331330
@click.option(
332331
"-w",
333332
"--wrap/--no-wrap",
@@ -340,7 +339,7 @@ def report(ctx, file, metrics: tuple[str, ...] | None, number, message, format,
340339
help=_("Table style (ROUNDED, SIMPLE, HEAVY, DOUBLE, MINIMAL, ASCII, etc.)"),
341340
)
342341
@click.pass_context
343-
def diff(ctx, files, metrics, all, detail, revision, wrap, table_style):
342+
def diff(ctx, files, metrics, all, detail, wrap, table_style):
344343
"""Show the differences in metrics for each file."""
345344
config = ctx.obj["CONFIG"]
346345

@@ -361,7 +360,6 @@ def diff(ctx, files, metrics, all, detail, revision, wrap, table_style):
361360
metrics=metrics_list,
362361
changes_only=not all,
363362
detail=detail,
364-
revision=revision,
365363
wrap=wrap,
366364
table_style=table_style,
367365
)

src/wily/commands/diff.py

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Compares metrics between uncommitted files and indexed files.
55
"""
66

7+
from collections import defaultdict
78
import os
89
import sys
910
from collections.abc import Iterable
@@ -12,8 +13,8 @@
1213

1314
from rich.text import Text
1415

15-
from wily import format_date, format_revision, logger
16-
from wily.backend import WilyIndex, find_revision, iter_filenames
16+
from wily import logger
17+
from wily.backend import WilyIndex, iter_filenames
1718
from wily.cache import get_default_metrics_path
1819
from wily.commands.build import run_operators_parallel
1920
from wily.config import DEFAULT_PATH
@@ -40,7 +41,6 @@ def diff( # noqa: C901
4041
metrics: list[str] | None,
4142
changes_only: bool = True,
4243
detail: bool = True,
43-
revision: str | None = None,
4444
wrap: bool = False,
4545
table_style: str = DEFAULT_TABLE_STYLE,
4646
) -> None:
@@ -86,57 +86,17 @@ def diff( # noqa: C901
8686
resolved_metrics = [(operator.name, metric) for operator, metric in ALL_METRICS if operator in operators]
8787

8888
operator_names = [op.name for op in operators]
89-
89+
last_data: dict[str, dict[str, Any]] = defaultdict(dict)
9090
# Load the index and find target revision
9191
with WilyIndex(parquet_path, operator_names) as index:
92-
# Find the target revision to compare against
93-
if not revision:
94-
# Get the most recent revision from the index
95-
all_rows = list(index)
96-
if not all_rows:
97-
logger.error("No data in cache. Run 'wily build' first.")
98-
sys.exit(1)
99-
# Sort by date descending to get most recent
100-
all_rows.sort(key=lambda r: r.get("revision_date", 0), reverse=True)
101-
target_revision_key = all_rows[0]["revision"]
102-
target_revision_author = all_rows[0].get("revision_author", "Unknown")
103-
target_revision_date = all_rows[0].get("revision_date", 0)
104-
else:
105-
# Resolve revision using git
106-
rev_data = find_revision(config.path, revision)
107-
if not rev_data:
108-
logger.error("Revision %s not found in git.", revision)
109-
sys.exit(1)
110-
target_revision_key = rev_data["key"]
111-
target_revision_author = rev_data.get("author_name", "Unknown")
112-
target_revision_date = rev_data.get("date", 0)
113-
# Verify it's in the cache
114-
all_rows = list(index)
115-
if not any(r["revision"] == target_revision_key for r in all_rows):
116-
logger.error(
117-
"Revision %s is not in the cache, make sure you have run wily build.",
118-
revision,
119-
)
120-
sys.exit(1)
121-
122-
logger.info(
123-
"Comparing current with %s by %s on %s.",
124-
format_revision(target_revision_key),
125-
target_revision_author,
126-
format_date(target_revision_date),
127-
)
128-
12992
# Build lookup of cached metrics for target revision: {path: {metric: value}}
130-
cached_data: dict[str, dict[str, Any]] = {}
131-
for row in index:
132-
if row["revision"] == target_revision_key:
133-
path = row["path"]
134-
if path not in cached_data:
135-
cached_data[path] = {}
93+
for file in files:
94+
for row in index[file]:
13695
# Copy all metric values
13796
for key, value in row.items():
13897
if key not in ("revision", "revision_date", "revision_author", "revision_message", "path", "path_type"):
139-
cached_data[path][key] = value
98+
last_data[file][key] = value
99+
break
140100

141101
# Run operators on current files
142102
data = run_operators_parallel(operators, targets, config)
@@ -163,7 +123,7 @@ def diff( # noqa: C901
163123
for operator, metric in resolved_metrics:
164124
# Get cached value for this file/metric (parquet stores just metric name, not operator.metric)
165125
try:
166-
current = cached_data.get(file, {}).get(metric.name, "-")
126+
current = last_data.get(file, {}).get(metric.name, "-")
167127
if current is None:
168128
current = "-"
169129
except KeyError:

0 commit comments

Comments
 (0)