Read about our upcoming Code of Conduct on this issue

files.rs 4.49 KB
Newer Older
1
2
use crate::commands::Command;
use crate::error::{CommandError, CommandErrorKind};
3
use crate::ui::utf8_to_local;
4
use crate::ui::Ui;
5
6
7
8
9
use hg::operations::FindRoot;
use hg::operations::{
    ListDirstateTrackedFiles, ListDirstateTrackedFilesError,
    ListDirstateTrackedFilesErrorKind,
};
10
11
12
13
use hg::operations::{
    ListRevTrackedFiles, ListRevTrackedFilesError,
    ListRevTrackedFilesErrorKind,
};
14
use hg::requirements;
15
use hg::utils::files::{get_bytes_from_path, relativize_path};
16
17
use hg::utils::hg_path::{HgPath, HgPathBuf};
use std::path::PathBuf;
18
19
20
21
22
23
24

pub const HELP_TEXT: &str = "
List tracked files.

Returns 0 on success.
";

25
26
pub struct FilesCommand<'a> {
    rev: Option<&'a str>,
27
28
}

29
30
31
32
impl<'a> FilesCommand<'a> {
    pub fn new(rev: Option<&'a str>) -> Self {
        FilesCommand { rev }
    }
33

34
35
36
37
38
39
    fn display_files(
        &self,
        ui: &Ui,
        root: &PathBuf,
        files: impl IntoIterator<Item = &'a HgPath>,
    ) -> Result<(), CommandError> {
40
41
42
        let cwd = std::env::current_dir()
            .or_else(|e| Err(CommandErrorKind::CurrentDirNotFound(e)))?;
        let rooted_cwd = cwd
43
            .strip_prefix(&root)
44
45
46
            .expect("cwd was already checked within the repository");
        let rooted_cwd = HgPathBuf::from(get_bytes_from_path(rooted_cwd));

47
        let mut stdout = ui.stdout_buffer();
48

49
        for file in files {
50
            stdout.write_all(relativize_path(file, &rooted_cwd).as_ref())?;
51
52
53
54
55
56
            stdout.write_all(b"\n")?;
        }
        stdout.flush()?;
        Ok(())
    }
}
57

58
59
60
impl<'a> Command for FilesCommand<'a> {
    fn run(&self, ui: &Ui) -> Result<(), CommandError> {
        let root = FindRoot::new().run()?;
61
        requirements::check(&root)?;
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
        if let Some(rev) = self.rev {
            let mut operation = ListRevTrackedFiles::new(&root, rev)
                .map_err(|e| map_rev_error(rev, e))?;
            let files = operation.run().map_err(|e| map_rev_error(rev, e))?;
            self.display_files(ui, &root, files)
        } else {
            let mut operation = ListDirstateTrackedFiles::new(&root)
                .map_err(map_dirstate_error)?;
            let files = operation.run().map_err(map_dirstate_error)?;
            self.display_files(ui, &root, files)
        }
    }
}

/// Convert `ListRevTrackedFilesErrorKind` to `CommandError`
fn map_rev_error(rev: &str, err: ListRevTrackedFilesError) -> CommandError {
    CommandError {
        kind: match err.kind {
            ListRevTrackedFilesErrorKind::IoError(err) => {
                CommandErrorKind::Abort(Some(
                    utf8_to_local(&format!("abort: {}\n", err)).into(),
                ))
            }
            ListRevTrackedFilesErrorKind::InvalidRevision => {
                CommandErrorKind::Abort(Some(
                    utf8_to_local(&format!(
                        "abort: invalid revision identifier{}\n",
                        rev
                    ))
                    .into(),
                ))
            }
            ListRevTrackedFilesErrorKind::UnsuportedRevlogVersion(version) => {
                CommandErrorKind::Abort(Some(
                    utf8_to_local(&format!(
                        "abort: unsupported revlog version {}\n",
                        version
                    ))
                    .into(),
                ))
            }
            ListRevTrackedFilesErrorKind::CorruptedRevlog => {
                CommandErrorKind::Abort(Some(
                    "abort: corrupted revlog\n".into(),
                ))
            }
            ListRevTrackedFilesErrorKind::UnknowRevlogDataFormat(format) => {
                CommandErrorKind::Abort(Some(
                    utf8_to_local(&format!(
                        "abort: unknow revlog dataformat {:?}\n",
                        format
                    ))
                    .into(),
                ))
            }
        },
    }
}

/// Convert `ListDirstateTrackedFilesError` to `CommandError`
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
fn map_dirstate_error(err: ListDirstateTrackedFilesError) -> CommandError {
    CommandError {
        kind: match err.kind {
            ListDirstateTrackedFilesErrorKind::IoError(err) => {
                CommandErrorKind::Abort(Some(
                    utf8_to_local(&format!("abort: {}\n", err)).into(),
                ))
            }
            ListDirstateTrackedFilesErrorKind::ParseError(_) => {
                CommandErrorKind::Abort(Some(
                    // TODO find a better error message
                    b"abort: parse error\n".to_vec(),
                ))
            }
        },
    }
}