Jacob
Jacob

Reputation: 914

Get the latest check in number (latest changeset id)

Is there a way to get programmatically latest changeset version in general.

It's fairly easy to get changeset id for certain file :

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

and then call GetItems or QueryHistory, but i would like to know what was the last checkin number.

Upvotes: 8

Views: 5890

Answers (3)

kapex
kapex

Reputation: 29959

Use VersionControlServer.GetLatestChangesetId to get the latest changeset id, as mentioned by user tbaskan in the comments.

(In the TFS Java SDK it's VersionControlClient.getLatestChangesetId)

Upvotes: 1

DaveShaw
DaveShaw

Reputation: 52798

You can do it like this:

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

Upvotes: 9

DotNetUser
DotNetUser

Reputation: 6612

I use following tf command for this

    /// <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
    }

after execution I parse the output of this command to get changeset number

    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;
            }
        }

This how I execute the command

    /// <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;
    }

Not a efficient way but still a solution this works in TFS 2008, hope this helps.

Upvotes: 0

Related Questions