PowerShell/docs/watch-commits.md

119 lines
3.4 KiB
Markdown
Raw Normal View History

2024-11-08 12:38:20 +01:00
The *watch-commits.ps1* Script
===========================
2024-05-19 10:25:56 +02:00
2024-11-20 11:52:20 +01:00
This PowerShell script continuously lists the latest commit in a Git repository in real-time.
2024-05-19 10:25:56 +02:00
Parameters
----------
```powershell
2024-11-08 12:35:11 +01:00
/home/markus/Repos/PowerShell/scripts/watch-commits.ps1 [[-pathToRepo] <String>] [[-updateInterval] <Int32>] [[-speed] <Int32>] [<CommonParameters>]
2024-05-19 10:25:56 +02:00
-pathToRepo <String>
Specifies the file path to the local Git repository.
Required? false
Position? 1
Default value "$PWD"
Accept pipeline input? false
Accept wildcard characters? false
-updateInterval <Int32>
Required? false
Position? 2
Default value 30
Accept pipeline input? false
Accept wildcard characters? false
-speed <Int32>
Required? false
Position? 3
Default value 17
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
Example
-------
```powershell
2024-11-20 11:52:20 +01:00
PS> ./watch-commits.ps1
2024-05-19 10:25:56 +02:00
❇️ Updated general.csv by Markus Fleschutz (HEAD -> main, origin/main, origin/HEAD)
...
```
Notes
-----
Author: Markus Fleschutz | License: CC0
Related Links
-------------
https://github.com/fleschutz/PowerShell
Script Content
--------------
```powershell
<#
.SYNOPSIS
2024-11-20 11:52:20 +01:00
Watch commits live.
2024-05-19 10:25:56 +02:00
.DESCRIPTION
2024-11-20 11:52:20 +01:00
This PowerShell script continuously lists the latest commit in a Git repository in real-time.
2024-05-19 10:25:56 +02:00
.PARAMETER pathToRepo
Specifies the file path to the local Git repository.
.EXAMPLE
2024-11-20 11:52:20 +01:00
PS> ./watch-commits.ps1
2024-05-19 10:25:56 +02:00
❇️ Updated general.csv by Markus Fleschutz (HEAD -> main, origin/main, origin/HEAD)
...
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$pathToRepo = "$PWD", [int]$updateInterval = 30, [int]$speed = 17)
try {
Write-Progress "Searching for Git executable..."
$null = (git --version)
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
2024-11-20 11:52:20 +01:00
Write-Progress "Checking local Git repository..."
2024-05-19 10:25:56 +02:00
if (-not(Test-Path "$pathToRepo" -pathType container)) { throw "Can't access directory: $pathToRepo" }
Write-Progress -completed "Done."
2024-11-20 11:52:20 +01:00
Write-Host ""
Write-Host "TIME COMMIT"
Write-Host "---- ------"
2024-05-19 10:25:56 +02:00
$prevLine = ""
$tzOffset = (Get-Timezone).BaseUtcOffset.TotalSeconds
for (;;) {
2024-11-20 11:52:20 +01:00
& git -C "$pathToRepo" fetch --all --recurse-submodules=no --jobs=1 --quiet
if ($lastExitCode -ne "0") { throw "'git fetch' failed" }
2024-05-19 10:25:56 +02:00
$line = (git -C "$pathToRepo" log origin --format=format:'%at %s by %an%d' --max-count=1)
2024-11-20 11:52:20 +01:00
if ($line -eq $prevLine) {
2024-05-19 10:25:56 +02:00
Start-Sleep -seconds $updateInterval
2024-11-20 11:52:20 +01:00
continue
2024-05-19 10:25:56 +02:00
}
2024-11-20 11:52:20 +01:00
$unixTimestamp = [int64]$line.Substring(0,10)
$time = (Get-Date -day 1 -month 1 -year 1970 -hour 0 -minute 0 -second 0).AddSeconds($unixTimestamp)
$time = $time.AddSeconds($tzOffset)
$timeString = $time.ToString("HH:mm")
$message = $line.Substring(11)
& "$PSScriptRoot/write-typewriter.ps1" "$timeString $message" $speed
$prevLine = $line
2024-05-19 10:25:56 +02:00
}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
```
2024-11-20 11:52:20 +01:00
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:52:01)*