|
| 1 | +use git2::{Oid, RebaseOptions, Repository}; |
| 2 | + |
| 3 | +use super::{ |
| 4 | + commit::signature_allow_undefined_name, |
| 5 | + repo, |
| 6 | + utils::{bytes2string, get_head_refname}, |
| 7 | + CommitId, RepoPath, |
| 8 | +}; |
| 9 | +use crate::error::{Error, Result}; |
| 10 | + |
| 11 | +/// This is the same as reword, but will abort and fix the repo if something goes wrong |
| 12 | +pub fn reword( |
| 13 | + repo_path: &RepoPath, |
| 14 | + commit: CommitId, |
| 15 | + message: &str, |
| 16 | +) -> Result<CommitId> { |
| 17 | + let repo = repo(repo_path)?; |
| 18 | + let cur_branch_ref = get_head_refname(&repo)?; |
| 19 | + |
| 20 | + match reword_internal(&repo, commit.get_oid(), message) { |
| 21 | + Ok(id) => Ok(id.into()), |
| 22 | + // Something went wrong, checkout the previous branch then error |
| 23 | + Err(e) => { |
| 24 | + if let Ok(mut rebase) = repo.open_rebase(None) { |
| 25 | + rebase.abort()?; |
| 26 | + repo.set_head(&cur_branch_ref)?; |
| 27 | + repo.checkout_head(None)?; |
| 28 | + } |
| 29 | + Err(e) |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/// Gets the current branch the user is on. |
| 35 | +/// Returns none if they are not on a branch |
| 36 | +/// and Err if there was a problem finding the branch |
| 37 | +fn get_current_branch( |
| 38 | + repo: &Repository, |
| 39 | +) -> Result<Option<git2::Branch>> { |
| 40 | + for b in repo.branches(None)? { |
| 41 | + let branch = b?.0; |
| 42 | + if branch.is_head() { |
| 43 | + return Ok(Some(branch)); |
| 44 | + } |
| 45 | + } |
| 46 | + Ok(None) |
| 47 | +} |
| 48 | + |
| 49 | +/// Changes the commit message of a commit with a specified oid |
| 50 | +/// |
| 51 | +/// While this function is most commonly associated with doing a |
| 52 | +/// reword opperation in an interactive rebase, that is not how it |
| 53 | +/// is implemented in git2rs |
| 54 | +/// |
| 55 | +/// This is dangerous if it errors, as the head will be detached so this should |
| 56 | +/// always be wrapped by another function which aborts the rebase if something goes wrong |
| 57 | +fn reword_internal( |
| 58 | + repo: &Repository, |
| 59 | + commit: Oid, |
| 60 | + message: &str, |
| 61 | +) -> Result<Oid> { |
| 62 | + let sig = signature_allow_undefined_name(repo)?; |
| 63 | + |
| 64 | + let parent_commit_oid = repo |
| 65 | + .find_commit(commit)? |
| 66 | + .parent(0) |
| 67 | + .map_or(None, |parent_commit| Some(parent_commit.id())); |
| 68 | + |
| 69 | + let commit_to_change = if let Some(pc_oid) = parent_commit_oid { |
| 70 | + // Need to start at one previous to the commit, so |
| 71 | + // first rebase.next() points to the actual commit we want to change |
| 72 | + repo.find_annotated_commit(pc_oid)? |
| 73 | + } else { |
| 74 | + return Err(Error::NoParent); |
| 75 | + }; |
| 76 | + |
| 77 | + // If we are on a branch |
| 78 | + if let Ok(Some(branch)) = get_current_branch(repo) { |
| 79 | + let cur_branch_ref = bytes2string(branch.get().name_bytes())?; |
| 80 | + let cur_branch_name = bytes2string(branch.name_bytes()?)?; |
| 81 | + let top_branch_commit = repo.find_annotated_commit( |
| 82 | + branch.get().peel_to_commit()?.id(), |
| 83 | + )?; |
| 84 | + |
| 85 | + let mut rebase = repo.rebase( |
| 86 | + Some(&top_branch_commit), |
| 87 | + Some(&commit_to_change), |
| 88 | + None, |
| 89 | + Some(&mut RebaseOptions::default()), |
| 90 | + )?; |
| 91 | + |
| 92 | + let mut target; |
| 93 | + |
| 94 | + rebase.next(); |
| 95 | + if parent_commit_oid.is_none() { |
| 96 | + return Err(Error::NoParent); |
| 97 | + } |
| 98 | + target = rebase.commit(None, &sig, Some(message))?; |
| 99 | + let reworded_commit = target; |
| 100 | + |
| 101 | + // Set target to top commit, don't know when the rebase will end |
| 102 | + // so have to loop till end |
| 103 | + while rebase.next().is_some() { |
| 104 | + target = rebase.commit(None, &sig, None)?; |
| 105 | + } |
| 106 | + rebase.finish(None)?; |
| 107 | + |
| 108 | + // Now override the previous branch |
| 109 | + repo.branch( |
| 110 | + &cur_branch_name, |
| 111 | + &repo.find_commit(target)?, |
| 112 | + true, |
| 113 | + )?; |
| 114 | + |
| 115 | + // Reset the head back to the branch then checkout head |
| 116 | + repo.set_head(&cur_branch_ref)?; |
| 117 | + repo.checkout_head(None)?; |
| 118 | + return Ok(reworded_commit); |
| 119 | + } |
| 120 | + // Repo is not on a branch, possibly detached head |
| 121 | + Err(Error::NoBranch) |
| 122 | +} |
| 123 | + |
| 124 | +#[cfg(test)] |
| 125 | +mod tests { |
| 126 | + use super::*; |
| 127 | + use crate::sync::{ |
| 128 | + get_commit_info, |
| 129 | + tests::{repo_init_empty, write_commit_file}, |
| 130 | + }; |
| 131 | + use pretty_assertions::assert_eq; |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn test_reword() { |
| 135 | + let (_td, repo) = repo_init_empty().unwrap(); |
| 136 | + let root = repo.path().parent().unwrap(); |
| 137 | + |
| 138 | + let repo_path: &RepoPath = |
| 139 | + &root.as_os_str().to_str().unwrap().into(); |
| 140 | + |
| 141 | + write_commit_file(&repo, "foo", "a", "commit1"); |
| 142 | + |
| 143 | + let oid2 = write_commit_file(&repo, "foo", "ab", "commit2"); |
| 144 | + |
| 145 | + let branch = |
| 146 | + repo.branches(None).unwrap().next().unwrap().unwrap().0; |
| 147 | + let branch_ref = branch.get(); |
| 148 | + let commit_ref = branch_ref.peel_to_commit().unwrap(); |
| 149 | + let message = commit_ref.message().unwrap(); |
| 150 | + |
| 151 | + assert_eq!(message, "commit2"); |
| 152 | + |
| 153 | + let reworded = |
| 154 | + reword(repo_path, oid2.into(), "NewCommitMessage") |
| 155 | + .unwrap(); |
| 156 | + |
| 157 | + // Need to get the branch again as top oid has changed |
| 158 | + let branch = |
| 159 | + repo.branches(None).unwrap().next().unwrap().unwrap().0; |
| 160 | + let branch_ref = branch.get(); |
| 161 | + let commit_ref_new = branch_ref.peel_to_commit().unwrap(); |
| 162 | + let message_new = commit_ref_new.message().unwrap(); |
| 163 | + assert_eq!(message_new, "NewCommitMessage"); |
| 164 | + |
| 165 | + assert_eq!( |
| 166 | + message_new, |
| 167 | + get_commit_info(repo_path, &reworded).unwrap().message |
| 168 | + ); |
| 169 | + } |
| 170 | +} |
0 commit comments