在 TFS 的工作区中获取当前的变更集 id

如何计算当前在本地工作区中的变更集?

当然,我可以选择一个文件并查看其历史记录。但是,如果该文件最近没有更新,则其变更集可能比同一解决方案中最近更新的文件更旧。

我们可能会犯的一个错误是,我们在解决方案文件上查看历史记录,但是解决方案文件很少更改,除非您添加新的项目/进行解决方案级别的更改。

最后,为了弄清楚变更集,我需要记住最新的文件变更了什么,并查看它们的历史记录。

还有更好的办法吗?

25520 次浏览

Your answer is on a MSDN blog by Buck Hodges: How to determine the latest changeset in your workspace

from the root (top) of your workspace, in cmd perform:

tf history . /r /noprompt /stopafter:1 /version:W

If you want to use PowerShell (see also; equivalent to answer of @kroonwijk):

  1. enable tfs snapin (once, if not already)

    add-pssnapin Microsoft.TeamFoundation.PowerShell

  2. use tfs cmdlet to get current changeset id

    Get-TfsItemHistory <PATH_TO_PROJECT> -Recurse -Stopafter 1 -Version W

The common answer to use tf.exe history . /r directly does work, but it can be horribly slow. In our case it takes 10-15 seconds. I now use a two stage check, first checking the revision of some arbitrary files (I'm using the files in the root folder).

With powershell:

$tfexepath = "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\tf.exe"
$localpath = "C:\some\tfs\directory"
    

$result = & $tfexepath history $localpath /noprompt /stopafter:1 /version:W
"$result" -match "\d+" | out-null
$id = $matches[0]

Then search from the root using the /r flag, but limit the search to start from the revision found above:

$result2 = & $tfexepath history $localpath /r /noprompt /stopafter:1 /version:$id~W
"$result2" -match "\d+" | out-null
$id2 =  $matches[0]


#result:
Write-Host $id2

For our code base, this brings down the total time from 10-15 to 1.4-1.5 seconds.

As far as I understand there are no drawbacks or limitations, but I suppose it could be slower in a tiny repository. - I'd be glad to know.

Run a Visual Studio CMD (in my case, for VS2015 is called: "Developer Command Promp for VS2015") and then get into your project folder and execute the following command:

tf history . /r /noprompt /stopafter:1 /version:W

If you really have no idea what version you have you should use one of the other suggested methods. If you are just not sure if you have a specific version or you are uncertain between a few change sets and you prefer working from the VS TFS GUI you can fallow this steps:

  1. Pick the change set you want to be certain about and do a compare: enter image description here
  2. If you have no difference: enter image description here

    or, if the only files that are different are files that you have pending changes in: enter image description here

That means you are up to date with the version in question.

  1. ** If you are not sure if file that has a pending change is actually also not up to date you can check that file version with properties: enter image description here