We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 17527e5 commit ebede48Copy full SHA for ebede48
README.md
@@ -124,7 +124,8 @@ or a file named `.git`.
124
Given a path, walk up the file system tree until a git repo working
125
directory is found. Since this calls `stat` a bunch of times, it's
126
probably best to only call it if you're reasonably sure you're likely to be
127
-in a git project somewhere.
+in a git project somewhere. Pass in `opts.root` to stop checking at that
128
+directory.
129
130
Resolves to `null` if not in a git project.
131
lib/find.js
@@ -1,15 +1,15 @@
1
const is = require('./is.js')
2
-const { dirname } = require('path')
+const { dirname, sep } = require('path')
3
4
-module.exports = async ({ cwd = process.cwd() } = {}) => {
5
- if (await is({ cwd })) {
6
- return cwd
7
- }
8
- while (cwd !== dirname(cwd)) {
9
- cwd = dirname(cwd)
+module.exports = async ({ cwd = process.cwd(), root = sep } = {}) => {
+ while (true) {
10
if (await is({ cwd })) {
11
return cwd
12
}
+ const next = dirname(cwd)
+ if (cwd === root || cwd === next) {
+ return null
+ }
13
+ cwd = next
14
- return null
15
test/find.js
@@ -1,4 +1,5 @@
const t = require('tap')
+const { join } = require('path')
const find = require('../lib/find.js')
t.test('find the git dir many folders up', t => {
@@ -10,6 +11,24 @@ t.test('find the git dir many folders up', t => {
return t.resolveMatch(find({ cwd: path }), root)
})
+t.test('stop before root dir', t => {
+ const root = t.testdir({
16
+ '.git': { index: 'hello' },
17
+ a: { b: { c: { d: { e: {} } } } },
18
+ })
19
+ const path = `${root}/a/b/c/d/e`
20
+ return t.resolveMatch(find({ cwd: path, root: join(root, 'a') }), null)
21
+})
22
+
23
+t.test('stop at root dir', t => {
24
25
26
27
28
29
+ return t.resolveMatch(find({ cwd: path, root }), root)
30
31
32
t.test('find the git dir at current level', t => {
33
const cwd = t.testdir({
34
'.git': { index: 'hello' },
0 commit comments