none
Using TFS API - how to get previous version of a file given a Changeset

    Question

  • Given a changeset, I can get that changeset's version of a file. How do I get the "previous" version of that file using the TFS API?

    Friday, May 15, 2009 11:20 PM

Answers

  • Please keep in mind a few things:

    • you should ignore adds and branches, because they don't have previous version
    • please define what you mean by getting previous version of the file - in both Steve's and Mohamed's example you can download the file to a temp directory, you are not updating your workspace
    • Examples are no longer true in TFS 2010 - itemid is changing during rename, so you need to use QueryHistory to find item if it was renamed (you can still use the above code for other change, e.g. edit)

    Michal Malecki
    • Marked as answer by Bill.Wang Wednesday, May 27, 2009 6:53 AM
    Friday, May 22, 2009 1:38 PM
  • Hi.

    You need the ItemId of the file that you'd like the previous version of, which luckily is stored against the item in the changeset.

    int _changesetNo = 100;
    Changeset _changeset = _versionControlServer.GetChangeset( _changesetNo );
    foreach ( Change change in _changeset.Changes ) {
    Item _previousItem = _yourVersionControlServerObj.GetItem( change.Item.ItemId, _changesetNo - 1, true );
    Console.WriteLine("Previous Version {0}", _previousItem.ChangesetId);
    _previousItem.DownloadFile( @"C:\localversion.cs" );
    }
    Obviously, the key thing here is _changesetNo - 1 which means that we ask TFS to find the version that was stored before the one the already know about. This line of code needs to be tightened up so that you don't ask for version -1 and you also need to check that the _previousItem variable is not null. It will return null if the file didn't exist before changeset number (_changesetNo - 1).

    Hope that helps

    Steve
    • Edited by Steve_UK Monday, May 18, 2009 1:34 PM fixed typo's
    • Marked as answer by Bill.Wang Wednesday, May 27, 2009 6:53 AM
    Monday, May 18, 2009 1:09 PM
  • foreach (Change change in changeset.Changes)
    {
        Item previousItem = vcs.GetItem(change.Item.ItemId, change.Item.ChangesetId - 1, true);
    }
    • Marked as answer by Bill.Wang Wednesday, May 27, 2009 6:53 AM
    Monday, May 18, 2009 3:47 PM

All replies

  • Hi.

    You need the ItemId of the file that you'd like the previous version of, which luckily is stored against the item in the changeset.

    int _changesetNo = 100;
    Changeset _changeset = _versionControlServer.GetChangeset( _changesetNo );
    foreach ( Change change in _changeset.Changes ) {
    Item _previousItem = _yourVersionControlServerObj.GetItem( change.Item.ItemId, _changesetNo - 1, true );
    Console.WriteLine("Previous Version {0}", _previousItem.ChangesetId);
    _previousItem.DownloadFile( @"C:\localversion.cs" );
    }
    Obviously, the key thing here is _changesetNo - 1 which means that we ask TFS to find the version that was stored before the one the already know about. This line of code needs to be tightened up so that you don't ask for version -1 and you also need to check that the _previousItem variable is not null. It will return null if the file didn't exist before changeset number (_changesetNo - 1).

    Hope that helps

    Steve
    • Edited by Steve_UK Monday, May 18, 2009 1:34 PM fixed typo's
    • Marked as answer by Bill.Wang Wednesday, May 27, 2009 6:53 AM
    Monday, May 18, 2009 1:09 PM
  • foreach (Change change in changeset.Changes)
    {
        Item previousItem = vcs.GetItem(change.Item.ItemId, change.Item.ChangesetId - 1, true);
    }
    • Marked as answer by Bill.Wang Wednesday, May 27, 2009 6:53 AM
    Monday, May 18, 2009 3:47 PM
  • Hi joe,

    Is this issue resolved?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
    Friday, May 22, 2009 5:56 AM
  • Please keep in mind a few things:

    • you should ignore adds and branches, because they don't have previous version
    • please define what you mean by getting previous version of the file - in both Steve's and Mohamed's example you can download the file to a temp directory, you are not updating your workspace
    • Examples are no longer true in TFS 2010 - itemid is changing during rename, so you need to use QueryHistory to find item if it was renamed (you can still use the above code for other change, e.g. edit)

    Michal Malecki
    • Marked as answer by Bill.Wang Wednesday, May 27, 2009 6:53 AM
    Friday, May 22, 2009 1:38 PM
  • What if I want to get the previous version of a specific file within the changeset, to compare against the most recent version of that same file? For example, if I have a file MyFile.cs and it was most recently changed as part of changeset# 12345, how do I find out (and download) the changeset of the previous time MyFile.cs was changed?

    Thanks,
    Shawn

    Wednesday, November 18, 2009 7:11 PM