Lines of Code Tracking
Relevant source files
The following files were used as context for generating this wiki page:
Lines of Code (LoC) tracking is a supplementary feature of the Duplicate Code Detection Tool that provides quantitative context for detected similarities. While the primary goal of the tool is identifying duplication percentages, tracking LoC allows developers to assess the magnitude of duplication (e.g., distinguishing between a 90% match in a 10-line file versus a 500-line file).
The system calculates total line counts for analyzed files, including blank lines and comments, unless specifically configured to strip non-code elements. This data is integrated into reports across CLI, library, and CI/CD interfaces.
Sources: README.md:12, duplicate_code_detection.py:126-127
Core Implementation Logic
The tracking mechanism relies on a simple file-reading utility that counts newlines within source files. This process is triggered conditionally based on user configuration.
Line Counting Mechanism
The function get_loc_count serves as the primary engine for this feature. It opens a file path and calculates the length of the list returned by readlines(). If a file cannot be read, it logs a warning and returns -1 to signify an error.
Sources: duplicate_code_detection.py:99-107
The flow shows how the system handles file reading for LoC calculation.
Sources: duplicate_code_detection.py:101-106
Configuration & Control
LoC tracking is controlled by the show_loc parameter. When enabled, the tool includes line counts in its internal data structures and final output formats.
| Configuration Element | Type | Default | Description |
|---|---|---|---|
show_loc | boolean | True (Library) | Toggle for including LoC in analysis and reports. |
show-loc | CLI flag | N/A | Enables LoC display in command line output. |
only_code | boolean | False | When True, docstrings and comments are removed from Python files before analysis, affecting perceived LoC. |
Sources: duplicate_code_detection.py:182-186, core.py:118, README.md:200
Data Structures for LoC Tracking
The library encapsulates LoC data within several dataclasses to maintain traceability throughout the analysis pipeline.
Library Classes
FileSimilarity: Storessource_locandtarget_locto provide context for a specific pairwise comparison.SimilarityReport: Contains aloc_countfield representing the total lines in the specific source file being analyzed.
Sources: core.py:32-35, core.py:53
The diagram illustrates how LoC data is nested within the reporting hierarchy.
Sources: core.py:30-65
Integration with Output Formats
LoC information is propagated to various output channels when the feature is enabled.
JSON and CSV Output
In JSON output, the LoC is represented by the #LoC key. In CSV output, specific columns (#LoC A and #LoC B) are added to the headers to accommodate the data.
Sources: duplicate_code_detection.py:270-280, core.py:165-170
CLI and Console Reporting
When using the CLI, enabling LoC tracking modifies the table headers to include ,#LoC. The tool calculates the largest string length to ensure the text-based table remains aligned despite the added numeric data.
Sources: duplicate_code_detection.py:202-205, duplicate_code_detection.py:217-220
CI/CD Integration (GitHub Actions)
The tool supports GitHub Actions through action.yml and run_action.py. While the current GitHub Action runner defaults show_loc to False in its internal logic, the underlying engine supports the data if the duplicate_code_detection.run function is invoked with the flag enabled.
Sources: run_action.py:145, action.yml:1-55
Usage Example
Developers using the tool as a library can enable LoC tracking during initialization:
from duplicate_code_tool import DuplicateCodeDetector
# Initialize with LoC tracking enabled
detector = DuplicateCodeDetector(
show_loc=True,
only_code=True # Strips comments before LoC/Similarity calculation
)
# Analysis will now include FileSimilarity.source_loc and FileSimilarity.target_loc
result = detector.detect_in_directories(["src"])
Sources: examples/flext_integration_example.py:48-55, core.py:126-127
Summary
Lines of Code Tracking provides critical scale information for code duplication analysis. By integrating with the core detection logic in duplicate_code_detection.py and being surfaced through the core.py dataclasses, it allows users to prioritize refactoring efforts based on the volume of redundant code identified.
Sources: duplicate_code_detection.py:10-25, core.py:155-160