hglib/commands/
import.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, Prompt, Runner};
6use crate::{runcommand, runcommand_with_prompt, MkArg};
7
8pub struct Arg<'a> {
9    pub patches: &'a [&'a str],
10    pub strip: Option<u32>,
11    pub force: bool,
12    pub nocommit: bool,
13    pub bypass: bool,
14    pub exact: bool,
15    pub importbranch: bool,
16    pub message: &'a str,
17    pub date: &'a str,
18    pub user: &'a str,
19    pub similarity: Option<u8>,
20}
21
22impl<'a> Default for Arg<'a> {
23    fn default() -> Self {
24        Self {
25            patches: &[],
26            strip: None,
27            force: false,
28            nocommit: false,
29            bypass: false,
30            exact: false,
31            importbranch: false,
32            message: "",
33            date: "",
34            user: "",
35            similarity: None,
36        }
37    }
38}
39
40struct ImportPrompt<'a> {
41    patch: &'a [u8],
42    pos: usize,
43}
44
45impl<'a> ImportPrompt<'a> {
46    fn new(patch: &'a str) -> Self {
47        Self {
48            patch: patch.as_bytes(),
49            pos: 0,
50        }
51    }
52}
53
54impl<'a> Prompt for ImportPrompt<'a> {
55    fn call(&mut self, size: usize) -> &[u8] {
56        if self.pos < self.patch.len() {
57            let end = self.patch.len().min(self.pos + size);
58            let buf = &self.patch[self.pos..end];
59            self.pos = end;
60
61            buf
62        } else {
63            &[]
64        }
65    }
66}
67
68impl<'a> Arg<'a> {
69    fn run(&self, client: &mut Client) -> Result<(Vec<u8>, i32), HglibError> {
70        if self.patches.len() == 1 && self.patches[0].as_bytes().iter().any(|c| *c == b'\n') {
71            let prompt = ImportPrompt::new(self.patches[0]);
72            runcommand_with_prompt!(
73                client,
74                "import",
75                prompt,
76                &["-"],
77                "--strip",
78                self.strip,
79                "--force",
80                self.force,
81                "--no-commit",
82                self.nocommit,
83                "--bypass",
84                self.bypass,
85                "--exact",
86                self.exact,
87                "--import-branch",
88                self.importbranch,
89                "--message",
90                self.message,
91                "--date",
92                self.date,
93                "--user",
94                self.user,
95                "--similarity",
96                self.similarity
97            )
98        } else {
99            runcommand!(
100                client,
101                "import",
102                self.patches,
103                "--strip",
104                self.strip,
105                "--force",
106                self.force,
107                "--no-commit",
108                self.nocommit,
109                "--bypass",
110                self.bypass,
111                "--exact",
112                self.exact,
113                "--import-branch",
114                self.importbranch,
115                "--message",
116                self.message,
117                "--date",
118                self.date,
119                "--user",
120                self.user,
121                "--similarity",
122                self.similarity
123            )
124        }
125    }
126}
127
128impl Client {
129    pub fn import(&mut self, x: Arg) -> Result<Vec<u8>, HglibError> {
130        let (data, _) = x.run(self)?;
131        Ok(data)
132    }
133}