获取最新的签入号码(最新变更集id)

本文关键字:最新 id 号码 获取 | 更新日期: 2023-09-27 18:23:57

有没有一种方法可以通过编程获得最新的变更集版本。

获取特定文件的变更集id相当容易:

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

然后调用GetItems或QueryHistory,但我想知道最后一次签入的号码是多少。

获取最新的签入号码(最新变更集id)

你可以这样做:

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

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

(在TFS Java SDK中是VersionControlClient.getLatestChangesetId

我对这个使用以下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;
    }

这不是一种有效的方法,但仍然是一种在TFS2008中有效的解决方案,希望这能有所帮助。