1+ <#
2+ . SYNOPSIS
3+ git checkout input extension
4+ . DESCRIPTION
5+ Extends the parameters for git checkout, making it easier to use from PowerShell.
6+
7+ If the -PullRequest parameter is provided, the branch will be fetched from the remote.
8+
9+ If the -BranchName parameter is provided, the branch will be checked out.
10+ #>
11+ [ValidatePattern (' ^git checkout' )]
12+ [Management.Automation.Cmdlet (" Use" , " Git" )]
13+ [CmdletBinding (PositionalBinding = $false )]
14+ param (
15+ # The branch name.
16+ [Parameter (ValueFromPipelineByPropertyName )]
17+ [string ]
18+ $BranchName ,
19+
20+ # The number of the pull request.
21+ # If no Branch Name is provided, the branch will be `PR-$PullRequest`.
22+ [Parameter (ValueFromPipelineByPropertyName )]
23+ [int ]
24+ $PullRequest ,
25+
26+ # The name of a new branch
27+ [Parameter (ValueFromPipelineByPropertyName )]
28+ [Alias (' New' , ' NewBranch' )]
29+ [string ]
30+ $NewBranchName ,
31+
32+ # The name of a branch to reset.
33+ [Parameter (ValueFromPipelineByPropertyName )]
34+ [Alias (' ResetBranch' )]
35+ [string ]
36+ $ResetBranchName ,
37+
38+ # One or more specific paths to reset.
39+ # This will overwrite the contents of the files with the contents of the index.
40+ [Parameter (ValueFromPipelineByPropertyName )]
41+ [Alias (' Reset' )]
42+ [string []]
43+ $ResetPath ,
44+
45+ # The name of the branch to checkout from.
46+ # This is only used when the -ResetPath parameter is provided.
47+ # It defaults to `HEAD`.
48+ [Parameter (ValueFromPipelineByPropertyName )]
49+ [string ]
50+ $FromBranch = ' HEAD' ,
51+
52+ # The revision number to checkout.
53+ # This is only used when the -ResetPath parameter is provided.
54+ # If provided, this will checkout the Nth most recent parent.
55+ # Eg. HEAD~2
56+ [Parameter (ValueFromPipelineByPropertyName )]
57+ [int ]
58+ $RevisionNumber ,
59+
60+ # The pattern number to checkout.
61+ # This is only used when the -ResetPath parameter is provided.
62+ # If provided, this will checkout the Nth most recent parent.
63+ # Eg. HEAD^2
64+ [Parameter (ValueFromPipelineByPropertyName )]
65+ [int ]
66+ $ParentNumber ,
67+
68+ # If set, will checkout a branch in a detached state.
69+ [Parameter (ValueFromPipelineByPropertyName )]
70+ [switch ]
71+ $Detach
72+ )
73+
74+ if ($Detach ) {
75+ " --detach"
76+ }
77+
78+ if ($PullRequest ) {
79+ $remoteName = git remote | Select-Object - ExpandProperty RemoteName | Select-Object - First 1
80+ if (-not $BranchName ) {
81+ $BranchName = " PR-$PullRequest "
82+ }
83+ $fetchedBranch = git fetch $remoteName " pull/$PullRequest /head:$BranchName "
84+ if (-not $fetchedBranch ) {
85+ return $BranchName
86+ }
87+ $BranchName
88+ }
89+ elseif ($NewBranchName ) {
90+ " -b"
91+ $NewBranchName
92+ }
93+ elseif ($ResetBranchName ) {
94+ if ($PSCmdlet -and $PSCmdlet.ShouldProcess (" Reset branch $ResetBranchName " )) {
95+ " -B" # Beware of capital B in git
96+ $ResetBranchName
97+ }
98+ }
99+ elseif ($BranchName ) {
100+ $BranchName
101+ }
102+
103+ if ($ResetPath ) {
104+ if ($ParentNumber ) {
105+ " $FromBranch ^$ParentNumber "
106+ } elseif ($RevisionNumber ) {
107+ " $FromBranch ~$RevisionNumber "
108+ } else {
109+ " $FromBranch "
110+ }
111+
112+ foreach ($pathToReset in $ResetPath ) {
113+ $pathToReset
114+ }
115+ }
0 commit comments