Skip to main content

git_proc/
rev_list.rs

1use std::path::Path;
2
3/// Create a new `git rev-list` command builder.
4#[must_use]
5pub fn new() -> RevList<'static> {
6    RevList::new()
7}
8
9/// Builder for `git rev-list` command.
10///
11/// See `git rev-list --help` for full documentation.
12#[derive(Debug)]
13pub struct RevList<'a> {
14    repo_path: Option<&'a Path>,
15    topo_order: bool,
16    reverse: bool,
17    max_count: Option<usize>,
18    commits: Vec<&'a str>,
19}
20
21crate::impl_repo_path!(RevList);
22
23impl<'a> RevList<'a> {
24    #[must_use]
25    fn new() -> Self {
26        Self {
27            repo_path: None,
28            topo_order: false,
29            reverse: false,
30            max_count: None,
31            commits: Vec::new(),
32        }
33    }
34
35    crate::flag_methods! {
36        /// Show commits in topological order.
37        ///
38        /// Corresponds to `--topo-order`.
39        pub fn topo_order / topo_order_if, topo_order, "Conditionally show commits in topological order."
40    }
41
42    crate::flag_methods! {
43        /// Output commits in reverse order.
44        ///
45        /// Corresponds to `--reverse`.
46        pub fn reverse / reverse_if, reverse, "Conditionally output commits in reverse order."
47    }
48
49    /// Limit the number of commits to output.
50    ///
51    /// Corresponds to `--max-count` or `-n`.
52    #[must_use]
53    pub fn max_count(mut self, count: usize) -> Self {
54        self.max_count = Some(count);
55        self
56    }
57
58    /// Add a commit or range to list.
59    #[must_use]
60    pub fn commit(mut self, commit: &'a str) -> Self {
61        self.commits.push(commit);
62        self
63    }
64}
65
66impl Default for RevList<'_> {
67    fn default() -> Self {
68        Self::new()
69    }
70}
71
72impl crate::Build for RevList<'_> {
73    fn build(self) -> cmd_proc::Command {
74        crate::base_command(self.repo_path)
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            repo_path: self.repo_path,
89            topo_order: self.topo_order,
90            reverse: self.reverse,
91            max_count: self.max_count,
92            commits: self.commits.clone(),
93        });
94        command.test_eq(other);
95    }
96}