| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | use std::path::{Path, PathBuf}; |
| 15 | use std::process::Stdio; |
| 16 | |
| 17 | use tokio::process::{Child, Command}; |
| 18 | |
| 19 | |
| 20 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 21 | pub enum Service { |
| 22 | |
| 23 | UploadPack, |
| 24 | |
| 25 | ReceivePack, |
| 26 | |
| 27 | UploadArchive, |
| 28 | } |
| 29 | |
| 30 | impl Service { |
| 31 | |
| 32 | fn subcommand(self) -> &'static str { |
| 33 | match self { |
| 34 | Self::UploadPack => "upload-pack", |
| 35 | Self::ReceivePack => "receive-pack", |
| 36 | Self::UploadArchive => "upload-archive", |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | |
| 42 | |
| 43 | #[derive(Debug, Clone, Default)] |
| 44 | pub struct PackEnv { |
| 45 | |
| 46 | |
| 47 | pub home: PathBuf, |
| 48 | |
| 49 | |
| 50 | pub protocol: Option<String>, |
| 51 | |
| 52 | pub config_path: Option<String>, |
| 53 | |
| 54 | pub user_id: Option<String>, |
| 55 | |
| 56 | pub user_name: Option<String>, |
| 57 | |
| 58 | pub repo_id: Option<String>, |
| 59 | |
| 60 | pub key_id: Option<String>, |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | pub fn spawn( |
| 73 | binary: &str, |
| 74 | service: Service, |
| 75 | extra_args: &[&str], |
| 76 | repo_path: &Path, |
| 77 | env: &PackEnv, |
| 78 | ) -> std::io::Result<Child> { |
| 79 | let mut cmd = Command::new(binary); |
| 80 | cmd.arg(service.subcommand()); |
| 81 | cmd.args(extra_args); |
| 82 | cmd.arg(repo_path); |
| 83 | |
| 84 | |
| 85 | cmd.env("GIT_DIR", repo_path); |
| 86 | cmd.env("GIT_TERMINAL_PROMPT", "0"); |
| 87 | cmd.env("GIT_CONFIG_NOSYSTEM", "1"); |
| 88 | cmd.env("HOME", &env.home); |
| 89 | |
| 90 | cmd.env_remove("GIT_ALTERNATE_OBJECT_DIRECTORIES"); |
| 91 | cmd.env_remove("GIT_OBJECT_DIRECTORY"); |
| 92 | |
| 93 | if let Some(protocol) = &env.protocol { |
| 94 | cmd.env("GIT_PROTOCOL", protocol); |
| 95 | } |
| 96 | for (key, value) in [ |
| 97 | ("FABRICA_CONFIG", &env.config_path), |
| 98 | ("FABRICA_USER_ID", &env.user_id), |
| 99 | ("FABRICA_USER_NAME", &env.user_name), |
| 100 | ("FABRICA_REPO_ID", &env.repo_id), |
| 101 | ("FABRICA_KEY_ID", &env.key_id), |
| 102 | ] { |
| 103 | if let Some(value) = value { |
| 104 | cmd.env(key, value); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | cmd.stdin(Stdio::piped()); |
| 109 | cmd.stdout(Stdio::piped()); |
| 110 | cmd.stderr(Stdio::piped()); |
| 111 | cmd.kill_on_drop(true); |
| 112 | cmd.spawn() |
| 113 | } |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | pub fn archive( |
| 124 | binary: &str, |
| 125 | repo_path: &Path, |
| 126 | format: &str, |
| 127 | rev: &str, |
| 128 | prefix: &str, |
| 129 | home: &Path, |
| 130 | ) -> std::io::Result<Child> { |
| 131 | let mut cmd = Command::new(binary); |
| 132 | cmd.arg("archive"); |
| 133 | cmd.arg(format!("--format={format}")); |
| 134 | cmd.arg(format!("--prefix={prefix}/")); |
| 135 | cmd.arg(rev); |
| 136 | cmd.env("GIT_DIR", repo_path); |
| 137 | cmd.env("GIT_TERMINAL_PROMPT", "0"); |
| 138 | cmd.env("GIT_CONFIG_NOSYSTEM", "1"); |
| 139 | cmd.env("HOME", home); |
| 140 | cmd.stdout(Stdio::piped()); |
| 141 | cmd.stderr(Stdio::null()); |
| 142 | cmd.kill_on_drop(true); |
| 143 | cmd.spawn() |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | #[must_use] |
| 149 | pub fn pkt_line(payload: &str) -> String { |
| 150 | format!("{:04x}{payload}", payload.len() + 4) |
| 151 | } |
| 152 | |
| 153 | |
| 154 | pub const FLUSH_PKT: &str = "0000"; |
| 155 | |
| 156 | #[cfg(test)] |
| 157 | mod tests { |
| 158 | #![allow(clippy::unwrap_used, clippy::expect_used)] |
| 159 | |
| 160 | use super::*; |
| 161 | |
| 162 | #[test] |
| 163 | fn pkt_line_frames_length() { |
| 164 | |
| 165 | assert_eq!( |
| 166 | pkt_line("# service=git-upload-pack\n"), |
| 167 | "001e# service=git-upload-pack\n" |
| 168 | ); |
| 169 | assert_eq!(pkt_line("a"), "0005a"); |
| 170 | } |
| 171 | |
| 172 | #[tokio::test] |
| 173 | async fn spawn_upload_pack_advertises_refs_on_a_real_repo() { |
| 174 | use std::path::Path; |
| 175 | |
| 176 | use crate::create_bare; |
| 177 | |
| 178 | |
| 179 | if Command::new("git").arg("--version").output().await.is_err() { |
| 180 | eprintln!("skipping: git not on PATH"); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | let dir = tempfile::tempdir().expect("tempdir"); |
| 185 | let id = "01hzxk9m2n8p7q6r5s4t3v2w1x"; |
| 186 | create_bare(dir.path(), id, "main", Path::new("/usr/bin/fabrica")).expect("create bare"); |
| 187 | let repo_path = crate::repo_path(dir.path(), id).expect("repo path"); |
| 188 | |
| 189 | let env = PackEnv { |
| 190 | home: dir.path().to_path_buf(), |
| 191 | ..PackEnv::default() |
| 192 | }; |
| 193 | let child = spawn( |
| 194 | "git", |
| 195 | Service::UploadPack, |
| 196 | &["--stateless-rpc", "--advertise-refs"], |
| 197 | &repo_path, |
| 198 | &env, |
| 199 | ) |
| 200 | .expect("spawn upload-pack"); |
| 201 | let output = child.wait_with_output().await.expect("wait"); |
| 202 | |
| 203 | |
| 204 | |
| 205 | assert!(output.status.success(), "upload-pack should exit cleanly"); |
| 206 | } |
| 207 | } |