forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching·0 forks·Rust·AGPL-3.0·9.3 MiB·cd4373f·Tahinli·4 hours ago·347 commits
| 1 | use std::path::{Path, PathBuf}; |
| 2 | use std::sync::atomic::{AtomicU64, Ordering}; |
| 3 | |
| 4 | use crate::command::{GitCommand, GitEnv}; |
| 5 | use crate::error::Result; |
| 6 | use crate::history::zero_oid; |
| 7 | use crate::repo::check_branch; |
| 8 | |
| 9 | static INDEX_SEQ: AtomicU64 = AtomicU64::new(0); |
| 10 | |
| 11 | fn temp_index(repo: &Path) -> PathBuf { |
| 12 | let seq = INDEX_SEQ.fetch_add(1, Ordering::Relaxed); |
| 13 | let nanos = std::time::SystemTime::now() |
| 14 | .duration_since(std::time::UNIX_EPOCH) |
| 15 | .map(|d| d.as_nanos()) |
| 16 | .unwrap_or(0); |
| 17 | repo.join(format!("forge-index-{}-{nanos}-{seq}", std::process::id())) |
| 18 | } |
| 19 | |
| 20 | pub async fn initial_commit( |
| 21 | env: &GitEnv, |
| 22 | repo: &Path, |
| 23 | branch: &str, |
| 24 | files: &[(&str, &[u8])], |
| 25 | author_name: &str, |
| 26 | author_email: &str, |
| 27 | message: &str, |
| 28 | ) -> Result<String> { |
| 29 | check_branch(env, branch).await?; |
| 30 | let index = temp_index(repo); |
| 31 | let result = write_commit( |
| 32 | env, |
| 33 | repo, |
| 34 | &index, |
| 35 | branch, |
| 36 | files, |
| 37 | author_name, |
| 38 | author_email, |
| 39 | message, |
| 40 | ) |
| 41 | .await; |
| 42 | let _ = tokio::fs::remove_file(&index).await; |
| 43 | result |
| 44 | } |
| 45 | |
| 46 | #[allow(clippy::too_many_arguments)] |
| 47 | async fn write_commit( |
| 48 | env: &GitEnv, |
| 49 | repo: &Path, |
| 50 | index: &Path, |
| 51 | branch: &str, |
| 52 | files: &[(&str, &[u8])], |
| 53 | author_name: &str, |
| 54 | author_email: &str, |
| 55 | message: &str, |
| 56 | ) -> Result<String> { |
| 57 | for (path, content) in files { |
| 58 | let blob = GitCommand::new(env, "hash-object") |
| 59 | .git_dir(repo) |
| 60 | .arg("-w") |
| 61 | .arg("--stdin") |
| 62 | .output_stdin(content) |
| 63 | .await?; |
| 64 | let blob = String::from_utf8_lossy(&blob).trim().to_owned(); |
| 65 | GitCommand::new(env, "update-index") |
| 66 | .git_dir(repo) |
| 67 | .env("GIT_INDEX_FILE", index) |
| 68 | .arg("--add") |
| 69 | .arg("--cacheinfo") |
| 70 | .user_arg("100644")? |
| 71 | .user_arg(&blob)? |
| 72 | .user_arg(path)? |
| 73 | .status() |
| 74 | .await?; |
| 75 | } |
| 76 | |
| 77 | let tree = GitCommand::new(env, "write-tree") |
| 78 | .git_dir(repo) |
| 79 | .env("GIT_INDEX_FILE", index) |
| 80 | .output() |
| 81 | .await?; |
| 82 | let tree = String::from_utf8_lossy(&tree).trim().to_owned(); |
| 83 | |
| 84 | let commit = GitCommand::new(env, "commit-tree") |
| 85 | .config("commit.gpgsign=false") |
| 86 | .git_dir(repo) |
| 87 | .env("GIT_AUTHOR_NAME", author_name) |
| 88 | .env("GIT_AUTHOR_EMAIL", author_email) |
| 89 | .env("GIT_COMMITTER_NAME", author_name) |
| 90 | .env("GIT_COMMITTER_EMAIL", author_email) |
| 91 | .user_arg(&tree)? |
| 92 | .arg("-F") |
| 93 | .arg("-"); |
| 94 | let oid = commit.output_stdin(message.as_bytes()).await?; |
| 95 | let oid = String::from_utf8_lossy(&oid).trim().to_owned(); |
| 96 | |
| 97 | GitCommand::new(env, "update-ref") |
| 98 | .git_dir(repo) |
| 99 | .arg("-m") |
| 100 | .arg("forge initial commit") |
| 101 | .user_arg(&format!("refs/heads/{branch}"))? |
| 102 | .user_arg(&oid)? |
| 103 | .user_arg(&zero_oid(&oid))? |
| 104 | .status() |
| 105 | .await?; |
| 106 | Ok(oid) |
| 107 | } |
| 108 | |
| 109 | #[cfg(test)] |
| 110 | mod tests { |
| 111 | use super::*; |
| 112 | use crate::browse::{default_branch_commit, read_text_blob, read_tree}; |
| 113 | use crate::repo::init_bare; |
| 114 | |
| 115 | #[tokio::test] |
| 116 | async fn initial_commit_holds_the_files_on_a_fresh_branch() { |
| 117 | let dir = tempfile::tempdir().unwrap(); |
| 118 | let env = GitEnv { |
| 119 | git_bin: "/usr/bin/git".into(), |
| 120 | data_dir: dir.path().to_path_buf(), |
| 121 | }; |
| 122 | let repo = dir.path().join("r.git"); |
| 123 | init_bare(&env, &repo, "main").await.unwrap(); |
| 124 | |
| 125 | let files: [(&str, &[u8]); 2] = [("README.md", b"# r\n"), ("LICENSE", b"text\n")]; |
| 126 | let oid = initial_commit( |
| 127 | &env, |
| 128 | &repo, |
| 129 | "main", |
| 130 | &files, |
| 131 | "Tahinli", |
| 132 | "t@example.test", |
| 133 | "Initial commit", |
| 134 | ) |
| 135 | .await |
| 136 | .unwrap(); |
| 137 | assert_eq!( |
| 138 | default_branch_commit(&repo, "main").unwrap().as_deref(), |
| 139 | Some(oid.as_str()) |
| 140 | ); |
| 141 | let names: Vec<String> = read_tree(&repo, &oid, "") |
| 142 | .unwrap() |
| 143 | .unwrap() |
| 144 | .into_iter() |
| 145 | .map(|e| e.name) |
| 146 | .collect(); |
| 147 | assert_eq!(names, ["LICENSE", "README.md"]); |
| 148 | assert_eq!( |
| 149 | read_text_blob(&repo, &oid, "README.md", 1024) |
| 150 | .unwrap() |
| 151 | .as_deref(), |
| 152 | Some("# r\n") |
| 153 | ); |
| 154 | } |
| 155 | } |