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
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.

use crate::client::{Client, HglibError, Prompt, Runner};
use crate::{runcommand, runcommand_with_prompt, MkArg};

pub struct Arg<'a> {
    pub patches: &'a [&'a str],
    pub strip: Option<u32>,
    pub force: bool,
    pub nocommit: bool,
    pub bypass: bool,
    pub exact: bool,
    pub importbranch: bool,
    pub message: &'a str,
    pub date: &'a str,
    pub user: &'a str,
    pub similarity: Option<u8>,
}

impl<'a> Default for Arg<'a> {
    fn default() -> Self {
        Self {
            patches: &[],
            strip: None,
            force: false,
            nocommit: false,
            bypass: false,
            exact: false,
            importbranch: false,
            message: "",
            date: "",
            user: "",
            similarity: None,
        }
    }
}

struct ImportPrompt<'a> {
    patch: &'a [u8],
    pos: usize,
}

impl<'a> ImportPrompt<'a> {
    fn new(patch: &'a str) -> Self {
        Self {
            patch: patch.as_bytes(),
            pos: 0,
        }
    }
}

impl<'a> Prompt for ImportPrompt<'a> {
    fn call(&mut self, size: usize) -> &[u8] {
        if self.pos < self.patch.len() {
            let end = self.patch.len().min(self.pos + size);
            let buf = &self.patch[self.pos..end];
            self.pos = end;

            buf
        } else {
            &[]
        }
    }
}

impl<'a> Arg<'a> {
    fn run(&self, client: &mut Client) -> Result<(Vec<u8>, i32), HglibError> {
        if self.patches.len() == 1 && self.patches[0].as_bytes().iter().any(|c| *c == b'\n') {
            let prompt = ImportPrompt::new(self.patches[0]);
            runcommand_with_prompt!(
                client,
                "import",
                prompt,
                &["-"],
                "--strip",
                self.strip,
                "--force",
                self.force,
                "--no-commit",
                self.nocommit,
                "--bypass",
                self.bypass,
                "--exact",
                self.exact,
                "--import-branch",
                self.importbranch,
                "--message",
                self.message,
                "--date",
                self.date,
                "--user",
                self.user,
                "--similarity",
                self.similarity
            )
        } else {
            runcommand!(
                client,
                "import",
                self.patches,
                "--strip",
                self.strip,
                "--force",
                self.force,
                "--no-commit",
                self.nocommit,
                "--bypass",
                self.bypass,
                "--exact",
                self.exact,
                "--import-branch",
                self.importbranch,
                "--message",
                self.message,
                "--date",
                self.date,
                "--user",
                self.user,
                "--similarity",
                self.similarity
            )
        }
    }
}

impl Client {
    pub fn import(&mut self, x: Arg) -> Result<Vec<u8>, HglibError> {
        let (data, _) = x.run(self)?;
        Ok(data)
    }
}