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;
#[allow(dead_code)]
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))
}
#[allow(dead_code)]
pub struct FakeGitOps {
pub _dummy_repo: Repository,
pub temp_dir: tempfile::TempDir,
pub staged_files: Vec<std::path::PathBuf>,
pub tracked_files: Vec<std::path::PathBuf>,
pub deleted_files: Vec<std::path::PathBuf>,
}
#[allow(dead_code)]
impl FakeGitOps {
pub fn new(
_dummy_repo: Repository,
temp_dir: tempfile::TempDir,
staged_files: Vec<std::path::PathBuf>,
tracked_files: Vec<std::path::PathBuf>,
deleted_files: Vec<std::path::PathBuf>,
) -> Self {
FakeGitOps {
_dummy_repo,
temp_dir,
staged_files,
tracked_files,
deleted_files,
}
}
}
impl rusty_todo_md::git_utils::GitOpsTrait for FakeGitOps {
fn open_repository(&self, _repo_path: &std::path::Path) -> Result<Repository, GitError> {
Repository::open(self.temp_dir.path())
}
fn get_staged_files(&self, _repo: &Repository) -> Result<Vec<std::path::PathBuf>, GitError> {
Ok(self.staged_files.clone())
}
fn get_tracked_files(&self, _repo: &Repository) -> Result<Vec<std::path::PathBuf>, GitError> {
Ok(self.tracked_files.clone())
}
fn get_deleted_files(&self, _repo: &Repository) -> Result<Vec<std::path::PathBuf>, GitError> {
Ok(self.deleted_files.clone())
}
}