Skip to content

Commit b2dd2cc

Browse files
committed
fix(git): properly stage deleted files in commit handler
Previously, committing deleted files failed with "No such file or directory" because add_path() tries to read the file from disk. Now we check if the file exists and use remove_path() for deletions, add_path() for new/modified files.
1 parent 6c6cdc8 commit b2dd2cc

2 files changed

Lines changed: 12 additions & 177 deletions

File tree

anycode-backend/src/handlers/git_handler.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,23 @@ fn git_commit_impl(request: &GitCommitRequest) -> Result<Value> {
129129
let repo = Repository::discover(&workdir)?;
130130
let mut index = repo.index()?;
131131

132-
// Add files to index
133-
for path in &request.files {
134-
let path = Path::new(path);
132+
// Add/remove files to index
133+
let repo_root = repo.workdir().unwrap_or(Path::new("."));
134+
for file_path in &request.files {
135+
let path = Path::new(file_path);
135136
let relative_path = if path.is_absolute() {
136-
path.strip_prefix(repo.workdir().unwrap_or(Path::new(".")))
137-
.unwrap_or(path)
137+
path.strip_prefix(repo_root).unwrap_or(path)
138138
} else {
139139
path
140140
};
141141

142-
index.add_path(relative_path)?;
142+
// Check if file exists on disk - if not, it's a deletion
143+
let full_path = repo_root.join(relative_path);
144+
if full_path.exists() {
145+
index.add_path(relative_path)?;
146+
} else {
147+
index.remove_path(relative_path)?;
148+
}
143149
}
144150

145151
index.write()?;

git_integration.md

Lines changed: 0 additions & 171 deletions
This file was deleted.

0 commit comments

Comments
 (0)