https://github.com/sonarsource/sonar-loc-count
https://github.com/sonarsource/sonar-loc-count
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/sonarsource/sonar-loc-count
- Owner: SonarSource
- License: lgpl-3.0
- Created: 2022-01-18T02:22:01.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2025-06-16T16:32:43.000Z (7 months ago)
- Last Synced: 2025-07-02T10:47:59.610Z (6 months ago)
- Language: PowerShell
- Size: 483 KB
- Stars: 45
- Watchers: 31
- Forks: 30
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README

## LoC Counting Scripts
This is a collection of shell scripts that demonstrate how to count lines of code from repositories and/or local directories.These scripts can be used to **estimate** LoC counts that would be produced by a Sonar analysis of these projects, without having to implement this analysis.
These scripts that connect to a DevOps platform must be run in a blank workspace (without data).
To exclude directories or files from the scan, you can insert the names of these exclusions into the **.clocignore** file in the environment where you run the scripts. Example :
> bootstrap
>
> test
* [Installation](#installation)
* [General usage](#general-usage)
* [Contributions and Feedbacks](#Contributions-and-feedbacks)

## Installation
Requirements:
* bash version 4+
* [Git](https://git-scm.com/)
* [curl](https://curl.haxx.se)
* [jq](https://stedolan.github.io/jq/)
* [cloc](https://github.com/AlDanial/cloc/releases/tag/v1.96) installed v1.96
* For Mac OSX you need gnu-sed (brew install gnu-sed)
## General usage
Most scripts will produce two reports of LoC by language (.lang) and by repository (.file).
### [github.com](https://github.com):
Counts lines of code from a GitHub.com organization. Requires to pass username, [personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) and the organization. The token must have repo scope.The script generates a report per project(File : ***ProjectName.txt***) that indicates the number of lines of code per branch and indicates the branch that has the highest number of lines of code.As well as a ***Report_global.txt*** file that indicates the maximum line of code on the repository.
```
github_com.sh myuser 1234567890abcdefgh myGitHubDotComOrg
```
or
```
github_com.sh myuser 1234567890abcdefgh myGitHubDotComOrg MyRepoName
```
### [bitbucket.org](https://bitbucket.org):
Counts lines of code from a Bitbucket.org organization. Requires to pass username, [App token password](https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/) and the workspace slug. The token must have Repositories Read permissions.The script generates a report per project(File : ***ProjectName.txt***) that indicates the number of lines of code per branch and indicates the branch that has the highest number of lines of code.As well as a ***Report_global.txt*** file that indicates the maximum line of code on the repository.
```
bitbucket_org.sh myuser 1234567890abcdefgh myBBWorkspace
```
or
```
myuser 1234567890abcdefgh myBBWorkspace MyProjectName
```
If you have more than 100 repos , you need to change Value of parameter page=Number_of_page on line 53
1 Page = 100 repos max
Example for 150 repos : GetAPI="repositories/$wks?pagelen=100&page=2"
### [Azure DevOps Services](https://dev.azure.com):
Counts lines of code from a Azure DevOps Services organization. Requires to pass [personal access token](https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops) and the organization. The token must have Code > Read permissions.
The script generates a report per project(File : ***ProjectName.txt***) that indicates the number of lines of code per branch and indicates the branch that has the highest number of lines of code.As well as a ***Report_global.txt*** file that indicates the maximum line of code on the repository.
```
azure_devops_services.sh 1234567890abcdefgh myADOOrg
```
or
```
azure_devops_services.sh 1234567890abcdefgh myADOOrg MyProjectName
```
### [Gitlab.com](https://gitlab.com):
Counts lines of code from a GitLab.com Group or Project. Requires to pass [personal access token](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html) and the group. The token must have read_api and read_repository scopes.The script generates a report per project(File : ***ProjectName.txt***) that indicates the number of lines of code per branch and indicates the branch that has the highest number of lines of code.As well as a ***Report_global.txt*** file that indicates the maximum line of code on the repository.
```
gitlab_com.sh 1234567890abcdefgh myGitLabGroupName
```
or
```
gitlab_com.sh 1234567890abcdefgh myGitLabGroupName/MyProjectName
```
If you have more than 100 repos , you need to change Value of parameter page=Number_of_page on line 58 or 61
1 Page = 100 repos max
Example for 150 repos : GetAPI="/projects/$groupname1?per_page=100&page=2"
### Local Filesystem:
Counts lines of code from a local directory or file. The script generates a report file : Report_***Name-of-Directory***.txt
```
PathToDirectory
```
Contributions and feedbacks
-------------
Contributions and feedbacks are welcome, as PRs or issues directly with this repository, or through your established Sonar communication channel.