Glob Pattern Matching Algorithm

Reference of the Glob Pattern Matching Algorithm

The glob pattern matching algorithm describes the rules and behavior for matching file paths against glob patterns on the Ketryx Platform, specifically for restricting dependency file locations and detecting Git-based items for repositories.

Examples

Glob Pattern
Meaning

**

Matches any file in the file path

!**

Matches no files

**/test.js

Matches any file in the given root directory and its sub directories matching test.js

test/**

Matches any file in the test directory and its sub directories

**/*.[jt]s

Matches any file with .js and .ts extensions

spec-[1-4].md

Matches files spec-1.md, spec-2.md, spec-3.md, and spec-4.md

**/*{.js,.ts,.jsx,.tsx}

Matches any file with .js, .ts, .jsx, and .tsx extensions

Recipes

Match everything but exclude a certain file

If you want to match all files but exclude dev/requirements.txt, you can use the following glob patterns:

**
!dev/requirements.txt

The first pattern ** matches all files. The second pattern !dev/requirements.txt negates the match for dev/requirements.txt, effectively excluding it.

Ignore package-lock.json but keep package.json

If you want to match all files but exclude package-lock.json while keeping package.json, you can use the following glob patterns:

**
!package-lock.json

The first pattern ** matches all files. The second pattern !package-lock.json negates the match for package-lock.json, effectively excluding it. Since package.json is not explicitly excluded, it will still be matched by the ** pattern.

As a result, package.json will be included in the analysis, while package-lock.json will be excluded. This way, Ketryx would only pick up the versions declared in package.json and ignore the lock file and the locked versions.

An alternative glob pattern to just select package.json would be:

**/package.json

This pattern will match any package.json file in any directory, compared to the previous pattern that matches all files except package-lock.json in the root directory.

Matching a file path when multiple glob patterns have been provided

  • Matching occurs from the first defined pattern to the last defined pattern

  • During the matching, it keeps a match / no match state that is updated based on the current matching result of each glob pattern

  • The given file path is matched against each glob pattern

  • As soon as a non-negated glob pattern matches the file path, the matching state would be true

  • From there on, negated patterns (starting with !) matching the file path may set the matching state back to false

  • Following negation, subsequent matches of non-negated patterns can revert the matching state back to true, allowing for toggling between inclusion and exclusion of files or paths

  • Alternating matches of negated and non-negated patterns can toggle the matching state, allowing fine-grained control over which files or paths are ultimately selected

  • The final matching result will be returned, i.e. "the last match wins", and whether the file is included in the analysis is based on whether that match is negated or not

Last updated

© 2024 Ketryx Corporation