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

Posted by: Satish Chinchorkar , on 3/22/2012, in Category Visual Studio, VSTS & TFS
Views: 135011
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.

 

Let’s get into action! Follow these steps:

Open the Administrator command prompt. To do so, login to TFS as the administrator user. From the ‘Start’ button, select ‘All programs’ > MS VS 2010 > Visual Studio tools > Visual Studio > Command prompt.

You will get a command line screen as shown below:

image

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

schtasks /create /tn "Get Latest Version" /tr C:\A\getlatestversion.bat /sc DAILY /ru SYSTEM

tf get /recursive /all /noprompt

REM fetching the latest (read-only) files in this newly created workspace

echo fetched the latest (read-only) files in the proxy workspace for the date:

date /T

REM ....latest file versions got downloaded in the specified folder (proxyworkspace at root) successfully

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

:STARTBATCH

schtasks /create /tn "Get Latest Version" /tr C:\A\deleteshelveset.bat /sc MONTHLY /ru SYSTEM

echo....Pl enter the owner name for the shelveset to be deleted

tf shelve /delete | tf shelvesets /%1:* /noprompt

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 -

@echo off

REM ....this utility script will delete workspace files of the specified users

:STARTDELETE

echo....Pl enter the workspace name to be deleted

tf workspace /delete %1 /noprompt /recursive

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.


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

[/notes:("NoteFieldName"="NoteFieldValue"|@notefile)] [/noprompt][/collection:TeamProjectCollectionUrl]]

[changesetnumber | /latest][/login:username,[password]]


Checkin Command


Commits pending changes in the current workspace to the server for Team Foundation version control.

tf checkin [/author:author name] 
[/comment:("comment"|@comment file)] 
[/noprompt] [/notes:("Note Name"="note text"|@notefile)] 
[/override:(reason|@reasonfile)] 
[/recursive] [/saved] [/validate] [itemspec] [/bypass]
 [/login:username,[password]]

Checkout and Edit Commands


Makes the local file writable, and changes its Pending Change status to "edit" in the workspace. Edit is an alias for the Checkout command.

tf checkout [/lock:(none|checkin|checkout)] [/recursive] 

Configure Command


Enables an administrator to view and change the following configuration settings for a team project in the Source Control Settings dialog box for Check-out settings, Check-in policies & Check-in notes


tf configure [PathOfTeamProject] [/collection:TeamProjectCollectionUrl]

[/login:username,[password]]


Delete Command (Team Foundation Source Control)


Removes files and folders from the Team Foundation version control server and deletes them from the disk.

tf delete [/lock:(none|checkin|checkout)] 
[/recursive] [/login:username,[password]] itemspec

Difference Command


Compares, and if it is possible, displays differences between two files, files in two folders, or a shelveset and a local or a server file.


tf diff[erence] itemspec [/version:versionspec]

[/type:filetype]

[/format:format [/ignorespace] [/ignoreeol]

[/ignorecase] [/recursive]

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


Dir Command


The dir command displays all or some of the contents of the server for Team Foundation version control.

tf dir itemspec [/version:versionspec] [/recursive] 
[/folders] [/deleted] [/login:username,[password]] 
[/collection:TeamProjectCollectionUrl]

Get Command


Retrieves a read-only copy of a file from the server for Team Foundation version control to the workspace and creates folders on disk to contain it.


tf get [itemspec] [/version:versionspec] [/all]

[/overwrite] [/force]

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


Help Command (Team Foundation Source Control)


Displays help on the command line that contains information about syntax for a Team Foundation version control command.

tf help command name

History Command


Displays the revision history for one or more files, folders or both.


tf history itemspec [/version:versionspec]

[/stopafter:number] [/recursive] [/user:username]

[/format:(brief|detailed)] [/slotmode] [/itemmode] [/noprompt] [/login:username,[password]] [/sort:ascending,descending] [/collection:TeamProjectCollectionUrl]


Label Command (Team Foundation Source Control)


Attaches a label to or removes a label from a version of a file or folder in the server for Team Foundation version control.


tf label labelname[@scope] [/owner:ownername]

itemspec [/version:versionspec] [/comment:("comment"|@commentfile)]

[/child:(replace|merge)] [/recursive] [login:username,[password]] [/collection:TeamProjectCollectionUrl]


Labels Command


Displays the list of labels in the server for Team Foundation version control.

tf labels [/owner:ownername] [/format:(brief|detailed)] 
[/collection:TeamProjectCollectionUrl] [labelname] [/login:username,[password]]

Lock Command


Locks or unlocks a file or folder to deny or restore the right of users to check out an item for edit into a different workspace or to check in pending changes to an item from a different workspace.

tf lock itemspec /lock:(none|checkout|checkin) 
[/workspace:workspacename] [/recursive] 
[/login:username,[password]] [/collection:TeamProjectCollectionUrl] 

Merge Command


The merge command applies changes from one branch into another.

tf merge [/recursive] [/force] [/candidate] [/discard] 
[/version:versionspec] [/lock:none|checkin|checkout] [/preview] 
[/baseless] [/nosummary]
 [/noimplicitbaseless] 
[/conservative] [/format:(brief|detailed)] 
[/noprompt] [/login:username,[password]] source destination

Merges Command


Displays detailed information about past merges between the specified source and destination branches.

tf merges [source] destination [/recursive] 
[/extended] [/format:(brief|deltailed)]
[/login:username, [password]] [/showall]]] 
[/collection:TeamProjectCollectionUrl]

