@@ -3,6 +3,7 @@ |
| 3 | 3 | #![allow(dead_code)] |
| 4 | 4 | |
| 5 | 5 | use std::path::{Path, PathBuf}; |
| 6 | +use std::process::Command; |
| 6 | 7 | use super::tree::FileTree; |
| 7 | 8 | |
| 8 | 9 | /// Fuss mode state |
@@ -47,7 +48,9 @@ impl FussMode { |
| 47 | 48 | /// Initialize with a root path |
| 48 | 49 | pub fn init(&mut self, root_path: &Path) { |
| 49 | 50 | self.root_path = Some(root_path.to_path_buf()); |
| 50 | | - self.tree = Some(FileTree::new(root_path)); |
| 51 | + let mut tree = FileTree::new(root_path); |
| 52 | + tree.update_git_status(); |
| 53 | + self.tree = Some(tree); |
| 51 | 54 | self.selected = 0; |
| 52 | 55 | self.scroll = 0; |
| 53 | 56 | } |
@@ -160,6 +163,275 @@ impl FussMode { |
| 160 | 163 | pub fn reload(&mut self) { |
| 161 | 164 | if let Some(ref mut tree) = self.tree { |
| 162 | 165 | tree.reload(); |
| 166 | + tree.update_git_status(); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /// Refresh git status without reloading file tree |
| 171 | + pub fn refresh_git_status(&mut self) { |
| 172 | + if let Some(ref mut tree) = self.tree { |
| 173 | + tree.update_git_status(); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + /// Stage the currently selected file |
| 178 | + /// Returns true on success, false on failure |
| 179 | + pub fn stage_selected(&mut self) -> bool { |
| 180 | + let root = match &self.root_path { |
| 181 | + Some(p) => p.clone(), |
| 182 | + None => return false, |
| 183 | + }; |
| 184 | + |
| 185 | + let path = match self.selected_path() { |
| 186 | + Some(p) => p, |
| 187 | + None => return false, |
| 188 | + }; |
| 189 | + |
| 190 | + // Don't stage directories |
| 191 | + if self.is_dir_selected() { |
| 192 | + return false; |
| 193 | + } |
| 194 | + |
| 195 | + let output = Command::new("git") |
| 196 | + .arg("-C") |
| 197 | + .arg(&root) |
| 198 | + .arg("add") |
| 199 | + .arg(&path) |
| 200 | + .output(); |
| 201 | + |
| 202 | + if let Ok(output) = output { |
| 203 | + if output.status.success() { |
| 204 | + self.refresh_git_status(); |
| 205 | + return true; |
| 206 | + } |
| 207 | + } |
| 208 | + false |
| 209 | + } |
| 210 | + |
| 211 | + /// Unstage the currently selected file |
| 212 | + /// Returns true on success, false on failure |
| 213 | + pub fn unstage_selected(&mut self) -> bool { |
| 214 | + let root = match &self.root_path { |
| 215 | + Some(p) => p.clone(), |
| 216 | + None => return false, |
| 217 | + }; |
| 218 | + |
| 219 | + let path = match self.selected_path() { |
| 220 | + Some(p) => p, |
| 221 | + None => return false, |
| 222 | + }; |
| 223 | + |
| 224 | + // Don't unstage directories |
| 225 | + if self.is_dir_selected() { |
| 226 | + return false; |
| 227 | + } |
| 228 | + |
| 229 | + let output = Command::new("git") |
| 230 | + .arg("-C") |
| 231 | + .arg(&root) |
| 232 | + .arg("restore") |
| 233 | + .arg("--staged") |
| 234 | + .arg(&path) |
| 235 | + .output(); |
| 236 | + |
| 237 | + if let Ok(output) = output { |
| 238 | + if output.status.success() { |
| 239 | + self.refresh_git_status(); |
| 240 | + return true; |
| 241 | + } |
| 242 | + } |
| 243 | + false |
| 244 | + } |
| 245 | + |
| 246 | + /// Get the root path |
| 247 | + pub fn root_path(&self) -> Option<&Path> { |
| 248 | + self.root_path.as_deref() |
| 249 | + } |
| 250 | + |
| 251 | + /// Push to remote |
| 252 | + /// Returns (success, message) |
| 253 | + pub fn git_push(&mut self) -> (bool, String) { |
| 254 | + let root = match &self.root_path { |
| 255 | + Some(p) => p.clone(), |
| 256 | + None => return (false, "No workspace".to_string()), |
| 257 | + }; |
| 258 | + |
| 259 | + let output = Command::new("git") |
| 260 | + .arg("-C") |
| 261 | + .arg(&root) |
| 262 | + .arg("push") |
| 263 | + .output(); |
| 264 | + |
| 265 | + match output { |
| 266 | + Ok(out) if out.status.success() => { |
| 267 | + self.refresh_git_status(); |
| 268 | + (true, "Pushed".to_string()) |
| 269 | + } |
| 270 | + Ok(out) => { |
| 271 | + let stderr = String::from_utf8_lossy(&out.stderr); |
| 272 | + (false, format!("Push failed: {}", stderr.lines().next().unwrap_or("unknown error"))) |
| 273 | + } |
| 274 | + Err(e) => (false, format!("Failed to run git: {}", e)), |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + /// Pull from remote |
| 279 | + /// Returns (success, message) |
| 280 | + pub fn git_pull(&mut self) -> (bool, String) { |
| 281 | + let root = match &self.root_path { |
| 282 | + Some(p) => p.clone(), |
| 283 | + None => return (false, "No workspace".to_string()), |
| 284 | + }; |
| 285 | + |
| 286 | + let output = Command::new("git") |
| 287 | + .arg("-C") |
| 288 | + .arg(&root) |
| 289 | + .arg("pull") |
| 290 | + .output(); |
| 291 | + |
| 292 | + match output { |
| 293 | + Ok(out) if out.status.success() => { |
| 294 | + self.refresh_git_status(); |
| 295 | + (true, "Pulled".to_string()) |
| 296 | + } |
| 297 | + Ok(out) => { |
| 298 | + let stderr = String::from_utf8_lossy(&out.stderr); |
| 299 | + (false, format!("Pull failed: {}", stderr.lines().next().unwrap_or("unknown error"))) |
| 300 | + } |
| 301 | + Err(e) => (false, format!("Failed to run git: {}", e)), |
| 302 | + } |
| 303 | + } |
| 304 | + |
| 305 | + /// Create a git tag |
| 306 | + /// Returns (success, message) |
| 307 | + pub fn git_tag(&mut self, tag_name: &str) -> (bool, String) { |
| 308 | + let root = match &self.root_path { |
| 309 | + Some(p) => p.clone(), |
| 310 | + None => return (false, "No workspace".to_string()), |
| 311 | + }; |
| 312 | + |
| 313 | + if tag_name.trim().is_empty() { |
| 314 | + return (false, "Empty tag name".to_string()); |
| 315 | + } |
| 316 | + |
| 317 | + let output = Command::new("git") |
| 318 | + .arg("-C") |
| 319 | + .arg(&root) |
| 320 | + .arg("tag") |
| 321 | + .arg(tag_name.trim()) |
| 322 | + .output(); |
| 323 | + |
| 324 | + match output { |
| 325 | + Ok(out) if out.status.success() => { |
| 326 | + (true, format!("Created tag: {}", tag_name.trim())) |
| 327 | + } |
| 328 | + Ok(out) => { |
| 329 | + let stderr = String::from_utf8_lossy(&out.stderr); |
| 330 | + (false, format!("Tag failed: {}", stderr.lines().next().unwrap_or("unknown error"))) |
| 331 | + } |
| 332 | + Err(e) => (false, format!("Failed to run git: {}", e)), |
| 333 | + } |
| 334 | + } |
| 335 | + |
| 336 | + /// Fetch from remote |
| 337 | + /// Returns (success, message) |
| 338 | + pub fn git_fetch(&mut self) -> (bool, String) { |
| 339 | + let root = match &self.root_path { |
| 340 | + Some(p) => p.clone(), |
| 341 | + None => return (false, "No workspace".to_string()), |
| 342 | + }; |
| 343 | + |
| 344 | + let output = Command::new("git") |
| 345 | + .arg("-C") |
| 346 | + .arg(&root) |
| 347 | + .arg("fetch") |
| 348 | + .output(); |
| 349 | + |
| 350 | + match output { |
| 351 | + Ok(out) if out.status.success() => { |
| 352 | + self.refresh_git_status(); |
| 353 | + (true, "Fetched".to_string()) |
| 354 | + } |
| 355 | + Ok(out) => { |
| 356 | + let stderr = String::from_utf8_lossy(&out.stderr); |
| 357 | + (false, format!("Fetch failed: {}", stderr.lines().next().unwrap_or("unknown error"))) |
| 358 | + } |
| 359 | + Err(e) => (false, format!("Failed to run git: {}", e)), |
| 360 | + } |
| 361 | + } |
| 362 | + |
| 363 | + /// Commit staged changes with the given message |
| 364 | + /// Returns (success, message) |
| 365 | + pub fn git_commit(&mut self, message: &str) -> (bool, String) { |
| 366 | + let root = match &self.root_path { |
| 367 | + Some(p) => p.clone(), |
| 368 | + None => return (false, "No workspace".to_string()), |
| 369 | + }; |
| 370 | + |
| 371 | + if message.trim().is_empty() { |
| 372 | + return (false, "Empty commit message".to_string()); |
| 373 | + } |
| 374 | + |
| 375 | + let output = Command::new("git") |
| 376 | + .arg("-C") |
| 377 | + .arg(&root) |
| 378 | + .arg("commit") |
| 379 | + .arg("-m") |
| 380 | + .arg(message) |
| 381 | + .output(); |
| 382 | + |
| 383 | + match output { |
| 384 | + Ok(out) if out.status.success() => { |
| 385 | + self.refresh_git_status(); |
| 386 | + (true, "Committed".to_string()) |
| 387 | + } |
| 388 | + Ok(out) => { |
| 389 | + let stderr = String::from_utf8_lossy(&out.stderr); |
| 390 | + if stderr.contains("nothing to commit") { |
| 391 | + (false, "Nothing to commit".to_string()) |
| 392 | + } else { |
| 393 | + (false, format!("Commit failed: {}", stderr.lines().next().unwrap_or("unknown error"))) |
| 394 | + } |
| 395 | + } |
| 396 | + Err(e) => (false, format!("Failed to run git: {}", e)), |
| 397 | + } |
| 398 | + } |
| 399 | + |
| 400 | + /// Get git diff for the currently selected file |
| 401 | + /// Returns (filename, diff_content) or None if no diff |
| 402 | + pub fn get_diff_for_selected(&self) -> Option<(String, String)> { |
| 403 | + let root = self.root_path.as_ref()?; |
| 404 | + let path = self.selected_path()?; |
| 405 | + |
| 406 | + // Don't diff directories |
| 407 | + if self.is_dir_selected() { |
| 408 | + return None; |
| 409 | + } |
| 410 | + |
| 411 | + // Get relative path for display |
| 412 | + let rel_path = path.strip_prefix(root).unwrap_or(&path); |
| 413 | + let filename = rel_path.to_string_lossy().to_string(); |
| 414 | + |
| 415 | + // Run git diff |
| 416 | + let output = Command::new("git") |
| 417 | + .arg("-C") |
| 418 | + .arg(root) |
| 419 | + .arg("diff") |
| 420 | + .arg("HEAD") |
| 421 | + .arg("--") |
| 422 | + .arg(&path) |
| 423 | + .output() |
| 424 | + .ok()?; |
| 425 | + |
| 426 | + if output.status.success() { |
| 427 | + let diff = String::from_utf8_lossy(&output.stdout).to_string(); |
| 428 | + if diff.is_empty() { |
| 429 | + Some((filename, "(no changes)".to_string())) |
| 430 | + } else { |
| 431 | + Some((filename, diff)) |
| 432 | + } |
| 433 | + } else { |
| 434 | + None |
| 163 | 435 | } |
| 164 | 436 | } |
| 165 | 437 | } |