support altering commit-msg on ok hook

This commit is contained in:
Stephan Dilly 2020-04-09 11:13:02 +02:00
parent f144d97e0c
commit b9ac1865db

View File

@ -26,14 +26,12 @@ pub fn hooks_commit_msg(
let res = run_hook(repo_path, HOOK_COMMIT_MSG, &[&file_path]);
if let HookResult::NotOk(e) = res {
let mut file = file.reopen().unwrap();
msg.clear();
file.read_to_string(msg).unwrap();
HookResult::NotOk(e)
} else {
HookResult::Ok
}
// load possibly altered msg
let mut file = file.reopen().unwrap();
msg.clear();
file.read_to_string(msg).unwrap();
res
} else {
HookResult::Ok
}
@ -171,4 +169,35 @@ exit 1
assert_eq!(msg, String::from("msg\n"));
}
#[test]
#[cfg(not(windows))]
fn test_commit_msg_no_block_but_alter() {
let (_td, repo) = repo_init();
let root = repo.path().parent().unwrap();
let repo_path = root.as_os_str().to_str().unwrap();
let hook = b"
#!/bin/sh
echo 'msg' > $1
exit 0
";
File::create(&root.join(HOOK_COMMIT_MSG))
.unwrap()
.write_all(hook)
.unwrap();
Command::new("chmod")
.args(&["+x", HOOK_COMMIT_MSG])
.current_dir(root)
.output()
.unwrap();
let mut msg = String::from("test");
let res = hooks_commit_msg(repo_path, &mut msg);
assert_eq!(res, HookResult::Ok);
assert_eq!(msg, String::from("msg\n"));
}
}