DEV Community

Laba Kumar Deka
Laba Kumar Deka

Posted on

Shell Script to get the number of commits in all repositories present in a directory

I am currently working as a grader for the Applied Internet Technology course at New York University, taught by the amazing Professor Joseph Versoza.
My responsibilities include grading student projects and assignments, and other tasks as well.

One of the more tedious tasks I face as a grader is checking the number of commits made for a specific homework. While it’s straightforward to get this information using a single command, running it for every student individually was becoming incredibly time-consuming and quite boring honestly.

Since I was already experimenting with Bash scripts on Linux, I thought about creating a similar solution for Windows. As a Windows user, I figured this should be possible — and it turns out, it is!

The task basically involves writing some code to automate these things:

Retrieve all subfolders within a directory.
Navigate into each folder.
Run the required Git commands in each folder automatically.
So here are the steps I had taken to do this, and the steps to run the code too is given below.

First of all start by going through the documentation here
Documentation — https://learn.microsoft.com/en-us/powershell/

I searched online for articles and resources as well ,have pasted relevant resources below.

  1. Here is the final code. It is a simple for loop that goes through each folder one by one. First of all it checks if that repo is a git repo or not, then once that is confirmed it gives the code for finding total commits and last 4 commits.
# This is needed to get all the subfolders in the current directory
Get-ChildItem -Directory | ForEach-Object {
    $folderPath = $_.FullName
    # First of all we need to check if it is even a git repo or not
    if (Test-Path "$folderPath\.git") {
        Write-Output "Folder: $($_.Name)"
        # This is the code to get the total number of commits
        $totalCommits = git -C $folderPath rev-list --all --count
        Write-Output "Total Commits: $totalCommits"
        # Here we get the last 4 commits with hash and detailed date (date and time)
        Write-Output "Last 4 commits are:"
        git -C $folderPath log -n 4 --pretty=format:"  Commit %h - %ad" --date=iso
        Write-Output ""  # For readabillity
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. Next we save this script in the same parent directory as Listcommit.ps1

  2. run it as .\Listcommit.ps1

And that is it. You have your output on your screen.

References :

For getting the child folders : https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7.4

For running a for loop :https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.4

For the output : https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-output?view=powershell-7.4

Getting child items : https://adamtheautomator.com/get-childitem/

Powershell tasks automation : https://www.ninjaone.com/blog/how-to-automate-tasks-with-powershell/

Top comments (0)