public void IsIgnored_IgnoreCase() { using var temp = new TempRoot(); var rootDir = temp.CreateDirectory(); var workingDir = rootDir.CreateDirectory("Repo"); // root // A (.gitignore) // diR var dirA = workingDir.CreateDirectory("A"); dirA.CreateDirectory("diR"); dirA.CreateFile(".gitignore").WriteAllText(@" *.txt !a.TXT dir/ "); var ignore = new GitIgnore(root: null, PathUtils.ToPosixDirectoryPath(workingDir.Path), ignoreCase: true); var matcher = ignore.CreateMatcher(); // outside of the working directory: Assert.Null(matcher.IsPathIgnored(rootDir.Path.ToUpperInvariant())); // special case: Assert.True(matcher.IsPathIgnored(Path.Combine(workingDir.Path, ".GIT"))); // matches "*.txt" Assert.True(matcher.IsPathIgnored(Path.Combine(workingDir.Path, "A", "b.TXT"))); // matches "!a.TXT" Assert.False(matcher.IsPathIgnored(Path.Combine(workingDir.Path, "A", "a.txt"))); // matches directory name "dir/" Assert.True(matcher.IsPathIgnored(Path.Combine(workingDir.Path, "A", "DIr", "a.txt"))); // matches "dir/" (treated as a directory path) Assert.True(matcher.IsPathIgnored(Path.Combine(workingDir.Path, "A", "DiR") + Path.DirectorySeparatorChar)); if (Path.DirectorySeparatorChar == '\\') { // matches "dir/" (existing directory path, the directory DIR only exists on case-insensitive FS) Assert.True(matcher.IsPathIgnored(Path.Combine(workingDir.Path, "A", "DIR"))); } Assert.True(matcher.IsPathIgnored(Path.Combine(workingDir.Path, "A", "diR"))); AssertEx.SetEqual(new[] { "/Repo/A/DIr: True", "/Repo/A: False", "/Repo: False", }, matcher.DirectoryIgnoreStateCache.Select(kvp => $"{kvp.Key.Substring(rootDir.Path.Length)}: {kvp.Value}")); }
public void IsIgnoredFileTest(string path, bool expectedResult) { string gitIgnoreText = Resource.GetResource("gitignore.txt"); string[] ignoreArr = gitIgnoreText.Split(new string[] { "\r\n" }, StringSplitOptions.None); GitIgnore gitIgnore = new GitIgnore(ignoreArr); Assert.AreEqual(expectedResult, gitIgnore.IsIgnoredFile(path)); }
public void GlobPatternToRegexTest(string globPatt, string expectedRegex, Type expectedException) { if (expectedException != null) { Assert.Catch(expectedException, () => GitIgnore.GlobPatternToRegex(globPatt)); return; } Regex actualRegex = GitIgnore.GlobPatternToRegex(globPatt); Assert.AreEqual(expectedRegex, actualRegex.ToString()); }
private bool GetGitIgnore(FileSystem fileSystem, ref string directoryPath, out GitIgnore gitIgnore) { KeyValuePair <string, GitIgnore> gitIgnoreEntry; gitIgnore = null; if (gitIgnoreMap.TryGetValue(directoryPath, out gitIgnoreEntry)) { directoryPath = gitIgnoreEntry.Key; gitIgnore = gitIgnoreEntry.Value; return(true); } var gitIgnoreFilePath = Path.Combine(directoryPath, ".gitignore"); bool tfIgnore = false; while (true) { if (File.Exists(gitIgnoreFilePath)) { try { Console.WriteLine("Parsing: " + gitIgnoreFilePath); using (var gitIgnoreStream = fileSystem.OpenFile(gitIgnoreFilePath)) using (var reader = new StreamReader(gitIgnoreStream)) { gitIgnore = GitIgnore.Parse(reader, tfIgnore); gitIgnoreMap[directoryPath] = new KeyValuePair <string, GitIgnore>(directoryPath, gitIgnore); } } catch { Console.WriteLine("Error parsing: " + gitIgnoreFilePath); } return(true); } if (!tfIgnore) { gitIgnoreFilePath = Path.Combine(directoryPath, ".tfignore"); tfIgnore = true; } else { break; } } return(false); }
private GitRepository CreateRepository( string workingDir = null, GitConfig config = null, string commitSha = null, ImmutableArray <GitSubmodule> submodules = default, GitIgnore ignore = null) { workingDir ??= _workingDir; var gitDir = Path.Combine(workingDir, ".git"); return(new GitRepository( GitEnvironment.Empty, config ?? GitConfig.Empty, gitDir, gitDir, _workingDir, submodules.IsDefault ? ImmutableArray <GitSubmodule> .Empty : submodules, submoduleDiagnostics: ImmutableArray <string> .Empty, ignore ?? new GitIgnore(root: null, workingDir, ignoreCase: false), commitSha)); }
private bool Include(string filePath, string gitIgnoreDirectoryPath, GitIgnore gitIgnore, bool isDirectory = false) { var originalFilePath = filePath; // Exclude .git folder if (filePath.Contains(@"\.git\")) { return(false); } if (gitIgnore == null) { return(true); } int startIndex = gitIgnoreDirectoryPath.Length; while (filePath.Length > startIndex && (filePath[startIndex] == '\\' || filePath[startIndex] == '/')) { startIndex++; } filePath = filePath.Substring(startIndex); var include = !gitIgnore.Excludes(filePath); if (isDirectory && include) { include = !gitIgnore.Excludes(filePath + '/'); } if (!include) { Console.WriteLine("Ignoring: " + originalFilePath); } return(include); }
public void ShouldIgnoreFile() { //TODO: figure out how to hook up gitignore var ignore = new GitIgnore(); ignore.Excludes.Add("bin"); ignore.Excludes.Add("obj"); var root = new TestTree(null, "root") { new TestTreeEntry("hi"), new TestTreeEntry("obj", ObjectType.Tree) { new TestTreeEntry("something.o") }, new TestTreeEntry("bin", ObjectType.Tree) { new TestTreeEntry("something.exe"), new TestTreeEntry("something.pdb") }, new TestTreeEntry("files", ObjectType.Tree) { new TestTreeEntry("file1"), new TestTreeEntry("file2") }, new TestTreeEntry("readme.txt") }; var expectedEntries = new[] { "files/file1", "files/file2", "root/hi", "root/readme.txt" }; var i = 0; var items = root.EnumerateItems(true).ToArray(); Assert.AreEqual(expectedEntries.Length, items.Length, "Number of Entries"); foreach(var entry in items) { Assert.AreEqual(expectedEntries[i++], entry.FullName); } }
static void Main(string[] args) { var gitIgnoreTemplateNames = GitIgnore.GetAvailableTemplateNames(); var gitIgnoreTemplate = GitIgnore.CreateTemplate("visualstudio", "visualstudiocode", "rider"); }
public void IsIgnored_CaseSensitive() { using var temp = new TempRoot(); var rootDir = temp.CreateDirectory(); var workingDir = rootDir.CreateDirectory("Repo"); // root // A (.gitignore) // B // C (.gitignore) // D1, D2, D3 var dirA = workingDir.CreateDirectory("A"); var dirB = dirA.CreateDirectory("B"); var dirC = dirB.CreateDirectory("C"); dirC.CreateDirectory("D1"); dirC.CreateDirectory("D2"); dirC.CreateDirectory("D3"); dirA.CreateFile(".gitignore").WriteAllText(@" !z.txt *.txt !u.txt !v.txt !.git b/ D3/ Bar/**/*.xyz v.txt "); dirC.CreateFile(".gitignore").WriteAllText(@" !a.txt D2 D1/c.cs /*.c "); var ignore = new GitIgnore(root: null, workingDir.Path, ignoreCase: false); var matcher = ignore.CreateMatcher(); // outside of the working directory: Assert.Null(matcher.IsPathIgnored(rootDir.Path)); Assert.Null(matcher.IsPathIgnored(workingDir.Path.ToUpperInvariant())); // special case: Assert.True(matcher.IsPathIgnored(Path.Combine(workingDir.Path, ".git") + Path.DirectorySeparatorChar)); Assert.True(matcher.IsPathIgnored(Path.Combine(workingDir.Path, ".git", "config"))); Assert.False(matcher.IsPathIgnored(workingDir.Path)); Assert.False(matcher.IsPathIgnored(workingDir.Path + Path.DirectorySeparatorChar)); Assert.False(matcher.IsPathIgnored(Path.Combine(workingDir.Path, "X"))); // matches "*.txt" Assert.True(matcher.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "D1", "b.txt"))); // matches "!a.txt" Assert.False(matcher.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "D1", "a.txt"))); // matches "*.txt", "!z.txt" is ignored Assert.True(matcher.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "z.txt"))); // matches "*.txt", overriden by "!u.txt" Assert.False(matcher.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "u.txt"))); // matches "*.txt", overriden by "!v.txt", which is overriden by "v.txt" Assert.True(matcher.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "v.txt"))); // matches directory name "D2" Assert.True(matcher.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "D2", "E", "a.txt"))); // does not match "b/" (treated as a file path) Assert.False(matcher.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "D1", "b"))); // matches "b/" (treated as a directory path) Assert.True(matcher.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "D1", "b") + Path.DirectorySeparatorChar)); // matches "D3/" (existing directory path) Assert.True(matcher.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "D3"))); // matches "D1/c.cs" Assert.True(matcher.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "D1", "c.cs"))); // matches "Bar/**/*.xyz" Assert.True(matcher.IsPathIgnored(Path.Combine(workingDir.Path, "A", "Bar", "Baz", "Goo", ".xyz"))); // matches "/*.c" Assert.True(matcher.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "x.c"))); // does not match "/*.c" Assert.False(matcher.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "D1", "x.c"))); AssertEx.SetEqual(new[] { "/Repo/.git: True", "/Repo/A/B/C/D1/b: True", "/Repo/A/B/C/D1: False", "/Repo/A/B/C/D2/E: True", "/Repo/A/B/C/D2: True", "/Repo/A/B/C/D3: True", "/Repo/A/B/C: False", "/Repo/A/B: False", "/Repo/A: False", "/Repo: False" }, matcher.DirectoryIgnoreStateCache.Select(kvp => $"{kvp.Key.Substring(rootDir.Path.Length)}: {kvp.Value}")); }
public void TryParsePattern_None(string line) { Assert.False(GitIgnore.TryParsePattern(line, new StringBuilder(), out _, out _)); }
internal void TryParsePattern(string line, string glob, GitIgnore.PatternFlags flags) { Assert.True(GitIgnore.TryParsePattern(line, new StringBuilder(), out var actualGlob, out var actualFlags)); Assert.Equal(glob, actualGlob); Assert.Equal(flags, actualFlags); }
private void PopulateGitIgnore(FileSystem fileSystem, ref string directoryPath, out GitIgnore gitIgnore) { directoryPath = directoryPath.TrimEnd(PathSeparators); var originalDirectoryPath = directoryPath; gitIgnore = null; while (!string.IsNullOrEmpty(directoryPath)) { if (GetGitIgnore(fileSystem, ref directoryPath, out gitIgnore)) { break; } directoryPath = Path.GetDirectoryName(directoryPath); } gitIgnoreMap[originalDirectoryPath] = new KeyValuePair <string, GitIgnore>(directoryPath, gitIgnore); }