use git2::IndexAddOption;
use git2::{Error as GitError, Repository, Signature};
use log::debug;
use log::info;
use std::fs::File;
use std::io::Write;
use tempfile::TempDir;
pub fn init_repo() -> Result<(TempDir, Repository), GitError> {
let temp_dir = TempDir::new().expect("failed to create temp dir");
let repo = Repository::init(temp_dir.path())?;
debug!("Initialized repository at {:?}", temp_dir.path());
let head_path = temp_dir.path().join(".git").join("HEAD");
std::fs::write(&head_path, "ref: refs/heads/master\n").expect("failed to write HEAD file");
debug!("Set HEAD to 'refs/heads/master'");
let file_path = temp_dir.path().join("test.txt");
{
let mut file = File::create(&file_path).expect("failed to create initial file");
writeln!(file, "initial content").expect("failed to write to initial file");
}
debug!("Created initial file: {:?}", file_path);
let mut index = repo.index()?;
index.add_all(["."].iter(), IndexAddOption::DEFAULT, None)?;
index.write()?;
debug!("Staged files via index.add_all");
let tree_id = index.write_tree()?;
let tree = repo.find_tree(tree_id)?;
debug!("Wrote tree with id: {}", tree_id);
let sig = Signature::now("Test User", "test@example.com")?;
let commit_oid = repo.commit(Some("HEAD"), &sig, &sig, "initial commit", &tree, &[])?;
debug!("Created initial commit: {}", commit_oid);
let head_ref = repo.head()?;
debug!("Final HEAD is: {:?}", head_ref.name().unwrap_or("unknown"));
info!("Repository initialized with HEAD pointing to 'refs/heads/master'");
drop(tree);
drop(head_ref);
Ok((temp_dir, repo))
}