1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use crate::client::{Client, HglibError, Runner};
use crate::{runcommand, MkArg};
pub struct Arg<'a> {
pub name: &'a str,
pub clean: bool,
pub force: bool,
}
impl<'a> Default for Arg<'a> {
fn default() -> Self {
Self {
name: "",
clean: false,
force: false,
}
}
}
impl<'a> Arg<'a> {
fn run(&self, client: &mut Client) -> Result<(Vec<u8>, i32), HglibError> {
runcommand!(
client,
"branch",
&[self.name],
"-C",
self.clean,
"-f",
self.force
)
}
}
impl Client {
pub fn branch(&mut self, x: Arg) -> Result<String, HglibError> {
if !x.name.is_empty() && x.clean {
return Err(HglibError::from("Cannot use both name and clean"));
}
let (data, _) = x.run(self)?;
if !x.name.is_empty() {
Ok(x.name.to_string())
} else {
let pos = if let Some(pos) = data
.iter()
.rposition(|x| *x != b' ' && *x != b'\t' && *x != b'\n')
{
pos + 1
} else {
data.len()
};
let data = &data[..pos];
if !x.clean {
let o = String::from_utf8(data.to_vec())?;
Ok(o)
} else {
let len = "reset working directory to branch ".len();
let o = String::from_utf8(data[len..].to_vec())?;
Ok(o)
}
}
}
}