Git/Git.psm1
<#
.SYNOPSIS Gets information from one or more Git repositories in a team project. .PARAMETER Project Specifies either the name of the Team Project or a previously initialized Microsoft.TeamFoundation.WorkItemTracking.Client.Project object to connect to. If omitted, it defaults to the connection opened by Connect-TfsTeamProject (if any). For more details, see the Get-TfsTeamProject cmdlet. .PARAMETER Collection Specifies either a URL/name of the Team Project Collection to connect to, or a previously initialized TfsTeamProjectCollection object. When using a URL, it must be fully qualified. The format of this string is as follows: http[s]://<ComputerName>:<Port>/[<TFS-vDir>/]<CollectionName> Valid values for the Transport segment of the URI are HTTP and HTTPS. If you specify a connection URI with a Transport segment, but do not specify a port, the session is created with standards ports: 80 for HTTP and 443 for HTTPS. To connect to a Team Project Collection by using its name, a TfsConfigurationServer object must be supplied either via -Server argument or via a previous call to the Connect-TfsConfigurationServer cmdlet. For more details, see the Get-TfsTeamProjectCollection cmdlet. .INPUTS Microsoft.TeamFoundation.WorkItemTracking.Client.Project System.String #> Function Get-TfsGitRepository { [CmdletBinding()] [OutputType([Microsoft.TeamFoundation.SourceControl.WebApi.GitRepository])] Param ( [Parameter()] [SupportsWildcards()] [string] $Name = '*', [Parameter(ValueFromPipeline=$true)] [object] $Project, [Parameter()] [object] $Collection ) Process { $tp = Get-TfsTeamProject -Project $Project -Collection $Collection $tpc = $tp.Store.TeamProjectCollection $id = $tp.Guid $gitService = $tpc.GetService([type]'Microsoft.TeamFoundation.Git.Client.GitRepositoryService') return $gitService.QueryRepositories($tp.Name) | ? Name -Like $Name } } <# .SYNOPSIS Creates a new Git repository in a team project. .PARAMETER Project Specifies either the name of the Team Project or a previously initialized Microsoft.TeamFoundation.WorkItemTracking.Client.Project object to connect to. If omitted, it defaults to the connection opened by Connect-TfsTeamProject (if any). For more details, see the Get-TfsTeamProject cmdlet. .PARAMETER Collection Specifies either a URL/name of the Team Project Collection to connect to, or a previously initialized TfsTeamProjectCollection object. When using a URL, it must be fully qualified. The format of this string is as follows: http[s]://<ComputerName>:<Port>/[<TFS-vDir>/]<CollectionName> Valid values for the Transport segment of the URI are HTTP and HTTPS. If you specify a connection URI with a Transport segment, but do not specify a port, the session is created with standards ports: 80 for HTTP and 443 for HTTPS. To connect to a Team Project Collection by using its name, a TfsConfigurationServer object must be supplied either via -Server argument or via a previous call to the Connect-TfsConfigurationServer cmdlet. For more details, see the Get-TfsTeamProjectCollection cmdlet. .PARAMETER Passthru Returns the results of the command. By default, this cmdlet does not generate any output. .INPUTS Microsoft.TeamFoundation.WorkItemTracking.Client.Project System.String #> Function New-TfsGitRepository { Param ( [Parameter(Mandatory=$true)] [string] $Name, [Parameter(ValueFromPipeline=$true)] [object] $Project, [Parameter()] [object] $Collection, [Parameter()] [switch] $Passthru ) Begin { Add-Type -AssemblyName 'Microsoft.TeamFoundation.Core.WebApi' Add-Type -AssemblyName 'Microsoft.TeamFoundation.SourceControl.WebApi' Add-Type -AssemblyName 'Microsoft.TeamFoundation.Common' } Process { $tp = Get-TfsTeamProject -Project $Project -Collection $Collection $tpc = $tp.Store.TeamProjectCollection $gitClient = Get-TfsHttpClient -Type 'Microsoft.TeamFoundation.SourceControl.WebApi.GitHttpClient' $tpRef = [Microsoft.TeamFoundation.Core.WebApi.TeamProjectReference] @{Id = $tp.Guid; Name = $tp.Name} $repoToCreate = [Microsoft.TeamFoundation.SourceControl.WebApi.GitRepository] @{Name = $Name; ProjectReference = $tpRef} $task = $gitClient.CreateRepositoryAsync($repoToCreate, $tp.Name) $result = $task.Result if ($Passthru) { return $result } } } <# .SYNOPSIS Deletes one or more Git repositories from a team project. .PARAMETER Project Specifies either the name of the Team Project or a previously initialized Microsoft.TeamFoundation.WorkItemTracking.Client.Project object to connect to. If omitted, it defaults to the connection opened by Connect-TfsTeamProject (if any). For more details, see the Get-TfsTeamProject cmdlet. .PARAMETER Collection Specifies either a URL/name of the Team Project Collection to connect to, or a previously initialized TfsTeamProjectCollection object. When using a URL, it must be fully qualified. The format of this string is as follows: http[s]://<ComputerName>:<Port>/[<TFS-vDir>/]<CollectionName> Valid values for the Transport segment of the URI are HTTP and HTTPS. If you specify a connection URI with a Transport segment, but do not specify a port, the session is created with standards ports: 80 for HTTP and 443 for HTTPS. To connect to a Team Project Collection by using its name, a TfsConfigurationServer object must be supplied either via -Server argument or via a previous call to the Connect-TfsConfigurationServer cmdlet. For more details, see the Get-TfsTeamProjectCollection cmdlet. .INPUTS Microsoft.TeamFoundation.SourceControl.WebApi.GitRepository System.String #> Function Remove-TfsGitRepository { [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='High')] Param ( [Parameter(Mandatory=$true, ValueFromPipeline=$true)] [SupportsWildcards()] [Alias('Name')] [object] $Repository = '*', [Parameter()] [object] $Project, [Parameter()] [object] $Collection ) Begin { Add-Type -AssemblyName 'Microsoft.TeamFoundation.Core.WebApi' Add-Type -AssemblyName 'Microsoft.TeamFoundation.SourceControl.WebApi' } Process { if ($Repository -is [Microsoft.TeamFoundation.SourceControl.WebApi.GitRepository]) { $Project = $Repository.ProjectReference.Name } $tp = Get-TfsTeamProject -Project $Project -Collection $Collection $tpc = $tp.Store.TeamProjectCollection $gitClient = Get-TfsHttpClient -Type 'Microsoft.TeamFoundation.SourceControl.WebApi.GitHttpClient' if ($Repository -is [Microsoft.TeamFoundation.SourceControl.WebApi.GitRepository]) { $reposToDelete = @($Repository) } else { $reposToDelete = Get-TfsGitRepository -Name $Repository -Project $Project -Collection $Collection } foreach($repo in $reposToDelete) { if ($PSCmdlet.ShouldProcess($repo.Name, "Delete Git repository from Team Project $($tp.Name)")) { $gitClient.DeleteRepositoryAsync($repo.Id).Wait() } } } } <# .SYNOPSIS Renames a Git repository in a team project. .PARAMETER Project Specifies either the name of the Team Project or a previously initialized Microsoft.TeamFoundation.WorkItemTracking.Client.Project object to connect to. If omitted, it defaults to the connection opened by Connect-TfsTeamProject (if any). For more details, see the Get-TfsTeamProject cmdlet. .PARAMETER Collection Specifies either a URL/name of the Team Project Collection to connect to, or a previously initialized TfsTeamProjectCollection object. When using a URL, it must be fully qualified. The format of this string is as follows: http[s]://<ComputerName>:<Port>/[<TFS-vDir>/]<CollectionName> Valid values for the Transport segment of the URI are HTTP and HTTPS. If you specify a connection URI with a Transport segment, but do not specify a port, the session is created with standards ports: 80 for HTTP and 443 for HTTPS. To connect to a Team Project Collection by using its name, a TfsConfigurationServer object must be supplied either via -Server argument or via a previous call to the Connect-TfsConfigurationServer cmdlet. For more details, see the Get-TfsTeamProjectCollection cmdlet. .PARAMETER Passthru Returns the results of the command. By default, this cmdlet does not generate any output. .INPUTS Microsoft.TeamFoundation.SourceControl.WebApi.GitRepository System.String #> Function Rename-TfsGitRepository { [CmdletBinding(SupportsShouldProcess=$true)] [OutputType([Microsoft.TeamFoundation.SourceControl.WebApi.GitRepository])] Param ( [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] [object] $Repository, [Parameter(Mandatory=$true, Position=1)] [string] $NewName, [Parameter()] [object] $Project, [Parameter()] [object] $Collection, [Parameter()] [switch] $Passthru ) Begin { Add-Type -AssemblyName 'Microsoft.TeamFoundation.Core.WebApi' Add-Type -AssemblyName 'Microsoft.TeamFoundation.SourceControl.WebApi' } Process { if ($Repository -is [Microsoft.TeamFoundation.SourceControl.WebApi.GitRepository]) { $Project = $Repository.ProjectReference.Name } $tp = Get-TfsTeamProject -Project $Project -Collection $Collection $tpc = $tp.Store.TeamProjectCollection $gitClient = Get-TfsHttpClient -Type 'Microsoft.TeamFoundation.SourceControl.WebApi.GitHttpClient' if ($Repository -is [Microsoft.TeamFoundation.SourceControl.WebApi.GitRepository]) { $reposToRename = @($Repository) } else { $reposToRename = Get-TfsGitRepository -Name $Repository -Project $Project -Collection $Collection } foreach($repo in $reposToRename) { if ($PSCmdlet.ShouldProcess($repo.Name, "Rename Git repository in Team Project $($tp.Name) to $NewName")) { $task = $gitClient.RenameRepositoryAsync($repo, $NewName) $task.Wait() if ($Passthru) { return $task.Result } } } } } |