File size: 12,483 Bytes
8755993
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
"""
Merkle Tree implementation for efficient codebase change detection.

Inspired by Cursor's approach to incremental indexing, this module builds
a cryptographic hash tree of the codebase to quickly identify which files
have changed since the last indexing operation.
"""

import hashlib
import json
import logging
import os
from dataclasses import dataclass, asdict
from pathlib import Path
from typing import Dict, List, Optional, Set
from datetime import datetime

logger = logging.getLogger(__name__)


@dataclass
class MerkleNode:
    """Represents a node in the Merkle tree (file or directory)."""
    
    path: str  # Relative path from root
    hash: str  # SHA-256 hash of content (or combined child hashes for directories)
    is_directory: bool
    size: int = 0  # File size in bytes (0 for directories)
    modified_time: Optional[str] = None  # ISO format timestamp
    children: Optional[List['MerkleNode']] = None
    
    def to_dict(self) -> Dict:
        """Convert to dictionary for JSON serialization."""
        result = {
            'path': self.path,
            'hash': self.hash,
            'is_directory': self.is_directory,
            'size': self.size,
            'modified_time': self.modified_time,
        }
        if self.children:
            result['children'] = [child.to_dict() for child in self.children]
        return result
    
    @classmethod
    def from_dict(cls, data: Dict) -> 'MerkleNode':
        """Create MerkleNode from dictionary."""
        children = None
        if 'children' in data and data['children']:
            children = [cls.from_dict(child) for child in data['children']]
        
        return cls(
            path=data['path'],
            hash=data['hash'],
            is_directory=data['is_directory'],
            size=data.get('size', 0),
            modified_time=data.get('modified_time'),
            children=children
        )


@dataclass
class ChangeSet:
    """Represents changes detected between two Merkle trees."""
    
    added: List[str]  # New files
    modified: List[str]  # Changed files
    deleted: List[str]  # Removed files
    unchanged: List[str]  # Files that haven't changed
    
    def has_changes(self) -> bool:
        """Check if there are any changes."""
        return bool(self.added or self.modified or self.deleted)
    
    def total_changes(self) -> int:
        """Total number of changed files."""
        return len(self.added) + len(self.modified) + len(self.deleted)
    
    def summary(self) -> str:
        """Human-readable summary of changes."""
        return (
            f"Added: {len(self.added)}, "
            f"Modified: {len(self.modified)}, "
            f"Deleted: {len(self.deleted)}, "
            f"Unchanged: {len(self.unchanged)}"
        )


