@@ -30,96 +30,43 @@ Future<int> fixCopyrights(
30
30
void stdErr (String message) =>
31
31
(error ?? stderr.writeln as LogFunction ).call (message);
32
32
33
- final Set <String > submodulePaths;
33
+ String getExtension (File file) {
34
+ final pathExtension = path.extension (file.path);
35
+ return pathExtension.isNotEmpty ? pathExtension.substring (1 ) : '' ;
36
+ }
37
+
34
38
final gitRootResult = await processManager.run ([
35
39
'git' ,
36
40
'rev-parse' ,
37
41
'--show-toplevel' ,
38
42
]);
39
43
if (gitRootResult.exitCode != 0 ) {
40
- stdErr ('Warning: not a git repository. Cannot check for submodules.' );
41
- submodulePaths = < String > {};
42
- } else {
43
- final repoRoot = gitRootResult.stdout.toString ().trim ();
44
- final result = await processManager.run ([
45
- 'git' ,
46
- 'submodule' ,
47
- 'status' ,
48
- '--recursive' ,
49
- ], workingDirectory: repoRoot);
50
- if (result.exitCode == 0 ) {
51
- submodulePaths = result.stdout
52
- .toString ()
53
- .split ('\n ' )
54
- .where ((line) => line.trim ().isNotEmpty)
55
- .map ((line) {
56
- final parts = line.trim ().split (RegExp (r'\s+' ));
57
- if (parts.length > 1 ) {
58
- return path.canonicalize (path.join (repoRoot, parts[1 ]));
59
- }
60
- return null ;
61
- })
62
- .whereType <String >()
63
- .toSet ();
64
- } else {
65
- submodulePaths = < String > {};
66
- stdErr (
67
- 'Warning: could not get submodule status. '
68
- 'Not skipping any submodules.' ,
69
- );
70
- }
44
+ stdErr (
45
+ 'Error: not a git repository. '
46
+ 'This tool only works within a git repository.' ,
47
+ );
48
+ return 1 ;
71
49
}
50
+ final repoRoot = gitRootResult.stdout.toString ().trim ();
72
51
73
- String getExtension (File file) {
74
- final pathExtension = path.extension (file.path);
75
- return pathExtension.isNotEmpty ? pathExtension.substring (1 ) : '' ;
76
- }
52
+ final gitFilesResult = await processManager.run ([
53
+ 'git' ,
54
+ 'ls-files' ,
55
+ ...paths,
56
+ ], workingDirectory: repoRoot);
77
57
78
- Iterable <File > matchingFiles (Directory dir) {
79
- final files = < File > [];
80
- final directories = < Directory > [dir];
81
- while (directories.isNotEmpty) {
82
- final currentDir = directories.removeAt (0 );
83
- if (submodulePaths.contains (path.canonicalize (currentDir.path))) {
84
- stdLog ('Skipping submodule: ${currentDir .path }' );
85
- continue ;
86
- }
87
- try {
88
- for (final entity in currentDir.listSync ()) {
89
- if (entity is File ) {
90
- if (extensionMap.containsKey (getExtension (entity))) {
91
- files.add (entity.absolute);
92
- }
93
- } else if (entity is Directory ) {
94
- directories.add (entity);
95
- }
96
- }
97
- } on FileSystemException catch (e) {
98
- stdErr ('Could not list directory ${currentDir .path }: $e ' );
99
- }
100
- }
101
- return files;
58
+ if (gitFilesResult.exitCode != 0 ) {
59
+ stdErr ('Error running "git ls-files":\n ${gitFilesResult .stderr }' );
60
+ return 1 ;
102
61
}
103
62
104
- final rest = paths.isEmpty ? < String > ['.' ] : paths;
105
-
106
- final files = < File > [];
107
- for (final fileOrDir in rest) {
108
- switch (fileSystem.typeSync (fileOrDir)) {
109
- case FileSystemEntityType .directory:
110
- files.addAll (matchingFiles (fileSystem.directory (fileOrDir)));
111
- break ;
112
- case FileSystemEntityType .file:
113
- files.add (fileSystem.file (fileOrDir));
114
- break ;
115
- case FileSystemEntityType .link:
116
- case FileSystemEntityType .notFound:
117
- case FileSystemEntityType .pipe:
118
- case FileSystemEntityType .unixDomainSock:
119
- // We don't care about these, just ignore them.
120
- break ;
121
- }
122
- }
63
+ final files = gitFilesResult.stdout
64
+ .toString ()
65
+ .split ('\n ' )
66
+ .where ((line) => line.trim ().isNotEmpty)
67
+ .map ((filePath) => fileSystem.file (path.join (repoRoot, filePath)))
68
+ .where ((file) => extensionMap.containsKey (getExtension (file)))
69
+ .toList ();
123
70
124
71
final nonCompliantFiles = < File > [];
125
72
for (final file in files) {
0 commit comments