Get latest from Visual Studio Team Services using Command Line passing /login credentials with TF.exe

后端 未结 2 1324
傲寒
傲寒 2021-01-03 04:50

Has anyone had success getting latest source code from the Visual Studio Team Services (formerly Visual Studio Online, Team Foundation Service) Version Control Server using

2条回答
  •  萌比男神i
    2021-01-03 05:39

    Here is a snippet using .NET TFS libraries & Powershell integration that worked for us - tf.exe does not play well with VSO authorization. Curious if anyone else has success using this route. We are using ADFS, so the powershell process is run as the user we want to authenticate with.

    TF Get Latest Workspace.ps1

    $path = "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v2.0" 
    Add-Type -Path "$path\Microsoft.TeamFoundation.Client.dll"
    Add-Type -Path "$path\Microsoft.TeamFoundation.VersionControl.Client.dll"
    Add-Type -Path "$path\Microsoft.TeamFoundation.VersionControl.Common.dll"
     
    $collection = "https://mycorp.visualstudio.com/defaultcollection"
    $tpc = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($collection)
    $vc = $tpc.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
    
    # retrieve workspace by path 
    $projectPath = "$/MyApp/MyBranch"
    $workspacePath = "C:\Projects\MyApp\MyBranch"
    $workspace = $vc.GetWorkspace($workspacePath)
    
    
    # get full download every time (tf get /force /recursive)
    function DownloadAllFiles([Microsoft.TeamFoundation.VersionControl.Client.Workspace] $workspace, [string] $projectPath) {     
        $recursionType = [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::Full
        $versionSpec = [Microsoft.TeamFoundation.VersionControl.Client.LatestVersionSpec]::Instance
        $itemSpec = new-object Microsoft.TeamFoundation.VersionControl.Client.ItemSpec($projectPath,$recursionType)
        $getRequest = New-Object Microsoft.TeamFoundation.VersionControl.Client.GetRequest($projectPath,$recursionType,$versionSpec)
        $getOptions = [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::GetAll -bor [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::Overwrite 
        $workpaceStatus = $workspace.Get($getRequest, $getOptions)
        write-output $workpaceStatus 
    }
    
    # get delta download - changes only (retrieves all mapped items)
    function DownloadWorkspaceUpdates([Microsoft.TeamFoundation.VersionControl.Client.Workspace] $workspace) {
        $workpaceStatus = $workspace.Get()
        write-output $workpaceStatus 
    }
    
    # get delta download - changes only (retrieves specific mapped items)
    function DownloadWorkspaceUpdates([Microsoft.TeamFoundation.VersionControl.Client.Workspace] $workspace, [string] $projectPath) {
        $recursionType = [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::Full
        $versionSpec = [Microsoft.TeamFoundation.VersionControl.Client.LatestVersionSpec]::Instance
        $itemSpec = new-object Microsoft.TeamFoundation.VersionControl.Client.ItemSpec($projectPath,$recursionType)
        $getRequest = New-Object Microsoft.TeamFoundation.VersionControl.Client.GetRequest($projectPath,$recursionType,$versionSpec)
        $getOptions = [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::Overwrite 
        $workpaceStatus = $workspace.Get($getRequest, $getOptions)
        write-output $workpaceStatus 
    }
    
    # force latest download (slower)
    DownloadAllFiles -workspace $workspace -projectPath $projectPath
    
    # download deltas (fast), all mappings
    DownloadWorkspaceUpdates -workspace $workspace
    
    # download deltas (fast), specific mapping
    DownloadWorkspaceUpdates -workspace $workspace -projectPath $projectPath
    

    This could easily be extended to support TfsClientCredentials (i.e. Alternate Access credentials). I prefer the powershell approach as it doesn't require compilation or an extra EXE to copy around.

提交回复
热议问题