Skip to content

Commit 570cb28

Browse files
authored
Use ignore files up to git root (#3126)
1 parent f7fdcdd commit 570cb28

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

lib/src/package.dart

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,20 +211,38 @@ class Package {
211211
/// [recursive] is true, this will return all files beneath that path;
212212
/// otherwise, it will only return files one level beneath it.
213213
///
214-
/// This will take .pubignore and .gitignore files into account. For each
215-
/// directory a .pubignore takes precedence over a .gitignore.
214+
/// This will take .pubignore and .gitignore files into account.
215+
///
216+
/// If [dir] is inside a git repository, all ignore files from the repo root
217+
/// are considered.
218+
///
219+
/// For each directory a .pubignore takes precedence over a .gitignore.
216220
///
217221
/// Note that the returned paths won't always be beneath [dir]. To safely
218222
/// convert them to paths relative to the package root, use [relative].
219223
List<String> listFiles({String beneath, bool recursive = true}) {
220224
// An in-memory package has no files.
221225
if (dir == null) return [];
222-
beneath = beneath == null ? '.' : p.toUri(p.normalize(beneath)).path;
226+
227+
var root = dir;
228+
if (git.isInstalled) {
229+
try {
230+
root = p.normalize(
231+
git.runSync(['rev-parse', '--show-toplevel'], workingDir: dir).first,
232+
);
233+
} on git.GitException {
234+
// Not in a git folder.
235+
}
236+
}
237+
beneath = p
238+
.toUri(p.normalize(p.relative(p.join(dir, beneath ?? '.'), from: root)))
239+
.path;
240+
if (beneath == './') beneath = '.';
223241
String resolve(String path) {
224242
if (Platform.isWindows) {
225-
return p.joinAll([dir, ...p.posix.split(path)]);
243+
return p.joinAll([root, ...p.posix.split(path)]);
226244
}
227-
return p.join(dir, path);
245+
return p.join(root, path);
228246
}
229247

230248
return Ignore.listFiles(
@@ -246,7 +264,7 @@ class Package {
246264
'''Pub does not support publishing packages with non-resolving symlink: `${entity.path}` => `$target`.''');
247265
}
248266
}
249-
final relative = p.relative(entity.path, from: this.dir);
267+
final relative = p.relative(entity.path, from: root);
250268
if (Platform.isWindows) {
251269
return p.posix.joinAll(p.split(relative));
252270
}

test/package_list_files_test.dart

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,23 +224,32 @@ void main() {
224224
"ignores files that are gitignored even if the package isn't "
225225
'the repo root', () async {
226226
await d.dir(appPath, [
227-
d.dir('sub', [
228-
d.appPubspec(),
229-
d.file('.gitignore', '*.txt'),
230-
d.file('file1.txt', 'contents'),
231-
d.file('file2.text', 'contents'),
232-
d.dir('subdir', [
233-
d.file('subfile1.txt', 'subcontents'),
234-
d.file('subfile2.text', 'subcontents')
235-
])
227+
d.file('.gitignore', '*.bak'),
228+
d.dir('rep', [
229+
d.file('.gitignore', '*.gak'),
230+
d.file('.pubignore', '*.hak'),
231+
d.dir('sub', [
232+
d.appPubspec(),
233+
d.file('.gitignore', '*.txt'),
234+
d.file('file1.txt', 'contents'),
235+
d.file('file2.text', 'contents'),
236+
d.file('file3.bak', 'contents'),
237+
d.file('file4.gak', 'contents'),
238+
d.file('file5.hak', 'contents'),
239+
d.dir('subdir', [
240+
d.file('subfile1.txt', 'subcontents'),
241+
d.file('subfile2.text', 'subcontents'),
242+
])
243+
]),
236244
])
237245
]).create();
238246

239-
createEntrypoint(p.join(appPath, 'sub'));
247+
createEntrypoint(p.join(appPath, 'rep', 'sub'));
240248

241249
expect(entrypoint.root.listFiles(), {
242250
p.join(root, 'pubspec.yaml'),
243251
p.join(root, 'file2.text'),
252+
p.join(root, 'file4.gak'),
244253
p.join(root, 'subdir', 'subfile2.text')
245254
});
246255
});

0 commit comments

Comments
 (0)