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