use git2::Signature;
use crate::git::commit_message::get_commit_message;
use super::*;
pub fn commit() {
let commit_message = get_commit_message();
commit_changes(&commit_message);
}
fn get_signature() -> Signature<'static> {
let repo = get_repo();
let config = repo.config().unwrap();
let name = config
.get_string("user.name")
.unwrap_or("Unknown".to_string());
let email = config
.get_string("user.email")
.unwrap_or("unknown@example.com".to_string());
Signature::now(&name, &email).unwrap()
}
fn commit_changes(commit_message: &str) {
let repo = get_repo();
let mut index = repo.index().unwrap();
let tree_id = index.write_tree().unwrap();
let tree = repo.find_tree(tree_id).unwrap();
let parent_commit = match repo.head() {
Ok(head_ref) => Some(head_ref.peel_to_commit().unwrap()),
Err(_) => None,
};
let signature = get_signature();
if let Some(parent) = parent_commit {
repo.commit(
Some("HEAD"),
&signature,
&signature,
commit_message,
&tree,
&[&parent],
)
.unwrap()
} else {
repo.commit(
Some("HEAD"),
&signature,
&signature,
commit_message,
&tree,
&[],
)
.unwrap()
};
}