Permission Command


Modifies the user access control list (ACL) and displays authorization settings for an item under version control.

tf permission [/allow:(* |perm1[,perm2,...]] 
[/deny:(* |perm1[,perm2,...])] [/remove:(* |perm1[,perm2,...])] 
[/inherit:yes|no] [/user:username1[,username2,...]] 
[/group:groupname1[,groupname2,...]] 
[/collection:TeamProjectCollectionUrl] 
[/recursive] itemspec [/global][/login:username,[password]]

Properties Command


Displays information about items under version control.

tf properties [/collection:TeamProjectCollectionUrl]
 [/recursive] [/login:username,[password]]
itemspec [/version:versionspec] [/workspace] 

Rename Command (Team Foundation Source Control)


The rename command changes the name or the path of a file or folder. You can use the rename command or the aliases move or ren, to move a file or folder to a new location.

tf rename [/lock:(none|checkout|checkin)] 
[/login:username,[password]] olditem newitem

Resolve Command


Lets you resolve conflicts between changed items in your workspace and the latest or destination versions of items on the server.


tf resolve [itemspec]

[/auto:(AutoMerge|TakeTheirs|KeepYours|

OverwriteLocal|DeleteConflict

|KeepYoursRenameTheirs)]

[/preview] [(/overridetype:overridetype | /converttotype:converttype] [/recursive]

[/newname:path] [/noprompt]

[/login:username, [password]]


Shelve Command


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.


tf status itemspec

[/collection:TeamProjectCollectionUrl]

[/login:username,[password]] ([/workspace:workspacename[;workspaceowner]]|

[/shelveset:shelvesetname[;shelvesetowner]])

[/format:(brief|detailed)]

[/recursive][/user:(*|username)]


Undelete Command


The undelete command restores items that were previously deleted.

tf undelete [/noget] [/lock:(none|checkin|checkout)] 
[/recursive] itemspec[;deletionID] [/login:username,[password]]

Undo Command


Removes pending changes from a workspace.

tf undo [/workspace:workspacename
[;workspaceowner]] 
 [/recursive] itemspec [/noprompt] 
[/login:username, [password]] 
[/collection:TeamProjectCollectionUrl]

Unlabel Command


Removes an item from an existing label in the server for Team Foundation version control.

tf unlabel [/collection:TeamProjectCollectionUrl]
 [/recursive] [/login:username, [password]] labelname itemspec

Unshelve Command


Restores shelved file revisions, check-in notes, comments, and work item associations to the current workspace or removes an existing shelveset from the server.

tf unshelve [/move] [shelvesetname[;username]]
 itemspec 
[/recursive] [/noprompt][/login:username,[password]]

View Command


The view command retrieves a specific version of a file to a temporary folder on your computer and displays it.

tf view [/collection:TeamProjectCollectionUrl] 
[/console] [/recursive] [/output:localfile]
[/shelveset:shelvesetname[;owner]]
 [/noprompt] itemspec 
[/version:versionspec] [/login:username,[password]]

Workfold Command


Creates, modifies, or displays information about the mappings between your workspace folders and the folders on the server for Team Foundation version control.

tf workfold localfolder [/login:username,[password]]

Workspace Command


Lets you create, delete, view, or modify properties and mappings associated with a workspace.


tf workspace /new [/noprompt]

[/template:workspacename[;workspaceowner]]

[/computer:computername] [/comment:("comment"|@comment file)]

[/collection:TeamProjectCollectionUrl] [/permission:(Private|PublicLimited|Public)]

[workspacename[;workspaceowner]]

[/login:username,[password]]


Workspaces Command


Displays information about workspaces in the system and updates cached information about a user name or computer name change on the server that is running Visual Studio Team Foundation Server.

tf workspaces [/owner:ownername] [/computer:computername] 
[/collection:TeamProjectCollectionUrl]
 [/format:(brief|detailed)] 
[/updateUserName:oldUserName] [/updateComputerName:oldComputerName] 
[workspacename][/login:username,[password]]

Give me a +1 if you think it was a good article. Thanks!
Recommended Articles


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by Sham on Tuesday, February 26, 2013 11:27 PM
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

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel