7

I'm writing a script that will run a build only if there is a change in source code. I need to know whether there is a change since the build last ran. This used to work because the folder would not be deleted, so it was easy to determine whether there was a change, but now everything is deleted everytime the build is run. I thought about using the TFS TF history command to query the last changeset or the last two changesets, but have had issues parsing just the changeset number from the output. I also considered using the changeset command. Is there any command-line parameter that I can use to answer the question, has there been a change since either a date, or a changeset number?

Joan Arc
  • 71
  • 1
  • 1
  • 2
  • First, Team Build has a feature that does this already. Are you using something else? Second, is _any_ change in the repository sufficient to trigger the build, or do you need to see whether there is a change in some specific directory or directories? – Andrew Apr 26 '12 at 17:22
  • Yeah, I'm using a different system, buildforge specifically. We use a TFS adaptor for CI builds, but we only want to run the build if there is an update. Since the build is branch specific, I only want to know if there is a change in that branch/folder. – Joan Arc Apr 27 '12 at 18:37
  • The answer from Guanghui Qin, works very well, is very fast and you don´t need a workspace for it. – Yogurtu Mar 10 '16 at 20:49

6 Answers6

6

To the latest changeset number without local workspace, please use this command:

tf history /collection:"http://server:8080/tfs/Collection" "$/Project" /recursive /stopafter:1 /noprompt /login:domain\user,password
xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
Toby Qin
  • 61
  • 1
  • 3
3

excerpt from my batch file to build.

set _aPath="f:\TFS\a"
set _TFPath="c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE"

...

pushd %_aPath%
%_TFPath%\TF.exe history . /r /noprompt /stopafter:1 /Version:W > temp
FOR /f "tokens=1" %%a in ('findstr /R "^[0-9][0-9]*" temp') do set _BuildVersion=10.3.0.%%a
del temp
popd

uses a temp file, but works well.

Nathan
  • 153
  • 1
  • 6
  • Note that you don't require the intermediate file, e.g.: `tf vc info d:\w\code04\Server.sln | findstr /R "Last modified: [^\r\n]+r\n"` – Dustin Oprea Sep 27 '16 at 21:09
2

As Andrew mentioned, TFS has continuous integration functionality built-in. However, yes, it's easy to query the changesets since a certain point, be it a date or a changeset. You want to look at the history between that changeset and latest:

tf history <folder> /version:C<changeset>~T /noprompt /recursive

If the only line output is the changeset you queried for, then obviously there have been no changes since that checkin. Otherwise, you will see the additional changesets, one per line.

Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
  • 1
    Yeah, this tells me the changesets including the one queried. How could I parse the number alone from the output? Is there a way to parse using batch commands? – Joan Arc Apr 27 '12 at 18:38
1

There is an adaptor that can integrate BuildForge and Microsoft Team Foundation Server. Here is the url if you are interested...http://www-304.ibm.com/partnerworld/gsd/solutiondetails.do?&solution=46360&lc=en

The Automatra TFS Adaptor for Rational Build Forge provides Continuous Integration (CI) and reporting capabilities.

The TFS Adaptor further enables CI capabilities at both the TFS Source (Change Set) and WorkItem levels. Out of the box reporting delivers clear Bill of Materials (BOM) reports that can be delivered to downstream consumer of your builds.

Finally, as you must be aware, the strength of Build Forge is its capability to bridge build with deployment (and beyond). Obviously, with these Continuous Integration capabilities you can then move forward with the continuous delivery capability I believe you wish to achieve.

Community
  • 1
  • 1
Ka Ho
  • 19
  • 2
1

My PowerShell script that is called GetVcsRevision.ps1 and located in subfolder of VCS Root:

param (
    [string]$PathToTF='C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\TF.exe'
    ,[Parameter(Mandatory=$true)][string]$Login
    ,[Parameter(Mandatory=$true)][string]$Password
)
$result = &$PathToTF @("history","/stopafter:1","/recursive","..\*","/login:""$Login"",""$Password""") 2>&1 3>&1

if ($result.GetType().Name -eq "Object[]")
{
    <# $result format is:
        Changeset User              Date       Comment
        --------- ----------------- ---------- ----------------------------------------
        26038     user              24.06.2014 Sample commit comment

        $result[2] is:
        26038     user              24.06.2014 Sample commit comment

        $result[2].Split(" ")[0] is:
        26038
    #>

    $result[2].Split(" ")[0]
}
else
{
    "0"
}

It is sending last changeset number to out pipe. If something goes wrong, then this number is 0.

You can make a function from this script and call it in you build script.

v.karbovnichy
  • 3,183
  • 2
  • 36
  • 47
1

My one-line command:

for /f "usebackq tokens=*" %%a in (`tf history . /recursive /noprompt /stopafter:1 /version:T ^| powershell -Command "$input | ? { $_ -imatch '^(\d+)\s+' } | %% { $matches[0].Trim() } | Select-Object -First 1"`) do set TIP_CHANGESET=%%a

after execution TIP_CHANGESET env. variable contains tip changeset

Sergey Azarkevich
  • 2,641
  • 2
  • 22
  • 38