R – Determine the used source control system for a local folder (Subversion or TFS)

powershellsvntfsversion control

Summary: How can I determine TFS source control configuration for a local folder.

Explanation:

In our Software Factory I am creating CruiseControl.NET configuration files automatically based on the local folder where code from a source control system is living. I currently only want to support Subversion and TFS. Determining that Subversion is used as source control system, and determining the location of the repository is easy using the svn.exe info command, like shown in the PowerShell code below. But how can I do this when TFS is used?

One of the "problems" is that the currently logged-in user does NOT have access to the TFS server. We can't use integrated security because the dev machines are not domain-joined.

The closest I got was tf dir , I have to enter credentials in a popup box. Couldn't find command-line arguments to specify user and password. tf properties looks promising as well, but you must be authenticated to execute call. Subversion can provide info without authentication.

My current code in PowerShell with working code for SubVersion, the TFS code is missing;-)
Note that for the actual creation of the CruiseCOntrol.NET block we have a configured source control username and password available in global variables.

#
#   
#       
#           https://svn.mycompany.nl/svn/Acme.Intranet
#       
#   
#
#   If under source control return a CruiseControl.NET source control configuration block in
#   the following format:
#   
#       https://svn.mycompany.nl/svn/Acme.Intranet
#       c:\project\Acme.Intranet.Build\WorkingDirectory\$configuration
#       acmebuild
#       acmepassword
#           
#.Parameter configuration
#   Either "Debug" or "Release"
# .Returns
#   A string with the configuration block, or $null if not under Subversion source control.
##>
function GetSubversionSourceControlBlock
{
    param
    (
        $configuration
    )

    $svn_exe = Join-Path -Path $productfolder -ChildPath "tools\DotNet2\svn-win32-1.5.2\svn.exe"
    $svnProductInfo = [xml](& $svn_exe info `"$productFolder`" --xml)
    $trunkUrl = $svnProductInfo.info.entry.url
    if ($trunkUrl -ne $null)
    {
@"
        
            $trunkUrl
            $buildfolder\WorkingDirectory\$configuration
            $MastConfigurationGlobal_SourceControlUserName
            $MastConfigurationGlobal_SourceControlPassword
                
"@
        Verbose "Under Subversion source control. Trunk Url: $trunkUrl"
    }
    else
    {
        Verbose "Not under Subversion source control"
        $null
    }
}   

#
#       tfs.mycompany.nl
#       $/Acme.Intranet
#       c:\project\Acme.Intranet.Build\WorkingDirectory\$configuration
#       acmebuild
#       acmepassword
#           
#.Parameter configuration
#   Either "Debug" or "Release"
# .Returns
#   A string with the configuration block, or $null if not under Tfs source control.
##>
function GetTfsSourceControlBlock
{
    param
    (
        $configuration
    )

    $null
}   

Best Answer

The story continues:

If you execute the tf command from a cmd prompt, everything goes as described in the previous answer. If you do it from a PowerShell command prompt you get the following:

PS C:\> & tf.exe workfold c:\projects\Macaw.SolutionsFactory\FactoryIdeTools\trunk /login:macaw\serge,Trasimeno5
TF10125: The path 'c:\projects\Macaw.SolutionsFactory\FactoryIdeTools\trunk' must start with $/

It gives an error!

I have tried everything, but to no avail. I even tried it through the TFS object model, but that got really complex. To get the workspace for a local folder you need a name of the tfs server first. It is probably possible to iterate all currently mapped workspaces and iterate through the servers defined there (that is probably what tf.exe does) to test if the local folder belongs to that workspace.

I finally came up with the following solution:

I create a simple batch file ExecTfWorkprodCommand.bat wit the following code:

@echo off
rem This is a kind of strange batch file, needed because execution of this command in PowerShell gives an error.
rem This script retrieves TFS sourcecontrol information about a local folder using the following command:
rem tf workfold  /login:domain\user,password
rem %1 is path to the tf.exe executable
rem %2 is the local path
rem %3 is the domain\user [optional, integrated security used if ommited]
rem %4 is the password    [optional, if %3 is not specified]
rem Output is in format:
rem ===============================================================================
rem Workspace: Macaw.SolutionsFactory@VS-D-SVDOMOSS-1 (Serge)
rem Server   : tfs.macaw.nl
rem $/Macaw.SolutionsFactory/FactoryIdeTools/trunk: C:\Projects\Macaw.SolutionsFactory\FactoryIdeTools\trunk

if [%3]==[] goto integratedsecurity
%1 workfold "%2" /login:%3,%4
goto end

:integratedsecurity
%1 workfold "%2"

:end

And I call this batch script from PowerShell. From the result I want to parse:

  • Tfs server name
  • Folder on server

Example code on how to solve this:

$tfsProductInfo = & "ExecTfWorkprodCommand.bat "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\TF.exe" "c:\projects\Macaw.SolutionsFactory\FactoryIdeTools\trunk" domain\user password
if ($tfsProductInfo -eq $null)
{
    Write-Output "Can't determine Tfs source control information on local folder"
}
else
{
    $tfsserver = [Regex]::Split($tfsProductInfo[2], ": ")[1].Trim()
    $serverpath = [Regex]::Split($tfsProductInfo[3], ": ")[0].Trim()
    $localpath = [Regex]::Split($tfsProductInfo[3], ": ")[1].Trim()
    Write-Output "Server     : $tfsserver"
    Write-Output "Server path: $serverpath"
    Write-Output "Local path : $localpath"
}