PowerShell/Scripts/list-commits.ps1

61 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
2021-10-04 21:29:23 +02:00
Lists all commits in a Git repository
2021-07-13 21:10:02 +02:00
.DESCRIPTION
2021-10-16 13:40:20 +02:00
This script lists all commits in a Git repository. Supported output formats are: list, compact, normal or JSON.
2021-10-06 09:36:47 +02:00
.PARAMETER RepoDir
Specifies the path to the Git repository.
.PARAMETER Format
Specifies the output format: list|compact|normal|JSON
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
291d785 Wed Sep 29 08:18:28 2021 +0200 Markus Fleschutz Update README.md
...
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-09-29 22:15:40 +02:00
write-progress "🢃 Fetching latest tags..."
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" }
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-09-27 10:09:45 +02:00
exit 0 # success
2021-03-10 11:22:47 +01:00
} catch {
2021-09-16 20:19:10 +02:00
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
2021-03-10 11:22:47 +01:00
exit 1
}