| 1 |
use std::ffi::CString; |
| 2 |
use std::{marker, mem, ptr, str}; |
| 3 |
|
| 4 |
use crate::build::CheckoutBuilder; |
| 5 |
use crate::util::Binding; |
| 6 |
use crate::{raw, Error, Index, MergeOptions, Oid, Signature}; |
| 7 |
|
| 8 |
/// Rebase options |
| 9 |
/// |
| 10 |
/// Use to tell the rebase machinery how to operate. |
| 11 |
pub struct RebaseOptions<'cb> { |
| 12 |
raw: raw::git_rebase_options, |
| 13 |
rewrite_notes_ref: Option<CString>, |
| 14 |
merge_options: Option<MergeOptions>, |
| 15 |
checkout_options: Option<CheckoutBuilder<'cb>>, |
| 16 |
} |
| 17 |
|
| 18 |
impl<'cb> Default for RebaseOptions<'cb> { |
| 19 |
fn default() -> Self { |
| 20 |
Self::new() |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
impl<'cb> RebaseOptions<'cb> { |
| 25 |
/// Creates a new default set of rebase options. |
| 26 |
pub fn new() -> RebaseOptions<'cb> { |
| 27 |
let mut opts = RebaseOptions { |
| 28 |
raw: unsafe { mem::zeroed() }, |
| 29 |
rewrite_notes_ref: None, |
| 30 |
merge_options: None, |
| 31 |
checkout_options: None, |
| 32 |
}; |
| 33 |
assert_eq!(unsafe { raw::git_rebase_init_options(&mut opts.raw, 1) }, 0); |
| 34 |
opts |
| 35 |
} |
| 36 |
|
| 37 |
/// Used by `Repository::rebase`, this will instruct other clients working on this |
| 38 |
/// rebase that you want a quiet rebase experience, which they may choose to |
| 39 |
/// provide in an application-specific manner. This has no effect upon |
| 40 |
/// libgit2 directly, but is provided for interoperability between Git |
| 41 |
/// tools. |
| 42 |
pub fn quiet(&mut self, quiet: bool) -> &mut RebaseOptions<'cb> { |
| 43 |
self.raw.quiet = quiet as i32; |
| 44 |
self |
| 45 |
} |
| 46 |
|
| 47 |
/// Used by `Repository::rebase`, this will begin an in-memory rebase, |
| 48 |
/// which will allow callers to step through the rebase operations and |
| 49 |
/// commit the rebased changes, but will not rewind HEAD or update the |
| 50 |
/// repository to be in a rebasing state. This will not interfere with |
| 51 |
/// the working directory (if there is one). |
| 52 |
pub fn inmemory(&mut self, inmemory: bool) -> &mut RebaseOptions<'cb> { |
| 53 |
self.raw.inmemory = inmemory as i32; |
| 54 |
self |
| 55 |
} |
| 56 |
|
| 57 |
/// Used by `finish()`, this is the name of the notes reference |
| 58 |
/// used to rewrite notes for rebased commits when finishing the rebase; |
| 59 |
/// if NULL, the contents of the configuration option `notes.rewriteRef` |
| 60 |
/// is examined, unless the configuration option `notes.rewrite.rebase` |
| 61 |
/// is set to false. If `notes.rewriteRef` is also NULL, notes will |
| 62 |
/// not be rewritten. |
| 63 |
pub fn rewrite_notes_ref(&mut self, rewrite_notes_ref: &str) -> &mut RebaseOptions<'cb> { |
| 64 |
self.rewrite_notes_ref = Some(CString::new(rewrite_notes_ref).unwrap()); |
| 65 |
self |
| 66 |
} |
| 67 |
|
| 68 |
/// Options to control how trees are merged during `next()`. |
| 69 |
pub fn merge_options(&mut self, opts: MergeOptions) -> &mut RebaseOptions<'cb> { |
| 70 |
self.merge_options = Some(opts); |
| 71 |
self |
| 72 |
} |
| 73 |
|
| 74 |
/// Options to control how files are written during `Repository::rebase`, |
| 75 |
/// `next()` and `abort()`. Note that a minimum strategy of |
| 76 |
/// `GIT_CHECKOUT_SAFE` is defaulted in `init` and `next`, and a minimum |
| 77 |
/// strategy of `GIT_CHECKOUT_FORCE` is defaulted in `abort` to match git |
| 78 |
/// semantics. |
| 79 |
pub fn checkout_options(&mut self, opts: CheckoutBuilder<'cb>) -> &mut RebaseOptions<'cb> { |
| 80 |
self.checkout_options = Some(opts); |
| 81 |
self |
| 82 |
} |
| 83 |
|
| 84 |
/// Acquire a pointer to the underlying raw options. |
| 85 |
pub fn raw(&mut self) -> *const raw::git_rebase_options { |
| 86 |
unsafe { |
| 87 |
if let Some(opts) = self.merge_options.as_mut().take() { |
| 88 |
ptr::copy_nonoverlapping(opts.raw(), &mut self.raw.merge_options, 1); |
| 89 |
} |
| 90 |
if let Some(opts) = self.checkout_options.as_mut() { |
| 91 |
opts.configure(&mut self.raw.checkout_options); |
| 92 |
} |
| 93 |
self.raw.rewrite_notes_ref = self |
| 94 |
.rewrite_notes_ref |
| 95 |
.as_ref() |
| 96 |
.map(|s| s.as_ptr()) |
| 97 |
.unwrap_or(ptr::null()); |
| 98 |
} |
| 99 |
&self.raw |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/// Representation of a rebase |
| 104 |
pub struct Rebase<'repo> { |
| 105 |
raw: *mut raw::git_rebase, |
| 106 |
_marker: marker::PhantomData<&'repo raw::git_rebase>, |
| 107 |
} |
| 108 |
|
| 109 |
impl<'repo> Rebase<'repo> { |
| 110 |
/// Gets the count of rebase operations that are to be applied. |
| 111 |
pub fn len(&self) -> usize { |
| 112 |
unsafe { raw::git_rebase_operation_entrycount(self.raw) } |
| 113 |
} |
| 114 |
|
| 115 |
/// Gets the original `HEAD` ref name for merge rebases. |
| 116 |
pub fn orig_head_name(&self) -> Result<Option<&str>, Error> { |
| 117 |
let name_bytes = |
| 118 |
unsafe { crate::opt_bytes(self, raw::git_rebase_orig_head_name(self.raw)) }; |
| 119 |
match name_bytes { |
| 120 |
Some(nb) => str::from_utf8(nb).map(|s| Some(s)).map_err(|e| e.into()), |
| 121 |
None => Ok(None), |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/// Gets the original HEAD id for merge rebases. |
| 126 |
pub fn orig_head_id(&self) -> Option<Oid> { |
| 127 |
unsafe { Oid::from_raw_opt(raw::git_rebase_orig_head_id(self.raw)) } |
| 128 |
} |
| 129 |
|
| 130 |
/// Gets the rebase operation specified by the given index. |
| 131 |
pub fn nth(&mut self, n: usize) -> Option<RebaseOperation<'_>> { |
| 132 |
unsafe { |
| 133 |
let op = raw::git_rebase_operation_byindex(self.raw, n); |
| 134 |
if op.is_null() { |
| 135 |
None |
| 136 |
} else { |
| 137 |
Some(RebaseOperation::from_raw(op)) |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
/// Gets the index of the rebase operation that is currently being applied. |
| 143 |
/// If the first operation has not yet been applied (because you have called |
| 144 |
/// `init` but not yet `next`) then this returns None. |
| 145 |
pub fn operation_current(&mut self) -> Option<usize> { |
| 146 |
let cur = unsafe { raw::git_rebase_operation_current(self.raw) }; |
| 147 |
if cur == raw::GIT_REBASE_NO_OPERATION { |
| 148 |
None |
| 149 |
} else { |
| 150 |
Some(cur) |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/// Gets the index produced by the last operation, which is the result of |
| 155 |
/// `next()` and which will be committed by the next invocation of |
| 156 |
/// `commit()`. This is useful for resolving conflicts in an in-memory |
| 157 |
/// rebase before committing them. |
| 158 |
/// |
| 159 |
/// This is only applicable for in-memory rebases; for rebases within a |
| 160 |
/// working directory, the changes were applied to the repository's index. |
| 161 |
pub fn inmemory_index(&mut self) -> Result<Index, Error> { |
| 162 |
let mut idx = ptr::null_mut(); |
| 163 |
unsafe { |
| 164 |
try_call!(raw::git_rebase_inmemory_index(&mut idx, self.raw)); |
| 165 |
Ok(Binding::from_raw(idx)) |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/// Commits the current patch. You must have resolved any conflicts that |
| 170 |
/// were introduced during the patch application from the `git_rebase_next` |
| 171 |
/// invocation. To keep the author and message from the original commit leave |
| 172 |
/// them as None |
| 173 |
pub fn commit( |
| 174 |
&mut self, |
| 175 |
author: Option<&Signature<'_>>, |
| 176 |
committer: &Signature<'_>, |
| 177 |
message: Option<&str>, |
| 178 |
) -> Result<Oid, Error> { |
| 179 |
let mut id: raw::git_oid = unsafe { mem::zeroed() }; |
| 180 |
let message = crate::opt_cstr(message)?; |
| 181 |
unsafe { |
| 182 |
try_call!(raw::git_rebase_commit( |
| 183 |
&mut id, |
| 184 |
self.raw, |
| 185 |
author.map(|a| a.raw()), |
| 186 |
committer.raw(), |
| 187 |
ptr::null(), |
| 188 |
message |
| 189 |
)); |
| 190 |
Ok(Binding::from_raw(&id as *const _)) |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/// Aborts a rebase that is currently in progress, resetting the repository |
| 195 |
/// and working directory to their state before rebase began. |
| 196 |
pub fn abort(&mut self) -> Result<(), Error> { |
| 197 |
unsafe { |
| 198 |
try_call!(raw::git_rebase_abort(self.raw)); |
| 199 |
} |
| 200 |
|
| 201 |
Ok(()) |
| 202 |
} |
| 203 |
|
| 204 |
/// Finishes a rebase that is currently in progress once all patches have |
| 205 |
/// been applied. |
| 206 |
pub fn finish(&mut self, signature: Option<&Signature<'_>>) -> Result<(), Error> { |
| 207 |
unsafe { |
| 208 |
try_call!(raw::git_rebase_finish(self.raw, signature.map(|s| s.raw()))); |
| 209 |
} |
| 210 |
|
| 211 |
Ok(()) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
impl<'rebase> Iterator for Rebase<'rebase> { |
| 216 |
type Item = Result<RebaseOperation<'rebase>, Error>; |
| 217 |
|
| 218 |
/// Performs the next rebase operation and returns the information about it. |
| 219 |
/// If the operation is one that applies a patch (which is any operation except |
| 220 |
/// GitRebaseOperation::Exec) then the patch will be applied and the index and |
| 221 |
/// working directory will be updated with the changes. If there are conflicts, |
| 222 |
/// you will need to address those before committing the changes. |
| 223 |
fn next(&mut self) -> Option<Result<RebaseOperation<'rebase>, Error>> { |
| 224 |
let mut out = ptr::null_mut(); |
| 225 |
unsafe { |
| 226 |
try_call_iter!(raw::git_rebase_next(&mut out, self.raw)); |
| 227 |
Some(Ok(RebaseOperation::from_raw(out))) |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
impl<'repo> Binding for Rebase<'repo> { |
| 233 |
type Raw = *mut raw::git_rebase; |
| 234 |
unsafe fn from_raw(raw: *mut raw::git_rebase) -> Rebase<'repo> { |
| 235 |
Rebase { |
| 236 |
raw, |
| 237 |
_marker: marker::PhantomData, |
| 238 |
} |
| 239 |
} |
| 240 |
fn raw(&self) -> *mut raw::git_rebase { |
| 241 |
self.raw |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
impl<'repo> Drop for Rebase<'repo> { |
| 246 |
fn drop(&mut self) { |
| 247 |
unsafe { raw::git_rebase_free(self.raw) } |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
/// A rebase operation |
| 252 |
/// |
| 253 |
/// Describes a single instruction/operation to be performed during the |
| 254 |
/// rebase. |
| 255 |
#[derive(Debug, PartialEq)] |
| 256 |
pub enum RebaseOperationType { |
| 257 |
/// The given commit is to be cherry-picked. The client should commit the |
| 258 |
/// changes and continue if there are no conflicts. |
| 259 |
Pick, |
| 260 |
|
| 261 |
/// The given commit is to be cherry-picked, but the client should prompt |
| 262 |
/// the user to provide an updated commit message. |
| 263 |
Reword, |
| 264 |
|
| 265 |
/// The given commit is to be cherry-picked, but the client should stop to |
| 266 |
/// allow the user to edit the changes before committing them. |
| 267 |
Edit, |
| 268 |
|
| 269 |
/// The given commit is to be squashed into the previous commit. The commit |
| 270 |
/// message will be merged with the previous message. |
| 271 |
Squash, |
| 272 |
|
| 273 |
/// The given commit is to be squashed into the previous commit. The commit |
| 274 |
/// message from this commit will be discarded. |
| 275 |
Fixup, |
| 276 |
|
| 277 |
/// No commit will be cherry-picked. The client should run the given command |
| 278 |
/// and (if successful) continue. |
| 279 |
Exec, |
| 280 |
} |
| 281 |
|
| 282 |
impl RebaseOperationType { |
| 283 |
/// Convert from the int into an enum. Returns None if invalid. |
| 284 |
pub fn from_raw(raw: raw::git_rebase_operation_t) -> Option<RebaseOperationType> { |
| 285 |
match raw { |
| 286 |
raw::GIT_REBASE_OPERATION_PICK => Some(RebaseOperationType::Pick), |
| 287 |
raw::GIT_REBASE_OPERATION_REWORD => Some(RebaseOperationType::Reword), |
| 288 |
raw::GIT_REBASE_OPERATION_EDIT => Some(RebaseOperationType::Edit), |
| 289 |
raw::GIT_REBASE_OPERATION_SQUASH => Some(RebaseOperationType::Squash), |
| 290 |
raw::GIT_REBASE_OPERATION_FIXUP => Some(RebaseOperationType::Fixup), |
| 291 |
raw::GIT_REBASE_OPERATION_EXEC => Some(RebaseOperationType::Exec), |
| 292 |
_ => None, |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
/// A rebase operation |
| 298 |
/// |
| 299 |
/// Describes a single instruction/operation to be performed during the |
| 300 |
/// rebase. |
| 301 |
#[derive(Debug)] |
| 302 |
pub struct RebaseOperation<'rebase> { |
| 303 |
raw: *const raw::git_rebase_operation, |
| 304 |
_marker: marker::PhantomData<Rebase<'rebase>>, |
| 305 |
} |
| 306 |
|
| 307 |
impl<'rebase> RebaseOperation<'rebase> { |
| 308 |
/// The type of rebase operation |
| 309 |
pub fn kind(&self) -> Option<RebaseOperationType> { |
| 310 |
unsafe { RebaseOperationType::from_raw((*self.raw).kind) } |
| 311 |
} |
| 312 |
|
| 313 |
/// The commit ID being cherry-picked. This will be populated for all |
| 314 |
/// operations except those of type `GIT_REBASE_OPERATION_EXEC`. |
| 315 |
pub fn id(&self) -> Oid { |
| 316 |
unsafe { Binding::from_raw(&(*self.raw).id as *const _) } |
| 317 |
} |
| 318 |
|
| 319 |
///The executable the user has requested be run. This will only |
| 320 |
/// be populated for operations of type RebaseOperationType::Exec |
| 321 |
pub fn exec(&self) -> Result<Option<&str>, Error> { |
| 322 |
let exec_bytes = unsafe { crate::opt_bytes(self, (*self.raw).exec) }; |
| 323 |
match exec_bytes { |
| 324 |
Some(eb) => str::from_utf8(eb).map(|s| Some(s)).map_err(|e| e.into()), |
| 325 |
None => Ok(None), |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
impl<'rebase> Binding for RebaseOperation<'rebase> { |
| 331 |
type Raw = *const raw::git_rebase_operation; |
| 332 |
unsafe fn from_raw(raw: *const raw::git_rebase_operation) -> RebaseOperation<'rebase> { |
| 333 |
RebaseOperation { |
| 334 |
raw, |
| 335 |
_marker: marker::PhantomData, |
| 336 |
} |
| 337 |
} |
| 338 |
fn raw(&self) -> *const raw::git_rebase_operation { |
| 339 |
self.raw |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
#[cfg(test)] |
| 344 |
mod tests { |
| 345 |
use crate::{RebaseOperationType, RebaseOptions, Signature}; |
| 346 |
use std::{fs, path}; |
| 347 |
|
| 348 |
#[test] |
| 349 |
fn smoke() { |
| 350 |
let (_td, repo) = crate::test::repo_init(); |
| 351 |
let head_target = repo.head().unwrap().target().unwrap(); |
| 352 |
let tip = repo.find_commit(head_target).unwrap(); |
| 353 |
let sig = tip.author(); |
| 354 |
let tree = tip.tree().unwrap(); |
| 355 |
|
| 356 |
// We just want to see the iteration work so we can create commits with |
| 357 |
// no changes |
| 358 |
let c1 = repo |
| 359 |
.commit(Some("refs/heads/main"), &sig, &sig, "foo", &tree, &[&tip]) |
| 360 |
.unwrap(); |
| 361 |
let c1 = repo.find_commit(c1).unwrap(); |
| 362 |
let c2 = repo |
| 363 |
.commit(Some("refs/heads/main"), &sig, &sig, "foo", &tree, &[&c1]) |
| 364 |
.unwrap(); |
| 365 |
|
| 366 |
let head = repo.find_reference("refs/heads/main").unwrap(); |
| 367 |
let branch = repo.reference_to_annotated_commit(&head).unwrap(); |
| 368 |
let upstream = repo.find_annotated_commit(tip.id()).unwrap(); |
| 369 |
let mut rebase = repo |
| 370 |
.rebase(Some(&branch), Some(&upstream), None, None) |
| 371 |
.unwrap(); |
| 372 |
|
| 373 |
assert_eq!(Ok(Some("refs/heads/main")), rebase.orig_head_name()); |
| 374 |
assert_eq!(Some(c2), rebase.orig_head_id()); |
| 375 |
|
| 376 |
assert_eq!(rebase.len(), 2); |
| 377 |
{ |
| 378 |
let op = rebase.next().unwrap().unwrap(); |
| 379 |
assert_eq!(op.kind(), Some(RebaseOperationType::Pick)); |
| 380 |
assert_eq!(op.id(), c1.id()); |
| 381 |
} |
| 382 |
{ |
| 383 |
let op = rebase.next().unwrap().unwrap(); |
| 384 |
assert_eq!(op.kind(), Some(RebaseOperationType::Pick)); |
| 385 |
assert_eq!(op.id(), c2); |
| 386 |
} |
| 387 |
{ |
| 388 |
let op = rebase.next(); |
| 389 |
assert!(op.is_none()); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
#[test] |
| 394 |
fn keeping_original_author_msg() { |
| 395 |
let (td, repo) = crate::test::repo_init(); |
| 396 |
let head_target = repo.head().unwrap().target().unwrap(); |
| 397 |
let tip = repo.find_commit(head_target).unwrap(); |
| 398 |
let sig = Signature::now("testname", "testemail").unwrap(); |
| 399 |
let mut index = repo.index().unwrap(); |
| 400 |
|
| 401 |
fs::File::create(td.path().join("file_a")).unwrap(); |
| 402 |
index.add_path(path::Path::new("file_a")).unwrap(); |
| 403 |
index.write().unwrap(); |
| 404 |
let tree_id_a = index.write_tree().unwrap(); |
| 405 |
let tree_a = repo.find_tree(tree_id_a).unwrap(); |
| 406 |
let c1 = repo |
| 407 |
.commit(Some("refs/heads/main"), &sig, &sig, "A", &tree_a, &[&tip]) |
| 408 |
.unwrap(); |
| 409 |
let c1 = repo.find_commit(c1).unwrap(); |
| 410 |
|
| 411 |
fs::File::create(td.path().join("file_b")).unwrap(); |
| 412 |
index.add_path(path::Path::new("file_b")).unwrap(); |
| 413 |
index.write().unwrap(); |
| 414 |
let tree_id_b = index.write_tree().unwrap(); |
| 415 |
let tree_b = repo.find_tree(tree_id_b).unwrap(); |
| 416 |
let c2 = repo |
| 417 |
.commit(Some("refs/heads/main"), &sig, &sig, "B", &tree_b, &[&c1]) |
| 418 |
.unwrap(); |
| 419 |
|
| 420 |
let branch = repo.find_annotated_commit(c2).unwrap(); |
| 421 |
let upstream = repo.find_annotated_commit(tip.id()).unwrap(); |
| 422 |
let mut opts: RebaseOptions<'_> = Default::default(); |
| 423 |
let mut rebase = repo |
| 424 |
.rebase(Some(&branch), Some(&upstream), None, Some(&mut opts)) |
| 425 |
.unwrap(); |
| 426 |
|
| 427 |
assert_eq!(rebase.len(), 2); |
| 428 |
|
| 429 |
{ |
| 430 |
rebase.next().unwrap().unwrap(); |
| 431 |
let id = rebase.commit(None, &sig, None).unwrap(); |
| 432 |
let commit = repo.find_commit(id).unwrap(); |
| 433 |
assert_eq!(commit.message(), Ok("A")); |
| 434 |
assert_eq!(commit.author().name(), Ok("testname")); |
| 435 |
assert_eq!(commit.author().email(), Ok("testemail")); |
| 436 |
} |
| 437 |
|
| 438 |
{ |
| 439 |
rebase.next().unwrap().unwrap(); |
| 440 |
let id = rebase.commit(None, &sig, None).unwrap(); |
| 441 |
let commit = repo.find_commit(id).unwrap(); |
| 442 |
assert_eq!(commit.message(), Ok("B")); |
| 443 |
assert_eq!(commit.author().name(), Ok("testname")); |
| 444 |
assert_eq!(commit.author().email(), Ok("testemail")); |
| 445 |
} |
| 446 |
rebase.finish(None).unwrap(); |
| 447 |
} |
| 448 |
} |