Commit the staged changes with an accompanying message if applicable.
(message: &Option<String>, repo: &Repository)
| 13 | |
| 14 | /// Commit the staged changes with an accompanying message if applicable. |
| 15 | pub fn commit_changes(message: &Option<String>, repo: &Repository) -> Result<(), NomadError> { |
| 16 | match repo.signature() { |
| 17 | Ok(signature) => { |
| 18 | let checked_message = if let Some(message) = message { |
| 19 | message.to_string() |
| 20 | } else { |
| 21 | "Updating".to_string() |
| 22 | }; |
| 23 | |
| 24 | let mut index = repo.index()?; |
| 25 | let staged_tree = repo.find_tree(index.write_tree()?)?; |
| 26 | |
| 27 | let previous_head = repo.head()?.peel(ObjectType::Tree)?.id(); |
| 28 | |
| 29 | let parent_commit = get_last_commit(repo)?; |
| 30 | let commit_oid = repo |
| 31 | .commit( |
| 32 | Some("HEAD"), |
| 33 | &signature, |
| 34 | &signature, |
| 35 | &checked_message, |
| 36 | &staged_tree, |
| 37 | &[&parent_commit], |
| 38 | )? |
| 39 | .to_string(); |
| 40 | |
| 41 | let branch_name = get_repo_branch(repo).unwrap_or_else(|| "?".to_string()); |
| 42 | let branch = Colour::Green.bold().paint(branch_name).to_string(); |
| 43 | |
| 44 | let sliced_oid = &commit_oid[..7]; |
| 45 | |
| 46 | println!("\n[{branch} {sliced_oid}] {checked_message}\n"); |
| 47 | |
| 48 | let old_tree = repo.find_tree(previous_head)?; |
| 49 | if let (Some(files_changed), Some(insertions), Some(deletions)) = |
| 50 | get_diff_stats(&mut index, &old_tree, repo) |
| 51 | { |
| 52 | println!( |
| 53 | "| {colored_changed} {changed_label} changed | {colored_insertions} {insertions_label} | {colored_deletions} {deletions_label} |\n", |
| 54 | colored_changed = Colour::Fixed(172).bold().paint(format!("{files_changed}")), |
| 55 | changed_label = if files_changed == 1 { "file" } else { "files" }, |
| 56 | colored_insertions = Colour::Green.bold().paint(format!("+{insertions}")), |
| 57 | insertions_label = if insertions == 1 { "insertion" } else { "insertions" }, |
| 58 | colored_deletions = Colour::Red.bold().paint(format!("-{deletions}")), |
| 59 | deletions_label = if deletions == 1 { "deletion" } else { "deletions" }, |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | Ok(()) |
| 64 | } |
| 65 | Err(error) => Err(NomadError::GitError { |
| 66 | context: "Unable to commit changes without a Git signature".into(), |
| 67 | source: error, |
| 68 | }), |
| 69 | } |
| 70 | } |
no test coverage detected