| 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 std::cmp::Ordering; |
| 9 |
|
| 10 |
use itertools::put_back; |
| 11 |
use itertools::PutBack; |
| 12 |
|
| 13 |
use crate::errors::HgError; |
| 14 |
use crate::repo::Repo; |
| 15 |
use crate::revlog::manifest::Manifest; |
| 16 |
use crate::revlog::manifest::ManifestEntry; |
| 17 |
use crate::revlog::Node; |
| 18 |
use crate::revlog::RevlogError; |
| 19 |
use crate::utils::hg_path::HgPath; |
| 20 |
use crate::utils::RawData; |
| 21 |
|
| 22 |
pub struct CatOutput<'a> { |
| 23 |
/// Whether any file in the manifest matched the paths given as CLI |
| 24 |
/// arguments |
| 25 |
pub found_any: bool, |
| 26 |
/// The contents of matching files, in manifest order |
| 27 |
pub results: Vec<(&'a HgPath, RawData)>, |
| 28 |
/// Which of the CLI arguments did not match any manifest file |
| 29 |
pub missing: Vec<&'a HgPath>, |
| 30 |
/// The node ID that the given revset was resolved to |
| 31 |
pub node: Node, |
| 32 |
} |
| 33 |
|
| 34 |
// Find an item in an iterator over a sorted collection. |
| 35 |
fn find_item<'a>( |
| 36 |
i: &mut PutBack<impl Iterator<Item = Result<ManifestEntry<'a>, HgError>>>, |
| 37 |
needle: &HgPath, |
| 38 |
) -> Result<Option<Node>, HgError> { |
| 39 |
loop { |
| 40 |
match i.next() { |
| 41 |
None => return Ok(None), |
| 42 |
Some(result) => { |
| 43 |
let entry = result?; |
| 44 |
match needle.as_bytes().cmp(entry.path.as_bytes()) { |
| 45 |
Ordering::Less => { |
| 46 |
i.put_back(Ok(entry)); |
| 47 |
return Ok(None); |
| 48 |
} |
| 49 |
Ordering::Greater => continue, |
| 50 |
Ordering::Equal => return Ok(Some(entry.node_id()?)), |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// Tuple of (missing, found) paths in the manifest |
| 58 |
type ManifestQueryResponse<'a> = (Vec<(&'a HgPath, Node)>, Vec<&'a HgPath>); |
| 59 |
|
| 60 |
fn find_files_in_manifest<'query>( |
| 61 |
manifest: &Manifest, |
| 62 |
query: impl Iterator<Item = &'query HgPath>, |
| 63 |
) -> Result<ManifestQueryResponse<'query>, HgError> { |
| 64 |
let mut manifest = put_back(manifest.iter()); |
| 65 |
let mut res = vec![]; |
| 66 |
let mut missing = vec![]; |
| 67 |
|
| 68 |
for file in query { |
| 69 |
match find_item(&mut manifest, file)? { |
| 70 |
None => missing.push(file), |
| 71 |
Some(item) => res.push((file, item)), |
| 72 |
} |
| 73 |
} |
| 74 |
Ok((res, missing)) |
| 75 |
} |
| 76 |
|
| 77 |
/// Output the given revision of files |
| 78 |
/// |
| 79 |
/// * `root`: Repository root |
| 80 |
/// * `rev`: The revision to cat the files from. |
| 81 |
/// * `files`: The files to output. |
| 82 |
pub fn cat<'a>( |
| 83 |
repo: &Repo, |
| 84 |
revset: &str, |
| 85 |
mut files: Vec<&'a HgPath>, |
| 86 |
) -> Result<CatOutput<'a>, RevlogError> { |
| 87 |
let Some(rev) = crate::revset::resolve_single(revset, repo)?.exclude_wdir() |
| 88 |
else { |
| 89 |
return Err(HgError::unsupported("cat wdir not implemented").into()); |
| 90 |
}; |
| 91 |
let manifest = repo.manifest_for_rev(rev.into())?; |
| 92 |
let mut results: Vec<(&'a HgPath, RawData)> = vec![]; |
| 93 |
let node = *repo.changelog()?.node_from_rev(rev); |
| 94 |
let mut found_any = false; |
| 95 |
|
| 96 |
files.sort_unstable(); |
| 97 |
|
| 98 |
let (found, missing) = |
| 99 |
find_files_in_manifest(&manifest, files.into_iter())?; |
| 100 |
|
| 101 |
for (file_path, file_node) in found { |
| 102 |
found_any = true; |
| 103 |
let file_log = repo.filelog(file_path)?; |
| 104 |
results.push(( |
| 105 |
file_path, |
| 106 |
file_log.data_for_node(file_node)?.into_file_data()?, |
| 107 |
)); |
| 108 |
} |
| 109 |
|
| 110 |
Ok(CatOutput { found_any, results, missing, node }) |
| 111 |
} |