Show More
@@ -0,0 +1,85 b'' | |||||
|
1 | // list_tracked_files.rs | |||
|
2 | // | |||
|
3 | // Copyright 2020 Antoine Cezar <antoine.cezar@octobus.net> | |||
|
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 super::find_root; | |||
|
9 | use crate::dirstate::parsers::parse_dirstate; | |||
|
10 | use crate::utils::hg_path::HgPath; | |||
|
11 | use crate::{DirstateParseError, EntryState}; | |||
|
12 | use rayon::prelude::*; | |||
|
13 | use std::convert::From; | |||
|
14 | use std::fmt; | |||
|
15 | use std::fs; | |||
|
16 | use std::io; | |||
|
17 | use std::path::PathBuf; | |||
|
18 | ||||
|
19 | /// Kind of error encoutered by ListTrackedFiles | |||
|
20 | #[derive(Debug)] | |||
|
21 | pub enum ListTrackedFilesErrorKind { | |||
|
22 | ParseError(DirstateParseError), | |||
|
23 | } | |||
|
24 | ||||
|
25 | /// A ListTrackedFiles error | |||
|
26 | #[derive(Debug)] | |||
|
27 | pub struct ListTrackedFilesError { | |||
|
28 | /// Kind of error encoutered by ListTrackedFiles | |||
|
29 | pub kind: ListTrackedFilesErrorKind, | |||
|
30 | } | |||
|
31 | ||||
|
32 | impl std::error::Error for ListTrackedFilesError {} | |||
|
33 | ||||
|
34 | impl fmt::Display for ListTrackedFilesError { | |||
|
35 | fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { | |||
|
36 | unimplemented!() | |||
|
37 | } | |||
|
38 | } | |||
|
39 | ||||
|
40 | impl From<ListTrackedFilesErrorKind> for ListTrackedFilesError { | |||
|
41 | fn from(kind: ListTrackedFilesErrorKind) -> Self { | |||
|
42 | ListTrackedFilesError { kind } | |||
|
43 | } | |||
|
44 | } | |||
|
45 | ||||
|
46 | /// List files under Mercurial control in the working directory | |||
|
47 | pub struct ListTrackedFiles { | |||
|
48 | root: PathBuf, | |||
|
49 | } | |||
|
50 | ||||
|
51 | impl ListTrackedFiles { | |||
|
52 | pub fn new() -> Result<Self, find_root::FindRootError> { | |||
|
53 | let root = find_root::FindRoot::new().run()?; | |||
|
54 | Ok(ListTrackedFiles { root }) | |||
|
55 | } | |||
|
56 | ||||
|
57 | /// Load the tracked files data from disk | |||
|
58 | pub fn load(&self) -> Result<ListDirstateTrackedFiles, io::Error> { | |||
|
59 | let dirstate = &self.root.join(".hg/dirstate"); | |||
|
60 | let content = fs::read(&dirstate)?; | |||
|
61 | Ok(ListDirstateTrackedFiles { content }) | |||
|
62 | } | |||
|
63 | } | |||
|
64 | ||||
|
65 | /// List files under Mercurial control in the working directory | |||
|
66 | /// by reading the dirstate | |||
|
67 | pub struct ListDirstateTrackedFiles { | |||
|
68 | content: Vec<u8>, | |||
|
69 | } | |||
|
70 | ||||
|
71 | impl ListDirstateTrackedFiles { | |||
|
72 | pub fn run(&self) -> Result<Vec<&HgPath>, ListTrackedFilesError> { | |||
|
73 | let (_, entries, _) = parse_dirstate(&self.content) | |||
|
74 | .map_err(ListTrackedFilesErrorKind::ParseError)?; | |||
|
75 | let mut files: Vec<&HgPath> = entries | |||
|
76 | .into_iter() | |||
|
77 | .filter_map(|(path, entry)| match entry.state { | |||
|
78 | EntryState::Removed => None, | |||
|
79 | _ => Some(path), | |||
|
80 | }) | |||
|
81 | .collect(); | |||
|
82 | files.par_sort_unstable(); | |||
|
83 | Ok(files) | |||
|
84 | } | |||
|
85 | } |
@@ -4,7 +4,11 b'' | |||||
4 |
|
4 | |||
5 | mod dirstate_status; |
|
5 | mod dirstate_status; | |
6 | mod find_root; |
|
6 | mod find_root; | |
|
7 | mod list_tracked_files; | |||
7 | pub use find_root::{FindRoot, FindRootError, FindRootErrorKind}; |
|
8 | pub use find_root::{FindRoot, FindRootError, FindRootErrorKind}; | |
|
9 | pub use list_tracked_files::{ | |||
|
10 | ListTrackedFiles, ListTrackedFilesError, ListTrackedFilesErrorKind, | |||
|
11 | }; | |||
8 |
|
12 | |||
9 | // TODO add an `Operation` trait when GAT have landed (rust #44265): |
|
13 | // TODO add an `Operation` trait when GAT have landed (rust #44265): | |
10 | // there is no way to currently define a trait which can both return |
|
14 | // there is no way to currently define a trait which can both return |
General Comments 0
You need to be logged in to leave comments.
Login now