Skip to content

Commit 1f5c340

Browse files
committed
feat: add staging and overlap detection operations
Introduces functions to stage files, unstage all changes, and detect unstaged overlaps with staged files. Enables better git workflow control and prevents accidental commits with conflicting changes.
1 parent 101ae86 commit 1f5c340

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

src/services/git.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,44 @@ impl GitService {
255255
)
256256
}
257257

258+
// ─── Staging Operations ───
259+
260+
/// Check if any staged file also has unstaged modifications.
261+
/// Returns the list of overlapping file paths.
262+
pub async fn has_unstaged_overlap(&self) -> Result<Vec<PathBuf>> {
263+
let (staged_output, unstaged_output) = tokio::try_join!(
264+
self.run_git(&["diff", "--cached", "--name-only"]),
265+
self.run_git(&["diff", "--name-only"]),
266+
)?;
267+
268+
let staged: std::collections::HashSet<&str> =
269+
staged_output.lines().filter(|l| !l.is_empty()).collect();
270+
let unstaged: std::collections::HashSet<&str> =
271+
unstaged_output.lines().filter(|l| !l.is_empty()).collect();
272+
273+
Ok(staged.intersection(&unstaged).map(PathBuf::from).collect())
274+
}
275+
276+
/// Unstage all currently staged files (soft reset).
277+
pub async fn unstage_all(&self) -> Result<()> {
278+
self.run_git(&["reset", "HEAD"]).await?;
279+
Ok(())
280+
}
281+
282+
/// Stage specific files by path.
283+
pub async fn stage_files(&self, paths: &[PathBuf]) -> Result<()> {
284+
if paths.is_empty() {
285+
return Ok(());
286+
}
287+
288+
let path_strs: Vec<String> = paths.iter().map(|p| p.display().to_string()).collect();
289+
let mut args: Vec<&str> = vec!["add", "--"];
290+
args.extend(path_strs.iter().map(|s| s.as_str()));
291+
292+
self.run_git(&args).await?;
293+
Ok(())
294+
}
295+
258296
// ─── Commit ───
259297

260298
pub async fn commit(&self, message: &str) -> Result<()> {

0 commit comments

Comments
 (0)