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