@simbo/is-git-repository-root 3.0.0 → 4.0.1

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,36 @@
1
1
  # @simbo/is-git-repository-root
2
2
 
3
+ ## 4.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [c017ffe]
8
+ - @simbo/git-exec@1.0.2
9
+
10
+ ## 4.0.0
11
+
12
+ ### Major Changes
13
+
14
+ - 9410189: Detect the root of a Git working tree with
15
+ `git rev-parse --show-toplevel` instead of looking for a `.git` directory.
16
+
17
+ - The roots of linked worktrees and submodules, where `.git` is a file, are
18
+ detected as roots.
19
+ - The given directory and the top-level directory are compared after resolving
20
+ symbolic links.
21
+ - Bare repositories, the `.git` directory, subdirectories, missing paths and
22
+ files still return `false`.
23
+ - A directory outside a Git repository returns `false`, even if it contains a
24
+ `.git` directory.
25
+ - The function rejects with a `GitError` from `@simbo/git-exec` if Git cannot
26
+ be started or is terminated.
27
+ - Replace the dependency on `@simbo/git-constants` with `@simbo/git-exec`.
28
+
29
+ Git must be installed and on the `PATH`. Code that relied on `false` for
30
+ worktree and submodule roots, on `true` for a `.git` directory outside a
31
+ repository, or on the function never rejecting must be adapted; catch the
32
+ `GitError` where Git may be missing.
33
+
3
34
  ## 3.0.0
4
35
 
5
36
  ### Major Changes
