using System; using System.Collections.Generic; using System.Text.RegularExpressions; using Sep.Git.Tfs.Core; namespace Sep.Git.Tfs.Util { public class PathResolver { readonly IGitTfsRemote _remote; readonly IDictionary _initialTree; public PathResolver(IGitTfsRemote remote, IDictionary initialTree) { _remote = remote; _initialTree = initialTree; } public string GetPathInGitRepo(string tfsPath) { return GetGitObject(tfsPath).Try(x => x.Path); } public GitObject GetGitObject(string tfsPath) { var pathInGitRepo = _remote.GetPathInGitRepo(tfsPath); if (pathInGitRepo == null) return null; return Lookup(pathInGitRepo); } public bool ShouldIncludeGitItem(string gitPath) { return !String.IsNullOrEmpty(gitPath) && !_remote.ShouldSkip(gitPath); } public bool Contains(string pathInGitRepo) { if (pathInGitRepo != null) { GitObject result; if (_initialTree.TryGetValue(pathInGitRepo, out result)) return result.Commit != null; } return false; } private static readonly Regex SplitDirnameFilename = new Regex(@"(?.*)[/\\](?[^/\\]+)", RegexOptions.Compiled); private GitObject Lookup(string pathInGitRepo) { GitObject result; if (_initialTree.TryGetValue(pathInGitRepo, out result)) return result; var fullPath = pathInGitRepo; var splitResult = SplitDirnameFilename.Match(pathInGitRepo); if (splitResult.Success) { var dirName = splitResult.Groups["dir"].Value; var fileName = splitResult.Groups["file"].Value; fullPath = Lookup(dirName).Path + "/" + fileName; } result = new GitObject { Path = fullPath }; _initialTree[fullPath] = result; return result; } } }