<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Laba Kumar Deka</title>
    <description>The latest articles on DEV Community by Laba Kumar Deka (@laba_kumar_deka).</description>
    <link>https://dev.to/laba_kumar_deka</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2584007%2F43cdb2ce-be9b-4536-959d-985291748eca.jpg</url>
      <title>DEV Community: Laba Kumar Deka</title>
      <link>https://dev.to/laba_kumar_deka</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/laba_kumar_deka"/>
    <language>en</language>
    <item>
      <title>Shell Script to get the number of commits in all repositories present in a directory</title>
      <dc:creator>Laba Kumar Deka</dc:creator>
      <pubDate>Wed, 18 Dec 2024 00:27:51 +0000</pubDate>
      <link>https://dev.to/laba_kumar_deka/shell-script-to-get-the-number-of-commits-in-all-repositories-present-in-a-directory-4e06</link>
      <guid>https://dev.to/laba_kumar_deka/shell-script-to-get-the-number-of-commits-in-all-repositories-present-in-a-directory-4e06</guid>
      <description>&lt;p&gt;I am currently working as a grader for the Applied Internet Technology course at New York University, taught by the amazing Professor Joseph Versoza.&lt;br&gt;
My responsibilities include grading student projects and assignments, and other tasks as well.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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!&lt;/p&gt;

&lt;p&gt;The task basically involves writing some code to automate these things:&lt;/p&gt;

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

&lt;p&gt;First of all start by going through the documentation here&lt;br&gt;
Documentation — &lt;a href="https://learn.microsoft.com/en-us/powershell/" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/powershell/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I searched online for articles and resources as well ,have pasted relevant resources below.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;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.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# 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
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Next we save this script in the same parent directory as Listcommit.ps1&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;run it as .\Listcommit.ps1&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And that is it. You have your output on your screen.&lt;/p&gt;

&lt;p&gt;References :&lt;/p&gt;

&lt;p&gt;For getting the child folders : &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7.4" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7.4&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For running a for loop :&lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.4" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.4&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the output : &lt;a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-output?view=powershell-7.4" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-output?view=powershell-7.4&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Getting child items : &lt;a href="https://adamtheautomator.com/get-childitem/" rel="noopener noreferrer"&gt;https://adamtheautomator.com/get-childitem/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Powershell tasks automation : &lt;a href="https://www.ninjaone.com/blog/how-to-automate-tasks-with-powershell/" rel="noopener noreferrer"&gt;https://www.ninjaone.com/blog/how-to-automate-tasks-with-powershell/&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
