Functions/Receive-GitObject.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. function Receive-GitObject { <# .SYNOPSIS Downloads the given rev spec or all branches (and their commits) from remote repositories. .DESCRIPTION The `Recieve-GitCommit` function gets all the commits on all branches from all remote repositories and brings them into your repository. It defaults to the repository in the current directory. Pass the path to a different repository to the `RepoRoot` parameter. This function implements the `git fetch` command. .EXAMPLE Receive-GitObject Demonstrates how to get all branches from a remote repository. #> [CmdletBinding(DefaultParameterSetName = 'CurrentBranchRemote')] param( # The repository to fetch updates for. Defaults to the current directory. [string] $RepoRoot = (Get-Location).ProviderPath, [Parameter(Mandatory, ParameterSetName = 'Remote')] [string[]] $Remote, [Parameter(Mandatory, ParameterSetName = 'All')] [switch] $All, # Before fetching, remove any remote-tracking references that no longer # exist on the remote. [switch] $Prune, [LibGit2Sharp.TagFetchMode] $TagFetchMode, [pscredential] $Credential ) process { Set-StrictMode -Version 'Latest' Use-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState $repo = Find-GitRepository -Path $RepoRoot -Verify if (-not $repo) { return } $cancel = $false $fetchOptions = [LibGit2Sharp.FetchOptions]::new() $fetchOptions.OnProgress = { param([string] $serverProgressOutput) if ($ProgressPreference -ne 'SilentlyContinue') { if ($serverProgressOutput -match '^(.+):\s+(\d+)% \((\d+/\d+)\)') { # Compressing objects: 0% (1/123) # Counting objects: 3% (11/550) if ($ProgressPreference -ne 'SilentlyContinue') { Write-Progress ` -Activity $Matches[1] ` -PercentComplete $Matches[2] ` -Status $Matches[3] } } elseif ($serverProgressOutput -match '^(.+)(?::)?\s+(\d+)') { # Enumerating objects: 576, done. # Counting objects 4 if ($ProgressPreference -ne 'SilentlyContinue') { Write-Progress ` -Activity $Matches[1] ` -Status $Matches[2] ` -PercentComplete -1 } } elseif (-not [string]::IsNullOrWhiteSpace($serverProgressOutput)) { Write-Information $serverProgressOutput } } return -not $cancel -and -not $PSCmdlet.Stopping } $fetchOptions.OnTransferProgress = { param([LibGit2Sharp.TransferProgress] $progress) if ($ProgressPreference -ne 'SilentlyContinue' -and $progress.TotalObjects -ne 0) { Write-Progress ` -Activity "Fetching objects" ` -Status "$($progress.ReceivedObjects)/$($progress.TotalObjects), $($progress.ReceivedBytes) bytes" ` -PercentComplete (($progress.ReceivedObjects / $progress.TotalObjects) * 100) } return -not $cancel -and -not $PSCmdlet.Stopping } $credentialsProviderCalled = $false $fetchOptions.CredentialsProvider = { param([string]$Url, [string]$UsernameForUrl, [LibGit2Sharp.SupportedCredentialTypes]$Types) Write-Verbose "Credentials required" if ($credentialsProviderCalled) { $Credential = Get-Credential -Title "Wrong credentials provided for $Url" } Set-Variable -Name credentialsProviderCalled -Value $true -Scope 1 if (-not $Credential) { $Credential = Get-Credential -Title "Authentication required for $Url" } $gitCredential = [LibGit2Sharp.SecureUsernamePasswordCredentials]::new() $gitCredential.Username = $Credential.UserName $gitCredential.Password = $Credential.Password return $gitCredential } if ($PSBoundParameters.ContainsKey('Prune')) { $fetchOptions.Prune = $Prune } if ($PSBoundParameters.ContainsKey('TagFetchMode')) { $fetchOptions.TagFetchMode = $TagFetchMode } $remoteObjects = if ($All) { $repo.Network.Remotes } elseif (-not $Remote) { $Remote = if ($repo.Head.RemoteName) { $repo.Head.RemoteName } else { 'origin' } $repo.Network.Remotes[$Remote] } try { foreach ($remoteObject in $remoteObjects) { [string[]]$refspecs = $remoteObject.FetchRefSpecs | Select-Object -ExpandProperty Specification Write-Verbose "Fetching remote $($remoteObject.Name) with refspecs $refspecs" try { [LibGit2Sharp.Commands]::Fetch($repo, $remoteObject.Name, $refspecs, $fetchOptions, $null) } catch { Write-Error -ErrorRecord $_ } } } finally { $cancel = $true } } } |