Show More
@@ -1,114 +1,117 b'' | |||
|
1 | 1 | // list_tracked_files.rs |
|
2 | 2 | // |
|
3 | 3 | // Copyright 2020 Antoine Cezar <antoine.cezar@octobus.net> |
|
4 | 4 | // |
|
5 | 5 | // This software may be used and distributed according to the terms of the |
|
6 | 6 | // GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | use crate::repo::Repo; |
|
9 | 9 | use crate::revlog::revlog::RevlogError; |
|
10 | 10 | use crate::revlog::Node; |
|
11 | 11 | |
|
12 | 12 | use crate::utils::hg_path::HgPath; |
|
13 | 13 | |
|
14 | 14 | use crate::errors::HgError; |
|
15 | 15 | use crate::manifest::Manifest; |
|
16 | 16 | use crate::manifest::ManifestEntry; |
|
17 | 17 | use itertools::put_back; |
|
18 | 18 | use itertools::PutBack; |
|
19 | 19 | use std::cmp::Ordering; |
|
20 | 20 | |
|
21 | 21 | pub struct CatOutput<'a> { |
|
22 | 22 | /// Whether any file in the manifest matched the paths given as CLI |
|
23 | 23 | /// arguments |
|
24 | 24 | pub found_any: bool, |
|
25 | 25 | /// The contents of matching files, in manifest order |
|
26 | 26 | pub results: Vec<(&'a HgPath, Vec<u8>)>, |
|
27 | 27 | /// Which of the CLI arguments did not match any manifest file |
|
28 | 28 | pub missing: Vec<&'a HgPath>, |
|
29 | 29 | /// The node ID that the given revset was resolved to |
|
30 | 30 | pub node: Node, |
|
31 | 31 | } |
|
32 | 32 | |
|
33 | 33 | // Find an item in an iterator over a sorted collection. |
|
34 | 34 | fn find_item<'a>( |
|
35 | 35 | i: &mut PutBack<impl Iterator<Item = Result<ManifestEntry<'a>, HgError>>>, |
|
36 | 36 | needle: &HgPath, |
|
37 | 37 | ) -> Result<Option<Node>, HgError> { |
|
38 | 38 | loop { |
|
39 | 39 | match i.next() { |
|
40 | 40 | None => return Ok(None), |
|
41 | 41 | Some(result) => { |
|
42 | 42 | let entry = result?; |
|
43 | 43 | match needle.as_bytes().cmp(entry.path.as_bytes()) { |
|
44 | 44 | Ordering::Less => { |
|
45 | 45 | i.put_back(Ok(entry)); |
|
46 | 46 | return Ok(None); |
|
47 | 47 | } |
|
48 | 48 | Ordering::Greater => continue, |
|
49 | 49 | Ordering::Equal => return Ok(Some(entry.node_id()?)), |
|
50 | 50 | } |
|
51 | 51 | } |
|
52 | 52 | } |
|
53 | 53 | } |
|
54 | 54 | } |
|
55 | 55 | |
|
56 | // Tuple of (missing, found) paths in the manifest | |
|
57 | type ManifestQueryResponse<'a> = (Vec<(&'a HgPath, Node)>, Vec<&'a HgPath>); | |
|
58 | ||
|
56 | 59 | fn find_files_in_manifest<'query>( |
|
57 | 60 | manifest: &Manifest, |
|
58 | 61 | query: impl Iterator<Item = &'query HgPath>, |
|
59 | ) -> Result<(Vec<(&'query HgPath, Node)>, Vec<&'query HgPath>), HgError> { | |
|
62 | ) -> Result<ManifestQueryResponse<'query>, HgError> { | |
|
60 | 63 | let mut manifest = put_back(manifest.iter()); |
|
61 | 64 | let mut res = vec![]; |
|
62 | 65 | let mut missing = vec![]; |
|
63 | 66 | |
|
64 | 67 | for file in query { |
|
65 | 68 | match find_item(&mut manifest, file)? { |
|
66 | 69 | None => missing.push(file), |
|
67 | 70 | Some(item) => res.push((file, item)), |
|
68 | 71 | } |
|
69 | 72 | } |
|
70 | 73 | return Ok((res, missing)); |
|
71 | 74 | } |
|
72 | 75 | |
|
73 | 76 | /// Output the given revision of files |
|
74 | 77 | /// |
|
75 | 78 | /// * `root`: Repository root |
|
76 | 79 | /// * `rev`: The revision to cat the files from. |
|
77 | 80 | /// * `files`: The files to output. |
|
78 | 81 | pub fn cat<'a>( |
|
79 | 82 | repo: &Repo, |
|
80 | 83 | revset: &str, |
|
81 | 84 | mut files: Vec<&'a HgPath>, |
|
82 | 85 | ) -> Result<CatOutput<'a>, RevlogError> { |
|
83 | 86 | let rev = crate::revset::resolve_single(revset, repo)?; |
|
84 | 87 | let manifest = repo.manifest_for_rev(rev)?; |
|
85 | 88 | let node = *repo |
|
86 | 89 | .changelog()? |
|
87 | 90 | .node_from_rev(rev) |
|
88 | 91 | .expect("should succeed when repo.manifest did"); |
|
89 | 92 | let mut results: Vec<(&'a HgPath, Vec<u8>)> = vec![]; |
|
90 | 93 | let mut found_any = false; |
|
91 | 94 | |
|
92 | 95 | files.sort_unstable(); |
|
93 | 96 | |
|
94 | 97 | let (found, missing) = find_files_in_manifest( |
|
95 | 98 | &manifest, |
|
96 | 99 | files.into_iter().map(|f| f.as_ref()), |
|
97 | 100 | )?; |
|
98 | 101 | |
|
99 | 102 | for (file_path, file_node) in found { |
|
100 | 103 | found_any = true; |
|
101 | 104 | let file_log = repo.filelog(file_path)?; |
|
102 | 105 | results.push(( |
|
103 | 106 | file_path, |
|
104 | 107 | file_log.data_for_node(file_node)?.into_file_data()?, |
|
105 | 108 | )); |
|
106 | 109 | } |
|
107 | 110 | |
|
108 | 111 | Ok(CatOutput { |
|
109 | 112 | found_any, |
|
110 | 113 | results, |
|
111 | 114 | missing, |
|
112 | 115 | node, |
|
113 | 116 | }) |
|
114 | 117 | } |
General Comments 0
You need to be logged in to leave comments.
Login now