Horje
How to Count number of Lines in a Git Repository?

Counting the number of lines in a Git repository can be useful for various reasons, such as understanding the size of the project or monitoring codebase growth over time. There are several methods to achieve this, including using Git command line tools, the CLOC (Count Lines of Code) tool, and custom scripts.

Approach 1: Using Git Command Line Tools

Git provides various commands that can help you count the number of lines in a repository. Here’s how you can do it using basic Unix command line tools along with Git

Step 1: Clone the Repository

The first step is to make the repo. available on your local machine and this can be done by using the commands given below .

git clone <repository_url>
cd <repository_name>
git-clone-repo

Making git repo available on local machine

Step 2 : Count Lines Using “git ls-files” and “wc”

Run the command given below to count the number of lines in all the files tracked by Git

git ls-files | xargs wc -l
  • git ls-files: Lists all files in the repository.
  • xargs wc -l: Passes the list of files to the wc command to count the number of lines in each file.
git-ls-files---xargs-wc--l

git ls-files | xargs wc -l

Step 3: Count Only Specific File Types

You can filter files by extension, for example, counting only .js and .jsx files:

git ls-files '*.js' '*.jsx' | xargs wc -l

Approach 2: Using the CLOC Tool

CLOC (Count Lines of Code) is a popular tool specifically designed to count lines of code in various programming languages. It provides more detailed statistics compared to basic line counting.

Step 1 : Install cloc

Install cloc using a package manager or download it from its official website.

npm install -g cloc 
# On windows using npm

sudo apt-get install cloc
# On Debian/Ubuntu

brew install cloc
# On macOS using Homebrew

Step 2 : Run cloc on the Repository

Navigate to the repo using cd <repository_directory> . If you haven’t cloned it on your local machine , use command git clone <repository_url> . Once you are in the rep directory , use the command given below

cloc .
cloc-

cloc .

Approach 3: Using Scripts

You can also use custom scripts to count the number of lines in a Git repository. Here we have shown an example using a Python script.

Step 1 :Create the Python Script

Write down the “count_lines.py” file and save it in the git repo. on your local machine.

Python
import os  
import subprocess

def count_lines_of_code():
  """
  This function counts the total number of lines of code in a Git repository.

  It retrieves a list of all tracked files using `git ls-files` and then iterates
  over each file, counting all lines within it.

  **Note:** This method counts all lines, including comments and blank lines.
  """

  # Get list of files tracked by the Git repository
  result = subprocess.run(['git', 'ls-files'], capture_output=True, text=True)
  files = result.stdout.splitlines()

  total_lines = 0
  for file in files:
    # Open the file in read mode, ignoring any encoding errors
    with open(file, 'r', errors='ignore') as f:
      # Concise way to count lines using a generator expression
      total_lines += sum(1 for _ in f)

  # Print the total number of lines found
  print(f"Total lines of code: {total_lines}")

if __name__ == "__main__":
  count_lines_of_code()

Output
Total lines of code: 0

Step 2: Run “count_lines.py” on the Repository

Navigate to the repo using cd <repository_directory> . If you haven’t cloned it on your local machine , use command git clone <repository_url> . Once you are in the rep directory , use the command given below

python count_lines.py
Using-python-script

python count_lines.py

Conclusion

Counting the number of lines in a Git repository can be done in various ways, from using basic command-line tools to more sophisticated third-party tools and custom scripts using python. Each method has its own advantages, so you can choose the one that best fits your needs and is flexible for you . Be it a small project or a large codebase, understanding the size and complexity of your code can provide valuable insights and conclusions that in turn helps you to maintain the quality of your software.




Reffered: https://www.geeksforgeeks.org


Git

Related
How To Change the Remote a Branch is Tracking in Git? How To Change the Remote a Branch is Tracking in Git?
Create a Git Patch from Uncommitted Changes Create a Git Patch from Uncommitted Changes
Top 50 Git Interview Questions and Answers (2024) Top 50 Git Interview Questions and Answers (2024)
How to Pip Install From a Git Repo Branch? How to Pip Install From a Git Repo Branch?
How To Use Multiple Users With Git? How To Use Multiple Users With Git?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
18