Skip to content

Commit 7e42705

Browse files
committed
feat(hooks): Define post-rewrite wrapper
1 parent cf8f874 commit 7e42705

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/hooks.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::io::Write;
2-
31
#[derive(Clone, Debug)]
42
pub struct Hooks {
53
root: std::path::PathBuf,
@@ -75,13 +73,45 @@ impl Hooks {
7573
}
7674
let mut process = cmd.spawn()?;
7775
if let Some(stdin) = stdin {
76+
use std::io::Write;
77+
7878
process.stdin.as_mut().unwrap().write_all(stdin)?;
7979
}
8080
let exit = process.wait()?;
8181

8282
const SIGNAL_EXIT_CODE: i32 = 1;
8383
Ok(exit.code().unwrap_or(SIGNAL_EXIT_CODE))
8484
}
85+
86+
/// Run `post-rewrite` hook as if called by `git rebase`
87+
///
88+
/// The hook should be run after any automatic note copying (see "notes.rewrite.<command>" in
89+
/// git-config(1)) has happened, and thus has access to these notes.
90+
///
91+
/// **changedd_shas:**
92+
/// - For the squash and fixup operation, all commits that were squashed are listed as being rewritten to the squashed commit. This means
93+
/// that there will be several lines sharing the same new-sha1.
94+
/// - The commits are must be listed in the order that they were processed by rebase.
95+
/// - `git` doesn't include entries for dropped commits
96+
pub fn run_post_rewrite_rebase(
97+
&self,
98+
repo: &git2::Repository,
99+
changed_oids: &[(git2::Oid, git2::Oid)],
100+
) -> Result<(), std::io::Error> {
101+
let name = "post-rewrite";
102+
let command = "rebase";
103+
let args = [command];
104+
let mut stdin = String::new();
105+
for (old_oid, new_oid) in changed_oids {
106+
use std::fmt::Write;
107+
writeln!(stdin, "{} {}", old_oid, new_oid).expect("Always writeable");
108+
}
109+
110+
let code = self.run_hook(repo, name, &args, Some(stdin.as_bytes()), &[])?;
111+
log::trace!("Hook `{}` failed with code {}", name, code);
112+
113+
Ok(())
114+
}
85115
}
86116

87117
const PUSH_HOOKS: &[&str] = &[

0 commit comments

Comments
 (0)