2011-07-14 42 views
2

我一直在爲我的公司編寫一個VB加載項,它將進入TFS並自動標記以「.delete」結尾以刪除的文件。爲此,我想創建一個工作空間「Temp」,它映射到我本地的D:\ TFSTemp以及TFS中的一個文件夾。然後,我只想將.delete文件下載到本地(以避免必須獲取服務器中的所有文件的最新版本),將它們從本地映射到服務器,將它們標記爲刪除(workspace.PendDelete( )),然後立即檢查它們。TFS的Visual Studio加載項:標記要刪除的文件

我的問題是我不確定我是否設置了正確的映射。我可以下載所有的.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"); 
+0

使用ChangesetVersionSpec。 –

+0

謝謝愛德華!我有最後一個問題。一旦我執行合併,我想檢查目標分支的更改,但是當我執行GetPendingChanges()時,數組再次返回0.我一直在調試整天,似乎無法弄清楚這一點。我錯過了什麼?我已經更新了上面的代碼以反映我的更改。 –

+0

這是一個很好的問題 - Workspace.PendDelete()調用是否成功?它會在成功時返回非零值,在失敗時返回零。 –

回答

1

只有在完成了將項目獲取到本地工作區之後,才能刪除刪除項。

您目前正在執行一個DownloadFile,它將簡單地獲取文件的內容 - 它不會更新您的工作空間以反映您在本地具有該文件。在等待刪除之前,您應該爲該文件調用Workspace.Get

另一個項目:你不應該使用文件的完整遞歸(不知道後果),你應該使用RecursionType.None。文件上的完全遞歸執行模式匹配,並將包括具有該文件名的文件在給定路徑下的所有文件。 (也就是說,$/file.txt的完全遞歸將匹配$/file.txt,$/A/file.txt,$/A/B/file.txt等)。

+0

感謝您的及時迴應。我可以問你是否有方法來獲取()一個文件而不是服務器中的所有文件?如果是這樣,怎麼樣? –

+0

當然,你可以使用Workspace.Get(new GetRequest(@「$/Path/To/File」,RecursionType.None,VersionSpec.Latest),GetOptions.None);實際獲得整個服務器實際上非常困難:您必須擁有$ /映射。 –

相關問題