Skip to content

feat(hook): Simplify transaction management #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 81 additions & 13 deletions src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl Hooks {
&self,
repo: &git2::Repository,
changed_oids: &[(git2::Oid, git2::Oid)],
) -> Result<(), std::io::Error> {
) {
let name = "post-rewrite";
let command = "rebase";
let args = [command];
Expand All @@ -133,10 +133,35 @@ impl Hooks {
writeln!(stdin, "{} {}", old_oid, new_oid).expect("Always writeable");
}

let code = self.run_hook(repo, name, &args, Some(stdin.as_bytes()), &[])?;
log::trace!("Hook `{}` failed with code {}", name, code);
match self.run_hook(repo, name, &args, Some(stdin.as_bytes()), &[]) {
Ok(code) if code != 0 => {
log::trace!("Hook `{}` failed with code {}", name, code);
}
Ok(_) => {}
Comment on lines +137 to +140
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Ok(code) if code != 0 => {
log::trace!("Hook `{}` failed with code {}", name, code);
}
Ok(_) => {}
Ok(0) => {}
Ok(code) => {
log::trace!("Hook `{}` failed with code {}", name, code);
}

Likewise below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #6

Err(err) => {
log::trace!("Hook `{}` failed with {}", name, err);
}
}
}

Ok(())
/// Run `reference-transaction` hook to signal that all reference updates have been queued to the transaction.
///
/// **changed_refs (old, new, name):**
/// - `name` is the full name of the ref
/// - `old` is zeroed out when force updating the reference regardless of its current value or
/// when the reference is to be created anew
pub fn run_reference_transaction<'t>(
&'t self,
repo: &'t git2::Repository,
changed_refs: &'t [(git2::Oid, git2::Oid, &'t str)],
) -> Result<ReferenceTransaction<'_>, std::io::Error> {
self.run_reference_transaction_prepare(repo, changed_refs)?;

Ok(ReferenceTransaction {
hook: self,
repo,
changed_refs,
})
}

/// Run `reference-transaction` hook to signal that all reference updates have been queued to the transaction.
Expand Down Expand Up @@ -187,7 +212,7 @@ impl Hooks {
&self,
repo: &git2::Repository,
changed_refs: &[(git2::Oid, git2::Oid, &str)],
) -> Result<(), std::io::Error> {
) {
let name = "reference-transaction";
let state = "committed";
let args = [state];
Expand All @@ -197,10 +222,15 @@ impl Hooks {
writeln!(stdin, "{} {} {}", old_oid, new_oid, ref_name).expect("Always writeable");
}

let code = self.run_hook(repo, name, &args, Some(stdin.as_bytes()), &[])?;
log::trace!("Hook `{}` failed with code {}", name, code);

Ok(())
match self.run_hook(repo, name, &args, Some(stdin.as_bytes()), &[]) {
Ok(code) if code != 0 => {
log::trace!("Hook `{}` failed with code {}", name, code);
}
Ok(_) => {}
Err(err) => {
log::trace!("Hook `{}` failed with {}", name, err);
}
}
}

/// Run `reference-transaction` hook to signal that no changes have been made
Expand All @@ -213,7 +243,7 @@ impl Hooks {
&self,
repo: &git2::Repository,
changed_refs: &[(git2::Oid, git2::Oid, &str)],
) -> Result<(), std::io::Error> {
) {
let name = "reference-transaction";
let state = "aborted";
let args = [state];
Expand All @@ -223,10 +253,48 @@ impl Hooks {
writeln!(stdin, "{} {} {}", old_oid, new_oid, ref_name).expect("Always writeable");
}

let code = self.run_hook(repo, name, &args, Some(stdin.as_bytes()), &[])?;
log::trace!("Hook `{}` failed with code {}", name, code);
match self.run_hook(repo, name, &args, Some(stdin.as_bytes()), &[]) {
Ok(code) if code != 0 => {
log::trace!("Hook `{}` failed with code {}", name, code);
}
Ok(_) => {}
Err(err) => {
log::trace!("Hook `{}` failed with {}", name, err);
}
}
}
}

pub struct ReferenceTransaction<'t> {
hook: &'t Hooks,
repo: &'t git2::Repository,
changed_refs: &'t [(git2::Oid, git2::Oid, &'t str)],
}

impl<'t> ReferenceTransaction<'t> {
pub fn committed(self) {
let Self {
hook,
repo,
changed_refs,
} = self;
hook.run_reference_transaction_committed(repo, changed_refs);
}

pub fn aborted(self) {
let Self {
hook,
repo,
changed_refs,
} = self;
hook.run_reference_transaction_aborted(repo, changed_refs);
}
}

Ok(())
impl<'t> Drop for ReferenceTransaction<'t> {
fn drop(&mut self) {
self.hook
.run_reference_transaction_aborted(self.repo, self.changed_refs);
}
}

Expand Down