Skip to content

Commit 971eddb

Browse files
committed
config: add hostname-based conditional config scopes
This enables configuration to be conditionally applied based on the hostname set in `operation.hostname`. Users can now use `--when.hostnames = ["host-a", "host-b"]` in their config files to apply settings only on specific machines. Fixes: #6441
1 parent 4140f94 commit 971eddb

File tree

6 files changed

+202
-0
lines changed

6 files changed

+202
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
5656

5757
* Add support for `--when.workspaces` config scopes.
5858

59+
* Add support for `--when.hostnames` config scopes. This allows configuration to
60+
be conditionally applied based on the hostname set in `operation.hostname`.
61+
5962
* `jj bisect run` accepts the command and arguments to pass to the command
6063
directly as positional arguments, such as
6164
`jj bisect --range=..main -- cargo check --all-targets`.

cli/src/cli_util.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3916,6 +3916,10 @@ impl<'a> CliRunner<'a> {
39163916
process_global_args_fn(ui, &matches)?;
39173917
}
39183918
config_env.set_command_name(command_name(&matches));
3919+
// Set hostname for --when.hostnames conditional config scopes.
3920+
if let Ok(hostname) = whoami::fallible::hostname() {
3921+
config_env.set_hostname(hostname);
3922+
}
39193923

39203924
let maybe_workspace_loader = if let Some(path) = &args.global_args.repository {
39213925
// TODO: maybe path should be canonicalized by WorkspaceLoader?

cli/src/config-schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,13 @@
950950
"type": "string"
951951
}
952952
},
953+
"hostnames": {
954+
"type": "array",
955+
"description": "List of hostnames to match the hostname",
956+
"items": {
957+
"type": "string"
958+
}
959+
},
953960
"workspaces": {
954961
"type": "array",
955962
"description": "List of paths to match the workspace path prefix",

cli/src/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ pub struct ConfigEnv {
376376
repo_config_path: Option<ConfigPath>,
377377
workspace_config_path: Option<ConfigPath>,
378378
command: Option<String>,
379+
hostname: Option<String>,
379380
}
380381

381382
impl ConfigEnv {
@@ -423,13 +424,18 @@ impl ConfigEnv {
423424
repo_config_path: None,
424425
workspace_config_path: None,
425426
command: None,
427+
hostname: None,
426428
}
427429
}
428430

429431
pub fn set_command_name(&mut self, command: String) {
430432
self.command = Some(command);
431433
}
432434

435+
pub fn set_hostname(&mut self, hostname: String) {
436+
self.hostname = Some(hostname);
437+
}
438+
433439
/// Returns the paths to the user-specific config files or directories.
434440
pub fn user_config_paths(&self) -> impl Iterator<Item = &Path> {
435441
self.user_config_paths.iter().map(ConfigPath::as_path)
@@ -602,6 +608,7 @@ impl ConfigEnv {
602608
repo_path: self.repo_path.as_deref(),
603609
workspace_path: self.workspace_path.as_deref(),
604610
command: self.command.as_deref(),
611+
hostname: self.hostname.as_deref(),
605612
};
606613
jj_lib::config::resolve(config.as_ref(), &context)
607614
}
@@ -1852,6 +1859,7 @@ mod tests {
18521859
repo_config_path: None,
18531860
workspace_config_path: None,
18541861
command: None,
1862+
hostname: None,
18551863
}
18561864
}
18571865
}

docs/config.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,6 +1883,12 @@ email = "[email protected]"
18831883
[--scope.user]
18841884
18851885

1886+
# override ui.pager on specific machines
1887+
[[--scope]]
1888+
--when.hostnames = ["work-laptop", "work-desktop"]
1889+
[--scope.ui]
1890+
pager = "delta"
1891+
18861892
# disable pagination for `jj status`, use `delta` for `jj diff` and `jj show`
18871893
[[--scope]]
18881894
--when.commands = ["status"]
@@ -1940,6 +1946,16 @@ wip = ["log", "-r", "work"]
19401946

19411947
Use `jj root` to see the workspace root directory.
19421948

1949+
* `--when.hostnames`: List of hostnames to match against the `operation.hostname`
1950+
config setting.
1951+
1952+
Hostnames are compared case-sensitively and must match exactly.
1953+
1954+
```toml
1955+
--when.hostnames = ["work-laptop"] # matches only "work-laptop"
1956+
--when.hostnames = ["home-desktop", "laptop"] # matches "home-desktop" OR "laptop"
1957+
```
1958+
19431959
* `--when.commands`: List of subcommands to match.
19441960

19451961
Subcommands are space-separated and matched by prefix.

0 commit comments

Comments
 (0)