Skip to content

Commit

Permalink
feat: Added 'isIgnored' command (#1413)
Browse files Browse the repository at this point in the history
  • Loading branch information
billiegoose committed Aug 21, 2021
1 parent 3b9c17c commit f7ca4d0
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ docs/hashBlob.md
docs/indexPack.md
docs/init.md
docs/isDescendent.md
docs/isIgnored.md
docs/listBranches.md
docs/listFiles.md
docs/listNotes.md
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ unless there is a major version bump.
- [indexPack](https://isomorphic-git.github.io/docs/indexPack.html)
- [init](https://isomorphic-git.github.io/docs/init.html)
- [isDescendent](https://isomorphic-git.github.io/docs/isDescendent.html)
- [isIgnored](https://isomorphic-git.github.io/docs/isIgnored.html)
- [listBranches](https://isomorphic-git.github.io/docs/listBranches.html)
- [listFiles](https://isomorphic-git.github.io/docs/listFiles.html)
- [listNotes](https://isomorphic-git.github.io/docs/listNotes.html)
Expand Down
1 change: 1 addition & 0 deletions __tests__/test-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('exports', () => {
"indexPack",
"init",
"isDescendent",
"isIgnored",
"listBranches",
"listFiles",
"listNotes",
Expand Down
1 change: 1 addition & 0 deletions docs/alphabetic.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ sidebar_label: Alphabetical Index
- [indexPack](indexPack)
- [init](init)
- [isDescendent](isDescendent)
- [isIgnored](isIgnored)
- [listBranches](listBranches)
- [listFiles](listFiles)
- [listNotes](listNotes)
Expand Down
46 changes: 46 additions & 0 deletions src/api/isIgnored.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// @ts-check
import '../typedefs.js'

import { GitIgnoreManager } from '../managers/GitIgnoreManager.js'
import { FileSystem } from '../models/FileSystem.js'
import { assertParameter } from '../utils/assertParameter.js'
import { join } from '../utils/join.js'

/**
* Test whether a filepath should be ignored (because of .gitignore or .git/exclude)
*
* @param {object} args
* @param {FsClient} args.fs - a file system client
* @param {string} args.dir - The [working tree](dir-vs-gitdir.md) directory path
* @param {string} [args.gitdir=join(dir, '.git')] - [required] The [git directory](dir-vs-gitdir.md) path
* @param {string} args.filepath - The filepath to test
*
* @returns {Promise<boolean>} Resolves to true if the file should be ignored
*
* @example
* await git.isIgnored({ fs, dir: '/tutorial', filepath: 'docs/add.md' })
*
*/
export async function isIgnored({
fs,
dir,
gitdir = join(dir, '.git'),
filepath,
}) {
try {
assertParameter('fs', fs)
assertParameter('dir', dir)
assertParameter('gitdir', gitdir)
assertParameter('filepath', filepath)

return GitIgnoreManager.isIgnored({
fs: new FileSystem(fs),
dir,
gitdir,
filepath,
})
} catch (err) {
err.caller = 'git.isIgnored'
throw err
}
}
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { hashBlob } from './api/hashBlob.js'
import { indexPack } from './api/indexPack.js'
import { init } from './api/init.js'
import { isDescendent } from './api/isDescendent.js'
import { isIgnored } from './api/isIgnored.js'
import { listBranches } from './api/listBranches.js'
import { listFiles } from './api/listFiles.js'
import { listNotes } from './api/listNotes.js'
Expand Down Expand Up @@ -100,6 +101,7 @@ export {
indexPack,
init,
isDescendent,
isIgnored,
listBranches,
listFiles,
listNotes,
Expand Down Expand Up @@ -169,6 +171,7 @@ export default {
indexPack,
init,
isDescendent,
isIgnored,
listBranches,
listFiles,
listNotes,
Expand Down
3 changes: 2 additions & 1 deletion website/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"add",
"remove",
"listFiles",
"status"
"status",
"isIgnored"
],
"Notes": [
"addNote",
Expand Down
1 change: 1 addition & 0 deletions website/versioned_docs/version-1.x/alphabetic.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ original_id: alphabetic
- [indexPack](indexPack)
- [init](init)
- [isDescendent](isDescendent)
- [isIgnored](isIgnored)
- [listBranches](listBranches)
- [listFiles](listFiles)
- [listNotes](listNotes)
Expand Down
44 changes: 44 additions & 0 deletions website/versioned_docs/version-1.x/isIgnored.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
title: isIgnored
sidebar_label: isIgnored
id: version-1.x-isIgnored
original_id: isIgnored
---

Test whether a filepath should be ignored (because of .gitignore or .git/exclude)

| param | type [= default] | description |
| -------------- | -------------------------- | --------------------------------------------------- |
| [**fs**](./fs) | FsClient | a file system client |
| **dir** | string | The [working tree](dir-vs-gitdir.md) directory path |
| **gitdir** | string = join(dir, '.git') | The [git directory](dir-vs-gitdir.md) path |
| **filepath** | string | The filepath to test |
| return | Promise\<boolean\> | Resolves to true if the file should be ignored |

Example Code:

```js live
await git.isIgnored({ fs, dir: '/tutorial', filepath: 'docs/add.md' })
```


---

<details>
<summary><i>Tip: If you need a clean slate, expand and run this snippet to clean up the file system.</i></summary>

```js live
window.fs = new LightningFS('fs', { wipe: true })
window.pfs = window.fs.promises
console.log('done')
```
</details>

<script>
(function rewriteEditLink() {
const el = document.querySelector('a.edit-page-link.button');
if (el) {
el.href = 'https://github.com/isomorphic-git/isomorphic-git/edit/main/src/api/isIgnored.js';
}
})();
</script>
3 changes: 2 additions & 1 deletion website/versioned_sidebars/version-1.x-sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"version-1.x-add",
"version-1.x-remove",
"version-1.x-listFiles",
"version-1.x-status"
"version-1.x-status",
"version-1.x-isIgnored"
],
"Notes": [
"version-1.x-addNote",
Expand Down

0 comments on commit f7ca4d0

Please sign in to comment.