Open an editor to get the commit message.
(&self)
| 150 | |
| 151 | /// Open an editor to get the commit message. |
| 152 | pub(super) fn get_message_from_editor(&self) -> CliResult<String> { |
| 153 | // Create a temporary file for the message |
| 154 | let temp_dir = std::env::temp_dir(); |
| 155 | let temp_file = temp_dir.join("ATOMIC_EDITMSG"); |
| 156 | |
| 157 | // Write template to file |
| 158 | let template = r#" |
| 159 | # Enter your commit message above. |
| 160 | # Lines starting with '#' will be ignored. |
| 161 | # An empty message aborts the commit. |
| 162 | "#; |
| 163 | |
| 164 | std::fs::write(&temp_file, template).map_err(|e| { |
| 165 | CliError::Internal(anyhow::anyhow!("Failed to create temp file: {}", e)) |
| 166 | })?; |
| 167 | |
| 168 | // Get editor from environment |
| 169 | let editor = std::env::var("EDITOR") |
| 170 | .or_else(|_| std::env::var("VISUAL")) |
| 171 | .unwrap_or_else(|_| { |
| 172 | if cfg!(windows) { |
| 173 | "notepad".to_string() |
| 174 | } else { |
| 175 | "vi".to_string() |
| 176 | } |
| 177 | }); |
| 178 | |
| 179 | // Open editor |
| 180 | let status = std::process::Command::new(&editor) |
| 181 | .arg(&temp_file) |
| 182 | .status() |
| 183 | .map_err(|e| { |
| 184 | CliError::Internal(anyhow::anyhow!("Failed to open editor '{}': {}", editor, e)) |
| 185 | })?; |
| 186 | |
| 187 | if !status.success() { |
| 188 | return Err(CliError::Internal(anyhow::anyhow!( |
| 189 | "Editor exited with non-zero status: {:?}", |
| 190 | status.code() |
| 191 | ))); |
| 192 | } |
| 193 | |
| 194 | // Read the file back |
| 195 | let content = std::fs::read_to_string(&temp_file) |
| 196 | .map_err(|e| CliError::Internal(anyhow::anyhow!("Failed to read temp file: {}", e)))?; |
| 197 | |
| 198 | // Clean up |
| 199 | let _ = std::fs::remove_file(&temp_file); |
| 200 | |
| 201 | // Process content - remove comment lines and trim |
| 202 | let message: String = content |
| 203 | .lines() |
| 204 | .filter(|line| !line.starts_with('#')) |
| 205 | .collect::<Vec<_>>() |
| 206 | .join("\n") |
| 207 | .trim() |
| 208 | .to_string(); |
| 209 |