Skip to content

Commit d319702

Browse files
committed
hooksresult refactor
1 parent aff07c2 commit d319702

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

git2-hooks/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ pub enum HooksError {
1414

1515
#[error("shellexpand error:{0}")]
1616
ShellExpand(#[from] shellexpand::LookupError<std::env::VarError>),
17+
18+
#[error("hook process terminated by signal without exit code")]
19+
NoExitCode,
1720
}
1821

1922
/// crate specific `Result` type

git2-hooks/src/hookspath.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,15 @@ impl HookPaths {
241241
let stdout =
242242
String::from_utf8_lossy(&output.stdout).to_string();
243243

244+
// Get exit code, or fail if process was killed by signal
245+
let code =
246+
output.status.code().ok_or(HooksError::NoExitCode)?;
247+
244248
Ok(HookResult::Run(crate::HookRunResponse {
245249
hook,
246250
stdout,
247251
stderr,
248-
code: if output.status.success() {
249-
None
250-
} else {
251-
output.status.code()
252-
},
252+
code,
253253
}))
254254
}
255255
}

git2-hooks/src/lib.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ pub struct HookRunResponse {
5757
pub stdout: String,
5858
/// stderr output emitted by hook
5959
pub stderr: String,
60-
/// exit code as reported back from process calling the hook (None if successful)
61-
pub code: Option<i32>,
60+
/// exit code as reported back from process calling the hook (0 = success)
61+
pub code: i32,
6262
}
6363

6464
#[derive(Debug, PartialEq, Eq)]
@@ -73,15 +73,15 @@ impl HookResult {
7373
/// helper to check if result is ok (hook succeeded with exit code 0)
7474
pub const fn is_ok(&self) -> bool {
7575
match self {
76-
Self::Run(response) => response.code.is_none(),
77-
Self::NoHookFound => false,
76+
Self::Run(response) => response.code == 0,
77+
Self::NoHookFound => true,
7878
}
7979
}
8080

8181
/// helper to check if result was run and not successful (non-zero exit code)
8282
pub const fn is_not_successful(&self) -> bool {
8383
match self {
84-
Self::Run(response) => response.code.is_some(),
84+
Self::Run(response) => response.code != 0,
8585
Self::NoHookFound => false,
8686
}
8787
}
@@ -90,7 +90,7 @@ impl HookResult {
9090
impl HookRunResponse {
9191
/// Check if the hook succeeded (exit code 0)
9292
pub const fn is_successful(&self) -> bool {
93-
self.code.is_none()
93+
self.code == 0
9494
}
9595
}
9696

@@ -424,7 +424,7 @@ exit 0
424424

425425
let stdout = response.stdout.as_str().trim_ascii_end();
426426

427-
assert_eq!(response.code, Some(42));
427+
assert_eq!(response.code, 42);
428428
assert_eq!(response.hook, hook.hook);
429429
assert_eq!(stdout, TEXT, "{:?} != {TEXT:?}", stdout);
430430
assert!(response.stderr.is_empty());
@@ -561,7 +561,7 @@ exit 1
561561
unreachable!()
562562
};
563563

564-
assert_eq!(response.code.unwrap(), 1);
564+
assert_eq!(response.code, 1);
565565
assert_eq!(&response.stdout, "rejected\n");
566566
}
567567

@@ -640,7 +640,7 @@ sys.exit(1)
640640
unreachable!()
641641
};
642642

643-
assert_eq!(response.code.unwrap(), 1);
643+
assert_eq!(response.code, 1);
644644
assert_eq!(&response.stdout, "rejected\n");
645645

646646
assert_eq!(msg, String::from("msg\n"));
@@ -735,7 +735,7 @@ exit 2
735735
unreachable!()
736736
};
737737

738-
assert_eq!(response.code.unwrap(), 2);
738+
assert_eq!(response.code, 2);
739739
assert_eq!(&response.stdout, "rejected\n");
740740

741741
assert_eq!(
@@ -790,7 +790,7 @@ exit 3
790790
let HookResult::Run(response) = res else {
791791
unreachable!()
792792
};
793-
assert_eq!(response.code.unwrap(), 3);
793+
assert_eq!(response.code, 3);
794794
assert_eq!(&response.stdout, "failed\n");
795795
}
796796

0 commit comments

Comments
 (0)