Skip to content

Commit 0b5015a

Browse files
authored
fix_copyright should only look at files from git ls-files (#327)
1 parent a2d7016 commit 0b5015a

File tree

3 files changed

+52
-223
lines changed

3 files changed

+52
-223
lines changed

.github/workflows/flutter_packages.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ on:
1414
- ".github/workflows/flutter_packages.yaml"
1515
- "packages/**"
1616
- "examples/**"
17+
- "tool/**"
1718
pull_request:
1819
branches:
1920
- main
2021
paths:
2122
- ".github/workflows/flutter_packages.yaml"
2223
- "packages/**"
2324
- "examples/**"
25+
- "tool/**"
2426

2527
concurrency:
2628
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}

tool/fix_copyright/lib/src/fix_copyright.dart

Lines changed: 26 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -30,96 +30,43 @@ Future<int> fixCopyrights(
3030
void stdErr(String message) =>
3131
(error ?? stderr.writeln as LogFunction).call(message);
3232

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+
3438
final gitRootResult = await processManager.run([
3539
'git',
3640
'rev-parse',
3741
'--show-toplevel',
3842
]);
3943
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;
7149
}
50+
final repoRoot = gitRootResult.stdout.toString().trim();
7251

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);
7757

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;
10261
}
10362

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();
12370

12471
final nonCompliantFiles = <File>[];
12572
for (final file in files) {

0 commit comments

Comments
 (0)