rust/hg-core/src/operations/list_tracked_files.rs
author Simon Sapin <simon.sapin@octobus.net>
Mon, 14 Dec 2020 14:59:23 +0100
changeset 46135 dca9cb99971c
parent 46134 cc6faec62cb7
child 46167 8a4914397d02
permissions -rw-r--r--
rust: replace most "operation" structs with functions The hg-core crate has a partially-formed concept of "operation", represented as structs with constructors and a `run` method. Each struct’s contructor takes different parameters, and each `run` has a different return type. Constructors typically don’t do much more than store parameters for `run` to access them. There was a comment about adding an `Operation` trait when the language supports expressing something so general, but it’s hard to imagine how operations with such different APIs could be used in a generic context. This commit starts removing the concept of "operation", since those are pretty much just functions. Differential Revision: https://phab.mercurial-scm.org/D9595
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45359
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     1
// list_tracked_files.rs
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     2
//
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     3
// Copyright 2020 Antoine Cezar <antoine.cezar@octobus.net>
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     4
//
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     5
// This software may be used and distributed according to the terms of the
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     6
// GNU General Public License version 2 or any later version.
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     7
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     8
use crate::dirstate::parsers::parse_dirstate;
45536
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
     9
use crate::revlog::changelog::Changelog;
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    10
use crate::revlog::manifest::{Manifest, ManifestEntry};
46033
88e741bf2d93 rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents: 46032
diff changeset
    11
use crate::revlog::node::{Node, NodePrefix};
45536
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    12
use crate::revlog::revlog::RevlogError;
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    13
use crate::revlog::Revision;
45359
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    14
use crate::utils::hg_path::HgPath;
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    15
use crate::{DirstateParseError, EntryState};
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    16
use rayon::prelude::*;
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    17
use std::convert::From;
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    18
use std::fs;
46134
cc6faec62cb7 rust: change &PathBuf parameters to &Path
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
    19
use std::path::Path;
45359
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    20
45535
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    21
/// Kind of error encountered by `ListDirstateTrackedFiles`
45359
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    22
#[derive(Debug)]
45535
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    23
pub enum ListDirstateTrackedFilesErrorKind {
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    24
    /// Error when reading the `dirstate` file
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    25
    IoError(std::io::Error),
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    26
    /// Error when parsing the `dirstate` file
45359
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    27
    ParseError(DirstateParseError),
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    28
}
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    29
45535
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    30
/// A `ListDirstateTrackedFiles` error
45359
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    31
#[derive(Debug)]
45535
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    32
pub struct ListDirstateTrackedFilesError {
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    33
    /// Kind of error encountered by `ListDirstateTrackedFiles`
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    34
    pub kind: ListDirstateTrackedFilesErrorKind,
45359
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    35
}
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    36
45535
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    37
impl From<ListDirstateTrackedFilesErrorKind>
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    38
    for ListDirstateTrackedFilesError
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    39
{
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    40
    fn from(kind: ListDirstateTrackedFilesErrorKind) -> Self {
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    41
        ListDirstateTrackedFilesError { kind }
45359
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    42
    }
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    43
}
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    44
45535
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    45
impl From<std::io::Error> for ListDirstateTrackedFilesError {
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    46
    fn from(err: std::io::Error) -> Self {
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    47
        let kind = ListDirstateTrackedFilesErrorKind::IoError(err);
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    48
        ListDirstateTrackedFilesError { kind }
45436
1b3197047f5c rhg: make output of `files` relative to the current directory and the root
Raphaël Gomès <rgomes@octobus.net>
parents: 45359
diff changeset
    49
    }
45359
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    50
}
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    51
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    52
/// List files under Mercurial control in the working directory
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    53
/// by reading the dirstate
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
    54
pub struct Dirstate {
45535
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    55
    /// The `dirstate` content.
45359
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    56
    content: Vec<u8>,
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    57
}
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    58
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
    59
