| 1 |
// list_tracked_files.rs |
| 2 |
// |
| 3 |
// Copyright 2020 Antoine Cezar <[email protected]> |
| 4 |
// |
| 5 |
// This software may be used and distributed according to the terms of the |
| 6 |
// GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
use crate::errors::HgError; |
| 9 |
use crate::matchers::Matcher; |
| 10 |
use crate::matchers::VisitChildrenSet; |
| 11 |
use crate::repo::Repo; |
| 12 |
use crate::revlog::manifest::Manifest; |
| 13 |
use crate::revlog::manifest::ManifestFlags; |
| 14 |
use crate::revlog::RevlogError; |
| 15 |
use crate::utils::filter_map_results; |
| 16 |
use crate::utils::hg_path::HgPath; |
| 17 |
use crate::Node; |
| 18 |
use crate::UncheckedRevision; |
| 19 |
|
| 20 |
/// List files under Mercurial control at a given revset. |
| 21 |
pub fn list_revset_tracked_files<M: Matcher>( |
| 22 |
repo: &Repo, |
| 23 |
revset: &str, |
| 24 |
narrow_matcher: M, |
| 25 |
) -> Result<FilesForRev<M>, RevlogError> { |
| 26 |
match crate::revset::resolve_single(revset, repo)?.exclude_wdir() { |
| 27 |
Some(rev) => list_rev_tracked_files(repo, rev.into(), narrow_matcher), |
| 28 |
None => { |
| 29 |
Err(HgError::unsupported("list wdir files not implemented").into()) |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
/// List files under Mercurial control at a given revision. |
| 35 |
pub fn list_rev_tracked_files<M: Matcher>( |
| 36 |
repo: &Repo, |
| 37 |
rev: UncheckedRevision, |
| 38 |
narrow_matcher: M, |
| 39 |
) -> Result<FilesForRev<M>, RevlogError> { |
| 40 |
// TODO move this to the repo itself |
| 41 |
// This implies storing the narrow matcher in the repo, bubbling up the |
| 42 |
// errors and warnings, so it's a bit of churn. In the meantime, the repo |
| 43 |
// method will error out on narrowed manifests. |
| 44 |
let manifest = match repo.manifest_for_rev(rev) { |
| 45 |
Ok(manifest) => manifest, |
| 46 |
Err(e) => match e { |
| 47 |
RevlogError::InvalidRevision(_) => { |
| 48 |
let outside_of_current_narrow_spec = narrow_matcher |
| 49 |
.visit_children_set(HgPath::new("")) |
| 50 |
== VisitChildrenSet::Empty; |
| 51 |
if outside_of_current_narrow_spec { |
| 52 |
// Fake a manifest for a manifest whose node is known, but |
| 53 |
// which doesn't exist because it's empty after narrowing |
| 54 |
Manifest::empty() |
| 55 |
} else { |
| 56 |
return Err(e); |
| 57 |
} |
| 58 |
} |
| 59 |
_ => return Err(e), |
| 60 |
}, |
| 61 |
}; |
| 62 |
Ok(FilesForRev { manifest, narrow_matcher }) |
| 63 |
} |
| 64 |
|
| 65 |
pub struct FilesForRev<M> { |
| 66 |
manifest: Manifest, |
| 67 |
narrow_matcher: M, |
| 68 |
} |
| 69 |
|
| 70 |
pub struct FilesForRevBorrowed<'a> { |
| 71 |
manifest: &'a Manifest, |
| 72 |
narrow_matcher: &'a dyn Matcher, |
| 73 |
} |
| 74 |
|
| 75 |
/// Like [`crate::revlog::manifest::ManifestEntry`], but with the `Node` |
| 76 |
/// already checked. |
| 77 |
pub type ExpandedManifestEntry<'a> = (&'a HgPath, Node, ManifestFlags); |
| 78 |
|
| 79 |
impl<M: Matcher> FilesForRev<M> { |
| 80 |
pub fn iter( |
| 81 |
&self, |
| 82 |
) -> impl Iterator<Item = Result<ExpandedManifestEntry, HgError>> { |
| 83 |
filter_map_results(self.manifest.iter(), |entry| { |
| 84 |
let path = entry.path; |
| 85 |
Ok(if self.narrow_matcher.matches(path) { |
| 86 |
Some((path, entry.node_id()?, entry.flags)) |
| 87 |
} else { |
| 88 |
None |
| 89 |
}) |
| 90 |
}) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
type IterResult<'a> = Result<ExpandedManifestEntry<'a>, HgError>; |
| 95 |
|
| 96 |
impl<'a> FilesForRevBorrowed<'a> { |
| 97 |
pub fn new( |
| 98 |
manifest: &'a Manifest, |
| 99 |
narrow_matcher: &'a impl Matcher, |
| 100 |
) -> Self { |
| 101 |
Self { manifest, narrow_matcher } |
| 102 |
} |
| 103 |
pub fn iter(&self) -> impl Iterator<Item = IterResult<'a>> + use<'a> { |
| 104 |
filter_map_results(self.manifest.iter(), |entry| { |
| 105 |
let path = entry.path; |
| 106 |
Ok(if self.narrow_matcher.matches(path) { |
| 107 |
Some((path, entry.node_id()?, entry.flags)) |
| 108 |
} else { |
| 109 |
None |
| 110 |
}) |
| 111 |
}) |
| 112 |
} |
| 113 |
} |