A Node.js tool to calculate lines of code contributed to GitHub repositories in a given year.
-
Install dependencies:
npm install
-
Configure environment variables:
# Copy the example environment file cp env.example .env # Edit .env with your GitHub credentials nano .env
-
Get your GitHub Personal Access Token:
- Go to GitHub Settings > Developer settings > Personal access tokens
- Generate a new token with the following scopes:
repo(Full control of private repositories)user(Update ALL user data)
- Copy the token to your
.envfile
-
Set your GitHub username:
- Add your GitHub username to the
.envfile
- Add your GitHub username to the
Run the analysis on all your repositories:
node gh-loc-report.jsRun the analysis on a specific repository:
# By repository name
node gh-loc-report.js my-repo-name
# By full repository name
node gh-loc-report.js username/my-repo-nameThe tool will:
- Fetch all your repositories (handles large numbers of repositories)
- Analyze commits for the specified year (default: 2025)
- Calculate lines of code added/deleted
- Track processing status for each repository
- Generate detailed JSON and summary text files in the
./reportsdirectory
| Variable | Required | Description |
|---|---|---|
GITHUB_TOKEN |
Yes | Your GitHub Personal Access Token |
GITHUB_USERNAME |
Yes | Your GitHub username |
ANALYSIS_YEAR |
No | Year to analyze (defaults to 2025) |
All output files are saved to the ./reports directory:
github-loc-{username}-{year}-{timestamp}.json- Detailed analysis resultsgithub-loc-summary-{username}-{year}-{timestamp}.txt- Human-readable summary
- All Repositories Mode: Analyzes all repositories in your GitHub account
- Single Repository Mode: Analyzes only the specified repository
- Flexible Repository Naming: Supports both repository name and full repository name (username/repo-name)
- Comprehensive Repository Discovery: Fetches all repositories across multiple pages (up to 1000 pages)
- Repository Search: Efficiently finds specific repositories by name or full name
- Processing Status Tracking: Tracks which repositories were successfully processed vs. failed
- Error Handling: Gracefully handles API errors and continues processing other repositories
- Progress Reporting: Shows real-time progress as repositories are processed
- Increased Commit Limits: Handles up to 5,000 commits per repository (vs. previous 2,000)
- Rate Limiting Protection: Built-in rate limiting to avoid GitHub API limits
- Large Commit Handling: Properly handles commits with 240,000+ lines of code
- Truncation Detection: Identifies and reports when commit data is truncated
- Smart File Exclusion: Automatically excludes generated files, lock files, build artifacts
- Source Code Focus: Only counts actual source code files
- File Type Statistics: Provides breakdown by file extension
- Retry Logic: Automatic retries for failed API calls
- Progress Tracking: Real-time progress updates during processing
- Memory Efficient: Processes repositories one at a time to manage memory usage
- Comprehensive Logging: Detailed logs for debugging and monitoring
Run the test suite:
npm testRun tests in watch mode:
npm run test:watchThe tool now tracks processing status for each repository:
- ✅ Successfully Processed: Repositories that were analyzed completely
- ❌ Failed to Process: Repositories that encountered errors (with error details)
- Progress Tracking: Real-time percentage completion
- Error Reporting: Detailed error messages for failed repositories
The script is optimized for repositories with:
- 240,000+ lines of code
- 5,000+ commits
- Multiple large files
- Complex file structures
The tool handles various error scenarios:
- API rate limiting
- Network timeouts
- Repository access issues
- Large commit processing
- File truncation
- Non-existent repository errors
All errors are logged and the tool continues processing other repositories.
ANALYSIS_YEAR=2024 node gh-loc-report.jsnode gh-loc-report.js my-awesome-projectnode gh-loc-report.js username/my-awesome-projectnode gh-loc-report.jsnode gh-loc-report.js github-loc-reportResults:
GitHub Lines of Code Analysis for bullishpip (2025)
============================================================
Analysis Mode: Single Repository
Total Lines Added: 1,361
Total Lines Deleted: 150
Net Lines of Code: 1,211
Total Commits: 7
Repositories Analyzed: 1
Total API Calls: 11
Processing Status:
✅ Successfully processed: 1 repositories
❌ Failed to process: 0 repositories
Top Repositories by Lines Added:
1. bullishpip/github-loc-report: +1,361 lines (0.0 MB)
This example shows the analysis of the github-loc-report project itself, demonstrating how the tool tracks lines of code added, deleted, and the net change over time.