Skip to main content

git_proc/
rev_list.rs

1/// Create a new `git rev-list` command builder.
2#[must_use]
3pub fn new() -> RevList<'static> {
4    RevList::new()
5}
6
7/// Builder for `git rev-list` command.
8///
9/// See `git rev-list --help` for full documentation.
10#[derive(Debug)]
11pub struct RevList<'a> {
12    base: crate::RepoBase<'a>,
13    topo_order: bool,
14    reverse: bool,
15    max_count: Option<usize>,
16    commits: Vec<&'a str>,
17}
18
19crate::impl_repo_base!(RevList);
20
21impl<'a> RevList<'a> {
22    #[must_use]
23    fn new() -> Self {
24        Self {
25            base: crate::RepoBase::default(),
26            topo_order: false,
27            reverse: false,
28            max_count: None,
29            commits: Vec::new(),
30        }
31    }
32
33    crate::flag_methods! {
34        /// Show commits in topological order.
35        ///
36        /// Corresponds to `--topo-order`.
37        pub fn topo_order / topo_order_if, topo_order, "Conditionally show commits in topological order."
38    }
39
40    crate::flag_methods! {
41        /// Output commits in reverse order.
42        ///
43        /// Corresponds to `--reverse`.
44        pub fn reverse / reverse_if, reverse, "Conditionally output commits in reverse order."
45    }
46
47    /// Limit the number of commits to output.
48    ///
49    /// Corresponds to `--max-count` or `-n`.
50    #[must_use]
51    pub fn max_count(mut self, count: usize) -> Self {
52        self.max_count = Some(count);
53        self
54    }
55
56    /// Add a commit or range to list.
57    #[must_use]
58    pub fn commit(mut self, commit: &'a str) -> Self {
59        self.commits.push(commit);
60        self
61    }
62}
63
64impl Default for RevList<'_> {
65    fn default() -> Self {
66        Self::new()
67    }
68}
69
70impl crate::Build for RevList<'_> {
71    fn build(self) -> Result<cmd_proc::Command, crate::EnvError> {
72        Ok(self
73            .base
74            .command()?
75            .argument("rev-list")
76            .optional_flag(self.topo_order, "--topo-order")
77            .optional_flag(self.reverse, "--reverse")
78            .optional_option("--max-count", self.max_count.map(|c| c.to_string()))
79            .arguments(self.commits))
80    }
81}
82
83#[cfg(feature = "test-utils")]
84impl RevList<'_> {
85    /// Compare the built command with another command using debug representation.
86    pub fn test_eq(&self, other: &cmd_proc::Command) {
87        let command = crate::Build::build(Self {
88            base: self.base,
89            topo_order: self.topo_order,
90            reverse: self.reverse,
91            max_count: self.max_count,
92            commits: self.commits.clone(),
93        })
94        .unwrap();
95        command.test_eq(other);
96    }
97}