8

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.

TomP89
  • 1,420
  • 3
  • 11
  • 29
Jacob
  • 914
  • 2
  • 10
  • 30

3 Answers3

9

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;
pantelif
  • 8,524
  • 2
  • 33
  • 48
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
  • 5
    It seems VersionControlServer also has a GetLatestChangesetId method. It is much shorter :-) – tbaskan Feb 29 '12 at 18:52
  • can we pass the username and pwd for below tfs connection `var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsServer)); tfs.Connect(ConnectOptions.None);` bcoz after deploying my web pages to IIS server i m unable to get the tfs details but for localhost i m able to get the tfs details for same web pages. i m getting below err Microsoft.TeamFoundation.TeamFoundationServerUnauthorizedException: TF30063: You are not authorized to access http://tfsserver:8080/tfs/mycollection. at Microsoft.TeamFoundation.Client.TfsConnection.ThrowAuthorizationException(Exception e) – Piyush Oct 22 '12 at 16:02
  • @picnic4u probably best raising a new question for that. – DaveShaw Oct 22 '12 at 17:17
  • @DaveShaw - i have tried below n working perfectly.... NetworkCrdential cred = new NetworkCredential("username", "password","domain_name"); var tfs = new TeamFoundationServer("http://tfsserver:8080/tfs/mycollection", cred); tfs.EnsureAuthenticated(); – Piyush Oct 23 '12 at 05:46
  • How do you do it for a specific project or folder? I changed "$/" to "$/MyProject1" and it worked, but if I change it to "$/MyProject2" it did not, it said there were no items in the collection. In fact this only works on the project with the latest changeset in it. How do you specify a range besides "latest - latest"? – BrainSlugs83 Oct 04 '13 at 21:00
  • I was able to make this work (and it's surprisingly performant even in projects that have several thousand changesets): `Console.WriteLine(vcs.QueryHistory("$/Project1/", VersionSpec.Latest, 0, RecursionType.Full, null, null, VersionSpec.Latest, Int32.MaxValue, false, true).Cast().Select(cs => cs.ChangesetId).Max());` and `Console.WriteLine(vcs.QueryHistory("$/Project1/", VersionSpec.Latest, 0, RecursionType.Full, null, null, VersionSpec.Latest, Int32.MaxValue, false, true).Cast().Select(cs => cs.ChangesetId).Max());` – BrainSlugs83 Oct 04 '13 at 21:15
1

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)

kapex
  • 28,903
  • 6
  • 107
  • 121
0

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.

DotNetUser
  • 6,494
  • 1
  • 25
  • 27