| 1 |
use std::borrow::Cow; |
| 2 |
|
| 3 |
use itertools::EitherOrBoth; |
| 4 |
|
| 5 |
use crate::dirstate::status::StatusPath; |
| 6 |
use crate::errors::HgError; |
| 7 |
use crate::matchers::Matcher; |
| 8 |
use crate::repo::Repo; |
| 9 |
use crate::revlog::manifest::Manifest; |
| 10 |
use crate::revlog::manifest::ManifestEntry; |
| 11 |
use crate::utils::filter_map_results; |
| 12 |
use crate::utils::hg_path::HgPath; |
| 13 |
use crate::utils::hg_path::HgPathBuf; |
| 14 |
use crate::utils::merge_join_results_by; |
| 15 |
use crate::Revision; |
| 16 |
use crate::NULL_REVISION; |
| 17 |
|
| 18 |
#[derive(Debug, Copy, Clone)] |
| 19 |
pub enum DiffStatus { |
| 20 |
Removed, |
| 21 |
Added, |
| 22 |
Matching, |
| 23 |
Modified, |
| 24 |
} |
| 25 |
|
| 26 |
/// What copy/rename information to report. |
| 27 |
pub enum ListCopies { |
| 28 |
/// Report copies only for added files. |
| 29 |
Added, |
| 30 |
/// Report copies for files that are added or modified. |
| 31 |
AddedOrModified, |
| 32 |
} |
| 33 |
|
| 34 |
/// Strategy for determining a file's copy source. |
| 35 |
enum CopyStrategy<'a> { |
| 36 |
/// Use the [`Repo`] to look up copy information in filelog metadata. |
| 37 |
/// Assumes we are producing the status for a single changeset. |
| 38 |
Change(&'a Repo), |
| 39 |
// TODO: For --rev --rev --copies use a precomputed copy map |
| 40 |
} |
| 41 |
|
| 42 |
pub struct StatusRevRev<'a, M> { |
| 43 |
manifest1: Manifest, |
| 44 |
manifest2: Manifest, |
| 45 |
narrow_matcher: M, |
| 46 |
copies: Option<(ListCopies, CopyStrategy<'a>)>, |
| 47 |
} |
| 48 |
|
| 49 |
fn manifest_for_rev(repo: &Repo, rev: Revision) -> Result<Manifest, HgError> { |
| 50 |
repo.manifest_for_rev(rev.into()).map_err(|e| { |
| 51 |
HgError::corrupted(format!( |
| 52 |
"manifest lookup failed for revision {}: {}", |
| 53 |
rev, e |
| 54 |
)) |
| 55 |
}) |
| 56 |
} |
| 57 |
|
| 58 |
pub fn status_rev_rev_no_copies<M: Matcher>( |
| 59 |
repo: &Repo, |
| 60 |
rev1: Revision, |
| 61 |
rev2: Revision, |
| 62 |
narrow_matcher: M, |
| 63 |
) -> Result<StatusRevRev<M>, HgError> { |
| 64 |
Ok(StatusRevRev { |
| 65 |
manifest1: manifest_for_rev(repo, rev1)?, |
| 66 |
manifest2: manifest_for_rev(repo, rev2)?, |
| 67 |
narrow_matcher, |
| 68 |
copies: None, |
| 69 |
}) |
| 70 |
} |
| 71 |
|
| 72 |
/// Computes the status of `rev` against its first parent. |
| 73 |
pub fn status_change<M: Matcher>( |
| 74 |
repo: &Repo, |
| 75 |
rev: Revision, |
| 76 |
narrow_matcher: M, |
| 77 |
list_copies: Option<ListCopies>, |
| 78 |
) -> Result<StatusRevRev<M>, HgError> { |
| 79 |
let parent = repo.changelog()?.revlog.get_entry(rev)?.p1(); |
| 80 |
let parent = parent.unwrap_or(NULL_REVISION); |
| 81 |
Ok(StatusRevRev { |
| 82 |
manifest1: manifest_for_rev(repo, parent)?, |
| 83 |
manifest2: manifest_for_rev(repo, rev)?, |
| 84 |
narrow_matcher, |
| 85 |
copies: list_copies.map(|list| (list, CopyStrategy::Change(repo))), |
| 86 |
}) |
| 87 |
} |
| 88 |
|
| 89 |
impl<M: Matcher> StatusRevRev<'_, M> { |
| 90 |
pub fn iter( |
| 91 |
&self, |
| 92 |
) -> impl Iterator<Item = Result<(StatusPath<'_>, DiffStatus), HgError>> |
| 93 |
{ |
| 94 |
let iter1 = self.manifest1.iter(); |
| 95 |
let iter2 = self.manifest2.iter(); |
| 96 |
|
| 97 |
let merged = |
| 98 |
merge_join_results_by(iter1, iter2, |i1, i2| i1.path.cmp(i2.path)); |
| 99 |
|
| 100 |
filter_map_results(merged, |entry| { |
| 101 |
let (path, status) = match &entry { |
| 102 |
EitherOrBoth::Left(entry) => (entry.path, DiffStatus::Removed), |
| 103 |
EitherOrBoth::Right(entry) => (entry.path, DiffStatus::Added), |
| 104 |
EitherOrBoth::Both(entry1, entry2) => { |
| 105 |
let path = entry1.path; |
| 106 |
if entry1.node_id().unwrap() == entry2.node_id().unwrap() |
| 107 |
&& entry1.flags == entry2.flags |
| 108 |
{ |
| 109 |
(path, DiffStatus::Matching) |
| 110 |
} else { |
| 111 |
(path, DiffStatus::Modified) |
| 112 |
} |
| 113 |
} |
| 114 |
}; |
| 115 |
if !self.narrow_matcher.matches(path) { |
| 116 |
return Ok(None); |
| 117 |
} |
| 118 |
let path = StatusPath { |
| 119 |
path: Cow::Borrowed(path), |
| 120 |
copy_source: self |
| 121 |
.find_copy_source(path, status, entry.right().as_ref())? |
| 122 |
.map(Cow::Owned), |
| 123 |
}; |
| 124 |
Ok(Some((path, status))) |
| 125 |
}) |
| 126 |
} |
| 127 |
|
| 128 |
/// Returns the path that a file was copied from, if it should be reported. |
| 129 |
fn find_copy_source( |
| 130 |
&self, |
| 131 |
path: &HgPath, |
| 132 |
status: DiffStatus, |
| 133 |
entry: Option<&ManifestEntry>, |
| 134 |
) -> Result<Option<HgPathBuf>, HgError> { |
| 135 |
let Some(entry) = entry else { return Ok(None) }; |
| 136 |
let Some((list, strategy)) = &self.copies else { |
| 137 |
return Ok(None); |
| 138 |
}; |
| 139 |
match (list, status) { |
| 140 |
(ListCopies::Added, DiffStatus::Added) => {} |
| 141 |
( |
| 142 |
ListCopies::AddedOrModified, |
| 143 |
DiffStatus::Added | DiffStatus::Modified, |
| 144 |
) => {} |
| 145 |
_ => return Ok(None), |
| 146 |
} |
| 147 |
match strategy { |
| 148 |
CopyStrategy::Change(repo) => { |
| 149 |
let data = repo |
| 150 |
.filelog(path)? |
| 151 |
.data_for_node(entry.node_id().unwrap())?; |
| 152 |
if let Some(copy) = data.metadata()?.parse()?.copy { |
| 153 |
if self.manifest1.find_by_path(copy)?.is_some() { |
| 154 |
return Ok(Some(copy.to_owned())); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
Ok(None) |
| 160 |
} |
| 161 |
} |