hglib/commands/
diff.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this file,
3// You can obtain one at http://mozilla.org/MPL/2.0/.
4
5use crate::client::{Client, HglibError, Runner};
6use crate::{runcommand, MkArg};
7
8pub struct Arg<'a> {
9    pub files: &'a [&'a str],
10    pub revs: &'a [&'a str],
11    pub change: &'a str,
12    pub text: bool,
13    pub git: bool,
14    pub nodates: bool,
15    pub showfunction: bool,
16    pub reverse: bool,
17    pub ignoreallspace: bool,
18    pub ignorespacechange: bool,
19    pub ignoreblanklines: bool,
20    pub unified: Option<u32>,
21    pub stat: bool,
22    pub subrepos: bool,
23    pub include: &'a [&'a str],
24    pub exclude: &'a [&'a str],
25}
26
27impl<'a> Default for Arg<'a> {
28    fn default() -> Self {
29        Self {
30            files: &[],
31            revs: &[],
32            change: "",
33            text: false,
34            git: false,
35            nodates: false,
36            showfunction: false,
37            reverse: false,
38            ignoreallspace: false,
39            ignorespacechange: false,
40            ignoreblanklines: false,
41            unified: None,
42            stat: false,
43            subrepos: false,
44            include: &[],
45            exclude: &[],
46        }
47    }
48}
49
50impl<'a> Arg<'a> {
51    fn run(&self, client: &mut Client) -> Result<(Vec<u8>, i32), HglibError> {
52        runcommand!(
53            client,
54            "diff",
55            self.files,
56            "-r",
57            self.revs,
58            "-c",
59            self.change,
60            "-a",
61            self.text,
62            "-g",
63            self.git,
64            "--nodates",
65            self.nodates,
66            "-p",
67            self.showfunction,
68            "--reverse",
69            self.reverse,
70            "-w",
71            self.ignoreallspace,
72            "-b",
73            self.ignorespacechange,
74            "-B",
75            self.ignoreblanklines,
76            "-U",
77            self.unified,
78            "--stat",
79            self.stat,
80            "-S",
81            self.subrepos,
82            "-I",
83            self.include,
84            "-X",
85            self.exclude
86        )
87    }
88}
89
90impl Client {
91    pub fn diff(&mut self, x: Arg) -> Result<Vec<u8>, HglibError> {
92        if !x.change.is_empty() && !x.revs.is_empty() {
93            return Err(HglibError::from("Cannot specify both change and rev"));
94        }
95        let (data, _) = x.run(self)?;
96        Ok(data)
97    }
98}