Skip to content

Commit c070677

Browse files
Merge pull request #40 from StartAutomating/CheckoutSupportAndFixes
CheckoutSupportAndFixes
2 parents 7e7832c + b9c3f2a commit c070677

File tree

8 files changed

+329
-14
lines changed

8 files changed

+329
-14
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.1.8:
2+
* Adding Support for git checkout (#38)
3+
* Use-Git: Avoiding unwanted confirmation ( Fixing #39 )
4+
---
5+
16
## 0.1.7:
27
* Use-Git: -Verbose no longer infers --verbose (#10)
38
* Out-Git: Support for extension caching (#35)
@@ -43,4 +48,4 @@
4348
---
4449
## 0.1
4550
* Initial Release of ugit
46-
---
51+
---
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<#
2+
.SYNOPSIS
3+
git checkout extension
4+
.DESCRIPTION
5+
Outputs git checkout as objects.
6+
.EXAMPLE
7+
git checkout -b CreateNewBranch
8+
.EXAMPLE
9+
git checkout main
10+
#>
11+
[Management.Automation.Cmdlet('Out', 'Git')]
12+
[ValidatePattern('^git checkout')]
13+
param()
14+
15+
begin {
16+
$gitCheckoutLines = @()
17+
}
18+
19+
process {
20+
$gitCheckoutLines += $gitOut
21+
}
22+
23+
end {
24+
25+
if ($($gitCheckoutLines) -match "^Switched to a new branch '(?<b>[^']+)'") {
26+
[PSCustomObject]@{
27+
BranchName = $matches.b
28+
PSTypeName = 'git.checkout.newbranch'
29+
GitRoot = $GitRoot
30+
}
31+
}
32+
elseif ($gitCheckoutLines -match "Switched to branch '(?<b>[^']+)'")
33+
{
34+
$gitCheckoutInfo = @{PSTypeName='git.checkout.switchbranch';GitRoot=$GitRoot;GitOutputLines=$gitCheckoutLines;Modified=@()}
35+
foreach ($checkoutLine in $gitCheckoutLines) {
36+
if ($checkoutLine -match "Switched to branch '(?<b>[^']+)'") {
37+
$gitCheckoutInfo.BranchName = $matches.b
38+
}
39+
elseif ($checkoutLine -match '^Your branch') {
40+
$gitCheckoutInfo.Status = $checkoutLine
41+
}
42+
elseif ($checkoutLine -match '^(?<ct>\w)\s+(?<fn>\S+)') {
43+
if ($matches.ct -eq 'M') {
44+
$gitCheckoutInfo.modified += (Get-Item $matches.fn -ErrorAction SilentlyContinue)
45+
}
46+
}
47+
}
48+
[PSCustomObject]$gitCheckoutInfo
49+
}
50+
elseif ($gitCheckoutLines -match "Already on '(?<b>[^']+)'") {
51+
$gitCheckoutInfo = @{PSTypeName='git.checkout.alreadyonbranch';GitRoot=$GitRoot;GitOutputLines=$gitCheckoutLines}
52+
foreach ($checkoutLine in $gitCheckoutLines) {
53+
if ($checkoutLine -match "Already on '(?<b>[^']+)'") {
54+
$gitCheckoutInfo.BranchName = $matches.b
55+
}
56+
elseif ($checkoutLine -match '^Your branch') {
57+
$gitCheckoutInfo.Status = $checkoutLine
58+
}
59+
}
60+
[PSCustomObject]$gitCheckoutInfo
61+
}
62+
else {
63+
$gitCheckoutLines
64+
}
65+
}

Formatting/Git.Checkout.format.ps1

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Write-FormatView -TypeName git.checkout.newbranch -Action {
2+
Write-FormatViewExpression -Text "Switched to a new branch "
3+
Write-FormatViewExpression -ScriptBlock {"'$($_.BranchName)'"} -ForegroundColor verbose
4+
} -GroupByProperty GitRoot
5+
6+
Write-FormatView -TypeName git.checkout.alreadyonbranch -Action {
7+
Write-FormatViewExpression -Text "Already on "
8+
Write-FormatViewExpression -ScriptBlock {"'$($_.BranchName)'"} -ForegroundColor warning -If { $_.BranchName -in 'main', 'master'}
9+
Write-FormatViewExpression -ScriptBlock {"'$($_.BranchName)'"} -ForegroundColor verbose -If { $_.BranchName -notin 'main', 'master'}
10+
Write-FormatViewExpression -Newline
11+
Write-FormatViewExpression -Property Status
12+
} -GroupByProperty GitRoot
13+
14+
Write-FormatView -TypeName git.checkout.switchbranch -Action {
15+
Write-FormatViewExpression -Text "Switched to "
16+
Write-FormatViewExpression -ScriptBlock {"'$($_.BranchName)'"} -ForegroundColor warning -If { $_.BranchName -in 'main', 'master'}
17+
Write-FormatViewExpression -ScriptBlock {"'$($_.BranchName)'"} -ForegroundColor verbose -If { $_.BranchName -notin 'main', 'master'}
18+
Write-FormatViewExpression -Newline
19+
Write-FormatViewExpression -Property Status
20+
Write-FormatViewExpression -If { $_.Modified } -ScriptBlock {
21+
@(
22+
" Untracked modifications:"
23+
""
24+
$($_.Modified | Out-String).Trim()
25+
) -join [Environment]::NewLine
26+
}
27+
} -GroupByProperty GitRoot

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,16 @@ Get-UGitExtension is built using [Piecemeal](https://github.com/StartAutomating/
6262
## Git Commands Extended
6363

6464
* git branch
65-
* git commit
65+
* git checkout
6666
* git clone
67+
* git commit
6768
* git diff
6869
* git log
70+
* git pull
6971
* git push
7072
* git pull
7173
* git status
7274

73-
7475
### Extensions that may apply to any git command:
7576

7677
* git.fileoutput

Use-Git.ps1

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@
8383
$GitArgument[($argumentNumber + 1)..($GitArgument.Length - 1)]
8484
})
8585
$GitArgument = @($beforeArgs) + @("$($commandElement.Extent)") + @($afterArgs)
86+
if ($commandElement.parameterName -eq 'd') { # Also, if they passed -d/-D, they probably don't want to be confirmed
87+
$ConfirmPreference ='none' # so set confirm impact to none
88+
}
8689
}
8790
$argumentNumber++
8891
}
@@ -114,6 +117,10 @@
114117
# git sometimes like to return information along standard error, and CI/CD and user defaults sometimes set $ErrorActionPreference to 'Stop'
115118
# So we change $ErrorActionPreference before we call git, just in case.
116119
$ErrorActionPreference = 'continue'
120+
121+
# Before we process each directory, make a copy of the bound parameters.
122+
$paramCopy = ([Ordered]@{} + $PSBoundParameters)
123+
117124

118125
# Now, we will force there to be at least one directory (the current path).
119126
# This makes the code simpler, because we are always going thru a loop.
@@ -124,7 +131,6 @@
124131
$directories = @(foreach ($dir in $directories) { $dir.Fullname })
125132
}
126133

