Skip to content

Commit 738e8e3

Browse files
authored
Merge pull request #393 from akhilkpdasan/rebase
Use Option for author and message in rebase_commit
2 parents d04a23f + d54d1b2 commit 738e8e3

File tree

1 file changed

+65
-7
lines changed

1 file changed

+65
-7
lines changed

src/rebase.rs

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,13 @@ impl <'repo> Rebase<'repo> {
151151

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

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

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

314315
#[cfg(test)]
315316
mod tests {
316-
use {RebaseOptions, RebaseOperationType};
317+
use std::{fs, path};
318+
use {RebaseOptions, RebaseOperationType, Signature};
317319

318320
#[test]
319321
fn smoke() {
@@ -351,4 +353,60 @@ mod tests {
351353
assert!(op.is_none());
352354
}
353355
}
356+
357+
#[test]
358+
fn keeping_original_author_msg() {
359+
let (td, repo) = ::test::repo_init();
360+
let head_target = repo.head().unwrap().target().unwrap();
361+
let tip = repo.find_commit(head_target).unwrap();
362+
let sig = Signature::now("testname", "testemail").unwrap();
363+
let mut index = repo.index().unwrap();
364+
365+
fs::File::create(td.path().join("file_a")).unwrap();
366+
index.add_path(path::Path::new("file_a")).unwrap();
367+
index.write().unwrap();
368+
let tree_id_a = index.write_tree().unwrap();
369+
let tree_a = repo.find_tree(tree_id_a).unwrap();
370+
let c1 = repo
371+
.commit(Some("refs/heads/master"), &sig, &sig, "A", &tree_a, &[&tip])
372+
.unwrap();
373+
let c1 = repo.find_commit(c1).unwrap();
374+
375+
fs::File::create(td.path().join("file_b")).unwrap();
376+
index.add_path(path::Path::new("file_b")).unwrap();
377+
index.write().unwrap();
378+
let tree_id_b = index.write_tree().unwrap();
379+
let tree_b = repo.find_tree(tree_id_b).unwrap();
380+
let c2 = repo
381+
.commit(Some("refs/heads/master"), &sig, &sig, "B", &tree_b, &[&c1])
382+
.unwrap();
383+
384+
let branch = repo.find_annotated_commit(c2).unwrap();
385+
let upstream = repo.find_annotated_commit(tip.id()).unwrap();
386+
let mut opts: RebaseOptions = Default::default();
387+
let mut rebase = repo
388+
.rebase(Some(&branch), Some(&upstream), None, Some(&mut opts))
389+
.unwrap();
390+
391+
assert_eq!(rebase.len(), 2);
392+
393+
{
394+
rebase.next().unwrap().unwrap();
395+
let id = rebase.commit(None, &sig, None).unwrap();
396+
let commit = repo.find_commit(id).unwrap();
397+
assert_eq!(commit.message(), Some("A"));
398+
assert_eq!(commit.author().name(), Some("testname"));
399+
assert_eq!(commit.author().email(), Some("testemail"));
400+
}
401+
402+
{
403+
rebase.next().unwrap().unwrap();
404+
let id = rebase.commit(None, &sig, None).unwrap();
405+
let commit = repo.find_commit(id).unwrap();
406+
assert_eq!(commit.message(), Some("B"));
407+
assert_eq!(commit.author().name(), Some("testname"));
408+
assert_eq!(commit.author().email(), Some("testemail"));
409+
}
410+
rebase.finish(None).unwrap();
411+
}
354412
}

0 commit comments

Comments
 (0)