2012-02-10 85 views
7

有没有办法以编程方式获取最新的变更集版本。获取最新的登记号码(最新的变更集ID)

这是相当容易得到变更ID为某些文件:

var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("https://my.tfs.com/DefaultCollection")); 
     tfs.EnsureAuthenticated(); 
     var vcs = tfs.GetService<VersionControlServer>(); 

,然后调用GetItems或QueryHistory,但我想知道什么是最后一次签号码。

回答

9

你可以这样说:

var latestChangesetId = 
    vcs.QueryHistory(
     "$/", 
     VersionSpec.Latest, 
     0, 
     RecursionType.Full, 
     String.Empty, 
     VersionSpec.Latest, 
     VersionSpec.Latest, 
     1, 
     false, 
     true) 
     .Cast<Changeset>() 
     .Single() 
     .ChangesetId; 
+5

看来VersionControlServer也有GetLatestChangesetId方法。它要短得多:-) – tbaskan 2012-02-29 18:52:16

+0

我们可以通过用户名和密码下面的tfs连接 'var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsServer)); tfs.Connect(ConnectOptions.None);' bcoz在将我的网页部署到IIS服务器后,我无法获取tfs的详细信息,但对于本地主机,我能够获取相同网页的tfs详细信息。 我越来越低err Microsoft.TeamFoundation.TeamFoundationServerUnauthorizedException:TF30063:您无权访问http:// tfsserver:8080/tfs/mycollection。在Microsoft.TeamFoundation.Client.TfsConnection.ThrowAuthorizationException(异常e) – picnic4u 2012-10-22 16:02:31

+0

@ picnic4u可能最好提出一个新的问题。 – DaveShaw 2012-10-22 17:17:38

0

我用这个

/// <summary> 
    /// Return last check-in History of a file 
    /// </summary> 
    /// <param name="filename">filename for which history is required</param> 
    /// <returns>TFS history command</returns> 
    private string GetTfsHistoryCommand(string filename) 
    { 
     //tfs history command (return only one recent record stopafter:1) 
     return string.Format("history /stopafter:1 {0} /noprompt", filename); // return recent one row 
    } 

执行后,我分析此命令的输出来获得变更数量

using (StreamReader Output = ExecuteTfsCommand(GetTfsHistoryCommand(fullFilePath))) 
     { 
      string line; 
      bool foundChangeSetLine = false; 
      Int64 latestChangeSet; 
      while ((line = Output.ReadLine()) != null) 
      { 
       if (foundChangeSetLine) 
       { 
        if (Int64.TryParse(line.Split(' ').First().ToString(), out latestChangeSet)) 
        { 
         return latestChangeSet; // this is the lastest changeset number of input file 
        } 
       } 
       if (line.Contains("-----"))  // output stream contains history records after "------" row 
        foundChangeSetLine = true; 
      } 
     } 

这我怎么执行命令后TF命令

/// <summary> 
    /// Executes TFS commands by setting up TFS environment 
    /// </summary> 
    /// <param name="commands">TFS commands to be executed in sequence</param> 
    /// <returns>Output stream for the commands</returns> 
    private StreamReader ExecuteTfsCommand(string command) 
    { 
     logger.Info(string.Format("\n Executing TFS command: {0}",command)); 
     Process process = new Process(); 
     process.StartInfo.CreateNoWindow = true; 
     process.StartInfo.FileName = _tFPath; 
     process.StartInfo.UseShellExecute = false; 
     process.StartInfo.RedirectStandardOutput = true; 
     process.StartInfo.RedirectStandardInput = true; 
     process.StartInfo.Arguments = command; 
     process.StartInfo.RedirectStandardError = true; 
     process.Start(); 
     process.WaitForExit();            // wait until process finishes 
     // log the error if there's any 
     StreamReader errorReader = process.StandardError; 
     if(errorReader.ReadToEnd()!="") 
      logger.Error(string.Format(" \n Error in TF process execution ", errorReader.ReadToEnd())); 
     return process.StandardOutput; 
    } 

不是一种有效的方式,但仍然是TFS 2008中的解决方案,希望这有助于。