【问题标题】:Issue with API Deleting for Team Foundation Server TFSTeam Foundation Server TFS 的 API 删除问题
【发布时间】:2018-09-11 20:06:19
【问题描述】:

您好,谁能告诉我如何使用 TFS 的 API 删除文件?以下是我所拥有的,但我无法让它工作,任何帮助将不胜感激。

string[] InLocalDirectory = Directory.GetFiles(LogicAppConfig.Query(AppConfigLogic.TypeOfConfig.Path), "*", SearchOption.AllDirectories);

// Source Control
List<string> InSourceControl = new List<string>();
ItemSet SetOfItem = _ServerVersionControl.GetItems(_ServerPath, VersionSpec.Latest, RecursionType.Full);
foreach (Item GotItem in SetOfItem.Items)
{
    ItemType TypeOfItem = GotItem.ItemType;
    if (TypeOfItem == ItemType.File)
    {
        string LocalPath = _WorkspaceLocal.GetLocalItemForServerItem(GotItem.ServerItem);
        InSourceControl.Add(LocalPath);
    }
}

List<int> ToDeleteById = new List<int>();
foreach (string SourceFile in InSourceControl)
{
    if (!IsIgnored(SourceFile) && !InLocalDirectory.Contains(SourceFile))
    {
        // Delete Source Control File
        Item DeleteItem = _ServerVersionControl.GetItem(SourceFile);
        ToDeleteById.Add(DeleteItem.ItemId);
        // Update Local XML Directory
        DataXml.Delete(SourceFile);
    }
}

WorkItemStore wis = _CollectionTeamProject.GetService<WorkItemStore>();
wis.DestroyWorkItems(ToDeleteById);

【问题讨论】:

标签: api tfs


【解决方案1】:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.WebApi;

namespace ConsoleAppX
{
    class Program
    {
        static void Main(string[] args)
        {
            VssCredentials creds = new VssClientCredentials();
            creds.Storage = new VssClientCredentialStorage();
            VssConnection connection = new VssConnection(new Uri("https://tfsuri"), creds);
            TfvcHttpClient tfvcClient = connection.GetClient<TfvcHttpClient>();
            TfvcItem ti = tfvcClient.GetItemAsync("ProjectName", "$/FilePath","FileName").Result;
            TfvcChange tchange = new TfvcChange(ti,VersionControlChangeType.Delete);          
            List<TfvcChange> change = new List<TfvcChange> { tchange };
            TfvcChangeset tchangeset = new TfvcChangeset();
            tchangeset.Changes = change;
            tfvcClient.CreateChangesetAsync(tchangeset);
        }
    }
}

【讨论】:

  • 这解决了我刚刚使用这个从 TFS 中删除了我的整个项目... opps...正在运行备份...
  • 看起来这是从版本 6 到 7 的 API 问题,其中 7 与删除不向后兼容。我也通过 API 删除工作区时遇到了这个问题。
【解决方案2】:

要删除文件,您需要使用VersionControlServer class 获取现有工作区或创建新工作区。工作区有一个 PendDelete 方法来在工作区中创建挂起的更改。然后使用 Workspace.Checkin 方法将它们提交到源代码管理:

https://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.versioncontrol.client.workspace.aspx

【讨论】:

    【解决方案3】:

    在尝试最后一种方法之前,我尝试了 penddelete。但即使我手动去删除一个文件并且它命中 _WorkspaceLocal.PendDelete(SourceFile);如果 (changes.Count() > 0) 行并被插入删除它不会在该行上拾取,然后永远不会签入。

    string[] InLocalDirectory = Directory.GetFiles(LogicAppConfig.Query(AppConfigLogic.TypeOfConfig.Path), "*", SearchOption.AllDirectories);
    
                // Source Control
                List<string> InSourceControl = new List<string>();
                ItemSet SetOfItem = _ServerVersionControl.GetItems(_ServerPath, VersionSpec.Latest, RecursionType.Full);
                foreach (Item GotItem in SetOfItem.Items)
                {
                    ItemType TypeOfItem = GotItem.ItemType;
                    if (TypeOfItem == ItemType.File)
                    {
                        string LocalPath = _WorkspaceLocal.GetLocalItemForServerItem(GotItem.ServerItem);
                        InSourceControl.Add(LocalPath);
                    }
                }
    
                List<int> ToDeleteById = new List<int>();
                foreach (string SourceFile in InSourceControl)
                {
                    if (!IsIgnored(SourceFile) && !InLocalDirectory.Contains(SourceFile))
                    {
                        // Delete Source Control File
                        Item DeleteItem = _ServerVersionControl.GetItem(SourceFile);
                        ToDeleteById.Add(DeleteItem.ItemId);
                        // Update Local XML Directory
                        DataXml.Delete(SourceFile);
                        // Set for Deletion
                        _WorkspaceLocal.PendDelete(SourceFile);
                    }
                }
    
                string ConflictMessage = "";
                Conflict[] conflicts = _WorkspaceLocal.QueryConflicts(new string[] { _LocalPath }, true);
                foreach (Conflict conflict in conflicts)
                {
                    if (conflict != null)
                    {
                        try
                        {
                            if (conflict.CanMergeContent)
                            {
                                conflict.Resolution = Resolution.AcceptMerge;
                            }
                            else
                            {
                                conflict.Resolution = Resolution.AcceptYoursRenameTheirs;
                            }
                            ConflictMessage += @"\n\r\n\r" + conflict.GetFullMessage();
                            _WorkspaceLocal.ResolveConflict(conflict);
                        }
                        catch (Exception ex)
                        {
                            LogicAppConfig.Insert(AppConfigLogic.TypeOfConfig.Message, "Error Detected Previously:\r\n\r\n" + ex.Message + "\r\n\r\n" + ex.Source + "\r\n\r\n" + ex.StackTrace + "\r\n\r\n" + LogicAppConfig.Query(AppConfigLogic.TypeOfConfig.Path));
                        }
                    }
                }
    
                if (!String.IsNullOrEmpty(ConflictMessage))
                {
                    LogicAppConfig.Insert(AppConfigLogic.TypeOfConfig.Message, ConflictMessage);
                }
    
                PendingChange[] changes = _WorkspaceLocal.GetPendingChanges();
    
                if (changes.Count() > 0)
                {
                    int ChangeSetId = _WorkspaceLocal.CheckIn(changes, _WorkspaceName + " Deleted by Member Collaboration Utility");
                }
    

    【讨论】:

    • 试试把_WorkspaceLocal.GetPendingChanges()改成_WorkspaceLocal.GetPendingChanges(tfsServerFolderPath,RecursionType.Full)看看有没有用。
    • 不,这也不起作用我不确定这个 API 发生了什么......
    • 查看以下情况下的答案是否对你有帮助:stackoverflow.com/questions/20487590/…
    • 是的,这就是我正在做的事情,但我什么也没得到。唯一的区别是我使用挂起的删除而不是挂起的添加。
    猜你喜欢
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 2014-08-13
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多