Skip to content

Commit 4918552

Browse files
committed
feat(hook): Simplify transaction management
1 parent 585a646 commit 4918552

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/hooks.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,26 @@ impl Hooks {
144144
}
145145
}
146146

147+
/// Run `reference-transaction` hook to signal that all reference updates have been queued to the transaction.
148+
///
149+
/// **changed_refs (old, new, name):**
150+
/// - `name` is the full name of the ref
151+
/// - `old` is zeroed out when force updating the reference regardless of its current value or
152+
/// when the reference is to be created anew
153+
pub fn run_reference_transaction<'t>(
154+
&'t self,
155+
repo: &'t git2::Repository,
156+
changed_refs: &'t [(git2::Oid, git2::Oid, &'t str)],
157+
) -> Result<ReferenceTransaction<'_>, std::io::Error> {
158+
self.run_reference_transaction_prepare(repo, changed_refs)?;
159+
160+
Ok(ReferenceTransaction {
161+
hook: self,
162+
repo,
163+
changed_refs,
164+
})
165+
}
166+
147167
/// Run `reference-transaction` hook to signal that all reference updates have been queued to the transaction.
148168
///
149169
/// **changed_refs (old, new, name):**
@@ -245,6 +265,39 @@ impl Hooks {
245265
}
246266
}
247267

268+
pub struct ReferenceTransaction<'t> {
269+
hook: &'t Hooks,
270+
repo: &'t git2::Repository,
271+
changed_refs: &'t [(git2::Oid, git2::Oid, &'t str)],
272+
}
273+
274+
impl<'t> ReferenceTransaction<'t> {
275+
pub fn committed(self) {
276+
let Self {
277+
hook,
278+
repo,
279+
changed_refs,
280+
} = self;
281+
hook.run_reference_transaction_committed(repo, changed_refs);
282+
}
283+
284+
pub fn aborted(self) {
285+
let Self {
286+
hook,
287+
repo,
288+
changed_refs,
289+
} = self;
290+
hook.run_reference_transaction_aborted(repo, changed_refs);
291+
}
292+
}
293+
294+
impl<'t> Drop for ReferenceTransaction<'t> {
295+
fn drop(&mut self) {
296+
self.hook
297+
.run_reference_transaction_aborted(self.repo, self.changed_refs);
298+
}
299+
}
300+
248301
const PUSH_HOOKS: &[&str] = &[
249302
"pre-receive",
250303
"update",

0 commit comments

Comments
 (0)