Skip to content

Commit f3f7a1f

Browse files
feat: add --force
1 parent 21213b7 commit f3f7a1f

3 files changed

Lines changed: 45 additions & 12 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "git-essentials",
3-
"version": "0.6.3",
3+
"version": "0.6.4",
44
"description": "A collection of essential Git commands for your browser and Node.js",
55
"main": "dist/esm/index.js",
66
"types": "index.d.ts",

src/api/add.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ export type AddParams = {
2525
/** The path to the file to add to the index. */
2626
filepath: string
2727

28+
/** Allow adding otherwise ignored files. */
29+
force?: boolean
30+
2831
/** A cache object. */
2932
cache?: Cache
3033
}
@@ -49,6 +52,7 @@ export async function add({
4952
dir,
5053
gitdir = join(dir, '.git'),
5154
filepath,
55+
force = false,
5256
cache = {}
5357
}: AddParams): Promise<void> {
5458
try {
@@ -59,7 +63,7 @@ export async function add({
5963

6064
const fs = new FileSystem(_fs)
6165
await GitIndexManager.acquire({ fs, gitdir, cache }, async function(index) {
62-
await addToIndex({ dir, gitdir, fs, filepath, index })
66+
await addToIndex({ dir, gitdir, fs, filepath, index, force })
6367
})
6468
} catch (err: any) {
6569
err.caller = 'git.add'
@@ -68,17 +72,19 @@ export async function add({
6872
}
6973

7074
async function addToIndex(
71-
{ dir, gitdir, fs, filepath, index }:
72-
{ dir: string, gitdir: string, fs: FileSystem, filepath: string, index: GitIndex }) {
75+
{ dir, gitdir, fs, filepath, index, force }:
76+
{ dir: string, gitdir: string, fs: FileSystem, filepath: string, index: GitIndex, force: boolean }) {
7377
// TODO: Should ignore UNLESS it's already in the index.
74-
const ignored = await GitIgnoreManager.isIgnored({
75-
fs,
76-
dir,
77-
gitdir,
78-
filepath,
79-
})
78+
if (!force) {
79+
const ignored = await GitIgnoreManager.isIgnored({
80+
fs,
81+
dir,
82+
gitdir,
83+
filepath,
84+
})
8085

81-
if (ignored) return
86+
if (ignored) return
87+
}
8288

8389
const stats = await fs.lstat(join(dir, filepath))
8490

@@ -87,7 +93,7 @@ async function addToIndex(
8793
if (stats.isDirectory()) {
8894
const children = await fs.readdir(join(dir, filepath))
8995
const promises = (children || []).map(child =>
90-
addToIndex({ dir, gitdir, fs, filepath: join(filepath, child), index })
96+
addToIndex({ dir, gitdir, fs, filepath: join(filepath, child), index, force })
9197
)
9298

9399
await Promise.all(promises)

tests/add.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ describe('add', () => {
4545
expect((await listFiles({ fs, dir })).length).toBe(0)
4646
})
4747

48+
it('ignored file but with force=true', async () => {
49+
// arrange
50+
const { fs, dir } = await makeFsFixture(addFsFixtureData as FsFixtureData)
51+
52+
// act
53+
await init({ fs, dir })
54+
await add({ fs, dir, filepath: 'i.txt', force: true })
55+
56+
// assert
57+
expect((await listFiles({ fs, dir })).length).toEqual(1)
58+
})
59+
4860
it('non-existant file', async () => {
4961
// arrange
5062
const { fs, dir } = await makeFsFixture(addFsFixtureData as FsFixtureData)
@@ -91,6 +103,21 @@ describe('add', () => {
91103
expect((await listFiles({ fs, dir })).length).toBe(3)
92104
})
93105

106+
it('folder with .gitignore and force', async () => {
107+
// arrange
108+
const { fs, dir } = await makeFsFixture(addFsFixtureData as FsFixtureData)
109+
110+
// act
111+
await init({ fs, dir })
112+
// assert
113+
expect((await listFiles({ fs, dir })).length).toEqual(0)
114+
115+
// act
116+
await add({ fs, dir, filepath: 'c', force: true })
117+
// assert
118+
expect((await listFiles({ fs, dir })).length).toEqual(4)
119+
})
120+
94121
it('git add .', async () => {
95122
// arrange
96123
const { fs, dir } = await makeFsFixture(addFsFixtureData as FsFixtureData)

0 commit comments

Comments
 (0)