@@ -9,8 +9,8 @@ namespace BinaryToolKit;
9
9
10
10
public static class DetectBinaries
11
11
{
12
- private static readonly string Utf16Marker = "UTF-16" ;
13
- private static readonly int ChunkSize = 4096 ;
12
+ private const string Utf16Marker = "UTF-16" ;
13
+ private const int ChunkSize = 4096 ;
14
14
private static readonly Regex GitCleanRegex = new Regex ( @"Would (remove|skip)( repository)? (.*)" ) ;
15
15
16
16
public static async Task < List < string > > ExecuteAsync ( string targetDirectory )
@@ -40,32 +40,40 @@ private static async Task<List<string>> GetIgnoredPatternsAsync(string targetDir
40
40
{
41
41
string gitDirectory = Path . Combine ( targetDirectory , ".git" ) ;
42
42
bool isGitRepo = Directory . Exists ( gitDirectory ) ;
43
- if ( ! isGitRepo )
43
+
44
+ try
44
45
{
45
- // Configure a fake git repo to use so that we can run git clean -ndx
46
- await ExecuteProcessAsync ( "git" , $ "-C { targetDirectory } init -q") ;
47
- }
46
+ if ( ! isGitRepo )
47
+ {
48
+ // Configure a fake git repo to use so that we can run git clean -ndx
49
+ await ExecuteProcessAsync ( "git" , $ "-C { targetDirectory } init -q") ;
50
+ }
48
51
49
- await ExecuteProcessAsync ( "git" , $ "-C { targetDirectory } config --global safe.directory { targetDirectory } ") ;
52
+ await ExecuteProcessAsync ( "git" , $ "-C { targetDirectory } config --global safe.directory { targetDirectory } ") ;
50
53
51
- string output = await ExecuteProcessAsync ( "git" , $ "-C { targetDirectory } clean -ndx") ;
54
+ string output = await ExecuteProcessAsync ( "git" , $ "-C { targetDirectory } clean -ndx") ;
52
55
53
- List < string > ignoredPaths = output . Split ( Environment . NewLine )
54
- . Select ( line => GitCleanRegex . Match ( line ) )
55
- . Where ( match => match . Success )
56
- . Select ( match => match . Groups [ 3 ] . Value )
57
- . ToList ( ) ;
56
+ List < string > ignoredPaths = output . Split ( Environment . NewLine )
57
+ . Select ( line => GitCleanRegex . Match ( line ) )
58
+ . Where ( match => match . Success )
59
+ . Select ( match => match . Groups [ 3 ] . Value )
60
+ . ToList ( ) ;
58
61
59
- if ( ! isGitRepo )
60
- {
61
- Directory . Delete ( gitDirectory , true ) ;
62
+ if ( isGitRepo )
63
+ {
64
+ ignoredPaths . Add ( ".git" ) ;
65
+ }
66
+
67
+ return ignoredPaths ;
62
68
}
63
- else
69
+ finally
64
70
{
65
- ignoredPaths . Add ( ".git" ) ;
71
+ // Ensure .git directory is deleted if it wasn't originally a git repo
72
+ if ( ! isGitRepo && Directory . Exists ( gitDirectory ) )
73
+ {
74
+ Directory . Delete ( gitDirectory , true ) ;
75
+ }
66
76
}
67
-
68
- return ignoredPaths ;
69
77
}
70
78
71
79
private static async Task < bool > IsBinaryAsync ( string filePath )
@@ -96,7 +104,7 @@ private static async Task<bool> IsNotUTF16Async(string file)
96
104
{
97
105
if ( Environment . OSVersion . Platform == PlatformID . Unix )
98
106
{
99
- string output = await ExecuteProcessAsync ( "file" , file ) ;
107
+ string output = await ExecuteProcessAsync ( "file" , $ " \" { file } \" " ) ;
100
108
output = output . Split ( ":" ) [ 1 ] . Trim ( ) ;
101
109
102
110
if ( output . Contains ( Utf16Marker ) )
0 commit comments