|
| 1 | +use std::path::Path; |
| 2 | +use std::process::Command; |
| 3 | + |
| 4 | +use crate::{CoreError, CoreResult, jj_binary}; |
| 5 | + |
| 6 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 7 | +pub struct JjCommand { |
| 8 | + raw: String, |
| 9 | +} |
| 10 | + |
| 11 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 12 | +pub struct JjCommandResult { |
| 13 | + pub output: String, |
| 14 | + pub exit_code: i32, |
| 15 | +} |
| 16 | + |
| 17 | +impl JjCommand { |
| 18 | + pub fn new(command: impl Into<String>) -> Self { |
| 19 | + Self { |
| 20 | + raw: command.into(), |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + pub fn from_palette_query(query: &str) -> Option<Self> { |
| 25 | + let body_after = |rest: &str| Self::new(rest.trim_start()); |
| 26 | + if query == "jj" || query == "!" { |
| 27 | + return Some(Self::new(String::new())); |
| 28 | + } |
| 29 | + if let Some(rest) = query.strip_prefix("jj ") { |
| 30 | + return Some(body_after(rest)); |
| 31 | + } |
| 32 | + query.strip_prefix('!').map(body_after) |
| 33 | + } |
| 34 | + |
| 35 | + pub fn raw(&self) -> &str { |
| 36 | + &self.raw |
| 37 | + } |
| 38 | + |
| 39 | + pub fn into_raw(self) -> String { |
| 40 | + self.raw |
| 41 | + } |
| 42 | + |
| 43 | + pub fn parse_args(&self) -> Option<Vec<String>> { |
| 44 | + parse_args(&self.raw) |
| 45 | + } |
| 46 | + |
| 47 | + pub fn run_in_path(&self, path: &Path) -> CoreResult<JjCommandResult> { |
| 48 | + let args = self.parse_args().ok_or_else(|| CoreError::Internal { |
| 49 | + message: "Unclosed quote in jj command.".to_owned(), |
| 50 | + })?; |
| 51 | + if args.is_empty() { |
| 52 | + return Err(CoreError::Internal { |
| 53 | + message: "No jj command to run.".to_owned(), |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + let output = Command::new(jj_binary()) |
| 58 | + .args(&args) |
| 59 | + .current_dir(path) |
| 60 | + .output() |
| 61 | + .map_err(|e| CoreError::Internal { |
| 62 | + message: format!("run jj: {e}"), |
| 63 | + })?; |
| 64 | + |
| 65 | + let stdout = trim_output(&output.stdout); |
| 66 | + let stderr = trim_output(&output.stderr); |
| 67 | + let combined = match (stdout.is_empty(), stderr.is_empty()) { |
| 68 | + (true, true) => "(no output)".to_owned(), |
| 69 | + (false, true) => stdout.clone(), |
| 70 | + (true, false) => stderr.clone(), |
| 71 | + (false, false) => format!("{stdout}\n{stderr}"), |
| 72 | + }; |
| 73 | + |
| 74 | + Ok(JjCommandResult { |
| 75 | + output: combined, |
| 76 | + exit_code: output.status.code().unwrap_or(-1), |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +fn parse_args(command: &str) -> Option<Vec<String>> { |
| 82 | + let mut args = Vec::new(); |
| 83 | + let mut current = String::new(); |
| 84 | + let mut quote = None; |
| 85 | + let mut escaping = false; |
| 86 | + let mut arg_started = false; |
| 87 | + |
| 88 | + let mut chars = command.chars().peekable(); |
| 89 | + while let Some(ch) = chars.next() { |
| 90 | + if escaping { |
| 91 | + current.push(ch); |
| 92 | + escaping = false; |
| 93 | + arg_started = true; |
| 94 | + continue; |
| 95 | + } |
| 96 | + if let Some(current_quote) = quote { |
| 97 | + if ch == current_quote { |
| 98 | + quote = None; |
| 99 | + } else if current_quote == '"' && ch == '\\' { |
| 100 | + match chars.peek().copied() { |
| 101 | + Some('"') | Some('\\') => current.push(chars.next().expect("peeked next char")), |
| 102 | + _ => current.push(ch), |
| 103 | + } |
| 104 | + } else { |
| 105 | + current.push(ch); |
| 106 | + } |
| 107 | + arg_started = true; |
| 108 | + continue; |
| 109 | + } |
| 110 | + if ch == '\\' { |
| 111 | + escaping = true; |
| 112 | + arg_started = true; |
| 113 | + continue; |
| 114 | + } |
| 115 | + if ch == '"' || ch == '\'' { |
| 116 | + quote = Some(ch); |
| 117 | + arg_started = true; |
| 118 | + continue; |
| 119 | + } |
| 120 | + if ch.is_whitespace() { |
| 121 | + if arg_started { |
| 122 | + args.push(std::mem::take(&mut current)); |
| 123 | + arg_started = false; |
| 124 | + } |
| 125 | + continue; |
| 126 | + } |
| 127 | + current.push(ch); |
| 128 | + arg_started = true; |
| 129 | + } |
| 130 | + |
| 131 | + if escaping { |
| 132 | + current.push('\\'); |
| 133 | + } |
| 134 | + quote.is_none().then(|| { |
| 135 | + if arg_started { |
| 136 | + args.push(current); |
| 137 | + } |
| 138 | + args |
| 139 | + }) |
| 140 | +} |
| 141 | + |
| 142 | +fn trim_output(bytes: &[u8]) -> String { |
| 143 | + String::from_utf8_lossy(bytes).trim().to_owned() |
| 144 | +} |
| 145 | + |
| 146 | +#[cfg(test)] |
| 147 | +mod tests { |
| 148 | + use super::*; |
| 149 | + |
| 150 | + #[test] |
| 151 | + fn parses_shell_like_args() { |
| 152 | + assert_eq!( |
| 153 | + JjCommand::new(r#"log -r "description(exact:'fix bug')" --limit 5"#).parse_args(), |
| 154 | + Some(vec![ |
| 155 | + "log".to_owned(), |
| 156 | + "-r".to_owned(), |
| 157 | + "description(exact:'fix bug')".to_owned(), |
| 158 | + "--limit".to_owned(), |
| 159 | + "5".to_owned() |
| 160 | + ]) |
| 161 | + ); |
| 162 | + assert_eq!( |
| 163 | + JjCommand::new(r#"file\ with\ spaces"#).parse_args(), |
| 164 | + Some(vec!["file with spaces".to_owned()]) |
| 165 | + ); |
| 166 | + assert_eq!( |
| 167 | + JjCommand::new(r#"describe -m "a\"b""#).parse_args(), |
| 168 | + Some(vec![ |
| 169 | + "describe".to_owned(), |
| 170 | + "-m".to_owned(), |
| 171 | + "a\"b".to_owned() |
| 172 | + ]) |
| 173 | + ); |
| 174 | + assert_eq!( |
| 175 | + JjCommand::new(r#"describe -m """#).parse_args(), |
| 176 | + Some(vec!["describe".to_owned(), "-m".to_owned(), String::new()]) |
| 177 | + ); |
| 178 | + assert_eq!( |
| 179 | + JjCommand::new(r#"new '' file\ with\ spaces"#).parse_args(), |
| 180 | + Some(vec![ |
| 181 | + "new".to_owned(), |
| 182 | + String::new(), |
| 183 | + "file with spaces".to_owned() |
| 184 | + ]) |
| 185 | + ); |
| 186 | + assert_eq!( |
| 187 | + JjCommand::new(r#"describe -m 'a\b'"#).parse_args(), |
| 188 | + Some(vec![ |
| 189 | + "describe".to_owned(), |
| 190 | + "-m".to_owned(), |
| 191 | + r#"a\b"#.to_owned() |
| 192 | + ]) |
| 193 | + ); |
| 194 | + assert_eq!( |
| 195 | + JjCommand::new(r#"log -r "description(regex:'\bfix\b')""#).parse_args(), |
| 196 | + Some(vec![ |
| 197 | + "log".to_owned(), |
| 198 | + "-r".to_owned(), |
| 199 | + r#"description(regex:'\bfix\b')"#.to_owned() |
| 200 | + ]) |
| 201 | + ); |
| 202 | + assert_eq!(JjCommand::new(r#"log -r "mine()"#).parse_args(), None); |
| 203 | + } |
| 204 | + |
| 205 | + #[test] |
| 206 | + fn extracts_prefixed_command_body() { |
| 207 | + assert_eq!( |
| 208 | + JjCommand::from_palette_query("jj log").map(JjCommand::into_raw), |
| 209 | + Some("log".to_owned()) |
| 210 | + ); |
| 211 | + assert_eq!( |
| 212 | + JjCommand::from_palette_query("!status").map(JjCommand::into_raw), |
| 213 | + Some("status".to_owned()) |
| 214 | + ); |
| 215 | + assert_eq!(JjCommand::from_palette_query("status"), None); |
| 216 | + } |
| 217 | +} |
0 commit comments