g1t/services/repos/src/catch_up.rs
Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Catching up with main takes seconds when the two sides touched different files | 1 | //! Bringing a pull request up to date with the branch it would merge into, |
| 2 | //! without a sandbox, when that is safe. | |
| 3 | //! | |
| 4 | //! When the pull request and the default branch changed different files | |
| 5 | //! since they last agreed, the merge cannot conflict, and its result is | |
| 6 | //! known without merging any file: the default branch's tree, with the | |
| 7 | //! files the pull request changed taken from the pull request. Only the | |
| 8 | //! trees on the way to those files change. They are rebuilt here, with one | |
| 9 | //! merge commit on top whose parents are the pull request's head and the | |
| 10 | //! default branch's head, written as a pack of whole objects and pushed to | |
| 11 | //! the pull request's branch, if it is still where it was. | |
| 12 | //! | |
| 13 | //! When both sides changed a file, the merge needs git itself (and maybe an | |
| 14 | //! agent), so the answer is that a sandbox is needed, and nothing is pushed. | |
| 15 | ||
| 16 | use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; | |
| 17 | ||
| 18 | use futures_util::future::{try_join, try_join_all}; | |
| 19 | use g1t_contracts::audit::{AuditActor, NewAuditEntry, Surface}; | |
| 20 | use g1t_contracts::credentials::Decision; | |
| 21 | use g1t_contracts::repos::{ | |
| 22 | EntryKind, NeedsAgentReason, PullBranchUpdate, RepoPath, TreeEntry, UpdatePullBranchArgs, | |
| 23 | }; | |
| 24 | use g1t_contracts::{FailureCode, Outcome}; | |
| 25 | use g1t_kit::now_ms; | |
| 26 | use g1t_scan::pack::{ObjectKind, TreeItem, encode_tree, extend_pack, object_id, write_pack}; | |
| 27 | use worker::Result; | |
| 28 | ||
| 29 | use crate::registry::{can_read, can_write, store_key}; | |
| 30 | use crate::store::{GitRepo, GitStore, Scope}; | |
| 31 | use crate::{MAX_ANCESTRY, Repos, UNVERIFIED, descends_from, diff, land, nearest_ancestor_in, not_found}; | |
| 32 | ||
| 33 | /// The mode git writes for an entry of each kind. | |
| 34 | fn mode(kind: EntryKind) -> &'static str { | |
| 35 | match kind { | |
| 36 | EntryKind::Tree => "40000", | |
| 37 | EntryKind::Blob => "100644", | |
| 38 | EntryKind::Exec => "100755", | |
| 39 | EntryKind::Symlink => "120000", | |
| 40 | EntryKind::Gitlink => "160000", | |
| 41 | } | |
| 42 | } | |
| 43 | ||
| 44 | /// Git orders a tree's entries by name, comparing a subtree's name as if | |
| 45 | /// it ended in `/`. | |
| 46 | fn sort_key(entry: &TreeEntry) -> Vec<u8> { | |
| 47 | let mut key = entry.name.as_bytes().to_vec(); | |
| 48 | if entry.kind == EntryKind::Tree { | |
| 49 | key.push(b'/'); | |
| 50 | } | |
| 51 | key | |
| 52 | } | |
| 53 | ||
| 54 | /// A tree object's bytes, its entries in git's order. | |
| 55 | pub(crate) fn encode_entries(entries: &[TreeEntry]) -> Vec<u8> { | |
| 56 | let mut sorted: Vec<&TreeEntry> = entries.iter().collect(); | |
| 57 | sorted.sort_by_key(|entry| sort_key(entry)); | |
| 58 | let items: Vec<TreeItem> = sorted | |
| 59 | .into_iter() | |
| 60 | .map(|entry| TreeItem { | |
| 61 | mode: mode(entry.kind).to_owned(), | |
| 62 | name: entry.name.clone(), | |
| 63 | id: entry.hash.clone(), | |
| 64 | }) | |
| 65 | .collect(); | |
| 66 | encode_tree(&items) | |
| 67 | } | |
| 68 | ||
| 69 | /// The directories above a path, nearest the root first: `a/b/c` is in | |
| 70 | /// `a` and `a/b`. | |
| 71 | fn ancestors(path: &str) -> impl Iterator<Item = &str> { | |
| 72 | path.match_indices('/').map(move |(at, _)| &path[..at]) | |
| 73 | } | |
| 74 | ||
| 75 | /// The paths at which the two sides' changes meet, so that the merge is not | |
| 76 | /// a matter of taking each side's files: a file both changed, or a file on | |
| 77 | /// one side where the other has a directory (a file `a` against `a/b`). | |
| 78 | pub(crate) fn overlapping(ours: &[String], theirs: &[String]) -> Vec<String> { | |
| 79 | let their_files: HashSet<&str> = theirs.iter().map(String::as_str).collect(); | |
| 80 | let their_dirs: HashSet<&str> = theirs.iter().flat_map(|path| ancestors(path)).collect(); | |
| 81 | let mut met: BTreeSet<String> = BTreeSet::new(); | |
| 82 | for path in ours { | |
| 83 | if their_files.contains(path.as_str()) || their_dirs.contains(path.as_str()) { | |
| 84 | met.insert(path.clone()); | |
| 85 | } | |
| 86 | for dir in ancestors(path) { | |
| 87 | if their_files.contains(dir) { | |
| 88 | met.insert(dir.to_owned()); | |
| 89 | } | |
| 90 | } | |
| 91 | } | |
| 92 | met.into_iter().collect() | |
| 93 | } | |
| 94 | ||
| 95 | /// One file the pull request changed, as it is on the pull request: its | |
| 96 | /// kind and blob, or `None` when it deleted it. | |
| 97 | #[derive(Clone, Debug)] | |
| 98 | pub(crate) struct Change { | |
| 99 | pub path: String, | |
| 100 | pub entry: Option<(EntryKind, String)>, | |
| 101 | } | |
| 102 | ||
| 103 | /// The merged tree's id, and the tree objects written for it. | |
| 104 | #[derive(Debug)] | |
| 105 | pub(crate) struct Merged { | |
| 106 | pub tree: String, | |
| 107 | pub objects: Vec<Vec<u8>>, | |
| 108 | } | |
| 109 | ||
| 110 | enum Node { | |
| 111 | Leaf(EntryKind, String), | |
| 112 | /// A subtree left as it is. | |
| 113 | Subtree(String), | |
| 114 | /// A subtree being changed. | |
| 115 | Dir(Dir), | |
| 116 | } | |
| 117 | ||
| 118 | #[derive(Default)] | |
| 119 | struct Dir { | |
| 120 | entries: BTreeMap<String, Node>, | |
| 121 | } | |
| 122 | ||
| 123 | /// A tree, ready to change. Its entries are checked to write back to | |
| 124 | /// exactly its id: a tree holding something this cannot write, such as an | |
| 125 | /// unusual file mode, is refused rather than changed. | |
| 126 | fn load(id: &str, trees: &HashMap<String, Vec<TreeEntry>>) -> std::result::Result<Dir, String> { | |
| 127 | let entries = trees | |
| 128 | .get(id) | |
| 129 | .ok_or_else(|| format!("tree {id} was not read"))?; | |
| 130 | if object_id(ObjectKind::Tree, &encode_entries(entries)) != id { | |
| 131 | return Err(format!("tree {id} holds entries g1t cannot write back exactly")); | |
| 132 | } | |
| 133 | Ok(Dir { | |
| 134 | entries: entries | |
| 135 | .iter() | |
| 136 | .map(|entry| { | |
| 137 | let node = match entry.kind { | |
| 138 | EntryKind::Tree => Node::Subtree(entry.hash.clone()), | |
| 139 | kind => Node::Leaf(kind, entry.hash.clone()), | |
| 140 | }; | |
| 141 | (entry.name.clone(), node) | |
| 142 | }) | |
| 143 | .collect(), | |
| 144 | }) | |
| 145 | } | |
| 146 | ||
| 147 | /// The directory at `node`, read if it was not yet. | |
| 148 | fn open<'a>( | |
| 149 | node: &'a mut Node, | |
| 150 | trees: &HashMap<String, Vec<TreeEntry>>, | |
| 151 | ) -> std::result::Result<&'a mut Dir, String> { | |
| 152 | if let Node::Subtree(id) = node { | |
| 153 | *node = Node::Dir(load(id, trees)?); | |
| 154 | } | |
| 155 | match node { | |
| 156 | Node::Dir(dir) => Ok(dir), | |
| 157 | _ => Err("a file is where a directory was expected".to_owned()), | |
| 158 | } | |
| 159 | } | |
| 160 | ||
| 161 | /// Removes the file at `parts`, and any directory that leaves empty. | |
| 162 | fn remove( | |
| 163 | dir: &mut Dir, | |
| 164 | parts: &[&str], | |
| 165 | trees: &HashMap<String, Vec<TreeEntry>>, | |
| 166 | ) -> std::result::Result<(), String> { | |
| 167 | let (name, rest) = parts.split_first().ok_or("an empty path")?; | |
| 168 | if rest.is_empty() { | |
| 169 | return match dir.entries.get(*name) { | |
| 170 | Some(Node::Leaf(..)) => { | |
| 171 | dir.entries.remove(*name); | |
| 172 | Ok(()) | |
| 173 | } | |
| 174 | Some(_) => Err(format!("{name} is a directory, not a file")), | |
| 175 | None => Err(format!("{name} is not there to remove")), | |
| 176 | }; | |
| 177 | } | |
| 178 | let child = dir | |
| 179 | .entries | |
| 180 | .get_mut(*name) | |
| 181 | .ok_or_else(|| format!("{name} is not there"))?; | |
| 182 | let inner = open(child, trees)?; | |
| 183 | remove(inner, rest, trees)?; | |
| 184 | if inner.entries.is_empty() { | |
| 185 | dir.entries.remove(*name); | |
| 186 | } | |
| 187 | Ok(()) | |
| 188 | } | |
| 189 | ||
| 190 | /// Puts a file at `parts`, making the directories it needs. | |
| 191 | fn insert( | |
| 192 | dir: &mut Dir, | |
| 193 | parts: &[&str], | |
| 194 | kind: EntryKind, | |
| 195 | hash: &str, | |
| 196 | trees: &HashMap<String, Vec<TreeEntry>>, | |
| 197 | ) -> std::result::Result<(), String> { | |
| 198 | let (name, rest) = parts.split_first().ok_or("an empty path")?; | |
| 199 | if rest.is_empty() { | |
| 200 | if matches!(dir.entries.get(*name), Some(Node::Subtree(_) | Node::Dir(_))) { | |
| 201 | return Err(format!("{name} is a directory, not a file")); | |
| 202 | } | |
| 203 | dir.entries.insert((*name).to_owned(), Node::Leaf(kind, hash.to_owned())); | |
| 204 | return Ok(()); | |
| 205 | } | |
| 206 | let child = dir | |
| 207 | .entries | |
| 208 | .entry((*name).to_owned()) | |
| 209 | .or_insert_with(|| Node::Dir(Dir::default())); | |
| 210 | insert(open(child, trees)?, rest, kind, hash, trees) | |
| 211 | } | |
| 212 | ||
| 213 | /// Writes a changed directory and those in it; returns its id. | |
| 214 | fn write(dir: Dir, objects: &mut Vec<Vec<u8>>) -> String { | |
| 215 | let mut entries = Vec::with_capacity(dir.entries.len()); | |
| 216 | for (name, node) in dir.entries { | |
| 217 | let (kind, hash) = match node { | |
| 218 | Node::Leaf(kind, hash) => (kind, hash), | |
| 219 | Node::Subtree(hash) => (EntryKind::Tree, hash), | |
| 220 | // Git keeps no empty directories. | |
| 221 | Node::Dir(inner) if inner.entries.is_empty() => continue, | |
| 222 | Node::Dir(inner) => (EntryKind::Tree, write(inner, objects)), | |
| 223 | }; | |
| 224 | entries.push(TreeEntry { name, hash, kind }); | |
| 225 | } | |
| 226 | let bytes = encode_entries(&entries); | |
| 227 | let id = object_id(ObjectKind::Tree, &bytes); | |
| 228 | objects.push(bytes); | |
| 229 | id | |
| 230 | } | |
| 231 | ||
| 232 | /// The tree of `base_root` with `changes` applied: deletions first, so a | |
| 233 | /// file can take the place of a directory the pull request emptied. | |
| 234 | /// `trees` holds every tree of the base on the way to a changed path. | |
| 235 | pub(crate) fn merge_tree( | |
| 236 | base_root: &str, | |
| 237 | trees: &HashMap<String, Vec<TreeEntry>>, | |
| 238 | changes: &[Change], | |
| 239 | ) -> std::result::Result<Merged, String> { | |
| 240 | let mut root = load(base_root, trees)?; | |
| 241 | for change in changes.iter().filter(|change| change.entry.is_none()) { | |
| 242 | let parts: Vec<&str> = change.path.split('/').collect(); | |
| 243 | remove(&mut root, &parts, trees).map_err(|why| format!("{}: {why}", change.path))?; | |
| 244 | } | |
| 245 | for change in changes { | |
| 246 | if let Some((kind, hash)) = &change.entry { | |
| 247 | let parts: Vec<&str> = change.path.split('/').collect(); | |
| 248 | insert(&mut root, &parts, *kind, hash, trees) | |
| 249 | .map_err(|why| format!("{}: {why}", change.path))?; | |
| 250 | } | |
| 251 | } | |
| 252 | let mut objects = Vec::new(); | |
| 253 | let tree = write(root, &mut objects); | |
| 254 | // A subtree that came out as it was is already stored. | |
| 255 | let mut seen = HashSet::new(); | |
| 256 | objects.retain(|bytes| seen.insert(object_id(ObjectKind::Tree, bytes))); | |
| 257 | Ok(Merged { tree, objects }) | |
| 258 | } | |
| 259 | ||
| 260 | /// Who a commit is by, and when. | |
| 261 | pub(crate) struct Signature<'a> { | |
| 262 | pub name: &'a str, | |
| 263 | pub email: &'a str, | |
| 264 | /// Seconds since the epoch, in UTC. | |
| 265 | pub seconds: u64, | |
| 266 | } | |
| 267 | ||
| 268 | /// A commit object's bytes, authored and committed by `by`. | |
| 269 | pub(crate) fn commit_object(tree: &str, parents: &[&str], by: &Signature, message: &str) -> Vec<u8> { | |
| 270 | let mut out = format!("tree {tree}\n"); | |
| 271 | for parent in parents { | |
| 272 | out.push_str(&format!("parent {parent}\n")); | |
| 273 | } | |
| 274 | // Git takes everything up to `<` as the name. | |
| 275 | let name: String = by.name.chars().filter(|c| !matches!(c, '<' | '>' | '\n')).collect(); | |
| 276 | let email: String = by.email.chars().filter(|c| !matches!(c, '<' | '>' | '\n')).collect(); | |
| 277 | let line = format!("{name} <{email}> {} +0000", by.seconds); | |
| 278 | out.push_str(&format!("author {line}\ncommitter {line}\n\n{message}\n")); | |
| 279 | out.into_bytes() | |
| 280 | } | |
| 281 | ||
| 282 | /// What the merge commit says. | |
| 283 | pub(crate) fn merge_message(base: &str, branch: &str, number: u32) -> String { | |
| 284 | if branch == base { | |
| 285 | // A fork carries its change on a branch named like the default. | |
| 286 | format!("Merge {base} into pull request #{number}") | |
| 287 | } else { | |
| 288 | format!("Merge {base} into {branch}") | |
| 289 | } | |
| 290 | } | |
| 291 | ||
| 292 | /// The trees at `dirs` (and the root, `""`) under `root`, by path: each | |
| 293 | /// one's id and entries. A directory that is not there is left out. Each | |
| 294 | /// level is read at once. | |
| 295 | async fn read_dirs<R: GitRepo>( | |
| 296 | repo: &R, | |
| 297 | root: &str, | |
| 298 | dirs: &BTreeSet<String>, | |
| 299 | ) -> Result<HashMap<String, (String, Vec<TreeEntry>)>> { | |
| 300 | let mut found: HashMap<String, (String, Vec<TreeEntry>)> = HashMap::new(); | |
| 301 | if let Some(entries) = repo.read_tree(root).await? { | |
| 302 | found.insert(String::new(), (root.to_owned(), entries)); | |
| 303 | } | |
| 304 | let depth = |path: &str| path.matches('/').count(); | |
| 305 | let deepest = dirs.iter().map(|dir| depth(dir)).max().unwrap_or(0); | |
| 306 | for level in 0..=deepest { | |
| 307 | let wanted: Vec<(String, String)> = dirs | |
| 308 | .iter() | |
| 309 | .filter(|dir| !dir.is_empty() && depth(dir) == level) | |
| 310 | .filter_map(|dir| { | |
| 311 | let (parent, name) = dir.rsplit_once('/').unwrap_or(("", dir.as_str())); | |
| 312 | let (_, entries) = found.get(parent)?; | |
| 313 | entries | |
| 314 | .iter() | |
| 315 | .find(|entry| entry.name == name && entry.kind == EntryKind::Tree) | |
| 316 | .map(|entry| (dir.clone(), entry.hash.clone())) | |
| 317 | }) | |
| 318 | .collect(); | |
| 319 | let read = try_join_all(wanted.iter().map(|(_, id)| repo.read_tree(id))).await?; | |
| 320 | for ((dir, id), entries) in wanted.into_iter().zip(read) { | |
| 321 | if let Some(entries) = entries { | |
| 322 | found.insert(dir, (id, entries)); | |
| 323 | } | |
| 324 | } | |
| 325 | } | |
| 326 | Ok(found) | |
| 327 | } | |
| 328 | ||
| 329 | fn needs_agent(reason: NeedsAgentReason, detail: impl Into<String>, paths: Vec<String>) -> Outcome<PullBranchUpdate> { | |
| 330 | Outcome::Ok(PullBranchUpdate::NeedsAgent { | |
| 331 | reason, | |
| 332 | detail: detail.into(), | |
| 333 | paths, | |
| 334 | }) | |
| 335 | } | |
| 336 | ||
| 337 | impl<S: GitStore> Repos<S> { | |
| 338 | pub(crate) async fn update_pull_branch(&self, a: UpdatePullBranchArgs) -> Result<Outcome<PullBranchUpdate>> { | |
| 339 | let actor = Some(a.actor.clone()); | |
| 340 | let Some(source) = self.registry.by_id(&a.source_id).await? else { | |
| 341 | return Ok(not_found()); | |
| 342 | }; | |
| 343 | let target = match &source.fork_of { | |
| 344 | Some(id) => self.registry.by_id(id).await?, | |
| 345 | None => Some(source.clone()), | |
| 346 | }; | |
| 347 | let Some(target) = target.filter(|repo| can_read(repo, &actor)) else { | |
| 348 | return Ok(not_found()); | |
| 349 | }; | |
| 350 | // The merge is pushed as the person asking, so they must be able to push. | |
| 351 | if !can_write(&source, &actor) { | |
| 352 | return Ok(Outcome::fail( | |
| 353 | FailureCode::Forbidden, | |
| 354 | if source.fork_of.is_some() { | |
| 355 | "Only whoever opened this pull request can update it." | |
| 356 | } else { | |
| 357 | "Only members of the workspace can update this pull request." | |
| 358 | }, | |
| 359 | )); | |
| 360 | } | |
| 361 | if !a.actor.verified { | |
| 362 | return Ok(Outcome::fail(FailureCode::Forbidden, UNVERIFIED)); | |
| 363 | } | |
| 364 | let from_fork = source.id != target.id; | |
| 365 | let base_branch = target.default_branch.clone(); | |
| 366 | let branch = a.branch.clone().unwrap_or_else(|| base_branch.clone()); | |
| 367 | if !from_fork && branch == base_branch { | |
| 368 | return Ok(Outcome::fail( | |
| 369 | FailureCode::Invalid, | |
| 370 | format!("{base_branch} cannot be merged into itself."), | |
| 371 | )); | |
| 372 | } | |
| 373 | ||
| 374 | let source_git = self.store.open(&store_key(&source)).await?; | |
| 375 | let target_git = self.store.open(&store_key(&target)).await?; | |
| 376 | let (history, target_history) = try_join( | |
| 377 | source_git.log(&branch, MAX_ANCESTRY), | |
| 378 | target_git.log(&base_branch, MAX_ANCESTRY), | |
| 379 | ) | |
| 380 | .await?; | |
| 381 | let (Some(head), Some(base)) = (history.first(), target_history.first()) else { | |
| 382 | return Ok(Outcome::fail( | |
| 383 | FailureCode::Conflict, | |
| 384 | "This pull request has no commits to bring up to date.", | |
| 385 | )); | |
| 386 | }; | |
| 387 | if descends_from(&source_git, &history, &base.hash).await? { | |
| 388 | return Ok(Outcome::Ok(PullBranchUpdate::UpToDate { | |
| 389 | commit: head.hash.clone(), | |
| 390 | })); | |
| 391 | } | |
| 392 | let shared: HashSet<String> = target_history.iter().map(|commit| commit.hash.clone()).collect(); | |
| 393 | let merge_base = nearest_ancestor_in(&source_git, &history, &shared).await?; | |
| 394 | let Some((merge_base, merge_base_tree)) = merge_base.and_then(|hash| { | |
| 395 | target_history | |
| 396 | .iter() | |
| 397 | .find(|commit| commit.hash == hash) | |
| 398 | .map(|commit| (hash, commit.tree_hash.clone())) | |
| 399 | }) else { | |
| 400 | return Ok(needs_agent( | |
| 401 | NeedsAgentReason::Unsupported, | |
| 402 | format!("g1t could not find where this pull request left {base_branch}."), | |
| 403 | Vec::new(), | |
| 404 | )); | |
| 405 | }; | |
| 406 | ||
| 407 | let ((ours, ours_cut), (theirs, theirs_cut)) = try_join( | |
| 408 | diff::changed_paths(&source_git, Some(&merge_base_tree), &head.tree_hash), | |
| 409 | diff::changed_paths(&target_git, Some(&merge_base_tree), &base.tree_hash), | |
| 410 | ) | |
| 411 | .await?; | |
| 412 | if ours_cut || theirs_cut { | |
| 413 | return Ok(needs_agent( | |
| 414 | NeedsAgentReason::Unsupported, | |
| 415 | "The change is too large to merge without a sandbox.", | |
| 416 | Vec::new(), | |
| 417 | )); | |
| 418 | } | |
| 419 | let met = overlapping(&ours, &theirs); | |
| 420 | if !met.is_empty() { | |
| 421 | return Ok(needs_agent( | |
| 422 | NeedsAgentReason::Overlap, | |
| 423 | format!("This pull request and {base_branch} both changed some of the same files."), | |
| 424 | met, | |
| 425 | )); | |
| 426 | } | |
| 427 | ||
| 428 | // The trees on the way to each changed file, on both sides. | |
| 429 | let dirs: BTreeSet<String> = ours | |
| 430 | .iter() | |
| 431 | .flat_map(|path| ancestors(path).map(str::to_owned)) | |
| 432 | .collect(); | |
| 433 | let (on_pull, on_base) = try_join( | |
| 434 | read_dirs(&source_git, &head.tree_hash, &dirs), | |
| 435 | read_dirs(&target_git, &base.tree_hash, &dirs), | |
| 436 | ) | |
| 437 | .await?; | |
| 438 | let changes: Vec<Change> = ours | |
| 439 | .iter() | |
| 440 | .map(|path| { | |
| 441 | let (parent, name) = path.rsplit_once('/').unwrap_or(("", path.as_str())); | |
| 442 | let entry = on_pull.get(parent).and_then(|(_, entries)| { | |
| 443 | entries | |
| 444 | .iter() | |
| 445 | .find(|entry| entry.name == name && entry.kind != EntryKind::Tree) | |
| 446 | .map(|entry| (entry.kind, entry.hash.clone())) | |
| 447 | }); | |
| 448 | Change { | |
| 449 | path: path.clone(), | |
| 450 | entry, | |
| 451 | } | |
| 452 | }) | |
| 453 | .collect(); | |
| 454 | let trees: HashMap<String, Vec<TreeEntry>> = on_base.into_values().collect(); | |
| 455 | let merged = match merge_tree(&base.tree_hash, &trees, &changes) { | |
| 456 | Ok(merged) => merged, | |
| 457 | Err(why) => { | |
| 458 | return Ok(needs_agent( | |
| 459 | NeedsAgentReason::Unsupported, | |
| 460 | format!("g1t could not merge this itself: {why}."), | |
| 461 | Vec::new(), | |
| 462 | )); | |
| 463 | } | |
| 464 | }; | |
| 465 | ||
| 466 | let email = format!("{}@users.g1t.sh", a.actor.username); | |
| 467 | let commit = commit_object( | |
| 468 | &merged.tree, | |
| 469 | &[&head.hash, &base.hash], | |
| 470 | &Signature { | |
| 471 | name: &a.actor.username, | |
| 472 | email: &email, | |
| 473 | seconds: now_ms() / 1000, | |
| 474 | }, | |
| 475 | &merge_message(&base_branch, &branch, a.number), | |
| 476 | ); | |
| 477 | let commit_id = object_id(ObjectKind::Commit, &commit); | |
| 478 | let mut objects: Vec<(ObjectKind, Vec<u8>)> = merged | |
| 479 | .objects | |
| 480 | .into_iter() | |
| 481 | .map(|bytes| (ObjectKind::Tree, bytes)) | |
| 482 | .collect(); | |
| 483 | objects.push((ObjectKind::Commit, commit)); | |
| 484 | ||
| 485 | // A fork lacks what the default branch gained since it was made: | |
| 486 | // those objects come from the repository, with the merge after them. | |
| 487 | let pack = if from_fork { | |
| 488 | let target_access = target_git.access(Scope::Read).await?; | |
| 489 | let fetched = land::fetch_pack(&target_access, &base.hash, Some(&merge_base)).await?; | |
| 490 | extend_pack(&fetched, &objects).map_err(worker::Error::RustError)? | |
| 491 | } else { | |
| 492 | write_pack(&objects) | |
| 493 | }; | |
| 494 | let source_access = source_git.access(Scope::Write).await?; | |
| 495 | // Only if the branch is still where it was: a push that landed | |
| 496 | // meanwhile is kept, and this is refused. | |
| 497 | let pushed = land::push_pack(&source_access, &branch, Some(&head.hash), &commit_id, pack).await?; | |
| 498 | let git_ref = format!("refs/heads/{branch}"); | |
| 499 | let path = RepoPath { | |
| 500 | namespace: source.namespace.clone(), | |
| 501 | name: source.name.clone(), | |
| 502 | }; | |
| 503 | let mut target_entry = self.audit_target(&path).await?; | |
| 504 | target_entry.git_ref = Some(git_ref.clone()); | |
| 505 | let mut entry = NewAuditEntry::new( | |
| 506 | AuditActor::of(&a.actor), | |
| 507 | "git.push", | |
| 508 | Surface::Git, | |
| 509 | target_entry, | |
| 510 | &Decision::allow("person"), | |
| 511 | g1t_contracts::new_id("req", now_ms()), | |
| 512 | ); | |
| 513 | if let Err(reason) = pushed { | |
| 514 | entry.result = Some("conflict".to_owned()); | |
| 515 | entry.message = Some(reason); | |
| 516 | self.record_git(entry).await; | |
| 517 | return Ok(Outcome::fail( | |
| 518 | FailureCode::Conflict, | |
| 519 | format!("{branch} moved while it was being brought up to date. Nothing was lost; try again."), | |
| 520 | )); | |
| 521 | } | |
| 522 | entry.result = Some("ok".to_owned()); | |
| 523 | self.record_git(entry).await; | |
| 524 | // As any push does: the pull request's head moves, its checks run | |
| 525 | // again, and whether it merges cleanly is worked out anew. | |
| 526 | self.publish_push(&source, &git_ref, Some(&head.hash), &commit_id, Some(a.actor.id.clone())) | |
| 527 | .await?; | |
| 528 | Ok(Outcome::Ok(PullBranchUpdate::Updated { | |
| 529 | commit: commit_id, | |
| 530 | previous: head.hash.clone(), | |
| 531 | })) | |
| 532 | } | |
| 533 | } | |
| 534 | ||
| 535 | #[cfg(test)] | |
| 536 | mod tests { | |
| 537 | use super::*; | |
| 538 | ||
| 539 | fn entry(name: &str, kind: EntryKind, hash: &str) -> TreeEntry { | |
| 540 | TreeEntry { | |
| 541 | name: name.to_owned(), | |
| 542 | hash: hash.to_owned(), | |
| 543 | kind, | |
| 544 | } | |
| 545 | } | |
| 546 | ||
| 547 | fn paths(list: &[&str]) -> Vec<String> { | |
| 548 | list.iter().map(|path| (*path).to_owned()).collect() | |
| 549 | } | |
| 550 | ||
| 551 | #[test] | |
| 552 | fn different_files_do_not_meet() { | |
| 553 | assert!(overlapping(&paths(&["src/a.rs", "README.md"]), &paths(&["src/b.rs", "docs/x.md"])).is_empty()); | |
| 554 | } | |
| 555 | ||
| 556 | #[test] | |
| 557 | fn the_same_file_meets() { | |
| 558 | assert_eq!(overlapping(&paths(&["src/a.rs", "b"]), &paths(&["src/a.rs"])), ["src/a.rs"]); | |
| 559 | } | |
| 560 | ||
| 561 | #[test] | |
| 562 | fn a_file_against_a_directory_meets() { | |
| 563 | // Ours made `a` a file where theirs put files under `a/`. | |
| 564 | assert_eq!(overlapping(&paths(&["a"]), &paths(&["a/b"])), ["a"]); | |
| 565 | // And the other way round. | |
| 566 | assert_eq!(overlapping(&paths(&["a/b/c"]), &paths(&["a/b"])), ["a/b"]); | |
| 567 | // A shared prefix of a name is not a directory. | |
| 568 | assert!(overlapping(&paths(&["ab"]), &paths(&["a/b"])).is_empty()); | |
| 569 | } | |
| 570 | ||
| 571 | #[test] | |
| 572 | fn modes_are_written_as_git_writes_them() { | |
| 573 | assert_eq!(mode(EntryKind::Tree), "40000"); | |
| 574 | assert_eq!(mode(EntryKind::Blob), "100644"); | |
| 575 | assert_eq!(mode(EntryKind::Exec), "100755"); | |
| 576 | assert_eq!(mode(EntryKind::Symlink), "120000"); | |
| 577 | assert_eq!(mode(EntryKind::Gitlink), "160000"); | |
| 578 | } | |
| 579 | ||
| 580 | // Ids below come from git itself (`git mktree --missing`, | |
| 581 | // `git hash-object`, `git commit-tree`) in a scratch repository. | |
| 582 | const EMPTY: &str = "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"; | |
| 583 | const HELLO: &str = "ce013625030ba8dba906f756967f9e9ca394464a"; | |
| 584 | const SUBMODULE: &str = "1111111111111111111111111111111111111111"; | |
| 585 | ||
| 586 | #[test] | |
| 587 | fn entries_sort_as_git_sorts_them() { | |
| 588 | // `a` as a directory sorts after `a.b` and `a-c` but before `a0`: | |
| 589 | // it compares as `a/`. | |
| 590 | let entries = vec![ | |
| 591 | entry("a0", EntryKind::Exec, HELLO), | |
| 592 | entry("a", EntryKind::Tree, "4b825dc642cb6eb9a060e54bf8d69288fbee4904"), | |
| 593 | entry("a.b", EntryKind::Blob, EMPTY), | |
| 594 | entry("a-c", EntryKind::Symlink, HELLO), | |
| 595 | entry("vendor", EntryKind::Gitlink, SUBMODULE), | |
| 596 | ]; | |
| 597 | let id = object_id(ObjectKind::Tree, &encode_entries(&entries)); | |
| 598 | assert_eq!(id, "59710ac869a643ad7e179b924af6fb3009b84859"); | |
| 599 | } | |
| 600 | ||
| 601 | #[test] | |
| 602 | fn the_empty_tree_is_gits() { | |
| 603 | assert_eq!( | |
| 604 | object_id(ObjectKind::Tree, &encode_entries(&[])), | |
| 605 | "4b825dc642cb6eb9a060e54bf8d69288fbee4904" | |
| 606 | ); | |
| 607 | } | |
| 608 | ||
| 609 | /// A base with `README.md`, `bin/run` (executable), `docs/old.md`, | |
| 610 | /// `src/lib.rs` and a submodule `vendor/dep`. | |
| 611 | fn base() -> (String, HashMap<String, Vec<TreeEntry>>) { | |
| 612 | let mut trees = HashMap::new(); | |
| 613 | let bin = vec![entry("run", EntryKind::Exec, HELLO)]; | |
| 614 | let docs = vec![entry("old.md", EntryKind::Blob, HELLO)]; | |
| 615 | let src = vec![entry("lib.rs", EntryKind::Blob, EMPTY)]; | |
| 616 | let vendor = vec![entry("dep", EntryKind::Gitlink, SUBMODULE)]; | |
| 617 | let mut id = |entries: Vec<TreeEntry>| { | |
| 618 | let id = object_id(ObjectKind::Tree, &encode_entries(&entries)); | |
| 619 | trees.insert(id.clone(), entries); | |
| 620 | id | |
| 621 | }; | |
| 622 | let root = vec![ | |
| 623 | entry("README.md", EntryKind::Blob, HELLO), | |
| 624 | entry("bin", EntryKind::Tree, &id(bin)), | |
| 625 | entry("docs", EntryKind::Tree, &id(docs)), | |
| 626 | entry("src", EntryKind::Tree, &id(src)), | |
| 627 | entry("vendor", EntryKind::Tree, &id(vendor)), | |
| 628 | ]; | |
| 629 | let root = id(root); | |
| 630 | (root, trees) | |
| 631 | } | |
| 632 | ||
| 633 | #[test] | |
| 634 | fn the_base_tree_matches_git() { | |
| 635 | assert_eq!(base().0, "cdbaeedd6c31387975e65e67f9d453589d3c73d1"); | |
| 636 | } | |
| 637 | ||
| 638 | #[test] | |
| 639 | fn changes_are_applied_to_the_base() { | |
| 640 | let (root, trees) = base(); | |
| 641 | let changes = vec![ | |
| 642 | // Added in a new directory. | |
| 643 | Change { path: "src/net/http.rs".into(), entry: Some((EntryKind::Blob, HELLO.into())) }, | |
| 644 | // Made executable: the same blob. | |
| 645 | Change { path: "src/lib.rs".into(), entry: Some((EntryKind::Exec, EMPTY.into())) }, | |
| 646 | // Deleted, leaving its directory empty. | |
| 647 | Change { path: "docs/old.md".into(), entry: None }, | |
| 648 | // A symlink added at the top. | |
| 649 | Change { path: "latest".into(), entry: Some((EntryKind::Symlink, HELLO.into())) }, | |
| 650 | ]; | |
| 651 | let merged = merge_tree(&root, &trees, &changes).unwrap(); | |
| 652 | assert_eq!(merged.tree, "3097aeb8d4a86f097eca63da84e432ebeb456158"); | |
| 653 | // The root, src and src/net; docs is gone, bin and vendor untouched. | |
| 654 | assert_eq!(merged.objects.len(), 3); | |
| 655 | } | |
| 656 | ||
| 657 | #[test] | |
| 658 | fn a_file_can_replace_a_directory_the_pull_request_emptied() { | |
| 659 | let (root, trees) = base(); | |
| 660 | let changes = vec![ | |
| 661 | Change { path: "docs".into(), entry: Some((EntryKind::Blob, HELLO.into())) }, | |
| 662 | Change { path: "docs/old.md".into(), entry: None }, | |
| 663 | ]; | |
| 664 | let merged = merge_tree(&root, &trees, &changes).unwrap(); | |
| 665 | assert_eq!(merged.tree, "088fc1bb6d44a71fcd0c33d4022e7bb1628b7202"); | |
| 666 | } | |
| 667 | ||
| 668 | #[test] | |
| 669 | fn a_tree_that_cannot_be_written_back_is_refused() { | |
| 670 | let (root, mut trees) = base(); | |
| 671 | // Entries that do not hash to the id they are filed under, as a | |
| 672 | // tree with a mode g1t does not know would not. | |
| 673 | let entries = trees.remove(&root).unwrap(); | |
| 674 | trees.insert(root.clone(), entries[1..].to_vec()); | |
| 675 | let changes = vec![Change { path: "x".into(), entry: Some((EntryKind::Blob, HELLO.into())) }]; | |
| 676 | assert!(merge_tree(&root, &trees, &changes).is_err()); | |
| 677 | } | |
| 678 | ||
| 679 | #[test] | |
| 680 | fn a_file_is_not_put_where_a_directory_still_is() { | |
| 681 | let (root, trees) = base(); | |
| 682 | let changes = vec![Change { path: "src".into(), entry: Some((EntryKind::Blob, HELLO.into())) }]; | |
| 683 | assert!(merge_tree(&root, &trees, &changes).is_err()); | |
| 684 | } | |
| 685 | ||
| 686 | #[test] | |
| 687 | fn the_merge_commit_matches_git() { | |
| 688 | let commit = commit_object( | |
| 689 | "cdbaeedd6c31387975e65e67f9d453589d3c73d1", | |
| 690 | &["74257edb70e8dc5d2f31af4b76fe898608f829da", "110bda84f45f58e44d3699be9efe91276381b43d"], | |
| 691 | &Signature { name: "octo", email: "octo@users.g1t.sh", seconds: 1_700_000_000 }, | |
| 692 | &merge_message("main", "feature", 7), | |
| 693 | ); | |
| 694 | assert_eq!(object_id(ObjectKind::Commit, &commit), "f8d35e9d454b11a179215ee1283ac71b7216f024"); | |
| 695 | } | |
| 696 | ||
| 697 | #[test] | |
| 698 | fn a_fork_on_the_default_branch_is_named_by_number() { | |
| 699 | assert_eq!(merge_message("main", "main", 12), "Merge main into pull request #12"); | |
| 700 | assert_eq!(merge_message("main", "fix-login", 12), "Merge main into fix-login"); | |
| 701 | } | |
| 702 | ||
| 703 | #[test] | |
| 704 | fn a_pack_of_the_merge_reads_back() { | |
| 705 | let (root, trees) = base(); | |
| 706 | let changes = vec![Change { path: "src/new.rs".into(), entry: Some((EntryKind::Blob, HELLO.into())) }]; | |
| 707 | let merged = merge_tree(&root, &trees, &changes).unwrap(); | |
| 708 | let mut objects: Vec<(ObjectKind, Vec<u8>)> = | |
| 709 | merged.objects.iter().map(|bytes| (ObjectKind::Tree, bytes.clone())).collect(); | |
| 710 | let pack = write_pack(&objects); | |
| 711 | let read = g1t_scan::pack::Pack::parse(&pack).unwrap(); | |
| 712 | assert!(read.tree(&merged.tree).is_some()); | |
| 713 | ||
| 714 | // Extended with a commit, it still reads, and its checksum holds. | |
| 715 | let commit = commit_object( | |
| 716 | &merged.tree, | |
| 717 | &[&root], | |
| 718 | &Signature { name: "octo", email: "octo@users.g1t.sh", seconds: 1 }, | |
| 719 | "m", | |
| 720 | ); | |
| 721 | let commit_id = object_id(ObjectKind::Commit, &commit); | |
| 722 | objects.clear(); | |
| 723 | objects.push((ObjectKind::Commit, commit)); | |
| 724 | let extended = extend_pack(&pack, &objects).unwrap(); | |
| 725 | let read = g1t_scan::pack::Pack::parse(&extended).unwrap(); | |
| 726 | assert!(read.tree(&merged.tree).is_some()); | |
| 727 | assert_eq!(read.commits(), [commit_id]); | |
| 728 | } | |
| 729 | } |