| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | pub mod archive; |
| 15 | pub mod import; |
| 16 | pub mod merge; |
| 17 | pub mod protocol; |
| 18 | pub mod storage; |
| 19 | |
| 20 | #[cfg(test)] |
| 21 | pub(crate) mod fixture; |
| 22 | |
| 23 | pub use archive::{DiskGitArchive, InMemoryGitArchive, RecordedArchive}; |
| 24 | pub use import::{DiskGitImport, InMemoryGitImport}; |
| 25 | pub use merge::{DiskGitMerge, GitFeatures, InMemoryGitMerge, RecordedRefUpdate}; |
| 26 | pub use protocol::{GitHttpBackend, InMemoryGitProtocol, RecordedGitRequest}; |
| 27 | pub use storage::{DiskGitStorage, InMemoryGitStorage}; |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | pub use super::git_query::InMemoryGitQuery; |
| 34 | |
| 35 | use std::{ |
| 36 | ffi::OsStr, |
| 37 | process::{Output, Stdio}, |
| 38 | }; |
| 39 | |
| 40 | use tokio::process::Command; |
| 41 | |
| 42 | use crate::{application::port::GitStorageError, infrastructure::metrics::metrics}; |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | const REDIRECTING_VARS: &[&str] = &[ |
| 51 | "GIT_ALTERNATE_OBJECT_DIRECTORIES", |
| 52 | "GIT_DIR", |
| 53 | "GIT_INDEX_FILE", |
| 54 | "GIT_OBJECT_DIRECTORY", |
| 55 | "GIT_WORK_TREE", |
| 56 | ]; |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | pub(crate) fn git_command() -> Command { |
| 65 | let mut command = Command::new("git"); |
| 66 | |
| 67 | |
| 68 | |
| 69 | command |
| 70 | .env("GIT_CONFIG_GLOBAL", "/dev/null") |
| 71 | .env("GIT_CONFIG_SYSTEM", "/dev/null"); |
| 72 | |
| 73 | for variable in REDIRECTING_VARS { |
| 74 | command.env_remove(variable); |
| 75 | } |
| 76 | |
| 77 | command |
| 78 | } |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | pub(crate) fn count_spawn(command: &Command) { |
| 91 | metrics().record_git(subcommand_of(command.as_std().get_args())); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | fn subcommand_of<'a>(args: impl Iterator<Item = &'a OsStr>) -> &'a str { |
| 101 | let mut args = args.peekable(); |
| 102 | |
| 103 | while let Some(arg) = args.peek().and_then(|arg| arg.to_str()) { |
| 104 | if !matches!(arg, "-C" | "--git-dir" | "-c") { |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | args.next(); |
| 110 | args.next(); |
| 111 | } |
| 112 | |
| 113 | args.next() |
| 114 | .and_then(OsStr::to_str) |
| 115 | .map(|arg| arg.trim_start_matches('-')) |
| 116 | .unwrap_or("other") |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | async fn run_git<I, S>(args: I) -> Result<Output, GitStorageError> |
| 126 | where |
| 127 | I: IntoIterator<Item = S>, |
| 128 | S: AsRef<OsStr>, |
| 129 | { |
| 130 | let mut command = git_command(); |
| 131 | command.args(args).stdin(Stdio::null()); |
| 132 | count_spawn(&command); |
| 133 | |
| 134 | |
| 135 | let output = command |
| 136 | .output() |
| 137 | .await |
| 138 | .map_err(|error| GitStorageError::backend(format!("could not run git: {error}")))?; |
| 139 | |
| 140 | if !output.status.success() { |
| 141 | |
| 142 | |
| 143 | return Err(GitStorageError::backend(format!( |
| 144 | "git exited with {}: {}", |
| 145 | output.status, |
| 146 | String::from_utf8_lossy(&output.stderr).trim() |
| 147 | ))); |
| 148 | } |
| 149 | |
| 150 | Ok(output) |
| 151 | } |
| 152 | |
| 153 | #[cfg(test)] |
| 154 | mod tests { |
| 155 | use super::*; |
| 156 | use crate::application::port::GitStorage; |
| 157 | use crate::infrastructure::git::fixture::{handle, repo_name, storage}; |
| 158 | |
| 159 | |
| 160 | |
| 161 | #[test] |
| 162 | fn the_subcommand_is_read_past_the_global_flags_steid_passes() { |
| 163 | let of = |args: &[&str]| { |
| 164 | let mut command = git_command(); |
| 165 | command.args(args); |
| 166 | subcommand_of(command.as_std().get_args()).to_owned() |
| 167 | }; |
| 168 | |
| 169 | assert_eq!(of(&["-C", "/srv/x.git", "log", "-n", "1"]), "log"); |
| 170 | assert_eq!( |
| 171 | of(&["--git-dir", "/srv/x.git", "config", "--get", "k"]), |
| 172 | "config" |
| 173 | ); |
| 174 | assert_eq!( |
| 175 | of(&["-c", "http.receivepack=true", "http-backend"]), |
| 176 | "http-backend" |
| 177 | ); |
| 178 | assert_eq!(of(&["-C", "/srv/x.git", "cat-file", "--batch"]), "cat-file"); |
| 179 | |
| 180 | assert_eq!(of(&["--version"]), "version"); |
| 181 | |
| 182 | |
| 183 | assert_eq!(of(&[]), "other"); |
| 184 | } |
| 185 | |
| 186 | #[tokio::test] |
| 187 | async fn running_git_counts_one_process_under_its_own_subcommand() { |
| 188 | |
| 189 | |
| 190 | let metrics = crate::infrastructure::metrics::metrics(); |
| 191 | let series = "steid_git_processes_total{command=\"init\"}"; |
| 192 | let before = metrics.value_of(series); |
| 193 | |
| 194 | let (_dir, storage) = storage(); |
| 195 | storage |
| 196 | .init_bare(&handle(), &repo_name("counted")) |
| 197 | .await |
| 198 | .expect("the repository should be created"); |
| 199 | |
| 200 | assert!( |
| 201 | metrics.value_of(series) > before, |
| 202 | "creating a repository runs git init, which must be counted" |
| 203 | ); |
| 204 | } |
| 205 | } |