use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicU64, Ordering}; use crate::command::{GitCommand, GitEnv}; use crate::error::Result; use crate::history::zero_oid; use crate::repo::check_branch; static INDEX_SEQ: AtomicU64 = AtomicU64::new(0); fn temp_index(repo: &Path) -> PathBuf { let seq = INDEX_SEQ.fetch_add(1, Ordering::Relaxed); let nanos = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .map(|d| d.as_nanos()) .unwrap_or(0); repo.join(format!("forge-index-{}-{nanos}-{seq}", std::process::id())) } pub async fn initial_commit( env: &GitEnv, repo: &Path, branch: &str, files: &[(&str, &[u8])], author_name: &str, author_email: &str, message: &str, ) -> Result { check_branch(env, branch).await?; let index = temp_index(repo); let result = write_commit( env, repo, &index, branch, files, author_name, author_email, message, ) .await; let _ = tokio::fs::remove_file(&index).await; result } #[allow(clippy::too_many_arguments)] async fn write_commit( env: &GitEnv, repo: &Path, index: &Path, branch: &str, files: &[(&str, &[u8])], author_name: &str, author_email: &str, message: &str, ) -> Result { for (path, content) in files { let blob = GitCommand::new(env, "hash-object") .git_dir(repo) .arg("-w") .arg("--stdin") .output_stdin(content) .await?; let blob = String::from_utf8_lossy(&blob).trim().to_owned(); GitCommand::new(env, "update-index") .git_dir(repo) .env("GIT_INDEX_FILE", index) .arg("--add") .arg("--cacheinfo") .user_arg("100644")? .user_arg(&blob)? .user_arg(path)? .status() .await?; } let tree = GitCommand::new(env, "write-tree") .git_dir(repo) .env("GIT_INDEX_FILE", index) .output() .await?; let tree = String::from_utf8_lossy(&tree).trim().to_owned(); let commit = GitCommand::new(env, "commit-tree") .config("commit.gpgsign=false") .git_dir(repo) .env("GIT_AUTHOR_NAME", author_name) .env("GIT_AUTHOR_EMAIL", author_email) .env("GIT_COMMITTER_NAME", author_name) .env("GIT_COMMITTER_EMAIL", author_email) .user_arg(&tree)? .arg("-F") .arg("-"); let oid = commit.output_stdin(message.as_bytes()).await?; let oid = String::from_utf8_lossy(&oid).trim().to_owned(); GitCommand::new(env, "update-ref") .git_dir(repo) .arg("-m") .arg("forge initial commit") .user_arg(&format!("refs/heads/{branch}"))? .user_arg(&oid)? .user_arg(&zero_oid(&oid))? .status() .await?; Ok(oid) } #[cfg(test)] mod tests { use super::*; use crate::browse::{default_branch_commit, read_text_blob, read_tree}; use crate::repo::init_bare; #[tokio::test] async fn initial_commit_holds_the_files_on_a_fresh_branch() { let dir = tempfile::tempdir().unwrap(); let env = GitEnv { git_bin: "/usr/bin/git".into(), data_dir: dir.path().to_path_buf(), }; let repo = dir.path().join("r.git"); init_bare(&env, &repo, "main").await.unwrap(); let files: [(&str, &[u8]); 2] = [("README.md", b"# r\n"), ("LICENSE", b"text\n")]; let oid = initial_commit( &env, &repo, "main", &files, "Tahinli", "t@example.test", "Initial commit", ) .await .unwrap(); assert_eq!( default_branch_commit(&repo, "main").unwrap().as_deref(), Some(oid.as_str()) ); let names: Vec = read_tree(&repo, &oid, "") .unwrap() .unwrap() .into_iter() .map(|e| e.name) .collect(); assert_eq!(names, ["LICENSE", "README.md"]); assert_eq!( read_text_blob(&repo, &oid, "README.md", 1024) .unwrap() .as_deref(), Some("# r\n") ); } }