PowerShell/Scripts/list-commits.ps1

51 lines
2.1 KiB
PowerShell
Raw Normal View History

2021-04-21 19:53:52 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-08-30 15:20:46 +02:00
list-commits.ps1 [<RepoDir>] [<Format>]
2021-07-13 21:10:02 +02:00
.DESCRIPTION
2021-08-30 15:37:15 +02:00
Lists all commits in a Git repository (format is: list|compact|normal|JSON).
2021-07-13 21:10:02 +02:00
.EXAMPLE
PS> .\list-commits.ps1
2021-08-29 17:50:03 +02:00
.NOTES
Author: Markus Fleschutz · License: CC0
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2021-03-10 11:22:47 +01:00
#>
2021-08-30 15:37:15 +02:00
param([string]$RepoDir = "$PWD", [string]$Format = "list")
2021-03-10 11:22:47 +01:00
try {
2021-03-16 08:45:53 +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" }
2021-08-30 15:20:46 +02:00
"🢃 Fetching latest tags..."
& git -C "$RepoDir" fetch --all --quiet
2021-05-06 17:10:00 +02:00
if ($lastExitCode -ne "0") { throw "'git fetch' failed" }
2021-03-10 11:22:47 +01:00
2021-08-30 15:37:15 +02:00
if ($Format -eq "list") {
""
"ID Date Committer Description"
"-- ---- --------- -----------"
& git log --pretty=format:"%h%x09%ad%x09%an%x09%s"
} 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-03-10 11:22:47 +01:00
exit 0
} catch {
2021-05-02 21:30:48 +02:00
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-03-10 11:22:47 +01:00
exit 1
}