Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional commands #6

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Public commands:
- `Update-GitBranch`

## [0.2.0] - 2024-08-25

### Added
Expand All @@ -21,3 +26,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Remove-PSHistory`
- `Remove-PSReadLineHistory`
- `Split-StringAtIndices`
- `Update-GitBranch`
35 changes: 35 additions & 0 deletions source/Public/Assert-GitLocalChanges.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<#
.SYNOPSIS
Asserts that there are no unstaged or staged changes in the local Git branch.

.DESCRIPTION
The Assert-GitLocalChanges command checks whether there are any unstaged
or staged changes in the local Git branch. If there are any staged or
unstaged changes, it throws a terminating error.

.EXAMPLE
Assert-GitLocalChanges

This example demonstrates how to use the Assert-GitLocalChanges command
to ensure that there are no local changes in the Git repository.
#>
function Assert-GitLocalChanges
{
[CmdletBinding()]
param ()

$hasChanges = Test-GitLocalChanges

if ($hasChanges)
{
# cSpell:ignore unstaged
$PSCmdlet.ThrowTerminatingError(

Check warning on line 26 in source/Public/Assert-GitLocalChanges.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Assert-GitLocalChanges.ps1#L26

Added line #L26 was not covered by tests
[System.Management.Automation.ErrorRecord]::new(
($script:localizedData.Assert_GitLocalChanges_FailedUnstagedChanges),

Check warning on line 28 in source/Public/Assert-GitLocalChanges.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Assert-GitLocalChanges.ps1#L28

Added line #L28 was not covered by tests
'AGLC0001', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::InvalidResult,
'Staged or unstaged changes'
)
)
}
}
50 changes: 50 additions & 0 deletions source/Public/Assert-GitRemote.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<#
.SYNOPSIS
Checks if the specified Git remote exists locally and throws an error if it doesn't.

.DESCRIPTION
The `Assert-GitRemote` command checks if the remote specified in the `$RemoteName`
parameter exists locally. If the remote doesn't exist, it throws an error.

.PARAMETER RemoteName
Specifies the name of the Git remote to check.

.EXAMPLE
PS> Assert-GitRemote -RemoteName "origin"

This example checks if the Git remote named "origin" exists locally.

.INPUTS
None.

.OUTPUTS
None.
#>
function Assert-GitRemote
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true, Position = 0)]
[System.String]
$RemoteName
)

<#
Check if the remote specified in $UpstreamRemoteName exists locally and
throw an error if it doesn't.
#>
$remoteExists = Test-GitRemote -RemoteName $RemoteName

if (-not $remoteExists)

Check warning

Code scanning / PSScriptAnalyzer

Opening brace on if-statements should only be followed by one new line. See https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md#one-newline-after-opening-brace Warning

Opening brace on if-statements should only be followed by one new line. See https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md#one-newline-after-opening-brace
{
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
($script:localizedData.New_SamplerGitHubReleaseTag_RemoteMissing -f $UpstreamRemoteName),
'AGR0001', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::ObjectNotFound,
$DatabaseName
)
)
}
}
148 changes: 148 additions & 0 deletions source/Public/Get-GitBranchCommit.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<#
.SYNOPSIS
Retrieves the commit ID(s) for a specified Git branch.

.DESCRIPTION
The Get-GitBranchCommit command retrieves the commit ID(s) for a specified
Git branch. It provides options to retrieve the latest commit ID, a specific
number of latest commit IDs, or the first X number of commit IDs.

.PARAMETER BranchName
Specifies the name of the Git branch. If not provided, the current branch
will be used.

.PARAMETER Latest
Retrieves only the latest commit ID.

.PARAMETER Last
Retrieves the specified number of latest commit IDs. The order will be from
the newest to the oldest commit.

.PARAMETER First
Retrieves the first X number of commit IDs. The order will be from the
oldest to the newest commit.

.OUTPUTS
System.String

The commit ID(s) for the specified Git branch.

.EXAMPLE
Get-GitBranchCommit -BranchName 'feature/branch'

Retrieves all commit IDs for the 'feature/branch' Git branch.

.EXAMPLE
Get-GitBranchCommit -Latest

Retrieves only the latest commit ID for the current Git branch.

.EXAMPLE
Get-GitBranchCommit -Last 5

Retrieves the 5 latest commit IDs for the current Git branch.

.EXAMPLE
Get-GitBranchCommit -First 3

Retrieves the first 3 commit IDs for the current Git branch.
#>
function Get-GitBranchCommit
{
[CmdletBinding(DefaultParameterSetName = 'NoParameter')]
[OutputType([System.String])]
param
(
[Parameter(ParameterSetName = 'NoParameter')]
[Parameter(ParameterSetName = 'Latest')]
[Parameter(ParameterSetName = 'Last')]
[Parameter(ParameterSetName = 'First')]
[System.String]
$BranchName,

[Parameter(ParameterSetName = 'Latest')]
[System.Management.Automation.SwitchParameter]
$Latest,

[Parameter(ParameterSetName = 'Last')]
[System.UInt32]
$Last,

[Parameter(ParameterSetName = 'First')]
[System.UInt32]
$First
)

$commitId = $null

$argument = @()

if ($PSBoundParameters.ContainsKey('BranchName'))
{
if ($BranchName -eq '.')

Check warning on line 82 in source/Public/Get-GitBranchCommit.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitBranchCommit.ps1#L82

Added line #L82 was not covered by tests
{
$BranchName = Get-GitLocalBranchName -Current

Check warning on line 84 in source/Public/Get-GitBranchCommit.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitBranchCommit.ps1#L84

Added line #L84 was not covered by tests
}

$argument += @(
$BranchName

Check warning on line 88 in source/Public/Get-GitBranchCommit.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitBranchCommit.ps1#L87-L88

Added lines #L87 - L88 were not covered by tests
)
}

if ($Latest.IsPresent)
{
# Return only the latest commit ID.
$commitId = git rev-parse HEAD @argument
}
elseif ($Last)

Check warning on line 97 in source/Public/Get-GitBranchCommit.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitBranchCommit.ps1#L97

Added line #L97 was not covered by tests
{
# Return the latest X number of commits.
$commitId = git log -n $Last --pretty=format:"%H" @argument

Check warning on line 100 in source/Public/Get-GitBranchCommit.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitBranchCommit.ps1#L100

Added line #L100 was not covered by tests
}
elseif ($First)

Check warning on line 102 in source/Public/Get-GitBranchCommit.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitBranchCommit.ps1#L102

Added line #L102 was not covered by tests
{
if (-not $PSBoundParameters.ContainsKey('BranchName'))

Check warning on line 104 in source/Public/Get-GitBranchCommit.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitBranchCommit.ps1#L104

Added line #L104 was not covered by tests
{
$BranchName = Get-GitLocalBranchName -Current

Check warning on line 106 in source/Public/Get-GitBranchCommit.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitBranchCommit.ps1#L106

Added line #L106 was not covered by tests
}

# Count the total number of commits in the branch.
$totalCommits = git rev-list --count $BranchName

Check warning on line 110 in source/Public/Get-GitBranchCommit.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitBranchCommit.ps1#L110

Added line #L110 was not covered by tests

# Calculate the number of commits to skip.
$skipCommits = $totalCommits - $First

Check warning on line 113 in source/Public/Get-GitBranchCommit.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitBranchCommit.ps1#L113

Added line #L113 was not covered by tests

# Return the first X number of commits.
$commitId = git log --skip $skipCommits --reverse -n $First --pretty=format:"%H" $BranchName

Check warning on line 116 in source/Public/Get-GitBranchCommit.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitBranchCommit.ps1#L116

Added line #L116 was not covered by tests
}
else
{
# Return all commit IDs.
$commitId = git log --pretty=format:"%H" @argument

Check warning on line 121 in source/Public/Get-GitBranchCommit.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitBranchCommit.ps1#L121

Added line #L121 was not covered by tests
}

# TODO: Should handle LASTEXITCODE above too

if ($LASTEXITCODE -ne 0) # cSpell: ignore LASTEXITCODE
{
if($PSBoundParameters.ContainsKey('BranchName'))

Check warning on line 128 in source/Public/Get-GitBranchCommit.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitBranchCommit.ps1#L128

Added line #L128 was not covered by tests

Check warning

Code scanning / PSScriptAnalyzer

If a keyword is followed by a parenthesis, there should be single space between the keyword and the parenthesis. See https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md#one-newline-after-opening-brace Warning

If a keyword is followed by a parenthesis, there should be single space between the keyword and the parenthesis. See https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md#one-newline-after-opening-brace

Check warning

Code scanning / PSScriptAnalyzer

Use space before open parenthesis. Warning

Use space before open parenthesis.
{
$errorMessage = $script:localizedData.Get_GitBranchCommit_FailedFromBranch -f $BranchName

Check warning on line 130 in source/Public/Get-GitBranchCommit.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitBranchCommit.ps1#L130

Added line #L130 was not covered by tests
}
else
{
$errorMessage = $script:localizedData.Get_GitBranchCommit_FailedFromCurrent

Check warning on line 134 in source/Public/Get-GitBranchCommit.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitBranchCommit.ps1#L134

Added line #L134 was not covered by tests
}

$PSCmdlet.ThrowTerminatingError(

Check warning on line 137 in source/Public/Get-GitBranchCommit.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitBranchCommit.ps1#L137

Added line #L137 was not covered by tests
[System.Management.Automation.ErrorRecord]::new(
$errorMessage,
'GGLBN0001', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::ObjectNotFound,
$branchName
)
)
}

return $commitId
}
70 changes: 70 additions & 0 deletions source/Public/Get-GitLocalBranchName.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<#
.SYNOPSIS
Retrieves the name of the local Git branch.

.DESCRIPTION
The Get-GitLocalBranchName command is used to retrieve the name of the
current local Git branch. It uses the `git rev-parse --abbrev-ref HEAD`
command to get the branch name.

.OUTPUTS
[System.String]

The function returns the name of the current local Git branch as a string.

.EXAMPLE
PS C:\> Get-GitLocalBranchName -Current

Returns the name of the current local Git branch.
#>
function Get-GitLocalBranchName
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '', Justification = 'ShouldProcess is implemented without ShouldProcess/ShouldContinue.')]
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')]
[OutputType([System.String])]
param
(
[Parameter()]
[System.Management.Automation.SwitchParameter]
$Current
)

$branchName = $null

if ($WhatIfPreference)
{
if ($Current.IsPresent)

Check warning on line 36 in source/Public/Get-GitLocalBranchName.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitLocalBranchName.ps1#L36

Added line #L36 was not covered by tests
{
Write-Information -MessageData 'What if: Getting current local branch name' -InformationAction Continue

Check warning on line 38 in source/Public/Get-GitLocalBranchName.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitLocalBranchName.ps1#L38

Added line #L38 was not covered by tests
}
else
{
Write-Information -MessageData 'What if: Getting local branch names.' -InformationAction Continue

Check warning on line 42 in source/Public/Get-GitLocalBranchName.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitLocalBranchName.ps1#L42

Added line #L42 was not covered by tests
}
}
else
{
if ($Current.IsPresent)
{
$branchName = git rev-parse --abbrev-ref HEAD
}
else
{
$branchName = git branch --format='%(refname:short)' --list

Check warning on line 53 in source/Public/Get-GitLocalBranchName.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitLocalBranchName.ps1#L53

Added line #L53 was not covered by tests
}

if ($LASTEXITCODE -ne 0) # cSpell: ignore LASTEXITCODE
{
$PSCmdlet.ThrowTerminatingError(

Check warning on line 58 in source/Public/Get-GitLocalBranchName.ps1

View check run for this annotation

Codecov / codecov/patch

source/Public/Get-GitLocalBranchName.ps1#L58

Added line #L58 was not covered by tests
[System.Management.Automation.ErrorRecord]::new(
$script:localizedData.Get_GitLocalBranchName_Failed,
'GGLBN0001', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::ObjectNotFound,
$branchName
)
)
}
}

return $branchName
}
Loading
Loading