Comparing Projects and Generating Reports
CLOC provides features for comparing different projects and generating reports in various formats. These capabilities are particularly useful for tracking changes over time or comparing different codebases.
Comparing Two Directories
Let's create a simple project to compare with Flask:
- Navigate to the project directory:
cd ~/project
- Create a new directory for a simple Python project:
mkdir sample_project
cd sample_project
- Create a few Python files with some code:
echo 'def hello_world():
"""
A simple function that prints Hello World
"""
print("Hello, World!")
if __name__ == "__main__":
hello_world()' > main.py
echo 'class Calculator:
"""A simple calculator class"""
def add(self, a, b):
"""Add two numbers"""
return a + b
def subtract(self, a, b):
"""Subtract b from a"""
return a - b' > calculator.py
- Now, let's compare this sample project with the Flask project using CLOC's diff feature:
cd ~/project
cloc --diff flask sample_project
The output will show the difference in code metrics between the two projects:
2 text files.
2 unique files.
0 files ignored.
github.com/AlDanial/cloc v 1.90 T=0.01 s (195.2 files/s, 1756.8 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
Python 2 3 4 11
-------------------------------------------------------------------------------
SUM: 2 3 4 11
-------------------------------------------------------------------------------
Diff by file type:
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
Python 39 3058 2084 7001
Markdown 5 175 0 314
YAML 2 10 3 84
make 1 21 30 46
TOML 1 6 0 19
-------------------------------------------------------------------------------
SUM: 48 3270 2117 7464
-------------------------------------------------------------------------------
CLOC can generate reports in various formats, including CSV and XML. Let's create a CSV report for the Flask project:
- Navigate to the Flask project:
cd ~/project/flask
- Generate a CSV report:
cloc --csv --out=flask_stats.csv .
- View the contents of the generated report:
cat flask_stats.csv
You should see the CLOC analysis in CSV format:
files,language,blank,comment,code,"github.com/AlDanial/cloc v 1.90 T=0.09 s (571.3 files/s, 96263.8 lines/s)"
41,Python,3061,2088,7012
5,Markdown,175,0,314
2,YAML,10,3,84
1,make,21,30,46
1,TOML,6,0,19
50,SUM,3273,2121,7475
This CSV format is particularly useful for importing into spreadsheets or other data analysis tools.
Similarly, you can generate an XML report:
cloc --xml --out=flask_stats.xml .
These reporting capabilities make CLOC a versatile tool for code analysis and project management.