public IEnumerable <string> ListFiles(string path, SearchOption searchOption, params string[] lookupList) { ITracer tracer = _tracerFactory.GetTracer(); path = PathUtility.CleanPath(path); if (!path.StartsWith(RepositoryPath, StringComparison.OrdinalIgnoreCase)) { throw new NotSupportedException("Only paths relative to the repository root path are supported, path provided: {0}".FormatCurrentCulture(path)); } if (Directory.Exists(path)) { // TODO: Consider an implementation where the gitExe returns the list of files as a list (not storing the files list output as a blob) // In-order to conserve memory consumption Tuple <string, string> result = _gitExe.Execute(tracer, @"ls-files {0}", String.Join(" ", lookupList), RepositoryPath); if (!String.IsNullOrEmpty(result.Item1)) { IEnumerable <string> lines = result.Item1.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); lines = lines .Select(line => Path.Combine(RepositoryPath, line.Trim().Replace('/', '\\'))) .Where(p => p.StartsWith(path, StringComparison.OrdinalIgnoreCase)); switch (searchOption) { case SearchOption.TopDirectoryOnly: lines = lines.Where(line => !line.Substring(path.Length).TrimStart('\\').Contains('\\')); break; case SearchOption.AllDirectories: break; default: throw new NotSupportedException("Search option {0} is not supported".FormatCurrentCulture(searchOption)); } return(lines); } } return(Enumerable.Empty <string>()); }
public IEnumerable <string> ListFiles(string path, SearchOption searchOption, params string[] lookupList) { path = PathUtility.CleanPath(path); if (!FileSystemHelpers.IsSubfolder(RepositoryPath, path)) { throw new NotSupportedException("Only paths relative to the repository root path are supported, path provided: '{0}' is not a child folder of '{1}'".FormatCurrentCulture(path, RepositoryPath)); } if (!Directory.Exists(path)) { return(Enumerable.Empty <string>()); } using (var repo = new LibGit2Sharp.Repository(RepositoryPath)) { var files = repo.Diff.Compare <TreeChanges>(null, DiffTargets.Index, lookupList, compareOptions: new CompareOptions() { IncludeUnmodified = true, Similarity = SimilarityOptions.None }) .Select(d => Path.Combine(repo.Info.WorkingDirectory, d.Path)) .Where(p => p.StartsWith(path, StringComparison.OrdinalIgnoreCase)); switch (searchOption) { case SearchOption.TopDirectoryOnly: files = files.Where(line => !line.Substring(path.Length).TrimStart('\\').Contains('\\')); break; case SearchOption.AllDirectories: break; default: throw new NotSupportedException("Search option {0} is not supported".FormatCurrentCulture(searchOption)); } // Make sure to materialize the list before finalizing repo return(files.ToList()); } }
public void CleanFixesOtherSlashes() { Assert.Equal(@"c:\foo\bar", PathUtility.CleanPath(@"c:/foo/bar")); }
public void CleanTrimsWhiteSpaces() { Assert.Equal(@"c:\foo\BAR", PathUtility.CleanPath(@" c:/foo/BAR/ ")); Assert.Equal(@"c:\foo\bar", PathUtility.CleanPath("\tc:\\foo\\bar\\")); }
public void CleanTrimsTrailingSlashes() { Assert.Equal(@"c:\foo\BAR", PathUtility.CleanPath(@"c:/foo/BAR/")); Assert.Equal(@"c:\foo\bar", PathUtility.CleanPath(@"c:\foo\bar\")); }