| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | | use super::*; |
| 11 | | use std::process::{Command, Stdio}; |
| 12 | | use std::sync::Arc; |
| 13 | |
|
| 14 | | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 15 | | pub struct GitImportReport { |
| 16 | | pub commits: usize, |
| 17 | | pub merges: usize, |
| 18 | | |
| 19 | | pub submodules: usize, |
| 20 | | pub heads: usize, |
| 21 | | } |
| 22 | |
|
| 23 | |
|
| 24 | | struct CatFile { |
| 25 | | child: std::process::Child, |
| 26 | | stdin: std::process::ChildStdin, |
| 27 | | stdout: std::io::BufReader<std::process::ChildStdout>, |
| 28 | | } |
| 29 | |
|
| 30 | | impl CatFile { |
| 31 | 26× | fn start(git_dir: &Path) -> Result<CatFile> { |
| 32 | 26× | let mut child = Command::new("git") |
| 33 | 26× | .arg("-C") |
| 34 | 26× | .arg(git_dir) |
| 35 | 26× | .args(["cat-file", "--batch"]) |
| 36 | 26× | .stdin(Stdio::piped()) |
| 37 | 26× | .stdout(Stdio::piped()) |
| 38 | 26× | .stderr(Stdio::null()) |
| 39 | 26× | .spawn() |
| 40 | 26× | .map_err(|e| RepoError::Msg(format!("running git cat-file: {e}")))?; |
| 41 | 26× | let stdin = child.stdin.take().unwrap(); |
| 42 | 26× | let stdout = std::io::BufReader::new(child.stdout.take().unwrap()); |
| 43 | 26× | Ok(CatFile { |
| 44 | 26× | child, |
| 45 | 26× | stdin, |
| 46 | 26× | stdout, |
| 47 | 26× | }) |
| 48 | 26× | } |
| 49 | |
|
| 50 | 131× | fn read(&mut self, sha: &str) -> Result<Vec<u8>> { |
| 51 | | use std::io::{BufRead, Read}; |
| 52 | 131× | writeln!(self.stdin, "{sha}")?; |
| 53 | 131× | self.stdin.flush()?; |
| 54 | 131× | let mut header = String::new(); |
| 55 | 131× | self.stdout.read_line(&mut header)?; |
| 56 | 131× | let parts: Vec<&str> = header.trim_end().split(' ').collect(); |
| 57 | 131× | if parts.len() != 3 { |
| 58 | 0× | return fail(format!("git cat-file: object {sha} not found")); |
| 59 | 131× | } |
| 60 | 131× | let size: usize = parts[2] |
| 61 | 131× | .parse() |
| 62 | 131× | .map_err(|_| RepoError::Msg("git cat-file: bad size".into()))?; |
| 63 | 131× | let mut body = vec![0u8; size]; |
| 64 | 131× | self.stdout.read_exact(&mut body)?; |
| 65 | 131× | let mut nl = [0u8; 1]; |
| 66 | 131× | self.stdout.read_exact(&mut nl)?; |
| 67 | 131× | Ok(body) |
| 68 | 131× | } |
| 69 | | } |
| 70 | |
|
| 71 | | impl Drop for CatFile { |
| 72 | 26× | fn drop(&mut self) { |
| 73 | 26× | let _ = self.child.kill(); |
| 74 | 26× | let _ = self.child.wait(); |
| 75 | 26× | } |
| 76 | | } |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | | type TreeDiff = Vec<(String, Option<(String, String)>)>; |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | | struct DiffTree { |
| 87 | | child: std::process::Child, |
| 88 | | stdout: std::io::BufReader<std::process::ChildStdout>, |
| 89 | | feeder: Option<std::thread::JoinHandle<()>>, |
| 90 | | |
| 91 | | next: Option<String>, |
| 92 | | } |
| 93 | |
|
| 94 | | impl DiffTree { |
| 95 | 16× | fn start(git_dir: &Path, pairs: Vec<(String, String)>) -> Result<DiffTree> { |
| 96 | 16× | let mut child = Command::new("git") |
| 97 | 16× | .arg("-C") |
| 98 | 16× | .arg(git_dir) |
| 99 | 16× | .args([ |
| 100 | 16× | "diff-tree", |
| 101 | 16× | "--stdin", |
| 102 | 16× | "--always", |
| 103 | 16× | "-r", |
| 104 | 16× | "-z", |
| 105 | 16× | "--no-renames", |
| 106 | 16× | ]) |
| 107 | 16× | .stdin(Stdio::piped()) |
| 108 | 16× | .stdout(Stdio::piped()) |
| 109 | 16× | .stderr(Stdio::null()) |
| 110 | 16× | .spawn() |
| 111 | 16× | .map_err(|e| RepoError::Msg(format!("running git diff-tree: {e}")))?; |
| 112 | 16× | let mut stdin = child.stdin.take().unwrap(); |
| 113 | 16× | let stdout = std::io::BufReader::new(child.stdout.take().unwrap()); |
| 114 | | |
| 115 | 16× | let feeder = std::thread::spawn(move || { |
| 116 | | |
| 117 | | |
| 118 | | |
| 119 | | |
| 120 | | |
| 121 | | #[cfg(unix)] |
| 122 | 16× | unsafe { |
| 123 | 16× | let mut set: libc::sigset_t = std::mem::zeroed(); |
| 124 | 16× | libc::sigemptyset(&mut set); |
| 125 | 16× | libc::sigaddset(&mut set, libc::SIGPIPE); |
| 126 | 16× | libc::pthread_sigmask(libc::SIG_BLOCK, &set, std::ptr::null_mut()); |
| 127 | 16× | } |
| 128 | 16× | let mut w = std::io::BufWriter::new(&mut stdin); |
| 129 | 72× | for (c, p) in pairs { |
| 130 | 72× | if writeln!(w, "{c} {p}").is_err() { |
| 131 | 0× | return; |
| 132 | 72× | } |
| 133 | | } |
| 134 | 16× | let _ = w.flush(); |
| 135 | 16× | }); |
| 136 | 16× | Ok(DiffTree { |
| 137 | 16× | child, |
| 138 | 16× | stdout, |
| 139 | 16× | feeder: Some(feeder), |
| 140 | 16× | next: None, |
| 141 | 16× | }) |
| 142 | 16× | } |
| 143 | |
|
| 144 | 267× | fn field(&mut self) -> Result<Option<String>> { |
| 145 | | use std::io::BufRead; |
| 146 | 267× | let mut buf = Vec::new(); |
| 147 | 267× | if self.stdout.read_until(0, &mut buf)? == 0 { |
| 148 | 15× | return Ok(None); |
| 149 | 252× | } |
| 150 | 252× | if buf.last() == Some(&0) { |
| 151 | 252× | buf.pop(); |
| 152 | 252× | } |
| 153 | 252× | Ok(Some(String::from_utf8_lossy(&buf).into_owned())) |
| 154 | 267× | } |
| 155 | |
|
| 156 | | |
| 157 | 72× | fn diff(&mut self, sha: &str) -> Result<TreeDiff> { |
| 158 | 72× | let header = match self.next.take() { |
| 159 | 57× | Some(h) => Some(h), |
| 160 | 15× | None => self.field()?, |
| 161 | | }; |
| 162 | 72× | if header.as_deref() != Some(sha) { |
| 163 | 0× | return fail(format!("git diff-tree: expected {sha}, got {header:?}")); |
| 164 | 72× | } |
| 165 | 72× | let mut out = Vec::new(); |
| 166 | 162× | while let Some(f) = self.field()? { |
| 167 | 147× | let Some(meta) = f.strip_prefix(':') else { |
| 168 | 57× | self.next = Some(f); |
| 169 | 57× | break; |
| 170 | | }; |
| 171 | 90× | let Some(path) = self.field()? else { |
| 172 | 0× | return fail("git diff-tree: record without a path"); |
| 173 | | }; |
| 174 | | |
| 175 | 90× | let m: Vec<&str> = meta.split(' ').collect(); |
| 176 | 90× | if m.len() != 5 { |
| 177 | 0× | return fail(format!("git diff-tree: bad record {f:?}")); |
| 178 | 90× | } |
| 179 | 90× | let entry = if m[4].starts_with('D') { |
| 180 | 18× | None |
| 181 | | } else { |
| 182 | 72× | Some((m[1].to_string(), m[3].to_string())) |
| 183 | | }; |
| 184 | 90× | out.push((path, entry)); |
| 185 | | } |
| 186 | 72× | Ok(out) |
| 187 | 72× | } |
| 188 | | } |
| 189 | |
|
| 190 | | impl Drop for DiffTree { |
| 191 | 16× | fn drop(&mut self) { |
| 192 | 16× | let _ = self.child.kill(); |
| 193 | 16× | let _ = self.child.wait(); |
| 194 | 16× | if let Some(f) = self.feeder.take() { |
| 195 | 16× | let _ = f.join(); |
| 196 | 16× | } |
| 197 | 16× | } |
| 198 | | } |
| 199 | |
|
| 200 | |
|
| 201 | 2× | fn git_email(author: &str) -> Option<String> { |
| 202 | 2× | let (_, rest) = author.rsplit_once('<')?; |
| 203 | 2× | let email = rest.strip_suffix('>')?.trim(); |
| 204 | 2× | (!email.is_empty()).then(|| email.to_lowercase()) |
| 205 | 2× | } |
| 206 | |
|
| 207 | |
|
| 208 | | struct GitCommit { |
| 209 | | sha: String, |
| 210 | | parents: Vec<String>, |
| 211 | | author_secs: u64, |
| 212 | | author: String, |
| 213 | | message: String, |
| 214 | | } |
| 215 | |
|
| 216 | 26× | fn list_commits(git_dir: &Path, branch: &str) -> Result<Vec<GitCommit>> { |
| 217 | 26× | let out = Command::new("git") |
| 218 | 26× | .arg("-C") |
| 219 | 26× | .arg(git_dir) |
| 220 | 26× | .args([ |
| 221 | 26× | "log", |
| 222 | 26× | "--reverse", |
| 223 | 26× | "--topo-order", |
| 224 | 26× | "--format=%H%x00%P%x00%at%x00%an <%ae>%x00%B%x1e", |
| 225 | 26× | branch, |
| 226 | 26× | ]) |
| 227 | 26× | .output() |
| 228 | 26× | .map_err(|e| RepoError::Msg(format!("running git log: {e}")))?; |
| 229 | 26× | if !out.status.success() { |
| 230 | 0× | return fail(format!( |
| 231 | | "git log {branch} failed: {}", |
| 232 | 0× | String::from_utf8_lossy(&out.stderr).trim() |
| 233 | | )); |
| 234 | 26× | } |
| 235 | 26× | let text = String::from_utf8_lossy(&out.stdout); |
| 236 | 26× | let mut commits = Vec::new(); |
| 237 | 182× | for record in text.split('\x1e') { |
| 238 | 182× | let record = record.trim_start_matches('\n'); |
| 239 | 182× | if record.trim().is_empty() { |
| 240 | 26× | continue; |
| 241 | 156× | } |
| 242 | 156× | let f: Vec<&str> = record.splitn(5, '\0').collect(); |
| 243 | 156× | if f.len() != 5 { |
| 244 | 0× | return fail("unexpected git log record"); |
| 245 | 156× | } |
| 246 | 156× | commits.push(GitCommit { |
| 247 | 156× | sha: f[0].to_string(), |
| 248 | 156× | parents: f[1].split_whitespace().map(str::to_string).collect(), |
| 249 | 156× | author_secs: f[2].parse().unwrap_or(0), |
| 250 | 156× | author: f[3].to_string(), |
| 251 | 156× | message: f[4].trim_end().to_string(), |
| 252 | 156× | }); |
| 253 | | } |
| 254 | 26× | Ok(commits) |
| 255 | 26× | } |
| 256 | |
|
| 257 | |
|
| 258 | 80× | fn list_tree(git_dir: &Path, sha: &str) -> Result<BTreeMap<String, (String, String)>> { |
| 259 | 80× | let text = git(git_dir, &["ls-tree", "-r", "-z", sha])?; |
| 260 | 80× | let mut out = BTreeMap::new(); |
| 261 | 256× | for entry in text.split('\0') { |
| 262 | 256× | if entry.is_empty() { |
| 263 | 80× | continue; |
| 264 | 176× | } |
| 265 | 176× | let Some((meta, path)) = entry.split_once('\t') else { |
| 266 | 0× | continue; |
| 267 | | }; |
| 268 | 176× | let m: Vec<&str> = meta.split(' ').collect(); |
| 269 | 176× | if m.len() == 3 { |
| 270 | 176× | out.insert(path.to_string(), (m[0].to_string(), m[2].to_string())); |
| 271 | 176× | } |
| 272 | | } |
| 273 | 80× | Ok(out) |
| 274 | 80× | } |
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | 88× | fn change_id_for(sha: &str, message: &str) -> ChangeId { |
| 279 | 88× | for line in message.lines().rev() { |
| 280 | 88× | if let Some(v) = line.strip_prefix("Change-Id:") { |
| 281 | 4× | if let Some(id) = ChangeId::from_hex(v.trim()) { |
| 282 | 4× | return id; |
| 283 | 0× | } |
| 284 | 84× | } |
| 285 | | } |
| 286 | 84× | let h = blake3::hash(sha.as_bytes()); |
| 287 | 84× | ChangeId(h.as_bytes()[..16].try_into().unwrap()) |
| 288 | 88× | } |
| 289 | |
|
| 290 | |
|
| 291 | | struct GitKnown { |
| 292 | | |
| 293 | | imported: HashMap<String, ChangeHash>, |
| 294 | | |
| 295 | | exported: HashMap<String, ObjectRef>, |
| 296 | | } |
| 297 | |
|
| 298 | | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 299 | | pub struct GitExportReport { |
| 300 | | pub exported: usize, |
| 301 | | pub skipped: usize, |
| 302 | | |
| 303 | | pub skipped_subrepos: usize, |
| 304 | | pub branch: String, |
| 305 | | pub heads: usize, |
| 306 | | } |
| 307 | |
|
| 308 | |
|
| 309 | | const MERGE_SHED_COMMITS: usize = 100; |
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | | const KEPT_ATOMS: usize = 1_500_000; |
| 314 | |
|
| 315 | | const KEPT_GRAPHS: usize = 2000; |
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | | const CHECKPOINT_COMMITS: usize = 2000; |
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | | const MERGE_BATCH: usize = 500; |
| 326 | |
|
| 327 | | thread_local! { |
| 328 | | |
| 329 | | |
| 330 | | static MERGE_BATCH_OVERRIDE: std::cell::Cell<Option<usize>> = const { std::cell::Cell::new(None) }; |
| 331 | | } |
| 332 | |
|
| 333 | | #[cfg(test)] |
| 334 | 4× | pub(crate) fn set_merge_batch_for_tests(n: Option<usize>) { |
| 335 | 4× | MERGE_BATCH_OVERRIDE.with(|c| c.set(n)); |
| 336 | 4× | } |
| 337 | |
|
| 338 | 29× | fn merge_batch() -> usize { |
| 339 | 29× | MERGE_BATCH_OVERRIDE |
| 340 | 29× | .with(|c| c.get()) |
| 341 | 29× | .filter(|n| *n > 0) |
| 342 | 29× | .or_else(|| { |
| 343 | 13× | std::env::var("OXY_MERGE_BATCH") |
| 344 | 13× | .ok() |
| 345 | 13× | .and_then(|v| v.parse().ok()) |
| 346 | 13× | }) |
| 347 | 29× | .unwrap_or(MERGE_BATCH) |
| 348 | 29× | } |
| 349 | |
|
| 350 | | impl Repo { |
| 351 | | |
| 352 | | |
| 353 | | |
| 354 | | |
| 355 | 5× | fn materialize_batched( |
| 356 | 5× | &self, |
| 357 | 5× | base: &mut State, |
| 358 | 5× | need: &[crate::ids::NodeId], |
| 359 | 5× | skip: &HashSet<crate::ids::NodeId>, |
| 360 | 5× | ) -> Result<crate::state::Snapshot> { |
| 361 | | use crate::graph::Materialized; |
| 362 | | use crate::state::{FileContent, FileState}; |
| 363 | | use rayon::prelude::*; |
| 364 | 5× | let view = base.tree().view(); |
| 365 | 5× | let mut rendered: HashMap<crate::ids::NodeId, (Vec<u8>, Materialized)> = |
| 366 | 5× | HashMap::with_capacity(need.len()); |
| 367 | 5× | let mut keep_built: Vec<crate::ids::NodeId> = Vec::new(); |
| 368 | 14× | for batch in need.chunks(merge_batch()) { |
| 369 | 14× | let t = std::time::Instant::now(); |
| 370 | 14× | let graphs = self.build_graphs_detached(base, batch)?; |
| 371 | 14× | crate::timing::add("render: build graphs, batch", t.elapsed()); |
| 372 | 14× | let t = std::time::Instant::now(); |
| 373 | 14× | let batch_rendered: Vec<(crate::ids::NodeId, Vec<u8>, Materialized)> = graphs |
| 374 | 14× | .par_iter() |
| 375 | 14× | .map(|(node, g)| { |
| 376 | 14× | let (bytes, m) = base.render_graph(g); |
| 377 | 14× | (*node, bytes, m) |
| 378 | 14× | }) |
| 379 | 14× | .collect(); |
| 380 | 14× | drop(graphs); |
| 381 | 14× | crate::timing::add("render: render, batch", t.elapsed()); |
| 382 | 14× | for (node, bytes, m) in batch_rendered { |
| 383 | 14× | if m.is_conflicted() |
| 384 | 14× | || m.live_atoms().len() != crate::diff::split_lines(&bytes).len() |
| 385 | 0× | { |
| 386 | 0× | keep_built.push(node); |
| 387 | 14× | } |
| 388 | 14× | rendered.insert(node, (bytes, m)); |
| 389 | | } |
| 390 | | } |
| 391 | 5× | if !keep_built.is_empty() { |
| 392 | 0× | self.load_graphs_of(base, &keep_built)?; |
| 393 | 5× | } |
| 394 | 5× | let mut files = BTreeMap::new(); |
| 395 | 5× | let mut unrendered = BTreeSet::new(); |
| 396 | 16× | for (path, e) in view.entries.iter() { |
| 397 | 16× | let f = if e.kind == NodeKind::File && !e.mode.opaque { |
| 398 | 16× | match rendered.remove(&e.node) { |
| 399 | 14× | Some((bytes, materialized)) => FileState { |
| 400 | 14× | node: e.node, |
| 401 | 14× | kind: e.kind, |
| 402 | 14× | mode: e.mode, |
| 403 | 14× | content: FileContent::Text { |
| 404 | 14× | bytes, |
| 405 | 14× | materialized, |
| 406 | 14× | }, |
| 407 | 14× | }, |
| 408 | 2× | None if skip.contains(&e.node) => { |
| 409 | 2× | unrendered.insert(path.clone()); |
| 410 | 2× | FileState { |
| 411 | 2× | node: e.node, |
| 412 | 2× | kind: e.kind, |
| 413 | 2× | mode: e.mode, |
| 414 | 2× | content: FileContent::Text { |
| 415 | 2× | bytes: Vec::new(), |
| 416 | 2× | materialized: Materialized::default(), |
| 417 | 2× | }, |
| 418 | 2× | } |
| 419 | | } |
| 420 | | None => { |
| 421 | | |
| 422 | | |
| 423 | 0× | self.load_graphs_of(base, &[e.node])?; |
| 424 | 0× | base.entry_state(e) |
| 425 | | } |
| 426 | | } |
| 427 | | } else { |
| 428 | 0× | base.entry_state(e) |
| 429 | | }; |
| 430 | 16× | files.insert(path.clone(), f); |
| 431 | | } |
| 432 | 5× | Ok(crate::state::Snapshot { |
| 433 | 5× | tree: view, |
| 434 | 5× | files, |
| 435 | 5× | unrendered, |
| 436 | 5× | }) |
| 437 | 5× | } |
| 438 | | } |
| 439 | |
|
| 440 | 177× | fn git(dir: &Path, args: &[&str]) -> Result<String> { |
| 441 | 177× | let out = Command::new("git") |
| 442 | 177× | .arg("-C") |
| 443 | 177× | .arg(dir) |
| 444 | 177× | .args(args) |
| 445 | 177× | .output() |
| 446 | 177× | .map_err(|e| RepoError::Msg(format!("running git: {e}")))?; |
| 447 | 177× | if !out.status.success() { |
| 448 | 0× | return fail(format!( |
| 449 | | "git {} failed: {}", |
| 450 | 0× | args.join(" "), |
| 451 | 0× | String::from_utf8_lossy(&out.stderr).trim() |
| 452 | | )); |
| 453 | 177× | } |
| 454 | 177× | Ok(String::from_utf8_lossy(&out.stdout).trim().to_string()) |
| 455 | 177× | } |
| 456 | |
|
| 457 | | impl Repo { |
| 458 | 50× | fn git_marks_dir(&self, git_dir: &Path) -> Result<PathBuf> { |
| 459 | 50× | let canonical = |
| 460 | 50× | crate::portable::canonical(git_dir).unwrap_or_else(|_| git_dir.to_path_buf()); |
| 461 | 50× | let key = hash_object(ObjectType::Chunk, canonical.to_string_lossy().as_bytes()).short(16); |
| 462 | 50× | let dir = self.oxy.join("git").join(key); |
| 463 | 50× | fs::create_dir_all(&dir)?; |
| 464 | 50× | Ok(dir) |
| 465 | 50× | } |
| 466 | |
|
| 467 | | |
| 468 | | |
| 469 | 47× | fn read_git_states(path: &Path) -> Result<BTreeMap<ObjectRef, String>> { |
| 470 | 47× | let mut out = BTreeMap::new(); |
| 471 | 47× | if let Ok(text) = fs::read_to_string(path) { |
| 472 | 12× | for line in text.lines() { |
| 473 | 12× | if let Some((h, sha)) = line.split_once(' ') { |
| 474 | 12× | if let Some(h) = ObjectRef::from_hex(h) { |
| 475 | 12× | out.insert(h, sha.trim().to_string()); |
| 476 | 12× | } |
| 477 | 0× | } |
| 478 | | } |
| 479 | 37× | } |
| 480 | 47× | Ok(out) |
| 481 | 47× | } |
| 482 | |
|
| 483 | 15× | fn record_git_state(&self, git_dir: &Path, state: &ObjectRef, sha: &str) -> Result<()> { |
| 484 | 15× | let path = self.git_marks_dir(git_dir)?.join("git-states"); |
| 485 | 15× | let mut states = Self::read_git_states(&path)?; |
| 486 | 15× | states.insert(*state, sha.to_string()); |
| 487 | 15× | let text: String = states |
| 488 | 15× | .iter() |
| 489 | 16× | .map(|(h, s)| format!("{} {s}\n", h.to_hex())) |
| 490 | 15× | .collect(); |
| 491 | 15× | write_atomic(&path, text.as_bytes()) |
| 492 | 15× | } |
| 493 | |
|
| 494 | | |
| 495 | 7× | fn git_mirror(&self, url: &str) -> Result<PathBuf> { |
| 496 | 7× | let key = hash_object(ObjectType::Chunk, url.as_bytes()).short(16); |
| 497 | 7× | let dir = self.oxy.join("git").join(key).join("mirror.git"); |
| 498 | 7× | if !dir.join("HEAD").exists() { |
| 499 | 1× | fs::create_dir_all(&dir)?; |
| 500 | 1× | git(&dir, &["init", "--bare", "-q"])?; |
| 501 | 6× | } |
| 502 | 7× | Ok(dir) |
| 503 | 7× | } |
| 504 | |
|
| 505 | | |
| 506 | 30× | pub(crate) fn imported_commits(&self) -> Result<HashMap<String, ChangeHash>> { |
| 507 | 30× | let mut map = HashMap::new(); |
| 508 | 112× | for e in self.log()? { |
| 509 | 112× | if e.is_working_copy { |
| 510 | 30× | continue; |
| 511 | 82× | } |
| 512 | 82× | if let Some(sha) = e |
| 513 | 82× | .change |
| 514 | 82× | .message |
| 515 | 82× | .lines() |
| 516 | 82× | .rev() |
| 517 | 158× | .find_map(|l| l.strip_prefix("Git-Commit: ")) |
| 518 | 76× | { |
| 519 | 76× | map.insert(sha.trim().to_string(), e.hash); |
| 520 | 76× | } |
| 521 | | } |
| 522 | 30× | Ok(map) |
| 523 | 30× | } |
| 524 | |
|
| 525 | | |
| 526 | | |
| 527 | 7× | fn git_mirror_fetch(&self, mirror: &Path, url: &str, branch: &str) -> Result<()> { |
| 528 | 7× | let spec = format!("+refs/heads/{branch}:refs/heads/{branch}"); |
| 529 | 7× | match git(mirror, &["fetch", "-q", url, &spec]) { |
| 530 | 7× | Ok(_) => Ok(()), |
| 531 | 0× | Err(e) => { |
| 532 | 0× | let ls = git(mirror, &["ls-remote", "--heads", url, branch]).unwrap_or_default(); |
| 533 | 0× | if ls.trim().is_empty() { |
| 534 | 0× | Ok(()) |
| 535 | | } else { |
| 536 | 0× | Err(e) |
| 537 | | } |
| 538 | | } |
| 539 | | } |
| 540 | 7× | } |
| 541 | |
|
| 542 | | |
| 543 | | |
| 544 | | |
| 545 | | |
| 546 | 3× | pub fn git_fetch(&self, url: &str, branch: &str) -> Result<GitImportReport> { |
| 547 | | |
| 548 | 3× | let local = crate::portable::canonical(Path::new(url)) |
| 549 | 3× | .ok() |
| 550 | 3× | .filter(|_| !url.contains("://")); |
| 551 | 3× | let absolute = local.as_ref().map(|p| p.to_string_lossy().into_owned()); |
| 552 | 3× | let url = absolute.as_deref().unwrap_or(url); |
| 553 | 3× | let mirror = self.git_mirror(url)?; |
| 554 | 3× | self.git_mirror_fetch(&mirror, url, branch)?; |
| 555 | 3× | if git( |
| 556 | 3× | &mirror, |
| 557 | 3× | &[ |
| 558 | 3× | "rev-parse", |
| 559 | 3× | "--verify", |
| 560 | 3× | "-q", |
| 561 | 3× | &format!("refs/heads/{branch}"), |
| 562 | 3× | ], |
| 563 | 3× | ) |
| 564 | 3× | .is_err() |
| 565 | | { |
| 566 | 0× | return fail(format!("{url} has no branch {branch:?}")); |
| 567 | 3× | } |
| 568 | 3× | self.import_git_commits( |
| 569 | 3× | &mirror, |
| 570 | 3× | branch, |
| 571 | 3× | &format!("oxy git fetch {url} {branch}"), |
| 572 | 1× | &mut |_| {}, |
| 573 | | ) |
| 574 | 3× | } |
| 575 | |
|
| 576 | | |
| 577 | | |
| 578 | | |
| 579 | 4× | pub fn git_push(&self, url: &str, branch: &str) -> Result<GitExportReport> { |
| 580 | 4× | let mirror = self.git_mirror(url)?; |
| 581 | 4× | self.git_mirror_fetch(&mirror, url, branch)?; |
| 582 | 4× | if let Ok(tip) = git( |
| 583 | 4× | &mirror, |
| 584 | 4× | &[ |
| 585 | 4× | "rev-parse", |
| 586 | 4× | "--verify", |
| 587 | 4× | "-q", |
| 588 | 4× | &format!("refs/heads/{branch}"), |
| 589 | 4× | ], |
| 590 | 4× | ) { |
| 591 | 4× | let known = self.git_known(&mirror)?; |
| 592 | 4× | if !known.imported.contains_key(&tip) && !known.exported.contains_key(&tip) { |
| 593 | 1× | return fail(format!( |
| 594 | | "{url} {branch} has commits not yet fetched; run `oxy git fetch {url}` first" |
| 595 | | )); |
| 596 | 3× | } |
| 597 | 0× | } |
| 598 | 3× | let report = self.git_export_as(&mirror, branch)?; |
| 599 | 3× | let spec = format!("refs/heads/{branch}:refs/heads/{branch}"); |
| 600 | 3× | git(&mirror, &["push", "-q", url, &spec])?; |
| 601 | 3× | Ok(report) |
| 602 | 4× | } |
| 603 | |
|
| 604 | 35× | fn read_oxy_marks(path: &Path) -> Result<BTreeMap<ObjectRef, u64>> { |
| 605 | 35× | let mut out = BTreeMap::new(); |
| 606 | 35× | if let Ok(text) = fs::read_to_string(path) { |
| 607 | 18× | for line in text.lines() { |
| 608 | 18× | if let Some((h, m)) = line.split_once(' ') { |
| 609 | 18× | if let (Some(h), Ok(m)) = (ObjectRef::from_hex(h), m.parse()) { |
| 610 | 18× | out.insert(h, m); |
| 611 | 18× | } |
| 612 | 0× | } |
| 613 | | } |
| 614 | 24× | } |
| 615 | 35× | Ok(out) |
| 616 | 35× | } |
| 617 | |
|
| 618 | | |
| 619 | | |
| 620 | | |
| 621 | | |
| 622 | 7× | pub fn git_export(&self, git_dir: &Path) -> Result<GitExportReport> { |
| 623 | 7× | let bookmark = self.config.bookmark.clone(); |
| 624 | 7× | self.git_export_as(git_dir, &bookmark) |
| 625 | 7× | } |
| 626 | |
|
| 627 | | |
| 628 | | |
| 629 | | |
| 630 | 12× | pub fn git_export_as(&self, git_dir: &Path, branch: &str) -> Result<GitExportReport> { |
| 631 | 12× | if !git_dir.join("HEAD").exists() && !git_dir.join(".git").exists() { |
| 632 | 4× | fs::create_dir_all(git_dir)?; |
| 633 | 4× | git(git_dir, &["init", "--bare", "-q"])?; |
| 634 | 8× | } |
| 635 | 12× | let marks_dir = self.git_marks_dir(git_dir)?; |
| 636 | 12× | let git_marks = marks_dir.join("git-marks"); |
| 637 | 12× | let oxy_marks_path = marks_dir.join("oxy-marks"); |
| 638 | 12× | let mut marks = Self::read_oxy_marks(&oxy_marks_path)?; |
| 639 | 12× | let git_states = Self::read_git_states(&marks_dir.join("git-states"))?; |
| 640 | 12× | let mut next_mark = marks.values().copied().max().unwrap_or(0) + 1; |
| 641 | |
|
| 642 | 12× | let bookmark = self.config.bookmark.clone(); |
| 643 | 12× | let mut stream: Vec<u8> = Vec::new(); |
| 644 | 12× | let mut report = GitExportReport { |
| 645 | 12× | branch: branch.to_string(), |
| 646 | 12× | heads: self.bookmark_heads()?.len(), |
| 647 | 12× | ..Default::default() |
| 648 | | }; |
| 649 | |
|
| 650 | | |
| 651 | 12× | let mut ops = self.op_log()?; |
| 652 | 12× | ops.reverse(); |
| 653 | 12× | let mut prev: Option<(ObjectRef, Vec<ChangeHash>)> = None; |
| 654 | | |
| 655 | 12× | let mut last_parent: Option<String> = None; |
| 656 | 49× | for (_, op) in &ops { |
| 657 | 49× | let Some(state_ref) = op.view.bookmarks.get(&bookmark).copied() else { |
| 658 | 0× | continue; |
| 659 | | }; |
| 660 | 49× | if prev.as_ref().is_some_and(|(p, _)| *p == state_ref) { |
| 661 | 12× | continue; |
| 662 | 37× | } |
| 663 | 37× | let Ok(state) = self.store.get_state(&state_ref) else { |
| 664 | 0× | continue; |
| 665 | | }; |
| 666 | 37× | if state.heads.is_empty() { |
| 667 | 12× | prev = Some((state_ref, vec![])); |
| 668 | 12× | continue; |
| 669 | 25× | } |
| 670 | 25× | if let Some(sha) = git_states.get(&state_ref) { |
| 671 | 4× | report.skipped += 1; |
| 672 | 4× | last_parent = Some(sha.clone()); |
| 673 | 4× | prev = Some((state_ref, state.heads.clone())); |
| 674 | 4× | continue; |
| 675 | 21× | } |
| 676 | 21× | if let Some(m) = marks.get(&state_ref) { |
| 677 | 10× | report.skipped += 1; |
| 678 | 10× | last_parent = Some(format!(":{m}")); |
| 679 | 10× | prev = Some((state_ref, state.heads.clone())); |
| 680 | 10× | continue; |
| 681 | 11× | } |
| 682 | 11× | let prev_heads = prev.as_ref().map(|(_, h)| h.clone()).unwrap_or_default(); |
| 683 | 11× | let added = self |
| 684 | 11× | .negotiate(&prev_heads, &state.heads) |
| 685 | 11× | .unwrap_or_default(); |
| 686 | 11× | let loaded = self.load_state(&state.heads)?; |
| 687 | 11× | let snapshot = loaded.materialize(); |
| 688 | 11× | let (author, secs, mut message) = match added.as_slice() { |
| 689 | 8× | [h] => { |
| 690 | 8× | let c = self.store.get_change(h)?; |
| 691 | 8× | let mut m = c.message.clone(); |
| 692 | 8× | if !m.ends_with('\n') { |
| 693 | 8× | m.push('\n'); |
| 694 | 8× | } |
| 695 | 8× | m.push_str(&format!( |
| 696 | 8× | "\nChange-Id: {}\nOxy-Hash: {}\n", |
| 697 | 8× | c.change_id.to_hex(), |
| 698 | 8× | h.to_hex() |
| 699 | 8× | )); |
| 700 | 8× | (self.git_author(&c), c.hlc.wall_ms / 1000, m) |
| 701 | | } |
| 702 | 3× | [] => ( |
| 703 | 0× | "oxy <oxy@localhost>".to_string(), |
| 704 | 0× | op.hlc.wall_ms / 1000, |
| 705 | 0× | format!("oxy: {}\n\n", op.command), |
| 706 | 0× | ), |
| 707 | 3× | many => { |
| 708 | 3× | let mut m = format!("oxy: {} ({} changes)\n\n", op.command, many.len()); |
| 709 | 22× | for h in many { |
| 710 | 22× | let c = self.store.get_change(h)?; |
| 711 | 22× | m.push_str(&format!( |
| 712 | 22× | "- {} {}\n", |
| 713 | 22× | c.change_id.short(8), |
| 714 | 22× | c.message.lines().next().unwrap_or("") |
| 715 | 22× | )); |
| 716 | | } |
| 717 | 3× | m.push_str(&format!( |
| 718 | | "\nOxy-Changes: {}\n", |
| 719 | 3× | many.iter() |
| 720 | 22× | .map(|h| h.to_hex()) |
| 721 | 3× | .collect::<Vec<_>>() |
| 722 | 3× | .join(" ") |
| 723 | | )); |
| 724 | 3× | ("oxy <oxy@localhost>".to_string(), op.hlc.wall_ms / 1000, m) |
| 725 | | } |
| 726 | | }; |
| 727 | 11× | message.push_str(&format!("Oxy-State: {}\n", state_ref.to_hex())); |
| 728 | 11× | if snapshot.is_conflicted() { |
| 729 | 0× | message.push_str("Oxy-Conflicted: true\n"); |
| 730 | 11× | } |
| 731 | 11× | let mark = next_mark; |
| 732 | 11× | next_mark += 1; |
| 733 | 11× | let parents: Vec<String> = last_parent.clone().into_iter().collect(); |
| 734 | 11× | self.write_commit( |
| 735 | 11× | &mut stream, |
| 736 | 11× | "refs/oxy/export", |
| 737 | 11× | mark, |
| 738 | 11× | &author, |
| 739 | 11× | secs, |
| 740 | 11× | &message, |
| 741 | 11× | &parents, |
| 742 | 11× | &snapshot, |
| 743 | 0× | )?; |
| 744 | 11× | marks.insert(state_ref, mark); |
| 745 | 11× | last_parent = Some(format!(":{mark}")); |
| 746 | 11× | prev = Some((state_ref, state.heads.clone())); |
| 747 | 11× | report.exported += 1; |
| 748 | | } |
| 749 | 12× | if let Some(p) = &last_parent { |
| 750 | 12× | stream.extend_from_slice(format!("reset refs/heads/{branch}\nfrom {p}\n\n").as_bytes()); |
| 751 | 12× | } |
| 752 | | |
| 753 | | |
| 754 | 12× | for t in self.tags()? { |
| 755 | 0× | let target = match (git_states.get(&t.state), marks.get(&t.state)) { |
| 756 | 0× | (Some(sha), _) => sha.clone(), |
| 757 | 0× | (None, Some(m)) => format!(":{m}"), |
| 758 | | (None, None) => { |
| 759 | 0× | let loaded = self.load_state(&t.heads)?; |
| 760 | 0× | let snapshot = loaded.materialize(); |
| 761 | 0× | let mark = next_mark; |
| 762 | 0× | next_mark += 1; |
| 763 | 0× | let message = format!( |
| 764 | | "oxy: tag {}\n\n{}Oxy-State: {}\n", |
| 765 | | t.name, |
| 766 | 0× | if t.message.is_empty() { |
| 767 | 0× | String::new() |
| 768 | | } else { |
| 769 | 0× | format!("{}\n\n", t.message) |
| 770 | | }, |
| 771 | 0× | t.state.to_hex() |
| 772 | | ); |
| 773 | 0× | self.write_commit( |
| 774 | 0× | &mut stream, |
| 775 | 0× | "refs/oxy/export", |
| 776 | 0× | mark, |
| 777 | 0× | "oxy <oxy@localhost>", |
| 778 | 0× | Hlc::now().wall_ms / 1000, |
| 779 | 0× | &message, |
| 780 | 0× | &[], |
| 781 | 0× | &snapshot, |
| 782 | 0× | )?; |
| 783 | 0× | marks.insert(t.state, mark); |
| 784 | 0× | report.exported += 1; |
| 785 | 0× | format!(":{mark}") |
| 786 | | } |
| 787 | | }; |
| 788 | 0× | stream.extend_from_slice( |
| 789 | 0× | format!("reset refs/tags/{}\nfrom {target}\n\n", t.name).as_bytes(), |
| 790 | | ); |
| 791 | | } |
| 792 | 12× | stream.extend_from_slice(b"done\n"); |
| 793 | |
|
| 794 | 12× | let mut cmd = Command::new("git"); |
| 795 | | |
| 796 | 12× | cmd.arg("-C") |
| 797 | 12× | .arg(git_dir) |
| 798 | 12× | .args(["fast-import", "--quiet", "--done", "--force"]) |
| 799 | 12× | .arg(format!("--export-marks={}", git_marks.display())); |
| 800 | 12× | if git_marks.exists() { |
| 801 | 6× | cmd.arg(format!("--import-marks={}", git_marks.display())); |
| 802 | 6× | } |
| 803 | 12× | let mut child = cmd |
| 804 | 12× | .stdin(Stdio::piped()) |
| 805 | 12× | .stdout(Stdio::null()) |
| 806 | 12× | .stderr(Stdio::piped()) |
| 807 | 12× | .spawn() |
| 808 | 12× | .map_err(|e| RepoError::Msg(format!("running git fast-import: {e}")))?; |
| 809 | 12× | child.stdin.take().unwrap().write_all(&stream)?; |
| 810 | 12× | let out = child.wait_with_output()?; |
| 811 | 12× | if !out.status.success() { |
| 812 | 0× | return fail(format!( |
| 813 | | "git fast-import failed: {}", |
| 814 | 0× | String::from_utf8_lossy(&out.stderr).trim() |
| 815 | | )); |
| 816 | 12× | } |
| 817 | 12× | let text: String = marks |
| 818 | 12× | .iter() |
| 819 | 21× | .map(|(h, m)| format!("{} {m}\n", h.to_hex())) |
| 820 | 12× | .collect(); |
| 821 | 12× | write_atomic(&oxy_marks_path, text.as_bytes())?; |
| 822 | 12× | let _ = git(git_dir, &["update-ref", "-d", "refs/oxy/export"]); |
| 823 | 12× | report.skipped_subrepos = self.git_export_skipped.replace(0); |
| 824 | 12× | Ok(report) |
| 825 | 12× | } |
| 826 | |
|
| 827 | | |
| 828 | | |
| 829 | | |
| 830 | | |
| 831 | 13× | pub fn import_git(&self, git_dir: &Path, branch: &str) -> Result<GitImportReport> { |
| 832 | 13× | self.import_git_with(git_dir, branch, &mut |_| {}) |
| 833 | 13× | } |
| 834 | |
|
| 835 | | |
| 836 | | |
| 837 | | |
| 838 | 13× | pub fn import_git_with( |
| 839 | 13× | &self, |
| 840 | 13× | git_dir: &Path, |
| 841 | 13× | branch: &str, |
| 842 | 13× | progress: crate::store::Progress<'_>, |
| 843 | 13× | ) -> Result<GitImportReport> { |
| 844 | 13× | if !self.bookmark_heads()?.is_empty() { |
| 845 | 0× | return fail("git import needs an empty bookmark; import into a fresh repository"); |
| 846 | 13× | } |
| 847 | 13× | self.import_git_commits( |
| 848 | 13× | git_dir, |
| 849 | 13× | branch, |
| 850 | 13× | &format!("oxy git import {} {branch}", git_dir.display()), |
| 851 | 13× | progress, |
| 852 | | ) |
| 853 | 13× | } |
| 854 | |
|
| 855 | | |
| 856 | | |
| 857 | 16× | fn import_git_commits( |
| 858 | 16× | &self, |
| 859 | 16× | git_dir: &Path, |
| 860 | 16× | branch: &str, |
| 861 | 16× | command: &str, |
| 862 | 16× | progress: crate::store::Progress<'_>, |
| 863 | 16× | ) -> Result<GitImportReport> { |
| 864 | 16× | let batch = self.store.begin_batch()?; |
| 865 | 16× | let result = self.import_git_commits_batched(git_dir, branch, command, progress); |
| 866 | 16× | batch.finish()?; |
| 867 | 16× | result |
| 868 | 16× | } |
| 869 | |
|
| 870 | 16× | fn import_git_commits_batched( |
| 871 | 16× | &self, |
| 872 | 16× | git_dir: &Path, |
| 873 | 16× | branch: &str, |
| 874 | 16× | command: &str, |
| 875 | 16× | progress: crate::store::Progress<'_>, |
| 876 | 16× | ) -> Result<GitImportReport> { |
| 877 | 16× | let commits = list_commits(git_dir, branch)?; |
| 878 | 16× | let mut cat = CatFile::start(git_dir)?; |
| 879 | 16× | let mut report = GitImportReport::default(); |
| 880 | 16× | let mut known = self.git_known(git_dir)?; |
| 881 | 16× | let old_heads = self.bookmark_heads()?; |
| 882 | | |
| 883 | | |
| 884 | | |
| 885 | 16× | let mut heads_now: BTreeSet<ChangeHash> = old_heads.iter().copied().collect(); |
| 886 | | |
| 887 | | |
| 888 | | |
| 889 | | |
| 890 | | |
| 891 | | |
| 892 | 16× | let max_kept: usize = std::env::var("OXY_IMPORT_KEEP") |
| 893 | 16× | .ok() |
| 894 | 16× | .and_then(|v| v.parse().ok()) |
| 895 | 16× | .unwrap_or(8); |
| 896 | 16× | let mut waiting: HashMap<&str, usize> = HashMap::new(); |
| 897 | 93× | for gc in &commits { |
| 898 | 93× | if let Some(p1) = gc.parents.first() { |
| 899 | 75× | *waiting.entry(p1.as_str()).or_default() += 1; |
| 900 | 75× | } |
| 901 | | } |
| 902 | | type Kept = (State, BTreeMap<String, (String, String)>); |
| 903 | 16× | let mut kept: HashMap<String, Kept> = HashMap::new(); |
| 904 | | |
| 905 | 16× | let pairs: Vec<(String, String)> = commits |
| 906 | 16× | .iter() |
| 907 | 93× | .filter(|gc| { |
| 908 | 93× | !known.imported.contains_key(&gc.sha) && !known.exported.contains_key(&gc.sha) |
| 909 | 93× | }) |
| 910 | 88× | .filter_map(|gc| Some((gc.sha.clone(), gc.parents.first()?.clone()))) |
| 911 | 16× | .collect(); |
| 912 | 16× | let mut diffs = DiffTree::start(git_dir, pairs)?; |
| 913 | 16× | let mut rebuilt = 0usize; |
| 914 | 16× | let mut kept_order: std::collections::VecDeque<String> = std::collections::VecDeque::new(); |
| 915 | 16× | let mut last: Option<String> = None; |
| 916 | 16× | let mut blob_cache: HashMap<String, Vec<u8>> = HashMap::new(); |
| 917 | 16× | let mut cache_bytes: usize = 0; |
| 918 | | |
| 919 | 16× | let mut spent = [std::time::Duration::ZERO; 6]; |
| 920 | 16× | let mut clock = std::time::Instant::now(); |
| 921 | | |
| 922 | | |
| 923 | | |
| 924 | 16× | let trace = std::env::var_os("OXY_IMPORT_TRACE").is_some(); |
| 925 | 16× | let rss_mb = || -> u64 { |
| 926 | 0× | std::fs::read_to_string("/proc/self/statm") |
| 927 | 0× | .ok() |
| 928 | 0× | .and_then(|s| s.split_whitespace().nth(1)?.parse::<u64>().ok()) |
| 929 | 0× | .map(|pages| (pages * 4096) >> 20) |
| 930 | 0× | .unwrap_or(0) |
| 931 | 0× | }; |
| 932 | 16× | let mut done = 0usize; |
| 933 | | |
| 934 | | |
| 935 | 16× | let kept_atoms: usize = std::env::var("OXY_IMPORT_KEPT_ATOMS") |
| 936 | 16× | .ok() |
| 937 | 16× | .and_then(|v| v.parse().ok()) |
| 938 | 16× | .unwrap_or(KEPT_ATOMS); |
| 939 | | |
| 940 | 16× | let mut shed = 0usize; |
| 941 | 528× | let mut lap = |i: usize, clock: &mut std::time::Instant| { |
| 942 | 528× | let now = std::time::Instant::now(); |
| 943 | 528× | spent[i] += now - *clock; |
| 944 | 528× | *clock = now; |
| 945 | 528× | }; |
| 946 | |
|
| 947 | | |
| 948 | 16× | let checkpoint_every: usize = std::env::var("OXY_IMPORT_CHECKPOINT") |
| 949 | 16× | .ok() |
| 950 | 16× | .and_then(|v| v.parse().ok()) |
| 951 | 16× | .filter(|n| *n > 0) |
| 952 | 16× | .unwrap_or(CHECKPOINT_COMMITS); |
| 953 | | |
| 954 | | |
| 955 | 16× | let stop_after: Option<usize> = std::env::var("OXY_IMPORT_STOP_AFTER") |
| 956 | 16× | .ok() |
| 957 | 16× | .and_then(|v| v.parse().ok()); |
| 958 | | |
| 959 | | |
| 960 | | |
| 961 | | |
| 962 | 16× | let partial_merges = std::env::var_os("OXY_IMPORT_WHOLE_MERGES").is_none_or(|v| v == "0"); |
| 963 | 16× | let total = commits.len(); |
| 964 | | |
| 965 | | |
| 966 | | |
| 967 | 16× | let claim: Option<(KeyId, String)> = if self.config.git_claim { |
| 968 | 1× | let Some(key) = self.config.author_key else { |
| 969 | 0× | return fail("git.claim authors your own commits with your key, and there is none: run `oxy identity create` first, or `oxy config git.claim false`"); |
| 970 | | }; |
| 971 | 1× | Some((key, self.identity(&key)?.email.to_lowercase())) |
| 972 | | } else { |
| 973 | 15× | None |
| 974 | | }; |
| 975 | 93× | for (index, gc) in commits.iter().enumerate() { |
| 976 | 93× | if known.imported.contains_key(&gc.sha) || known.exported.contains_key(&gc.sha) { |
| 977 | 5× | continue; |
| 978 | 88× | } |
| 979 | 88× | if stop_after.is_some_and(|n| done >= n) { |
| 980 | 0× | break; |
| 981 | 88× | } |
| 982 | 88× | let commit_started = std::time::Instant::now(); |
| 983 | 88× | if index % 500 == 0 { |
| 984 | 14× | progress(&format!( |
| 985 | 14× | "{index} of {total} commits, {} merges; at {} {}", |
| 986 | 14× | report.merges, |
| 987 | 14× | &gc.sha[..12.min(gc.sha.len())], |
| 988 | 14× | gc.message |
| 989 | 14× | .lines() |
| 990 | 14× | .next() |
| 991 | 14× | .unwrap_or("") |
| 992 | 14× | .chars() |
| 993 | 14× | .take(60) |
| 994 | 14× | .collect::<String>() |
| 995 | 14× | )); |
| 996 | 74× | } |
| 997 | 88× | let mut base_how = "root"; |
| 998 | | |
| 999 | | |
| 1000 | | |
| 1001 | 88× | let (mut base, prev_tree) = match gc.parents.first() { |
| 1002 | 72× | Some(p1) => { |
| 1003 | 72× | let left = waiting.get_mut(p1.as_str()).map(|n| { |
| 1004 | 72× | *n -= 1; |
| 1005 | 72× | *n |
| 1006 | 72× | }); |
| 1007 | 72× | let found = if left == Some(0) { |
| 1008 | 71× | kept_order.retain(|k| k != p1); |
| 1009 | 52× | kept.remove(p1.as_str()) |
| 1010 | | } else { |
| 1011 | 20× | kept.get(p1.as_str()).cloned() |
| 1012 | | }; |
| 1013 | 72× | let how = match (&found, left) { |
| 1014 | 51× | (Some(_), Some(0)) => "kept", |
| 1015 | 20× | (Some(_), _) => "cloned", |
| 1016 | 1× | (None, _) => "rebuilt", |
| 1017 | | }; |
| 1018 | 72× | if trace { |
| 1019 | 0× | base_how = how; |
| 1020 | 72× | } |
| 1021 | 72× | match found { |
| 1022 | 71× | Some((st, tree)) => (st, Some(tree)), |
| 1023 | | None => { |
| 1024 | | |
| 1025 | | |
| 1026 | | |
| 1027 | 1× | let mut st = State::new_history_only(); |
| 1028 | 1× | self.add_git_ancestors(&mut st, git_dir, &[p1.as_str()], &known)?; |
| 1029 | | |
| 1030 | | |
| 1031 | 1× | rebuilt += 1; |
| 1032 | 1× | (st, Some(list_tree(git_dir, p1)?)) |
| 1033 | | } |
| 1034 | | } |
| 1035 | | } |
| 1036 | 16× | None => (State::new_history_only(), None), |
| 1037 | | }; |
| 1038 | | |
| 1039 | | |
| 1040 | 88× | let mut touched: HashSet<NodeId> = HashSet::new(); |
| 1041 | 88× | let mut merged = 0usize; |
| 1042 | | |
| 1043 | | |
| 1044 | | |
| 1045 | | |
| 1046 | 88× | let mut first_parent_view: Option<crate::tree::TreeView> = None; |
| 1047 | 88× | if gc.parents.len() > 1 { |
| 1048 | 24× | report.merges += 1; |
| 1049 | 24× | if prev_tree.is_some() && partial_merges { |
| 1050 | 24× | first_parent_view = Some(base.tree().view()); |
| 1051 | 24× | } |
| 1052 | 24× | let mut args: Vec<String> = gc.parents[1..].to_vec(); |
| 1053 | 24× | args.push(format!("^{}", gc.parents[0])); |
| 1054 | 24× | let refs: Vec<&str> = args.iter().map(String::as_str).collect(); |
| 1055 | | |
| 1056 | | |
| 1057 | | |
| 1058 | | |
| 1059 | | |
| 1060 | | |
| 1061 | 24× | if base.built_count() > 0 { |
| 1062 | 21× | let mut count_args = vec!["rev-list", "--count"]; |
| 1063 | 21× | count_args.extend(refs.iter().copied()); |
| 1064 | 21× | let others: usize = git(git_dir, &count_args)?.trim().parse().unwrap_or(0); |
| 1065 | 21× | let shed_at = std::env::var("OXY_MERGE_SHED") |
| 1066 | 21× | .ok() |
| 1067 | 21× | .and_then(|v| v.parse().ok()) |
| 1068 | 21× | .unwrap_or(MERGE_SHED_COMMITS); |
| 1069 | 21× | if others > shed_at { |
| 1070 | 0× | shed += base.shed_graphs(|b| self.store_blob(b))?; |
| 1071 | 21× | } |
| 1072 | 3× | } |
| 1073 | 24× | let before = base.len(); |
| 1074 | 24× | self.add_git_ancestors_touching(&mut base, git_dir, &refs, &known, &mut touched)?; |
| 1075 | 24× | merged = base.len() - before; |
| 1076 | 64× | } |
| 1077 | 88× | lap(0, &mut clock); |
| 1078 | | |
| 1079 | | |
| 1080 | 88× | let diff = match gc.parents.first() { |
| 1081 | 72× | Some(_) => Some(diffs.diff(&gc.sha)?), |
| 1082 | 16× | None => None, |
| 1083 | | }; |
| 1084 | 88× | let (tree, changed) = match (prev_tree, diff) { |
| 1085 | 72× | (Some(mut t), Some(diff)) => { |
| 1086 | 72× | let mut changed: HashSet<String> = HashSet::new(); |
| 1087 | 90× | for (path, entry) in diff { |
| 1088 | 90× | match entry { |
| 1089 | 72× | Some(v) => { |
| 1090 | 72× | t.insert(path.clone(), v); |
| 1091 | 72× | } |
| 1092 | 18× | None => { |
| 1093 | 18× | t.remove(&path); |
| 1094 | 18× | } |
| 1095 | | } |
| 1096 | 90× | changed.insert(path); |
| 1097 | | } |
| 1098 | 72× | (t, Some(changed)) |
| 1099 | | } |
| 1100 | 16× | _ => (list_tree(git_dir, &gc.sha)?, None), |
| 1101 | | }; |
| 1102 | 88× | lap(1, &mut clock); |
| 1103 | | |
| 1104 | | |
| 1105 | | |
| 1106 | | |
| 1107 | 88× | let mut same: HashMap<&str, NodeId> = HashMap::new(); |
| 1108 | | |
| 1109 | | |
| 1110 | 88× | let mut same_opaque: HashSet<&str> = HashSet::new(); |
| 1111 | | |
| 1112 | | |
| 1113 | | |
| 1114 | | |
| 1115 | | |
| 1116 | | |
| 1117 | | |
| 1118 | | |
| 1119 | | |
| 1120 | | |
| 1121 | | |
| 1122 | | |
| 1123 | 88× | let mut changed = changed; |
| 1124 | | |
| 1125 | | |
| 1126 | 88× | let mut merge_how = if gc.parents.len() > 1 { |
| 1127 | 24× | if first_parent_view.is_some() { |
| 1128 | 24× | "partial" |
| 1129 | | } else { |
| 1130 | 0× | "whole" |
| 1131 | | } |
| 1132 | | } else { |
| 1133 | 64× | "-" |
| 1134 | | }; |
| 1135 | | |
| 1136 | | |
| 1137 | 88× | if first_parent_view.is_some() |
| 1138 | 24× | && base.tree().view().conflicts.iter().any(|c| { |
| 1139 | | use crate::tree::TreeConflict::*; |
| 1140 | 7× | match c { |
| 1141 | 5× | Name { nodes, .. } => nodes.iter().any(|n| touched.contains(n)), |
| 1142 | 1× | MultiPlacement { node, .. } | Cycle { node } | Mode { node, .. } => { |
| 1143 | 1× | touched.contains(node) |
| 1144 | | } |
| 1145 | 1× | Orphan { node, parent } => { |
| 1146 | 1× | touched.contains(node) || touched.contains(parent) |
| 1147 | | } |
| 1148 | | } |
| 1149 | 7× | }) |
| 1150 | 7× | { |
| 1151 | 7× | first_parent_view = None; |
| 1152 | 7× | merge_how = "conflicted"; |
| 1153 | 81× | } |
| 1154 | 88× | if let (Some(ch), Some(fp)) = (changed.as_mut(), &first_parent_view) { |
| 1155 | 17× | let union = base.tree().view(); |
| 1156 | 34× | for v in [fp, &union] { |
| 1157 | 139× | for (path, e) in v.entries.iter() { |
| 1158 | 139× | if touched.contains(&e.node) { |
| 1159 | 39× | ch.insert(path.clone()); |
| 1160 | 100× | } |
| 1161 | | } |
| 1162 | | } |
| 1163 | 71× | } |
| 1164 | 88× | let partial = changed.is_some() |
| 1165 | 72× | && (gc.parents.len() == 1 || first_parent_view.is_some()) |
| 1166 | 172× | && !tree.values().any(|(mode, _)| mode == "160000"); |
| 1167 | 88× | let mut rel: BTreeSet<&str> = BTreeSet::new(); |
| 1168 | | |
| 1169 | | let need_n: usize; |
| 1170 | 88× | let base_snapshot = if let (true, Some(changed)) = (partial, &changed) { |
| 1171 | 64× | let view = base.tree().view(); |
| 1172 | 64× | rel.extend(changed.iter().map(String::as_str)); |
| 1173 | 64× | rel.extend( |
| 1174 | 64× | tree.keys() |
| 1175 | 64× | .map(String::as_str) |
| 1176 | 169× | .filter(|p| *p == ".oxyattributes" || p.ends_with("/.oxyattributes")), |
| 1177 | | ); |
| 1178 | 64× | let mut dirs: BTreeSet<&str> = BTreeSet::new(); |
| 1179 | 89× | for p in changed.iter().filter(|p| !tree.contains_key(*p)) { |
| 1180 | 18× | let mut end = p.len(); |
| 1181 | 34× | while let Some(i) = p[..end].rfind('/') { |
| 1182 | 16× | if !dirs.insert(&p[..i]) { |
| 1183 | 0× | break; |
| 1184 | 16× | } |
| 1185 | 16× | end = i; |
| 1186 | | } |
| 1187 | | } |
| 1188 | 64× | let need: Vec<NodeId> = rel |
| 1189 | 64× | .iter() |
| 1190 | 89× | .filter_map(|p| view.entries.get(*p)) |
| 1191 | 65× | .filter(|e| e.kind == NodeKind::File && !e.mode.opaque) |
| 1192 | 64× | .map(|e| e.node) |
| 1193 | 64× | .collect(); |
| 1194 | 64× | crate::timing::add("render: same and need", clock.elapsed()); |
| 1195 | 64× | need_n = need.len(); |
| 1196 | 64× | let t = std::time::Instant::now(); |
| 1197 | 64× | self.load_graphs_of(&mut base, &need)?; |
| 1198 | 64× | crate::timing::add("render: load graphs", t.elapsed()); |
| 1199 | 64× | let t = std::time::Instant::now(); |
| 1200 | 64× | let snap = base.materialize_paths(view, rel.iter().chain(dirs.iter()).copied()); |
| 1201 | 64× | crate::timing::add("render: materialize", t.elapsed()); |
| 1202 | 64× | snap |
| 1203 | | } else { |
| 1204 | 24× | if let Some(changed) = &changed { |
| 1205 | 8× | let view = base.tree().view(); |
| 1206 | 19× | for path in tree.keys() { |
| 1207 | | |
| 1208 | 19× | if changed.contains(path) || path == ".gitmodules" || path == ".oxymodules" |
| 1209 | | { |
| 1210 | 10× | continue; |
| 1211 | 9× | } |
| 1212 | 9× | if let Some(e) = view.entries.get(path) { |
| 1213 | 5× | if touched.contains(&e.node) { |
| 1214 | 0× | continue; |
| 1215 | 5× | } |
| 1216 | 5× | let opaque = e.kind == NodeKind::Symlink |
| 1217 | 5× | || (e.kind == NodeKind::File && e.mode.opaque); |
| 1218 | 5× | if e.kind == NodeKind::File && !e.mode.opaque { |
| 1219 | 5× | same.insert(path.as_str(), e.node); |
| 1220 | 5× | } else if opaque && base.live_blobs(&e.node).len() == 1 { |
| 1221 | 0× | same_opaque.insert(path.as_str()); |
| 1222 | 0× | } |
| 1223 | 4× | } |
| 1224 | | } |
| 1225 | 16× | } |
| 1226 | | |
| 1227 | 24× | let skip: HashSet<NodeId> = same.values().copied().collect(); |
| 1228 | 24× | let need: Vec<NodeId> = base |
| 1229 | 24× | .tree() |
| 1230 | 24× | .view() |
| 1231 | 24× | .entries |
| 1232 | 24× | .values() |
| 1233 | 24× | .filter(|e| { |
| 1234 | 21× | e.kind == NodeKind::File && !e.mode.opaque && !skip.contains(&e.node) |
| 1235 | 21× | }) |
| 1236 | 24× | .map(|e| e.node) |
| 1237 | 24× | .collect(); |
| 1238 | 24× | crate::timing::add("render: same and need", clock.elapsed()); |
| 1239 | 24× | need_n = need.len(); |
| 1240 | 24× | let t = std::time::Instant::now(); |
| 1241 | 24× | let snap = if need.len() > merge_batch() { |
| 1242 | | |
| 1243 | | |
| 1244 | 5× | self.materialize_batched(&mut base, &need, &skip)? |
| 1245 | | } else { |
| 1246 | 19× | let g = std::time::Instant::now(); |
| 1247 | 19× | self.load_graphs_of(&mut base, &need)?; |
| 1248 | 19× | crate::timing::add("render: whole, load graphs", g.elapsed()); |
| 1249 | 19× | let m = std::time::Instant::now(); |
| 1250 | 19× | let snap = base.materialize_except(&skip); |
| 1251 | 19× | crate::timing::add("render: whole, materialize", m.elapsed()); |
| 1252 | 19× | snap |
| 1253 | | }; |
| 1254 | 24× | crate::timing::add("render: whole tree", t.elapsed()); |
| 1255 | 24× | snap |
| 1256 | | }; |
| 1257 | |
|
| 1258 | 88× | lap(2, &mut clock); |
| 1259 | | |
| 1260 | 88× | let mut entries: BTreeMap<String, FsEntry> = BTreeMap::new(); |
| 1261 | 88× | let mut gitlinks: Vec<(String, String)> = Vec::new(); |
| 1262 | 88× | let listed: Box<dyn Iterator<Item = (&String, &(String, String))>> = if partial { |
| 1263 | 89× | Box::new(rel.iter().filter_map(|p| tree.get_key_value(*p))) |
| 1264 | | } else { |
| 1265 | 24× | Box::new(tree.iter()) |
| 1266 | | }; |
| 1267 | 118× | for (path, (mode, sha)) in listed { |
| 1268 | 118× | let (path, sha) = (path.clone(), sha.clone()); |
| 1269 | 118× | let (kind, exec) = match mode.as_str() { |
| 1270 | 118× | "100644" | "100664" => (NodeKind::File, false), |
| 1271 | 13× | "100755" => (NodeKind::File, true), |
| 1272 | 1× | "120000" => (NodeKind::Symlink, false), |
| 1273 | 1× | "160000" => { |
| 1274 | 1× | gitlinks.push((path, sha)); |
| 1275 | 1× | continue; |
| 1276 | | } |
| 1277 | 0× | _ => continue, |
| 1278 | | }; |
| 1279 | 117× | if same.contains_key(path.as_str()) || same_opaque.contains(path.as_str()) { |
| 1280 | 5× | let mut e = FsEntry::new(kind, exec, Vec::new()); |
| 1281 | 5× | e.unchanged = true; |
| 1282 | 5× | entries.insert(path, e); |
| 1283 | 5× | continue; |
| 1284 | 112× | } |
| 1285 | 112× | let content = match blob_cache.get(&sha) { |
| 1286 | 36× | Some(c) => c.clone(), |
| 1287 | | None => { |
| 1288 | 76× | let c = cat.read(&sha)?; |
| 1289 | 76× | if cache_bytes + c.len() > 64 * 1024 * 1024 { |
| 1290 | 0× | blob_cache.clear(); |
| 1291 | 0× | cache_bytes = 0; |
| 1292 | 76× | } |
| 1293 | 76× | cache_bytes += c.len(); |
| 1294 | 76× | blob_cache.insert(sha.clone(), c.clone()); |
| 1295 | 76× | c |
| 1296 | | } |
| 1297 | | }; |
| 1298 | 112× | entries.insert(path, FsEntry::new(kind, exec, content)); |
| 1299 | | } |
| 1300 | |
|
| 1301 | | |
| 1302 | | |
| 1303 | 88× | if !gitlinks.is_empty() { |
| 1304 | 1× | report.submodules += gitlinks.len(); |
| 1305 | 1× | let gm = entries |
| 1306 | 1× | .get(".gitmodules") |
| 1307 | 1× | .map(|e| { |
| 1308 | 1× | crate::subrepo::Modules::from_gitmodules(&String::from_utf8_lossy( |
| 1309 | 1× | &e.content, |
| 1310 | 1× | )) |
| 1311 | 1× | }) |
| 1312 | 1× | .unwrap_or_default(); |
| 1313 | 1× | let mut modules = entries |
| 1314 | 1× | .get(".oxymodules") |
| 1315 | 1× | .map(|e| crate::subrepo::Modules::parse(&String::from_utf8_lossy(&e.content))) |
| 1316 | 1× | .unwrap_or_default(); |
| 1317 | 1× | for (path, sha) in &gitlinks { |
| 1318 | 1× | let m = gm.entries.get(path); |
| 1319 | 1× | let branch = m |
| 1320 | 1× | .and_then(|m| m.bookmark.clone()) |
| 1321 | 1× | .unwrap_or_else(|| "HEAD".into()); |
| 1322 | 1× | entries.insert( |
| 1323 | 1× | path.clone(), |
| 1324 | 1× | FsEntry::new( |
| 1325 | 1× | NodeKind::Subrepo, |
| 1326 | | false, |
| 1327 | 1× | crate::subrepo::Pin::Git { |
| 1328 | 1× | sha: sha.clone(), |
| 1329 | 1× | branch, |
| 1330 | 1× | } |
| 1331 | 1× | .encode(), |
| 1332 | | ), |
| 1333 | | ); |
| 1334 | 1× | if let Some(m) = m { |
| 1335 | 1× | modules.entries.insert(path.clone(), m.clone()); |
| 1336 | 1× | } |
| 1337 | | } |
| 1338 | 1× | if !modules.entries.is_empty() { |
| 1339 | 1× | entries.insert( |
| 1340 | 1× | ".oxymodules".into(), |
| 1341 | 1× | FsEntry::new(NodeKind::File, false, modules.to_text().into_bytes()), |
| 1342 | 1× | ); |
| 1343 | 1× | } |
| 1344 | 87× | } |
| 1345 | 88× | lap(3, &mut clock); |
| 1346 | | |
| 1347 | | |
| 1348 | 88× | let seed = *blake3::hash(format!("oxy git import\0{}", gc.sha).as_bytes()).as_bytes(); |
| 1349 | 88× | let mut built = if partial { |
| 1350 | | |
| 1351 | 64× | let dir_needed = |dir: &str| { |
| 1352 | 16× | let prefix = format!("{dir}/"); |
| 1353 | 16× | tree.range(prefix.clone()..) |
| 1354 | 16× | .next() |
| 1355 | 16× | .is_some_and(|(k, _)| k.starts_with(&prefix)) |
| 1356 | 16× | }; |
| 1357 | 64× | self.build_from_entries_seeded( |
| 1358 | 64× | &base, |
| 1359 | 64× | &base_snapshot, |
| 1360 | 64× | &entries, |
| 1361 | 64× | seed, |
| 1362 | 64× | Some(&dir_needed), |
| 1363 | 0× | )? |
| 1364 | | } else { |
| 1365 | 24× | self.build_from_entries_seeded(&base, &base_snapshot, &entries, seed, None)? |
| 1366 | | }; |
| 1367 | 88× | lap(4, &mut clock); |
| 1368 | | |
| 1369 | | |
| 1370 | | |
| 1371 | | |
| 1372 | 102× | for (path, f) in &base_snapshot.files { |
| 1373 | 102× | let (FileContent::Opaque { blobs }, Some(e)) = (&f.content, entries.get(path)) |
| 1374 | | else { |
| 1375 | 98× | continue; |
| 1376 | | }; |
| 1377 | 4× | if blobs.len() < 2 || f.kind != e.kind { |
| 1378 | 3× | continue; |
| 1379 | 1× | } |
| 1380 | 1× | let chosen = self.store_blob(&e.content)?; |
| 1381 | 2× | for b in blobs { |
| 1382 | 2× | built.edits.push(Edit::Replace { |
| 1383 | 2× | node: f.node, |
| 1384 | 2× | old: Some(*b), |
| 1385 | 2× | new: Some(chosen), |
| 1386 | 2× | }); |
| 1387 | 2× | } |
| 1388 | 1× | built.resolves = true; |
| 1389 | 1× | built |
| 1390 | 1× | .contributors |
| 1391 | 1× | .extend(base.live_replaces(&f.node).into_iter().map(|(c, _)| c)); |
| 1392 | | } |
| 1393 | | |
| 1394 | | |
| 1395 | 88× | drop(base_snapshot); |
| 1396 | 88× | drop(entries); |
| 1397 | 88× | let mut deps: BTreeSet<ChangeHash> = built.contributors.iter().copied().collect(); |
| 1398 | 88× | let mut message = gc.message.clone(); |
| 1399 | 88× | if !message.ends_with('\n') { |
| 1400 | 88× | message.push('\n'); |
| 1401 | 88× | } |
| 1402 | 88× | message.push_str(&format!( |
| 1403 | 88× | "\nGit-Commit: {}\nGit-Author: {}\n", |
| 1404 | 88× | gc.sha, gc.author |
| 1405 | 88× | )); |
| 1406 | 88× | let mut change = Change { |
| 1407 | | version: crate::objects::CHANGE_VERSION, |
| 1408 | 88× | change_id: change_id_for(&gc.sha, &gc.message), |
| 1409 | 88× | deps: vec![], |
| 1410 | 88× | author: claim |
| 1411 | 88× | .as_ref() |
| 1412 | 88× | .filter(|(_, email)| git_email(&gc.author).is_some_and(|e| e == *email)) |
| 1413 | 88× | .map_or(ObjectRef::ZERO, |(key, _)| *key), |
| 1414 | 88× | hlc: Hlc { |
| 1415 | 88× | wall_ms: gc.author_secs * 1000, |
| 1416 | 88× | logical: 0, |
| 1417 | 88× | }, |
| 1418 | 88× | message, |
| 1419 | 88× | tree_ops: built.tree_ops, |
| 1420 | 88× | edits: built.edits, |
| 1421 | 88× | kind: if built.resolves { |
| 1422 | 12× | ChangeKind::Resolution |
| 1423 | | } else { |
| 1424 | 76× | ChangeKind::Normal |
| 1425 | | }, |
| 1426 | | }; |
| 1427 | 88× | let t = std::time::Instant::now(); |
| 1428 | | |
| 1429 | | |
| 1430 | | |
| 1431 | 88× | let need: Vec<NodeId> = change |
| 1432 | 88× | .edits |
| 1433 | 88× | .iter() |
| 1434 | 93× | .filter(|e| matches!(e, Edit::Delete { .. })) |
| 1435 | 88× | .map(|e| e.node()) |
| 1436 | 88× | .filter(|n| !base.is_built(n)) |
| 1437 | 88× | .collect(); |
| 1438 | 88× | if !need.is_empty() { |
| 1439 | 0× | self.load_graphs_of(&mut base, &need)?; |
| 1440 | 88× | } |
| 1441 | 88× | deps.extend(referenced_changes(&change, &base)); |
| 1442 | 88× | crate::timing::add("store: referenced", t.elapsed()); |
| 1443 | 88× | change.deps = deps.into_iter().collect(); |
| 1444 | 88× | change.normalize(); |
| 1445 | 88× | let hash = self.store.put_change(&change)?; |
| 1446 | 96× | for d in &change.deps { |
| 1447 | 96× | heads_now.remove(d); |
| 1448 | 96× | } |
| 1449 | 88× | heads_now.insert(hash); |
| 1450 | | |
| 1451 | | |
| 1452 | 88× | let t = std::time::Instant::now(); |
| 1453 | 88× | let (skeleton, texts) = self.store.get_change_lazy(&hash)?; |
| 1454 | 88× | base.add_known_mixed(hash, change, skeleton, texts); |
| 1455 | 88× | crate::timing::add("store: add to state", t.elapsed()); |
| 1456 | 88× | known.imported.insert(gc.sha.clone(), hash); |
| 1457 | 88× | if waiting.get(gc.sha.as_str()).is_some_and(|n| *n > 0) { |
| 1458 | | |
| 1459 | | |
| 1460 | | |
| 1461 | | |
| 1462 | | |
| 1463 | 51× | if base.built_count() > KEPT_GRAPHS || base.built_atoms() > kept_atoms { |
| 1464 | 0× | shed += base.shed_graphs(|b| self.store_blob(b))?; |
| 1465 | 51× | } |
| 1466 | 51× | kept.insert(gc.sha.clone(), (base, tree)); |
| 1467 | 51× | kept_order.push_back(gc.sha.clone()); |
| 1468 | 51× | if kept_order.len() > max_kept { |
| 1469 | 0× | if let Some(old) = kept_order.pop_front() { |
| 1470 | 0× | kept.remove(&old); |
| 1471 | 0× | } |
| 1472 | 51× | } |
| 1473 | 37× | } |
| 1474 | 88× | last = Some(gc.sha.clone()); |
| 1475 | 88× | report.commits += 1; |
| 1476 | 88× | lap(5, &mut clock); |
| 1477 | 88× | done += 1; |
| 1478 | 88× | if done.is_multiple_of(checkpoint_every) { |
| 1479 | 0× | self.checkpoint_import(&heads_now, done)?; |
| 1480 | 88× | } |
| 1481 | | |
| 1482 | | |
| 1483 | | |
| 1484 | 88× | let before_trim = if trace { rss_mb() } else { 0 }; |
| 1485 | 88× | if gc.parents.len() > 1 { |
| 1486 | 24× | crate::alloc_stats::release_freed_memory(); |
| 1487 | 64× | } |
| 1488 | 88× | if trace { |
| 1489 | 0× | let built = kept |
| 1490 | 0× | .get(gc.sha.as_str()) |
| 1491 | 0× | .map(|(st, _)| st.built_count()) |
| 1492 | 0× | .unwrap_or(0); |
| 1493 | 0× | let (edits, most) = kept |
| 1494 | 0× | .get(gc.sha.as_str()) |
| 1495 | 0× | .map(|(st, _)| st.deferred_edit_count()) |
| 1496 | 0× | .unwrap_or((0, 0)); |
| 1497 | 0× | let atoms = kept |
| 1498 | 0× | .get(gc.sha.as_str()) |
| 1499 | 0× | .map(|(st, _)| st.built_atoms()) |
| 1500 | 0× | .unwrap_or(0); |
| 1501 | 0× | eprintln!( |
| 1502 | | "trace {done} {} parents={} base={base_how} merged={merged} built={built} atoms={atoms} kept={} shed={shed} rss_mb={} before_trim_mb={before_trim} deferred_edits={edits} most={most} need={need_n} merge={merge_how} ms={}", |
| 1503 | 0× | &gc.sha[..12.min(gc.sha.len())], |
| 1504 | 0× | gc.parents.len(), |
| 1505 | 0× | kept.len(), |
| 1506 | 0× | rss_mb(), |
| 1507 | 0× | commit_started.elapsed().as_millis() |
| 1508 | | ); |
| 1509 | 88× | } |
| 1510 | | } |
| 1511 | 96× | for (name, d) in [ |
| 1512 | 16× | "git import: bases", |
| 1513 | 16× | "git import: tree", |
| 1514 | 16× | "git import: render base", |
| 1515 | 16× | "git import: read blobs", |
| 1516 | 16× | "git import: build change", |
| 1517 | 16× | "git import: store change", |
| 1518 | 16× | ] |
| 1519 | 16× | .iter() |
| 1520 | 16× | .zip(spent) |
| 1521 | 96× | { |
| 1522 | 96× | crate::timing::record(name, d); |
| 1523 | 96× | } |
| 1524 | 16× | crate::timing::report_totals(); |
| 1525 | 16× | if crate::timing::enabled() { |
| 1526 | 0× | eprintln!( |
| 1527 | 0× | "timing git import: {rebuilt} bases rebuilt from history, {shed} graphs shed" |
| 1528 | 0× | ); |
| 1529 | 16× | } |
| 1530 | |
|
| 1531 | | |
| 1532 | 16× | report.heads = old_heads.len(); |
| 1533 | 16× | if last.is_none() { |
| 1534 | 1× | return Ok(report); |
| 1535 | 15× | } |
| 1536 | 15× | let heads: Vec<ChangeHash> = heads_now.into_iter().collect(); |
| 1537 | 15× | report.heads = heads.len(); |
| 1538 | 15× | let (op_ref, op) = self.head_op()?; |
| 1539 | 15× | let mut view = op.view.clone(); |
| 1540 | 15× | let old_state_ref = view.bookmarks.get(&self.config.bookmark).copied(); |
| 1541 | 15× | let state_ref = self.store.put_state(&StateObject::new(heads.clone()))?; |
| 1542 | 15× | view.bookmarks |
| 1543 | 15× | .insert(self.config.bookmark.clone(), state_ref); |
| 1544 | 15× | self.record_op(&[op_ref], command, view)?; |
| 1545 | | |
| 1546 | | |
| 1547 | 15× | let old_known = old_heads.is_empty() |
| 1548 | 2× | || old_state_ref.is_some_and(|s| known.exported.values().any(|e| *e == s)); |
| 1549 | 15× | if let (Some(tip), true) = (commits.last(), old_known) { |
| 1550 | 15× | self.record_git_state(git_dir, &state_ref, &tip.sha)?; |
| 1551 | 0× | } |
| 1552 | | |
| 1553 | 15× | if old_heads.is_empty() { |
| 1554 | 14× | self.checkout(&heads, true, &format!("{command} (checkout)"))?; |
| 1555 | | } else { |
| 1556 | 1× | let snap = self.snapshot(command)?; |
| 1557 | 1× | let clean = snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty(); |
| 1558 | 1× | let mut base = snap.base.maximal(&snap.wc.deps); |
| 1559 | 1× | base.sort(); |
| 1560 | 1× | let mut sorted_old = old_heads.clone(); |
| 1561 | 1× | sorted_old.sort(); |
| 1562 | | |
| 1563 | | |
| 1564 | 1× | if clean && (base == sorted_old || base.is_empty()) { |
| 1565 | 1× | self.checkout(&heads, false, &format!("{command} (checkout)"))?; |
| 1566 | 0× | } |
| 1567 | | } |
| 1568 | 15× | Ok(report) |
| 1569 | 16× | } |
| 1570 | |
|
| 1571 | | |
| 1572 | | |
| 1573 | | |
| 1574 | 1× | fn add_git_ancestors( |
| 1575 | 1× | &self, |
| 1576 | 1× | st: &mut State, |
| 1577 | 1× | git_dir: &Path, |
| 1578 | 1× | args: &[&str], |
| 1579 | 1× | known: &GitKnown, |
| 1580 | 1× | ) -> Result<()> { |
| 1581 | 1× | self.add_git_ancestors_touching(st, git_dir, args, known, &mut HashSet::new()) |
| 1582 | 1× | } |
| 1583 | |
|
| 1584 | | |
| 1585 | | |
| 1586 | 25× | fn add_git_ancestors_touching( |
| 1587 | 25× | &self, |
| 1588 | 25× | st: &mut State, |
| 1589 | 25× | git_dir: &Path, |
| 1590 | 25× | args: &[&str], |
| 1591 | 25× | known: &GitKnown, |
| 1592 | 25× | touched: &mut HashSet<NodeId>, |
| 1593 | 25× | ) -> Result<()> { |
| 1594 | 25× | let mut full = vec!["rev-list"]; |
| 1595 | 25× | full.extend(args); |
| 1596 | 25× | let out = git(git_dir, &full)?; |
| 1597 | 32× | let mut add = |st: &mut State, h: &ChangeHash| -> Result<()> { |
| 1598 | 32× | if st.contains(h) { |
| 1599 | 2× | return Ok(()); |
| 1600 | 30× | } |
| 1601 | 30× | if !st.is_history_only() { |
| 1602 | 0× | let c = self.store.get_change(h)?; |
| 1603 | 0× | touched.extend(c.edits.iter().map(|e| e.node())); |
| 1604 | 0× | touched.extend(c.tree_ops.iter().map(|o| o.node())); |
| 1605 | 0× | st.add_unchecked(c); |
| 1606 | 0× | return Ok(()); |
| 1607 | 30× | } |
| 1608 | | |
| 1609 | | |
| 1610 | 30× | let (skeleton, texts) = self.store.get_change_lazy(h)?; |
| 1611 | 32× | touched.extend(skeleton.edits.iter().map(|e| e.node())); |
| 1612 | 30× | touched.extend(skeleton.tree_ops.iter().map(|o| o.node())); |
| 1613 | 30× | let built = skeleton |
| 1614 | 30× | .edits |
| 1615 | 30× | .iter() |
| 1616 | 32× | .any(|e| !matches!(e, Edit::Replace { .. }) && st.is_built(&e.node())); |
| 1617 | 30× | if built { |
| 1618 | 4× | let full = self.store.get_change(h)?; |
| 1619 | 4× | st.add_known_mixed(*h, full, skeleton, texts); |
| 1620 | 26× | } else { |
| 1621 | 26× | st.add_known(*h, skeleton, texts); |
| 1622 | 26× | } |
| 1623 | 30× | Ok(()) |
| 1624 | 32× | }; |
| 1625 | 30× | for sha in out.lines() { |
| 1626 | 30× | if let Some(h) = known.imported.get(sha) { |
| 1627 | 29× | add(st, h)?; |
| 1628 | 1× | } else if let Some(state_ref) = known.exported.get(sha) { |
| 1629 | 1× | let heads = self.store.get_state(state_ref)?.heads; |
| 1630 | 3× | for h in self.negotiate(&[], &heads)? { |
| 1631 | 3× | add(st, &h)?; |
| 1632 | | } |
| 1633 | 0× | } |
| 1634 | | } |
| 1635 | 25× | Ok(()) |
| 1636 | 25× | } |
| 1637 | |
|
| 1638 | | |
| 1639 | | |
| 1640 | | |
| 1641 | | |
| 1642 | | |
| 1643 | | |
| 1644 | 0× | pub fn recover_git_import(&self, progress: &dyn Fn(&str)) -> Result<(usize, usize)> { |
| 1645 | | |
| 1646 | | |
| 1647 | | |
| 1648 | 0× | let min_age = std::env::var("OXY_RECOVER_MIN_AGE") |
| 1649 | 0× | .ok() |
| 1650 | 0× | .and_then(|v| v.parse().ok()) |
| 1651 | 0× | .unwrap_or(300); |
| 1652 | 0× | self.store |
| 1653 | 0× | .adopt_unfinished_packs(std::time::Duration::from_secs(min_age), progress)?; |
| 1654 | 0× | let all = self |
| 1655 | 0× | .store |
| 1656 | 0× | .list() |
| 1657 | 0× | .map_err(|e| RepoError::Msg(format!("listing the store: {e}")))?; |
| 1658 | 0× | let total = all.len(); |
| 1659 | 0× | let mut found: HashMap<ChangeHash, Vec<ChangeHash>> = HashMap::new(); |
| 1660 | 0× | for (i, r) in all.iter().enumerate() { |
| 1661 | 0× | if i.is_multiple_of(100_000) { |
| 1662 | 0× | progress(&format!( |
| 1663 | 0× | "{i} of {total} objects read, {} imported changes", |
| 1664 | 0× | found.len() |
| 1665 | 0× | )); |
| 1666 | 0× | } |
| 1667 | 0× | let Ok(meta) = self.store.get_change_meta(r) else { |
| 1668 | 0× | continue; |
| 1669 | | }; |
| 1670 | 0× | if meta.message.lines().any(|l| l.starts_with("Git-Commit: ")) { |
| 1671 | 0× | found.insert(*r, meta.deps.clone()); |
| 1672 | 0× | } |
| 1673 | | } |
| 1674 | 0× | if found.is_empty() { |
| 1675 | 0× | return fail("no imported Git commit's change in this store"); |
| 1676 | 0× | } |
| 1677 | 0× | let depended: HashSet<ChangeHash> = found.values().flatten().copied().collect(); |
| 1678 | 0× | let mut heads: Vec<ChangeHash> = found |
| 1679 | 0× | .keys() |
| 1680 | 0× | .filter(|h| !depended.contains(h)) |
| 1681 | 0× | .copied() |
| 1682 | 0× | .collect(); |
| 1683 | | |
| 1684 | 0× | heads.extend(self.bookmark_heads().unwrap_or_default()); |
| 1685 | 0× | let st = self.load_state_history(&heads)?; |
| 1686 | 0× | let mut heads = st.maximal(&heads); |
| 1687 | 0× | heads.sort(); |
| 1688 | 0× | heads.dedup(); |
| 1689 | 0× | let (op_ref, op) = self.head_op()?; |
| 1690 | 0× | let mut view = op.view.clone(); |
| 1691 | 0× | let state_ref = self.store.put_state(&StateObject::new(heads.clone()))?; |
| 1692 | 0× | view.bookmarks |
| 1693 | 0× | .insert(self.config.bookmark.clone(), state_ref); |
| 1694 | 0× | self.record_op(&[op_ref], "oxy git recover", view)?; |
| 1695 | 0× | Ok((found.len(), heads.len())) |
| 1696 | 0× | } |
| 1697 | |
|
| 1698 | | |
| 1699 | | |
| 1700 | 0× | fn checkpoint_import(&self, heads: &BTreeSet<ChangeHash>, done: usize) -> Result<()> { |
| 1701 | 0× | let heads: Vec<ChangeHash> = heads.iter().copied().collect(); |
| 1702 | 0× | let (op_ref, op) = self.head_op()?; |
| 1703 | 0× | let mut view = op.view.clone(); |
| 1704 | 0× | let state_ref = self.store.put_state(&StateObject::new(heads))?; |
| 1705 | 0× | view.bookmarks |
| 1706 | 0× | .insert(self.config.bookmark.clone(), state_ref); |
| 1707 | 0× | self.record_op( |
| 1708 | 0× | &[op_ref], |
| 1709 | 0× | &format!("oxy git import (checkpoint after {done} commits)"), |
| 1710 | 0× | view, |
| 1711 | 0× | )?; |
| 1712 | 0× | Ok(()) |
| 1713 | 0× | } |
| 1714 | |
|
| 1715 | | |
| 1716 | | |
| 1717 | 20× | fn git_known(&self, git_dir: &Path) -> Result<GitKnown> { |
| 1718 | 20× | let imported = self.imported_commits()?; |
| 1719 | 20× | let marks_dir = self.git_marks_dir(git_dir)?; |
| 1720 | 20× | let oxy_marks = Self::read_oxy_marks(&marks_dir.join("oxy-marks"))?; |
| 1721 | 20× | let mut sha_of_mark: HashMap<u64, String> = HashMap::new(); |
| 1722 | 20× | if let Ok(text) = fs::read_to_string(marks_dir.join("git-marks")) { |
| 1723 | 4× | for line in text.lines() { |
| 1724 | 4× | if let Some((m, sha)) = line.split_once(' ') { |
| 1725 | 4× | if let Ok(m) = m.trim_start_matches(':').parse::<u64>() { |
| 1726 | 4× | sha_of_mark.insert(m, sha.trim().to_string()); |
| 1727 | 4× | } |
| 1728 | 0× | } |
| 1729 | | } |
| 1730 | 16× | } |
| 1731 | 20× | let mut exported = HashMap::new(); |
| 1732 | 20× | for (state_ref, m) in oxy_marks { |
| 1733 | 4× | if let Some(sha) = sha_of_mark.get(&m) { |
| 1734 | 4× | exported.insert(sha.clone(), state_ref); |
| 1735 | 4× | } |
| 1736 | | } |
| 1737 | 20× | for (state_ref, sha) in Self::read_git_states(&marks_dir.join("git-states"))? { |
| 1738 | 7× | exported.entry(sha).or_insert(state_ref); |
| 1739 | 7× | } |
| 1740 | 20× | Ok(GitKnown { imported, exported }) |
| 1741 | 20× | } |
| 1742 | |
|
| 1743 | 8× | fn git_author(&self, c: &Change) -> String { |
| 1744 | 8× | self.git_author_key(&c.author) |
| 1745 | 8× | } |
| 1746 | |
|
| 1747 | | #[allow(clippy::too_many_arguments)] |
| 1748 | 19× | fn write_commit( |
| 1749 | 19× | &self, |
| 1750 | 19× | out: &mut Vec<u8>, |
| 1751 | 19× | reference: &str, |
| 1752 | 19× | mark: u64, |
| 1753 | 19× | author: &str, |
| 1754 | 19× | secs: u64, |
| 1755 | 19× | message: &str, |
| 1756 | 19× | parents: &[String], |
| 1757 | 19× | snapshot: &Snapshot, |
| 1758 | 19× | ) -> Result<()> { |
| 1759 | 19× | out.extend_from_slice(format!("commit {reference}\nmark :{mark}\nauthor {author} {secs} +0000\ncommitter {author} {secs} +0000\ndata {}\n{message}\n", message.len()).as_bytes()); |
| 1760 | 19× | for (i, p) in parents.iter().enumerate() { |
| 1761 | 12× | out.extend_from_slice( |
| 1762 | 12× | format!("{} {p}\n", if i == 0 { "from" } else { "merge" }).as_bytes(), |
| 1763 | | ); |
| 1764 | | } |
| 1765 | 19× | out.extend_from_slice(b"deleteall\n"); |
| 1766 | 19× | let has_gitmodules = snapshot.files.contains_key(".gitmodules"); |
| 1767 | 58× | for (path, f) in &snapshot.files { |
| 1768 | 58× | if path == ".oxymodules" { |
| 1769 | | |
| 1770 | 1× | if !has_gitmodules { |
| 1771 | 0× | if let FileContent::Text { bytes, .. } = &f.content { |
| 1772 | 0× | let gm = crate::subrepo::Modules::parse(&String::from_utf8_lossy(bytes)) |
| 1773 | 0× | .to_gitmodules(); |
| 1774 | 0× | if !gm.is_empty() { |
| 1775 | 0× | out.extend_from_slice( |
| 1776 | 0× | format!("M 100644 inline .gitmodules\ndata {}\n", gm.len()) |
| 1777 | 0× | .as_bytes(), |
| 1778 | 0× | ); |
| 1779 | 0× | out.extend_from_slice(gm.as_bytes()); |
| 1780 | 0× | out.push(b'\n'); |
| 1781 | 0× | } |
| 1782 | 0× | } |
| 1783 | 1× | } |
| 1784 | 1× | continue; |
| 1785 | 57× | } |
| 1786 | 57× | if f.kind == NodeKind::Subrepo { |
| 1787 | 1× | if let FileContent::Opaque { blobs } = &f.content { |
| 1788 | 1× | if let Some(b) = blobs.first() { |
| 1789 | 1× | match crate::subrepo::Pin::decode(&self.read_blob(b)?)? { |
| 1790 | 1× | crate::subrepo::Pin::Git { sha, .. } => { |
| 1791 | 1× | out.extend_from_slice(format!("M 160000 {sha} {path}\n").as_bytes()) |
| 1792 | | } |
| 1793 | 0× | crate::subrepo::Pin::Oxy { .. } => self |
| 1794 | 0× | .git_export_skipped |
| 1795 | 0× | .set(self.git_export_skipped.get() + 1), |
| 1796 | | } |
| 1797 | 0× | } |
| 1798 | 0× | } |
| 1799 | 1× | continue; |
| 1800 | 56× | } |
| 1801 | 56× | let (mode, bytes): (&str, Vec<u8>) = match (&f.kind, &f.content) { |
| 1802 | 13× | (NodeKind::Dir, _) => continue, |
| 1803 | 0× | (NodeKind::Symlink, FileContent::Opaque { blobs }) => ( |
| 1804 | 0× | "120000", |
| 1805 | 0× | blobs |
| 1806 | 0× | .first() |
| 1807 | 0× | .map(|b| self.read_blob(b)) |
| 1808 | 0× | .transpose()? |
| 1809 | 0× | .unwrap_or_default(), |
| 1810 | | ), |
| 1811 | 40× | (_, FileContent::Text { bytes, .. }) => { |
| 1812 | 40× | (if f.mode.exec { "100755" } else { "100644" }, bytes.clone()) |
| 1813 | | } |
| 1814 | 3× | (_, FileContent::Opaque { blobs }) => ( |
| 1815 | 3× | if f.mode.exec { "100755" } else { "100644" }, |
| 1816 | 3× | blobs |
| 1817 | 3× | .first() |
| 1818 | 3× | .map(|b| self.read_blob(b)) |
| 1819 | 3× | .transpose()? |
| 1820 | 3× | .unwrap_or_default(), |
| 1821 | | ), |
| 1822 | 0× | (_, FileContent::Dir) => continue, |
| 1823 | | }; |
| 1824 | 43× | out.extend_from_slice( |
| 1825 | 43× | format!("M {mode} inline {path}\ndata {}\n", bytes.len()).as_bytes(), |
| 1826 | | ); |
| 1827 | 43× | out.extend_from_slice(&bytes); |
| 1828 | 43× | out.push(b'\n'); |
| 1829 | | } |
| 1830 | 19× | out.push(b'\n'); |
| 1831 | 19× | Ok(()) |
| 1832 | 19× | } |
| 1833 | | } |
| 1834 | |
|
| 1835 | |
|
| 1836 | | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 1837 | | pub struct GitVerifyReport { |
| 1838 | | pub commits: usize, |
| 1839 | | |
| 1840 | | pub skipped: usize, |
| 1841 | | pub files_compared: usize, |
| 1842 | | |
| 1843 | | pub mismatches: Vec<(String, String)>, |
| 1844 | | |
| 1845 | | |
| 1846 | | pub not_yet: usize, |
| 1847 | | } |
| 1848 | |
|
| 1849 | | impl Repo { |
| 1850 | | |
| 1851 | | |
| 1852 | | |
| 1853 | | |
| 1854 | | |
| 1855 | 10× | pub fn verify_git_import(&self, git_dir: &Path, branch: &str) -> Result<GitVerifyReport> { |
| 1856 | 10× | self.verify_git_import_last(git_dir, branch, None, 50) |
| 1857 | 10× | } |
| 1858 | |
|
| 1859 | | |
| 1860 | | |
| 1861 | | |
| 1862 | | |
| 1863 | | |
| 1864 | 10× | pub fn verify_git_import_last( |
| 1865 | 10× | &self, |
| 1866 | 10× | git_dir: &Path, |
| 1867 | 10× | branch: &str, |
| 1868 | 10× | last: Option<usize>, |
| 1869 | 10× | max: usize, |
| 1870 | 10× | ) -> Result<GitVerifyReport> { |
| 1871 | 10× | let mut rep = GitVerifyReport::default(); |
| 1872 | 10× | let mut commits = list_commits(git_dir, branch)?; |
| 1873 | 10× | let imported = self.imported_commits()?; |
| 1874 | | |
| 1875 | | |
| 1876 | | |
| 1877 | 10× | if let Some(end) = commits.iter().rposition(|c| imported.contains_key(&c.sha)) { |
| 1878 | 10× | let rest = commits.len() - end - 1; |
| 1879 | 10× | if rest > 0 { |
| 1880 | 0× | commits.truncate(end + 1); |
| 1881 | 10× | } |
| 1882 | 10× | rep.not_yet = rest; |
| 1883 | 0× | } |
| 1884 | 10× | let mut cat = CatFile::start(git_dir)?; |
| 1885 | | |
| 1886 | 10× | let mut closure: HashMap<String, Arc<BTreeSet<ChangeHash>>> = HashMap::new(); |
| 1887 | | |
| 1888 | 10× | let mut verified_graphs: HashMap<[u8; 32], String> = HashMap::new(); |
| 1889 | 10× | let mut verified_blobs: HashMap<ObjectRef, String> = HashMap::new(); |
| 1890 | | |
| 1891 | | |
| 1892 | | |
| 1893 | 10× | let mut waiting: HashMap<&str, usize> = HashMap::new(); |
| 1894 | | |
| 1895 | | |
| 1896 | | |
| 1897 | | |
| 1898 | | |
| 1899 | 10× | let mut children: HashMap<&str, usize> = HashMap::new(); |
| 1900 | 63× | for c in &commits { |
| 1901 | 63× | if let Some(p1) = c.parents.first() { |
| 1902 | 51× | *waiting.entry(p1.as_str()).or_default() += 1; |
| 1903 | 51× | } |
| 1904 | 69× | for p in &c.parents { |
| 1905 | 69× | *children.entry(p.as_str()).or_default() += 1; |
| 1906 | 69× | } |
| 1907 | | } |
| 1908 | 10× | let mut kept: HashMap<String, State> = HashMap::new(); |
| 1909 | 10× | let mut kept_order: std::collections::VecDeque<String> = std::collections::VecDeque::new(); |
| 1910 | 63× | for c in &commits { |
| 1911 | 63× | rep.commits += 1; |
| 1912 | | |
| 1913 | | |
| 1914 | 63× | let mut set: BTreeSet<ChangeHash> = BTreeSet::new(); |
| 1915 | 69× | for (i, p) in c.parents.iter().enumerate() { |
| 1916 | 69× | let left = children.get_mut(p.as_str()).map(|n| { |
| 1917 | 69× | *n -= 1; |
| 1918 | 69× | *n |
| 1919 | 69× | }); |
| 1920 | 69× | let ps = if left == Some(0) { |
| 1921 | 53× | closure.remove(p) |
| 1922 | | } else { |
| 1923 | 16× | closure.get(p).cloned() |
| 1924 | | }; |
| 1925 | 69× | if let Some(ps) = ps { |
| 1926 | 69× | if i == 0 && set.is_empty() { |
| 1927 | 51× | set = Arc::try_unwrap(ps).unwrap_or_else(|a| (*a).clone()); |
| 1928 | 18× | } else { |
| 1929 | 18× | set.extend(ps.iter().copied()); |
| 1930 | 18× | } |
| 1931 | 0× | } |
| 1932 | | } |
| 1933 | 63× | match imported.get(&c.sha) { |
| 1934 | 63× | Some(h) => { |
| 1935 | 63× | set.insert(*h); |
| 1936 | 63× | } |
| 1937 | 0× | None => { |
| 1938 | 0× | rep.mismatches.push(( |
| 1939 | 0× | c.sha.clone(), |
| 1940 | 0× | "no imported change carries this commit".into(), |
| 1941 | 0× | )); |
| 1942 | 0× | } |
| 1943 | | } |
| 1944 | 63× | let set = Arc::new(set); |
| 1945 | 63× | if children.get(c.sha.as_str()).is_some_and(|n| *n > 0) { |
| 1946 | 53× | closure.insert(c.sha.clone(), set.clone()); |
| 1947 | 53× | } |
| 1948 | 63× | if max > 0 && rep.mismatches.len() >= max { |
| 1949 | 0× | break; |
| 1950 | 63× | } |
| 1951 | 63× | if last.is_some_and(|n| rep.commits + n <= commits.len()) { |
| 1952 | 0× | rep.skipped += 1; |
| 1953 | 0× | continue; |
| 1954 | 63× | } |
| 1955 | 63× | let heads: Vec<ChangeHash> = set.iter().copied().collect(); |
| 1956 | | |
| 1957 | | |
| 1958 | | |
| 1959 | 63× | let from_parent = c.parents.first().and_then(|p1| { |
| 1960 | 51× | let left = waiting.get_mut(p1.as_str()).map(|n| { |
| 1961 | 51× | *n -= 1; |
| 1962 | 51× | *n |
| 1963 | 51× | }); |
| 1964 | 51× | if left == Some(0) { |
| 1965 | 53× | kept_order.retain(|k| k != p1); |
| 1966 | 37× | kept.remove(p1.as_str()) |
| 1967 | | } else { |
| 1968 | 14× | kept.get(p1.as_str()).cloned() |
| 1969 | | } |
| 1970 | 51× | }); |
| 1971 | 63× | let state = match from_parent { |
| 1972 | 51× | Some(mut st) => { |
| 1973 | | |
| 1974 | | |
| 1975 | | |
| 1976 | 223× | for h in set.iter() { |
| 1977 | 223× | if !st.contains(h) { |
| 1978 | 72× | let (change, texts) = self.store.get_change_lazy(h)?; |
| 1979 | 72× | st.add_known(*h, change, texts); |
| 1980 | 151× | } |
| 1981 | | } |
| 1982 | 51× | st |
| 1983 | | } |
| 1984 | 12× | None => self.load_state_history(&heads)?, |
| 1985 | | }; |
| 1986 | 63× | let view = state.tree().view(); |
| 1987 | 63× | let tree = list_tree(git_dir, &c.sha)?; |
| 1988 | 63× | let short = &c.sha[..12.min(c.sha.len())]; |
| 1989 | 63× | let mut differ = |what: String| rep.mismatches.push((c.sha.clone(), what)); |
| 1990 | 63× | let mut conflicted: Vec<String> = |
| 1991 | 63× | view.conflicts.iter().map(|c| format!("{c:?}")).collect(); |
| 1992 | 63× | let mut need: Vec<NodeId> = Vec::new(); |
| 1993 | 145× | for (path, (mode, sha)) in &tree { |
| 1994 | 145× | let Some(e) = view.entries.get(path) else { |
| 1995 | 0× | continue; |
| 1996 | | }; |
| 1997 | 145× | if mode != "160000" |
| 1998 | 145× | && mode != "120000" |
| 1999 | 145× | && e.kind == NodeKind::File |
| 2000 | 145× | && !e.mode.opaque |
| 2001 | | { |
| 2002 | 134× | let known = state |
| 2003 | 134× | .graph_key(&e.node) |
| 2004 | 134× | .is_some_and(|k| verified_graphs.get(&k) == Some(sha)); |
| 2005 | 134× | if !known { |
| 2006 | 50× | need.push(e.node); |
| 2007 | 84× | } |
| 2008 | 11× | } |
| 2009 | | } |
| 2010 | | |
| 2011 | 63× | let keys: HashMap<NodeId, [u8; 32]> = need |
| 2012 | 63× | .iter() |
| 2013 | 63× | .filter_map(|n| Some((*n, state.graph_key(n)?))) |
| 2014 | 63× | .collect(); |
| 2015 | 63× | let need_set: HashSet<NodeId> = need.iter().copied().collect(); |
| 2016 | 63× | let graphs = self.build_graphs_detached(&state, &need)?; |
| 2017 | 145× | for (path, (mode, sha)) in &tree { |
| 2018 | 145× | let Some(e) = view.entries.get(path) else { |
| 2019 | 0× | differ(format!("{short}: {path} is missing")); |
| 2020 | 0× | continue; |
| 2021 | | }; |
| 2022 | 145× | rep.files_compared += 1; |
| 2023 | 145× | match mode.as_str() { |
| 2024 | 145× | "160000" => { |
| 2025 | 0× | let blobs = state.live_blobs(&e.node); |
| 2026 | 0× | match (e.kind, blobs.as_slice()) { |
| 2027 | 0× | (NodeKind::Subrepo, [b]) => { |
| 2028 | 0× | let pin = crate::subrepo::Pin::decode(&self.read_blob(b)?)?; |
| 2029 | 0× | if !matches!(&pin, crate::subrepo::Pin::Git { sha: s, .. } if s == sha) |
| 2030 | 0× | { |
| 2031 | 0× | differ(format!("{short}: {path} pins another commit")); |
| 2032 | 0× | } |
| 2033 | | } |
| 2034 | 0× | (kind, _) => differ(format!( |
| 2035 | 0× | "{short}: {path} is not one submodule pin: {kind:?} with {} pins", |
| 2036 | 0× | blobs.len() |
| 2037 | 0× | )), |
| 2038 | | } |
| 2039 | | } |
| 2040 | 145× | "120000" => { |
| 2041 | 0× | let target = cat.read(sha)?; |
| 2042 | 0× | let blobs = state.live_blobs(&e.node); |
| 2043 | 0× | match (e.kind, blobs.as_slice()) { |
| 2044 | 0× | (NodeKind::Symlink, [b]) if self.read_blob(b)? == target => {} |
| 2045 | 0× | _ => differ(format!("{short}: symlink {path} differs")), |
| 2046 | | } |
| 2047 | | } |
| 2048 | | _ => { |
| 2049 | 145× | if e.mode.exec != (mode == "100755") { |
| 2050 | 0× | differ(format!("{short}: {path} executable bit differs")); |
| 2051 | 145× | } |
| 2052 | 145× | if e.kind == NodeKind::File && !e.mode.opaque { |
| 2053 | 134× | if !need_set.contains(&e.node) { |
| 2054 | | |
| 2055 | 84× | continue; |
| 2056 | 50× | } |
| 2057 | 50× | let (have, m) = match graphs.get(&e.node) { |
| 2058 | 50× | Some(g) => state.render_graph(g), |
| 2059 | 0× | None => (Vec::new(), Default::default()), |
| 2060 | | }; |
| 2061 | 50× | if m.is_conflicted() { |
| 2062 | | |
| 2063 | 0× | let mut detail = state.clone(); |
| 2064 | 0× | self.load_graphs_of(&mut detail, &[e.node])?; |
| 2065 | 0× | let who: Vec<String> = detail |
| 2066 | 0× | .conflict_regions(&e.node) |
| 2067 | 0× | .contributors |
| 2068 | 0× | .iter() |
| 2069 | 0× | .filter_map(|h| state.get(h)) |
| 2070 | 0× | .map(|m| { |
| 2071 | 0× | let sha = m |
| 2072 | 0× | .message |
| 2073 | 0× | .lines() |
| 2074 | 0× | .rev() |
| 2075 | 0× | .find_map(|l| l.strip_prefix("Git-Commit: ")) |
| 2076 | 0× | .unwrap_or("?"); |
| 2077 | 0× | format!( |
| 2078 | | "{} {:?}", |
| 2079 | 0× | &sha[..12.min(sha.len())], |
| 2080 | 0× | m.message.lines().next().unwrap_or("") |
| 2081 | | ) |
| 2082 | 0× | }) |
| 2083 | 0× | .collect(); |
| 2084 | 0× | conflicted.push(format!("{path} between {}", who.join(" and "))); |
| 2085 | 50× | } |
| 2086 | 50× | let want = cat.read(sha)?; |
| 2087 | 50× | if have != want { |
| 2088 | 0× | differ(format!( |
| 2089 | 0× | "{short}: {path} differs: {}", |
| 2090 | 0× | first_difference(&have, &want) |
| 2091 | 0× | )); |
| 2092 | 50× | } else if !m.is_conflicted() { |
| 2093 | 50× | if let Some(k) = keys.get(&e.node) { |
| 2094 | 50× | verified_graphs.insert(*k, sha.clone()); |
| 2095 | 50× | } |
| 2096 | 0× | } |
| 2097 | 50× | continue; |
| 2098 | 11× | } |
| 2099 | 11× | let blobs = state.live_blobs(&e.node); |
| 2100 | 11× | let [b] = blobs.as_slice() else { |
| 2101 | 0× | differ(format!("{short}: {path} is not one file")); |
| 2102 | 0× | continue; |
| 2103 | | }; |
| 2104 | 11× | if verified_blobs.get(b) == Some(sha) { |
| 2105 | 6× | continue; |
| 2106 | 5× | } |
| 2107 | 5× | if self.read_blob(b)? != cat.read(sha)? { |
| 2108 | 0× | differ(format!("{short}: {path} differs")); |
| 2109 | 5× | } else { |
| 2110 | 5× | verified_blobs.insert(*b, sha.clone()); |
| 2111 | 5× | } |
| 2112 | | } |
| 2113 | | } |
| 2114 | | } |
| 2115 | 63× | if !conflicted.is_empty() { |
| 2116 | 0× | differ(format!( |
| 2117 | 0× | "{short}: the state is conflicted: {}", |
| 2118 | 0× | conflicted.join("; ") |
| 2119 | 0× | )); |
| 2120 | 63× | } |
| 2121 | | |
| 2122 | | |
| 2123 | 145× | let has_submodules = tree.values().any(|(m, _)| m == "160000"); |
| 2124 | 186× | for (path, e) in view.entries.iter() { |
| 2125 | 186× | if e.kind != NodeKind::Dir |
| 2126 | 145× | && !tree.contains_key(path) |
| 2127 | 0× | && !(has_submodules && path == ".oxymodules") |
| 2128 | 0× | { |
| 2129 | 0× | differ(format!("{short}: {path} is extra")); |
| 2130 | 186× | } |
| 2131 | | } |
| 2132 | 63× | if waiting.get(c.sha.as_str()).is_some_and(|n| *n > 0) { |
| 2133 | 37× | kept.insert(c.sha.clone(), state); |
| 2134 | 37× | kept_order.push_back(c.sha.clone()); |
| 2135 | 37× | if kept_order.len() > 8 { |
| 2136 | 0× | if let Some(old) = kept_order.pop_front() { |
| 2137 | 0× | kept.remove(&old); |
| 2138 | 0× | } |
| 2139 | 37× | } |
| 2140 | 26× | } |
| 2141 | 63× | if max > 0 && rep.mismatches.len() >= max { |
| 2142 | 0× | break; |
| 2143 | 63× | } |
| 2144 | | } |
| 2145 | 10× | Ok(rep) |
| 2146 | 10× | } |
| 2147 | | } |
| 2148 | |
|
| 2149 | |
|
| 2150 | 0× | fn first_difference(have: &[u8], want: &[u8]) -> String { |
| 2151 | 0× | let (h, w): (Vec<&[u8]>, Vec<&[u8]>) = ( |
| 2152 | 0× | have.split(|b| *b == b'\n').collect(), |
| 2153 | 0× | want.split(|b| *b == b'\n').collect(), |
| 2154 | | ); |
| 2155 | 0× | let i = h |
| 2156 | 0× | .iter() |
| 2157 | 0× | .zip(&w) |
| 2158 | 0× | .position(|(a, b)| a != b) |
| 2159 | 0× | .unwrap_or(h.len().min(w.len())); |
| 2160 | 0× | let show = |l: Option<&&[u8]>| { |
| 2161 | 0× | l.map(|l| String::from_utf8_lossy(&l[..l.len().min(60)]).into_owned()) |
| 2162 | 0× | .unwrap_or_else(|| "(end)".into()) |
| 2163 | 0× | }; |
| 2164 | 0× | format!( |
| 2165 | | "line {}: has {:?}, Git has {:?} ({} lines, Git has {})", |
| 2166 | 0× | i + 1, |
| 2167 | 0× | show(h.get(i)), |
| 2168 | 0× | show(w.get(i)), |
| 2169 | 0× | h.len(), |
| 2170 | 0× | w.len() |
| 2171 | | ) |
| 2172 | 0× | } |
| 2173 | |
|
| 2174 | |
|
| 2175 | | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 2176 | | pub struct GitGraphReport { |
| 2177 | | |
| 2178 | | pub commits: usize, |
| 2179 | | |
| 2180 | | pub branches: usize, |
| 2181 | | pub tags: usize, |
| 2182 | | |
| 2183 | | pub skipped_refs: Vec<String>, |
| 2184 | | |
| 2185 | | pub skipped_subrepos: usize, |
| 2186 | | } |
| 2187 | |
|
| 2188 | |
|
| 2189 | 7× | fn git_ref_ok(name: &str) -> bool { |
| 2190 | 7× | !name.is_empty() |
| 2191 | 7× | && !name.starts_with(['-', '/', '.']) |
| 2192 | 7× | && !name.ends_with(['/', '.']) |
| 2193 | 7× | && !name.contains("..") |
| 2194 | 7× | && !name.contains("//") |
| 2195 | 7× | && !name.ends_with(".lock") |
| 2196 | 7× | && name |
| 2197 | 7× | .bytes() |
| 2198 | 22× | .all(|b| b.is_ascii_alphanumeric() || b"-_./+".contains(&b)) |
| 2199 | 7× | } |
| 2200 | |
|
| 2201 | | impl Repo { |
| 2202 | | |
| 2203 | | |
| 2204 | | |
| 2205 | | |
| 2206 | | |
| 2207 | | |
| 2208 | | |
| 2209 | | |
| 2210 | | |
| 2211 | | |
| 2212 | | |
| 2213 | | |
| 2214 | | |
| 2215 | | |
| 2216 | | |
| 2217 | | |
| 2218 | | |
| 2219 | | |
| 2220 | | |
| 2221 | | |
| 2222 | 3× | pub fn git_export_graph(&self, git_dir: &Path) -> Result<GitGraphReport> { |
| 2223 | 3× | if !git_dir.join("HEAD").exists() { |
| 2224 | 2× | fs::create_dir_all(git_dir)?; |
| 2225 | 2× | git(git_dir, &["init", "--bare", "-q"])?; |
| 2226 | 1× | } |
| 2227 | 3× | let marks_dir = self.git_marks_dir(git_dir)?; |
| 2228 | 3× | let git_marks = marks_dir.join("git-marks"); |
| 2229 | 3× | let graph_marks = marks_dir.join("graph-marks"); |
| 2230 | 3× | let mut marks = Self::read_oxy_marks(&graph_marks)?; |
| 2231 | 3× | let mut next = marks.values().copied().max().unwrap_or(0) + 1; |
| 2232 | 3× | let mut report = GitGraphReport::default(); |
| 2233 | 3× | let mut stream: Vec<u8> = Vec::new(); |
| 2234 | |
|
| 2235 | 3× | let mut refs: Vec<(String, Vec<ChangeHash>)> = Vec::new(); |
| 2236 | 4× | for b in self.bookmarks()? { |
| 2237 | 4× | if git_ref_ok(&b.name) { |
| 2238 | 4× | refs.push((format!("refs/heads/{}", b.name), b.heads)); |
| 2239 | 4× | } else { |
| 2240 | 0× | report.skipped_refs.push(b.name); |
| 2241 | 0× | } |
| 2242 | | } |
| 2243 | 3× | let tags = self.tags()?; |
| 2244 | 3× | for t in &tags { |
| 2245 | 3× | if git_ref_ok(&t.name) { |
| 2246 | 3× | refs.push((format!("refs/tags/{}", t.name), t.heads.clone())); |
| 2247 | 3× | } else { |
| 2248 | 0× | report.skipped_refs.push(t.name.clone()); |
| 2249 | 0× | } |
| 2250 | | } |
| 2251 | |
|
| 2252 | | |
| 2253 | | |
| 2254 | 3× | let mut order: Vec<ChangeHash> = Vec::new(); |
| 2255 | 3× | let mut seen: HashSet<ChangeHash> = HashSet::new(); |
| 2256 | 3× | let mut metas: HashMap<ChangeHash, crate::state::ChangeMeta> = HashMap::new(); |
| 2257 | 7× | let mut roots: Vec<ChangeHash> = refs.iter().flat_map(|(_, h)| h.iter().copied()).collect(); |
| 2258 | 3× | roots.sort(); |
| 2259 | 3× | roots.dedup(); |
| 2260 | 7× | for root in roots { |
| 2261 | 7× | if !seen.insert(root) { |
| 2262 | 1× | continue; |
| 2263 | 6× | } |
| 2264 | 6× | let mut stack: Vec<(ChangeHash, bool)> = vec![(root, false)]; |
| 2265 | 24× | while let Some((h, done)) = stack.pop() { |
| 2266 | 18× | if done { |
| 2267 | 9× | order.push(h); |
| 2268 | 9× | continue; |
| 2269 | 9× | } |
| 2270 | 9× | stack.push((h, true)); |
| 2271 | 9× | let meta = self.store.get_change_meta(&h)?; |
| 2272 | 9× | let mut deps = meta.deps.clone(); |
| 2273 | 9× | deps.sort(); |
| 2274 | 9× | for d in deps.into_iter().rev() { |
| 2275 | 3× | if seen.insert(d) { |
| 2276 | 3× | stack.push((d, false)); |
| 2277 | 3× | } |
| 2278 | | } |
| 2279 | 9× | metas.insert(h, meta); |
| 2280 | | } |
| 2281 | | } |
| 2282 | |
|
| 2283 | 9× | for h in &order { |
| 2284 | 9× | if marks.contains_key(h) { |
| 2285 | 3× | continue; |
| 2286 | 6× | } |
| 2287 | 6× | let meta = &metas[h]; |
| 2288 | 6× | let snapshot = self.load_state(&[*h])?.materialize(); |
| 2289 | 6× | let mut message = meta.message.clone(); |
| 2290 | 6× | if !message.ends_with('\n') { |
| 2291 | 6× | message.push('\n'); |
| 2292 | 6× | } |
| 2293 | 6× | message.push_str(&format!( |
| 2294 | 6× | "\nChange-Id: {}\nOxy-Hash: {}\n", |
| 2295 | 6× | meta.change_id.to_hex(), |
| 2296 | 6× | h.to_hex() |
| 2297 | 6× | )); |
| 2298 | 6× | if snapshot.is_conflicted() { |
| 2299 | 0× | message.push_str("Oxy-Conflicted: true\n"); |
| 2300 | 6× | } |
| 2301 | 6× | let mut deps = meta.deps.clone(); |
| 2302 | 6× | deps.sort(); |
| 2303 | 6× | let parents: Vec<String> = deps |
| 2304 | 6× | .iter() |
| 2305 | 6× | .filter_map(|d| marks.get(d)) |
| 2306 | 6× | .map(|m| format!(":{m}")) |
| 2307 | 6× | .collect(); |
| 2308 | 6× | let mark = next; |
| 2309 | 6× | next += 1; |
| 2310 | 6× | if parents.is_empty() { |
| 2311 | 4× | |
| 2312 | 4× | |
| 2313 | 4× | stream.extend_from_slice(b"reset refs/oxy/export\n\n"); |
| 2314 | 4× | } |
| 2315 | 6× | self.write_commit( |
| 2316 | 6× | &mut stream, |
| 2317 | 6× | "refs/oxy/export", |
| 2318 | 6× | mark, |
| 2319 | 6× | &self.git_author_key(&meta.author), |
| 2320 | 6× | meta.hlc.wall_ms / 1000, |
| 2321 | 6× | &message, |
| 2322 | 6× | &parents, |
| 2323 | 6× | &snapshot, |
| 2324 | 0× | )?; |
| 2325 | 6× | marks.insert(*h, mark); |
| 2326 | 6× | report.commits += 1; |
| 2327 | | } |
| 2328 | |
|
| 2329 | 3× | let mut wanted: HashSet<String> = HashSet::new(); |
| 2330 | 7× | for (name, heads) in &refs { |
| 2331 | 7× | let mut heads = heads.clone(); |
| 2332 | 7× | heads.sort(); |
| 2333 | 7× | heads.dedup(); |
| 2334 | 7× | let target = match heads.as_slice() { |
| 2335 | 7× | [] => continue, |
| 2336 | 4× | [one] => format!(":{}", marks[one]), |
| 2337 | 3× | many => { |
| 2338 | | |
| 2339 | 3× | let key = hash_object( |
| 2340 | 3× | ObjectType::Chunk, |
| 2341 | 3× | many.iter() |
| 2342 | 6× | .map(|h| h.to_hex()) |
| 2343 | 3× | .collect::<Vec<_>>() |
| 2344 | 3× | .join(" ") |
| 2345 | 3× | .as_bytes(), |
| 2346 | | ); |
| 2347 | 3× | if let Some(m) = marks.get(&key) { |
| 2348 | 1× | format!(":{m}") |
| 2349 | | } else { |
| 2350 | 2× | let snapshot = self.load_state(many)?.materialize(); |
| 2351 | 4× | let newest = many.iter().map(|h| metas[h].hlc.wall_ms).max().unwrap_or(0); |
| 2352 | 2× | let mut message = format!( |
| 2353 | | "Merge of {} heads\n\nOxy-Heads: {}\n", |
| 2354 | 2× | many.len(), |
| 2355 | 2× | many.iter() |
| 2356 | 4× | .map(|h| h.to_hex()) |
| 2357 | 2× | .collect::<Vec<_>>() |
| 2358 | 2× | .join(" ") |
| 2359 | | ); |
| 2360 | 2× | if snapshot.is_conflicted() { |
| 2361 | 0× | message.push_str("Oxy-Conflicted: true\n"); |
| 2362 | 2× | } |
| 2363 | 2× | let parents: Vec<String> = |
| 2364 | 4× | many.iter().map(|h| format!(":{}", marks[h])).collect(); |
| 2365 | 2× | let mark = next; |
| 2366 | 2× | next += 1; |
| 2367 | 2× | self.write_commit( |
| 2368 | 2× | &mut stream, |
| 2369 | 2× | "refs/oxy/export", |
| 2370 | 2× | mark, |
| 2371 | 2× | "oxy <oxy@localhost>", |
| 2372 | 2× | newest / 1000, |
| 2373 | 2× | &message, |
| 2374 | 2× | &parents, |
| 2375 | 2× | &snapshot, |
| 2376 | 0× | )?; |
| 2377 | 2× | marks.insert(key, mark); |
| 2378 | 2× | format!(":{mark}") |
| 2379 | | } |
| 2380 | | } |
| 2381 | | }; |
| 2382 | 7× | stream.extend_from_slice(format!("reset {name}\nfrom {target}\n\n").as_bytes()); |
| 2383 | 7× | wanted.insert(name.clone()); |
| 2384 | 7× | if name.starts_with("refs/tags/") { |
| 2385 | 3× | report.tags += 1; |
| 2386 | 4× | } else { |
| 2387 | 4× | report.branches += 1; |
| 2388 | 4× | } |
| 2389 | | } |
| 2390 | 3× | stream.extend_from_slice(b"done\n"); |
| 2391 | 3× | run_fast_import(git_dir, &stream, &git_marks)?; |
| 2392 | 3× | let text: String = marks |
| 2393 | 3× | .iter() |
| 2394 | 12× | .map(|(h, m)| format!("{} {m}\n", h.to_hex())) |
| 2395 | 3× | .collect(); |
| 2396 | 3× | write_atomic(&graph_marks, text.as_bytes())?; |
| 2397 | 3× | let _ = git(git_dir, &["update-ref", "-d", "refs/oxy/export"]); |
| 2398 | | |
| 2399 | 3× | let listed = git( |
| 2400 | 3× | git_dir, |
| 2401 | 3× | &[ |
| 2402 | 3× | "for-each-ref", |
| 2403 | 3× | "--format=%(refname)", |
| 2404 | 3× | "refs/heads", |
| 2405 | 3× | "refs/tags", |
| 2406 | 3× | ], |
| 2407 | 0× | )?; |
| 2408 | 3× | for r in listed |
| 2409 | 3× | .lines() |
| 2410 | 3× | .map(str::trim) |
| 2411 | 8× | .filter(|r| !r.is_empty() && !wanted.contains(*r)) |
| 2412 | | { |
| 2413 | 1× | git(git_dir, &["update-ref", "-d", r])?; |
| 2414 | | } |
| 2415 | | |
| 2416 | 3× | let main = ["main", "master", "trunk", "develop"] |
| 2417 | 3× | .into_iter() |
| 2418 | 3× | .find(|b| wanted.contains(&format!("refs/heads/{b}"))); |
| 2419 | 3× | if let Some(b) = main { |
| 2420 | 3× | git( |
| 2421 | 3× | git_dir, |
| 2422 | 3× | &["symbolic-ref", "HEAD", &format!("refs/heads/{b}")], |
| 2423 | 0× | )?; |
| 2424 | 0× | } |
| 2425 | 3× | report.skipped_subrepos = self.git_export_skipped.replace(0); |
| 2426 | 3× | Ok(report) |
| 2427 | 3× | } |
| 2428 | |
|
| 2429 | | |
| 2430 | 14× | fn git_author_key(&self, key: &KeyId) -> String { |
| 2431 | 14× | match self.store.get_identity(key) { |
| 2432 | 14× | Ok(id) if !id.name.is_empty() => format!( |
| 2433 | | "{} <{}>", |
| 2434 | 14× | id.name.replace(['<', '>', '\n'], " "), |
| 2435 | 14× | id.email.replace(['<', '>', '\n', ' '], "") |
| 2436 | | ), |
| 2437 | 0× | _ => "oxy <oxy@localhost>".to_string(), |
| 2438 | | } |
| 2439 | 14× | } |
| 2440 | | } |
| 2441 | |
|
| 2442 | |
|
| 2443 | |
|
| 2444 | 3× | fn run_fast_import(git_dir: &Path, stream: &[u8], git_marks: &Path) -> Result<()> { |
| 2445 | 3× | let mut cmd = Command::new("git"); |
| 2446 | | |
| 2447 | 3× | cmd.arg("-C") |
| 2448 | 3× | .arg(git_dir) |
| 2449 | 3× | .args(["fast-import", "--quiet", "--done", "--force"]) |
| 2450 | 3× | .arg(format!("--export-marks={}", git_marks.display())); |
| 2451 | 3× | if git_marks.exists() { |
| 2452 | 1× | cmd.arg(format!("--import-marks={}", git_marks.display())); |
| 2453 | 2× | } |
| 2454 | 3× | let mut child = cmd |
| 2455 | 3× | .stdin(Stdio::piped()) |
| 2456 | 3× | .stdout(Stdio::null()) |
| 2457 | 3× | .stderr(Stdio::piped()) |
| 2458 | 3× | .spawn() |
| 2459 | 3× | .map_err(|e| RepoError::Msg(format!("running git fast-import: {e}")))?; |
| 2460 | 3× | child.stdin.take().unwrap().write_all(stream)?; |
| 2461 | 3× | let out = child.wait_with_output()?; |
| 2462 | 3× | if !out.status.success() { |
| 2463 | 0× | return fail(format!( |
| 2464 | | "git fast-import failed: {}", |
| 2465 | 0× | String::from_utf8_lossy(&out.stderr).trim() |
| 2466 | | )); |
| 2467 | 3× | } |
| 2468 | 3× | Ok(()) |
| 2469 | 3× | } |
| 2470 | |
|
| 2471 | | impl Repo { |
| 2472 | | |
| 2473 | | |
| 2474 | | |
| 2475 | | |
| 2476 | | |
| 2477 | | |
| 2478 | | |
| 2479 | | |
| 2480 | 4× | pub fn git_colocate_sync(&self) -> Result<bool> { |
| 2481 | 4× | if self.bare { |
| 2482 | 0× | return fail("a bare repository has no working copy for a .git beside it"); |
| 2483 | 4× | } |
| 2484 | 4× | let git_dir = self.root.join(".git"); |
| 2485 | 4× | if git_dir.is_file() { |
| 2486 | 0× | return fail(".git is a file (a Git worktree or submodule link); remove it to keep a side-by-side Git repository here"); |
| 2487 | 4× | } |
| 2488 | 4× | if !git_dir.join("HEAD").exists() { |
| 2489 | 1× | git(&self.root, &["init", "-q"])?; |
| 2490 | 3× | } |
| 2491 | 4× | let exclude = git_dir.join("info").join("exclude"); |
| 2492 | 4× | let have = fs::read_to_string(&exclude).unwrap_or_default(); |
| 2493 | 30× | if !have.lines().any(|l| l.trim() == "/.oxy/") { |
| 2494 | 1× | fs::create_dir_all(exclude.parent().expect("info/"))?; |
| 2495 | 1× | let sep = if have.is_empty() || have.ends_with('\n') { |
| 2496 | 1× | "" |
| 2497 | | } else { |
| 2498 | 0× | "\n" |
| 2499 | | }; |
| 2500 | 1× | fs::write( |
| 2501 | 1× | &exclude, |
| 2502 | 1× | format!("{have}{sep}# Oxygen's own repository (oxy git colocate)\n/.oxy/\n"), |
| 2503 | 0× | )?; |
| 2504 | 3× | } |
| 2505 | 4× | let bookmark = self.config.bookmark.clone(); |
| 2506 | 4× | let state = self |
| 2507 | 4× | .view()? |
| 2508 | | .bookmarks |
| 2509 | 4× | .get(&bookmark) |
| 2510 | 4× | .map(|s| s.to_hex()) |
| 2511 | 4× | .unwrap_or_default(); |
| 2512 | 4× | let stamp_file = self.oxy.join("git").join("colocate-stamp"); |
| 2513 | 4× | let stamp = format!("{bookmark} {state}"); |
| 2514 | 4× | if fs::read_to_string(&stamp_file).is_ok_and(|s| s.trim() == stamp) { |
| 2515 | 2× | return Ok(false); |
| 2516 | 2× | } |
| 2517 | 2× | if !state.is_empty() { |
| 2518 | 2× | self.git_export_as(&git_dir, &bookmark)?; |
| 2519 | 2× | git( |
| 2520 | 2× | &self.root, |
| 2521 | 2× | &["symbolic-ref", "HEAD", &format!("refs/heads/{bookmark}")], |
| 2522 | 0× | )?; |
| 2523 | 2× | git(&self.root, &["read-tree", "HEAD"])?; |
| 2524 | 0× | } |
| 2525 | 2× | fs::create_dir_all(stamp_file.parent().expect("git/"))?; |
| 2526 | 2× | fs::write(&stamp_file, stamp)?; |
| 2527 | 2× | Ok(!state.is_empty()) |
| 2528 | 4× | } |
| 2529 | | } |