Skip to content

Commit 7a8a255

Browse files
committed
add a global shell variable
1 parent c5902d9 commit 7a8a255

5 files changed

Lines changed: 32 additions & 1 deletion

File tree

reposerver/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ See `reposerver/example.reposerver.toml` for a complete example.
1919

2020
`global.indexer_args` are applied first for every invocation, then `repo.indexer_args` are appended.
2121
Per-branch args can be set with `[[repo.per_branch]]`; those args are appended last.
22+
Hooks run as `<global.shell> -c "<command>"` and `global.shell` defaults to `sh`.
2223

2324
Optional global finish hook:
2425

reposerver/example.reposerver.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
state_dir = ".reposerver-state"
33
default_interval = "5m"
44
max_repo_concurrency = 1
5+
shell = "sh"
56
git_bin = "git"
67
indexer_bin = "pointer-indexer"
78
indexer_args = ["--upload-url", "http://127.0.0.1:8080/api/v1/index"]

reposerver/src/config.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub struct GlobalConfig {
1818
pub state_dir: PathBuf,
1919
pub default_interval: Duration,
2020
pub max_repo_concurrency: usize,
21+
pub shell: String,
2122
pub git_bin: String,
2223
pub indexer_bin: String,
2324
pub indexer_args: Vec<String>,
@@ -61,6 +62,7 @@ struct RawGlobalConfig {
6162
state_dir: Option<PathBuf>,
6263
default_interval: Option<String>,
6364
max_repo_concurrency: Option<usize>,
65+
shell: Option<String>,
6466
git_bin: Option<String>,
6567
indexer_bin: Option<String>,
6668
#[serde(default)]
@@ -125,12 +127,16 @@ impl AppConfig {
125127

126128
let max_repo_concurrency = raw.global.max_repo_concurrency.unwrap_or(1).max(1);
127129

130+
let shell = raw.global.shell.unwrap_or_else(|| "sh".to_string());
128131
let git_bin = raw.global.git_bin.unwrap_or_else(|| "git".to_string());
129132
let indexer_bin = raw
130133
.global
131134
.indexer_bin
132135
.unwrap_or_else(|| "pointer-indexer".to_string());
133136

137+
if shell.trim().is_empty() {
138+
bail!("global.shell must not be empty");
139+
}
134140
if git_bin.trim().is_empty() {
135141
bail!("global.git_bin must not be empty");
136142
}
@@ -142,6 +148,7 @@ impl AppConfig {
142148
state_dir,
143149
default_interval,
144150
max_repo_concurrency,
151+
shell,
145152
git_bin,
146153
indexer_bin,
147154
indexer_args: raw.global.indexer_args,
@@ -331,9 +338,26 @@ mod tests {
331338
assert_eq!(cfg.repos.len(), 1);
332339
assert_eq!(cfg.repos[0].interval, Duration::from_secs(300));
333340
assert_eq!(cfg.global.max_repo_concurrency, 1);
341+
assert_eq!(cfg.global.shell, "sh");
334342
assert!(cfg.global.indexer_args.is_empty());
335343
}
336344

345+
#[test]
346+
fn rejects_empty_global_shell() {
347+
let raw = r#"
348+
[global]
349+
shell = " "
350+
351+
[[repo]]
352+
name = "foo"
353+
url = "git@example.com:foo.git"
354+
branches = ["main"]
355+
"#;
356+
let parsed: FileConfig = toml::from_str(raw).expect("parse config");
357+
let err = AppConfig::from_raw(parsed).expect_err("should fail");
358+
assert!(err.to_string().contains("global.shell"));
359+
}
360+
337361
#[test]
338362
fn rejects_zero_duration() {
339363
let raw = r#"

reposerver/src/hooks.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub struct HookResult {
1616
}
1717

1818
pub async fn run_hook(
19+
shell: &str,
1920
hook: &HookConfig,
2021
hook_type: &str,
2122
hook_index: usize,
@@ -28,6 +29,7 @@ pub async fn run_hook(
2829
info!(
2930
stage = "hook",
3031
event = "hook.begin",
32+
shell = %shell,
3133
hook_type = %hook_type,
3234
hook_index,
3335
repo = %repo,
@@ -37,7 +39,7 @@ pub async fn run_hook(
3739
"starting hook command"
3840
);
3941

40-
let mut cmd = Command::new("sh");
42+
let mut cmd = Command::new(shell);
4143
cmd.arg("-c").arg(&hook.command);
4244
cmd.env("REPOSERVER_REPO", repo);
4345
cmd.env("REPOSERVER_BRANCH", branch);

reposerver/src/scheduler.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ impl Scheduler {
518518
for (idx, hook) in repo.pre_index_hooks.iter().enumerate() {
519519
let hook_index = idx + 1;
520520
match hooks::run_hook(
521+
&self.cfg.global.shell,
521522
hook,
522523
"pre",
523524
hook_index,
@@ -632,6 +633,7 @@ impl Scheduler {
632633
for (idx, hook) in repo.post_upload_hooks.iter().enumerate() {
633634
let hook_index = idx + 1;
634635
match hooks::run_hook(
636+
&self.cfg.global.shell,
635637
hook,
636638
"post",
637639
hook_index,
@@ -747,6 +749,7 @@ impl Scheduler {
747749
);
748750

749751
match hooks::run_hook(
752+
&self.cfg.global.shell,
750753
hook,
751754
"global_finish",
752755
1,

0 commit comments

Comments
 (0)