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>());
        }
示例#2
0
        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());
            }
        }
示例#3
0
 public void CleanFixesOtherSlashes()
 {
     Assert.Equal(@"c:\foo\bar", PathUtility.CleanPath(@"c:/foo/bar"));
 }
示例#4
0
 public void CleanTrimsWhiteSpaces()
 {
     Assert.Equal(@"c:\foo\BAR", PathUtility.CleanPath(@" c:/foo/BAR/ "));
     Assert.Equal(@"c:\foo\bar", PathUtility.CleanPath("\tc:\\foo\\bar\\"));
 }
示例#5
0
 public void CleanTrimsTrailingSlashes()
 {
     Assert.Equal(@"c:\foo\BAR", PathUtility.CleanPath(@"c:/foo/BAR/"));
     Assert.Equal(@"c:\foo\bar", PathUtility.CleanPath(@"c:\foo\bar\"));
 }