Using TFS 2010 Source Control From Command Line tf.exe is FUN!

Abstract: Team Foundation Server (TFS 2010) administrators have experienced that managing source control from the command line (tf.exe) is faster and more convenient. In this article, we are going to explore the capability to build customized utilities using a set of commands provided by TFS in tf.exe

Team Foundation Server (TFS 2010) administrators have experienced that managing source control from the command line (tf.exe) is faster and more convenient. In this article, we are going to explore the capability to build customized utilities using a set of commands provided by TFS in tf.exe. I am going to demonstrate this by creating simple scripts as utilities.

tf.exe is the Team Foundation source control command-line tool which can be used to perform source control operations without GUI interaction. Using the commands from tf.exe, a script file of repeated series of commands can be prepared (as .bat files) and the same can be used to automate recurring tasks by executing the script files as a ‘batch’.

This activity of creating useful scripts (for tailored requirements of source control) using TFS tf.exe is very flexible, useful, swift and hence enjoyable for administrators.

You can test a couple of tf.exe commands on this command prompt. Get a list of tf commands by just typing tf /? on the command prompt.

Few commonly used tf.exe commands

tf /? – list all available tf.exe commands

tf workspace – view details about the current workspace

tf workfold – view a list of server folders and the local folders they’re mapped to, for the current workspace

tf workspaces – view a list of all workspaces on the current machine

tf dir – view a listing of files & folders in the source repository folder mapped to the current local folder

tf status – view pending changes in the current workspace

For more details on these commands, please refer to Annex-A and Annex-B of this article.

In this article, I am going to create three sample utilities for the TFS source control by preparing small script files (using notepad) consisting of tf commands. Though these are working scripts, the basic objective of these samples is to demonstrate some of the benefits of tf.exe facility provided by TFS.

Assumptions:

On the TFS Version Control Proxy, the Team Explorer Client is installed. This will also ensure that the TFS command line client (tf.exe) will be installed.

You are having administrative rights and permissions to execute the scripts.

A directory to act as a temporary dumping ground will be created. In this case this is C:\A

It is assumed that these executable script files (.bat files) are stored in local directory of proxy c:/A

Workplace for proxy will be created in this folder

3 Sample utilities prepared by using TFS tf.exe commands

A. Pre-caching of TFS Proxy by ‘getting’ the files on daily basis

Since you can only deploy one master TFS server and replication mechanisms are not supported, a geographically distributed development team working at long distances from a TFS Server, might consider using a TFS Proxy server, which caches the downloads from the Team Foundation Version Control.

The proxy gets populated with an item the first time someone requests that item, for a particular version. It's not a continuous synchronization/replication. The first time a proxy user gets a version of an item that doesn't exist in the cache yet, it will obviously take longer when compared to an item which is in the cache already (a "cache hit").

Anticipating that for most number of times, a team working at remote place prefers to work on the latest version, a small script can be written using command line prompts of TFS. This can then be run by Proxy administrator, that will download all the latest version from TFS source control repository and share the same with local team. With this utility, the distributed development team working at long distances from a TFS Server might save their valuable time required to get the files. On a daily basis, the script will ensure that the files are getting downloaded on proxy, available for the proxy users.

@echo off

REM ....this utility script will daily get you all latest versions from TFS source control repository (for your Proxy Server)

cd /

mkdir proxyworkspace

REM creating new file folder (directory) at root as physical proxy workspace which is one time task

cd proxyworkspace

tf workspace /new proxyworkspace /noprompt

REM mapping the workspace with newly created physical folder

REM following scheduling command will ensure that on daily basis on the same time specified task will be executed

This script need to be run from Administrator: Visual Studio Command Prompt (2010) only.

Please note that “tf get” command will retrieve a read-only copy of files from TFS repository to local workspace and create a folder on the disk to hold these files. A ‘Checkout’ command is needed to make these files ‘editable’, however changing the file status from ‘read-only’ to ‘editable’ is not going to impact the traffic and will not consume time as compared to actually getting the latest version of file from TFS repository to proxy. Therefore the ‘checkout’ command which makes these files editable, is not included in the script. On a need basis, proxy users need to make the status of selected downloaded files as ‘editable’ by issuing the checkout command separately.

B. Optimize the storage on the server by deleting the ‘shelvesets’ created by the users periodically

Team Foundation Server source control has a great feature called Shelving. Shelving lets individual users store a batch of pending changes onto the server and optionally remove them from the local workspace. It comes in really handy at times when users want to backup code and store it on the server but don't want to commit it to source control.

However, most of the times when users shelve, it is a temporary thing. User creates a shelveset and then unshelves it - and then users no longer wants it. The old shelvesets will sit on the shelf, gathering dust until a user manually deletes them.

