用于TFS的Visual Studio加载项:标记要删除的文件

时间:2011-07-14 20:00:07

标签: c# tfs add-in delete-file

我一直在为我的公司编写一个VB加载项,它将进入TFS并自动标记以“.delete”结尾的文件以进行删除。为此,我想创建一个工作区“Temp”,它映射到我本地的D:\ TFSTemp和TFS中的文件夹。然后,我只想将.delete文件下载到我的本地(以避免获得服务器中所有文件的最新信息),将它们从我的本地映射到服务器,将它们标记为删除(workspace.PendDelete( ))然后立即检查它们。

我的问题是我不确定我是否正在设置所需的正确映射。我可以下载所有.delete文件,但是当我调用Workspace.GetPendingChanges()时,数组没有被填充,这就是为什么我怀疑我可能没有正确设置它。

我知道这是一个复杂的加载项,所以如果我的代码对您没有意义,请向我提问。

//establish connection to tfs
                TeamFoundationServer server = new TeamFoundationServer(TFS1);
                //test file to output to
                StreamWriter xw = new StreamWriter(@"C:\Documents and Settings\A087649\Desktop\FileList.txt");
                //get a working object in tfs
                VersionControlServer sourceControl = server.GetService(typeof(VersionControlServer)) as VersionControlServer;

                int numberOfFiles = 0;
                int numToDelete = 0;

                try
                {
                    //load config file
                     //LoadConfig();

                    //path where we are going to look in tfs          
                    String path = @"$/PAIT_ECOMPARE/Dev/TFSTool/Prod/Offeringdata/AU/CT";
                    //array of item objects in that path
                    ItemSet items = sourceControl.GetItems(path, RecursionType.Full);
                    numberOfFiles = items.Items.Length;
                    Workspace workspace = sourceControl.CreateWorkspace("Temp");
                    WorkingFolder workingFolder = new WorkingFolder(path, @"D:\TFSTemp\");
                    workspace.CreateMapping(workingFolder);

                    //instance of own created class that represents the progressbar and log output
                    TFSToolLoad ProgressBar = new TFSToolLoad();
                    ProgressBar.SetValues(numberOfFiles);
                    ProgressBar.TopMost = true;


                    foreach (Item item in items.Items)
                    {
                        ProgressBar.Show();
                        //get only the file path to the file
                        serverPath = item.ServerItem;
                        //get changeset Id
                        changeSetID = item.ChangesetId;                            
                        if (serverPath.EndsWith(".delete"))
                        {
                            //get file name only and local path
                            fileName = Path.GetFileName(serverPath);
                            localPath = @"D:\TFSTemp\"+ fileName;
                            //get latest on the file
                            workspace.Get(new GetRequest(serverPath, RecursionType.None, VersionSpec.Latest), GetOptions.None);
                            workspace.PendDelete(serverPath, RecursionType.None);

                            numToDelete++;
                         }
                        ProgressBar.Step();
                    }

                    ProgressBar.SetText
                       ("Number of Files Marked for Delete: " + numToDelete+"\n");

                    //if there are any pending changes, check them in and merge them into staging
                    if (numToDelete > 0)
                    {
                        //check in all the changes
                        ProgressBar.SetText("Checking in changes...\n");
                        PendingChange[] pendingChanges = workspace.GetPendingChanges();

                        //if there are any pending changes, check them in and merge them into staging
                        workspace.CheckIn(pendingChanges, "Automated TFS tool cleanup");
                        ProgressBar.SetText("Done\n Merging changes into Staging...");

                        //merge
                        //set up merge by changeset id
                        ChangesetVersionSpec changeSet = new ChangesetVersionSpec(changeSetID);
                       //map to target server path before merging, otherwise it won't work
                        Workspace eCompareAdmin = sourceControl.GetWorkspace(@"D:\PAIT_ECOMPARE");
                        string mainPath = @"$/PAIT_ECOMPARE/Dev/TFSTool";
                        string stagingPath = @"$/PAIT_ECOMPARE/Dev/TFSToolStaging";
//Problem:
                        eCompareAdmin.Merge(mainPath, stagingPath, changeSet, changeSet);
                        PendingChange[] mergeChanges = eCompareAdmin.GetPendingChanges();
                        workspace.CheckIn(mergeChanges, "Automated TFS Cleanup");
                        ProgressBar.SetText("Done\n");

1 个答案:

答案 0 :(得分:1)

在完成将项目放入本地工作区之前,不能删除删除。

您目前正在执行DownloadFile,它只会获取文件的内容 - 它不会更新您的工作区以反映您在本地拥有该文件。在等待删除之前,您应该为该文件调用Workspace.Get

另一项:你不应该对文件使用完全递归(不知道后果),你可能应该使用RecursionType.None。对文件的完全递归执行模式匹配,并将包含在给定路径下具有该文件名的所有文件。 (即$ / file.txt的完整递归将匹配$ / file.txt,$ / A / file.txt,$ / A / B / file.txt等。)