forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

seed.rs152 lines, 4.5 KiBHistoryRawDownload
1//! The first commit of a new repo, for the README and license files the create form can add. It
2//! is written with the same plumbing as a wiki page (see [`crate::wiki`]): `hash-object`, a
3//! temporary index, `write-tree`, `commit-tree`, and a compare-and-swap `update-ref`.
4
5use std::path::Path;
6
7use crate::command::{GitCommand, GitEnv};
8use crate::error::Result;
9use crate::repo::check_branch;
10use crate::wiki::temp_index;
11
12/// Commits `files` (path and bytes, all at the top level) as the only commit on `branch` of the
13/// bare repo at `repo`, and returns it. The branch must not exist yet: the ref is moved from the
14/// zero oid, so an existing branch is an error and nothing is written to it.
15pub async fn initial_commit(
16 env: &GitEnv,
17 repo: &Path,
18 branch: &str,
19 files: &[(&str, &[u8])],
20 author_name: &str,
21 author_email: &str,
22 message: &str,
23) -> Result<String> {
24 check_branch(env, branch).await?;
25 let index = temp_index(repo);
26 let result = write_commit(
27 env,
28 repo,
29 &index,
30 branch,
31 files,
32 author_name,
33 author_email,
34 message,
35 )
36 .await;
37 // The index is scratch space. A leftover file is harmless, but do not keep it.
38 let _ = tokio::fs::remove_file(&index).await;
39 result
40}
41
42#[allow(clippy::too_many_arguments)]
43async fn write_commit(
44 env: &GitEnv,
45 repo: &Path,
46 index: &Path,
47 branch: &str,
48 files: &[(&str, &[u8])],
49 author_name: &str,
50 author_email: &str,
51 message: &str,
52) -> Result<String> {
53 for (path, content) in files {
54 let blob = GitCommand::new(env, "hash-object")
55 .git_dir(repo)
56 .arg("-w")
57 .arg("--stdin")
58 .output_stdin(content)
59 .await?;
60 let blob = String::from_utf8_lossy(&blob).trim().to_owned();
61 GitCommand::new(env, "update-index")
62 .git_dir(repo)
63 .env("GIT_INDEX_FILE", index)
64 .arg("--add")
65 .arg("--cacheinfo")
66 .user_arg("100644")?
67 .user_arg(&blob)?
68 .user_arg(path)?
69 .status()
70 .await?;
71 }
72
73 let tree = GitCommand::new(env, "write-tree")
74 .git_dir(repo)
75 .env("GIT_INDEX_FILE", index)
76 .output()
77 .await?;
78 let tree = String::from_utf8_lossy(&tree).trim().to_owned();
79
80 let commit = GitCommand::new(env, "commit-tree")
81 .config("commit.gpgsign=false")
82 .git_dir(repo)
83 .env("GIT_AUTHOR_NAME", author_name)
84 .env("GIT_AUTHOR_EMAIL", author_email)
85 .env("GIT_COMMITTER_NAME", author_name)
86 .env("GIT_COMMITTER_EMAIL", author_email)
87 .user_arg(&tree)?
88 .arg("-F")
89 .arg("-");
90 let oid = commit.output_stdin(message.as_bytes()).await?;
91 let oid = String::from_utf8_lossy(&oid).trim().to_owned();
92
93 // The zero oid makes this a create: it fails if the branch already has a commit.
94 GitCommand::new(env, "update-ref")
95 .git_dir(repo)
96 .arg("-m")
97 .arg("forge initial commit")
98 .user_arg(&format!("refs/heads/{branch}"))?
99 .user_arg(&oid)?
100 .user_arg(&"0".repeat(oid.len()))?
101 .status()
102 .await?;
103 Ok(oid)
104}
105
106#[cfg(test)]
107mod tests {
108 use super::*;
109 use crate::browse::{default_branch_commit, read_text_blob, read_tree};
110 use crate::repo::init_bare;
111
112 #[tokio::test]
113 async fn initial_commit_holds_the_files_on_a_fresh_branch() {
114 let dir = tempfile::tempdir().unwrap();
115 let env = GitEnv {
116 git_bin: "/usr/bin/git".into(),
117 data_dir: dir.path().to_path_buf(),
118 };
119 let repo = dir.path().join("r.git");
120 init_bare(&env, &repo, "main").await.unwrap();
121
122 let files: [(&str, &[u8]); 2] = [("README.md", b"# r\n"), ("LICENSE", b"text\n")];
123 let oid = initial_commit(
124 &env,
125 &repo,
126 "main",
127 &files,
128 "Tahinli",
129 "t@example.test",
130 "Initial commit",
131 )
132 .await
133 .unwrap();
134 assert_eq!(
135 default_branch_commit(&repo, "main").unwrap().as_deref(),
136 Some(oid.as_str())
137 );
138 let names: Vec<String> = read_tree(&repo, &oid, "")
139 .unwrap()
140 .unwrap()
141 .into_iter()
142 .map(|e| e.name)
143 .collect();
144 assert_eq!(names, ["LICENSE", "README.md"]);
145 assert_eq!(
146 read_text_blob(&repo, &oid, "README.md", 1024)
147 .unwrap()
148 .as_deref(),
149 Some("# r\n")
150 );
151 }
152}