Skip to content

Commit 78bb2cc

Browse files
committed
feat: provide branch info in stdin for push hooks
1 parent add8e85 commit 78bb2cc

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src/commit.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(crate) fn command_commit(
1313
if let Some(pre_commit) = &config.hooks.pre_commit
1414
&& !no_pre_hook
1515
{
16-
run_hook(config, pre_commit.clone(), "pre-commit")?;
16+
run_hook(config, pre_commit.clone(), "pre-commit", None)?;
1717
}
1818
if let Some(msg) = message {
1919
cmd!("jj", "commit", "--interactive", "--message", msg).run()?;
@@ -23,7 +23,7 @@ pub(crate) fn command_commit(
2323
if let Some(post_commit) = &config.hooks.post_commit
2424
&& !no_post_hook
2525
{
26-
run_hook(config, post_commit.clone(), "post-commit")?;
26+
run_hook(config, post_commit.clone(), "post-commit", None)?;
2727
}
2828
// TODO: should we give more options here?
2929
if push {
@@ -57,7 +57,7 @@ pub(crate) fn command_amend(
5757
if let Some(pre_commit) = &config.hooks.pre_commit
5858
&& !no_pre_hook
5959
{
60-
run_hook(config, pre_commit.clone(), "pre-commit")?;
60+
run_hook(config, pre_commit.clone(), "pre-commit", None)?;
6161
}
6262
let args: Vec<&str> = vec![
6363
"squash",
@@ -76,7 +76,7 @@ pub(crate) fn command_amend(
7676
if let Some(post_commit) = &config.hooks.post_commit
7777
&& !no_post_hook
7878
{
79-
run_hook(config, post_commit.clone(), "post-commit")?;
79+
run_hook(config, post_commit.clone(), "post-commit", None)?;
8080
}
8181
// TODO: should we give more options here?
8282
if push {

src/hook.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub(crate) fn run_hook(
1212
config: &AppConfig,
1313
script: String,
1414
hook_name: &str,
15+
stdin_input: Option<String>,
1516
) -> anyhow::Result<bool> {
1617
if config.hooks.use_just && !check_just_installed() {
1718
step("Install just to run hooks");
@@ -42,7 +43,14 @@ pub(crate) fn run_hook(
4243
.split_once(' ')
4344
.map(|(p, a)| (p, a.trim().split(' ').collect()))
4445
.unwrap_or((&script, Vec::new()));
45-
if let Err(e) = cmd(program, &args).run()
46+
47+
let result = if let Some(input) = stdin_input {
48+
cmd(program, &args).stdin_bytes(input.as_bytes()).run()
49+
} else {
50+
cmd(program, &args).run()
51+
};
52+
53+
if let Err(e) = result
4654
&& hook_name.starts_with("pre-")
4755
{
4856
anyhow::bail!("HJ {} hook failed: {}. Aborting.", hook_name, e.to_string());

src/push.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ pub(crate) fn command_push(
3232
if let Some(pre_push) = &config.hooks.pre_push
3333
&& !no_pre_hook
3434
{
35-
run_hook(config, pre_push.clone(), "pre-push")?;
35+
let stdin_input = if !branch.is_empty() {
36+
Some(branch.join(" "))
37+
} else {
38+
None
39+
};
40+
run_hook(config, pre_push.clone(), "pre-push", stdin_input)?;
3641
}
3742

3843
let mut args = vec!["git", "push", "--allow-new"];
@@ -55,7 +60,12 @@ pub(crate) fn command_push(
5560
if let Some(post_push) = &config.hooks.post_push
5661
&& !no_post_hook
5762
{
58-
run_hook(config, post_push.clone(), "post-push")?;
63+
let stdin_input = if !branch.is_empty() {
64+
Some(branch.join(" "))
65+
} else {
66+
None
67+
};
68+
run_hook(config, post_push.clone(), "post-push", stdin_input)?;
5969
}
6070

6171
Ok(())

0 commit comments

Comments
 (0)