44Compares metrics between uncommitted files and indexed files.
55"""
66
7+ from collections import defaultdict
78import os
89import sys
910from collections .abc import Iterable
1213
1314from 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
1718from wily .cache import get_default_metrics_path
1819from wily .commands .build import run_operators_parallel
1920from 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