| 34 | |
| 35 | impl SpanCompiler { |
| 36 | pub(super) fn lower( |
| 37 | &self, |
| 38 | pending: &[PendingLineIndexUpdate], |
| 39 | hunks: &mut Vec<GraphOp<Option<ContentHash>>>, |
| 40 | ) -> CliResult<(Vec<SpanOp>, Vec<NewRoot>)> { |
| 41 | let mut ops = Vec::new(); |
| 42 | let mut roots = Vec::new(); |
| 43 | for update in pending { |
| 44 | match update { |
| 45 | PendingLineIndexUpdate::Add { |
| 46 | path, new_ranges, .. |
| 47 | } => { |
| 48 | let (empty_start, root_path) = hunks |
| 49 | .iter() |
| 50 | .find_map(|hunk| { |
| 51 | if let GraphOp::FileAdd { |
| 52 | path: name, |
| 53 | add_inode, |
| 54 | .. |
| 55 | } = hunk |
| 56 | { |
| 57 | (atomic_repository::canonical_path_identity(name) |
| 58 | == atomic_repository::canonical_path_identity(path)) |
| 59 | .then_some((add_inode.start.get(), name.clone())) |
| 60 | } else { |
| 61 | None |
| 62 | } |
| 63 | }) |
| 64 | .ok_or_else(|| { |
| 65 | git_error(format!("span add has no namespace operation for {path}")) |
| 66 | })?; |
| 67 | let start = new_ranges |
| 68 | .first() |
| 69 | .map_or(empty_start, |range| range.0.get()); |
| 70 | let end = new_ranges.last().map_or(start, |range| range.1.get()); |
| 71 | let ordinal = u32::try_from(roots.len()) |
| 72 | .map_err(|_| git_error("too many span roots in one change"))?; |
| 73 | ops.push(SpanOp::Root { |
| 74 | path: root_path, |
| 75 | ordinal, |
| 76 | start, |
| 77 | end, |
| 78 | }); |
| 79 | roots.push(NewRoot { |
| 80 | path: path.clone(), |
| 81 | ordinal, |
| 82 | start, |
| 83 | end, |
| 84 | }); |
| 85 | } |
| 86 | PendingLineIndexUpdate::Modify { path, replacements } => { |
| 87 | let file = self.files.get(path).ok_or_else(|| { |
| 88 | git_error(format!("span file identity missing for {path}")) |
| 89 | })?; |
| 90 | for replacement in replacements { |
| 91 | let begin = replacement.start_idx; |
| 92 | let end = begin |
| 93 | .checked_add(replacement.old_len) |