@simbo/find-git-repository-root 3.0.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,40 @@
1
1
  # @simbo/find-git-repository-root
2
2
 
3
+ ## 4.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - bf3749b: Find the root of a Git working tree with
8
+ `git rev-parse --show-toplevel` instead of searching upward for a `.git`
9
+ directory.
10
+
11
+ - Inside a linked worktree or a submodule, the root of the worktree or
12
+ submodule is returned instead of the root of an enclosing repository.
13
+ - The returned path is canonical: Git resolves symbolic links, so on macOS a
14
+ temporary directory is returned as `/private/var/…`.
15
+ - Bare repositories, the `.git` directory, directories outside a repository,
16
+ missing paths and files resolve to `undefined`, even if a parent directory
17
+ is a repository.
18
+ - The function rejects with a `GitError` from `@simbo/git-exec` if Git cannot
19
+ be started or is terminated.
20
+ - Replace the dependencies on `find-up` and `@simbo/is-git-repository-root`
21
+ with `@simbo/git-exec` and `@simbo/accessible`.
22
+
23
+ Version 3.0.1 already returned the roots of worktrees and submodules and
24
+ rejected without Git through `@simbo/is-git-repository-root` 4.0.0; this
25
+ release makes that the contract of this package.
26
+
27
+ Git must be installed and on the `PATH`. Compare returned paths with
28
+ `realpath()` results instead of the given path, handle `undefined` for missing
29
+ directories, and catch the `GitError` where Git may be missing.
30
+
31
+ ## 3.0.1
32
+
33
+ ### Patch Changes
34
+
35
+ - Updated dependencies [9410189]
36
+ - @simbo/is-git-repository-root@4.0.0
37
+
3
38
  ## 3.0.0
4
39
 
5
40
  ### Major Changes
package/README.md CHANGED
@@ -3,22 +3,19 @@
3
3
  📦
