| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | | use super::*; |
| 6 | | use crate::subrepo::{Module, Modules, Pin}; |
| 7 | | use std::process::Command; |
| 8 | |
|
| 9 | |
|
| 10 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 11 | | pub enum SubrepoSync { |
| 12 | | Updated, |
| 13 | | UpToDate, |
| 14 | | Missing, |
| 15 | | Dirty, |
| 16 | | |
| 17 | | |
| 18 | | |
| 19 | | KeptBookmark { |
| 20 | | bookmark: String, |
| 21 | | newer: usize, |
| 22 | | }, |
| 23 | | Failed(String), |
| 24 | | } |
| 25 | |
|
| 26 | | impl SubrepoSync { |
| 27 | | |
| 28 | 12× | pub fn note(&self, path: &str) -> Option<String> { |
| 29 | 12× | match self { |
| 30 | 5× | SubrepoSync::Updated | SubrepoSync::UpToDate => None, |
| 31 | 3× | SubrepoSync::Missing => Some(format!( |
| 32 | 3× | "{path}: nested repository not present; run oxy submodule update --init" |
| 33 | 3× | )), |
| 34 | 1× | SubrepoSync::Dirty => Some(format!( |
| 35 | 1× | "{path}: nested repository has uncommitted changes; left as is" |
| 36 | 1× | )), |
| 37 | 3× | SubrepoSync::KeptBookmark { bookmark, newer } => Some(format!( |
| 38 | | "{path}: checked out at the pin; its bookmark {bookmark} keeps {newer} newer change{} (oxy bookmark switch {bookmark} there to return to them)", |
| 39 | 3× | if *newer == 1 { "" } else { "s" } |
| 40 | | )), |
| 41 | 0× | SubrepoSync::Failed(e) => Some(format!("{path}: {e}")), |
| 42 | | } |
| 43 | 12× | } |
| 44 | | } |
| 45 | |
|
| 46 | |
|
| 47 | | #[derive(Clone, Debug)] |
| 48 | | pub struct SubrepoInfo { |
| 49 | | pub path: String, |
| 50 | | pub pin: Pin, |
| 51 | | pub url: Option<String>, |
| 52 | | |
| 53 | | pub actual: Option<Pin>, |
| 54 | | pub dirty: bool, |
| 55 | | |
| 56 | | |
| 57 | | |
| 58 | | pub committed: bool, |
| 59 | | } |
| 60 | |
|
| 61 | 2× | fn git_out(dir: &Path, args: &[&str]) -> Result<String> { |
| 62 | 2× | let out = Command::new("git") |
| 63 | 2× | .arg("-C") |
| 64 | 2× | .arg(dir) |
| 65 | 2× | .args(args) |
| 66 | 2× | .output() |
| 67 | 2× | .map_err(|e| RepoError::Msg(format!("running git: {e}")))?; |
| 68 | 2× | if !out.status.success() { |
| 69 | 0× | return fail(format!( |
| 70 | | "git {} in {}: {}", |
| 71 | 0× | args.join(" "), |
| 72 | 0× | dir.display(), |
| 73 | 0× | String::from_utf8_lossy(&out.stderr).trim() |
| 74 | | )); |
| 75 | 2× | } |
| 76 | 2× | Ok(String::from_utf8_lossy(&out.stdout).trim().to_string()) |
| 77 | 2× | } |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | 80× | pub(crate) fn read_pin(dir: &Path, bookmark_hint: Option<&str>) -> Result<Pin> { |
| 83 | 80× | if dir.join(OXY_DIR).exists() { |
| 84 | 76× | let nested = Repo::open(dir)?; |
| 85 | | return Ok(Pin::Oxy { |
| 86 | 76× | repo: nested.id()?, |
| 87 | 76× | heads: nested.working_copy()?.1.deps, |
| 88 | 76× | bookmark: nested.config.bookmark.clone(), |
| 89 | | }); |
| 90 | 4× | } |
| 91 | 4× | if dir.join(".git").exists() { |
| 92 | 2× | let sha = git_out(dir, &["rev-parse", "HEAD"])?; |
| 93 | 2× | return Ok(Pin::Git { |
| 94 | 2× | sha, |
| 95 | 2× | branch: bookmark_hint.unwrap_or("HEAD").to_string(), |
| 96 | 2× | }); |
| 97 | 2× | } |
| 98 | 2× | fail(format!("{} is not a repository", dir.display())) |
| 99 | 80× | } |
| 100 | |
|
| 101 | |
|
| 102 | 10× | fn nested_dirty(dir: &Path) -> Result<bool> { |
| 103 | 10× | if dir.join(OXY_DIR).exists() { |
| 104 | 10× | let nested = Repo::open(dir)?; |
| 105 | 10× | let snap = nested.snapshot("oxy submodule")?; |
| 106 | 10× | return Ok(!snap.wc.tree_ops.is_empty() || !snap.wc.edits.is_empty()); |
| 107 | 0× | } |
| 108 | 0× | Ok(!git_out(dir, &["status", "--porcelain"])?.is_empty()) |
| 109 | 10× | } |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | 10× | fn probe_oxy_url(url: &str, options: &RemoteOptions) -> std::result::Result<(), String> { |
| 116 | 10× | if url.starts_with("http://") || url.starts_with("https://") { |
| 117 | 0× | return crate::protocol::open_remote(url, options) |
| 118 | 0× | .and_then(|r| r.repo_id()) |
| 119 | 0× | .map(|_| ()) |
| 120 | 0× | .map_err(|e| e.to_string()); |
| 121 | 10× | } |
| 122 | 10× | let p = Path::new(url.strip_prefix("file://").unwrap_or(url)); |
| 123 | 10× | if p.join(OXY_DIR).exists() { |
| 124 | 10× | Ok(()) |
| 125 | | } else { |
| 126 | 0× | Err(format!("{} has no {OXY_DIR} directory", p.display())) |
| 127 | | } |
| 128 | 10× | } |
| 129 | |
|
| 130 | | impl Repo { |
| 131 | 1574× | pub(crate) fn modules_on_disk(&self) -> Modules { |
| 132 | 1574× | fs::read_to_string(self.root.join(".oxymodules")) |
| 133 | 1574× | .map(|t| Modules::parse(&t)) |
| 134 | 1574× | .unwrap_or_default() |
| 135 | 1574× | } |
| 136 | |
|
| 137 | 11× | fn write_modules(&self, m: &Modules) -> Result<()> { |
| 138 | 11× | if m.entries.is_empty() { |
| 139 | 1× | let _ = fs::remove_file(self.root.join(".oxymodules")); |
| 140 | 1× | return Ok(()); |
| 141 | 10× | } |
| 142 | 10× | write_atomic(&self.root.join(".oxymodules"), m.to_text().as_bytes()) |
| 143 | 11× | } |
| 144 | |
|
| 145 | | |
| 146 | | |
| 147 | 17× | pub(crate) fn sync_subrepo(&self, path: &str, pin: &Pin, init: bool) -> Result<SubrepoSync> { |
| 148 | 17× | let dir = self.root.join(path); |
| 149 | 17× | let module = self.modules_on_disk().entries.get(path).cloned(); |
| 150 | 17× | let present = dir.join(OXY_DIR).exists() || dir.join(".git").exists(); |
| 151 | 17× | let cloned = !present; |
| 152 | 17× | if !present { |
| 153 | 6× | if !init { |
| 154 | 4× | return Ok(SubrepoSync::Missing); |
| 155 | 2× | } |
| 156 | 2× | let Some(m) = &module else { |
| 157 | 0× | return Ok(SubrepoSync::Failed( |
| 158 | 0× | "no .oxymodules entry to clone from".into(), |
| 159 | 0× | )); |
| 160 | | }; |
| 161 | 2× | if let Some(parent) = dir.parent() { |
| 162 | 2× | fs::create_dir_all(parent)?; |
| 163 | 0× | } |
| 164 | 2× | let _ = fs::remove_dir(&dir); |
| 165 | 2× | let r = match pin { |
| 166 | 1× | Pin::Oxy { .. } => self.clone_as_subrepo(&m.url, &dir).map(|_| ()), |
| 167 | 1× | Pin::Git { .. } => Command::new("git") |
| 168 | 1× | .args(["clone", "-q", &m.url]) |
| 169 | 1× | .arg(&dir) |
| 170 | 1× | .status() |
| 171 | 1× | .map_err(|e| RepoError::Msg(e.to_string())) |
| 172 | 1× | .and_then(|s| { |
| 173 | 1× | if s.success() { |
| 174 | 1× | Ok(()) |
| 175 | | } else { |
| 176 | 0× | fail(format!("git clone {} failed", m.url)) |
| 177 | | } |
| 178 | 1× | }), |
| 179 | | }; |
| 180 | 2× | if let Err(e) = r { |
| 181 | 0× | return Ok(SubrepoSync::Failed(format!("cloning {}: {e}", m.url))); |
| 182 | 2× | } |
| 183 | 11× | } |
| 184 | 13× | let actual = read_pin(&dir, module.as_ref().and_then(|m| m.bookmark.as_deref()))?; |
| 185 | 13× | let same = match (pin, &actual) { |
| 186 | 12× | (Pin::Oxy { heads: a, .. }, Pin::Oxy { heads: b, .. }) => { |
| 187 | 12× | let (mut a, mut b) = (a.clone(), b.clone()); |
| 188 | 12× | a.sort(); |
| 189 | 12× | b.sort(); |
| 190 | 12× | a == b |
| 191 | | } |
| 192 | 1× | (Pin::Git { sha: a, .. }, Pin::Git { sha: b, .. }) => a == b, |
| 193 | 0× | _ => false, |
| 194 | | }; |
| 195 | 13× | if same { |
| 196 | 8× | return Ok(if cloned { |
| 197 | 2× | SubrepoSync::Updated |
| 198 | | } else { |
| 199 | 6× | SubrepoSync::UpToDate |
| 200 | | }); |
| 201 | 5× | } |
| 202 | 5× | if nested_dirty(&dir)? { |
| 203 | 1× | return Ok(SubrepoSync::Dirty); |
| 204 | 4× | } |
| 205 | 4× | match pin { |
| 206 | 4× | Pin::Oxy { heads, .. } => { |
| 207 | 4× | let nested = Repo::open(&dir)?; |
| 208 | 4× | if nested.load_state(heads).is_err() { |
| 209 | | |
| 210 | 0× | if let Err(e) = nested.fetch("origin") { |
| 211 | 0× | return Ok(SubrepoSync::Failed(format!( |
| 212 | 0× | "fetching the pinned changes: {e}" |
| 213 | 0× | ))); |
| 214 | 0× | } |
| 215 | 4× | } |
| 216 | 4× | match nested.checkout(heads, false, "oxy submodule update") { |
| 217 | | Ok(_) => { |
| 218 | | |
| 219 | | |
| 220 | 4× | let bookmark = nested.config.bookmark.clone(); |
| 221 | 4× | let at = nested.bookmark_heads().unwrap_or_default(); |
| 222 | 4× | let newer = nested.state_gap(&at, heads).map(|g| g.lacks).unwrap_or(0); |
| 223 | 4× | if newer == 0 { |
| 224 | 1× | let _ = nested.bookmark_set(&bookmark, heads); |
| 225 | 1× | Ok(SubrepoSync::Updated) |
| 226 | | } else { |
| 227 | 3× | Ok(SubrepoSync::KeptBookmark { bookmark, newer }) |
| 228 | | } |
| 229 | | } |
| 230 | 0× | Err(e) => Ok(SubrepoSync::Failed(e.to_string())), |
| 231 | | } |
| 232 | | } |
| 233 | 0× | Pin::Git { sha, .. } => { |
| 234 | 0× | if git_out(&dir, &["cat-file", "-e", &format!("{sha}^{{commit}}")]).is_err() { |
| 235 | 0× | if let Err(e) = git_out(&dir, &["fetch", "-q", "origin"]) { |
| 236 | 0× | return Ok(SubrepoSync::Failed(e.to_string())); |
| 237 | 0× | } |
| 238 | 0× | } |
| 239 | 0× | match git_out(&dir, &["checkout", "-q", "--detach", sha]) { |
| 240 | 0× | Ok(_) => Ok(SubrepoSync::Updated), |
| 241 | 0× | Err(e) => Ok(SubrepoSync::Failed(e.to_string())), |
| 242 | | } |
| 243 | | } |
| 244 | | } |
| 245 | 17× | } |
| 246 | |
|
| 247 | | |
| 248 | 29× | pub fn subrepos(&self) -> Result<Vec<SubrepoInfo>> { |
| 249 | 29× | let (_, wc) = self.working_copy()?; |
| 250 | | |
| 251 | | |
| 252 | | |
| 253 | | |
| 254 | 29× | let mut state = self.load_state_indexed(&wc.deps)?; |
| 255 | 29× | let tree = self.tree_view(&mut state)?; |
| 256 | 29× | let modules = self.modules_on_disk(); |
| 257 | 29× | let mut out = Vec::new(); |
| 258 | 49× | for (path, e) in tree.entries.iter() { |
| 259 | 49× | if e.kind != NodeKind::Subrepo { |
| 260 | 42× | continue; |
| 261 | 7× | } |
| 262 | 7× | let Some(b) = state.live_blobs(&e.node).first().copied() else { |
| 263 | 0× | continue; |
| 264 | | }; |
| 265 | 7× | let pin = Pin::decode(&self.read_blob(&b)?)?; |
| 266 | 7× | let dir = self.root.join(path); |
| 267 | 7× | let present = dir.join(OXY_DIR).exists() || dir.join(".git").exists(); |
| 268 | 7× | let actual = if present { |
| 269 | 3× | read_pin( |
| 270 | 3× | &dir, |
| 271 | 3× | modules |
| 272 | 3× | .entries |
| 273 | 3× | .get(path) |
| 274 | 3× | .and_then(|m| m.bookmark.as_deref()), |
| 275 | | ) |
| 276 | 3× | .ok() |
| 277 | | } else { |
| 278 | 4× | None |
| 279 | | }; |
| 280 | 7× | let dirty = present && nested_dirty(&dir).unwrap_or(false); |
| 281 | 7× | out.push(SubrepoInfo { |
| 282 | 7× | path: path.clone(), |
| 283 | 7× | pin, |
| 284 | 7× | url: modules.entries.get(path).map(|m| m.url.clone()), |
| 285 | 7× | actual, |
| 286 | 7× | dirty, |
| 287 | | committed: true, |
| 288 | | }); |
| 289 | | } |
| 290 | | |
| 291 | 29× | for (path, m) in &modules.entries { |
| 292 | 11× | if tree.entries.contains_key(path) { |
| 293 | 7× | continue; |
| 294 | 4× | } |
| 295 | 4× | let dir = self.root.join(path); |
| 296 | 4× | let Ok(pin) = read_pin(&dir, m.bookmark.as_deref()) else { |
| 297 | 2× | continue; |
| 298 | | }; |
| 299 | 2× | out.push(SubrepoInfo { |
| 300 | 2× | path: path.clone(), |
| 301 | 2× | pin: pin.clone(), |
| 302 | 2× | url: Some(m.url.clone()), |
| 303 | 2× | actual: Some(pin), |
| 304 | 2× | dirty: nested_dirty(&dir).unwrap_or(false), |
| 305 | 2× | committed: false, |
| 306 | 2× | }); |
| 307 | | } |
| 308 | 29× | out.sort_by(|a, b| a.path.cmp(&b.path)); |
| 309 | 29× | Ok(out) |
| 310 | 29× | } |
| 311 | |
|
| 312 | | |
| 313 | | |
| 314 | 3× | pub fn unpinned_modules(&self) -> Result<Vec<(String, Module)>> { |
| 315 | 3× | let pinned: Vec<String> = self.subrepos()?.into_iter().map(|s| s.path).collect(); |
| 316 | 3× | Ok(self |
| 317 | 3× | .modules_on_disk() |
| 318 | 3× | .entries |
| 319 | 3× | .into_iter() |
| 320 | 3× | .filter(|(path, _)| !pinned.contains(path)) |
| 321 | 3× | .collect()) |
| 322 | 3× | } |
| 323 | |
|
| 324 | | |
| 325 | | |
| 326 | 10× | pub fn submodule_add(&self, url: &str, path: &str, bookmark: Option<&str>) -> Result<Pin> { |
| 327 | 10× | let path = path.trim_end_matches('/').to_string(); |
| 328 | 10× | if path.is_empty() |
| 329 | 10× | || path.starts_with('/') |
| 330 | 19× | || path.split('/').any(|c| c == ".." || c == ".") |
| 331 | | { |
| 332 | 0× | return fail(format!("{path:?} is not a valid subrepo path")); |
| 333 | 10× | } |
| 334 | 10× | let dir = self.root.join(&path); |
| 335 | 10× | if dir.exists() && fs::read_dir(&dir)?.next().is_some() { |
| 336 | 0× | return fail(format!("{path} exists and is not empty")); |
| 337 | 10× | } |
| 338 | 10× | if let Some(parent) = dir.parent() { |
| 339 | 10× | fs::create_dir_all(parent)?; |
| 340 | 0× | } |
| 341 | 10× | let probe = probe_oxy_url(url, &self.credentials_for(url)); |
| 342 | 10× | let git = probe.is_err(); |
| 343 | 10× | if git { |
| 344 | 0× | let mut cmd = Command::new("git"); |
| 345 | 0× | cmd.args(["clone", "-q"]); |
| 346 | 0× | if let Some(b) = bookmark { |
| 347 | 0× | cmd.args(["--branch", b]); |
| 348 | 0× | } |
| 349 | 0× | let status = cmd |
| 350 | 0× | .arg(url) |
| 351 | 0× | .arg(&dir) |
| 352 | 0× | .status() |
| 353 | 0× | .map_err(|e| RepoError::Msg(format!("running git: {e}")))?; |
| 354 | 0× | if !status.success() { |
| 355 | 0× | let oxy_error = probe.err().unwrap_or_default(); |
| 356 | 0× | return fail(format!( |
| 357 | | "git clone {url} failed; as an Oxygen repository: {oxy_error}" |
| 358 | | )); |
| 359 | 0× | } |
| 360 | | } else { |
| 361 | 10× | let _ = fs::remove_dir(&dir); |
| 362 | 10× | let mut nested = self.clone_as_subrepo(url, &dir)?; |
| 363 | 10× | if let Some(b) = bookmark { |
| 364 | 0× | nested.bookmark_switch(b, true)?; |
| 365 | 10× | } |
| 366 | | } |
| 367 | 10× | let mut modules = self.modules_on_disk(); |
| 368 | 10× | modules.entries.insert( |
| 369 | 10× | path.clone(), |
| 370 | 10× | Module { |
| 371 | 10× | url: url.to_string(), |
| 372 | 10× | bookmark: bookmark.map(str::to_string), |
| 373 | 10× | git, |
| 374 | 10× | }, |
| 375 | | ); |
| 376 | 10× | self.write_modules(&modules)?; |
| 377 | 10× | read_pin(&dir, bookmark) |
| 378 | 10× | } |
| 379 | |
|
| 380 | | |
| 381 | 5× | pub fn submodule_update( |
| 382 | 5× | &self, |
| 383 | 5× | init: bool, |
| 384 | 5× | paths: &[String], |
| 385 | 5× | ) -> Result<Vec<(String, SubrepoSync)>> { |
| 386 | 5× | let mut out = Vec::new(); |
| 387 | 5× | for s in self.subrepos()? { |
| 388 | 4× | if !paths.is_empty() && !paths.iter().any(|p| p.trim_end_matches('/') == s.path) { |
| 389 | 0× | continue; |
| 390 | 4× | } |
| 391 | 4× | let r = self.sync_subrepo(&s.path, &s.pin, init)?; |
| 392 | 4× | out.push((s.path, r)); |
| 393 | | } |
| 394 | 5× | Ok(out) |
| 395 | 5× | } |
| 396 | |
|
| 397 | | |
| 398 | | |
| 399 | | |
| 400 | | |
| 401 | | |
| 402 | 1× | pub fn submodule_remove(&self, path: &str) -> Result<()> { |
| 403 | 1× | let path = path.trim_end_matches('/').to_string(); |
| 404 | 1× | let mut modules = self.modules_on_disk(); |
| 405 | 1× | let known = modules.entries.remove(&path).is_some() |
| 406 | 0× | || self.subrepos()?.iter().any(|s| s.path == path); |
| 407 | 1× | if !known { |
| 408 | 0× | return fail(format!("{path} is not a subrepo")); |
| 409 | 1× | } |
| 410 | 1× | self.write_modules(&modules)?; |
| 411 | | |
| 412 | | |
| 413 | | |
| 414 | 1× | let dir = self.oxy.join("subrepos-removed"); |
| 415 | 1× | fs::create_dir_all(&dir)?; |
| 416 | 1× | write_atomic(&dir.join(path.replace('/', "%2F")), b"")?; |
| 417 | 1× | Ok(()) |
| 418 | 1× | } |
| 419 | |
|
| 420 | | |
| 421 | | |
| 422 | 1493× | pub(crate) fn removed_subrepos(&self) -> BTreeSet<String> { |
| 423 | 1493× | let mut out = BTreeSet::new(); |
| 424 | 1493× | if let Ok(rd) = fs::read_dir(self.oxy.join("subrepos-removed")) { |
| 425 | 3× | for e in rd.flatten() { |
| 426 | 3× | out.insert(e.file_name().to_string_lossy().replace("%2F", "/")); |
| 427 | 3× | } |
| 428 | 1490× | } |
| 429 | 1493× | out |
| 430 | 1493× | } |
| 431 | |
|
| 432 | 1× | pub(crate) fn clear_removed_subrepo(&self, path: &str) { |
| 433 | 1× | let _ = fs::remove_file( |
| 434 | 1× | self.oxy |
| 435 | 1× | .join("subrepos-removed") |
| 436 | 1× | .join(path.replace('/', "%2F")), |
| 437 | 1× | ); |
| 438 | 1× | } |
| 439 | | } |