g1t/services/repos/src/diff.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.
| Diffs on attempts; hosted agent presented as the g1t agent | 1 | //! Comparing two commits: which files changed, and how. |
| 2 | //! | |
| 3 | //! Trees are walked together and identical subtrees are skipped by hash, so | |
| 4 | //! the cost follows the size of the change rather than the repository. | |
| 5 | ||
| 6 | use std::collections::BTreeMap; | |
| 7 | ||
| 8 | use g1t_contracts::repos::{DiffLine, EntryKind, FileDiff, FileStatus, Hunk, LineKind, TreeEntry}; | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 9 | use futures_util::future::{try_join, try_join_all}; |
| Diffs on attempts; hosted agent presented as the g1t agent | 10 | use similar::{ChangeTag, TextDiff}; |
| 11 | use worker::Result; | |
| 12 | ||
| 13 | use crate::store::GitRepo; | |
| 14 | ||
| 15 | /// Beyond these the comparison is cut short and marked truncated. | |
| 16 | const MAX_FILES: usize = 300; | |
| 17 | const MAX_LINES: usize = 20_000; | |
| 18 | /// Files larger than this are listed without their lines. | |
| 19 | const MAX_FILE_BYTES: usize = 512 * 1024; | |
| 20 | const CONTEXT_LINES: usize = 3; | |
| 21 | ||
| 22 | /// A file that differs between the two trees. | |
| 23 | struct Change { | |
| 24 | path: String, | |
| 25 | old: Option<String>, | |
| 26 | new: Option<String>, | |
| 27 | } | |
| 28 | ||
| 29 | async fn entries<R: GitRepo>(repo: &R, tree: Option<&str>) -> Result<BTreeMap<String, TreeEntry>> { | |
| 30 | let Some(tree) = tree else { | |
| 31 | return Ok(BTreeMap::new()); | |
| 32 | }; | |
| 33 | Ok(repo | |
| 34 | .read_tree(tree) | |
| 35 | .await? | |
| 36 | .unwrap_or_default() | |
| 37 | .into_iter() | |
| 38 | .map(|entry| (entry.name.clone(), entry)) | |
| 39 | .collect()) | |
| 40 | } | |
| 41 | ||
| Agents as a team: lifecycle, merge queue, billing and a new shell | 42 | /// How many files' contents are read at once. |
| 43 | const READS_AT_ONCE: usize = 16; | |
| 44 | ||
| 45 | /// Collects the files that differ between two trees. Each level of the | |
| 46 | /// trees is read at once, since every read is a round trip to the store | |
| 47 | /// and the levels' trees do not depend on each other. | |
| Diffs on attempts; hosted agent presented as the g1t agent | 48 | async fn changed_files<R: GitRepo>( |
| 49 | repo: &R, | |
| 50 | old_root: Option<&str>, | |
| 51 | new_root: &str, | |
| 52 | ) -> Result<(Vec<Change>, bool)> { | |
| 53 | let mut changes = Vec::new(); | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 54 | let mut level = vec![( |
| Diffs on attempts; hosted agent presented as the g1t agent | 55 | String::new(), |
| 56 | old_root.map(str::to_owned), | |
| 57 | Some(new_root.to_owned()), | |
| 58 | )]; | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 59 | while !level.is_empty() { |
| 60 | let read = try_join_all(level.iter().map(|(_, old_tree, new_tree)| async move { | |
| 61 | let (old, new) = try_join( | |
| 62 | entries(repo, old_tree.as_deref()), | |
| 63 | entries(repo, new_tree.as_deref()), | |
| 64 | ) | |
| 65 | .await?; | |
| 66 | Ok::<_, worker::Error>((old, new)) | |
| 67 | })) | |
| 68 | .await?; | |
| 69 | let mut next = Vec::new(); | |
| 70 | for ((prefix, _, _), (old, new)) in level.iter().zip(read) { | |
| 71 | let names: std::collections::BTreeSet<&String> = old.keys().chain(new.keys()).collect(); | |
| 72 | for name in names { | |
| 73 | let (before, after) = (old.get(name), new.get(name)); | |
| 74 | if before.map(|e| &e.hash) == after.map(|e| &e.hash) { | |
| 75 | continue; | |
| 76 | } | |
| 77 | let path = format!("{prefix}{name}"); | |
| 78 | let subtree = |entry: Option<&TreeEntry>| { | |
| 79 | entry | |
| 80 | .filter(|entry| entry.kind == EntryKind::Tree) | |
| 81 | .map(|entry| entry.hash.clone()) | |
| 82 | }; | |
| 83 | let file = |entry: Option<&TreeEntry>| { | |
| 84 | entry | |
| 85 | .filter(|entry| entry.kind != EntryKind::Tree) | |
| 86 | .map(|entry| entry.hash.clone()) | |
| 87 | }; | |
| 88 | let (old_dir, new_dir) = (subtree(before), subtree(after)); | |
| 89 | if old_dir.is_some() || new_dir.is_some() { | |
| 90 | next.push((format!("{path}/"), old_dir, new_dir)); | |
| 91 | } | |
| 92 | let (old_file, new_file) = (file(before), file(after)); | |
| 93 | if old_file.is_some() || new_file.is_some() { | |
| 94 | if changes.len() >= MAX_FILES { | |
| 95 | changes.sort_by(|a: &Change, b: &Change| a.path.cmp(&b.path)); | |
| 96 | return Ok((changes, true)); | |
| 97 | } | |
| 98 | changes.push(Change { | |
| 99 | path, | |
| 100 | old: old_file, | |
| 101 | new: new_file, | |
| 102 | }); | |
| Diffs on attempts; hosted agent presented as the g1t agent | 103 | } |
| 104 | } | |
| 105 | } | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 106 | level = next; |
| Diffs on attempts; hosted agent presented as the g1t agent | 107 | } |
| 108 | changes.sort_by(|a, b| a.path.cmp(&b.path)); | |
| 109 | Ok((changes, false)) | |
| 110 | } | |
| 111 | ||
| 112 | /// The text of a blob, or `None` if it is binary, too large or missing. | |
| 113 | async fn text<R: GitRepo>(repo: &R, hash: Option<&str>) -> Result<Option<String>> { | |
| 114 | let Some(hash) = hash else { | |
| 115 | return Ok(Some(String::new())); | |
| 116 | }; | |
| 117 | let Some(bytes) = repo.read_blob(hash).await? else { | |
| 118 | return Ok(None); | |
| 119 | }; | |
| 120 | if bytes.len() > MAX_FILE_BYTES || bytes.contains(&0) { | |
| 121 | return Ok(None); | |
| 122 | } | |
| 123 | Ok(String::from_utf8(bytes).ok()) | |
| 124 | } | |
| 125 | ||
| 126 | fn line_diff(old: &str, new: &str) -> (Vec<Hunk>, u32, u32) { | |
| 127 | let diff = TextDiff::from_lines(old, new); | |
| 128 | let (mut additions, mut deletions) = (0, 0); | |
| 129 | let hunks = diff | |
| 130 | .grouped_ops(CONTEXT_LINES) | |
| 131 | .iter() | |
| 132 | .map(|group| Hunk { | |
| 133 | lines: group | |
| 134 | .iter() | |
| 135 | .flat_map(|op| diff.iter_changes(op)) | |
| 136 | .map(|change| { | |
| 137 | let kind = match change.tag() { | |
| 138 | ChangeTag::Equal => LineKind::Context, | |
| 139 | ChangeTag::Insert => { | |
| 140 | additions += 1; | |
| 141 | LineKind::Add | |
| 142 | } | |
| 143 | ChangeTag::Delete => { | |
| 144 | deletions += 1; | |
| 145 | LineKind::Delete | |
| 146 | } | |
| 147 | }; | |
| 148 | DiffLine { | |
| 149 | kind, | |
| 150 | old: change.old_index().map(|index| index as u32 + 1), | |
| 151 | new: change.new_index().map(|index| index as u32 + 1), | |
| 152 | text: change.value().trim_end_matches(['\r', '\n']).to_owned(), | |
| 153 | } | |
| 154 | }) | |
| 155 | .collect(), | |
| 156 | }) | |
| 157 | .collect(); | |
| 158 | (hunks, additions, deletions) | |
| 159 | } | |
| 160 | ||
| 161 | /// The files that differ between two trees, with their line changes. | |
| 162 | /// Returns the files and whether the result was cut short. | |
| 163 | pub async fn compare_trees<R: GitRepo>( | |
| 164 | repo: &R, | |
| 165 | old_tree: Option<&str>, | |
| 166 | new_tree: &str, | |
| 167 | ) -> Result<(Vec<FileDiff>, bool)> { | |
| 168 | let (changes, mut truncated) = changed_files(repo, old_tree, new_tree).await?; | |
| 169 | let mut files = Vec::with_capacity(changes.len()); | |
| 170 | let mut lines = 0; | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 171 | // Contents are read a batch at a time, all of a batch at once. |
| 172 | let mut texts_read: Vec<Option<(String, String)>> = Vec::with_capacity(changes.len()); | |
| 173 | for batch in changes.chunks(READS_AT_ONCE) { | |
| 174 | let read = try_join_all(batch.iter().map(|change| async move { | |
| 175 | let (old, new) = try_join( | |
| 176 | text(repo, change.old.as_deref()), | |
| 177 | text(repo, change.new.as_deref()), | |
| 178 | ) | |
| 179 | .await?; | |
| 180 | Ok::<_, worker::Error>(old.zip(new)) | |
| 181 | })) | |
| 182 | .await?; | |
| 183 | lines += read | |
| 184 | .iter() | |
| 185 | .flatten() | |
| 186 | .map(|(old, new)| old.lines().count().max(new.lines().count())) | |
| 187 | .sum::<usize>(); | |
| 188 | texts_read.extend(read); | |
| 189 | if lines >= MAX_LINES { | |
| 190 | break; | |
| 191 | } | |
| 192 | } | |
| 193 | let mut texts_read = texts_read.into_iter(); | |
| 194 | lines = 0; | |
| Diffs on attempts; hosted agent presented as the g1t agent | 195 | for change in changes { |
| 196 | let status = match (&change.old, &change.new) { | |
| 197 | (None, _) => FileStatus::Added, | |
| 198 | (_, None) => FileStatus::Deleted, | |
| 199 | _ => FileStatus::Modified, | |
| 200 | }; | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 201 | let texts = match texts_read.next() { |
| 202 | Some(texts) if lines < MAX_LINES => texts, | |
| 203 | _ => { | |
| 204 | truncated = true; | |
| 205 | None | |
| 206 | } | |
| Diffs on attempts; hosted agent presented as the g1t agent | 207 | }; |
| 208 | let (hunks, additions, deletions, binary) = match texts { | |
| 209 | Some((old, new)) => { | |
| 210 | let (hunks, additions, deletions) = line_diff(&old, &new); | |
| 211 | (hunks, additions, deletions, false) | |
| 212 | } | |
| 213 | None => (Vec::new(), 0, 0, true), | |
| 214 | }; | |
| 215 | lines += hunks.iter().map(|hunk| hunk.lines.len()).sum::<usize>(); | |
| 216 | files.push(FileDiff { | |
| 217 | path: change.path, | |
| 218 | status, | |
| 219 | additions, | |
| 220 | deletions, | |
| 221 | binary, | |
| 222 | hunks, | |
| 223 | }); | |
| 224 | } | |
| 225 | Ok((files, truncated)) | |
| 226 | } |