Skip to content

Commit 9551544

Browse files
AtkinsSJKernelDeimos
authored andcommitted
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.
1 parent 057b3ac commit 9551544

File tree

1 file changed

+19
-6
lines changed
  • packages/git/src/subcommands

1 file changed

+19
-6
lines changed

packages/git/src/subcommands/add.js

+19-6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export default {
3232
const { io, fs, env, args } = ctx;
3333
const { stdout, stderr } = io;
3434
const { options, positionals } = args;
35+
const cache = {};
3536

3637
const pathspecs = [...positionals];
3738
if (pathspecs.length === 0) {
@@ -41,13 +42,25 @@ export default {
4142

4243
const { dir, gitdir } = await find_repo_root(fs, env.PWD);
4344

44-
await git.add({
45-
fs,
46-
dir,
47-
gitdir,
45+
// NOTE: Canonical git lets you `git add FILE` with a FILE that's been deleted, to add that deletion to the index.
46+
// However, `git.add()` only handles files that currently exist. So, we have to implement this manually.
47+
const file_status = await git.statusMatrix({
48+
fs, dir, gitdir, cache,
4849
ignored: false,
49-
filepath: pathspecs,
50-
parallel: true,
50+
filepaths: pathspecs,
5151
});
52+
53+
const operations = file_status
54+
.filter(([ filepath, head, worktree, staged ]) => worktree !== staged)
55+
.map(([ filepath, head, worktree, index ]) => {
56+
// Remove deleted files
57+
if (worktree === 0)
58+
return git.remove({ fs, dir, gitdir, cache, filepath });
59+
60+
// All other files have changes to add
61+
return git.add({ fs, dir, gitdir, cache, filepath });
62+
});
63+
64+
await Promise.all(operations);
5265
}
5366
}

0 commit comments

Comments
 (0)