Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ignored paths used by binary tool #40777

Merged
merged 8 commits into from
May 10, 2024
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 15 additions & 37 deletions src/SourceBuild/content/eng/tools/BinaryToolKit/DetectBinaries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static class DetectBinaries

var matcher = new Matcher(StringComparison.Ordinal);
matcher.AddInclude("**/*");
matcher.AddExcludePatterns(await GetIgnoredPatternsAsync(targetDirectory));
matcher.AddExcludePatterns(GetIgnoredPatterns(targetDirectory));

IEnumerable<string> matchingFiles = matcher.GetResultsInFullPath(targetDirectory);

Expand All @@ -45,44 +45,22 @@ public static class DetectBinaries
return unmatchedBinaryFiles;
}

private static async Task<List<string>> GetIgnoredPatternsAsync(string targetDirectory)
private static List<string> GetIgnoredPatterns(string targetDirectory)
{
string gitDirectory = Path.Combine(targetDirectory, ".git");
bool isGitRepo = Directory.Exists(gitDirectory);

try
{
if (!isGitRepo)
{
// Configure a fake git repo to use so that we can run git clean -ndx
await ExecuteProcessAsync("git", $"-C {targetDirectory} init -q");
}

await ExecuteProcessAsync("git", $"-C {targetDirectory} config --global safe.directory {targetDirectory}");

string output = await ExecuteProcessAsync("git", $"-C {targetDirectory} clean -ndx");

List<string> ignoredPaths = output.Split(Environment.NewLine)
.Select(line => GitCleanRegex.Match(line))
.Where(match => match.Success)
.Select(match => match.Groups[3].Value)
.ToList();

if (isGitRepo)
{
ignoredPaths.Add(".git");
}

return ignoredPaths;
}
finally
return new List<string>
{
// Ensure .git directory is deleted if it wasn't originally a git repo
if (!isGitRepo && Directory.Exists(gitDirectory))
{
Directory.Delete(gitDirectory, true);
}
}
"**/.git/**/*",
"**/.dotnet/**/*",
"**/.packages/**/*",
"**/artifacts/**/*",
"**/prereqs/packages/**/*",
"**/*.binlog",
"**/bin/**/*",
"**/obj/**/*",
"**/.vs/**/*",
"**/.vscode/**/*",
"**/.tools/**/*",
};
}

private static async Task<bool> IsBinaryAsync(string filePath)
Expand Down