VOOZH about

URL: https://deepwiki.com/guanguans/ai-commit/7.1-configuration-file-format-and-precedence

⇱ Configuration File Format and Precedence | guanguans/ai-commit | DeepWiki


Loading...
Menu

Configuration File Format and Precedence

Purpose and Scope

This document describes the configuration file system used by ai-commit, including file locations, formats, precedence rules, and the merge strategy employed by the ConfigManager. Understanding this system is essential for customizing ai-commit's behavior at different scopes (system-wide vs. project-specific).

For information about managing configuration using the CLI, see Configuration Management. For details on specific configuration options, see Generator-Specific Configuration, Commit Types Configuration, and Prompt Templates Configuration.


Configuration File Locations

The ai-commit configuration system employs a three-layer file hierarchy (plus runtime CLI overrides) with the following file locations:

Class and Constant Reference for Configuration Paths















































PriorityTypeLocationFormatScopeModifiable
1 (Lowest)Defaultconfig/ai-commit.phpPHP arrayApplication defaultsNo (shipped with app)
2Global~/.ai-commit/.ai-commit.jsonJSONUser-wide settingsYes
3Local./.ai-commit.jsonJSONProject-specificYes
4 (Highest)CLI ArgumentsRuntime flagsN/ASingle executionN/A

Default Configuration Path

The default configuration is a PHP file shipped with the application at config/ai-commit.php. This file contains all factory defaults and is never modified by user operations. It serves as the baseline for reset operations.

Global Configuration Path

The global configuration file is stored in the user's home directory. The path varies by operating system:

Unix/Linux/macOS:

~/.ai-commit/.ai-commit.json

Windows:

C:\Users\<username>\.ai-commit\.ai-commit.json

The path resolution logic is implemented in ConfigManager::globalPath() at app/ConfigManager.php90-95 On Unix, it shells out via exec('cd ~; pwd') to resolve ~. On Windows, it uses get_current_user() to build the path under C:\Users\.

Local Configuration Path

The local configuration file is located in the current working directory:

./.ai-commit.json

This allows project-specific overrides that can be committed to version control for team-wide conventions. Path resolution is implemented in app/ConfigManager.php97-106

File Selection and Merge Flow


Sources: app/ConfigManager.php47-48 app/ConfigManager.php70-106 app/Commands/ConfigCommand.php177-188


Configuration File Format Specifications

PHP Default Configuration Format

The default configuration file at config/ai-commit.php returns a plain PHP array. It is read-only from the user's perspective—loaded via require during the merge process and used as the baseline for reset operations. It is never written back by any command.

JSON Configuration Format

Both global and local configuration files use JSON format. The encoding options are defined in ConfigManager::JSON_OPTIONS at app/ConfigManager.php49:

FlagEffect
JSON_PRETTY_PRINTHuman-readable indentation
JSON_UNESCAPED_UNICODEPreserves Unicode characters (e.g., 中文)
JSON_UNESCAPED_SLASHESPrevents escaping of /
JSON_THROW_ON_ERRORThrows JsonException on malformed JSON

Example JSON Configuration:


Supported File Extensions

The configuration system supports only two file extensions, validated in app/ConfigManager.php258-262:

ExtensionHandlerUsage
.phprequire $fileDefault configuration only
.jsonFile::json($file)Global and local configurations

Any other file extension will throw an UnsupportedConfigFileTypeException.

Sources: app/ConfigManager.php47-49 app/ConfigManager.php252-269 tests/Unit/ConfigManagerTest.php70-72


Configuration Precedence and Merge Strategy

Precedence Hierarchy

Configuration values are resolved using a cascading precedence system, where higher-priority sources override lower-priority ones:


Merge Algorithm

The configuration merge strategy employs PHP's array_replace_recursive() function, which provides deep merging of nested arrays. The merge process is implemented in app/ConfigManager.php252-269:


Merge Behavior:

  1. Scalar Values: Higher-priority values completely replace lower-priority values
  2. Arrays: Higher-priority arrays are recursively merged with lower-priority arrays
  3. Nested Keys: Each nesting level is merged independently

Example Merge:

Given these configurations:

Default (Priority 1):


Global (Priority 2):


Local (Priority 3):


Result:


Sources: app/ConfigManager.php176-181 app/ConfigManager.php252-269


