为了签出文件,您首先需要一个工作区映射。必须是服务器映射才能签出文件,本地工作区没有签出选项。
tf vc workspace new /noprompt YourUniqueNameHere /location:server
然后将本地文件夹映射到 TFVC 存储库中的服务器
tf vc workfold /map $/Project/Path c:\A\Local\Directory /workspace YourUniqueNameHere
然后调用get
或checkout
。我不是 100% 您可以在不先获取文件的情况下签出文件。
您可以在不依赖tf.exe
纯 powershell 的情况下完成同样的事情。您可以在我的 Azure Pipelines TFVC 任务中找到完整示例。您仍然需要一个本地工作区才能正常工作:
[string[]] $FilesToCheckout = $ItemSpec -split ';|\r?\n'
$RecursionType = [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]$Recursion
Write-Message "Checking out ItemSpec: $ItemSpec, Recursive: $RecursionType"
Try
{
$provider = Get-SourceProvider
if (-not $provider)
{
return;
}
Foreach ($change in $FilesToCheckout)
{
Write-Message "Checking out: $change"
$provider.Workspace.PendEdit(
@($change),
$RecursionType,
$null,
[Microsoft.TeamFoundation.VersionControl.Client.LockLevel]"Unchanged"
) | Out-Null
}
}
Finally
{
Invoke-DisposeSourceProvider -Provider $provider
}
从本地工作区缓存中查找工作区的代码可以在共享模块中找到:
function Get-TfsTeamProjectCollection()
{
$ProjectCollectionUri = Get-VstsTaskVariable -Name "System.TeamFoundationCollectionUri" -Require
$tfsClientCredentials = Get-VstsTfsClientCredentials -OMDirectory $(Find-VisualStudio)
$collection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection(
$ProjectCollectionUri,
$tfsClientCredentials)
$collection.EnsureAuthenticated()
return $collection
}
function Get-SourceProvider {
[cmdletbinding()]
param()
Write-Debug "Entering Get-SourceProvider"
$provider = @{
Name = $env:BUILD_REPOSITORY_PROVIDER
SourcesRootPath = $env:BUILD_SOURCESDIRECTORY
TeamProjectId = $env:SYSTEM_TEAMPROJECTID
}
$success = $false
try {
if ($provider.Name -eq 'TfsVersionControl') {
$provider.TfsTeamProjectCollection = Get-TfsTeamProjectCollection
$versionControlServer = $provider.TfsTeamProjectCollection.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
$versionControlServer.add_NonFatalError($OnNonFatalError)
$workstation = [Microsoft.TeamFoundation.VersionControl.Client.Workstation]::Current
$workstation.EnsureUpdateWorkspaceInfoCache($versionControlServer, $versionControlServer.AuthorizedUser)
$provider.VersionControlServer = $versionControlServer;
$provider.Workspace = $versionControlServer.TryGetWorkspace($provider.SourcesRootPath)
if (!$provider.Workspace) {
Write-Message -Type Debug "Unable to determine workspace from source folder: $($provider.SourcesRootPath)"
Write-Message -Type Debug "Attempting to resolve workspace recursively from locally cached info."
$workspaceInfos = $workstation.GetLocalWorkspaceInfoRecursively($provider.SourcesRootPath);
if ($workspaceInfos) {
foreach ($workspaceInfo in $workspaceInfos) {
Write-Message -Type Debug "Cached workspace info discovered. Server URI: $($workspaceInfo.ServerUri) ; Name: $($workspaceInfo.Name) ; Owner Name: $($workspaceInfo.OwnerName)"
try {
$provider.Workspace = $versionControlServer.GetWorkspace($workspaceInfo)
break
} catch {
Write-Message -Type Debug "Determination failed. Exception: $_"
}
}
}
}
if ((!$provider.Workspace) -and $env:BUILD_REPOSITORY_TFVC_WORKSPACE) {
Write-Message -Type Debug "Attempting to resolve workspace by name: $env:BUILD_REPOSITORY_TFVC_WORKSPACE"
try {
$provider.Workspace = $versionControlServer.GetWorkspace($env:BUILD_REPOSITORY_TFVC_WORKSPACE, '.')
} catch [Microsoft.TeamFoundation.VersionControl.Client.WorkspaceNotFoundException] {
Write-Message -Type Debug "Workspace not found."
} catch {
Write-Message -Type Debug "Determination failed. Exception: $_"
}
}
if (!$provider.Workspace) {
Write-Message -Type Warning (Get-LocalizedString -Key 'Unable to determine workspace from source folder ''{0}''.' -ArgumentList $provider.SourcesRootPath)
return
}
if ($provider.Workspace.Location -eq "Server")
{
Write-Warning "Server workspace support is experimental."
}
$provider.Workspace.Refresh()
$success = $true
return New-Object psobject -Property $provider
}
Write-Warning ("Only TfsVersionControl source providers are supported for TFVC tasks. Repository type: $provider")
return
} finally {
if (!$success) {
Invoke-DisposeSourceProvider -Provider $provider
}
Write-Message -Type Debug "Leaving Get-SourceProvider"
}
如果您愿意,此代码也应该包含足够的信息来构建新的工作区。