From 54f4350c71f46257cf12d52497f4d89dc156d668 Mon Sep 17 00:00:00 2001 From: Philippe Miossec Date: Wed, 2 Jan 2013 21:25:39 +0100 Subject: [PATCH 1/2] Filter label by name --- GitTfs.VsCommon/TfsHelper.Common.cs | 7 ++++--- GitTfs.VsFake/TfsHelper.VsFake.cs | 2 +- GitTfs/Commands/Labels.cs | 9 ++++++++- GitTfs/Core/TfsInterop/ITfsHelper.cs | 4 ++-- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/GitTfs.VsCommon/TfsHelper.Common.cs b/GitTfs.VsCommon/TfsHelper.Common.cs index fe9a485f..ea522718 100644 --- a/GitTfs.VsCommon/TfsHelper.Common.cs +++ b/GitTfs.VsCommon/TfsHelper.Common.cs @@ -559,9 +559,10 @@ namespace Sep.Git.Tfs.VsCommon return new WorkItemCheckedInfo(Convert.ToInt32(workitem), true, checkinAction); } - public IEnumerable GetLabels(string tfsPathBranch) + public IEnumerable GetLabels(string tfsPathBranch, string nameFilter = null) { - var labels = VersionControl.QueryLabels(null, tfsPathBranch, null, true, tfsPathBranch, VersionSpec.Latest); + var labels = VersionControl.QueryLabels(nameFilter, tfsPathBranch, null, true, tfsPathBranch, VersionSpec.Latest); + return labels.Select(e => new TfsLabel { Id = e.LabelId, Name = e.Name, @@ -574,4 +575,4 @@ namespace Sep.Git.Tfs.VsCommon } } -} \ No newline at end of file +} diff --git a/GitTfs.VsFake/TfsHelper.VsFake.cs b/GitTfs.VsFake/TfsHelper.VsFake.cs index 670a4ddd..e98f7d84 100644 --- a/GitTfs.VsFake/TfsHelper.VsFake.cs +++ b/GitTfs.VsFake/TfsHelper.VsFake.cs @@ -356,7 +356,7 @@ namespace Sep.Git.Tfs.VsFake throw new NotImplementedException(); } - public IEnumerable GetLabels(string tfsPathBranch) + public IEnumerable GetLabels(string tfsPathBranch, string nameFilter = null) { throw new NotImplementedException(); } diff --git a/GitTfs/Commands/Labels.cs b/GitTfs/Commands/Labels.cs index 2e39543d..2210bfc7 100644 --- a/GitTfs/Commands/Labels.cs +++ b/GitTfs/Commands/Labels.cs @@ -24,6 +24,7 @@ namespace Sep.Git.Tfs.Commands public string TfsPassword { get; set; } public string ParentBranch { get; set; } public bool LabelAllBranches { get; set; } + public string NameFilter { get; set; } string AuthorsFilePath { get; set; } public Labels(TextWriter stdout, Globals globals, AuthorsFile authors) @@ -40,6 +41,7 @@ namespace Sep.Git.Tfs.Commands return new OptionSet { { "all|fetch-all", "Fetch all the labels on all the TFS remotes (For TFS 2010 and later)", v => LabelAllBranches = v != null }, + { "n|label-name=", "Fetch all the labels respecting this name filter", v => NameFilter = v }, { "u|username=", "TFS username", v => TfsUsername = v }, { "p|password=", "TFS password", v => TfsPassword = v }, { "a|authors=", "Path to an Authors file to map TFS users to Git users", v => AuthorsFilePath = v }, @@ -83,9 +85,14 @@ namespace Sep.Git.Tfs.Commands private int CreateLabelsForTfsBranch(IGitTfsRemote tfsRemote) { + if (string.IsNullOrWhiteSpace(NameFilter)) + NameFilter = null; + else + NameFilter = NameFilter.Trim(); + UpdateRemote(tfsRemote); _stdout.WriteLine("Looking for label on " + tfsRemote.TfsRepositoryPath + "..."); - var labels = tfsRemote.Tfs.GetLabels(tfsRemote.TfsRepositoryPath); + var labels = tfsRemote.Tfs.GetLabels(tfsRemote.TfsRepositoryPath, NameFilter); _stdout.WriteLine(labels.Count() +" labels found!"); foreach (var label in labels) { diff --git a/GitTfs/Core/TfsInterop/ITfsHelper.cs b/GitTfs/Core/TfsInterop/ITfsHelper.cs index 1fc8fda1..dc09c322 100644 --- a/GitTfs/Core/TfsInterop/ITfsHelper.cs +++ b/GitTfs/Core/TfsInterop/ITfsHelper.cs @@ -29,9 +29,9 @@ namespace Sep.Git.Tfs.Core.TfsInterop long ShowCheckinDialog(IWorkspace workspace, IPendingChange[] pendingChanges, IEnumerable checkedInfos, string checkinComment); void CleanupWorkspaces(string workingDirectory); int GetRootChangesetForBranch(string tfsPathBranchToCreate, string tfsPathParentBranch = null); - IEnumerable GetLabels(string tfsPathBranch); + IEnumerable GetLabels(string tfsPathBranch, string nameFilter = null); bool CanGetBranchInformation { get; } IEnumerable GetAllTfsBranchesOrderedByCreation(); void EnsureAuthenticated(); } -} \ No newline at end of file +} From edc2def0e2d6ec20be492382d68423256597a4c0 Mon Sep 17 00:00:00 2001 From: Philippe Miossec Date: Wed, 2 Jan 2013 21:35:17 +0100 Subject: [PATCH 2/2] Filter label by name by excluding names using regular expression --- GitTfs/Commands/Labels.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/GitTfs/Commands/Labels.cs b/GitTfs/Commands/Labels.cs index 2210bfc7..b02f5025 100644 --- a/GitTfs/Commands/Labels.cs +++ b/GitTfs/Commands/Labels.cs @@ -3,6 +3,7 @@ using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Linq; +using System.Text.RegularExpressions; using NDesk.Options; using Sep.Git.Tfs.Core; using StructureMap; @@ -25,6 +26,7 @@ namespace Sep.Git.Tfs.Commands public string ParentBranch { get; set; } public bool LabelAllBranches { get; set; } public string NameFilter { get; set; } + public string ExcludeNameFilter { get; set; } string AuthorsFilePath { get; set; } public Labels(TextWriter stdout, Globals globals, AuthorsFile authors) @@ -42,6 +44,7 @@ namespace Sep.Git.Tfs.Commands { { "all|fetch-all", "Fetch all the labels on all the TFS remotes (For TFS 2010 and later)", v => LabelAllBranches = v != null }, { "n|label-name=", "Fetch all the labels respecting this name filter", v => NameFilter = v }, + { "e|exclude-label-name=", "Exclude all the labels respecting this regex name filter", v => ExcludeNameFilter = v }, { "u|username=", "TFS username", v => TfsUsername = v }, { "p|password=", "TFS password", v => TfsPassword = v }, { "a|authors=", "Path to an Authors file to map TFS users to Git users", v => AuthorsFilePath = v }, @@ -94,8 +97,16 @@ namespace Sep.Git.Tfs.Commands _stdout.WriteLine("Looking for label on " + tfsRemote.TfsRepositoryPath + "..."); var labels = tfsRemote.Tfs.GetLabels(tfsRemote.TfsRepositoryPath, NameFilter); _stdout.WriteLine(labels.Count() +" labels found!"); + + Regex exludeRegex = null; + if (ExcludeNameFilter != null) + exludeRegex = new Regex(ExcludeNameFilter); + foreach (var label in labels) { + if (ExcludeNameFilter != null && exludeRegex.IsMatch(label.Name)) + continue; + Trace.WriteLine("LabelId:" + label.Id + "/ChangesetId:" + label.ChangesetId + "/LabelName:" + label.Name + "/Owner:" + label.Owner); Trace.WriteLine("Try to find changeset in git repository..."); string sha1TagCommit = _globals.Repository.FindCommitHashByCommitMessage("git-tfs-id: .*;C" + label.ChangesetId + "[^0-9]");