impl Dirstate {
46134
cc6faec62cb7 rust: change &PathBuf parameters to &Path
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
    60
    pub fn new(root: &Path) -> Result<Self, ListDirstateTrackedFilesError> {
45535
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    61
        let dirstate = root.join(".hg/dirstate");
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    62
        let content = fs::read(&dirstate)?;
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    63
        Ok(Self { content })
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    64
    }
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    65
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
    66
    pub fn tracked_files(
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
    67
        &self,
45535
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    68
    ) -> Result<Vec<&HgPath>, ListDirstateTrackedFilesError> {
45359
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    69
        let (_, entries, _) = parse_dirstate(&self.content)
45535
72b7d58d6e35 hg-core: simplify `list_tracked_files` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45436
diff changeset
    70
            .map_err(ListDirstateTrackedFilesErrorKind::ParseError)?;
45359
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    71
        let mut files: Vec<&HgPath> = entries
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    72
            .into_iter()
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    73
            .filter_map(|(path, entry)| match entry.state {
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    74
                EntryState::Removed => None,
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    75
                _ => Some(path),
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    76
            })
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    77
            .collect();
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    78
        files.par_sort_unstable();
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    79
        Ok(files)
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    80
    }
0f5286ccf82c hg-core: define a `ListTrackedFiles` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    81
}
45536
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    82
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    83
/// Kind of error encountered by `ListRevTrackedFiles`
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    84
#[derive(Debug)]
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    85
pub enum ListRevTrackedFilesErrorKind {
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    86
    /// Error when reading a `revlog` file.
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    87
    IoError(std::io::Error),
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    88
    /// The revision has not been found.
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    89
    InvalidRevision,
46032
8d6164098782 rhg: allow specifying a changeset ID prefix
Simon Sapin <simon-commits@exyr.org>
parents: 45603
diff changeset
    90
    /// Found more than one revision whose ID match the requested prefix
8d6164098782 rhg: allow specifying a changeset ID prefix
Simon Sapin <simon-commits@exyr.org>
parents: 45603
diff changeset
    91
    AmbiguousPrefix,
45536
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    92
    /// A `revlog` file is corrupted.
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    93
    CorruptedRevlog,
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    94
    /// The `revlog` format version is not supported.
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    95
    UnsuportedRevlogVersion(u16),
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    96
    /// The `revlog` data format is not supported.
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    97
    UnknowRevlogDataFormat(u8),
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    98
}
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
    99
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   100
/// A `ListRevTrackedFiles` error
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   101
#[derive(Debug)]
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   102
pub struct ListRevTrackedFilesError {
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   103
    /// Kind of error encountered by `ListRevTrackedFiles`
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   104
    pub kind: ListRevTrackedFilesErrorKind,
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   105
}
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   106
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   107
impl From<ListRevTrackedFilesErrorKind> for ListRevTrackedFilesError {
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   108
    fn from(kind: ListRevTrackedFilesErrorKind) -> Self {
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   109
        ListRevTrackedFilesError { kind }
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   110
    }
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   111
}
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   112
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   113
impl From<RevlogError> for ListRevTrackedFilesError {
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   114
    fn from(err: RevlogError) -> Self {
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   115
        match err {
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   116
            RevlogError::IoError(err) => {
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   117
                ListRevTrackedFilesErrorKind::IoError(err)
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   118
            }
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   119
            RevlogError::UnsuportedVersion(version) => {
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   120
                ListRevTrackedFilesErrorKind::UnsuportedRevlogVersion(version)
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   121
            }
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   122
            RevlogError::InvalidRevision => {
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   123
                ListRevTrackedFilesErrorKind::InvalidRevision
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   124
            }
46032
8d6164098782 rhg: allow specifying a changeset ID prefix
Simon Sapin <simon-commits@exyr.org>
parents: 45603
diff changeset
   125
            RevlogError::AmbiguousPrefix => {
8d6164098782 rhg: allow specifying a changeset ID prefix
Simon Sapin <simon-commits@exyr.org>
parents: 45603
diff changeset
   126
                ListRevTrackedFilesErrorKind::AmbiguousPrefix
8d6164098782 rhg: allow specifying a changeset ID prefix
Simon Sapin <simon-commits@exyr.org>
parents: 45603
diff changeset
   127
            }
45536
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   128
            RevlogError::Corrupted => {
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   129
                ListRevTrackedFilesErrorKind::CorruptedRevlog
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   130
            }
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   131
            RevlogError::UnknowDataFormat(format) => {
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   132
                ListRevTrackedFilesErrorKind::UnknowRevlogDataFormat(format)
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   133
            }
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   134
        }
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   135
        .into()
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   136
    }
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   137
}
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   138
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   139
/// List files under Mercurial control at a given revision.
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   140
pub fn list_rev_tracked_files(
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   141
    root: &Path,
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   142
    rev: &str,
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   143
) -> Result<FilesForRev, ListRevTrackedFilesError> {
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   144
    let changelog = Changelog::open(root)?;
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   145
    let manifest = Manifest::open(root)?;
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   146
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   147
    let changelog_entry = match rev.parse::<Revision>() {
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   148
        Ok(rev) => changelog.get_rev(rev)?,
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   149
        _ => {
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   150
            let changelog_node = NodePrefix::from_hex(&rev)
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   151
                .or(Err(ListRevTrackedFilesErrorKind::InvalidRevision))?;
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   152
            changelog.get_node(changelog_node.borrow())?
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   153
        }
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   154
    };
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   155
    let manifest_node = Node::from_hex(&changelog_entry.manifest_node()?)
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   156
        .or(Err(ListRevTrackedFilesErrorKind::CorruptedRevlog))?;
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   157
    let manifest_entry = manifest.get_node((&manifest_node).into())?;
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   158
    Ok(FilesForRev(manifest_entry))
45536
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   159
}
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   160
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   161
pub struct FilesForRev(ManifestEntry);
45536
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   162
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   163
impl FilesForRev {
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   164
    pub fn iter(&self) -> impl Iterator<Item = &HgPath> {
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46134
diff changeset
   165
        self.0.files()
45536
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   166
    }
639f33f22faf hg-core: add a `ListRevTrackedFiles` operation
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45535
diff changeset
   167
}