PowerShell/Scripts/list-commits.ps1

64 lines
2.6 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2023-04-03 11:20:19 +02:00
Lists Git commits
2021-07-13 21:10:02 +02:00
.DESCRIPTION
2023-04-03 11:20:19 +02:00
This PowerShell script lists all commits in a Git repository. Supported output formats are: pretty, list, compact, normal or JSON.
2021-10-06 09:36:47 +02:00
.PARAMETER RepoDir
Specifies the path to the Git repository.
.PARAMETER Format
2023-04-03 11:20:19 +02:00
Specifies the output format: pretty|list|compact|normal|JSON (pretty by default)
2021-07-13 21:10:02 +02:00
.EXAMPLE
2021-09-24 17:19:49 +02:00
PS> ./list-commits
2021-09-29 22:15:40 +02:00
ID Date Committer Description
-- ---- --------- -----------
ccd0d3e Wed Sep 29 08:28:20 2021 +0200 Markus Fleschutz Fix typo
...
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-29 12:47:46 +01:00
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2021-03-10 11:22:47 +01:00
#>
2023-04-03 11:20:19 +02:00
param([string]$RepoDir = "$PWD", [string]$Format = "pretty")
2021-03-10 11:22:47 +01:00
try {
2023-03-05 22:10:52 +01:00
if (-not(Test-Path "$RepoDir" -pathType container)) { throw "Can't access directory: $RepoDir" }
2021-03-10 11:22:47 +01:00
2021-04-17 11:29:32 +02:00
$Null = (git --version)
2021-03-10 11:22:47 +01:00
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
Write-Progress "Fetching latest updates..."
2021-08-30 15:20:46 +02:00
& git -C "$RepoDir" fetch --all --quiet
2021-05-06 17:10:00 +02:00
if ($lastExitCode -ne "0") { throw "'git fetch' failed" }
Write-Progress -Completed "Done."
2021-03-10 11:22:47 +01:00
2023-04-03 11:20:19 +02:00
if ($Format -eq "pretty") {
""
2023-05-17 11:53:34 +02:00
& git -C "$RepoDir" log --graph --format=format:'%C(bold yellow)%s%C(reset)%d by %an 🕘%cs 🔗%h' --all
2023-04-03 11:20:19 +02:00
} elseif ($Format -eq "list") {
2021-08-30 15:37:15 +02:00
""
2023-03-05 22:10:52 +01:00
"Hash Date Author Description"
"---- ---- ------ -----------"
& git log --pretty=format:"%h%x09%cs%x09%an%x09%s"
2021-08-30 15:37:15 +02:00
} elseif ($Format -eq "compact") {
2021-08-30 15:20:46 +02:00
""
"List of Git Commits"
"-------------------"
& git -C "$RepoDir" log --graph --pretty=format:'%Cred%h%Creset%C(yellow)%d%Creset %s %C(bold blue)by %an %cr%Creset' --abbrev-commit
if ($lastExitCode -ne "0") { throw "'git log' failed" }
2021-08-30 15:20:46 +02:00
} elseif ($Format -eq "JSON") {
& git -C "$RepoDir" log --pretty=format:'{%n "commit": "%H",%n "abbreviated_commit": "%h",%n "tree": "%T",%n "abbreviated_tree": "%t",%n "parent": "%P",%n "abbreviated_parent": "%p",%n "refs": "%D",%n "encoding": "%e",%n "subject": "%s",%n "sanitized_subject_line": "%f",%n "body": "%b",%n "commit_notes": "%N",%n "verification_flag": "%G?",%n "signer": "%GS",%n "signer_key": "%GK",%n "author": {%n "name": "%aN",%n "email": "%aE",%n "date": "%aD"%n },%n "commiter": {%n "name": "%cN",%n "email": "%cE",%n "date": "%cD"%n }%n},'
2021-03-10 16:00:16 +01:00
} else {
2021-08-30 15:20:46 +02:00
""
"List of Git Commits"
"-------------------"
& git -C "$RepoDir" log
if ($lastExitCode -ne "0") { throw "'git log' failed" }
2021-03-10 16:00:16 +01:00
}
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-03-10 11:22:47 +01:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-03-10 11:22:47 +01:00
exit 1
}