4
4
  [**`@simbo/find-git-repository-root`**](https://npmjs.com/package/@simbo/find-git-repository-root)
5
5
 
6
- A lightweight utility for locating the **root directory of a Git repository**.
6
+ A lightweight utility for locating the **root directory of a Git working tree**,
7
+ including linked worktrees and submodules.
7
8
 
8
- It searches upward from a given directory until it finds a valid repository root
9
- (a directory containing a `.git` folder).
10
-
11
- This package builds on [find-up](https://npmjs.com/package/find-up) and
12
- [`@simbo/is-git-repository-root`](https://npmjs.com/package/@simbo/is-git-repository-root).
9
+ It asks Git for the top-level directory with `git rev-parse --show-toplevel`.
13
10
 
14
11
  ## Features
15
12
 
16
- - Detects the **root directory** of a Git repository
13
+ - Detects the **root directory** of a Git working tree, including linked
14
+ worktrees and submodules
17
15
 
18
- - Returns the absolute path to the Git repository root
16
+ - Returns the canonical absolute path, with symbolic links resolved
19
17
 
20
- - Uses upward search from a given working directory (defaults to
21
- `process.cwd()`)
18
+ - Starts from a given working directory (defaults to `process.cwd()`)
22
19
 
23
20
  - Async and Promise-based API
24
21
 
@@ -26,7 +23,7 @@ This package builds on [find-up](https://npmjs.com/package/find-up) and
26
23
 
27
24
  ## Requirements
28
25
 
29
- Requires Node.js `^24.15.0 || >=26.0.0`.
26
+ Requires Node.js `^24.15.0 || >=26.0.0` and Git on the `PATH`.
30
27
 
31
28
  ## Installation
32
29
 
@@ -53,7 +50,17 @@ const root = await findGitRepositoryRoot();
53
50
  const rootFromPath = await findGitRepositoryRoot('/path/to/subdir');
54
51
  ```
55
52
 
56
- If no Git repository root is found, the function resolves to `undefined`.
53
+ ### Behavior
54
+
55
+ | Directory | Result |
56
+ | ----------------------------------------------- | ---------------------- |
57
+ | Inside a repository | root of the repository |
58
+ | Inside a linked worktree | root of the worktree |
59
+ | Inside a submodule | root of the submodule |
60
+ | Bare repository or `.git` directory | `undefined` |
61
+ | Outside a repository, even with a `.git` folder | `undefined` |
62
+ | Missing path or file | `undefined` |
63
+ | Git cannot be started | rejects (`GitError`) |
57
64
 
58
65
  ## License
59
66
 
@@ -1,9 +1,13 @@
1
1
  /**
2
- * Finds the root directory of a Git repository by searching upwards from the given working directory.
2
+ * Finds the root directory of the Git working tree that contains the given directory.
3
3
  *
4
- * It uses `@simbo/is-git-repository-root` to check if a directory is a Git repository root.
4
+ * Asks Git for the top-level directory with `git rev-parse --show-toplevel`. Inside a linked worktree or a submodule,
5
+ * this is the root of the worktree or submodule. Git resolves symbolic links, so the path is canonical.
5
6
  *
6
- * @param workingDir - The directory from which to start searching for the Git root. Defaults to the current working directory.
7
- * @returns A promise that resolves to the path of the Git repository root, or undefined if not found.
7
+ * @param workingDir - The directory from which to find the root. Defaults to the current working directory.
8
+ * @returns A promise that resolves to the absolute path of the root, or `undefined` if the directory is not inside a
9
+ * working tree, for example outside a repository, in a bare repository or in the `.git` directory, or if it does not
10
+ * exist or is not a directory.
11
+ * @throws {GitError} if Git cannot be started or is terminated.
8
12
  */
9
13
  export declare function findGitRepositoryRoot(workingDir?: string): Promise<string | undefined>;
@@ -1,16 +1,30 @@
1
1
  import { cwd } from 'node:process';
2
- import { isGitRepositoryRoot } from '@simbo/is-git-repository-root';
3
- import { findUp } from 'find-up';
2
+ import { isDirectory } from '@simbo/accessible';
3
+ import { git, GitError } from '@simbo/git-exec';
4
4
  /**
5
- * Finds the root directory of a Git repository by searching upwards from the given working directory.
5
+ * Finds the root directory of the Git working tree that contains the given directory.
6
6
  *
7
- * It uses `@simbo/is-git-repository-root` to check if a directory is a Git repository root.
7
+ * Asks Git for the top-level directory with `git rev-parse --show-toplevel`. Inside a linked worktree or a submodule,
8
+ * this is the root of the worktree or submodule. Git resolves symbolic links, so the path is canonical.
8
9
  *
9
- * @param workingDir - The directory from which to start searching for the Git root. Defaults to the current working directory.
10
- * @returns A promise that resolves to the path of the Git repository root, or undefined if not found.
10
+ * @param workingDir - The directory from which to find the root. Defaults to the current working directory.
11
+ * @returns A promise that resolves to the absolute path of the root, or `undefined` if the directory is not inside a
12
+ * working tree, for example outside a repository, in a bare repository or in the `.git` directory, or if it does not
13
+ * exist or is not a directory.
14
+ * @throws {GitError} if Git cannot be started or is terminated.
11
15
  */
12
16
  export async function findGitRepositoryRoot(workingDir = cwd()) {
13
- const matcher = async (path) => (await isGitRepositoryRoot(path)) ? path : undefined;
14
- return findUp(matcher, { cwd: workingDir, type: 'directory' });
17
+ if (!(await isDirectory(workingDir))) {
18
+ return undefined;
19
+ }
20
+ try {
21
+ return await git(['rev-parse', '--show-toplevel'], { cwd: workingDir });
22
+ }
23
+ catch (error) {
24
+ if (error instanceof GitError && error.exitCode !== undefined) {
25
+ return undefined;
26
+ }
27
+ throw error;
28
+ }
15
29
  }
16
30
  //# sourceMappingURL=find-git-repository-root.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"find-git-repository-root.js","sourceRoot":"","sources":["../src/find-git-repository-root.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAEnC,OAAO,EAAE,mBAAmB,EAAE,MAAM,+BAA+B,CAAC;AACpE,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEjC;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,UAAU,GAAG,GAAG,EAAE;IAC5D,MAAM,OAAO,GAAG,KAAK,EAAE,IAAY,EAA+B,EAAE,CAClE,CAAC,MAAM,mBAAmB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;IACvD,OAAO,MAAM,CAAC,OAAO,EAAE,EAAE,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC,CAAC;AACjE,CAAC","sourcesContent":["import { cwd } from 'node:process';\n\nimport { isGitRepositoryRoot } from '@simbo/is-git-repository-root';\nimport { findUp } from 'find-up';\n\n/**\n * Finds the root directory of a Git repository by searching upwards from the given working directory.\n *\n * It uses `@simbo/is-git-repository-root` to check if a directory is a Git repository root.\n *\n * @param workingDir - The directory from which to start searching for the Git root. Defaults to the current working directory.\n * @returns A promise that resolves to the path of the Git repository root, or undefined if not found.\n */\nexport async function findGitRepositoryRoot(workingDir = cwd()): Promise<string | undefined> {\n const matcher = async (path: string): Promise<string | undefined> =>\n (await isGitRepositoryRoot(path)) ? path : undefined;\n return findUp(matcher, { cwd: workingDir, type: 'directory' });\n}\n"]}
1
+ {"version":3,"file":"find-git-repository-root.js","sourceRoot":"","sources":["../src/find-git-repository-root.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAEnC,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAEhD;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,UAAU,GAAG,GAAG,EAAE;IAC5D,IAAI,CAAC,CAAC,MAAM,WAAW,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC;QACrC,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,IAAI,CAAC;QACH,OAAO,MAAM,GAAG,CAAC,CAAC,WAAW,EAAE,iBAAiB,CAAC,EAAE,EAAE,GAAG,EAAE,UAAU,EAAE,CAAC,CAAC;IAC1E,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,QAAQ,IAAI,KAAK,CAAC,QAAQ,KAAK,SAAS,EAAE,CAAC;YAC9D,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["import { cwd } from 'node:process';\n\nimport { isDirectory } from '@simbo/accessible';\nimport { git, GitError } from '@simbo/git-exec';\n\n/**\n * Finds the root directory of the Git working tree that contains the given directory.\n *\n * Asks Git for the top-level directory with `git rev-parse --show-toplevel`. Inside a linked worktree or a submodule,\n * this is the root of the worktree or submodule. Git resolves symbolic links, so the path is canonical.\n *\n * @param workingDir - The directory from which to find the root. Defaults to the current working directory.\n * @returns A promise that resolves to the absolute path of the root, or `undefined` if the directory is not inside a\n * working tree, for example outside a repository, in a bare repository or in the `.git` directory, or if it does not\n * exist or is not a directory.\n * @throws {GitError} if Git cannot be started or is terminated.\n */\nexport async function findGitRepositoryRoot(workingDir = cwd()): Promise<string | undefined> {\n if (!(await isDirectory(workingDir))) {\n return undefined;\n }\n\n try {\n return await git(['rev-parse', '--show-toplevel'], { cwd: workingDir });\n } catch (error) {\n if (error instanceof GitError && error.exitCode !== undefined) {\n return undefined;\n }\n throw error;\n }\n}\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@simbo/find-git-repository-root",
3
- "description": "A lightweight utility for locating the root directory of a Git repository.",
4
- "version": "3.0.0",
3
+ "description": "A lightweight utility for locating the root directory of a Git working tree, including linked worktrees and submodules.",
4
+ "version": "4.0.0",
5
5
  "keywords": [
6
6
  "git",
7
7
  "repository",
@@ -38,12 +38,13 @@
38
38
  "README.md"
39
39
  ],
40
40
  "dependencies": {
41
- "@simbo/is-git-repository-root": "3.0.0",
42
- "find-up": "^8.0.0"
41
+ "@simbo/accessible": "3.0.0",
42
+ "@simbo/git-exec": "1.0.1"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@simbo/eslint-config": "5.0.0",
46
- "@simbo/tsconfig": "3.0.0",
46
+ "@simbo/git-fixture": "1.0.1",
47
+ "@simbo/tsconfig": "3.0.1",
47
48
  "@types/node": "^24.19.2",
48
49
  "@vitest/coverage-v8": "^5.1.1",
49
50
  "@vitest/ui": "^5.1.1",