Skip to content

Add option to use rules from .gitignore even if .pubignore present #4546

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions lib/src/package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ See $workspacesDocUrl for more information.
'/pubspec_overrides.yaml',
];

static final _includeGitignoreIndicator = RegExp(
// We look for "#@ include .gitignore" at the start of a .pubignore
// We don't necessarily care about whitespace, just that it's at the start
// of the file, at the start of the line, and ends on its own line
r'^#\s*@\s*include\s*\.gitignore\s*?\r?\n',
);

/// Returns a list of files that are considered to be part of this package.
///
/// If [beneath] is passed, this will only return files beneath that path,
Expand Down Expand Up @@ -345,24 +352,35 @@ $symlinkResolvedDir => ${p.canonicalize(symlinkResolvedParent)}
});
},
ignoreForDir: (dir) {
final rules = [if (dir == beneath) ..._basicIgnoreRules];
var usedIgnoreFiles = '';

final pubIgnore = resolve('$dir/.pubignore');
final gitIgnore = resolve('$dir/.gitignore');
final ignoreFile =
fileExists(pubIgnore)
? pubIgnore
: (fileExists(gitIgnore) ? gitIgnore : null);

final rules = [
if (dir == beneath) ..._basicIgnoreRules,
if (ignoreFile != null) readTextFile(ignoreFile),
];

if (fileExists(pubIgnore)) {
final pubIgnoreText = readTextFile(pubIgnore);

rules.add(pubIgnoreText);
usedIgnoreFiles = pubIgnore;

if (pubIgnoreText.startsWith(_includeGitignoreIndicator) &&
fileExists(gitIgnore)) {
rules.add(readTextFile(gitIgnore));
usedIgnoreFiles += ' or $gitIgnore';
}
} else if (fileExists(gitIgnore)) {
rules.add(readTextFile(gitIgnore));
usedIgnoreFiles = gitIgnore;
}

return rules.isEmpty
? null
: Ignore(
rules,
onInvalidPattern: (pattern, exception) {
log.warning(
'$ignoreFile had invalid pattern $pattern. '
'$usedIgnoreFiles had invalid pattern $pattern. '
'${exception.message}',
);
},
Expand Down