查找和删除非活动的tfs分支

时间:2014-02-28 08:24:51

标签: sql-server tfs

有没有内置的方法来查找和删除(tf destroy)TFS项目的分支,这些分支处于非活动状态(我的意思是没有检查操作)很长一段时间,比如说1个月。无论是tfs工具还是sql脚本都可以做到。

3 个答案:

答案 0 :(得分:2)

你可以这样做,但你需要编写一个小程序,使用TFS API检查每个分支并删除未使用的分支。

您可以使用简单的C#控制台应用程序,我可以从经验告诉您,TFS公共API非常直观且易于使用。您可以开始使用here

Here是如何显示所有分支的。

答案 1 :(得分:1)

未使用的分支包含元素修改历史记录,而不是删除它们writelock分支并保留它。恢复的空间并不重要。

答案 2 :(得分:0)

嗯,整件事情并不难,在这里发布代码,可能对某人有所帮助:

private static string _tfLocation; //location of tf.exe
private static string _tfProject;  //our team project

static void Main(string[] args)
{
    _tfLocation = ConfigurationManager.AppSettings.Get("tfLocation");
    _tfProject = ConfigurationManager.AppSettings.Get("tfProject");
    var keepAliveBranches = ConfigurationManager.AppSettings.Get("keepAliveBranches").Split(',').ToList(); //branches that we keep anyway
    var latestDate = DateTime.Now.AddMonths(-3); //we delete all branches that are older than 3 months

    var folders = ExecuteCommand(string.Format("dir /folders \"{0}\"", _tfProject));
    var branches = folders.Split('\r', '\n').ToList();

    branches = branches.Where(b => !string.IsNullOrEmpty(b) && b.StartsWith("$")).Select(b => b.Remove(0, 1)).Skip(1).ToList();
    branches.ForEach(b => b = b.Remove(0, 1));

    foreach (var branch in branches)
    {
        if (keepAliveBranches.Contains(branch))
            continue;

        //get latest changeset
        var lastChangeset = ExecuteCommand(string.Format("history \"{0}/{1}\" /recursive /stopafter:1 /format:brief /sort:descending /noprompt", _tfProject, branch));
        var changesetDate = DateTime.Parse(Regex.Match(lastChangeset, @"\d{2}\.\d{2}\.\d{4}").Value); //get it's date 
        if (changesetDate < latestDate)                          
            //destroy
            ExecuteCommand(string.Format("destroy \"{0}/{1}\" /recursive /stopafter:1 /startcleanup /noprompt /silent", _tfProject, branch));            
    }
}

//execute console command and get results
private static string ExecuteCommand(string command)
{
    var process = new Process()
    {
        StartInfo = new ProcessStartInfo(_tfLocation)
        {
            UseShellExecute = false,
            RedirectStandardOutput = true,
            CreateNoWindow = true,
            Arguments = command
        }, 
    };
    process.Start();
    var result = process.StandardOutput.ReadToEnd();
    process.WaitForExit();

    return result;
}