feat(git): Make git add work for deleted files

Previously, this:

```sh
> rm deleted.txt
> git add deleted.txt
```

...would unhelpfully complain that deleted.txt cannot be found. Now, it
records the deleted file to the index, just as canonical git does.
This commit is contained in:
Sam Atkins 2024-06-20 15:46:43 +01:00 committed by Eric Dubé
parent 057b3acf00
commit 955154468f

View File

@ -32,6 +32,7 @@ export default {
const { io, fs, env, args } = ctx; const { io, fs, env, args } = ctx;
const { stdout, stderr } = io; const { stdout, stderr } = io;
const { options, positionals } = args; const { options, positionals } = args;
const cache = {};
const pathspecs = [...positionals]; const pathspecs = [...positionals];
if (pathspecs.length === 0) { if (pathspecs.length === 0) {
@ -41,13 +42,25 @@ export default {
const { dir, gitdir } = await find_repo_root(fs, env.PWD); const { dir, gitdir } = await find_repo_root(fs, env.PWD);
await git.add({ // NOTE: Canonical git lets you `git add FILE` with a FILE that's been deleted, to add that deletion to the index.
fs, // However, `git.add()` only handles files that currently exist. So, we have to implement this manually.
dir, const file_status = await git.statusMatrix({
gitdir, fs, dir, gitdir, cache,
ignored: false, ignored: false,
filepath: pathspecs, filepaths: pathspecs,
parallel: true,
}); });
const operations = file_status
.filter(([ filepath, head, worktree, staged ]) => worktree !== staged)
.map(([ filepath, head, worktree, index ]) => {
// Remove deleted files
if (worktree === 0)
return git.remove({ fs, dir, gitdir, cache, filepath });
// All other files have changes to add
return git.add({ fs, dir, gitdir, cache, filepath });
});
await Promise.all(operations);
} }
} }