PowerShell/Docs/list-commits.md

117 lines
4.1 KiB
Markdown
Raw Normal View History

2022-12-04 10:40:18 +01:00
## The *list-commits.ps1* Script
2021-11-08 21:36:42 +01:00
2022-02-10 09:01:07 +01:00
This PowerShell script lists all commits in a Git repository. Supported output formats are: list, compact, normal or JSON.
2021-11-08 21:36:42 +01:00
## Parameters
```powershell
2021-12-09 16:19:09 +01:00
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>
Specifies the output format: list|compact|normal|JSON
Required? false
Position? 2
Default value list
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
PS> ./list-commits
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
...
```
## Notes
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2021-11-08 21:36:42 +01:00
## Related Links
https://github.com/fleschutz/PowerShell
2022-11-17 20:02:26 +01:00
## Source Code
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
Lists all commits in a Git repository
.DESCRIPTION
This PowerShell script lists all commits in a Git repository. Supported output formats are: list, compact, normal or JSON.
.PARAMETER RepoDir
Specifies the path to the Git repository.
.PARAMETER Format
Specifies the output format: list|compact|normal|JSON
.EXAMPLE
PS> ./list-commits
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
...
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$RepoDir = "$PWD", [string]$Format = "list")
try {
if (-not(test-path "$RepoDir" -pathType container)) { throw "Can't access directory: $RepoDir" }
$Null = (git --version)
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
write-progress "🢃 Fetching latest tags..."
& git -C "$RepoDir" fetch --all --quiet
if ($lastExitCode -ne "0") { throw "'git fetch' failed" }
if ($Format -eq "list") {
""
"ID Date Committer Description"
"-- ---- --------- -----------"
& git log --pretty=format:"%h%x09%ad%x09%an%x09%s"
} 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
2021-11-08 21:36:42 +01:00
*Generated by convert-ps2md.ps1 using the comment-based help of list-commits.ps1*