PowerShell/docs/list-commits.md

125 lines
4.2 KiB
Markdown
Raw Permalink Normal View History

2024-11-08 12:38:20 +01:00
The *list-commits.ps1* Script
===========================
2021-11-08 21:36:42 +01:00
2023-05-26 12:20:18 +02:00
This PowerShell script lists all commits in a Git repository. Supported output formats are: pretty, list, compact, normal or JSON.
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-11-08 21:36:42 +01:00
```powershell
2024-11-08 12:35:11 +01:00
/home/markus/Repos/PowerShell/scripts/list-commits.ps1 [[-RepoDir] <String>] [[-Format] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
-RepoDir <String>
Specifies the path to the Git repository.
Required? false
Position? 1
Default value "$PWD"
Accept pipeline input? false
Accept wildcard characters? false
-Format <String>
2023-05-26 12:20:18 +02:00
Specifies the output format: pretty|list|compact|normal|JSON (pretty by default)
2021-11-08 21:36:42 +01:00
Required? false
Position? 2
2023-05-26 12:20:18 +02:00
Default value pretty
2021-11-08 21:36:42 +01:00
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.
```
2023-07-29 10:04:38 +02:00
Example
-------
2021-11-08 21:36:42 +01:00
```powershell
PS> ./list-commits
ID Date Committer Description
-- ---- --------- -----------
ccd0d3e Wed Sep 29 08:28:20 2021 +0200 Markus Fleschutz Fix typo
...
```
2023-07-29 10:04:38 +02:00
Notes
-----
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Related Links
-------------
2021-11-08 21:36:42 +01:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
2023-05-26 12:20:18 +02:00
Lists Git commits
2022-11-17 20:02:26 +01:00
.DESCRIPTION
2023-05-26 12:20:18 +02:00
This PowerShell script lists all commits in a Git repository. Supported output formats are: pretty, list, compact, normal or JSON.
2022-11-17 20:02:26 +01:00
.PARAMETER RepoDir
Specifies the path to the Git repository.
.PARAMETER Format
2023-05-26 12:20:18 +02:00
Specifies the output format: pretty|list|compact|normal|JSON (pretty by default)
2022-11-17 20:02:26 +01:00
.EXAMPLE
PS> ./list-commits
ID Date Committer Description
-- ---- --------- -----------
ccd0d3e Wed Sep 29 08:28:20 2021 +0200 Markus Fleschutz Fix typo
...
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2023-05-26 12:20:18 +02:00
param([string]$RepoDir = "$PWD", [string]$Format = "pretty")
2022-11-17 20:02:26 +01:00
try {
2023-05-26 12:20:18 +02:00
if (-not(Test-Path "$RepoDir" -pathType container)) { throw "Can't access directory: $RepoDir" }
2022-11-17 20:02:26 +01:00
$Null = (git --version)
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
2023-12-07 20:24:45 +01:00
Write-Progress "Fetching latest updates..."
2022-11-17 20:02:26 +01:00
& git -C "$RepoDir" fetch --all --quiet
if ($lastExitCode -ne "0") { throw "'git fetch' failed" }
2023-12-07 20:24:45 +01:00
Write-Progress -Completed "Done."
2022-11-17 20:02:26 +01:00
2023-05-26 12:20:18 +02:00
if ($Format -eq "pretty") {
2022-11-17 20:02:26 +01:00
""
2023-05-26 12:20:18 +02:00
& git -C "$RepoDir" log --graph --format=format:'%C(bold yellow)%s%C(reset)%d by %an 🕘%cs 🔗%h' --all
} elseif ($Format -eq "list") {
""
"Hash Date Author Description"
"---- ---- ------ -----------"
& git log --pretty=format:"%h%x09%cs%x09%an%x09%s"
2022-11-17 20:02:26 +01:00
} elseif ($Format -eq "compact") {
""
"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" }
} 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},'
} else {
""
"List of Git Commits"
"-------------------"
& git -C "$RepoDir" log
if ($lastExitCode -ne "0") { throw "'git log' failed" }
}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2024-11-20 11:52:20 +01:00
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:55)*