Though the unshelve dialog has a "Delete" button that can be used to delete a particular shelveset; rarely users delete unwanted stuff and thus unnecessary baggage increases and becomes an overheads on the system. It's a good practice to delete the shelvesets when you no longer need them. Not only will it get rid of clutter for you, it will also help when a team member is trying to unshelve something that you have placed for them.

The following small utility generated using TFS tf.exe command line script will periodically (say monthly) delete such files for specified users.

@echo off

REM ....this utility script will delete the shelveset files of the specified owners (one after another) on monthly basis

SET /P M=Type 1 for next owner batch, 2 for completion then press ENTER:

IF %M%==1 GOTO STARTBATCH

IF %M%==2 GOTO ENDBATCH

:ENDBATCH

REM ....monthly accumulated files from shelvesets of specified owners will get deleted successfully

This utility will create batches for specified users to delete their shelvesets on periodic (Monthly) basis automatically.

C. Optimize the storage on the server by deleting the workplaces created by a specific user

In certain real-life situations (for example a team member has resigned and left the organization), it is becomes necessary to delete all the files and folders from the workspace which are not needed anymore. You can use the following utility -

SET /P M=Type 1 for next workspace deletion, 2 for completion of task, then press ENTER:

IF %M%==1 GOTO STARTDELETE

IF %M%==2 GOTO ENDDELETE

:ENDDELETE

echo ....files from specified workspaces got deleted successfully

This utility is asking names of the specific workspaces created by user (one-by-one) and removes the same.

Summary:

You can automate many similar tasks such as compiling, packaging, or archiving source code by scripting the tf command-line tool in Team Explorer provided by TFS-2010. You can use either external or internal automation, depending on which method best suits your needs.

You can download the entire source code of this article over here

Annex-A: Categories of command prompts provided by TFS-2010

Informational Commands

Listing and description of Team Foundation source control informational commands.

Command-Line Syntax

Description of Team Foundation source control command line syntax.

Command-Line Options

Overview of Team Foundation source control command line options.

Command-Line Exit Codes

Lists the exit codes for Team Foundation source control command line commands.

Operations Available Only From the Command-Line

Lists operations you can perform only from the command line.

Team Foundation Source Control Scripts and Command Files

Explains how scripts and command files can be used in association with Team Foundation source control command line commands.

Tf Command-Line Utility Commands

Lists all of the commands for Team Foundation source control.

Annex-B: Some commonly used Command line list (with purpose & syntax) for reference

Command Type

Purpose

Syntax

Add Command

Adds files and folders from a local file system to a server for Team Foundation version control.

tf add itemspec [/lock:(none|checkin|checkout)] [/type:filetype]

[/noprompt] [/recursive] [/login:username,[password]]

Branch Command

The branch command copies an item or set of items, including metadata and version control history, from one location to another in the Team Foundation version control server and in the local workspace.

tf branch olditem newitem [/version:versionspec]

[/noget] [/lock:(none|checkin|checkout)]

[/noprompt] [/silent] [/checkin]

[/comment:("comment"|@commentfile)]

[/author:authorname] [/login:username, [password]]

Branches Command

Displays the history of a branch for a specified file or folder.

tf branches itemspec [/version:versionspec]

[/collection:TeamProjectCollectionUrl]

[/login:username,[password]]

Changeset Command

Displays information about and lets you change the attributes, such as comments and check-in notes, that are associated with a changeset.

Stores a set of pending changes, together with pending check-in notes, a comment, and a list of associated work items on the server that is running Visual Studio Team Foundation Server without actually checking them into the version control server.

tf shelve [/replace]

[/comment:("comment"|@commentfile)]

[shelvesetname] [/validate][/noprompt] [/login:username,[password]]

Shelvesets Command

Displays information about a set of shelved changes.

tf shelvesets [/owner:ownername]

[/format:(brief|detailed)] [/collection:TeamProjectCollectionUrl]]

[/login:username,[password]] shelvesetname

Status Command

Displays information about pending changes to items in one or more workspaces.

Hi Satish,
This is a really good article on enlisting the latest code from TFS. However, I would like to know How we can build the solution what we get from the TFS and then deploy it on the local machine. Is this possible? If so can you please update the same in the post as well.

Thanks in advance.
Sham_

Comment posted by
Carol
on Wednesday, April 24, 2013 3:21 AM

It would have been more help helpful if some examples would have been provided. I m facing difficulty in creating a workspace. I need an example

Comment posted by
pregunton
on Friday, November 28, 2014 9:32 AM

I use TFS 2008, VS 2010, Addin VS, TeamFoundationClient.

Is it possible:

use tf merge command programmatically in C# (in my Addin VS code) ?

do Merge, known if there are conflicts and resolve conflicts using API TeamFoundationClient ?