Skip to main content

git_proc/
diff.rs

1use std::path::Path;
2
3/// Create a new `git diff` command builder.
4#[must_use]
5pub fn new() -> Diff<'static> {
6    Diff::new()
7}
8
9/// Builder for `git diff` command.
10///
11/// See `git diff --help` for full documentation.
12#[derive(Debug)]
13pub struct Diff<'a> {
14    repo_path: Option<&'a Path>,
15    exit_code: bool,
16}
17
18crate::impl_repo_path!(Diff);
19
20impl<'a> Diff<'a> {
21    #[must_use]
22    fn new() -> Self {
23        Self {
24            repo_path: None,
25            exit_code: false,
26        }
27    }
28
29    /// Make the program exit with codes similar to diff(1).
30    ///
31    /// Exits with 1 if there were differences and 0 means no differences.
32    ///
33    /// Corresponds to `--exit-code`.
34    #[must_use]
35    pub fn exit_code(mut self) -> Self {
36        self.exit_code = true;
37        self
38    }
39}
40
41impl Default for Diff<'_> {
42    fn default() -> Self {
43        Self::new()
44    }
45}
46
47impl crate::Build for Diff<'_> {
48    fn build(self) -> cmd_proc::Command {
49        crate::base_command(self.repo_path)
50            .argument("diff")
51            .optional_flag(self.exit_code, "--exit-code")
52    }
53}