Skip to content

Commit 52a7aee

Browse files
committed
feat(ops): Reword support
1 parent 0d0d347 commit 52a7aee

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/ops.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,27 @@ pub fn squash(
197197
Ok(new_id)
198198
}
199199

200+
/// Reword `head_id`s commit
201+
pub fn reword(
202+
repo: &git2::Repository,
203+
head_id: git2::Oid,
204+
msg: &str,
205+
) -> Result<git2::Oid, git2::Error> {
206+
let old_commit = repo.find_commit(head_id)?;
207+
let parents = old_commit.parents().collect::<Vec<_>>();
208+
let parents = parents.iter().collect::<Vec<_>>();
209+
let tree = repo.find_tree(old_commit.tree_id())?;
210+
let new_id = repo.commit(
211+
None,
212+
&old_commit.author(),
213+
&old_commit.committer(),
214+
msg,
215+
&tree,
216+
&parents,
217+
)?;
218+
Ok(new_id)
219+
}
220+
200221
// From git2 crate
201222
#[cfg(unix)]
202223
fn bytes2path(b: &[u8]) -> &std::path::Path {

tests/ops.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,28 @@ fn squash_clean() {
8888

8989
temp.close().unwrap();
9090
}
91+
92+
#[test]
93+
fn reword() {
94+
let temp = assert_fs::TempDir::new().unwrap();
95+
let plan = git_fixture::Dag::load(std::path::Path::new("tests/fixtures/branches.yml")).unwrap();
96+
plan.run(temp.path()).unwrap();
97+
98+
let repo = git2::Repository::discover(temp.path()).unwrap();
99+
100+
{
101+
assert!(!git2_ext::ops::is_dirty(&repo));
102+
103+
let feature2 = repo
104+
.find_branch("feature2", git2::BranchType::Local)
105+
.unwrap();
106+
let feature2_id = feature2.get().target().unwrap();
107+
108+
let new_id = git2_ext::ops::reword(&repo, feature2_id, "New message").unwrap();
109+
110+
println!("{:#?}", new_id);
111+
assert!(!git2_ext::ops::is_dirty(&repo));
112+
}
113+
114+
temp.close().unwrap();
115+
}

0 commit comments

Comments
 (0)