Andrei
Andrei

Reputation: 4328

How to get a list of files from TFS modified on a certain date in the specified workspace?

I have to create a list of files that were modified(checked out or checked in) on a specified date.

I have user name and their workspace name given as parameters to my function.

Any suggestions?

Upvotes: 1

Views: 6602

Answers (2)

Andrei
Andrei

Reputation: 4328

This what I ended up doing(I have left only the code related to the question):

Thank Martin for the directions!

void GetPastTFSChangesByDate()
    {
        using (TfsTeamProjectCollection tfsServer = new TfsTeamProjectCollection(_tfsServerUri))
        {
            tfsServer.Authenticate();

            if (tfsServer != null)
            {
                VersionControlServer vcServer = tfsServer.GetService(typeof (VersionControlServer)) as VersionControlServer;

                if (vcServer != null)
                {
                    var usersWorkspaces = vcServer.QueryWorkspaces(null, vcServer.AuthorizedUser, Environment.MachineName).ToList();

                    List<ChangedTFSItem> foundPastChanges = null;

                    if (_isSearchForPastChangesOn)
                    {
                        var allPastChangesets = vcServer.QueryHistory(@"C:\TFS",
                                                                      VersionSpec.Latest,
                                                                      0,
                                                                      RecursionType.Full,
                                                                      Environment.UserName,
                                                                      null,
                                                                      null,
                                                                      3000,
                                                                      true,
                                                                      false).Cast<Changeset>()
                            .Where(x => x.Committer.Contains(Environment.UserName));


                        _foundPastChanges = allPastChangesets
                            .SelectMany(x => x.Changes)
                            .Where(x => x.Item.CheckinDate.Date == _searchDate.Date) // Check-in date filter.
                            .DistinctBy(x => x.Item.ServerItem, x => x.Item.ServerItem.GetHashCode())
                            .Select(x => new ChangedTFSItem()
                                             {
                                                 FileName = "Uknown",
                                                 LocalItem = "Uknown",
                                                 ServerItem = x.Item.ServerItem,
                                                 ChangeTypeName = "",
                                                 ChangeDate = x.Item.CheckinDate.ToString(),
                                                 Workspace = "Uknown"
                                             }).ToList();

                        //Matching those foundPastChanges to the workspace corresponding file locations. 
                        if (usersWorkspaces.Any())
                        {
                            foreach (var item in foundPastChanges)
                            {
                                usersWorkspaces.ForEach(ws =>
                                                            {
                                                                item.LocalItem = ws.GetLocalItemForServerItem(item.ServerItem);
                                                                item.Workspace = ws.Name;
                                                            });

                                item.FileName = Path.GetFileName(item.ServerItem);
                            }
                        }
                    }
                }
            }
        }
    }

Upvotes: 2

Martin Woodward
Martin Woodward

Reputation: 11770

I think you want to do a combination of VersionControlServer.QueryHistory for figuring out the changes to files on a particular date combined with VersionControlServer.QueryWorkspaces to get hold of the workspace to pull out the working folder mappings that you'll want to query for the recursive history on.

I'm pretty sure you cannot tell which workspace a particular changeset came from though without doing something funky with a custom check-in policies to get that information into the changeset in the first place. That is to say, I do not know of a way to tell two changes apart from the same developer who happen to change them on two different workspaces. So what might be easier is to find out if you can remove the workspace requirement and simply return the files change by a particular user on a particular date using a single call to QueryHistory?

Upvotes: 2

Related Questions