Configuration Loading Process

Static Factory Methods

The ConfigManager provides static factory methods for creating configuration instances:

MethodParametersDescriptionReference
ConfigManager::make()?array $itemsCreates manager from all available files or provided arrayapp/ConfigManager.php70-80
ConfigManager::makeFrom()string ...$filesCreates manager from specific filesapp/ConfigManager.php85-88
ConfigManager::load()NoneCreates manager and registers it globally via Config::set()app/ConfigManager.php62-65

Loading Sequence Diagram

ConfigManager::load() Bootstrap Sequence


File Reading Implementation

The private ConfigManager::readFrom(string ...$files) method at app/ConfigManager.php252-269 handles both PHP and JSON formats. It iterates over each file, dispatches via a match on File::extension($file), and accumulates results with array_replace_recursive(). Unsupported extensions cause UnsupportedConfigFileTypeException to be thrown immediately.

Configuration Replacement at Runtime

After initial loading, configurations can be replaced or updated using ConfigManager::replaceFrom(string $file) at app/ConfigManager.php168-171 It reads a file and calls ConfigManager::replace() which uses array_replace_recursive() on the internal $items array. The ConfigCommand calls this after each mutating action (set, unset, reset) to ensure the in-memory state reflects the target file before writing.

Sources: app/ConfigManager.php62-88 app/ConfigManager.php168-181 app/ConfigManager.php252-269


Configuration Persistence

Write Operations

The ConfigManager provides methods to persist configuration changes back to files:

MethodTargetReference
putGlobal()Global configuration fileapp/ConfigManager.php111-114
putLocal()Local configuration fileapp/ConfigManager.php119-122
putFile(string $file)Arbitrary file pathapp/ConfigManager.php127-163

Filtering Non-Serializable Values

Before writing configuration to JSON files, the ConfigManager filters out non-serializable values in app/ConfigManager.php129-158 The following patterns are preserved:

  • generators.*.parameters.messages
  • generators.*.parameters.prompt
  • generators.*.parameters.user

All other object instances that are not JsonSerializable, Arrayable, or Jsonable are removed to prevent serialization errors.

Directory Creation

The putFile() method automatically creates the parent directory if it doesn't exist using File::ensureDirectoryExists() at app/ConfigManager.php160

Sources: app/ConfigManager.php111-163 tests/Feature/ConfigCommandTest.php30-58


ConfigCommand File Selection

The ConfigCommand determines which configuration file to operate on based on command options:


The resolution is handled by the private configFile() method at app/Commands/ConfigCommand.php177-188:

OptionResult
`--file-f `
`--global-g`
(neither)ConfigManager::localPath() (current working directory)

Sources: app/Commands/ConfigCommand.php152-162 app/Commands/ConfigCommand.php177-188 tests/Feature/ConfigCommandTest.php41-53


Configuration Initialization

Global Configuration Creation

The ConfigCommand::initialize() method at app/Commands/ConfigCommand.php170-175 runs before handle(). If ConfigManager::globalPath() does not exist on disk, it calls $this->configManager->putGlobal(), which writes the current (default-merged) configuration to ~/.ai-commit/.ai-commit.json and creates the directory as needed.

Local Configuration Creation

Local configuration files are created on-demand during the first config set operation without the --global flag. The file is created automatically by app/ConfigManager.php160:


Sources: app/Commands/ConfigCommand.php170-175 app/ConfigManager.php127-163


Error Handling

Invalid JSON Syntax

If a JSON configuration file contains syntax errors, the system throws a JsonException with error details. This is enforced by the JSON_THROW_ON_ERROR flag in app/ConfigManager.php49

Test Case: tests/Unit/ConfigManagerTest.php66-68

Unsupported File Types

Attempting to load a configuration file with an unsupported extension (anything other than .php or .json) throws an UnsupportedConfigFileTypeException.

Test Case: tests/Unit/ConfigManagerTest.php70-72

Missing Configuration Keys

When accessing a non-existent configuration key via get(), the ConfigManager (extending Laravel's Repository) returns null by default or an optional default value if provided.

Sources: app/ConfigManager.php252-269 app/Exceptions/UnsupportedConfigFileTypeException.php tests/Unit/ConfigManagerTest.php66-72

Refresh this wiki

On this page