Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- ⏰ Peak coding hours analysis
- 👥 Contributor insights
- 🎨 Beautiful terminal output
- 📤 JSON output for scripting

## 🚀 Installation

Expand All @@ -36,6 +37,9 @@ gitstats stats

# Show stats for a specific repo
gitstats stats /path/to/repo

# Output as JSON (for scripting)
gitstats stats --json
```

## 🛠️ Development
Expand Down
51 changes: 39 additions & 12 deletions gitstats/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,57 @@ def main(
@app.command()
def stats(
path: str = typer.Argument(".", help="Path to git repository"),
json_output: bool = typer.Option(
False,
"--json",
"-j",
help="Output statistics as JSON",
),
) -> None:
"""Show commit statistics for a git repository."""
from gitstats.parser import get_commit_stats
import json

console.print(f"\n[bold]📊 Git Statistics for:[/] [cyan]{path}[/]\n")
from gitstats.parser import get_commit_stats, get_hourly_activity, get_weekly_activity

stats = get_commit_stats(path)
stats_data = get_commit_stats(path)

if not stats:
console.print("[red]No commits found or not a git repository.[/]")
if not stats_data:
if json_output:
console.print(json.dumps({"error": "No commits found or not a git repository"}))
else:
console.print("[red]No commits found or not a git repository.[/]")
raise typer.Exit(1)

console.print(f"[bold]Total commits:[/] [green]{stats['total_commits']}[/]")
console.print(f"[bold]Contributors:[/] [green]{stats['total_authors']}[/]")
console.print(f"[bold]First commit:[/] [yellow]{stats['first_commit']}[/]")
console.print(f"[bold]Latest commit:[/] [yellow]{stats['last_commit']}[/]")
if json_output:
# Build JSON output
output = {
"repository": path,
"total_commits": stats_data["total_commits"],
"total_authors": stats_data["total_authors"],
"first_commit": stats_data["first_commit"],
"last_commit": stats_data["last_commit"],
"authors": stats_data["author_stats"],
"weekly_activity": get_weekly_activity(stats_data["commits"]),
"hourly_activity": get_hourly_activity(stats_data["commits"]),
}
console.print(json.dumps(output, indent=2))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rich console.print corrupts machine-readable JSON output

High Severity

Using Rich's console.print for JSON output intended for machine consumption (jq, scripting) is incorrect. Rich's Console.print applies syntax highlighting (ANSI escape codes in terminal), soft-wraps text to the terminal/default width (breaking long JSON lines), and interprets Rich markup (square brackets in JSON values could be misinterpreted). All three behaviors can produce invalid JSON. Python's built-in print() is the appropriate choice here.

Additional Locations (1)

Fix in Cursor Fix in Web

return

# Pretty terminal output
console.print(f"\n[bold]📊 Git Statistics for:[/] [cyan]{path}[/]\n")

console.print(f"[bold]Total commits:[/] [green]{stats_data['total_commits']}[/]")
console.print(f"[bold]Contributors:[/] [green]{stats_data['total_authors']}[/]")
console.print(f"[bold]First commit:[/] [yellow]{stats_data['first_commit']}[/]")
console.print(f"[bold]Latest commit:[/] [yellow]{stats_data['last_commit']}[/]")
console.print()

# Show author breakdown table
if stats.get("author_stats"):
_print_author_table(stats["author_stats"])
if stats_data.get("author_stats"):
_print_author_table(stats_data["author_stats"])

# Show activity heatmap
_print_activity_heatmap(stats["commits"])
_print_activity_heatmap(stats_data["commits"])


def _print_author_table(author_stats: list[dict]) -> None:
Expand Down
Loading