|
| 1 | +use std::{env, fs, path::Path}; |
| 2 | + |
| 3 | +use anyhow::{Context as _, Result}; |
| 4 | +use async_process::Command; |
| 5 | + |
| 6 | +const DATA_ROOT: &str = "dimbreath"; |
| 7 | +const GITHUB_DATA_PAT_ENV: &str = "GITHUB_DATA_PAT"; |
| 8 | + |
| 9 | +/// Syncs a cached data repo and returns true when downstream import work should rerun. |
| 10 | +pub async fn sync_data_repo(repo_url: &str, data_dir: &str) -> Result<bool> { |
| 11 | + fs::create_dir_all(DATA_ROOT)?; |
| 12 | + |
| 13 | + let data_path = Path::new(DATA_ROOT).join(data_dir); |
| 14 | + let remote_url = remote_url(repo_url); |
| 15 | + let mut changed = false; |
| 16 | + |
| 17 | + if data_path.exists() { |
| 18 | + match git_output(&["remote", "get-url", "origin"], &data_path).await { |
| 19 | + Ok(output) if strip_auth(output.trim()) == repo_url => {} |
| 20 | + _ => { |
| 21 | + // Existing servers may have older upstream clones cached under the same data path. |
| 22 | + fs::remove_dir_all(&data_path)?; |
| 23 | + changed = true; |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + if !data_path.exists() { |
| 29 | + git_output( |
| 30 | + &["clone", "--depth", "1", &remote_url, data_dir], |
| 31 | + Path::new(DATA_ROOT), |
| 32 | + ) |
| 33 | + .await?; |
| 34 | + changed = true; |
| 35 | + } else { |
| 36 | + // Store the PAT-backed remote when available so manual `git pull` works in the cache. |
| 37 | + git_output(&["remote", "set-url", "origin", &remote_url], &data_path).await?; |
| 38 | + } |
| 39 | + |
| 40 | + let output = git_output(&["pull"], &data_path).await?; |
| 41 | + if !output.contains("Already up to date.") { |
| 42 | + changed = true; |
| 43 | + } |
| 44 | + |
| 45 | + Ok(changed) |
| 46 | +} |
| 47 | + |
| 48 | +async fn git_output(args: &[&str], current_dir: &Path) -> Result<String> { |
| 49 | + let output = git_command() |
| 50 | + .args(args) |
| 51 | + .current_dir(current_dir) |
| 52 | + .output() |
| 53 | + .await |
| 54 | + .with_context(|| { |
| 55 | + format!( |
| 56 | + "failed to start git {} in {}", |
| 57 | + redact(args.join(" ")), |
| 58 | + current_dir.display() |
| 59 | + ) |
| 60 | + })?; |
| 61 | + |
| 62 | + if !output.status.success() { |
| 63 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 64 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 65 | + let details = [stderr.trim(), stdout.trim()] |
| 66 | + .into_iter() |
| 67 | + .filter(|part| !part.is_empty()) |
| 68 | + .collect::<Vec<_>>() |
| 69 | + .join("\n"); |
| 70 | + let details = redact(details); |
| 71 | + let args = redact(args.join(" ")); |
| 72 | + |
| 73 | + if details.is_empty() { |
| 74 | + anyhow::bail!("git {} failed with {}", args, output.status); |
| 75 | + } |
| 76 | + |
| 77 | + anyhow::bail!("git {} failed with {}: {}", args, output.status, details); |
| 78 | + } |
| 79 | + |
| 80 | + Ok(String::from_utf8(output.stdout)?) |
| 81 | +} |
| 82 | + |
| 83 | +fn git_command() -> Command { |
| 84 | + Command::new("git") |
| 85 | +} |
| 86 | + |
| 87 | +fn remote_url(repo_url: &str) -> String { |
| 88 | + if let Ok(token) = env::var(GITHUB_DATA_PAT_ENV) { |
| 89 | + let token = token.trim(); |
| 90 | + |
| 91 | + if !token.is_empty() { |
| 92 | + return repo_url.replacen("https://", &format!("https://x-access-token:{token}@"), 1); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + repo_url.to_string() |
| 97 | +} |
| 98 | + |
| 99 | +fn strip_auth(repo_url: &str) -> String { |
| 100 | + let Some(rest) = repo_url.strip_prefix("https://") else { |
| 101 | + return repo_url.to_string(); |
| 102 | + }; |
| 103 | + |
| 104 | + match rest.split_once('@') { |
| 105 | + Some((_, host_and_path)) => format!("https://{host_and_path}"), |
| 106 | + None => repo_url.to_string(), |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +fn redact(value: String) -> String { |
| 111 | + let Ok(token) = env::var(GITHUB_DATA_PAT_ENV) else { |
| 112 | + return value; |
| 113 | + }; |
| 114 | + let token = token.trim(); |
| 115 | + |
| 116 | + if token.is_empty() { |
| 117 | + value |
| 118 | + } else { |
| 119 | + value.replace(token, "[redacted]") |
| 120 | + } |
| 121 | +} |
0 commit comments