ignorePatternValidationError function

  1. @visibleForTesting
String? ignorePatternValidationError(
  1. String pattern
)

Returns an error message when pattern is invalid for ignore matching.

Patterns must use POSIX-style forward slashes. Gitignore root anchors such as /build/ refer to the reference or target root, not the OS filesystem root.

Implementation

@visibleForTesting
String? ignorePatternValidationError(String pattern) {
  var body = pattern;
  if (body.startsWith('!')) {
    body = body.substring(1);
  }
  if (body.isEmpty) {
    return null;
  }

  if (body.contains(r'\')) {
    return 'ignore pattern must use POSIX-style forward slashes: $pattern';
  }
  if (RegExp('^[a-zA-Z]:/').hasMatch(body)) {
    return 'ignore pattern must be relative to the scan root; '
        'Windows-absolute paths are not allowed: $pattern';
  }
  if (body.startsWith('//')) {
    return 'ignore pattern must be relative to the scan root; '
        'use a single leading / to anchor to the scan root: $pattern';
  }

  return null;
}