package/README.md CHANGED
@@ -4,13 +4,16 @@
4
4
  [**`@simbo/is-git-repository-root`**](https://npmjs.com/package/@simbo/is-git-repository-root)
5
5
 
6
6
  A lightweight utility for checking whether a directory is the root of a Git
7
- repository.
7
+ working tree, including linked worktrees and submodules.
8
8
 
9
- It simply checks for the presence of a `.git` folder in the given directory.
9
+ It asks Git for the top-level directory with `git rev-parse --show-toplevel` and
10
+ compares it with the given directory after resolving symbolic links in both
11
+ paths.
10
12
 
11
13
  ## Features
12
14
 
13
- - Detects if a directory is the root of a Git repository
15
+ - Detects if a directory is the root of a Git working tree, including linked
16
+ worktrees and submodules
14
17
 
15
18
  - Defaults to checking the **current working directory**
16
19
 
@@ -20,11 +23,9 @@ It simply checks for the presence of a `.git` folder in the given directory.
20
23
 
21
24
  - Zero dependencies (besides other `@simbo/*` packages)
22
25
 
23
- ⚠️ Git submodules and worktrees are not considered repository roots.
24
-
25
26
  ## Requirements
26
27
 
27
- Requires Node.js `^24.15.0 || >=26.0.0`.
28
+ Requires Node.js `^24.15.0 || >=26.0.0` and Git on the `PATH`.
28
29
 
29
30
  ## Installation
30
31
 
@@ -53,6 +54,18 @@ if (await isGitRepositoryRoot('/path/to/dir')) {
53
54
  }
54
55
  ```
55
56
 
57
+ ### Behavior
58
+
59
+ | Directory | Result |
60
+ | ----------------------------------------------- | -------------------- |
61
+ | Root of a repository, worktree or submodule | `true` |
62
+ | Symbolic link to such a root | `true` |
63
+ | Subdirectory of a working tree | `false` |
64
+ | Bare repository or `.git` directory | `false` |
65
+ | Outside a repository, even with a `.git` folder | `false` |
66
+ | Missing path or file | `false` |
67
+ | Git cannot be started | rejects (`GitError`) |
68
+
56
69
  ## License
57
70
 
58
71
  [MIT © Simon Lepel](https://simbo.mit-license.org/2025/)
@@ -1,11 +1,13 @@
1
1
  /**
2
- * Checks if the given directory is the root of a Git repository.
2
+ * Checks if the given directory is the root of a Git working tree.
3
3
  *
4
- * Only succeeds on directories that contain a `.git` folder.
4
+ * Asks Git for the top-level directory of the working tree with `git rev-parse --show-toplevel` and compares it with
5
+ * the given directory after resolving symbolic links in both paths. The roots of linked worktrees and submodules
6
+ * count as roots; bare repositories, the `.git` directory and directories outside a repository do not.
5
7
  *
6
- * Git submodules or worktrees are not considered as roots.
7
- *
8
- * @param workingDir - The directory in which to check for the Git root. Defaults to the current working directory.
9
- * @returns A promise that resolves to true if the directory is a Git root, false otherwise.
8
+ * @param workingDir - The directory to check. Defaults to the current working directory.
9
+ * @returns A promise that resolves to `true` if the directory is the root of a Git working tree, `false` otherwise,
10
+ * also if it does not exist or is not a directory.
11
+ * @throws {GitError} if Git cannot be started or is terminated.
10
12
  */
11
13
  export declare function isGitRepositoryRoot(workingDir?: string): Promise<boolean>;
@@ -1,24 +1,33 @@
1
- import { resolve } from 'node:path';
1
+ import { realpath } from 'node:fs/promises';
2
2
  import { cwd } from 'node:process';
3
3
  import { isDirectory } from '@simbo/accessible';
4
- import { GIT_FOLDER } from '@simbo/git-constants';
4
+ import { git, GitError } from '@simbo/git-exec';
5
5
  /**
6
- * Checks if the given directory is the root of a Git repository.
6
+ * Checks if the given directory is the root of a Git working tree.
7
7
  *
8
- * Only succeeds on directories that contain a `.git` folder.
8
+ * Asks Git for the top-level directory of the working tree with `git rev-parse --show-toplevel` and compares it with
9
+ * the given directory after resolving symbolic links in both paths. The roots of linked worktrees and submodules
10
+ * count as roots; bare repositories, the `.git` directory and directories outside a repository do not.
9
11
  *
10
- * Git submodules or worktrees are not considered as roots.
11
- *
12
- * @param workingDir - The directory in which to check for the Git root. Defaults to the current working directory.
13
- * @returns A promise that resolves to true if the directory is a Git root, false otherwise.
12
+ * @param workingDir - The directory to check. Defaults to the current working directory.
13
+ * @returns A promise that resolves to `true` if the directory is the root of a Git working tree, `false` otherwise,
14
+ * also if it does not exist or is not a directory.
15
+ * @throws {GitError} if Git cannot be started or is terminated.
14
16
  */
15
17
  export async function isGitRepositoryRoot(workingDir = cwd()) {
18
+ if (!(await isDirectory(workingDir))) {
19
+ return false;
20
+ }
21
+ const directory = await realpath(workingDir);
16
22
  try {
17
- const path = resolve(workingDir, GIT_FOLDER);
18
- return await isDirectory(path);
23
+ const topLevel = await git(['rev-parse', '--show-toplevel'], { cwd: directory });
24
+ return (await realpath(topLevel)) === directory;
19
25
  }
20
- catch {
21
- return false;
26
+ catch (error) {
27
+ if (error instanceof GitError && error.exitCode !== undefined) {
28
+ return false;
29
+ }
30
+ throw error;
22
31
  }
23
32
  }
24
33
  //# sourceMappingURL=is-git-repository-root.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"is-git-repository-root.js","sourceRoot":"","sources":["../src/is-git-repository-root.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAEnC,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAElD;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAC,UAAU,GAAG,GAAG,EAAE;IAC1D,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,OAAO,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;QAC7C,OAAO,MAAM,WAAW,CAAC,IAAI,CAAC,CAAC;IACjC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC","sourcesContent":["import { resolve } from 'node:path';\nimport { cwd } from 'node:process';\n\nimport { isDirectory } from '@simbo/accessible';\nimport { GIT_FOLDER } from '@simbo/git-constants';\n\n/**\n * Checks if the given directory is the root of a Git repository.\n *\n * Only succeeds on directories that contain a `.git` folder.\n *\n * Git submodules or worktrees are not considered as roots.\n *\n * @param workingDir - The directory in which to check for the Git root. Defaults to the current working directory.\n * @returns A promise that resolves to true if the directory is a Git root, false otherwise.\n */\nexport async function isGitRepositoryRoot(workingDir = cwd()): Promise<boolean> {\n try {\n const path = resolve(workingDir, GIT_FOLDER);\n return await isDirectory(path);\n } catch {\n return false;\n }\n}\n"]}
1
+ {"version":3,"file":"is-git-repository-root.js","sourceRoot":"","sources":["../src/is-git-repository-root.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,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,mBAAmB,CAAC,UAAU,GAAG,GAAG,EAAE;IAC1D,IAAI,CAAC,CAAC,MAAM,WAAW,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC;QACrC,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,SAAS,GAAG,MAAM,QAAQ,CAAC,UAAU,CAAC,CAAC;IAE7C,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,MAAM,GAAG,CAAC,CAAC,WAAW,EAAE,iBAAiB,CAAC,EAAE,EAAE,GAAG,EAAE,SAAS,EAAE,CAAC,CAAC;QACjF,OAAO,CAAC,MAAM,QAAQ,CAAC,QAAQ,CAAC,CAAC,KAAK,SAAS,CAAC;IAClD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,QAAQ,IAAI,KAAK,CAAC,QAAQ,KAAK,SAAS,EAAE,CAAC;YAC9D,OAAO,KAAK,CAAC;QACf,CAAC;QACD,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["import { realpath } from 'node:fs/promises';\nimport { cwd } from 'node:process';\n\nimport { isDirectory } from '@simbo/accessible';\nimport { git, GitError } from '@simbo/git-exec';\n\n/**\n * Checks if the given directory is the root of a Git working tree.\n *\n * Asks Git for the top-level directory of the working tree with `git rev-parse --show-toplevel` and compares it with\n * the given directory after resolving symbolic links in both paths. The roots of linked worktrees and submodules\n * count as roots; bare repositories, the `.git` directory and directories outside a repository do not.\n *\n * @param workingDir - The directory to check. Defaults to the current working directory.\n * @returns A promise that resolves to `true` if the directory is the root of a Git working tree, `false` otherwise,\n * also if it does not exist or is not a directory.\n * @throws {GitError} if Git cannot be started or is terminated.\n */\nexport async function isGitRepositoryRoot(workingDir = cwd()): Promise<boolean> {\n if (!(await isDirectory(workingDir))) {\n return false;\n }\n\n const directory = await realpath(workingDir);\n\n try {\n const topLevel = await git(['rev-parse', '--show-toplevel'], { cwd: directory });\n return (await realpath(topLevel)) === directory;\n } catch (error) {\n if (error instanceof GitError && error.exitCode !== undefined) {\n return false;\n }\n throw error;\n }\n}\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@simbo/is-git-repository-root",
3
- "description": "A lightweight utility for checking whether a directory is the root of a Git repository.",
4
- "version": "3.0.0",
3
+ "description": "A lightweight utility for checking whether a directory is the root of a Git working tree, including linked worktrees and submodules.",
4
+ "version": "4.0.1",
5
5
  "keywords": [
6
6
  "git",
7
7
  "repository",
@@ -39,11 +39,12 @@
39
39
  ],
40
40
  "dependencies": {
41
41
  "@simbo/accessible": "3.0.0",
42
- "@simbo/git-constants": "3.0.0"
42
+ "@simbo/git-exec": "1.0.2"
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.2",
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",