简体   繁体   中英

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. 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. 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.

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.

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. You should instead call Workspace.Get for that file before pending your delete.

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 . 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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