@simbo/is-git-repository-root 2.0.1 → 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,56 @@
1
1
  # @simbo/is-git-repository-root
2
2
 
3
+ ## 4.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - 9410189: Detect the root of a Git working tree with
8
+ `git rev-parse --show-toplevel` instead of looking for a `.git` directory.
9
+
10
+ - The roots of linked worktrees and submodules, where `.git` is a file, are
11
+ detected as roots.
12
+ - The given directory and the top-level directory are compared after resolving
13
+ symbolic links.
14
+ - Bare repositories, the `.git` directory, subdirectories, missing paths and
15
+ files still return `false`.
16
+ - A directory outside a Git repository returns `false`, even if it contains a
17
+ `.git` directory.
18
+ - The function rejects with a `GitError` from `@simbo/git-exec` if Git cannot
19
+ be started or is terminated.
20
+ - Replace the dependency on `@simbo/git-constants` with `@simbo/git-exec`.
21
+
22
+ Git must be installed and on the `PATH`. Code that relied on `false` for
23
+ worktree and submodule roots, on `true` for a `.git` directory outside a
24
+ repository, or on the function never rejecting must be adapted; catch the
25
+ `GitError` where Git may be missing.
26
+
27
+ ## 3.0.0
28
+
29
+ ### Major Changes
30
+
31
+ - 19c8dc1: Require Node.js ^24.15.0 || >=26.0.0 across all packages, as Node.js
32
+ 22 reaches its end of life on 2027-04-30.
33
+
34
+ - Remove `^22.22.2` from `engines.node` in every package.
35
+ - Update the requirements in the package READMEs.
36
+
37
+ Node.js 22 is no longer supported. Upgrade to Node.js ^24.15.0 or >=26.0.0
38
+ before updating these packages.
39
+
40
+ ### Patch Changes
41
+
42
+ - 19c8dc1: Point the `repository` field of the manifest to the workspace folder.
43
+
44
+ - `repository` uses the object form with `type`, `url`, and `directory`, so
45
+ npm links the package folder and resolves relative links in the README from
46
+ there.
47
+ - `packages-changelog` gets the field for the first time.
48
+
49
+ - Updated dependencies [19c8dc1]
50
+ - Updated dependencies [19c8dc1]
51
+ - @simbo/accessible@3.0.0
52
+ - @simbo/git-constants@3.0.0
53
+
3
54
  ## 2.0.1
4
55
 
5
56
  ### Patch 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 `^22.22.2 || ^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": "2.0.1",
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.0",
5
5
  "keywords": [
6
6
  "git",
7
7
  "repository",
@@ -20,11 +20,12 @@
20
20
  "homepage": "https://github.com/simbo/packages/tree/main/packages/is-git-repository-root",
21
21
  "repository": {
22
22
  "type": "git",
23
- "url": "github:simbo/packages"
23
+ "url": "git+https://github.com/simbo/packages.git",
24
+ "directory": "packages/is-git-repository-root"
24
25
  },
25
26
  "bugs": "https://github.com/simbo/packages/issues",
26
27
  "engines": {
27
- "node": "^22.22.2 || ^24.15.0 || >=26.0.0"
28
+ "node": "^24.15.0 || >=26.0.0"
28
29
  },
29
30
  "type": "module",
30
31
  "exports": {
@@ -37,19 +38,20 @@
37
38
  "README.md"
38
39
  ],
39
40
  "dependencies": {
40
- "@simbo/accessible": "2.0.1",
41
- "@simbo/git-constants": "2.0.1"
41
+ "@simbo/accessible": "3.0.0",
42
+ "@simbo/git-exec": "1.0.1"
42
43
  },
43
44
  "devDependencies": {
44
- "@simbo/eslint-config": "3.3.0",
45
- "@simbo/tsconfig": "2.1.0",
45
+ "@simbo/eslint-config": "5.0.0",
46
+ "@simbo/git-fixture": "1.0.1",
47
+ "@simbo/tsconfig": "3.0.1",
46
48
  "@types/node": "^24.19.2",
47
- "@vitest/coverage-v8": "^5.1.0",
48
- "@vitest/ui": "^5.1.0",
49
- "eslint": "^10.14.0",
49
+ "@vitest/coverage-v8": "^5.1.1",
50
+ "@vitest/ui": "^5.1.1",
51
+ "eslint": "^10.15.0",
50
52
  "jiti": "^2.7.0",
51
53
  "typescript": "^6.0.3",
52
- "vitest": "^5.1.0"
54
+ "vitest": "^5.1.1"
53
55
  },
54
56
  "scripts": {
55
57
  "build": "tsc --project tsconfig.build.json",