| 1 |
use std::cell::Ref; |
| 2 |
|
| 3 |
use itertools::Itertools as _; |
| 4 |
use rayon::prelude::*; |
| 5 |
use self_cell::self_cell; |
| 6 |
|
| 7 |
use crate::bdiff::Lines; |
| 8 |
use crate::bdiff::{self}; |
| 9 |
use crate::dirstate::owning::OwningDirstateMap; |
| 10 |
use crate::errors::HgError; |
| 11 |
use crate::repo::Repo; |
| 12 |
use crate::revlog::changelog::Changelog; |
| 13 |
use crate::revlog::filelog::Filelog; |
| 14 |
use crate::revlog::manifest::Manifestlog; |
| 15 |
use crate::revlog::RevisionOrWdir; |
| 16 |
use crate::utils::hg_path::HgPath; |
| 17 |
use crate::utils::hg_path::HgPathBuf; |
| 18 |
use crate::utils::strings::clean_whitespace; |
| 19 |
use crate::utils::strings::CleanWhitespace; |
| 20 |
use crate::utils::RawData; |
| 21 |
use crate::utils::{self}; |
| 22 |
use crate::AncestorsIterator; |
| 23 |
use crate::FastHashMap; |
| 24 |
use crate::Graph; |
| 25 |
use crate::GraphError; |
| 26 |
use crate::Node; |
| 27 |
use crate::Revision; |
| 28 |
use crate::NULL_REVISION; |
| 29 |
|
| 30 |
/// Options for [`annotate`]. |
| 31 |
#[derive(Copy, Clone)] |
| 32 |
pub struct AnnotateOptions { |
| 33 |
pub treat_binary_as_text: bool, |
| 34 |
pub follow_copies: bool, |
| 35 |
pub whitespace: Option<CleanWhitespace>, |
| 36 |
} |
| 37 |
|
| 38 |
/// The final result of annotating a file. |
| 39 |
pub enum AnnotateOutput { |
| 40 |
/// An annotated text file. |
| 41 |
Text(ChangesetAnnotatedFile), |
| 42 |
/// The file cannot be annotated because it is binary. |
| 43 |
Binary, |
| 44 |
/// The file was not found in the repository. |
| 45 |
NotFound, |
| 46 |
} |
| 47 |
|
| 48 |
/// A file with user-facing changeset annotations for each line. |
| 49 |
pub struct ChangesetAnnotatedFile { |
| 50 |
// The lines of the file, including original line endings. |
| 51 |
pub lines: Vec<Vec<u8>>, |
| 52 |
// List of annotations corresponding to `lines`. |
| 53 |
pub annotations: Vec<ChangesetAnnotation>, |
| 54 |
} |
| 55 |
|
| 56 |
/// A user-facing changeset annotation for one line. |
| 57 |
pub struct ChangesetAnnotation { |
| 58 |
/// The file path as it was at `revision`. This can be different from the |
| 59 |
/// file's current path if it was copied or renamed in the past. |
| 60 |
pub path: HgPathBuf, |
| 61 |
/// The changelog revision that introduced the line. |
| 62 |
pub revision: RevisionOrWdir, |
| 63 |
/// The one-based line number in the original file. |
| 64 |
pub line_number: u32, |
| 65 |
} |
| 66 |
|
| 67 |
self_cell!( |
| 68 |
/// A wrapper around [`Lines`] that owns the buffer the lines point into. |
| 69 |
/// The buffer contains the file text processed by [`clean_whitespace`]. |
| 70 |
struct OwnedLines { |
| 71 |
owner: RawData, |
| 72 |
#[covariant] |
| 73 |
dependent: Lines, |
| 74 |
} |
| 75 |
); |
| 76 |
|
| 77 |
impl OwnedLines { |
| 78 |
/// Cleans `data` based on `whitespace` and then splits into lines. |
| 79 |
fn split( |
| 80 |
data: RawData, |
| 81 |
whitespace: Option<CleanWhitespace>, |
| 82 |
) -> Result<Self, HgError> { |
| 83 |
if let Some(ws) = whitespace { |
| 84 |
let mut clean = data.into(); |
| 85 |
clean_whitespace(&mut clean, ws); |
| 86 |
Self::try_new(RawData::from(clean), |data| bdiff::split_lines(data)) |
| 87 |
} else { |
| 88 |
Self::try_new(data, |data| bdiff::split_lines(data)) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
fn get(&self) -> &Lines { |
| 93 |
self.borrow_dependent() |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/// A file with filelog annotations for each line. |
| 98 |
struct AnnotatedFile { |
| 99 |
lines: OwnedLines, |
| 100 |
annotations: Vec<Annotation>, |
| 101 |
} |
| 102 |
|
| 103 |
/// A filelog annotation for one line. |
| 104 |
#[derive(Copy, Clone)] |
| 105 |
struct Annotation { |
| 106 |
/// The file revision that introduced the line. |
| 107 |
id: FileId, |
| 108 |
/// The one-based line number in the original file. |
| 109 |
line_number: u32, |
| 110 |
} |
| 111 |
|
| 112 |
/// [`Repo`] and related objects that often need to be passed together. |
| 113 |
struct RepoState<'a> { |
| 114 |
repo: &'a Repo, |
| 115 |
changelog: Ref<'a, Changelog>, |
| 116 |
manifestlog: Ref<'a, Manifestlog>, |
| 117 |
dirstate_parents: Option<[Revision; 2]>, |
| 118 |
dirstate_map: Option<Ref<'a, OwningDirstateMap>>, |
| 119 |
} |
| 120 |
|
| 121 |
impl<'a> RepoState<'a> { |
| 122 |
fn new(repo: &'a Repo, include_dirstate: bool) -> Result<Self, HgError> { |
| 123 |
let changelog = repo.changelog()?; |
| 124 |
let manifestlog = repo.manifestlog()?; |
| 125 |
let (dirstate_parents, dirstate_map) = if include_dirstate { |
| 126 |
let crate::DirstateParents { p1, p2 } = repo.dirstate_parents()?; |
| 127 |
let p1 = changelog.rev_from_node(p1.into())?; |
| 128 |
let p2 = changelog.rev_from_node(p2.into())?; |
| 129 |
let dirstate_map = repo.dirstate_map()?; |
| 130 |
(Some([p1, p2]), Some(dirstate_map)) |
| 131 |
} else { |
| 132 |
(None, None) |
| 133 |
}; |
| 134 |
Ok(Self { |
| 135 |
repo, |
| 136 |
changelog, |
| 137 |
manifestlog, |
| 138 |
dirstate_parents, |
| 139 |
dirstate_map, |
| 140 |
}) |
| 141 |
} |
| 142 |
|
| 143 |
fn dirstate_parents(&self) -> [Revision; 2] { |
| 144 |
self.dirstate_parents.expect("should be set for wdir") |
| 145 |
} |
| 146 |
|
| 147 |
fn dirstate_map(&'a self) -> &'a OwningDirstateMap { |
| 148 |
self.dirstate_map.as_ref().expect("should be set for wdir") |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/// Helper for keeping track of multiple filelogs. |
| 153 |
/// Also abstracts over reading from filelogs and from the working directory. |
| 154 |
#[derive(Default)] |
| 155 |
struct FilelogSet { |
| 156 |
/// List of filelogs. The first one is for the root file being blamed. |
| 157 |
/// Others are added only when following copies/renames. |
| 158 |
items: Vec<FilelogSetItem>, |
| 159 |
/// Mapping of paths to indexes in `items`. |
| 160 |
path_to_index: FastHashMap<HgPathBuf, FilelogIndex>, |
| 161 |
} |
| 162 |
|
| 163 |
struct FilelogSetItem { |
| 164 |
path: HgPathBuf, |
| 165 |
filelog: Filelog, |
| 166 |
} |
| 167 |
|
| 168 |
/// Identifies a filelog in a FilelogSet. |
| 169 |
type FilelogIndex = u32; |
| 170 |
|
| 171 |
/// Identifies a file revision in a FilelogSet. |
| 172 |
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
| 173 |
enum FileId { |
| 174 |
/// The file in the working directory. |
| 175 |
Wdir, |
| 176 |
/// A revision of the file in a filelog. |
| 177 |
Rev(RevFileId), |
| 178 |
} |
| 179 |
|
| 180 |
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
| 181 |
struct RevFileId { |
| 182 |
index: FilelogIndex, |
| 183 |
revision: Revision, |
| 184 |
} |
| 185 |
|
| 186 |
impl FilelogSet { |
| 187 |
/// Returns filelog item at the given index. |
| 188 |
fn get(&self, index: FilelogIndex) -> &FilelogSetItem { |
| 189 |
&self.items[index as usize] |
| 190 |
} |
| 191 |
|
| 192 |
/// Opens a filelog by path and returns its index. |
| 193 |
fn open( |
| 194 |
&mut self, |
| 195 |
repo: &Repo, |
| 196 |
path: &HgPath, |
| 197 |
) -> Result<FilelogIndex, HgError> { |
| 198 |
if let Some(&index) = self.path_to_index.get(path) { |
| 199 |
return Ok(index); |
| 200 |
} |
| 201 |
let index = self.items.len() as FilelogIndex; |
| 202 |
self.items.push(FilelogSetItem { |
| 203 |
filelog: repo.filelog(path)?, |
| 204 |
path: path.into(), |
| 205 |
}); |
| 206 |
self.path_to_index.insert(path.into(), index); |
| 207 |
Ok(index) |
| 208 |
} |
| 209 |
|
| 210 |
/// Opens a filelog by path and returns the id for the given file node. |
| 211 |
fn open_at_node( |
| 212 |
&mut self, |
| 213 |
repo: &Repo, |
| 214 |
path: &HgPath, |
| 215 |
node: Node, |
| 216 |
) -> Result<RevFileId, HgError> { |
| 217 |
let index = self.open(repo, path)?; |
| 218 |
let revision = |
| 219 |
self.get(index).filelog.revlog.rev_from_node(node.into())?; |
| 220 |
Ok(RevFileId { index, revision }) |
| 221 |
} |
| 222 |
|
| 223 |
/// Opens a filelog by path and returns the id for the given changelog |
| 224 |
/// revision. Returns `None` if no filelog exists for that path. |
| 225 |
fn open_at_changelog_rev( |
| 226 |
&mut self, |
| 227 |
state: &RepoState, |
| 228 |
path: &HgPath, |
| 229 |
changelog_revision: Revision, |
| 230 |
) -> Result<Option<RevFileId>, HgError> { |
| 231 |
let changelog_data = |
| 232 |
state.changelog.entry(changelog_revision)?.data()?; |
| 233 |
let manifest = state |
| 234 |
.manifestlog |
| 235 |
.data_for_node(changelog_data.manifest_node()?.into())?; |
| 236 |
let Some(entry) = manifest.find_by_path(path)? else { |
| 237 |
return Ok(None); |
| 238 |
}; |
| 239 |
let node = entry.node_id()?; |
| 240 |
Ok(Some(self.open_at_node(state.repo, path, node)?)) |
| 241 |
} |
| 242 |
|
| 243 |
/// Opens and reads a file by path at a changelog revision (or working |
| 244 |
/// directory), returning its id and contents. Returns `None` if not found. |
| 245 |
fn open_and_read( |
| 246 |
&mut self, |
| 247 |
state: &RepoState, |
| 248 |
path: &HgPath, |
| 249 |
revision: RevisionOrWdir, |
| 250 |
) -> Result<Option<(FileId, RawData)>, HgError> { |
| 251 |
match revision.exclude_wdir() { |
| 252 |
Some(revision) => { |
| 253 |
match self.open_at_changelog_rev(state, path, revision)? { |
| 254 |
None => Ok(None), |
| 255 |
Some(id) => Ok(Some((FileId::Rev(id), self.read(id)?))), |
| 256 |
} |
| 257 |
} |
| 258 |
None => { |
| 259 |
let fs_path = utils::hg_path::hg_path_to_path_buf(path)?; |
| 260 |
let maybe_data = |
| 261 |
state.repo.working_directory_vfs().try_read(fs_path)?; |
| 262 |
Ok(maybe_data.map(|data| (FileId::Wdir, RawData::from(data)))) |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
/// Reads the contents of a file by id. |
| 268 |
fn read(&self, id: RevFileId) -> Result<RawData, HgError> { |
| 269 |
let filelog = &self.get(id.index).filelog; |
| 270 |
filelog.entry(id.revision)?.data()?.into_file_data() |
| 271 |
} |
| 272 |
|
| 273 |
/// Returns the parents of a file. If `follow_copies` is true, it treats |
| 274 |
/// the copy source as a parent. In that case, also returns the file data |
| 275 |
/// (since it has to read the file to extract the copy metadata). |
| 276 |
fn parents( |
| 277 |
&mut self, |
| 278 |
state: &RepoState, |
| 279 |
base_path: &HgPath, |
| 280 |
id: FileId, |
| 281 |
follow_copies: bool, |
| 282 |
) -> Result<(Vec<FileId>, Option<RawData>), HgError> { |
| 283 |
let mut parents = Vec::<FileId>::with_capacity(2); |
| 284 |
let id = match id { |
| 285 |
FileId::Rev(id) => id, |
| 286 |
FileId::Wdir => { |
| 287 |
// If a file in the working directory is copied/renamed, its |
| 288 |
// parent is the copy source (just as it will be after |
| 289 |
// committing). |
| 290 |
let path = state |
| 291 |
.dirstate_map() |
| 292 |
.copy_map_get(base_path)? |
| 293 |
.unwrap_or(base_path); |
| 294 |
for rev in state.dirstate_parents() { |
| 295 |
if let Some(id) = |
| 296 |
self.open_at_changelog_rev(state, path, rev)? |
| 297 |
{ |
| 298 |
parents.push(FileId::Rev(id)); |
| 299 |
} |
| 300 |
} |
| 301 |
return Ok((parents, None)); |
| 302 |
} |
| 303 |
}; |
| 304 |
let filelog = &self.get(id.index).filelog; |
| 305 |
let revisions = |
| 306 |
filelog.parents(id.revision).map_err(from_graph_error)?; |
| 307 |
let mut file_data = None; |
| 308 |
if revisions[0] != NULL_REVISION { |
| 309 |
parents.push(FileId::Rev(RevFileId { |
| 310 |
index: id.index, |
| 311 |
revision: revisions[0], |
| 312 |
})); |
| 313 |
} else if follow_copies { |
| 314 |
// A null p1 indicates there might be copy metadata. |
| 315 |
// Check for it, and if present use it as the parent. |
| 316 |
let data = filelog.entry(id.revision)?.data()?; |
| 317 |
let meta = data.metadata()?.parse()?; |
| 318 |
// If copy or copyrev occurs without the other, ignore it. |
| 319 |
// This matches filerevisioncopied in storageutil.py. |
| 320 |
if let (Some(copy), Some(copyrev)) = (meta.copy, meta.copyrev) { |
| 321 |
parents.push(FileId::Rev( |
| 322 |
self.open_at_node(state.repo, copy, copyrev)?, |
| 323 |
)); |
| 324 |
} |
| 325 |
file_data = Some(data.into_file_data()?); |
| 326 |
} |
| 327 |
if revisions[1] != NULL_REVISION { |
| 328 |
parents.push(FileId::Rev(RevFileId { |
| 329 |
index: id.index, |
| 330 |
revision: revisions[1], |
| 331 |
})); |
| 332 |
} |
| 333 |
Ok((parents, file_data)) |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
/// Per [`FileId`] information used in the [`annotate`] algorithm. |
| 338 |
#[derive(Default)] |
| 339 |
struct FileInfo { |
| 340 |
/// Parents of this revision (via p1 and p2 or copy metadata). |
| 341 |
/// These are always `FileId::Rev`, not `FileId::Wdir`, but we store |
| 342 |
/// `FileId` because everything would have to convert to it anyways. |
| 343 |
parents: Option<Vec<FileId>>, |
| 344 |
/// Current state for annotating the file. |
| 345 |
file: AnnotatedFileState, |
| 346 |
/// Remaining number of times `file` is needed before we can drop it. |
| 347 |
needed: usize, |
| 348 |
/// Current state for converting to a changelog revision. |
| 349 |
revision: ChangelogRevisionState, |
| 350 |
/// The value of `revision` from a descendant. If the linkrev needs |
| 351 |
/// adjustment, we can start iterating the changelog here. |
| 352 |
descendant: Option<RevisionOrWdir>, |
| 353 |
} |
| 354 |
|
| 355 |
/// State enum for reading a file and annotating it. |
| 356 |
#[derive(Default)] |
| 357 |
enum AnnotatedFileState { |
| 358 |
#[default] |
| 359 |
None, |
| 360 |
Read(OwnedLines), |
| 361 |
Annotated(AnnotatedFile), |
| 362 |
} |
| 363 |
|
| 364 |
/// State enum for converting a filelog revision to a changelog revision, but |
| 365 |
/// only if needed (because it will appear in the final output). |
| 366 |
#[derive(Default)] |
| 367 |
enum ChangelogRevisionState { |
| 368 |
#[default] |
| 369 |
NotNeeded, |
| 370 |
Needed, |
| 371 |
Done(RevisionOrWdir), |
| 372 |
} |
| 373 |
|
| 374 |
/// A collection of [`FileInfo`], forming a graph via [`FileInfo::parents`]. |
| 375 |
#[derive(Default)] |
| 376 |
struct FileGraph(FastHashMap<FileId, FileInfo>); |
| 377 |
|
| 378 |
impl FileGraph { |
| 379 |
fn get_or_insert_default(&mut self, id: FileId) -> &mut FileInfo { |
| 380 |
self.0.entry(id).or_default() |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
impl std::ops::Index<FileId> for FileGraph { |
| 385 |
type Output = FileInfo; |
| 386 |
fn index(&self, id: FileId) -> &Self::Output { |
| 387 |
self.0.get(&id).expect("the graph should be populated") |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
impl std::ops::IndexMut<FileId> for FileGraph { |
| 392 |
fn index_mut(&mut self, id: FileId) -> &mut Self::Output { |
| 393 |
self.0.get_mut(&id).expect("the graph should be populated") |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
/// Annotates each line of a file with changeset information. |
| 398 |
pub fn annotate( |
| 399 |
repo: &Repo, |
| 400 |
path: &HgPath, |
| 401 |
changelog_revision: RevisionOrWdir, |
| 402 |
options: AnnotateOptions, |
| 403 |
) -> Result<AnnotateOutput, HgError> { |
| 404 |
// Step 1: Load the base file and check if it's binary. |
| 405 |
let state = RepoState::new(repo, changelog_revision.is_wdir())?; |
| 406 |
let mut fls = FilelogSet::default(); |
| 407 |
let Some((base_id, base_file_data)) = |
| 408 |
fls.open_and_read(&state, path, changelog_revision)? |
| 409 |
else { |
| 410 |
return Ok(AnnotateOutput::NotFound); |
| 411 |
}; |
| 412 |
if !options.treat_binary_as_text && utils::files::is_binary(&base_file_data) |
| 413 |
{ |
| 414 |
return Ok(AnnotateOutput::Binary); |
| 415 |
} |
| 416 |
|
| 417 |
// Step 2: DFS to build the graph. |
| 418 |
let mut graph = FileGraph::default(); |
| 419 |
let mut visit = vec![base_id]; |
| 420 |
while let Some(id) = visit.pop() { |
| 421 |
let info = graph.get_or_insert_default(id); |
| 422 |
if info.parents.is_some() { |
| 423 |
continue; |
| 424 |
} |
| 425 |
let (parents, file_data) = |
| 426 |
fls.parents(&state, path, id, options.follow_copies)?; |
| 427 |
info.parents = Some(parents.clone()); |
| 428 |
if let Some(data) = file_data { |
| 429 |
info.file = AnnotatedFileState::Read(OwnedLines::split( |
| 430 |
data, |
| 431 |
options.whitespace, |
| 432 |
)?); |
| 433 |
} |
| 434 |
for id in parents { |
| 435 |
let info = graph.get_or_insert_default(id); |
| 436 |
info.needed += 1; |
| 437 |
if info.parents.is_none() { |
| 438 |
visit.push(id); |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
// Step 3: Read files and split lines. Do the base file with and without |
| 444 |
// whitespace cleaning. Do the rest of the files in parallel with rayon. |
| 445 |
let base_file_original_lines = match options.whitespace { |
| 446 |
None => None, |
| 447 |
_ => Some(OwnedLines::split(base_file_data.clone(), None)?), |
| 448 |
}; |
| 449 |
graph[base_id].file = AnnotatedFileState::Read(OwnedLines::split( |
| 450 |
base_file_data, |
| 451 |
options.whitespace, |
| 452 |
)?); |
| 453 |
graph.0.par_iter_mut().try_for_each( |
| 454 |
|(&id, info)| -> Result<(), HgError> { |
| 455 |
if let AnnotatedFileState::None = info.file { |
| 456 |
let id = match id { |
| 457 |
FileId::Rev(id) => id, |
| 458 |
FileId::Wdir => unreachable!("only base file can be wdir"), |
| 459 |
}; |
| 460 |
info.file = AnnotatedFileState::Read(OwnedLines::split( |
| 461 |
fls.read(id)?, |
| 462 |
options.whitespace, |
| 463 |
)?); |
| 464 |
} |
| 465 |
Ok(()) |
| 466 |
}, |
| 467 |
)?; |
| 468 |
|
| 469 |
// Step 4: DFS to do the actual annotate algorithm. |
| 470 |
// While we're at it, save the topological order. |
| 471 |
let mut topological_order = vec![]; |
| 472 |
visit.push(base_id); |
| 473 |
while let Some(&id) = visit.last() { |
| 474 |
let info = &mut graph[id]; |
| 475 |
if let AnnotatedFileState::Annotated(_) = info.file { |
| 476 |
visit.pop(); |
| 477 |
continue; |
| 478 |
} |
| 479 |
let visit_len = visit.len(); |
| 480 |
let parents = info.parents.clone().expect("parents set in step 2"); |
| 481 |
for &id in &parents { |
| 482 |
match graph[id].file { |
| 483 |
AnnotatedFileState::Annotated(_) => {} |
| 484 |
_ => visit.push(id), |
| 485 |
} |
| 486 |
} |
| 487 |
if visit.len() != visit_len { |
| 488 |
continue; |
| 489 |
} |
| 490 |
visit.pop(); |
| 491 |
topological_order.push(id); |
| 492 |
let lines = match std::mem::take(&mut graph[id].file) { |
| 493 |
AnnotatedFileState::Read(lines) => lines, |
| 494 |
_ => unreachable!(), |
| 495 |
}; |
| 496 |
let mut parent_files = Vec::with_capacity(2); |
| 497 |
for &id in &parents { |
| 498 |
match graph[id].file { |
| 499 |
AnnotatedFileState::Annotated(ref file) => { |
| 500 |
parent_files.push(file) |
| 501 |
} |
| 502 |
_ => unreachable!(), |
| 503 |
} |
| 504 |
} |
| 505 |
graph[id].file = AnnotatedFileState::Annotated(annotate_pair( |
| 506 |
id, |
| 507 |
lines, |
| 508 |
parent_files, |
| 509 |
)?); |
| 510 |
for &id in &parents { |
| 511 |
let info = &mut graph[id]; |
| 512 |
info.needed -= 1; |
| 513 |
if info.needed == 0 { |
| 514 |
info.file = AnnotatedFileState::None; |
| 515 |
} |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
// Step 5: Map filelog revisions to changelog revisions. |
| 520 |
let base_info = &mut graph[base_id]; |
| 521 |
base_info.descendant = Some(changelog_revision); |
| 522 |
let AnnotatedFileState::Annotated(AnnotatedFile { lines, annotations }) = |
| 523 |
std::mem::take(&mut base_info.file) |
| 524 |
else { |
| 525 |
panic!("the base file should have been annotated in step 4") |
| 526 |
}; |
| 527 |
// Don't use the lines from the graph if they had whitespace cleaned. |
| 528 |
let lines = base_file_original_lines.unwrap_or(lines); |
| 529 |
// Record which revisions appear in the output, and so must be converted. |
| 530 |
for &Annotation { id, .. } in &annotations { |
| 531 |
graph[id].revision = ChangelogRevisionState::Needed; |
| 532 |
} |
| 533 |
// Use the same object for all ancestor checks, since it internally |
| 534 |
// builds a hash set of seen revisions. |
| 535 |
let mut ancestors = ancestor_iter(&state, changelog_revision, None); |
| 536 |
// Do ancestor checks on all linkrevs. This is worthwhile even if they're |
| 537 |
// `ChangelogRevisionState::NotNeeded`, because it will yield better |
| 538 |
// `descendant`s for adjusting others. We go in topological order (older to |
| 539 |
// newer) so that we populate the ancestor bitset in a tight loop early on. |
| 540 |
for &id in &topological_order { |
| 541 |
if let Some(revision) = |
| 542 |
check_link_revision(&state, &fls, id, &mut ancestors)? |
| 543 |
{ |
| 544 |
graph[id].revision = ChangelogRevisionState::Done(revision); |
| 545 |
} |
| 546 |
} |
| 547 |
// Iterate in reverse topological order so that we visits nodes after their |
| 548 |
// children, that way we can propagate `descendant` correctly. |
| 549 |
for &id in topological_order.iter().rev() { |
| 550 |
let info = &mut graph[id]; |
| 551 |
let descendant = |
| 552 |
info.descendant.expect("descendant set by prior iteration"); |
| 553 |
let propagate = match info.revision { |
| 554 |
ChangelogRevisionState::NotNeeded => descendant, |
| 555 |
ChangelogRevisionState::Done(revision) => revision, |
| 556 |
ChangelogRevisionState::Needed => { |
| 557 |
let revision = |
| 558 |
adjust_link_revision(&state, &fls, descendant, id)?; |
| 559 |
info.revision = ChangelogRevisionState::Done(revision); |
| 560 |
revision |
| 561 |
} |
| 562 |
}; |
| 563 |
for id in info.parents.clone().expect("parents set in step 2") { |
| 564 |
let descendant = &mut graph[id].descendant; |
| 565 |
// If the parent had other descendants, choose the smallest one |
| 566 |
// because we want to skip over as much as possible. |
| 567 |
*descendant = Some(descendant.unwrap_or(propagate).min(propagate)); |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
// Step 6: Convert to `ChangesetAnnotatedFile`. |
| 572 |
let mut changeset_annotations = Vec::with_capacity(annotations.len()); |
| 573 |
for Annotation { id, line_number } in annotations { |
| 574 |
changeset_annotations.push(ChangesetAnnotation { |
| 575 |
path: match id { |
| 576 |
FileId::Wdir => path.into(), |
| 577 |
FileId::Rev(id) => fls.get(id.index).path.clone(), |
| 578 |
}, |
| 579 |
revision: match graph[id].revision { |
| 580 |
ChangelogRevisionState::Done(revision) => revision, |
| 581 |
_ => unreachable!(), |
| 582 |
}, |
| 583 |
line_number, |
| 584 |
}); |
| 585 |
} |
| 586 |
Ok(AnnotateOutput::Text(ChangesetAnnotatedFile { |
| 587 |
lines: lines.get().iter().map(ToOwned::to_owned).collect(), |
| 588 |
annotations: changeset_annotations, |
| 589 |
})) |
| 590 |
} |
| 591 |
|
| 592 |
/// Annotates a file by diffing against its parents, attributing changed lines |
| 593 |
/// to `id`, and copying ids from the parent results for unchanged lines. |
| 594 |
/// If there are two parents and a line is unchanged in both diffs, p2 wins. |
| 595 |
fn annotate_pair( |
| 596 |
id: FileId, |
| 597 |
lines: OwnedLines, |
| 598 |
parents: Vec<&AnnotatedFile>, |
| 599 |
) -> Result<AnnotatedFile, HgError> { |
| 600 |
let len = lines.get().len(); |
| 601 |
let mut annotations = Vec::with_capacity(len); |
| 602 |
for line_number in 1..(len + 1) as u32 { |
| 603 |
annotations.push(Annotation { id, line_number }); |
| 604 |
} |
| 605 |
for parent in parents { |
| 606 |
for bdiff::Hunk { a1, a2, b1, b2 } in |
| 607 |
bdiff::diff(parent.lines.get(), lines.get())?.iter() |
| 608 |
{ |
| 609 |
for (a, b) in (a1..a2).zip(b1..b2) { |
| 610 |
annotations[b as usize] = parent.annotations[a as usize]; |
| 611 |
} |
| 612 |
} |
| 613 |
} |
| 614 |
Ok(AnnotatedFile { lines, annotations }) |
| 615 |
} |
| 616 |
|
| 617 |
/// Creates an iterator over the ancestors of `base_revision` (inclusive), |
| 618 |
/// stopping at `stop_revision` if provided. Panics if `base_revision` is null. |
| 619 |
fn ancestor_iter<'a>( |
| 620 |
state: &'a RepoState<'a>, |
| 621 |
base_revision: RevisionOrWdir, |
| 622 |
stop_revision: Option<Revision>, |
| 623 |
) -> AncestorsIterator<&'a Changelog> { |
| 624 |
let base_revisions: &[Revision] = match base_revision.exclude_wdir() { |
| 625 |
Some(rev) => &[rev], |
| 626 |
None => &state.dirstate_parents(), |
| 627 |
}; |
| 628 |
let stop_revision = stop_revision.unwrap_or(NULL_REVISION); |
| 629 |
AncestorsIterator::new( |
| 630 |
&*state.changelog, |
| 631 |
base_revisions.iter().copied(), |
| 632 |
stop_revision, |
| 633 |
true, |
| 634 |
) |
| 635 |
.expect("base_revision should not be null") |
| 636 |
} |
| 637 |
|
| 638 |
/// If the linkrev of `id` is in `ancestors`, returns it. |
| 639 |
fn check_link_revision( |
| 640 |
state: &RepoState<'_>, |
| 641 |
fls: &FilelogSet, |
| 642 |
id: FileId, |
| 643 |
ancestors: &mut AncestorsIterator<&Changelog>, |
| 644 |
) -> Result<Option<RevisionOrWdir>, HgError> { |
| 645 |
let id = match id { |
| 646 |
FileId::Rev(id) => id, |
| 647 |
FileId::Wdir => return Ok(Some(RevisionOrWdir::wdir())), |
| 648 |
}; |
| 649 |
let FilelogSetItem { filelog, .. } = fls.get(id.index); |
| 650 |
let linkrev = |
| 651 |
filelog.revlog.link_revision(id.revision, &state.changelog.revlog)?; |
| 652 |
if ancestors.contains(linkrev).map_err(from_graph_error)? { |
| 653 |
return Ok(Some(linkrev.into())); |
| 654 |
} |
| 655 |
Ok(None) |
| 656 |
} |
| 657 |
|
| 658 |
/// Finds and returns the first ancestor of `descendant` that introduced `id` |
| 659 |
/// by scanning the changelog. |
| 660 |
fn adjust_link_revision( |
| 661 |
state: &RepoState<'_>, |
| 662 |
fls: &FilelogSet, |
| 663 |
descendant: RevisionOrWdir, |
| 664 |
id: FileId, |
| 665 |
) -> Result<RevisionOrWdir, HgError> { |
| 666 |
let id = match id { |
| 667 |
FileId::Rev(id) => id, |
| 668 |
FileId::Wdir => return Ok(RevisionOrWdir::wdir()), |
| 669 |
}; |
| 670 |
let FilelogSetItem { filelog, path } = fls.get(id.index); |
| 671 |
let linkrev = |
| 672 |
filelog.revlog.link_revision(id.revision, &state.changelog.revlog)?; |
| 673 |
let file_node = *filelog.revlog.node_from_rev(id.revision); |
| 674 |
for ancestor in ancestor_iter(state, descendant, Some(linkrev)) { |
| 675 |
let ancestor = ancestor.map_err(from_graph_error)?; |
| 676 |
let data = state.changelog.entry(ancestor)?.data()?; |
| 677 |
if data.files().contains(&path.as_ref()) { |
| 678 |
let manifest_rev = state |
| 679 |
.manifestlog |
| 680 |
.revlog |
| 681 |
.rev_from_node(data.manifest_node()?.into())?; |
| 682 |
if let Some(entry) = state |
| 683 |
.manifestlog |
| 684 |
.inexact_data_delta_parents(manifest_rev)? |
| 685 |
.find_by_path(path)? |
| 686 |
{ |
| 687 |
if entry.node_id()? == file_node { |
| 688 |
return Ok(ancestor.into()); |
| 689 |
} |
| 690 |
} |
| 691 |
} |
| 692 |
} |
| 693 |
// In theory this should be unreachable. But in case it happens, return the |
| 694 |
// linkrev. This matches _adjustlinkrev in context.py. |
| 695 |
Ok(linkrev.into()) |
| 696 |
} |
| 697 |
|
| 698 |
/// Converts a [`GraphError`] to an [`HgError`]. |
| 699 |
fn from_graph_error(err: GraphError) -> HgError { |
| 700 |
HgError::corrupted(err.to_string()) |
| 701 |
} |