简体   繁体   English

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

[英]Visual Studio Add-In for TFS: Marking Files for Deletion

I've been writing a VB Add-in for my company that will go into TFS and automatically mark files that end with ".delete" for deletion.我一直在为我的公司编写一个 VB 插件,它将 go 转换为 TFS 并自动标记以“.delete”结尾的文件以进行删除。 To do this, I'd like to create a workspace "Temp" which maps to my D:\TFSTemp in my local and a folder in TFS.为此,我想创建一个工作区“Temp”,它映射到我本地的 D:\TFSTemp 和 TFS 中的文件夹。 Then, I'd like to download only the.delete files to my local (to avoid having to get latest on all the files in the server), map them from my local to the server, mark them for deletion (workspace.PendDelete()) and then check them all in at once.然后,我只想将 .delete 文件下载到我的本地(以避免必须获取服务器中所有文件的最新信息),map 将它们从我的本地下载到服务器,将它们标记为删除(workspace.PendDelete( )) 然后一次检查它们。

My problem is I am not sure I am setting up the correct mapping needed.我的问题是我不确定我是否设置了所需的正确映射。 I am able to download all the.delete files, yet when I invoke Workspace.GetPendingChanges(), the array is not being populated, which is why I suspect I might be not setting it up correctly.我能够下载所有 the.delete 文件,但是当我调用 Workspace.GetPendingChanges() 时,数组没有被填充,这就是我怀疑我可能没有正确设置它的原因。

I understand it is a complicated add-in, so please ask me questions if my code does not make sense to you.我知道这是一个复杂的插件,所以如果我的代码对您没有意义,请向我提问。

//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");

You can't pend a delete until you've done a get of the item into your local workspace.在将项目获取到本地工作区之前,您无法挂起删除。

You're currently doing a DownloadFile , which will simply get the contents of the file - it will not update your workspace to reflect that you have the file locally.您当前正在执行DownloadFile ,它只会获取文件的内容 - 它不会更新您的工作区以反映您在本地拥有该文件。 You should instead call Workspace.Get for that file before pending your delete.在等待删除之前,您应该为该文件调用Workspace.Get

One other item: you should not be using full recursion for files (without being aware of the consequences), you should probably be using RecursionType.None .另一项:您不应该对文件使用完全递归(不知道后果),您可能应该使用RecursionType.None Full recursion on a file performs pattern matching and will include all files with that filename beneath the given path.文件的完全递归执行模式匹配,并将包含给定路径下具有该文件名的所有文件。 (Ie, full recursion for $/file.txt will match $/file.txt, $/A/file.txt, $/A/B/file.txt, etc.) (即,$/file.txt 的完全递归将匹配 $/file.txt、$/A/file.txt、$/A/B/file.txt 等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM