Skip to content

Use Option for author and message in rebase_commit #393

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
Jun 3, 2019
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
72 changes: 65 additions & 7 deletions src/rebase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,13 @@ impl <'repo> Rebase<'repo> {

/// Commits the current patch. You must have resolved any conflicts that
/// were introduced during the patch application from the `git_rebase_next`
/// invocation.
pub fn commit(&mut self, author: &Signature, committer: &Signature, message: &str) -> Result<Oid, Error> {
/// invocation. To keep the author and message from the original commit leave
/// them as None
pub fn commit(&mut self, author: Option<&Signature>, committer: &Signature, message: Option<&str>) -> Result<Oid, Error> {
let mut id: raw::git_oid = unsafe { mem::zeroed() };
let message = try!(CString::new(message));
let message = try!(::opt_cstr(message));
unsafe {
try_call!(raw::git_rebase_commit(&mut id, self.raw, author.raw(), committer.raw(), ptr::null(), message));
try_call!(raw::git_rebase_commit(&mut id, self.raw, author.map(|a| a.raw()), committer.raw(), ptr::null(), message));
Ok(Binding::from_raw(&id as *const _))
}
}
Expand All @@ -173,9 +174,9 @@ impl <'repo> Rebase<'repo> {

/// Finishes a rebase that is currently in progress once all patches have
/// been applied.
pub fn finish(&mut self, signature: &Signature) -> Result<(), Error> {
pub fn finish(&mut self, signature: Option<&Signature>) -> Result<(), Error> {
unsafe {
try_call!(raw::git_rebase_finish(self.raw, signature.raw()));
try_call!(raw::git_rebase_finish(self.raw, signature.map(|s| s.raw())));
}

Ok(())
Expand Down Expand Up @@ -313,7 +314,8 @@ impl<'rebase> Binding for RebaseOperation<'rebase> {

#[cfg(test)]
mod tests {
use {RebaseOptions, RebaseOperationType};
use std::{fs, path};
use {RebaseOptions, RebaseOperationType, Signature};

#[test]
fn smoke() {
Expand Down Expand Up @@ -351,4 +353,60 @@ mod tests {
assert!(op.is_none());
}
}

#[test]
fn keeping_original_author_msg() {
let (td, repo) = ::test::repo_init();
let head_target = repo.head().unwrap().target().unwrap();
let tip = repo.find_commit(head_target).unwrap();
let sig = Signature::now("testname", "testemail").unwrap();
let mut index = repo.index().unwrap();

fs::File::create(td.path().join("file_a")).unwrap();
index.add_path(path::Path::new("file_a")).unwrap();
index.write().unwrap();
let tree_id_a = index.write_tree().unwrap();
let tree_a = repo.find_tree(tree_id_a).unwrap();
let c1 = repo
.commit(Some("refs/heads/master"), &sig, &sig, "A", &tree_a, &[&tip])
.unwrap();
let c1 = repo.find_commit(c1).unwrap();

fs::File::create(td.path().join("file_b")).unwrap();
index.add_path(path::Path::new("file_b")).unwrap();
index.write().unwrap();
let tree_id_b = index.write_tree().unwrap();
let tree_b = repo.find_tree(tree_id_b).unwrap();
let c2 = repo
.commit(Some("refs/heads/master"), &sig, &sig, "B", &tree_b, &[&c1])
.unwrap();

let branch = repo.find_annotated_commit(c2).unwrap();
let upstream = repo.find_annotated_commit(tip.id()).unwrap();
let mut opts: RebaseOptions = Default::default();
let mut rebase = repo
.rebase(Some(&branch), Some(&upstream), None, Some(&mut opts))
.unwrap();

assert_eq!(rebase.len(), 2);

{
rebase.next().unwrap().unwrap();
let id = rebase.commit(None, &sig, None).unwrap();
let commit = repo.find_commit(id).unwrap();
assert_eq!(commit.message(), Some("A"));
assert_eq!(commit.author().name(), Some("testname"));
assert_eq!(commit.author().email(), Some("testemail"));
}

{
rebase.next().unwrap().unwrap();
let id = rebase.commit(None, &sig, None).unwrap();
let commit = repo.find_commit(id).unwrap();
assert_eq!(commit.message(), Some("B"));
assert_eq!(commit.author().name(), Some("testname"));
assert_eq!(commit.author().email(), Some("testemail"));
}
rebase.finish(None).unwrap();
}
}