127-
128134

129135
# For each directory we know of, we
130136
foreach ($dir in $directories) {
@@ -148,14 +154,16 @@
148154
continue # and continue to the next one.
149155
}
150156
}
151-
157+
152158
$OutGitParams.GitRoot = "$($script:RepoRoots[$dir])"
153159
Write-Verbose "Calling git with $AllGitArgs"
154160
if ($dirCount -gt 1) {
155161
Write-Progress -PercentComplete (($dirCount * 5) % 100) -Status "git $allGitArgs " -Activity "$($dir) " -Id $progId
156162
}
157163

158-
if ($PSCmdlet.ShouldProcess("$pwd : git $allGitArgs")) {
164+
if (($ConfirmPreference -eq 'None' -and -not $paramCopy.Confirm) -or # If we have indicated we do not care about -Confirmation, don't prompt
165+
$PSCmdlet.ShouldProcess("$pwd : git $allGitArgs") # otherwise, as for confirmation to run.
166+
) {
159167
& $script:CachedGitCmd @AllGitArgs *>&1 | # Then we run git, combining all streams into output.
160168
# then pipe to Out-Git, which will
161169
Out-Git @OutGitParams # output git as objects.
@@ -165,9 +173,7 @@
165173
# If Out-Git finds one or more extensions to run, these can parse the output.
166174
}
167175
Pop-Location # After we have run, Pop back out of the location.
168-
}
169-
170-
176+
}
171177
}
172178

173179
end {

0 commit comments

Comments
 (0)