How to Exclude Specific Files or Patterns from OpenCodeReview: 3 Configuration Methods
OpenCodeReview (OCR) lets you filter out files using the --exclude CLI flag, the "exclude" array in .opencodereview/rule.json, or built-in default patterns, with precedence following CLI > Project rule > Global rule > Defaults.
When using Alibaba's OpenCodeReview to analyze your codebase, you often need to skip generated files, dependencies, or test data to keep reviews focused on relevant code. The tool provides flexible mechanisms to exclude specific files or patterns from OpenCodeReview through multiple configuration layers that leverage Git-style glob syntax.
Understanding the Exclusion Architecture
OCR implements file filtering across three distinct layers defined in the source code. The --exclude flag in cmd/opencodereview/shared_flags.go provides immediate command-line control, while project-level configuration resides in .opencodereview/rule.json and is processed by the ProjectRule struct in internal/config/rules/system_rules.go. Additionally, OCR ships with built-in exclusions for common directories like node_modules/ and vendor/.
Excluding Files via the Command Line
The fastest way to filter files is via the --exclude flag. This flag accepts comma-separated glob patterns that are parsed into a string slice in cmd/opencodereview/shared_flags.go (lines 40-45).
The applyCLIExcludes function in cmd/opencodereview/shared.go (lines 17-28) merges these CLI patterns into the FileFilter.Exclude slice, appending them to any existing configuration:
# Exclude generated files and testdata directories
ocr review --exclude '**/generated/*,**/testdata/*' path/to/repo
Configuring Project-Level Exclusions in rule.json
For persistent exclusions, create a .opencodereview/rule.json file at your repository root. The "exclude" array in this JSON file maps to the Exclude field in the ProjectRule struct defined in internal/config/rules/system_rules.go (lines 102-107):
{
"exclude": [
"**/node_modules/**",
"**/*.log",
"**/generated/**",
"secret_config.yaml"
]
}
The loadProjectRule function reads this configuration and merges it into the FileFilter consulted by the scanner in internal/scan/provider.go via the IsUserExcluded method.
How Pattern Matching Works
OCR uses the doublestar library to support full Git-style glob syntax, including ** for recursive directories, brace expansion ({a,b}), and case-insensitive matching. Before comparison, the expandBraces helper in system_rules.go (lines 69-92) processes patterns to handle curly brace expansions.
The precedence order is CLI > Project rule > Global rule > Built-in defaults, meaning command-line arguments always override file-based configurations. When scanning paths, the FileFilter.IsUserExcluded method performs the validation:
if cc.FileFilter.IsUserExcluded(path) {
// file is skipped – see FileFilter.IsUserExcluded in system_rules.go
}
Built-in Default Exclusions
Even without user configuration, OCR ignores common non-source directories through patterns embedded in default_exclude_patterns.json. These defaults are applied automatically when initializing the FileFilter in system_rules.go, automatically excluding paths like node_modules/, vendor/, and common test file patterns unless overridden by user-defined rules.
Summary
- CLI exclusion: Use
--excludewith comma-separated patterns for ad-hoc filtering; defined incmd/opencodereview/shared_flags.goand processed byapplyCLIExcludesinshared.go. - Project configuration: Define persistent patterns in
.opencodereview/rule.jsonusing the"exclude"array; loaded byloadProjectRuleininternal/config/rules/system_rules.go. - Pattern syntax: Supports
doublestarglobs with brace expansion viaexpandBraces(lines 69-92). - Precedence order: CLI arguments override project rules, which override global rules, which override built-in defaults.
- Implementation: The
FileFilter.IsUserExcludedmethod performs path validation during the scan process ininternal/scan/provider.go.
Frequently Asked Questions
Can I combine CLI exclusions with rule.json configurations?
Yes. Command-line patterns are appended to the rule-file list rather than replacing them. The applyCLIExcludes function in cmd/opencodereview/shared.go merges both sources, allowing you to maintain baseline exclusions in rule.json while adding temporary filters via the --exclude flag.
What glob syntax does OpenCodeReview support?
OCR uses the doublestar library, which supports Git-style glob patterns including ** for recursive directory matching, brace expansion like {a,b,c}, and standard wildcards. The expandBraces function in internal/config/rules/system_rules.go preprocesses patterns before matching occurs.
Where are the default exclusion patterns defined?
Built-in exclusions for directories like node_modules/ and vendor/ are stored in default_exclude_patterns.json and embedded into the binary. These are applied automatically when initializing the FileFilter in system_rules.go if no user-defined exclusions exist.
How do I programmatically check if a file would be excluded?
You can instantiate a FileFilter struct from internal/config/rules/system_rules.go and call IsUserExcluded(path). This method performs brace expansion and doublestar matching to determine if a path matches any exclusion patterns in the filter, as implemented in lines 21-32 of the same file.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →