From 5295bbc1e12b59415f6557e8d5e0e0dd7b09cee9 Mon Sep 17 00:00:00 2001 From: Bluzzi Date: Tue, 9 Jan 2024 23:22:02 +0100 Subject: [PATCH] feat(Get-Stats.Cmd.ps1): add PowerShell script to retrieve git statistics The PowerShell script `Get-Stats.Cmd.ps1` has been added to the `Core/Commands/Get-Stats` directory. This script retrieves git statistics using the `git log` command with the `--numstat` and `--pretty=format:` options. It parses the output to extract the number of additions, deletions, and the filename for each commit. The script then groups the statistics by filename and calculates the total additions and deletions for each file. Finally, it sorts the statistics in descending order based on the total additions and deletions and selects the top N results specified by the `$Top` parameter. --- Core/Commands/Get-Stats/Get-Stats.Cmd.ps1 | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Core/Commands/Get-Stats/Get-Stats.Cmd.ps1 diff --git a/Core/Commands/Get-Stats/Get-Stats.Cmd.ps1 b/Core/Commands/Get-Stats/Get-Stats.Cmd.ps1 new file mode 100644 index 0000000..188ba10 --- /dev/null +++ b/Core/Commands/Get-Stats/Get-Stats.Cmd.ps1 @@ -0,0 +1,29 @@ +function Get-Stats { + param( + [int]$Top = 10 + ) + + $gitLogOutput = git log --numstat --pretty=format: + + $stats = $gitLogOutput | ForEach-Object { + if ($_ -match '^(?\d+)\s+(?\d+)\s+(?.+)$') { + [PSCustomObject]@{ + Filename = $Matches['filename'] + Additions = [int]$Matches['additions'] + Deletions = [int]$Matches['deletions'] + } + } + } + + $groupedStats = $stats | Group-Object Filename | ForEach-Object { + [PSCustomObject]@{ + Filename = $_.Name + TotalAdditions = ($_.Group | Measure-Object -Property Additions -Sum).Sum + TotalDeletions = ($_.Group | Measure-Object -Property Deletions -Sum).Sum + } + } + + $sortedStats = $groupedStats | Sort-Object TotalAdditions, TotalDeletions -Descending + + $sortedStats | Select-Object -First $Top +}