获取最新的入住号码(最​​新的变更集ID)

时间:2012-02-10 14:10:01

标签: c# tfs2010 tfs-sdk

是否有办法以编程方式获取最新的变更集版本。

获取某个文件的变更集ID非常容易:

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

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

3 个答案:

答案 0 :(得分:9)

你可以这样做:

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

答案 1 :(得分:1)

使用VersionControlServer.GetLatestChangesetId获取最新的变更集ID,如评论中用户 tbaskan 所述。

(在TFS Java SDK中VersionControlClient.getLatestChangesetId

答案 2 :(得分:0)

我对此

使用以下tf命令
    /// <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;
            }
        }

这是我如何执行命令

    /// <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中的解决方案,希望这会有所帮助。