class MerkleTree:
    """
    Builds and compares Merkle trees for efficient change detection.
    
    The tree structure mirrors the directory structure, with each node
    containing a hash of its content (for files) or combined child hashes
    (for directories). This allows quick identification of changes.
    """
    
    # File extensions to ignore
    IGNORE_EXTENSIONS = {
        '.pyc', '.pyo', '.pyd', '.so', '.dll', '.dylib',
        '.class', '.o', '.obj', '.exe', '.bin',
        '.git', '.svn', '.hg', '.DS_Store',
        '__pycache__', 'node_modules', '.venv', 'venv',
        '.egg-info', 'dist', 'build', '.pytest_cache',
        '.mypy_cache', '.tox', 'coverage', '.coverage'
    }
    
    def __init__(self, ignore_patterns: Optional[List[str]] = None):
        """
        Initialize Merkle tree builder.
        
        Args:
            ignore_patterns: Additional patterns to ignore (e.g., ['*.log', 'temp/*'])
        """
        self.ignore_patterns = ignore_patterns or []
    
    def _should_ignore(self, path: Path) -> bool:
        """Check if a path should be ignored."""
        # Check if any part of the path matches ignore extensions
        for part in path.parts:
            if part in self.IGNORE_EXTENSIONS:
                return True
        
        # Check file extension
        if path.suffix in self.IGNORE_EXTENSIONS:
            return True
        
        # Check custom patterns
        for pattern in self.ignore_patterns:
            if path.match(pattern):
                return True
        
        return False
    
    def _hash_file(self, file_path: Path) -> str:
        """
        Compute SHA-256 hash of a file's content.
        
        Args:
            file_path: Path to the file
            
        Returns:
            Hexadecimal hash string
        """
        sha256 = hashlib.sha256()
        try:
            with open(file_path, 'rb') as f:
                # Read in chunks to handle large files
                for chunk in iter(lambda: f.read(8192), b''):
                    sha256.update(chunk)
            return sha256.hexdigest()
        except Exception as e:
            logger.warning(f"Failed to hash file {file_path}: {e}")
            # Return a hash of the error message to ensure consistency
            return hashlib.sha256(str(e).encode()).hexdigest()
    
    def _hash_directory(self, children: List[MerkleNode]) -> str:
        """
        Compute hash for a directory based on its children.
        
        Args:
            children: List of child MerkleNodes
            
        Returns:
            Combined hash of all children
        """
        # Sort children by path for consistency
        sorted_children = sorted(children, key=lambda x: x.path)
        
        # Combine all child hashes
        combined = ''.join(child.hash for child in sorted_children)
        
        return hashlib.sha256(combined.encode()).hexdigest()
    
    def build_tree(self, root_path: str) -> MerkleNode:
        """
        Build a Merkle tree for the given directory.
        
        Args:
            root_path: Root directory to build tree from
            
        Returns:
            Root MerkleNode of the tree
        """
        root = Path(root_path).resolve()
        
        if not root.exists():
            raise ValueError(f"Path does not exist: {root_path}")
        
        logger.info(f"Building Merkle tree for: {root}")
        return self._build_node(root, root)
    
    def _build_node(self, path: Path, root: Path) -> MerkleNode:
        """
        Recursively build a MerkleNode for a path.
        
        Args:
            path: Current path to process
            root: Root directory (for computing relative paths)
            
        Returns:
            MerkleNode for this path
        """
        relative_path = str(path.relative_to(root))
        
        if path.is_file():
            # File node
            stat = path.stat()
            return MerkleNode(
                path=relative_path,
                hash=self._hash_file(path),
                is_directory=False,
                size=stat.st_size,
                modified_time=datetime.fromtimestamp(stat.st_mtime).isoformat(),
                children=None
            )
        else:
            # Directory node
            children = []
            try:
                for child_path in sorted(path.iterdir()):
                    if self._should_ignore(child_path):
                        continue
                    
                    child_node = self._build_node(child_path, root)
                    children.append(child_node)
            except PermissionError:
                logger.warning(f"Permission denied: {path}")
            
            return MerkleNode(
                path=relative_path,
                hash=self._hash_directory(children),
                is_directory=True,
                size=0,
                modified_time=None,
                children=children
            )
    
    def compare_trees(self, old_tree: Optional[MerkleNode], new_tree: MerkleNode) -> ChangeSet:
        """
        Compare two Merkle trees to find changes.
        
        Args:
            old_tree: Previous tree snapshot (None if first time)
            new_tree: Current tree snapshot
            
        Returns:
            ChangeSet describing all changes
        """
        if old_tree is None:
            # First time indexing - all files are new
            all_files = self._collect_all_files(new_tree)
            return ChangeSet(
                added=all_files,
                modified=[],
                deleted=[],
                unchanged=[]
            )
        
        added: List[str] = []
        modified: List[str] = []
        deleted: List[str] = []
        unchanged: List[str] = []
        
        # Build path->node maps for efficient lookup
        old_files = self._build_file_map(old_tree)
        new_files = self._build_file_map(new_tree)
        
        # Find added and modified files
        for path, new_node in new_files.items():
            if path not in old_files:
                added.append(path)
            elif old_files[path].hash != new_node.hash:
                modified.append(path)
            else:
                unchanged.append(path)
        
        # Find deleted files
        for path in old_files:
            if path not in new_files:
                deleted.append(path)
        
        change_set = ChangeSet(
            added=sorted(added),
            modified=sorted(modified),
            deleted=sorted(deleted),
            unchanged=sorted(unchanged)
        )
        
        logger.info(f"Change detection complete: {change_set.summary()}")
        return change_set
    
    def _collect_all_files(self, node: MerkleNode) -> List[str]:
        """Collect all file paths from a tree."""
        files = []
        
        if not node.is_directory:
            files.append(node.path)
        elif node.children:
            for child in node.children:
                files.extend(self._collect_all_files(child))
        
        return files
    
    def _build_file_map(self, node: MerkleNode) -> Dict[str, MerkleNode]:
        """Build a map of file paths to nodes."""
        file_map = {}
        
        if not node.is_directory:
            file_map[node.path] = node
        elif node.children:
            for child in node.children:
                file_map.update(self._build_file_map(child))
        
        return file_map
    
    def save_snapshot(self, tree: MerkleNode, snapshot_path: str):
        """
        Save a Merkle tree snapshot to disk.
        
        Args:
            tree: MerkleNode to save
            snapshot_path: Path to save the snapshot JSON file
        """
        snapshot_file = Path(snapshot_path)
        snapshot_file.parent.mkdir(parents=True, exist_ok=True)
        
        with open(snapshot_file, 'w') as f:
            json.dump(tree.to_dict(), f, indent=2)
        
        logger.info(f"Saved Merkle tree snapshot to: {snapshot_path}")
    
    def load_snapshot(self, snapshot_path: str) -> Optional[MerkleNode]:
        """
        Load a Merkle tree snapshot from disk.
        
        Args:
            snapshot_path: Path to the snapshot JSON file
            
        Returns:
            MerkleNode or None if snapshot doesn't exist
        """
        snapshot_file = Path(snapshot_path)
        
        if not snapshot_file.exists():
            logger.info(f"No snapshot found at: {snapshot_path}")
            return None
        
        try:
            with open(snapshot_file, 'r') as f:
                data = json.load(f)
            
            tree = MerkleNode.from_dict(data)
            logger.info(f"Loaded Merkle tree snapshot from: {snapshot_path}")
            return tree
        except Exception as e:
            logger.error(f"Failed to load snapshot: {e}")
            return None


def get_changed_files(root_path: str, snapshot_path: str) -> ChangeSet:
    """
    Convenience function to detect changes since last snapshot.
    
    Args:
        root_path: Root directory of codebase
        snapshot_path: Path to previous snapshot file
        
    Returns:
        ChangeSet describing all changes
    """
    merkle = MerkleTree()
    
    # Load previous snapshot
    old_tree = merkle.load_snapshot(snapshot_path)
    
    # Build current tree
    new_tree = merkle.build_tree(root_path)
    
    # Compare
    changes = merkle.compare_trees(old_tree, new_tree)
    
    # Save new snapshot
    merkle.save_snapshot(new_tree, snapshot_path)
    
    return changes